root/WiredClient/trunk/WCConnectionController.m

Revision 5621, 11.7 kB (checked in by morris, 3 weeks ago)

Remove unused, add -dealloc, etc

  • Property svn:keywords set to "Id"
Line 
1 /* $Id$ */
2
3 /*
4  *  Copyright (c) 2003-2007 Axel Andersson
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #import "WCConnectionController.h"
30 #import "WCFile.h"
31 #import "WCFiles.h"
32 #import "WCKeychain.h"
33 #import "WCPreferences.h"
34 #import "WCSettings.h"
35
36 @interface WCConnectionController(Private)
37
38 - (void)_loadWindowTemplate;
39 - (void)_saveWindowTemplate;
40
41 @end
42
43
44 @implementation WCConnectionController(Private)
45
46 - (void)_loadWindowTemplate {
47         NSDictionary    *windowTemplate;
48        
49         _identifier = [[[self connection] identifier] retain];
50        
51         windowTemplate = [WCSettings windowTemplateForKey:_identifier];
52        
53         if(!windowTemplate)
54                 windowTemplate = [WCSettings windowTemplateForKey:WCWindowTemplatesDefault];
55        
56         if(windowTemplate)
57                 _windowTemplate = [[windowTemplate objectForKey:[self windowNibName]] mutableCopy];
58
59         if(!_windowTemplate)
60                 _windowTemplate = [[NSMutableDictionary alloc] init];
61        
62         [self windowTemplateShouldLoad:_windowTemplate];
63 }
64
65
66
67 - (void)_saveWindowTemplate {
68         NSMutableDictionary             *windowTemplate;
69        
70         if(_windowTemplate) {
71                 [self windowTemplateShouldSave:_windowTemplate];
72                
73                 windowTemplate = [[WCSettings windowTemplateForKey:_identifier] mutableCopy];
74                
75                 if(!windowTemplate)
76                         windowTemplate = [[NSMutableDictionary alloc] init];
77                
78                 [windowTemplate setObject:_windowTemplate forKey:[self windowNibName]];
79                
80                 [WCSettings setWindowTemplate:windowTemplate forKey:_identifier];
81                
82                 [windowTemplate release];
83         }
84 }
85
86
87
88 #pragma mark -
89
90 - (void)_WC_applicationWillTerminate:(NSNotification *)notification {
91         [self _saveWindowTemplate];
92 }
93
94
95
96 - (void)_WC_windowWillClose:(NSNotification *)notification {
97         if([self isReleasedWhenClosed] && [notification object] == [self window]) {
98                 [self _saveWindowTemplate];
99                
100                 [self autorelease];
101         }
102 }
103
104
105
106 - (void)_serverConnectionShouldLoadWindowTemplate:(NSNotification *)notification {
107         [self _loadWindowTemplate];
108 }
109
110
111
112 - (void)_serverConnectionShouldSaveWindowTemplate:(NSNotification *)notification {
113         [self _saveWindowTemplate];
114 }
115
116
117
118
119 - (void)_disconnectSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
120         if(returnCode == NSAlertDefaultReturn)
121                 [[self connection] disconnect];
122 }
123
124 @end
125
126
127 @implementation WCConnectionController
128
129 - (id)initWithWindowNibName:(NSString *)nibName connection:(WCServerConnection *)connection {
130         return [self initWithWindowNibName:nibName name:NULL connection:connection];
131 }
132
133
134
135 - (id)initWithWindowNibName:(NSString *)nibName name:(NSString *)name connection:(WCServerConnection *)connection {
136         self = [super initWithWindowNibName:nibName];
137
138         _name = [name retain];
139         _connection = connection;
140        
141         [[NSNotificationCenter defaultCenter]
142                 addObserver:self
143                    selector:@selector(_WC_applicationWillTerminate:)
144                            name:NSApplicationWillTerminateNotification];
145
146         [_connection addObserver:self
147                                         selector:@selector(connectionWillTerminate:)
148                                                 name:WCConnectionWillTerminate];
149
150         [_connection addObserver:self
151                                         selector:@selector(connectionDidTerminate:)
152                                                 name:WCConnectionDidTerminate];
153
154         [_connection addObserver:self
155                                         selector:@selector(serverConnectionShouldHide:)
156                                                 name:WCServerConnectionShouldHide];
157        
158         [_connection addObserver:self
159                                         selector:@selector(serverConnectionShouldUnhide:)
160                                                 name:WCServerConnectionShouldUnhide];
161
162         [_connection addObserver:self
163                                         selector:@selector(_serverConnectionShouldLoadWindowTemplate:)
164                                                 name:WCServerConnectionShouldLoadWindowTemplate];
165
166         [_connection addObserver:self
167                                         selector:@selector(_serverConnectionShouldSaveWindowTemplate:)
168                                                 name:WCServerConnectionShouldSaveWindowTemplate];
169
170         if([self respondsToSelector:@selector(connectionDidConnect:)]) {
171                 [_connection addObserver:self
172                                                 selector:@selector(connectionDidConnect:)
173                                                         name:WCConnectionDidConnect];
174         }
175
176         if([self respondsToSelector:@selector(connectionDidClose:)]) {
177                 [_connection addObserver:self
178                                                 selector:@selector(connectionDidClose:)
179                                                         name:WCConnectionDidClose];
180         }
181        
182         if([self respondsToSelector:@selector(serverConnectionLoggedIn:)]) {
183                 [_connection addObserver:self
184                                                 selector:@selector(serverConnectionLoggedIn:)
185                                                         name:WCServerConnectionLoggedIn];
186         }
187        
188         if([self respondsToSelector:@selector(serverConnectionWillReconnect:)]) {
189                 [_connection addObserver:self
190                                                 selector:@selector(serverConnectionWillReconnect:)
191                                                         name:WCServerConnectionWillReconnect];
192         }
193        
194         if([self respondsToSelector:@selector(serverConnectionServerInfoDidChange:)]) {
195                 [_connection addObserver:self
196                                                 selector:@selector(serverConnectionServerInfoDidChange:)
197                                                         name:WCServerConnectionServerInfoDidChange];
198         }
199
200         if([self respondsToSelector:@selector(serverConnectionBannerDidChange:)]) {
201                 [_connection addObserver:self
202                                                 selector:@selector(serverConnectionBannerDidChange:)
203                                                         name:WCServerConnectionBannerDidChange];
204         }
205
206         if([self respondsToSelector:@selector(serverConnectionPrivilegesDidChange:)]) {
207                 [_connection addObserver:self
208                                                 selector:@selector(serverConnectionPrivilegesDidChange:)
209                                                         name:WCServerConnectionPrivilegesDidChange];
210         }
211
212         return self;
213 }
214
215
216
217 - (void)dealloc {
218         [[NSNotificationCenter defaultCenter] removeObserver:self];
219        
220         [_name release];
221         [_identifier release];
222
223         [_connection removeObserver:self];
224         _connection = NULL;
225        
226         [_windowTemplate release];
227
228         [super dealloc];
229 }
230
231
232
233 #pragma mark -
234
235 - (void)windowDidLoad {
236         [[NSNotificationCenter defaultCenter]
237                 addObserver:self
238                    selector:@selector(_WC_windowWillClose:)
239                            name:NSWindowWillCloseNotification];
240 }
241
242
243
244 - (void)windowTemplateShouldLoad:(NSMutableDictionary *)windowTemplate {
245 }
246
247
248
249 - (void)windowTemplateShouldSave:(NSMutableDictionary *)windowTemplate {
250 }
251
252
253
254 - (void)connectionWillTerminate:(NSNotification *)notification {
255         [self _saveWindowTemplate];
256 }
257
258
259
260 - (void)connectionDidTerminate:(NSNotification *)notification {
261         [_connection removeObserver:self];
262         _connection = NULL;
263 }
264
265
266
267 - (void)serverConnectionShouldHide:(NSNotification *)notification {
268         NSWindow        *sheet;
269                
270         _wasVisible = [[self window] isVisible];
271         _hidden = YES;
272        
273         if(_wasVisible) {
274                 sheet = [[self window] attachedSheet];
275                
276                 if(sheet)
277                         [NSApp endSheet:sheet returnCode:NSAlertAlternateReturn];
278                
279                 [[self window] orderOut:self];
280         }
281 }
282
283
284
285 - (void)serverConnectionShouldUnhide:(NSNotification *)notification {
286         if(_wasVisible)
287                 [[self window] orderFront:self];
288                
289         _hidden = NO;
290 }
291
292
293
294 #pragma mark -
295
296 - (void)setName:(NSString *)name {
297         [name retain];
298         [_name release];
299
300         _name = name;
301 }
302
303
304
305 - (NSString *)name {
306         return _name;
307 }
308
309
310
311 - (void)setConnection:(WCServerConnection *)connection {
312         [connection retain];
313         [_connection release];
314
315         _connection = connection;
316 }
317
318
319
320 - (WCServerConnection *)connection {
321         return _connection;
322 }
323
324
325
326 - (void)setReleasedWhenClosed:(BOOL)value {
327         _releasedWhenClosed = value;
328 }
329
330
331
332 - (BOOL)isReleasedWhenClosed {
333         return _releasedWhenClosed;
334 }
335
336
337
338 - (NSDictionary *)windowTemplate {
339         if(!_windowTemplate)
340                 [self _loadWindowTemplate];
341        
342         return _windowTemplate;
343 }
344
345
346
347 - (BOOL)isHidden {
348         return _hidden;
349 }
350
351
352
353 #pragma mark -
354
355 - (BOOL)beginConfirmDisconnectSheetModalForWindow:(NSWindow *)window modalDelegate:(id)delegate didEndSelector:(SEL)selector contextInfo:(void *)contextInfo {
356         if([WCSettings boolForKey:WCConfirmDisconnect] && [[self connection] isConnected]) {
357                 NSBeginAlertSheet(NSLS(@"Are you sure you want to disconnect?", @"Disconnect dialog title"),
358                                                   NSLS(@"Disconnect", @"Disconnect dialog button"),
359                                                   NSLS(@"Cancel", @"Disconnect dialog button title"),
360                                                   NULL,
361                                                   window,
362                                                   delegate,
363                                                   selector,
364                                                   NULL,
365                                                   contextInfo,
366                                                   NSLS(@"Disconnecting will close any ongoing file transfers.", @"Disconnect dialog description"));
367                
368                 return NO;
369         }
370        
371         return YES;
372 }
373
374
375
376 - (BOOL)validateAction:(SEL)selector {
377         BOOL            connected;
378
379         if(![self connection])
380                 return NO;
381        
382         connected = [[self connection] isConnected];
383        
384         if(selector == @selector(disconnect:))
385                 return (connected && ![[self connection] isDisconnecting]);
386         else if(selector == @selector(reconnect:))
387                 return (!connected && ![[self connection] isManuallyReconnecting]);
388         else if(selector == @selector(files:) || selector == @selector(postNews:) || selector == @selector(broadcast:))
389                 return connected;
390        
391         return YES;
392 }
393
394
395
396 #pragma mark -
397
398 - (BOOL)validateMenuItem:(NSMenuItem *)item {
399         return [self validateAction:[item action]];
400 }
401
402
403
404 - (IBAction)disconnect:(id)sender {
405         if(![[self connection] isDisconnecting]) {
406                 if([self beginConfirmDisconnectSheetModalForWindow:[[[self connection] chat] window]
407                                                                                          modalDelegate:self
408                                                                                         didEndSelector:@selector(_disconnectSheetDidEnd:returnCode:contextInfo:)
409                                                                                            contextInfo:NULL]) {
410                         [[self connection] disconnect];
411                 }
412         }
413 }
414
415
416
417 - (IBAction)reconnect:(id)sender {
418         [[self connection] reconnect];
419 }
420
421
422
423 - (IBAction)serverInfo:(id)sender {
424         [[[self connection] serverInfo] showWindow:self];
425 }
426
427
428
429 - (IBAction)chat:(id)sender {
430         [[[self connection] chat] showWindow:self];
431 }
432
433
434
435 - (IBAction)news:(id)sender {
436         [[[self connection] news] showWindow:self];
437 }
438
439
440
441 - (IBAction)messages:(id)sender {
442         [[[self connection] messages] showWindow:self];
443 }
444
445
446
447 - (IBAction)files:(id)sender {
448         [WCFiles filesWithConnection:[self connection] path:[WCFile fileWithRootDirectory]];
449 }
450
451
452
453 - (IBAction)transfers:(id)sender {
454         [[[self connection] transfers] showWindow:self];
455 }
456
457
458
459 - (IBAction)accounts:(id)sender {
460         [[[self connection] accounts] showWindow:self];
461 }
462
463
464
465 - (IBAction)postNews:(id)sender {
466         [[[self connection] news] postNews:self];
467 }
468
469
470
471 - (IBAction)broadcast:(id)sender {
472         [[[self connection] messages] broadcast:self];
473 }
474
475
476
477 #pragma mark -
478
479 - (IBAction)search:(id)sender {
480         [[[self connection] search] showWindow:self];
481 }
482
483
484
485 #pragma mark -
486
487 - (IBAction)addBookmark:(id)sender {
488         NSDictionary            *bookmark;
489         NSString                        *login, *password;
490         WIURL                           *url;
491         WCServerConnection      *connection;
492        
493         connection = [self connection];
494         url = [connection URL];
495        
496         if(url) {
497                 login           = [url user] ? [url user] : @"";
498                 password        = [url password] ? [url password] : @"";
499                 bookmark        = [NSDictionary dictionaryWithObjectsAndKeys:
500                         [connection name],                      WCBookmarksName,
501                         [url hostpair],                         WCBookmarksAddress,
502                         login,                                          WCBookmarksLogin,
503                         @"",                                            WCBookmarksNick,
504                         @"",                                            WCBookmarksStatus,
505                         [NSString UUIDString],          WCBookmarksIdentifier,
506                         NULL];
507                 [WCSettings addBookmark:bookmark];
508                
509                 [[WCKeychain keychain] setPassword:password forBookmark:bookmark];
510
511                 [connection setBookmark:bookmark];
512                 [connection postNotificationName:WCServerConnectionShouldSaveWindowTemplate];
513                
514                 [[NSNotificationCenter defaultCenter] postNotificationName:WCBookmarksDidChange];
515         }
516 }
517
518
519
520 #pragma mark -
521
522 - (IBAction)console:(id)sender {
523         [[[self connection] console] showWindow:self];
524 }
525
526 @end
Note: See TracBrowser for help on using the browser.