Searches for a missing server when a new one gets registered.
[MenuTunes.git] / MainController.m
1 #import "MainController.h"
2 #import "MenuController.h"
3 #import "PreferencesController.h"
4 #import "NetworkController.h"
5 #import "NetworkObject.h"
6 #import <ITKit/ITHotKeyCenter.h>
7 #import <ITKit/ITHotKey.h>
8 #import <ITKit/ITKeyCombo.h>
9 #import "StatusWindow.h"
10 #import "StatusWindowController.h"
11 #import "StatusItemHack.h"
12
13 @implementation NSImage (SmoothAdditions)
14
15 - (NSImage *)imageScaledSmoothlyToSize:(NSSize)scaledSize
16 {
17     NSImage *newImage;
18     NSImageRep *rep = [self bestRepresentationForDevice:nil];
19     
20     newImage = [[NSImage alloc] initWithSize:scaledSize];
21     [newImage lockFocus];
22     {
23         [[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
24         [[NSGraphicsContext currentContext] setShouldAntialias:YES];
25         [rep drawInRect:NSMakeRect(3, 3, scaledSize.width - 6, scaledSize.height - 6)];
26     }
27     [newImage unlockFocus];
28     return [newImage autorelease];
29 }
30
31 @end
32
33 @interface MainController(Private)
34 - (ITMTRemote *)loadRemote;
35 - (void)setLatestSongIdentifier:(NSString *)newIdentifier;
36 - (void)applicationLaunched:(NSNotification *)note;
37 - (void)applicationTerminated:(NSNotification *)note;
38 @end
39
40 static MainController *sharedController;
41
42 @implementation MainController
43
44 + (MainController *)sharedController
45 {
46     return sharedController;
47 }
48
49 /*************************************************************************/
50 #pragma mark -
51 #pragma mark INITIALIZATION/DEALLOCATION METHODS
52 /*************************************************************************/
53
54 - (id)init
55 {
56     if ( ( self = [super init] ) ) {
57         sharedController = self;
58         
59         remoteArray = [[NSMutableArray alloc] initWithCapacity:1];
60         [[PreferencesController sharedPrefs] setController:self];
61         statusWindowController = [StatusWindowController sharedController];
62         menuController = [[MenuController alloc] init];
63         df = [[NSUserDefaults standardUserDefaults] retain];
64         timerUpdating = NO;
65         blinged = NO;
66     }
67     return self;
68 }
69
70 - (void)applicationDidFinishLaunching:(NSNotification *)note
71 {
72     //Turn on debug mode if needed
73     if ([df boolForKey:@"ITDebugMode"]) {
74         SetITDebugMode(YES);
75     }
76     
77     if (([df integerForKey:@"appVersion"] < 1200) && ([df integerForKey:@"SongsInAdvance"] > 0)) {
78         [df removePersistentDomainForName:@"com.ithinksw.menutunes"];
79         [df synchronize];
80         [[PreferencesController sharedPrefs] registerDefaults];
81         [[StatusWindowController sharedController] showPreferencesUpdateWindow];
82     }
83     
84     currentRemote = [self loadRemote];
85     [[self currentRemote] begin];
86     
87     //Turn on network stuff if needed
88     networkController = [[NetworkController alloc] init];
89     if ([df boolForKey:@"enableSharing"]) {
90         [self setServerStatus:YES];
91     } else if ([df boolForKey:@"useSharedPlayer"]) {
92         [self checkForRemoteServer];
93     }
94     
95     //Setup for notification of the remote player launching or quitting
96     [[[NSWorkspace sharedWorkspace] notificationCenter]
97             addObserver:self
98             selector:@selector(applicationTerminated:)
99             name:NSWorkspaceDidTerminateApplicationNotification
100             object:nil];
101     
102     [[[NSWorkspace sharedWorkspace] notificationCenter]
103             addObserver:self
104             selector:@selector(applicationLaunched:)
105             name:NSWorkspaceDidLaunchApplicationNotification
106             object:nil];
107     
108     if (![df objectForKey:@"menu"]) {  // If this is nil, defaults have never been registered.
109         [[PreferencesController sharedPrefs] registerDefaults];
110     }
111     
112     if ([df boolForKey:@"ITMTNoStatusItem"]) {
113         statusItem = nil;
114     } else {
115         [StatusItemHack install];
116         statusItem = [[ITStatusItem alloc]
117                 initWithStatusBar:[NSStatusBar systemStatusBar]
118                 withLength:NSSquareStatusItemLength];
119     }
120     
121     bling = [[MTBlingController alloc] init];
122     [self blingTime];
123     registerTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
124                              target:self
125                              selector:@selector(blingTime)
126                              userInfo:nil
127                              repeats:YES] retain];
128     
129     NS_DURING
130         if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
131             [self applicationLaunched:nil];
132         } else {
133             if ([df boolForKey:@"LaunchPlayerWithMT"])
134                 [self showPlayer];
135             else
136                 [self applicationTerminated:nil];
137         }
138     NS_HANDLER
139         [self networkError:localException];
140     NS_ENDHANDLER
141     
142     [statusItem setImage:[NSImage imageNamed:@"MenuNormal"]];
143     [statusItem setAlternateImage:[NSImage imageNamed:@"MenuInverted"]];
144
145     [networkController startRemoteServerSearch];
146     [NSApp deactivate];
147 }
148
149 - (ITMTRemote *)loadRemote
150 {
151     NSString *folderPath = [[NSBundle mainBundle] builtInPlugInsPath];
152     ITDebugLog(@"Gathering remotes.");
153     if (folderPath) {
154         NSArray      *bundlePathList = [NSBundle pathsForResourcesOfType:@"remote" inDirectory:folderPath];
155         NSEnumerator *enumerator     = [bundlePathList objectEnumerator];
156         NSString     *bundlePath;
157
158         while ( (bundlePath = [enumerator nextObject]) ) {
159             NSBundle* remoteBundle = [NSBundle bundleWithPath:bundlePath];
160
161             if (remoteBundle) {
162                 Class remoteClass = [remoteBundle principalClass];
163
164                 if ([remoteClass conformsToProtocol:@protocol(ITMTRemote)] &&
165                     [(NSObject *)remoteClass isKindOfClass:[NSObject class]]) {
166                     id remote = [remoteClass remote];
167                     ITDebugLog(@"Adding remote at path %@", bundlePath);
168                     [remoteArray addObject:remote];
169                 }
170             }
171         }
172
173 //      if ( [remoteArray count] > 0 ) {  // UNCOMMENT WHEN WE HAVE > 1 PLUGIN
174 //          if ( [remoteArray count] > 1 ) {
175 //              [remoteArray sortUsingSelector:@selector(sortAlpha:)];
176 //          }
177 //          [self loadModuleAccessUI]; //Comment out this line to disable remote visibility
178 //      }
179     }
180 //  NSLog(@"%@", [remoteArray objectAtIndex:0]);  //DEBUG
181     return [remoteArray objectAtIndex:0];
182 }
183
184 /*************************************************************************/
185 #pragma mark -
186 #pragma mark INSTANCE METHODS
187 /*************************************************************************/
188
189 /*- (void)startTimerInNewThread
190 {
191     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
192     NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
193     refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:0.5
194                              target:self
195                              selector:@selector(timerUpdate)
196                              userInfo:nil
197                              repeats:YES] retain];
198     [runLoop run];
199     ITDebugLog(@"Timer started.");
200     [pool release];
201 }*/
202
203 - (void)setBlingTime:(NSDate*)date
204 {
205     NSMutableDictionary *globalPrefs;
206     [df synchronize];
207     globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
208     if (date) {
209         [globalPrefs setObject:date forKey:@"ITMTTrialStart"];
210         [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
211     } else {
212         [globalPrefs removeObjectForKey:@"ITMTTrialStart"];
213         [globalPrefs removeObjectForKey:@"ITMTTrialVers"];
214     }
215     [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
216     [df synchronize];
217     [globalPrefs release];
218 }
219
220 - (NSDate*)getBlingTime
221 {
222     [df synchronize];
223     return [[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialStart"];
224 }
225
226 - (void)blingTime
227 {
228     NSDate *now = [NSDate date];
229     if (![self blingBling]) {
230         if ( (! [self getBlingTime] ) || ([now timeIntervalSinceDate:[self getBlingTime]] < 0) ) {
231             [self setBlingTime:now];
232         } else if ([[[df persistentDomainForName:@".GlobalPreferences"] objectForKey:@"ITMTTrialVers"] intValue] < MT_CURRENT_VERSION) {
233             if ([now timeIntervalSinceDate:[self getBlingTime]] >= 345600) {
234                 [self setBlingTime:[now addTimeInterval:-259200]];
235             } else {
236                 NSMutableDictionary *globalPrefs;
237                 [df synchronize];
238                 globalPrefs = [[df persistentDomainForName:@".GlobalPreferences"] mutableCopy];
239                 [globalPrefs setObject:[NSNumber numberWithInt:MT_CURRENT_VERSION] forKey:@"ITMTTrialVers"];
240                 [df setPersistentDomain:globalPrefs forName:@".GlobalPreferences"];
241                 [df synchronize];
242                 [globalPrefs release];
243             }
244         }
245         
246         if ( ([now timeIntervalSinceDate:[self getBlingTime]] >= 604800) && (blinged != YES) ) {
247             blinged = YES;
248             [statusItem setEnabled:NO];
249             [self clearHotKeys];
250             if ([refreshTimer isValid]) {
251                 [refreshTimer invalidate];
252             }
253             [statusWindowController showRegistrationQueryWindow];
254         }
255     } else {
256         if (blinged) {
257             [statusItem setEnabled:YES];
258             [self setupHotKeys];
259             if (![refreshTimer isValid]) {
260                 [refreshTimer release];
261                 refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
262                              target:self
263                              selector:@selector(timerUpdate)
264                              userInfo:nil
265                              repeats:YES] retain];
266             }
267             blinged = NO;
268         }
269         [self setBlingTime:nil];
270     }
271 }
272
273 - (void)blingNow
274 {
275     [bling showPanel];
276 }
277
278 - (BOOL)blingBling
279 {
280     if ( ! ([bling checkDone] == 2475) ) {
281         return NO;
282     } else {
283         return YES;
284     }
285 }
286
287 - (BOOL)songIsPlaying
288 {
289     NSString *identifier = nil;
290     NS_DURING
291         identifier = [[self currentRemote] playerStateUniqueIdentifier];
292     NS_HANDLER
293         [self networkError:localException];
294     NS_ENDHANDLER
295     return ( ! ([identifier isEqualToString:@"0-0"]) );
296 }
297
298 - (BOOL)radioIsPlaying
299 {
300     ITMTRemotePlayerPlaylistClass class = nil;
301     NS_DURING
302         class = [[self currentRemote] currentPlaylistClass];
303     NS_HANDLER
304         [self networkError:localException];
305     NS_ENDHANDLER
306     return (class  == ITMTRemotePlayerRadioPlaylist );
307 }
308
309 - (BOOL)songChanged
310 {
311     NSString *identifier = nil;
312     NS_DURING
313         identifier = [[self currentRemote] playerStateUniqueIdentifier];
314     NS_HANDLER
315         [self networkError:localException];
316     NS_ENDHANDLER
317     return ( ! [identifier isEqualToString:_latestSongIdentifier] );
318 }
319
320 - (NSString *)latestSongIdentifier
321 {
322     return _latestSongIdentifier;
323 }
324
325 - (void)setLatestSongIdentifier:(NSString *)newIdentifier
326 {
327     ITDebugLog(@"Setting latest song identifier:");
328     ITDebugLog(@"   - Identifier: %@", newIdentifier);
329     [_latestSongIdentifier autorelease];
330     _latestSongIdentifier = [newIdentifier retain];
331 }
332
333 - (void)timerUpdate
334 {
335     if ( [self songChanged] && (timerUpdating != YES) && (playerRunningState == ITMTRemotePlayerRunning) ) {
336         ITDebugLog(@"The song changed.");
337         
338         if ([df boolForKey:@"runScripts"]) {
339             NSArray *scripts = [[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
340             NSEnumerator *scriptsEnum = [scripts objectEnumerator];
341             NSString *nextScript;
342             ITDebugLog(@"Running AppleScripts for song change.");
343             while ( (nextScript = [scriptsEnum nextObject]) ) {
344                 NSDictionary *error;
345                 NSAppleScript *currentScript = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] stringByAppendingPathComponent:nextScript]] error:&error];
346                 ITDebugLog(@"Running script: %@", nextScript);
347                 if (!currentScript || ![currentScript executeAndReturnError:nil]) {
348                     ITDebugLog(@"Error running script %@.", nextScript);
349                 }
350                 [currentScript release];
351             }
352         }
353         
354         timerUpdating = YES;
355         [statusItem setEnabled:NO];
356         
357         NS_DURING
358             latestPlaylistClass = [[self currentRemote] currentPlaylistClass];
359             [menuController rebuildSubmenus];
360     
361             if ( [df boolForKey:@"showSongInfoOnChange"] ) {
362                 [self performSelector:@selector(showCurrentTrackInfo) withObject:nil afterDelay:0.0];
363             }
364             
365             [self setLatestSongIdentifier:[[self currentRemote] playerStateUniqueIdentifier]];
366             
367             //Create the tooltip for the status item
368             if ( [df boolForKey:@"showToolTip"] ) {
369                 NSString *artist = [[self currentRemote] currentSongArtist];
370                 NSString *title = [[self currentRemote] currentSongTitle];
371                 NSString *toolTip;
372                 ITDebugLog(@"Creating status item tooltip.");
373                 if (artist) {
374                     toolTip = [NSString stringWithFormat:@"%@ - %@", artist, title];
375                 } else if (title) {
376                     toolTip = title;
377                 } else {
378                     toolTip = @"No Song Playing";
379                 }
380                 [statusItem setToolTip:toolTip];
381             } else {
382                 [statusItem setToolTip:nil];
383             }
384         NS_HANDLER
385             [self networkError:localException];
386         NS_ENDHANDLER
387         
388         timerUpdating = NO;
389         [statusItem setEnabled:YES];
390     }
391     
392     if ([networkController isConnectedToServer]) {
393         [statusItem setMenu:([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) ? [menuController menu] : [menuController menuForNoPlayer]];
394     }
395 }
396
397 - (void)menuClicked
398 {
399     ITDebugLog(@"Menu clicked.");
400     if ([networkController isConnectedToServer]) {
401         //Used the cached version
402         return;
403     }
404     
405     NS_DURING
406         if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
407             [statusItem setMenu:[menuController menu]];
408         } else {
409             [statusItem setMenu:[menuController menuForNoPlayer]];
410         }
411     NS_HANDLER
412         [self networkError:localException];
413     NS_ENDHANDLER
414 }
415
416 //
417 //
418 // Menu Selectors
419 //
420 //
421
422 - (void)playPause
423 {
424     NS_DURING
425         ITMTRemotePlayerPlayingState state = [[self currentRemote] playerPlayingState];
426         ITDebugLog(@"Play/Pause toggled");
427         if (state == ITMTRemotePlayerPlaying) {
428             [[self currentRemote] pause];
429         } else if ((state == ITMTRemotePlayerForwarding) || (state == ITMTRemotePlayerRewinding)) {
430             [[self currentRemote] pause];
431             [[self currentRemote] play];
432         } else {
433             [[self currentRemote] play];
434         }
435     NS_HANDLER
436         [self networkError:localException];
437     NS_ENDHANDLER
438     
439     [self timerUpdate];
440 }
441
442 - (void)nextSong
443 {
444     ITDebugLog(@"Going to next song.");
445     NS_DURING
446         [[self currentRemote] goToNextSong];
447     NS_HANDLER
448         [self networkError:localException];
449     NS_ENDHANDLER
450     [self timerUpdate];
451 }
452
453 - (void)prevSong
454 {
455     ITDebugLog(@"Going to previous song.");
456     NS_DURING
457         [[self currentRemote] goToPreviousSong];
458     NS_HANDLER
459         [self networkError:localException];
460     NS_ENDHANDLER
461     [self timerUpdate];
462 }
463
464 - (void)fastForward
465 {
466     ITDebugLog(@"Fast forwarding.");
467     NS_DURING
468         [[self currentRemote] forward];
469     NS_HANDLER
470         [self networkError:localException];
471     NS_ENDHANDLER
472     [self timerUpdate];
473 }
474
475 - (void)rewind
476 {
477     ITDebugLog(@"Rewinding.");
478     NS_DURING
479         [[self currentRemote] rewind];
480     NS_HANDLER
481         [self networkError:localException];
482     NS_ENDHANDLER
483     [self timerUpdate];
484 }
485
486 - (void)selectPlaylistAtIndex:(int)index
487 {
488     ITDebugLog(@"Selecting playlist %i", index);
489     NS_DURING
490         [[self currentRemote] switchToPlaylistAtIndex:(index % 1000) ofSourceAtIndex:(index / 1000)];
491         //[[self currentRemote] switchToPlaylistAtIndex:index];
492     NS_HANDLER
493         [self networkError:localException];
494     NS_ENDHANDLER
495     [self timerUpdate];
496 }
497
498 - (void)selectSongAtIndex:(int)index
499 {
500     ITDebugLog(@"Selecting song %i", index);
501     NS_DURING
502         [[self currentRemote] switchToSongAtIndex:index];
503     NS_HANDLER
504         [self networkError:localException];
505     NS_ENDHANDLER
506     [self timerUpdate];
507 }
508
509 - (void)selectSongRating:(int)rating
510 {
511     ITDebugLog(@"Selecting song rating %i", rating);
512     NS_DURING
513         [[self currentRemote] setCurrentSongRating:(float)rating / 100.0];
514     NS_HANDLER
515         [self networkError:localException];
516     NS_ENDHANDLER
517     [self timerUpdate];
518 }
519
520 - (void)selectEQPresetAtIndex:(int)index
521 {
522     ITDebugLog(@"Selecting EQ preset %i", index);
523     NS_DURING
524         [[self currentRemote] switchToEQAtIndex:index];
525     NS_HANDLER
526         [self networkError:localException];
527     NS_ENDHANDLER
528     [self timerUpdate];
529 }
530
531 - (void)showPlayer
532 {
533     ITDebugLog(@"Beginning show player.");
534     //if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
535         ITDebugLog(@"Showing player interface.");
536         NS_DURING
537             [[self currentRemote] showPrimaryInterface];
538         NS_HANDLER
539             [self networkError:localException];
540         NS_ENDHANDLER
541     /*} else {
542         ITDebugLog(@"Launching player.");
543         NS_DURING
544             NSString *path;
545             if ( (path = [df stringForKey:@"CustomPlayerPath"]) ) {
546             } else {
547                 pathITDebugLog(@"Showing player interface."); = [[self currentRemote] playerFullName];
548             }
549             if (![[NSWorkspace sharedWorkspace] launchApplication:path]) {
550                 ITDebugLog(@"Error Launching Player");
551             }
552         NS_HANDLER
553             [self networkError:localException];
554         NS_ENDHANDLER
555     }*/
556     ITDebugLog(@"Finished show player.");
557 }
558
559 - (void)showPreferences
560 {
561     ITDebugLog(@"Show preferences.");
562     [[PreferencesController sharedPrefs] showPrefsWindow:self];
563 }
564
565 - (void)showPreferencesAndClose
566 {
567     ITDebugLog(@"Show preferences.");
568     [[PreferencesController sharedPrefs] showPrefsWindow:self];
569     [[StatusWindow sharedWindow] setLocked:NO];
570     [[StatusWindow sharedWindow] vanish:self];
571     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
572 }
573
574 - (void)showTestWindow
575 {
576     [self showCurrentTrackInfo];
577 }
578
579 - (void)quitMenuTunes
580 {
581     ITDebugLog(@"Quitting MenuTunes.");
582     [NSApp terminate:self];
583 }
584
585 //
586 //
587
588 - (MenuController *)menuController
589 {
590     return menuController;
591 }
592
593 - (void)closePreferences
594 {
595     ITDebugLog(@"Preferences closed.");
596     if ( ( playerRunningState == ITMTRemotePlayerRunning) ) {
597         [self setupHotKeys];
598     }
599 }
600
601 - (ITMTRemote *)currentRemote
602 {
603     if ([networkController isConnectedToServer] && ![[networkController networkObject] isValid]) {
604         [self networkError:nil];
605         return nil;
606     }
607     return currentRemote;
608 }
609
610 //
611 //
612 // Hot key setup
613 //
614 //
615
616 - (void)clearHotKeys
617 {
618     NSEnumerator *hotKeyEnumerator = [[[ITHotKeyCenter sharedCenter] allHotKeys] objectEnumerator];
619     ITHotKey *nextHotKey;
620     ITDebugLog(@"Clearing hot keys.");
621     while ( (nextHotKey = [hotKeyEnumerator nextObject]) ) {
622         [[ITHotKeyCenter sharedCenter] unregisterHotKey:nextHotKey];
623     }
624     ITDebugLog(@"Done clearing hot keys.");
625 }
626
627 - (void)setupHotKeys
628 {
629     ITHotKey *hotKey;
630     ITDebugLog(@"Setting up hot keys.");
631     
632     if (playerRunningState == ITMTRemotePlayerNotRunning && ![[NetworkController sharedController] isConnectedToServer]) {
633         return;
634     }
635     
636     if ([df objectForKey:@"PlayPause"] != nil) {
637         ITDebugLog(@"Setting up play pause hot key.");
638         hotKey = [[ITHotKey alloc] init];
639         [hotKey setName:@"PlayPause"];
640         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]]];
641         [hotKey setTarget:self];
642         [hotKey setAction:@selector(playPause)];
643         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
644     }
645     
646     if ([df objectForKey:@"NextTrack"] != nil) {
647         ITDebugLog(@"Setting up next track hot key.");
648         hotKey = [[ITHotKey alloc] init];
649         [hotKey setName:@"NextTrack"];
650         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]]];
651         [hotKey setTarget:self];
652         [hotKey setAction:@selector(nextSong)];
653         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
654     }
655     
656     if ([df objectForKey:@"PrevTrack"] != nil) {
657         ITDebugLog(@"Setting up previous track hot key.");
658         hotKey = [[ITHotKey alloc] init];
659         [hotKey setName:@"PrevTrack"];
660         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]]];
661         [hotKey setTarget:self];
662         [hotKey setAction:@selector(prevSong)];
663         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
664     }
665     
666     if ([df objectForKey:@"ShowPlayer"] != nil) {
667         ITDebugLog(@"Setting up show player hot key.");
668         hotKey = [[ITHotKey alloc] init];
669         [hotKey setName:@"ShowPlayer"];
670         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
671         [hotKey setTarget:self];
672         [hotKey setAction:@selector(showPlayer)];
673         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
674     }
675     
676     if ([df objectForKey:@"TrackInfo"] != nil) {
677         ITDebugLog(@"Setting up track info hot key.");
678         hotKey = [[ITHotKey alloc] init];
679         [hotKey setName:@"TrackInfo"];
680         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]]];
681         [hotKey setTarget:self];
682         [hotKey setAction:@selector(showCurrentTrackInfo)];
683         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
684     }
685     
686     if ([df objectForKey:@"UpcomingSongs"] != nil) {
687         ITDebugLog(@"Setting up upcoming songs hot key.");
688         hotKey = [[ITHotKey alloc] init];
689         [hotKey setName:@"UpcomingSongs"];
690         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]]];
691         [hotKey setTarget:self];
692         [hotKey setAction:@selector(showUpcomingSongs)];
693         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
694     }
695     
696     if ([df objectForKey:@"ToggleLoop"] != nil) {
697         ITDebugLog(@"Setting up toggle loop hot key.");
698         hotKey = [[ITHotKey alloc] init];
699         [hotKey setName:@"ToggleLoop"];
700         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]]];
701         [hotKey setTarget:self];
702         [hotKey setAction:@selector(toggleLoop)];
703         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
704     }
705     
706     if ([df objectForKey:@"ToggleShuffle"] != nil) {
707         ITDebugLog(@"Setting up toggle shuffle hot key.");
708         hotKey = [[ITHotKey alloc] init];
709         [hotKey setName:@"ToggleShuffle"];
710         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]]];
711         [hotKey setTarget:self];
712         [hotKey setAction:@selector(toggleShuffle)];
713         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
714     }
715     
716     if ([df objectForKey:@"IncrementVolume"] != nil) {
717         ITDebugLog(@"Setting up increment volume hot key.");
718         hotKey = [[ITHotKey alloc] init];
719         [hotKey setName:@"IncrementVolume"];
720         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]]];
721         [hotKey setTarget:self];
722         [hotKey setAction:@selector(incrementVolume)];
723         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
724     }
725     
726     if ([df objectForKey:@"DecrementVolume"] != nil) {
727         ITDebugLog(@"Setting up decrement volume hot key.");
728         hotKey = [[ITHotKey alloc] init];
729         [hotKey setName:@"DecrementVolume"];
730         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]]];
731         [hotKey setTarget:self];
732         [hotKey setAction:@selector(decrementVolume)];
733         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
734     }
735     
736     if ([df objectForKey:@"IncrementRating"] != nil) {
737         ITDebugLog(@"Setting up increment rating hot key.");
738         hotKey = [[ITHotKey alloc] init];
739         [hotKey setName:@"IncrementRating"];
740         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]]];
741         [hotKey setTarget:self];
742         [hotKey setAction:@selector(incrementRating)];
743         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
744     }
745     
746     if ([df objectForKey:@"DecrementRating"] != nil) {
747         ITDebugLog(@"Setting up decrement rating hot key.");
748         hotKey = [[ITHotKey alloc] init];
749         [hotKey setName:@"DecrementRating"];
750         [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]]];
751         [hotKey setTarget:self];
752         [hotKey setAction:@selector(decrementRating)];
753         [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
754     }
755     ITDebugLog(@"Finished setting up hot keys.");
756 }
757
758 - (void)showCurrentTrackInfo
759 {
760     ITMTRemotePlayerSource  source      = 0;
761     NSString               *title       = nil;
762     NSString               *album       = nil;
763     NSString               *artist      = nil;
764     NSString               *composer    = nil;
765     NSString               *time        = nil;
766     NSString               *track       = nil;
767     NSImage                *art         = nil;
768     int                     rating      = -1;
769     
770     ITDebugLog(@"Showing track info status window.");
771     
772     NS_DURING
773         source      = [[self currentRemote] currentSource];
774         title       = [[self currentRemote] currentSongTitle];
775     NS_HANDLER
776         [self networkError:localException];
777     NS_ENDHANDLER
778     
779     if ( title ) {
780
781         if ( [df boolForKey:@"showAlbum"] ) {
782             NS_DURING
783                 album = [[self currentRemote] currentSongAlbum];
784             NS_HANDLER
785                 [self networkError:localException];
786             NS_ENDHANDLER
787         }
788
789         if ( [df boolForKey:@"showArtist"] ) {
790             NS_DURING
791                 artist = [[self currentRemote] currentSongArtist];
792             NS_HANDLER
793                 [self networkError:localException];
794             NS_ENDHANDLER
795         }
796
797         if ( [df boolForKey:@"showComposer"] ) {
798             NS_DURING
799                 composer = [[self currentRemote] currentSongComposer];
800             NS_HANDLER
801                 [self networkError:localException];
802             NS_ENDHANDLER
803         }
804
805         if ( [df boolForKey:@"showTime"] ) {
806             NS_DURING
807                 time = [NSString stringWithFormat:@"%@: %@ / %@",
808                 @"Time",
809                 [[self currentRemote] currentSongElapsed],
810                 [[self currentRemote] currentSongLength]];
811             NS_HANDLER
812                 [self networkError:localException];
813             NS_ENDHANDLER
814         }
815
816         if ( [df boolForKey:@"showTrackNumber"] ) {
817             int trackNo    = 0;
818             int trackCount = 0;
819             
820             NS_DURING
821                 trackNo    = [[self currentRemote] currentSongTrack];
822                 trackCount = [[self currentRemote] currentAlbumTrackCount];
823             NS_HANDLER
824                 [self networkError:localException];
825             NS_ENDHANDLER
826             
827             if ( (trackNo > 0) || (trackCount > 0) ) {
828                 track = [NSString stringWithFormat:@"%@: %i %@ %i",
829                     @"Track", trackNo, @"of", trackCount];
830             }
831         }
832
833         if ( [df boolForKey:@"showTrackRating"] ) {
834             float currentRating = 0;
835             
836             NS_DURING
837                 currentRating = [[self currentRemote] currentSongRating];
838             NS_HANDLER
839                 [self networkError:localException];
840             NS_ENDHANDLER
841             
842             if (currentRating >= 0.0) {
843                 rating = ( currentRating * 5 );
844             }
845         }
846         
847         if ( [df boolForKey:@"showAlbumArtwork"] ) {
848             NSSize oldSize, newSize;
849              NS_DURING
850                  art = [[self currentRemote] currentSongAlbumArt];
851                  oldSize = [art size];
852                  if (oldSize.width > oldSize.height) newSize = NSMakeSize(110,oldSize.height * (110.0f / oldSize.width));
853                  else newSize = NSMakeSize(oldSize.width * (110.0f / oldSize.height),110);
854                 art = [[[[NSImage alloc] initWithData:[art TIFFRepresentation]] autorelease] imageScaledSmoothlyToSize:newSize];
855             NS_HANDLER
856                 [self networkError:localException];
857             NS_ENDHANDLER
858         }
859         
860     } else {
861         title = NSLocalizedString(@"noSongPlaying", @"No song is playing.");
862     }
863     ITDebugLog(@"Showing current track info status window.");
864     [statusWindowController showSongInfoWindowWithSource:source
865                                                    title:title
866                                                    album:album
867                                                   artist:artist
868                                                 composer:composer
869                                                     time:time
870                                                    track:track
871                                                   rating:rating
872                                                    image:art];
873 }
874
875 - (void)showUpcomingSongs
876 {
877     int numSongs = 0;
878     NS_DURING
879         numSongs = [[self currentRemote] numberOfSongsInPlaylistAtIndex:[[self currentRemote] currentPlaylistIndex]];
880     NS_HANDLER
881         [self networkError:localException];
882     NS_ENDHANDLER
883     
884     ITDebugLog(@"Showing upcoming songs status window.");
885     NS_DURING
886         if (numSongs > 0) {
887             int numSongsInAdvance = [df integerForKey:@"SongsInAdvance"];
888             NSMutableArray *songList = [NSMutableArray arrayWithCapacity:numSongsInAdvance];
889             int curTrack = [[self currentRemote] currentSongIndex];
890             int i;
891     
892             for (i = curTrack + 1; i <= curTrack + numSongsInAdvance; i++) {
893                 if (i <= numSongs) {
894                     [songList addObject:[[self currentRemote] songTitleAtIndex:i]];
895                 }
896             }
897             
898             if ([songList count] == 0) {
899                 [songList addObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")];
900             }
901             
902             [statusWindowController showUpcomingSongsWindowWithTitles:songList];
903         } else {
904             [statusWindowController showUpcomingSongsWindowWithTitles:[NSArray arrayWithObject:NSLocalizedString(@"noUpcomingSongs", @"No upcoming songs.")]];
905         }
906     NS_HANDLER
907         [self networkError:localException];
908     NS_ENDHANDLER
909 }
910
911 - (void)incrementVolume
912 {
913     NS_DURING
914         float volume  = [[self currentRemote] volume];
915         float dispVol = volume;
916         ITDebugLog(@"Incrementing volume.");
917         volume  += 0.110;
918         dispVol += 0.100;
919         
920         if (volume > 1.0) {
921             volume  = 1.0;
922             dispVol = 1.0;
923         }
924     
925         ITDebugLog(@"Setting volume to %f", volume);
926         [[self currentRemote] setVolume:volume];
927     
928         // Show volume status window
929         [statusWindowController showVolumeWindowWithLevel:dispVol];
930     NS_HANDLER
931         [self networkError:localException];
932     NS_ENDHANDLER
933 }
934
935 - (void)decrementVolume
936 {
937     NS_DURING
938         float volume  = [[self currentRemote] volume];
939         float dispVol = volume;
940         ITDebugLog(@"Decrementing volume.");
941         volume  -= 0.090;
942         dispVol -= 0.100;
943     
944         if (volume < 0.0) {
945             volume  = 0.0;
946             dispVol = 0.0;
947         }
948         
949         ITDebugLog(@"Setting volume to %f", volume);
950         [[self currentRemote] setVolume:volume];
951         
952         //Show volume status window
953         [statusWindowController showVolumeWindowWithLevel:dispVol];
954     NS_HANDLER
955         [self networkError:localException];
956     NS_ENDHANDLER
957 }
958
959 - (void)incrementRating
960 {
961     NS_DURING
962         float rating = [[self currentRemote] currentSongRating];
963         ITDebugLog(@"Incrementing rating.");
964         
965         if ([[self currentRemote] currentPlaylistIndex] == 0) {
966             ITDebugLog(@"No song playing, rating change aborted.");
967             return;
968         }
969         
970         rating += 0.2;
971         if (rating > 1.0) {
972             rating = 1.0;
973         }
974         ITDebugLog(@"Setting rating to %f", rating);
975         [[self currentRemote] setCurrentSongRating:rating];
976         
977         //Show rating status window
978         [statusWindowController showRatingWindowWithRating:rating];
979     NS_HANDLER
980         [self networkError:localException];
981     NS_ENDHANDLER
982 }
983
984 - (void)decrementRating
985 {
986     NS_DURING
987         float rating = [[self currentRemote] currentSongRating];
988         ITDebugLog(@"Decrementing rating.");
989         
990         if ([[self currentRemote] currentPlaylistIndex] == 0) {
991             ITDebugLog(@"No song playing, rating change aborted.");
992             return;
993         }
994         
995         rating -= 0.2;
996         if (rating < 0.0) {
997             rating = 0.0;
998         }
999         ITDebugLog(@"Setting rating to %f", rating);
1000         [[self currentRemote] setCurrentSongRating:rating];
1001         
1002         //Show rating status window
1003         [statusWindowController showRatingWindowWithRating:rating];
1004     NS_HANDLER
1005         [self networkError:localException];
1006     NS_ENDHANDLER
1007 }
1008
1009 - (void)toggleLoop
1010 {
1011     NS_DURING
1012         ITMTRemotePlayerRepeatMode repeatMode = [[self currentRemote] repeatMode];
1013         ITDebugLog(@"Toggling repeat mode.");
1014         switch (repeatMode) {
1015             case ITMTRemotePlayerRepeatOff:
1016                 repeatMode = ITMTRemotePlayerRepeatAll;
1017             break;
1018             case ITMTRemotePlayerRepeatAll:
1019                 repeatMode = ITMTRemotePlayerRepeatOne;
1020             break;
1021             case ITMTRemotePlayerRepeatOne:
1022                 repeatMode = ITMTRemotePlayerRepeatOff;
1023             break;
1024         }
1025         ITDebugLog(@"Setting repeat mode to %i", repeatMode);
1026         [[self currentRemote] setRepeatMode:repeatMode];
1027         
1028         //Show loop status window
1029         [statusWindowController showRepeatWindowWithMode:repeatMode];
1030     NS_HANDLER
1031         [self networkError:localException];
1032     NS_ENDHANDLER
1033 }
1034
1035 - (void)toggleShuffle
1036 {
1037     NS_DURING
1038         BOOL newShuffleEnabled = ( ! [[self currentRemote] shuffleEnabled] );
1039         ITDebugLog(@"Toggling shuffle mode.");
1040         [[self currentRemote] setShuffleEnabled:newShuffleEnabled];
1041         //Show shuffle status window
1042         ITDebugLog(@"Setting shuffle mode to %i", newShuffleEnabled);
1043         [statusWindowController showShuffleWindow:newShuffleEnabled];
1044     NS_HANDLER
1045         [self networkError:localException];
1046     NS_ENDHANDLER
1047 }
1048
1049 - (void)registerNowOK
1050 {
1051     [[StatusWindow sharedWindow] setLocked:NO];
1052     [[StatusWindow sharedWindow] vanish:self];
1053     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1054
1055     [self blingNow];
1056 }
1057
1058 - (void)registerNowCancel
1059 {
1060     [[StatusWindow sharedWindow] setLocked:NO];
1061     [[StatusWindow sharedWindow] vanish:self];
1062     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1063
1064     [NSApp terminate:self];
1065 }
1066
1067 /*************************************************************************/
1068 #pragma mark -
1069 #pragma mark NETWORK HANDLERS
1070 /*************************************************************************/
1071
1072 - (void)setServerStatus:(BOOL)newStatus
1073 {
1074     if (newStatus) {
1075         //Turn on
1076         [networkController setServerStatus:YES];
1077     } else {
1078         //Tear down
1079         [networkController setServerStatus:NO];
1080     }
1081 }
1082
1083 - (int)connectToServer
1084 {
1085     int result;
1086     ITDebugLog(@"Attempting to connect to shared remote.");
1087     result = [networkController connectToHost:[df stringForKey:@"sharedPlayerHost"]];
1088     //Connect
1089     if (result == 1) {
1090         [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1091         currentRemote = [[[networkController networkObject] remote] retain];
1092         
1093         [self setupHotKeys];
1094         //playerRunningState = ITMTRemotePlayerRunning;
1095         playerRunningState = [[self currentRemote] playerRunningState];
1096         
1097         [refreshTimer invalidate];
1098         refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1099                                 target:self
1100                                 selector:@selector(timerUpdate)
1101                                 userInfo:nil
1102                                 repeats:YES] retain];
1103         [self timerUpdate];
1104         ITDebugLog(@"Connection successful.");
1105         return 1;
1106     } else if (result == 0) {
1107         ITDebugLog(@"Connection failed.");
1108         currentRemote = [remoteArray objectAtIndex:0];
1109         return 0;
1110     } else {
1111         //Do something about the password being invalid
1112         ITDebugLog(@"Connection failed.");
1113         currentRemote = [remoteArray objectAtIndex:0];
1114         return -1;
1115     }
1116 }
1117
1118 - (BOOL)disconnectFromServer
1119 {
1120     ITDebugLog(@"Disconnecting from shared remote.");
1121     //Disconnect
1122     [currentRemote release];
1123     currentRemote = [remoteArray objectAtIndex:0];
1124     [networkController disconnect];
1125     
1126     if ([[self currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
1127         [self applicationLaunched:nil];
1128     } else {
1129         [self applicationTerminated:nil];
1130     }
1131     [self timerUpdate];
1132     return YES;
1133 }
1134
1135 - (void)checkForRemoteServer
1136 {
1137     ITDebugLog(@"Checking for remote server.");
1138     [NSThread detachNewThreadSelector:@selector(runRemoteServerCheck:) toTarget:self withObject:nil];
1139 }
1140
1141 - (void)runRemoteServerCheck:(id)sender
1142 {
1143     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
1144     ITDebugLog(@"Remote server check running.");
1145     if ([networkController checkForServerAtHost:[df stringForKey:@"sharedPlayerHost"]]) {
1146         ITDebugLog(@"Remote server found.");
1147         [self performSelectorOnMainThread:@selector(remoteServerFound:) withObject:nil waitUntilDone:NO];
1148     } else {
1149         ITDebugLog(@"Remote server not found.");
1150         [self performSelectorOnMainThread:@selector(remoteServerNotFound:) withObject:nil waitUntilDone:NO];
1151     }
1152     [pool release];
1153 }
1154
1155 - (void)remoteServerFound:(id)sender
1156 {
1157     if (![networkController isServerOn] && ![networkController isConnectedToServer]) {
1158         [[StatusWindowController sharedController] showReconnectQueryWindow];
1159     }
1160 }
1161
1162 - (void)remoteServerNotFound:(id)sender
1163 {
1164     [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:NO];
1165 }
1166
1167 - (void)networkError:(NSException *)exception
1168 {
1169     ITDebugLog(@"Remote exception thrown: %@: %@", [exception name], [exception reason]);
1170     if ( ((exception == nil) || [[exception name] isEqualToString:NSPortTimeoutException]) && [networkController isConnectedToServer]) {
1171         NSRunCriticalAlertPanel(@"Remote MenuTunes Disconnected", @"The MenuTunes server you were connected to stopped responding or quit. MenuTunes will revert back to the local player.", @"OK", nil, nil);
1172         if ([self disconnectFromServer]) {
1173             [[PreferencesController sharedPrefs] resetRemotePlayerTextFields];
1174             [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:YES];
1175         } else {
1176             ITDebugLog(@"CRITICAL ERROR, DISCONNECTING!");
1177         }
1178     }
1179 }
1180
1181 - (void)reconnect
1182 {
1183     if ([self connectToServer] == 0) {
1184         [NSTimer scheduledTimerWithTimeInterval:90.0 target:self selector:@selector(checkForRemoteServer) userInfo:nil repeats:YES];
1185     }
1186     [[StatusWindow sharedWindow] setLocked:NO];
1187     [[StatusWindow sharedWindow] vanish:self];
1188     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1189 }
1190
1191 - (void)cancelReconnect
1192 {
1193     [[StatusWindow sharedWindow] setLocked:NO];
1194     [[StatusWindow sharedWindow] vanish:self];
1195     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
1196 }
1197
1198 /*************************************************************************/
1199 #pragma mark -
1200 #pragma mark WORKSPACE NOTIFICATION HANDLERS
1201 /*************************************************************************/
1202
1203 - (void)applicationLaunched:(NSNotification *)note
1204 {
1205     NS_DURING
1206         if (!note || ([[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer])) {
1207             ITDebugLog(@"Remote application launched.");
1208             playerRunningState = ITMTRemotePlayerRunning;
1209             [[self currentRemote] begin];
1210             [self setLatestSongIdentifier:@""];
1211             [self timerUpdate];
1212             refreshTimer = [[NSTimer scheduledTimerWithTimeInterval:([networkController isConnectedToServer] ? 10.0 : 0.5)
1213                                 target:self
1214                                 selector:@selector(timerUpdate)
1215                                 userInfo:nil
1216                                 repeats:YES] retain];
1217             //[NSThread detachNewThreadSelector:@selector(startTimerInNewThread) toTarget:self withObject:nil];
1218             [self setupHotKeys];
1219         }
1220     NS_HANDLER
1221         [self networkError:localException];
1222     NS_ENDHANDLER
1223 }
1224
1225  - (void)applicationTerminated:(NSNotification *)note
1226  {
1227     NS_DURING
1228         if (!note || [[[note userInfo] objectForKey:@"NSApplicationName"] isEqualToString:[[self currentRemote] playerFullName]] && ![[NetworkController sharedController] isConnectedToServer]) {
1229             ITDebugLog(@"Remote application terminated.");
1230             playerRunningState = ITMTRemotePlayerNotRunning;
1231             [[self currentRemote] halt];
1232             [refreshTimer invalidate];
1233             [refreshTimer release];
1234             refreshTimer = nil;
1235             [self clearHotKeys];
1236             
1237             if ([df objectForKey:@"ShowPlayer"] != nil) {
1238                 ITHotKey *hotKey;
1239                 ITDebugLog(@"Setting up show player hot key.");
1240                 hotKey = [[ITHotKey alloc] init];
1241                 [hotKey setName:@"ShowPlayer"];
1242                 [hotKey setKeyCombo:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]]];
1243                 [hotKey setTarget:self];
1244                 [hotKey setAction:@selector(showPlayer)];
1245                 [[ITHotKeyCenter sharedCenter] registerHotKey:[hotKey autorelease]];
1246             }
1247         }
1248     NS_HANDLER
1249         [self networkError:localException];
1250     NS_ENDHANDLER
1251  }
1252
1253
1254 /*************************************************************************/
1255 #pragma mark -
1256 #pragma mark NSApplication DELEGATE METHODS
1257 /*************************************************************************/
1258
1259 - (void)applicationWillTerminate:(NSNotification *)note
1260 {
1261     [networkController stopRemoteServerSearch];
1262     [self clearHotKeys];
1263     [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
1264 }
1265
1266
1267 /*************************************************************************/
1268 #pragma mark -
1269 #pragma mark DEALLOCATION METHOD
1270 /*************************************************************************/
1271
1272 - (void)dealloc
1273 {
1274     [self applicationTerminated:nil];
1275     [bling release];
1276     [statusItem release];
1277     [statusWindowController release];
1278     [menuController release];
1279     [networkController release];
1280     [super dealloc];
1281 }
1282
1283 @end