Added hotkeys for setting individual song ratings 0-5 stars.
[MenuTunes.git] / PreferencesController.m
1 #import "PreferencesController.h"
2 #import "MainController.h"
3 #import "MenuController.h"
4 #import "NetworkController.h"
5 #import "NetworkObject.h"
6 #import "StatusWindow.h"
7 #import "StatusWindowController.h"
8 #import "CustomMenuTableView.h"
9
10 #import <netinet/in.h>
11 #import <arpa/inet.h>
12 #import <openssl/sha.h>
13 #import <sys/types.h>
14 #import <sys/stat.h>
15
16 #import <ITFoundation/ITLoginItem.h>
17
18 #import <ITKit/ITHotKeyCenter.h>
19 #import <ITKit/ITKeyCombo.h>
20 #import <ITKit/ITKeyComboPanel.h>
21 #import <ITKit/ITWindowPositioning.h>
22 #import <ITKit/ITKeyBroadcaster.h>
23
24 #import <ITKit/ITTSWBackgroundView.h>
25 #import <ITKit/ITWindowEffect.h>
26 #import <ITKit/ITCutWindowEffect.h>
27 #import <ITKit/ITDissolveWindowEffect.h>
28 #import <ITKit/ITSlideHorizontallyWindowEffect.h>
29 #import <ITKit/ITSlideVerticallyWindowEffect.h>
30 #import <ITKit/ITPivotWindowEffect.h>
31
32
33 #define SENDER_STATE (([sender state] == NSOnState) ? YES : NO)
34
35 /*************************************************************************/
36 #pragma mark -
37 #pragma mark PRIVATE INTERFACE
38 /*************************************************************************/
39
40 @interface PreferencesController (Private)
41 - (void)setupWindow;
42 - (void)setupCustomizationTables;
43 - (void)setupMenuItems;
44 - (void)setupUI;
45 - (void)setStatusWindowEntryEffect:(Class)effectClass;
46 - (void)setStatusWindowExitEffect:(Class)effectClass;
47 - (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update;
48 - (void)repopulateEffectPopupsForVerticalPosition:(ITVerticalWindowPosition)vPos horizontalPosition:(ITHorizontalWindowPosition)hPos;
49 - (BOOL)effect:(Class)effectClass supportsVerticalPosition:(ITVerticalWindowPosition)vPos withHorizontalPosition:(ITHorizontalWindowPosition)hPos;
50 - (IBAction)changeMenus:(id)sender;
51 @end
52
53
54 @implementation PreferencesController
55
56
57 /*************************************************************************/
58 #pragma mark -
59 #pragma mark STATIC VARIABLES
60 /*************************************************************************/
61
62 static PreferencesController *prefs = nil;
63
64
65 /*************************************************************************/
66 #pragma mark -
67 #pragma mark INITIALIZATION METHODS
68 /*************************************************************************/
69
70 + (PreferencesController *)sharedPrefs;
71 {
72     if (! prefs) {
73         prefs = [[self alloc] init];
74     }
75     return prefs;
76 }
77
78 - (id)init
79 {
80     if ( (self = [super init]) ) {
81         ITDebugLog(@"Preferences initialized.");
82         df = [[NSUserDefaults standardUserDefaults] retain];
83         
84         effectClasses = [[ITWindowEffect effectClasses] retain];
85         
86         hotKeysArray = [[NSArray alloc] initWithObjects:@"PlayPause",
87                                                        @"NextTrack",
88                                                        @"PrevTrack",
89                                                        @"FastForward",
90                                                        @"Rewind",
91                                                        @"ShowPlayer",
92                                                        @"TrackInfo",
93                                                        @"UpcomingSongs",
94                                                        @"IncrementVolume",
95                                                        @"DecrementVolume",
96                                                        @"IncrementRating",
97                                                        @"DecrementRating",
98                                                        @"ToggleShuffle",
99                                                        @"ToggleLoop",
100                                                        @"PopupMenu",
101                                                        @"SetRating0",
102                                                        @"SetRating1",
103                                                        @"SetRating2",
104                                                        @"SetRating3",
105                                                        @"SetRating4",
106                                                        @"SetRating5",
107                                                        nil];
108         
109         hotKeyNamesArray = [[NSArray alloc] initWithObjects:@"Play/Pause",
110                                                        @"Next Track",
111                                                        @"Previous Track",
112                                                        @"Fast Forward",
113                                                        @"Rewind",
114                                                        @"Show Player",
115                                                        @"Track Info",
116                                                        @"Upcoming Songs",
117                                                        @"Increment Volume",
118                                                        @"Decrement Volume",
119                                                        @"Increment Rating",
120                                                        @"Decrement Rating",
121                                                        @"Toggle Shuffle",
122                                                        @"Toggle Loop",
123                                                        @"Pop-up status menu",
124                                                        [NSString stringWithUTF8String:"Set Rating: ☆☆☆☆☆"],
125                                                        [NSString stringWithUTF8String:"Set Rating: ★☆☆☆☆"],
126                                                        [NSString stringWithUTF8String:"Set Rating: ★★☆☆☆"],
127                                                        [NSString stringWithUTF8String:"Set Rating: ★★★☆☆"],
128                                                        [NSString stringWithUTF8String:"Set Rating: ★★★★☆"],
129                                                        [NSString stringWithUTF8String:"Set Rating: ★★★★★"],
130                                                        nil];
131         hotKeysDictionary = [[NSMutableDictionary alloc] init];
132         controller = nil;
133         
134         [self setupWindow];  // Load in the nib, and perform any initial setup.
135         [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
136     }
137     return self;
138 }
139
140
141 /*************************************************************************/
142 #pragma mark -
143 #pragma mark ACCESSOR METHODS
144 /*************************************************************************/
145
146 - (id)controller
147 {
148     return controller;
149 }
150
151 - (void)setController:(id)object
152 {
153     [controller autorelease];
154     controller = [object retain];
155 }
156
157
158 /*************************************************************************/
159 #pragma mark -
160 #pragma mark INSTANCE METHODS
161 /*************************************************************************/
162
163 - (BOOL)showPasswordPanel
164 {
165     [passwordPanel setLevel:NSStatusWindowLevel];
166     [passwordPanelOKButton setTitle:@"Connect"];
167     [passwordPanelTitle setStringValue:@"Password Required"];
168     [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"Please enter a password for access to the MenuTunes player named %@ at %@.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
169     [passwordPanel setLevel:NSStatusWindowLevel];
170     [NSApp activateIgnoringOtherApps:YES];
171     [passwordPanel center];
172     [passwordPanel orderFrontRegardless];
173     [passwordPanel makeKeyWindow];
174     if ([NSApp runModalForWindow:passwordPanel]) {
175         return YES;
176     } else {
177         return NO;
178     }
179 }
180
181 - (BOOL)showInvalidPasswordPanel
182 {
183     [passwordPanel setLevel:NSStatusWindowLevel];
184     [passwordPanelOKButton setTitle:@"Retry"];
185     [passwordPanelTitle setStringValue:@"Invalid Password"];
186     [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"The password entered for access to the MenuTunes player named %@ at %@ is invalid.  Please provide a new password.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
187     [passwordPanel setLevel:NSStatusWindowLevel];
188     [NSApp activateIgnoringOtherApps:YES];
189     [passwordPanel center];
190     [passwordPanel orderFrontRegardless];
191     [passwordPanel makeKeyWindow];
192     if ([NSApp runModalForWindow:passwordPanel]) {
193         return YES;
194     } else {
195         return NO;
196     }
197 }
198
199 - (IBAction)showPrefsWindow:(id)sender
200 {
201     ITDebugLog(@"Showing preferences window.");
202     if (!myItems) {  // If menu array does not exist yet, then the window hasn't been setup.
203         ITDebugLog(@"Window doesn't exist, initial setup.");
204         [self setupCustomizationTables];  // Setup the DnD manu config tables.
205         [self setupMenuItems];  // Setup the arrays of menu items
206         [self setupUI]; // Sets up additional UI
207         [window setDelegate:self];
208         [menuTableView reloadData];
209         [hotKeysTableView reloadData];
210         [hotKeysTableView setDoubleAction:@selector(hotKeysTableViewDoubleClicked:)];
211         
212         //Change the launch player checkbox to the proper name
213         NS_DURING
214             [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
215         NS_HANDLER
216             [controller networkError:localException];
217         NS_ENDHANDLER
218     }
219
220     [self resetRemotePlayerTextFields];
221     [launchAtLoginCheckbox becomeFirstResponder];
222     [NSApp activateIgnoringOtherApps:YES];
223     [window center];
224     [window orderFrontRegardless];
225     [window makeKeyWindow];
226 }
227
228 - (IBAction)changeGeneralSetting:(id)sender
229 {
230     ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
231     if ( [sender tag] == 1010) {
232         ITSetApplicationLaunchOnLogin([[NSBundle mainBundle] bundlePath], SENDER_STATE);
233     } else if ( [sender tag] == 1020) {
234         [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
235     } else if ( [sender tag] == 1030) {
236         [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
237         if ([[controller currentRemote] playerRunningState] == ITMTRemotePlayerRunning) {
238             [[controller menuController] performSelector:@selector(rebuildSubmenus) withObject:nil afterDelay:0];
239         }
240     } else if ( [sender tag] == 1040) {
241         // This will not be executed.  Song info always shows the title of the song.
242         // [df setBool:SENDER_STATE forKey:@"showName"];
243     } else if ( [sender tag] == 1050) {
244         [df setBool:SENDER_STATE forKey:@"showArtist"];
245     } else if ( [sender tag] == 1055) {
246         [df setBool:SENDER_STATE forKey:@"showComposer"];
247     } else if ( [sender tag] == 1060) {
248         [df setBool:SENDER_STATE forKey:@"showAlbum"];
249     } else if ( [sender tag] == 1070) {
250         [df setBool:SENDER_STATE forKey:@"showTime"];
251     } else if ( [sender tag] == 1080) {
252         [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
253     } else if ( [sender tag] == 1085) {
254         [df setBool:SENDER_STATE forKey:@"showPlayCount"];
255     } else if ( [sender tag] == 1090) {
256         [df setBool:SENDER_STATE forKey:@"showTrackRating"];
257     } else if ( [sender tag] == 1100) {
258         [df setBool:SENDER_STATE forKey:@"showAlbumArtwork"];
259     } else if ( [sender tag] == 1110) {
260         [df setBool:SENDER_STATE forKey:@"runScripts"];
261         if (SENDER_STATE) {
262             [runScriptsCheckbox setState:NSOnState];
263             [showScriptsButton setEnabled:YES];
264         } else {
265             [showScriptsButton setEnabled:NO];
266         }
267     } else if ( [sender tag] == 1120) {
268         mkdir([[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"] cString], 0744);
269         [[NSWorkspace sharedWorkspace] openFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/MenuTunes/Scripts"]];
270     }
271     [df synchronize];
272 }
273
274 - (IBAction)changeSharingSetting:(id)sender
275 {
276     ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
277     if ( [sender tag] == 5010 ) {
278         BOOL state = SENDER_STATE;
279         [df setBool:state forKey:@"enableSharing"];
280         //Disable/enable the use of shared player options
281         [useSharedMenuTunesCheckbox setEnabled:!state];
282         [passwordTextField setEnabled:state];
283         [nameTextField setEnabled:state];
284         [selectSharedPlayerButton setEnabled:NO];
285         [controller setServerStatus:state]; //Set server status
286     } else if ( [sender tag] == 5015 ) {
287         [df setObject:[sender stringValue] forKey:@"sharedPlayerName"];
288         [[NetworkController sharedController] resetServerName];
289     } else if ( [sender tag] == 5030 ) {
290         //Set the server password
291         const char *instring = [[sender stringValue] UTF8String];
292         const char *password = "p4s5w0rdMT1.2";
293         unsigned char *result;
294         NSData *hashedPass, *passwordStringHash;
295         if ([[sender stringValue] length] == 0) {
296             [df setObject:[NSData data] forKey:@"sharedPlayerPassword"];
297             return;
298         }
299         result = SHA1(instring, strlen(instring), NULL);
300         hashedPass = [NSData dataWithBytes:result length:strlen(result)];
301         result = SHA1(password, strlen(password), NULL);
302         passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
303         if (![hashedPass isEqualToData:passwordStringHash]) {
304             [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
305             [sender setStringValue:@"p4s5w0rdMT1.2"];
306         }
307     } else if ( [sender tag] == 5040 ) {
308         BOOL state = SENDER_STATE;
309         [df setBool:state forKey:@"useSharedPlayer"];
310         //Disable/enable the use of sharing options
311         [shareMenuTunesCheckbox setEnabled:!state];
312         [passwordTextField setEnabled:NO];
313         [nameTextField setEnabled:NO];
314         [selectSharedPlayerButton setEnabled:state];
315         
316         if (state && ([controller connectToServer] == 1)) {
317             [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
318             [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
319         } else {
320             [selectedPlayerTextField setStringValue:@"No shared player selected."];
321             [locationTextField setStringValue:@"-"];
322             if ([[NetworkController sharedController] isConnectedToServer]) {
323                 [controller disconnectFromServer];
324             }
325             
326         }
327     } else if ( [sender tag] == 5050 ) {
328         //If no player is selected in the table view, turn off OK button.
329         if ([sender clickedRow] == -1 ) {
330             [sharingPanelOKButton setEnabled:NO];
331         } else {
332             [sharingPanelOKButton setEnabled:YES];
333         }
334     } else if ( [sender tag] == 5051 ) {
335         [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
336     } else if ( [sender tag] == 5060 ) {
337         //Set OK button state
338         if (([selectPlayerBox contentView] == zeroConfView && [sharingTableView selectedRow] == -1) ||
339             ([selectPlayerBox contentView] == manualView && [[hostTextField stringValue] length] == 0)) {
340             [sharingPanelOKButton setEnabled:NO];
341         } else {
342             [sharingPanelOKButton setEnabled:YES];
343         }
344         //Show selection sheet
345         [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
346     } else if ( [sender tag] == 5100 ) {
347         //Change view
348         if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
349             NSRect frame = [selectPlayerSheet frame];
350             frame.origin.y -= 58;
351             frame.size.height = 273;
352             if ([sharingTableView selectedRow] == -1) {
353                 [sharingPanelOKButton setEnabled:NO];
354             }
355             [selectPlayerBox setContentView:zeroConfView];
356             [selectPlayerSheet setFrame:frame display:YES animate:YES];
357         } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
358             NSRect frame = [selectPlayerSheet frame];
359             frame.origin.y += 58;
360             frame.size.height = 215;
361             if ([[hostTextField stringValue] length] == 0) {
362                 [sharingPanelOKButton setEnabled:NO];
363             } else {
364                 [sharingPanelOKButton setEnabled:YES];
365             }
366             [selectPlayerBox setContentView:manualView];
367             [selectPlayerSheet setFrame:frame display:YES animate:YES];
368             [hostTextField selectText:nil];
369         }
370     } else if ( [sender tag] == 5150 ) {
371         const char *instring = [[sender stringValue] UTF8String];
372         unsigned char *result;
373         result = SHA1(instring, strlen(instring), NULL);
374         [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
375     } else if ( [sender tag] == 5110 ) {
376         //Cancel
377         [NSApp endSheet:selectPlayerSheet];
378         [selectPlayerSheet orderOut:nil];
379         if ([selectPlayerBox contentView] == manualView) {
380             [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
381         } else {
382         }
383     } else if ( [sender tag] == 5120 ) {
384         //OK, try to connect
385         [NSApp endSheet:selectPlayerSheet];
386         [selectPlayerSheet orderOut:nil];
387         
388         [self changeSharingSetting:clientPasswordTextField];
389         
390         if ([selectPlayerBox contentView] == manualView) {
391             [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
392         } else {
393             if ([sharingTableView selectedRow] > -1) {
394                 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
395             }
396         }
397         
398         if ([controller connectToServer] == 1) {
399             [useSharedMenuTunesCheckbox setState:NSOnState];
400             [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
401             [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
402         } else {
403             NSRunAlertPanel(@"Connection error.", @"The MenuTunes server you attempted to connect to was not responding. MenuTunes will revert back to the local player.", @"OK", nil, nil);
404         }
405     } else if ( [sender tag] == 6010 ) {
406         //Cancel password entry
407         [passwordPanel orderOut:nil];
408         [NSApp stopModalWithCode:0];
409     } else if ( [sender tag] == 6020 ) {
410         //OK password entry, retry connect
411         const char *instring = [[passwordPanelTextField stringValue] UTF8String];
412         unsigned char *result;
413         result = SHA1(instring, strlen(instring), NULL);
414         [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
415         [passwordPanel orderOut:nil];
416         [NSApp stopModalWithCode:1];
417     }
418     [df synchronize];
419 }
420
421 - (IBAction)changeStatusWindowSetting:(id)sender
422 {
423     StatusWindow *sw = [StatusWindow sharedWindow];
424     ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
425     
426     if ( [sender tag] == 2010) {
427     
428         BOOL entryEffectValid = YES;
429         BOOL exitEffectValid  = YES;
430                 
431         [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
432         [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
433         [sw setVerticalPosition:[sender selectedRow]];
434         [sw setHorizontalPosition:[sender selectedColumn]];
435         
436         // Enable/disable the items in the popups.
437         [self repopulateEffectPopupsForVerticalPosition:[sw verticalPosition]
438                                      horizontalPosition:[sw horizontalPosition]];
439
440         // Make sure the effects support the new position.  
441         entryEffectValid = ( [self effect:[[sw entryEffect] class] 
442                  supportsVerticalPosition:[sw verticalPosition]
443                    withHorizontalPosition:[sw horizontalPosition]] );
444         exitEffectValid  = ( [self effect:[[sw exitEffect] class] 
445                  supportsVerticalPosition:[sw verticalPosition]
446                    withHorizontalPosition:[sw horizontalPosition]] );
447         
448         if ( ! entryEffectValid ) {
449             [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITCutWindowEffect")]];
450             [self setStatusWindowEntryEffect:NSClassFromString(@"ITCutWindowEffect")];
451         } else {
452             [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:[[sw entryEffect] class]]];
453         }
454         
455         if ( ! exitEffectValid ) {
456             [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITDissolveWindowEffect")]];
457             [self setStatusWindowExitEffect:NSClassFromString(@"ITDissolveWindowEffect")];
458         } else {
459             [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:[[sw exitEffect] class]]];
460         }
461         
462         [(MainController *)controller showCurrentTrackInfo];
463         
464     } else if ( [sender tag] == 2020) {
465     
466         // Update screen selection.
467         
468     } else if ( [sender tag] == 2030) {
469     
470         [self setStatusWindowEntryEffect:[[sender selectedItem] representedObject]];
471         [(MainController *)controller showCurrentTrackInfo];
472         
473     } else if ( [sender tag] == 2040) {
474     
475         [self setStatusWindowExitEffect:[[sender selectedItem] representedObject]];
476         [(MainController *)controller showCurrentTrackInfo];
477         
478     } else if ( [sender tag] == 2050) {
479         float newTime = ( -([sender floatValue]) );
480         [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
481         [[sw entryEffect] setEffectTime:newTime];
482     } else if ( [sender tag] == 2060) {
483         float newTime = ( -([sender floatValue]) );
484         [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
485         [[sw exitEffect] setEffectTime:newTime];
486     } else if ( [sender tag] == 2070) {
487         [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
488         [sw setExitDelay:[sender floatValue]];
489     } else if ( [sender tag] == 2080) {
490         [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
491     } else if ( [sender tag] == 2090) {
492         
493         int setting = [sender indexOfSelectedItem];
494         
495         if ( setting == 0 ) {
496             [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundApple];
497             [backgroundColorWell  setEnabled:NO];
498             [backgroundColorPopup setEnabled:NO];
499         } else if ( setting == 1 ) {
500             [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundReadable];
501             [backgroundColorWell  setEnabled:NO];
502             [backgroundColorPopup setEnabled:NO];
503         } else if ( setting == 2 ) {
504             [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundColored];
505             [backgroundColorWell  setEnabled:YES];
506             [backgroundColorPopup setEnabled:YES];
507         }
508
509         [df setInteger:setting forKey:@"statusWindowBackgroundMode"];
510         [(MainController *)controller showCurrentTrackInfo];
511         
512     } else if ( [sender tag] == 2091) {
513         [self setCustomColor:[sender color] updateWell:NO];
514         [(MainController *)controller showCurrentTrackInfo];
515     } else if ( [sender tag] == 2092) {
516         
517         int selectedItem = [sender indexOfSelectedItem];
518         
519         if ( selectedItem == 1 ) { // An NSPopUpButton in PullDown mode uses item 0 as its title.  Its first selectable item is 1.
520             [self setCustomColor:[NSColor colorWithCalibratedRed:0.92549 green:0.686275 blue:0.0 alpha:1.0] updateWell:YES];
521         } else if ( selectedItem == 2 ) {
522             [self setCustomColor:[NSColor colorWithCalibratedRed:0.380392 green:0.670588 blue:0.0 alpha:1.0] updateWell:YES];
523         } else if ( selectedItem == 3 ) {
524             [self setCustomColor:[NSColor colorWithCalibratedRed:0.443137 green:0.231373 blue:0.619608 alpha:1.0] updateWell:YES];
525         } else if ( selectedItem == 4 ) {
526             [self setCustomColor:[NSColor colorWithCalibratedRed:0.831373 green:0.12549 blue:0.509804 alpha:1.0] updateWell:YES];
527         } else if ( selectedItem == 5 ) {
528             [self setCustomColor:[NSColor colorWithCalibratedRed:0.00784314 green:0.611765 blue:0.662745 alpha:1.0] updateWell:YES];
529         } else {
530             [self setCustomColor:[NSColor colorWithCalibratedWhite:0.15 alpha:0.70] updateWell:YES];
531         }
532         [(MainController *)controller showCurrentTrackInfo];
533
534     } else if ( [sender tag] == 2095) {
535         [df setInteger:[sender indexOfSelectedItem] forKey:@"statusWindowSizing"];
536         [(MainController *)controller showCurrentTrackInfo];
537     }
538     
539     [df synchronize];
540 }
541
542 - (void)registerDefaults
543 {
544     ITDebugLog(@"Registering defaults.");
545     [df setObject:[NSArray arrayWithObjects:
546         @"trackInfo",
547         @"separator",
548         @"playPause",
549         @"prevTrack",
550         @"nextTrack",
551         @"separator",
552         @"playlists",
553         @"upcomingSongs",
554         @"separator",
555         @"preferences",
556         @"quit",
557         nil] forKey:@"menu"];
558
559     [df setInteger:MT_CURRENT_VERSION forKey:@"appVersion"];
560     [df setInteger:5 forKey:@"SongsInAdvance"];
561 //  [df setBool:YES forKey:@"showName"];  // Song info will always show song title.
562     [df setBool:YES forKey:@"showArtist"];
563     [df setBool:YES forKey:@"showAlbumArtwork"];
564     [df setBool:NO forKey:@"showAlbum"];
565     [df setBool:NO forKey:@"showComposer"];
566     [df setBool:NO forKey:@"showTime"];
567     [df setBool:NO forKey:@"showToolTip"];
568
569     [df setObject:@"ITCutWindowEffect" forKey:@"statusWindowAppearanceEffect"];
570     [df setObject:@"ITDissolveWindowEffect" forKey:@"statusWindowVanishEffect"];
571     [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
572     [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
573     [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
574     [df setInteger:(int)ITWindowPositionBottom forKey:@"statusWindowVerticalPosition"];
575     [df setInteger:(int)ITWindowPositionLeft forKey:@"statusWindowHorizontalPosition"];
576     [[StatusWindow sharedWindow] setVerticalPosition:(int)ITWindowPositionBottom];
577     [[StatusWindow sharedWindow] setHorizontalPosition:(int)ITWindowPositionLeft];
578     [df setBool:YES forKey:@"showSongInfoOnChange"];
579     
580     [df setObject:[NSArchiver archivedDataWithRootObject:[NSColor blueColor]] forKey:@"statusWindowBackgroundColor"];
581     
582     [df synchronize];
583     
584     if (ITDoesApplicationLaunchOnLogin([[NSBundle mainBundle] bundlePath])) {
585         [[StatusWindowController sharedController] showSetupQueryWindow];
586     }
587 }
588
589 - (void)autoLaunchOK
590 {
591     [[StatusWindow sharedWindow] setLocked:NO];
592     [[StatusWindow sharedWindow] vanish:self];
593     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
594     
595     ITSetApplicationLaunchOnLogin([[NSBundle mainBundle] bundlePath], YES);
596 }
597
598 - (void)autoLaunchCancel
599 {
600     [[StatusWindow sharedWindow] setLocked:NO];
601     [[StatusWindow sharedWindow] vanish:self];
602     [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
603 }
604
605 - (void)deletePressedInTableView:(NSTableView *)tableView
606 {
607     if (tableView == menuTableView) {
608         int selRow = [tableView selectedRow];
609         ITDebugLog(@"Delete pressed in menu table view.");
610         if (selRow != - 1) {
611             NSString *object = [myItems objectAtIndex:selRow];
612             
613             if ([object isEqualToString:@"preferences"]) {
614                 NSBeep();
615                 return;
616             }
617             
618             if (![object isEqualToString:@"separator"])
619                 [availableItems addObject:object];
620             ITDebugLog(@"Removing object named %@", object);
621             [myItems removeObjectAtIndex:selRow];
622             [menuTableView reloadData];
623             [allTableView reloadData];
624         }
625         [self changeMenus:self];
626     }
627 }
628
629 - (void)resetRemotePlayerTextFields
630 {
631     if ([[NetworkController sharedController] isConnectedToServer]) {
632         [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
633         [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
634     } else {
635         [selectedPlayerTextField setStringValue:@"No shared player selected."];
636         [locationTextField setStringValue:@"-"];
637     }
638 }
639
640 /*************************************************************************/
641 #pragma mark -
642 #pragma mark HOTKEY SUPPORT METHODS
643 /*************************************************************************/
644
645 - (IBAction)clearHotKey:(id)sender
646 {
647     [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
648     [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
649     [controller setupHotKeys];
650     [hotKeysTableView reloadData];
651 }
652
653 - (IBAction)editHotKey:(id)sender
654 {
655     ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
656     NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
657     ITKeyCombo *keyCombo;
658     
659     ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
660     [controller clearHotKeys];
661     [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
662     [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
663     if ([panel runModal] == NSOKButton) {
664         NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
665         NSString *nextKey;
666         keyCombo = [panel keyCombo];
667         
668         //Check for duplicate key combo
669         while ( (nextKey = [keyEnumerator nextObject]) ) {
670             if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
671                 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
672                 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
673                                    forKey:nextKey];
674                 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
675                     forKey:nextKey];
676             }
677         }
678         
679         [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
680         [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
681         [controller setupHotKeys];
682         [hotKeysTableView reloadData];
683         ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
684     } else {
685         ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
686     }
687 }
688
689 - (void)hotKeysTableViewDoubleClicked:(id)sender
690 {
691     if ([sender clickedRow] > -1) {
692         [self editHotKey:sender];
693     }
694 }
695
696 /*************************************************************************/
697 #pragma mark -
698 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
699 /*************************************************************************/
700
701 - (void)setupWindow
702 {
703     ITDebugLog(@"Loading Preferences.nib.");
704     if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
705         ITDebugLog(@"Failed to load Preferences.nib.");
706         NSBeep();
707         return;
708     }
709 }
710
711 - (void)setupCustomizationTables
712 {
713     NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
714     ITDebugLog(@"Setting up table views.");
715     // Set the table view cells up
716     [imgCell setImageScaling:NSScaleNone];
717     [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
718     [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
719
720     // Register for drag and drop
721     [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
722         @"MenuTableViewPboardType",
723         @"AllTableViewPboardType",
724         nil]];
725     [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
726         @"MenuTableViewPboardType",
727         @"AllTableViewPboardType",
728         nil]];
729 }
730
731 - (void)setupMenuItems
732 {
733     NSEnumerator *itemEnum;
734     id            anItem;
735     ITDebugLog(@"Setting up table view arrays.");
736     // Set the list of items you can have.
737     availableItems = [[NSMutableArray alloc] initWithObjects:
738         @"separator",
739         @"trackInfo",
740         @"upcomingSongs",
741         @"playlists",
742         @"eqPresets",
743         @"songRating",
744         @"playPause",
745         @"nextTrack",
746         @"prevTrack",
747         @"fastForward",
748         @"rewind",
749         @"showPlayer",
750         @"quit",
751         nil];
752     
753     // Get our preferred menu
754     myItems = [[df arrayForKey:@"menu"] mutableCopy];
755     
756     // Delete items in the availableItems array that are already part of the menu
757     itemEnum = [myItems objectEnumerator];
758     while ( (anItem = [itemEnum nextObject]) ) {
759         if (![anItem isEqualToString:@"separator"]) {
760             [availableItems removeObject:anItem];
761         }
762     }
763     
764     // Items that show should a submenu image
765     submenuItems = [[NSArray alloc] initWithObjects:
766         @"upcomingSongs",
767         @"playlists",
768         @"eqPresets",
769         @"songRating",
770         nil];
771 }
772
773 - (void)setupUI
774 {
775     NSEnumerator   *keyArrayEnum;
776     NSString       *serverName;
777     NSData         *colorData;
778     int selectedBGStyle;
779     id anItem;
780     
781     [df setInteger:MT_CURRENT_VERSION forKey:@"appVersion"];
782     
783     ITDebugLog(@"Setting up preferences UI.");
784     // Fill in the number of songs in advance to show field
785     [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
786     
787     // Fill hot key array
788     keyArrayEnum = [hotKeysArray objectEnumerator];
789     
790     while ( (anItem = [keyArrayEnum nextObject]) ) {
791         if ([df objectForKey:anItem]) {
792             ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
793             [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
794         } else {
795             [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
796         }
797     }
798     
799     ITDebugLog(@"Setting up track info checkboxes.");
800     // Check current track info buttons
801     [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
802     [nameCheckbox setState:NSOnState];  // Song info will ALWAYS show song title.
803     [nameCheckbox setEnabled:NO];  // Song info will ALWAYS show song title.
804     [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
805     [composerCheckbox setState:[df boolForKey:@"showComposer"] ? NSOnState : NSOffState];
806     [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
807     [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
808     [playCountCheckbox setState:[df boolForKey:@"showPlayCount"] ? NSOnState : NSOffState];
809     [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
810     [albumArtworkCheckbox setState:[df boolForKey:@"showAlbumArtwork"] ? NSOnState : NSOffState];
811     
812     if ([df boolForKey:@"runScripts"]) {
813         [runScriptsCheckbox setState:NSOnState];
814         [showScriptsButton setEnabled:YES];
815     } else {
816         [showScriptsButton setEnabled:NO];
817     }
818     
819     // Set the launch at login checkbox state
820     ITDebugLog(@"Setting launch at login state.");
821     if (ITDoesApplicationLaunchOnLogin([[NSBundle mainBundle] bundlePath])) {
822         [launchAtLoginCheckbox setState:NSOnState];
823     }
824     
825     // Set the launch player checkbox state
826     ITDebugLog(@"Setting launch player with MenuTunes state.");
827     [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
828     
829     // Setup the positioning controls
830     [positionMatrix selectCellAtRow:[df integerForKey:@"statusWindowVerticalPosition"]
831                              column:[df integerForKey:@"statusWindowHorizontalPosition"]];
832     
833     // Setup effects controls
834     // Populate the effects popups
835     [appearanceEffectPopup setAutoenablesItems:NO];
836     [vanishEffectPopup     setAutoenablesItems:NO];
837     [self repopulateEffectPopupsForVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"] 
838                                  horizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
839     
840     // Attempt to find the pref'd effect in the list.
841     // If it's not there, use cut/dissolve.
842     if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])] ) {
843         [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])]];
844     } else {
845         [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
846     }
847     
848     if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])] ) {
849         [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])]];
850     } else {
851         [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
852     }
853     
854     [appearanceSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowAppearanceSpeed"]) )];
855     [vanishSpeedSlider     setFloatValue:( -([df floatForKey:@"statusWindowVanishSpeed"]) )];
856     [vanishDelaySlider     setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
857
858     // Setup General Controls
859     selectedBGStyle = [df integerForKey:@"statusWindowBackgroundMode"];
860     [backgroundStylePopup selectItem:[backgroundStylePopup itemAtIndex:[backgroundStylePopup indexOfItemWithTag:selectedBGStyle]]];
861
862     if ( selectedBGStyle == ITTSWBackgroundColored ) {
863         [backgroundColorWell  setEnabled:YES];
864         [backgroundColorPopup setEnabled:YES];
865     } else {
866         [backgroundColorWell  setEnabled:NO];
867         [backgroundColorPopup setEnabled:NO];
868     }
869
870     colorData = [df dataForKey:@"statusWindowBackgroundColor"];
871
872     if ( colorData ) {
873         [backgroundColorWell setColor:(NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
874     } else {
875         [backgroundColorWell setColor:[NSColor blueColor]];
876     }
877     
878     [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
879     
880     [windowSizingPopup selectItem:[windowSizingPopup itemAtIndex:[windowSizingPopup indexOfItemWithTag:[df integerForKey:@"statusWindowSizing"]]]];
881
882     // Setup the sharing controls
883     if ([df boolForKey:@"enableSharing"]) {
884         [shareMenuTunesCheckbox setState:NSOnState];
885         [useSharedMenuTunesCheckbox setEnabled:NO];
886         [selectSharedPlayerButton setEnabled:NO];
887         [passwordTextField setEnabled:YES];
888         [nameTextField setEnabled:YES];
889     } else if ([df boolForKey:@"useSharedPlayer"]) {
890         [useSharedMenuTunesCheckbox setState:NSOnState];
891         [shareMenuTunesCheckbox setEnabled:NO];
892         [selectSharedPlayerButton setEnabled:YES];
893     }
894     
895     [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
896     
897     serverName = [df stringForKey:@"sharedPlayerName"];
898     if (!serverName || [serverName length] == 0) {
899         serverName = @"MenuTunes Shared Player";
900     }
901     [nameTextField setStringValue:serverName];
902     
903     [selectPlayerBox setContentView:zeroConfView];
904     if ([[df dataForKey:@"sharedPlayerPassword"] length] > 0) {
905         [passwordTextField setStringValue:@"p4s5w0rdMT1.2"];
906     } else {
907         [passwordTextField setStringValue:@""];
908     }
909     if ([df stringForKey:@"sharedPlayerHost"]) {
910         [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
911     }
912     
913     if ([[NetworkController sharedController] isConnectedToServer]) {
914         [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
915         [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
916     } else {
917         [selectedPlayerTextField setStringValue:@"No shared player selected."];
918         [locationTextField setStringValue:@"-"];
919     }
920 }
921
922 - (void)setStatusWindowEntryEffect:(Class)effectClass
923 {
924     StatusWindow *sw = [StatusWindow sharedWindow];
925     
926     float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
927     [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowAppearanceEffect"];
928     
929     [sw setEntryEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
930     [[sw entryEffect] setEffectTime:time];
931 }
932
933 - (void)setStatusWindowExitEffect:(Class)effectClass
934 {
935     StatusWindow *sw = [StatusWindow sharedWindow];
936     
937     float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
938     [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowVanishEffect"];
939     
940     [sw setExitEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
941     [[sw exitEffect] setEffectTime:time];
942 }
943
944 - (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update
945 {
946     [(ITTSWBackgroundView *)[[StatusWindow sharedWindow] contentView] setBackgroundColor:color];
947     [df setObject:[NSArchiver archivedDataWithRootObject:color] forKey:@"statusWindowBackgroundColor"];
948     
949     if ( update ) {
950         [backgroundColorWell setColor:color];
951     }
952 }
953
954 - (void)repopulateEffectPopupsForVerticalPosition:(ITVerticalWindowPosition)vPos horizontalPosition:(ITHorizontalWindowPosition)hPos
955 {
956     NSEnumerator *effectEnum = [effectClasses objectEnumerator];
957     id anItem;
958     
959     [appearanceEffectPopup removeAllItems];
960     [vanishEffectPopup     removeAllItems];
961     
962     while ( (anItem = [effectEnum nextObject]) ) {
963         [appearanceEffectPopup addItemWithTitle:[anItem effectName]];
964         [vanishEffectPopup     addItemWithTitle:[anItem effectName]];
965         
966         [[appearanceEffectPopup lastItem] setRepresentedObject:anItem];
967         [[vanishEffectPopup     lastItem] setRepresentedObject:anItem];
968         
969         if ( [self effect:anItem supportsVerticalPosition:vPos withHorizontalPosition:hPos] ) {
970             [[appearanceEffectPopup lastItem] setEnabled:YES];
971             [[vanishEffectPopup     lastItem] setEnabled:YES];
972         } else {
973             [[appearanceEffectPopup lastItem] setEnabled:NO];
974             [[vanishEffectPopup     lastItem] setEnabled:NO];
975         }
976     }
977     
978 }
979
980 - (BOOL)effect:(Class)effectClass supportsVerticalPosition:(ITVerticalWindowPosition)vPos withHorizontalPosition:(ITHorizontalWindowPosition)hPos
981 {
982     BOOL valid = NO;
983     
984     if ( vPos == ITWindowPositionTop ) {
985         if ( hPos == ITWindowPositionLeft ) {
986             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Left"] boolValue] ) ;
987         } else if ( hPos == ITWindowPositionCenter ) {
988             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Center"] boolValue] );
989         } else if ( hPos == ITWindowPositionRight ) {
990             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Right"] boolValue] );
991         }
992     } else if ( vPos == ITWindowPositionMiddle ) {
993         if ( hPos == ITWindowPositionLeft ) {
994             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Left"] boolValue] );
995         } else if ( hPos == ITWindowPositionCenter ) {
996             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Center"] boolValue] );
997         } else if ( hPos == ITWindowPositionRight ) {
998             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Right"] boolValue] );
999         }
1000     } else if ( vPos == ITWindowPositionBottom ) {
1001         if ( hPos == ITWindowPositionLeft ) {
1002             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Left"] boolValue] );
1003         } else if ( hPos == ITWindowPositionCenter ) {
1004             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Center"] boolValue] );
1005         } else if ( hPos == ITWindowPositionRight ) {
1006             valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Right"] boolValue] );
1007         }
1008     }
1009     
1010     return valid;
1011 }
1012
1013 - (IBAction)changeMenus:(id)sender
1014 {
1015     ITDebugLog(@"Synchronizing menus");
1016     [df setObject:myItems forKey:@"menu"];
1017     [df synchronize];
1018     
1019     //If we're connected over a network, refresh the menu immediately
1020     if ([[NetworkController sharedController] isConnectedToServer]) {
1021         [controller timerUpdate];
1022     }
1023 }
1024
1025
1026 /*************************************************************************/
1027 #pragma mark -
1028 #pragma mark NSWindow DELEGATE METHODS
1029 /*************************************************************************/
1030
1031 - (void)windowWillClose:(NSNotification *)note
1032 {
1033     [(MainController *)controller closePreferences]; 
1034 }
1035
1036 /*************************************************************************/
1037 #pragma mark -
1038 #pragma mark NSTextField DELEGATE METHODS
1039 /*************************************************************************/
1040
1041 - (void)controlTextDidChange:(NSNotification*)note
1042 {
1043     if ([note object] == hostTextField) {
1044         if ([[hostTextField stringValue] length] == 0) {
1045             [sharingPanelOKButton setEnabled:NO];
1046         } else {
1047             [sharingPanelOKButton setEnabled:YES];
1048         }
1049     }
1050 }
1051
1052 /*************************************************************************/
1053 #pragma mark -
1054 #pragma mark NSTableView DATASOURCE METHODS
1055 /*************************************************************************/
1056
1057 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1058 {
1059     if (aTableView == menuTableView) {
1060         return [myItems count];
1061     } else if (aTableView == allTableView) {
1062         return [availableItems count];
1063     } else if (aTableView == hotKeysTableView) {
1064         return [hotKeysArray count];
1065     } else {
1066         return [[[NetworkController sharedController] remoteServices] count];
1067     }
1068 }
1069
1070 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
1071 {
1072     if (aTableView == menuTableView) {
1073         NSString *object = [myItems objectAtIndex:rowIndex];
1074         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1075             if ([object isEqualToString:@"showPlayer"]) {
1076                 NSString *string = nil;
1077                 NS_DURING
1078                     string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1079                 NS_HANDLER
1080                     [controller networkError:localException];
1081                 NS_ENDHANDLER
1082                 return string;
1083             }
1084             return NSLocalizedString(object, @"ERROR");
1085         } else {
1086             if ([submenuItems containsObject:object])
1087             {
1088                 return [NSImage imageNamed:@"submenu"];
1089             } else {
1090                 return nil;
1091             }
1092         }
1093     } else if (aTableView == allTableView) {
1094         NSString *object = [availableItems objectAtIndex:rowIndex];
1095         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1096             if ([object isEqualToString:@"showPlayer"]) {
1097                 NSString *string = nil;
1098                 NS_DURING
1099                     string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1100                 NS_HANDLER
1101                     [controller networkError:localException];
1102                 NS_ENDHANDLER
1103                 return string;
1104             }
1105             return NSLocalizedString(object, @"ERROR");
1106         } else {
1107             if ([submenuItems containsObject:object]) {
1108                 return [NSImage imageNamed:@"submenu"];
1109             } else {
1110                 return nil;
1111             }
1112         }
1113     } else if (aTableView == hotKeysTableView) {
1114         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1115             return [hotKeyNamesArray objectAtIndex:rowIndex];
1116         } else {
1117             return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
1118         }
1119     } else {
1120         return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
1121     }
1122 }
1123
1124 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
1125 {
1126     if (tableView == menuTableView) {
1127         [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
1128         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
1129         return YES;
1130     }
1131     
1132     if (tableView == allTableView) {
1133         [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
1134         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
1135         return YES;
1136     }
1137     return NO;
1138 }
1139
1140 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
1141 {
1142     NSPasteboard *pb;
1143     int dragRow;
1144     NSString *dragData, *temp;
1145     
1146     pb = [info draggingPasteboard];
1147     
1148     if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
1149         dragData = [pb stringForType:@"MenuTableViewPboardType"];
1150         dragRow = [dragData intValue];
1151         temp = [myItems objectAtIndex:dragRow];
1152         
1153         if (tableView == menuTableView) {
1154             [myItems insertObject:temp atIndex:row];
1155             if (row > dragRow) {
1156                 [myItems removeObjectAtIndex:dragRow];
1157             } else {
1158                 [myItems removeObjectAtIndex:dragRow + 1];
1159             }
1160         } else if (tableView == allTableView) {
1161             if (![temp isEqualToString:@"separator"]) {
1162                 [availableItems addObject:temp];
1163             }
1164             [myItems removeObjectAtIndex:dragRow];
1165         }
1166     } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
1167         dragData = [pb stringForType:@"AllTableViewPboardType"];
1168         dragRow = [dragData intValue];
1169         temp = [availableItems objectAtIndex:dragRow];
1170         
1171         [myItems insertObject:temp atIndex:row];
1172         
1173         if (![temp isEqualToString:@"separator"]) {
1174             [availableItems removeObjectAtIndex:dragRow];
1175         }
1176     }
1177     
1178     [menuTableView reloadData];
1179     [allTableView reloadData];
1180     [self changeMenus:self];
1181     return YES;
1182 }
1183
1184 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
1185 {
1186     if (tableView == allTableView) {
1187         if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
1188             return NSDragOperationNone;
1189         }
1190         
1191         if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
1192             NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
1193             if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
1194                 return NSDragOperationNone;
1195             }
1196         }
1197         
1198         [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
1199         return NSDragOperationGeneric;
1200     }
1201     
1202     if (operation == NSTableViewDropOn || row == -1)
1203     {
1204         return NSDragOperationNone;
1205     }
1206     return NSDragOperationGeneric;
1207 }
1208
1209
1210 /*************************************************************************/
1211 #pragma mark -
1212 #pragma mark DEALLOCATION METHODS
1213 /*************************************************************************/
1214
1215 - (void)dealloc
1216 {
1217     [hotKeysArray release];
1218     [hotKeysDictionary release];
1219     [effectClasses release];
1220     [menuTableView setDataSource:nil];
1221     [allTableView setDataSource:nil];
1222     [controller release];
1223     [availableItems release];
1224     [submenuItems release];
1225     [myItems release];
1226     [df release];
1227 }
1228
1229 @end