1/*
2    This file is part of Waiho (http://info.xdev.org/projets/waiho)
3    Copyright (C) 2001-2002 Nicolas Roard (nicolas@roard.com)
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#import "waiho.h"
21
22@implementation MyDelegate : NSObject
23
24- (void) getParametersOfConnection: (id) sender {
25    [self countBookmarks];
26    params = AUTORELEASE([[ParametersWindowController alloc] init]);
27    [myWindow setDialing];
28    [params showWindow: self];
29}
30- connectWithAddress: (NSString*) adr withLogin: (NSString*) login withPassword: (NSString*) password {
31
32    FTPInterface = [[FTP alloc] initWithAddress: adr withLogin: login withPassword: password];
33    DataTransfertIn = [[FTP alloc] initWithAddress: adr withLogin: login withPassword: password];
34    DataTransfertOut = [[FTP alloc] initWithAddress: adr withLogin: login withPassword: password];
35
36    if ([FTPInterface isConnected]) [myWindow setConnected];
37
38    [DistributedCenter addObserver: self
39	selector: @selector (receive:)
40	name: @"ChunkOfFile"
41	object: @"DataTransfert"];
42    [DistributedCenter addObserver: self
43	selector: @selector (send:)
44	name: @"ChunkOfFileUploaded"
45	object: @"DataTransfert"];
46    [DistributedCenter addObserver: self
47	selector: @selector (FileSaved:)
48	name: @"FileSaved"
49	object: @"DataTransfert"];
50    [DistributedCenter addObserver: self
51	selector: @selector (FileSend:)
52	name: @"FileSend"
53	object: @"DataTransfert"];
54    return self;
55}
56- (void) disconnectRemote: (id) sender {
57	NSLog (@"disconnectRemote");
58	[myWindow setDisconnected];
59	[downloadQueue dealloc];
60	[uploadQueue dealloc];
61        downloadQueue = [[NSMutableArray alloc] init];
62        uploadQueue = [[NSMutableArray alloc] init];
63        [myWindow refreshUploadTable];
64        [myWindow refreshDownloadTable];
65}
66- (void) getNameDir: (id) sender {
67    namedir = AUTORELEASE([[GetNameDirController alloc] init]);
68    [namedir showWindow: self];
69}
70- (void) disconnectFromFTP: (id) sender {
71    NSLog (@"On se deconnecte\n");
72    [FTPInterface shutdown];
73    [DataTransfertIn shutdown];
74    [DataTransfertOut shutdown];
75    [myWindow setDisconnected];
76    [downloadQueue dealloc];
77    [uploadQueue dealloc];
78    downloadQueue = [[NSMutableArray alloc] init];
79    uploadQueue = [[NSMutableArray alloc] init];
80    [myWindow refreshUploadTable];
81    [myWindow refreshDownloadTable];
82    NSLog (@"FTP closed\n");
83}
84
85- (void) getFile: (id) sender {
86    int i;
87    NSArray* rfiles = [myWindow remoteViewSelectedCells];
88
89    NSLog (@"remote path : %@\n", [myWindow remotePath]);
90    NSLog (@"local path : %@\n", [myWindow localPath]);
91    NSLog (@"rfiles count : %i", [rfiles count]);
92
93    for (i = 0; i < [rfiles count]; i++)
94    {
95	File* f = AUTORELEASE ([(File*)[File alloc] initWithFile: [[rfiles objectAtIndex: i] getFile]]);
96
97	[f setRemotePath: [myWindow remotePath]];
98	[f setLocalPath: [myWindow localPath]];
99	NSLog (@"on a mis dans le fichier %@ (%i) le localpath : %@", [f name], [f size], [f localpath]);
100	[f setID: [downloadQueue count]];
101	[downloadQueue addObject: f];
102    }
103    for (i = 0; i < [downloadQueue count]; i++)
104    {
105	File* f = [downloadQueue objectAtIndex: i];
106	NSLog (@"fichier %i : <%i><%@>", i, [f getID], [f name]);
107    }
108    [myWindow refreshDownloadTable];
109    [self downloadFiles];
110}
111- (void) putFile: (id) sender {
112    int i;
113    NSArray* lfiles = [myWindow localViewSelectedCells];
114
115    NSLog (@"remote path : %@\n", [myWindow remotePath]);
116    NSLog (@"local path : %@\n", [myWindow localPath]);
117    NSLog (@"lfiles count : %i", [lfiles count]);
118
119    NSLog (@"A uploadQueue count : %i", [uploadQueue count]);
120    for (i = 0; i < [lfiles count]; i++)
121    {
122	//File* f = [[lfiles objectAtIndex: i] getFile];
123	File* f = AUTORELEASE ([(File*)[File alloc] initWithFile: [[lfiles objectAtIndex: i] getFile]]);
124	[f setRemotePath: [myWindow remotePath]];
125	NSLog (@"[%i] on a mis dans le fichier %@ le remotepath : %@ (%i) ", i, [f name], [f remotepath], [f size]);
126	[f setID: [uploadQueue count]];
127	[uploadQueue addObject: f];
128    }
129    NSLog (@"B uploadQueue count : %i", [uploadQueue count]);
130    for (i = 0; i < [uploadQueue count]; i++)
131    {
132	File* f = [uploadQueue objectAtIndex: i];
133	NSLog (@"*** fichier %i : <%i> <%@>", i, [f getID], [f name]);
134    }
135    [myWindow refreshUploadTable];
136    [self uploadFiles];
137}
138- (void) createMenu {
139    NSMenu *menu;
140    NSMenu *infoMenu;
141    NSMenuItem* menuItem;
142
143    menu = AUTORELEASE ([NSMenu new]);
144    infoMenu = AUTORELEASE ([NSMenu new]);
145
146    [infoMenu addItemWithTitle: @"Info Panel ..."
147     action: @selector (orderFrontStandardInfoPanel:)
148     keyEquivalent: @""];
149
150    [infoMenu addItemWithTitle: @"Help..."
151     action: @selector (orderFrontHelpPanel:)
152     keyEquivalent: @"?"];
153
154    menuItem = [menu addItemWithTitle: @"Info.."
155                action: NULL
156                keyEquivalent: @""];
157
158    [menu addItemWithTitle: @"Quit"
159     action: @selector (terminate:)
160     keyEquivalent: @"q"];
161
162    [menu setSubmenu: infoMenu forItem: menuItem];
163
164    [NSApp setMainMenu: menu];
165}
166- (MainWindowController*) window {
167    return myWindow;
168}
169
170- (void) downloadFiles {
171    int i, ret;
172    for (i = 0; i < [downloadQueue count]; i++)
173    {
174	File* f = [downloadQueue objectAtIndex: i];
175
176	NSLog (@"DOWNLOADFILES : [%@] <%@> de <%@> a <%@>", [f getStatus], [f name], [f remotepath], [f localpath]);
177
178	if (![f isDone]) // si le fichier n'est pas telecharge
179	{
180	    if (![f isCurrent]) // si on a pas encore fait la demande de telechargement
181	    {
182		NSMutableString* filename = [NSMutableString stringWithString: [f localpath]];
183		NSMutableString* path = [NSMutableString stringWithString: [f remotepath]];
184
185		[filename appendString: @"/"];
186		[filename appendString: [f name]];
187		[path appendString: @"/"];
188		[path appendString: [f name]];
189
190		[f setCurrent: YES];
191
192		NSLog (@"DOWNLOAD fichier path : %@ dans : %@", path, filename);
193		ret = [DataTransfertIn get: path withSavedName: filename withID: [f getID]];
194		if (ret >= 400) // erreur
195		{
196		    [f setError];
197		}
198		[myWindow refreshDownloadTable];
199		return;
200	    }
201	    else return; // si on a trouve un fichier en cours de telechargement, on arrete les frais
202	}
203	else // s'il est telecharge, on enleve l'etat "current"
204	{
205	    [f setCurrent: NO];
206	}
207    }
208}
209- (void) uploadFiles {
210    int i, ret;
211    for (i = 0; i < [uploadQueue count]; i++)
212    {
213	File* f = [uploadQueue objectAtIndex: i];
214
215	NSLog (@"UPLOADFILES : [%@] <%@> de <%@> a <%@>", [f getStatus], [f name], [f localpath], [f remotepath]);
216
217	if (![f isDone]) // si le fichier n'est pas telecharge
218	{
219	    if (![f isCurrent]) // si on a pas encore fait la demande de telechargement
220	    {
221		NSMutableString* filename = [NSMutableString stringWithString: [f localpath]];
222		NSMutableString* path = [NSMutableString stringWithString: [f remotepath]];
223		[filename appendString: @"/"];
224		[filename appendString: [f name]];
225		[path appendString: @"/"];
226		[path appendString: [f name]];
227		[f setCurrent: YES];
228
229		NSLog (@"UPLOAD fichier path : %@ vers : %@ ", filename, path);
230		ret = [DataTransfertOut put: filename withSavedName: path withID: [f getID]];
231		if (ret >= 400) // erreur
232		{
233		    [f setError];
234		}
235		[myWindow refreshUploadTable];
236		return;
237	    }
238	    else return; // si on a trouve un fichier en cours de telechargement, on arrete les frais
239	}
240	else // s'il est telecharge, on enleve l'etat "current"
241	{
242	    [f setCurrent: NO];
243	}
244    }
245}
246
247- (void) FileSaved: (NSNotification*) notification {
248    int ID;
249    NSDictionary * info;
250
251    NSLog (@"Notification de fichier sauvegarde !");
252    info = [notification userInfo];
253    ID = [[info objectForKey: @"ID"] intValue];
254    NSLog (@"fichier : %i", ID);
255
256    if (ID < [downloadQueue count])
257    {
258	File* f = [downloadQueue objectAtIndex: ID];
259	[f setDone];
260    }
261    [myWindow refreshDownloadTable];
262    [myWindow refreshLocal];
263    [self downloadFiles];
264}
265
266- (void) FileSend: (NSNotification*) notification {
267    int ID;
268    NSDictionary * info;
269    NSLog (@"Notification de fichier envoye !");
270
271    info = [notification userInfo];
272    ID = [[info objectForKey: @"ID"] intValue];
273    NSLog (@"fichier : %i", ID);
274
275    if (ID < [uploadQueue count])
276    {
277	File* f = [uploadQueue objectAtIndex: ID];
278	[f setDone];
279    }
280
281    [myWindow refreshUploadTable];
282    [myWindow refreshRemote];
283    [self uploadFiles];
284}
285
286- (void) receive: (NSNotification*) notification {
287    NSDictionary* userInfo;
288    int size = 0;
289    int i;
290
291    userInfo = [notification userInfo];
292    size = [[userInfo objectForKey: @"size"] intValue];
293
294    if (size > 0)
295    {
296	for (i = 0; i < [downloadQueue count]; i++)
297	{
298	    File* f = [downloadQueue objectAtIndex: i];
299
300	    NSLog (@"RECEIVE <%@> <%@> <size: %i> <isDone: %i> <isCurrent: %i>",
301		    [f name], [f getStatus], [f currentsize], [f isDone], [f isCurrent]);
302
303	    if (![f isDone])
304	    {
305		if ([f isCurrent])
306		{
307		    [f addToSize: size];
308		    [myWindow refreshDownloadTable];
309		    return;
310		}
311	    }
312	}
313    }
314}
315- (void) send: (NSNotification*) notification {
316    NSDictionary* userInfo;
317    int size = 0;
318    int i;
319
320//    NSLog (@"Notification dans le App");
321
322    userInfo = [notification userInfo];
323    size = [[userInfo objectForKey: @"size"] intValue];
324
325    if (size > 0)
326    {
327	for (i = 0; i < [uploadQueue count]; i++)
328	{
329	    File* f = [uploadQueue objectAtIndex: i];
330
331	    NSLog (@"SEND <%@> <%@> <size: %i / %i> <isDone: %i> <isCurrent: %i>",
332		    [f name], [f getStatus], [f currentsize], [f size], [f isDone], [f isCurrent]);
333
334	    if (![f isDone])
335	    {
336//		NSLog (@"Traitement de %@", [f name]);
337		if ([f isCurrent])
338		{
339		    [f addToSize: size];
340		    [myWindow refreshUploadTable];
341		    return;
342		}
343	    }
344	}
345    }
346}
347
348- (FTP*) ftp {
349    return FTPInterface;
350}
351- (NSArray*) downloadQueue {
352    return downloadQueue;
353}
354- (NSArray*) uploadQueue {
355    return uploadQueue;
356}
357- (Sites*) bookmarks {
358    return bookmarks;
359}
360- (void) countBookmarks {
361    NSLog (@"on compte les bookmarks...\n");
362    NSLog (@"on a %d bookmarks.\n", [bookmarks count]);
363}
364
365- (void) dealloc {
366    RELEASE (myWindow);
367    RELEASE (downloadQueue);
368    RELEASE (uploadQueue);
369    //RELEASE (bookmarks);
370}
371- (void) applicationWillFinishLaunching: (NSNotification *) not {
372    //Bookmark* testbookmark = [[Bookmark alloc] initWithSite: @"127.0.0.1" withLogin: @"nico" withPassword: @"xkbmdu"];
373    //Bookmark* testbookmark2 = [[Bookmark alloc] initWithAnonymousSite: @"ftp.lip6.fr"];
374    //NSLog (@"bkm 1 : %@ (l/p : %@/%@)\n", [testbookmark site], [testbookmark login], [testbookmark password]);
375    //NSLog (@"bkm 2 : %@ (l/p : %@/%@)\n", [testbookmark2 site], [testbookmark2 login], [testbookmark2 password]);
376    //[bookmarks addSite: testbookmark];
377    //[bookmarks addSite: testbookmark2];
378    //[bookmarks writeFile: @"~/bookmarks.waiho"];
379    //printf ("On a �crit le fichier de bookmarks !\n");
380
381    [self createMenu];
382    myWindow = [[MainWindowController alloc] init];
383    NSLog (@"tudieu");
384    downloadQueue = [[NSMutableArray alloc] init];
385    uploadQueue = [[NSMutableArray alloc] init];
386    //center = [NSNotificationCenter defaultCenter];
387    DistributedCenter = [NSDistributedNotificationCenter defaultCenter];
388    NSLog (@"on arrive");
389    bookmarks = [(Sites*)[Sites alloc] initWithFile: @"~/bookmarks.waiho"];
390    [self countBookmarks];
391}
392- (void) applicationDidFinishLaunching: (NSNotification *) not {
393    int choice;
394    [[myWindow window] makeKeyAndOrderFront: nil];
395    [self countBookmarks];
396}
397
398@end
399
400