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