Added more to the hotkeys preferences.
[MenuTunes.git] / PreferencesController.m
1 #import "PreferencesController.h"
2 #import "MainController.h"
3 #import "HotKeyCenter.h"
4 #import <ITKit/ITWindowPositioning.h>
5
6 #define SENDER_STATE (([sender state] == NSOnState) ? YES : NO)
7
8 /*************************************************************************/
9 #pragma mark -
10 #pragma mark PRIVATE INTERFACE
11 /*************************************************************************/
12
13 @interface PreferencesController (Private)
14 - (void)setupWindow;
15 - (void)setupCustomizationTables;
16 - (void)setupMenuItems;
17 - (void)setupUI;
18 - (IBAction)changeMenus:(id)sender;
19 - (void)setLaunchesAtLogin:(BOOL)flag;
20 @end
21
22
23 @implementation PreferencesController
24
25
26 /*************************************************************************/
27 #pragma mark -
28 #pragma mark STATIC VARIABLES
29 /*************************************************************************/
30
31 static PreferencesController *prefs = nil;
32
33
34 /*************************************************************************/
35 #pragma mark -
36 #pragma mark INITIALIZATION METHODS
37 /*************************************************************************/
38
39 + (PreferencesController *)sharedPrefs;
40 {
41     if (! prefs) {
42         prefs = [[self alloc] init];
43     }
44     return prefs;
45 }
46
47 - (id)init
48 {
49     if ( (self = [super init]) ) {
50         df = [[NSUserDefaults standardUserDefaults] retain];
51         hotKeysDictionary = [[NSMutableDictionary alloc] init];
52         controller = nil;
53     }
54     return self;
55 }
56
57
58 /*************************************************************************/
59 #pragma mark -
60 #pragma mark ACCESSOR METHODS
61 /*************************************************************************/
62
63 - (id)controller
64 {
65     return controller;
66 }
67
68 - (void)setController:(id)object
69 {
70     [controller autorelease];
71     controller = [object retain];
72 }
73
74
75 /*************************************************************************/
76 #pragma mark -
77 #pragma mark INSTANCE METHODS
78 /*************************************************************************/
79
80 - (IBAction)showPrefsWindow:(id)sender
81 {
82     if (! window) {  // If window does not exist yet, then the nib hasn't been loaded.
83         [self setupWindow];  // Load in the nib, and perform any initial setup.
84         [self setupCustomizationTables];  // Setup the DnD manu config tables.
85         [self setupMenuItems];  // Setup the arrays of menu items
86         [self setupUI]; // Sets up additional UI
87         [window setDelegate:self];
88     }
89     
90     [window setLevel:NSStatusWindowLevel];
91     [window center];
92     [window makeKeyAndOrderFront:self];
93     [NSApp activateIgnoringOtherApps:YES];
94 }
95
96 - (IBAction)changeGeneralSetting:(id)sender
97 {
98     BOOL rebuildRequired = NO;
99
100     if ( [sender tag] == 1010) {
101         [self setLaunchesAtLogin:SENDER_STATE];
102     } else if ( [sender tag] == 1020) {
103         [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
104     } else if ( [sender tag] == 1030) {
105         [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
106         rebuildRequired = YES;
107     } else if ( [sender tag] == 1040) {
108         // This will not be executed.  Song info always shows the title of the song.
109         // [df setBool:SENDER_STATE forKey:@"showName"];
110         // rebuildRequired = YES;
111     } else if ( [sender tag] == 1050) {
112         [df setBool:SENDER_STATE forKey:@"showArtist"];
113         rebuildRequired = YES;
114     } else if ( [sender tag] == 1060) {
115         [df setBool:SENDER_STATE forKey:@"showAlbum"];
116         rebuildRequired = YES;
117     } else if ( [sender tag] == 1070) {
118         [df setBool:SENDER_STATE forKey:@"showTime"];
119         rebuildRequired = YES;
120     } else if ( [sender tag] == 1080) {
121         [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
122         rebuildRequired = YES;
123     } else if ( [sender tag] == 1090) {
124         [df setBool:SENDER_STATE forKey:@"showTrackRating"];
125         rebuildRequired = YES;
126     }
127
128     if ( rebuildRequired ) {
129         [controller rebuildMenu];
130         // redraw song info status window, or upcoming songs here
131     }
132
133     [df synchronize];
134 }
135
136 - (IBAction)changeStatusWindowSetting:(id)sender
137 {
138     if ( [sender tag] == 2010) {
139         [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
140         [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
141         // update the window's position here
142     } else if ( [sender tag] == 2020) {
143         // update screen selection
144     } else if ( [sender tag] == 2030) {
145         // Update appearance effect
146     } else if ( [sender tag] == 2040) {
147         // Update Vanish Effect
148     } else if ( [sender tag] == 2050) {
149         // Update appearance speed
150     } else if ( [sender tag] == 2060) {
151         // Update vanish speed
152     } else if ( [sender tag] == 2070) {
153         // Update vanish delay
154     } else if ( [sender tag] == 2080) {
155         // Update "Song Info window when song changes" setting.
156         [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
157     }
158 }
159
160 - (IBAction)changeHotKey:(id)sender
161 {
162     switch ([sender tag])
163     {
164         case 4010:
165             [self setKeyCombo:[hotKeysDictionary objectForKey:@"PlayPause"]];
166             [self setCurrentHotKey:@"PlayPause"];
167             break;
168         case 4020:
169             [self setKeyCombo:[hotKeysDictionary objectForKey:@"NextTrack"]];
170             [self setCurrentHotKey:@"NextTrack"];
171             break;
172         case 4030:
173             [self setKeyCombo:[hotKeysDictionary objectForKey:@"PrevTrack"]];
174             [self setCurrentHotKey:@"PrevTrack"];
175             break;
176         case 4035:
177             [self setKeyCombo:[hotKeysDictionary objectForKey:@"ToggleVisualizer"]];
178             [self setCurrentHotKey:@"ToggleVisualizer"];
179             break;
180         case 4040:
181             [self setKeyCombo:[hotKeysDictionary objectForKey:@"ToggleLoop"]];
182             [self setCurrentHotKey:@"ToggleLoop"];
183             break;
184         case 4050:
185             [self setKeyCombo:[hotKeysDictionary objectForKey:@"ToggleShuffle"]];
186             [self setCurrentHotKey:@"ToggleShuffle"];
187             break;
188         case 4060:
189             [self setKeyCombo:[hotKeysDictionary objectForKey:@"TrackInfo"]];
190             [self setCurrentHotKey:@"TrackInfo"];
191             break;
192         case 4070:
193             [self setKeyCombo:[hotKeysDictionary objectForKey:@"UpcomingSongs"]];
194             [self setCurrentHotKey:@"UpcomingSongs"];
195             break;
196         case 4080:
197             [self setKeyCombo:[hotKeysDictionary objectForKey:@"IncrementVolume"]];
198             [self setCurrentHotKey:@"IncrementVolume"];
199             break;
200         case 4090:
201             [self setKeyCombo:[hotKeysDictionary objectForKey:@"DecrementVolume"]];
202             [self setCurrentHotKey:@"DecrementVolume"];
203             break;
204         case 4100:
205             [self setKeyCombo:[hotKeysDictionary objectForKey:@"IncrementRating"]];
206             [self setCurrentHotKey:@"IncrementRating"];
207             break;
208         case 4110:
209             [self setKeyCombo:[hotKeysDictionary objectForKey:@"DecrementRating"]];
210             [self setCurrentHotKey:@"DecrementRating"];
211             break;
212     }
213 }
214
215 - (void)registerDefaults
216 {
217     BOOL found = NO;
218     NSMutableDictionary *loginWindow;
219     NSMutableArray *loginArray;
220     NSEnumerator *loginEnum;
221     id anItem;
222
223     [df setObject:[NSArray arrayWithObjects:
224         @"Play/Pause",
225         @"Next Track",
226         @"Previous Track",
227         @"Fast Forward",
228         @"Rewind",
229         @"Show Player",
230         @"<separator>",
231         @"Upcoming Songs",
232         @"Playlists",
233         @"Song Rating",
234         @"<separator>",
235         @"PreferencesÉ",
236         @"Quit",
237         @"<separator>",
238         @"Current Track Info",
239         nil] forKey:@"menu"];
240
241     [df setInteger:5 forKey:@"SongsInAdvance"];
242     // [df setBool:YES forKey:@"showName"];  // Song info will always show song title.
243     [df setBool:YES forKey:@"showArtist"];
244     [df setBool:NO forKey:@"showAlbum"];
245     [df setBool:NO forKey:@"showTime"];
246
247     [df synchronize];
248     
249     loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
250     loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
251     loginEnum = [loginArray objectEnumerator];
252
253     while ( (anItem = [loginEnum nextObject]) ) {
254         if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
255             found = YES;
256         }
257     }
258
259     [loginWindow release];
260     
261     // This is teh sux
262     // We must fix it so it is no longer suxy
263     if (!found) {
264         if (NSRunInformationalAlertPanel(@"Auto-launch MenuTunes", @"Would you like MenuTunes to automatically launch at login?", @"Yes", @"No", nil) == NSOKButton) {
265             AEDesc scriptDesc, resultDesc;
266             NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
267             ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
268
269             AECreateDesc(typeChar, [script cString], [script cStringLength],
270                          &scriptDesc);
271
272             OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
273
274             AEDisposeDesc(&scriptDesc);
275             AEDisposeDesc(&resultDesc);
276
277             CloseComponent(asComponent);
278         }
279     }
280 }
281
282 - (IBAction)cancelHotKey:(id)sender
283 {
284     [[NSNotificationCenter defaultCenter] removeObserver:self];
285     [NSApp endSheet:keyComboPanel];
286     [keyComboPanel orderOut:nil];
287 }
288
289 - (IBAction)clearHotKey:(id)sender
290 {
291     [self setKeyCombo:[KeyCombo clearKeyCombo]];
292 }
293
294 - (IBAction)okHotKey:(id)sender
295 {
296     NSString *string = [combo userDisplayRep];
297     NSEnumerator *enumerator = [hotKeysDictionary keyEnumerator];
298     NSString *enumKey;
299     BOOL duplicateCombo = NO;
300     
301     if (string == nil) {
302         string = @"";
303     }
304     
305     while ( (enumKey = [enumerator nextObject]) ) {
306         if (![enumKey isEqualToString:currentHotKey]) {
307             if (![combo isEqual:[KeyCombo clearKeyCombo]] &&
308                  [combo isEqual:[hotKeysDictionary objectForKey:enumKey]]) {
309                 [window setLevel:NSNormalWindowLevel];
310                 if ( NSRunAlertPanel(@"Duplicate Key Combo", @"The specified key combo is already in use...", @"Replace", @"Cancel", nil) ) {
311                     [hotKeysDictionary setObject:[KeyCombo clearKeyCombo] forKey:currentHotKey];
312                     if ([enumKey isEqualToString:@"PlayPause"]) {
313                         [playPauseButton setTitle:@""];
314                     } else if ([enumKey isEqualToString:@"NextTrack"]) {
315                         [nextTrackButton setTitle:@""];
316                     } else if ([enumKey isEqualToString:@"PrevTrack"]) {
317                         [previousTrackButton setTitle:@""];
318                     } else if ([enumKey isEqualToString:@"ToggleVisualizer"]) {
319                         [visualizerButton setTitle:@""];
320                     } else if ([enumKey isEqualToString:@"TrackInfo"]) {
321                         [trackInfoButton setTitle:@""];
322                     } else if ([enumKey isEqualToString:@"UpcomingSongs"]) {
323                         [upcomingSongsButton setTitle:@""];
324                     } else if ([enumKey isEqualToString:@"IncrementVolume"]) {
325                         [volumeIncrementButton setTitle:@""];
326                     } else if ([enumKey isEqualToString:@"DecrementVolume"]) {
327                         [volumeDecrementButton setTitle:@""];
328                     } else if ([enumKey isEqualToString:@"IncrementRating"]) {
329                         [ratingIncrementButton setTitle:@""];
330                     } else if ([enumKey isEqualToString:@"DecrementRating"]) {
331                         [ratingDecrementButton setTitle:@""];
332                     } else if ([enumKey isEqualToString:@"ToggleShuffle"]) {
333                         [toggleShuffleButton setTitle:@""];
334                     } else if ([enumKey isEqualToString:@"ToggleLoop"]) {
335                         [toggleLoopButton setTitle:@""];
336                     }
337                     [df setKeyCombo:[KeyCombo clearKeyCombo] forKey:enumKey];
338                 } else {
339                     return;
340                 }
341                 [window setLevel:NSStatusWindowLevel];
342             }
343         }
344     }
345     
346     if (!duplicateCombo) {
347         [hotKeysDictionary setObject:combo forKey:currentHotKey];
348         [df setKeyCombo:combo forKey:currentHotKey];
349         
350         if ([currentHotKey isEqualToString:@"PlayPause"]) {
351             [playPauseButton setTitle:string];
352         } else if ([currentHotKey isEqualToString:@"NextTrack"]) {
353             [nextTrackButton setTitle:string];
354         } else if ([currentHotKey isEqualToString:@"PrevTrack"]) {
355             [previousTrackButton setTitle:string];
356         } else if ([currentHotKey isEqualToString:@"ToggleVisualizer"]) {
357             [visualizerButton setTitle:string];
358         } else if ([currentHotKey isEqualToString:@"TrackInfo"]) {
359             [trackInfoButton setTitle:string];
360         } else if ([currentHotKey isEqualToString:@"UpcomingSongs"]) {
361             [upcomingSongsButton setTitle:string];
362         } else if ([currentHotKey isEqualToString:@"IncrementVolume"]) {
363             [volumeIncrementButton setTitle:string];
364         } else if ([currentHotKey isEqualToString:@"DecrementVolume"]) {
365             [volumeDecrementButton setTitle:string];
366         } else if ([currentHotKey isEqualToString:@"IncrementRating"]) {
367             [ratingIncrementButton setTitle:string];
368         } else if ([currentHotKey isEqualToString:@"DecrementRating"]) {
369             [ratingDecrementButton setTitle:string];
370         } else if ([currentHotKey isEqualToString:@"ToggleShuffle"]) {
371             [toggleShuffleButton setTitle:string];
372         } else if ([currentHotKey isEqualToString:@"ToggleLoop"]) {
373             [toggleLoopButton setTitle:string];
374         }
375         //[controller rebuildMenu];
376     }
377     [self cancelHotKey:sender];
378 }
379
380
381
382 /*************************************************************************/
383 #pragma mark -
384 #pragma mark HOTKEY SUPPORT METHODS
385 /*************************************************************************/
386
387 - (void)setCurrentHotKey:(NSString *)key
388 {
389     [currentHotKey autorelease];
390     currentHotKey = [key copy];
391     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
392     [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
393 }
394
395 - (void)keyEvent:(NSNotification *)note
396 {
397     NSDictionary *info = [note userInfo];
398     short keyCode;
399     long modifiers;
400     KeyCombo *newCombo;
401     
402     keyCode = [[info objectForKey:@"KeyCode"] shortValue];
403     modifiers = [[info objectForKey:@"Modifiers"] longValue];
404     
405     newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
406     [self setKeyCombo:newCombo];
407 }
408
409 - (void)setKeyCombo:(KeyCombo *)newCombo
410 {
411     NSString *string;
412     [combo release];
413     combo = [newCombo copy];
414     
415     string = [combo userDisplayRep];
416     if (string == nil) {
417         string = @"";
418     }
419     [keyComboField setStringValue:string];
420 }
421
422
423 /*************************************************************************/
424 #pragma mark -
425 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
426 /*************************************************************************/
427
428 - (void)setupWindow
429 {
430     if ( ! [NSBundle loadNibNamed:@"Preferences" owner:self] ) {
431         NSLog( @"Failed to load Preferences.nib" );
432         NSBeep();
433         return;
434     }
435 }
436
437 - (void)setupCustomizationTables
438 {
439     NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
440     
441     // Set the table view cells up
442     [imgCell setImageScaling:NSScaleNone];
443     [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
444     [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
445
446     // Register for drag and drop
447     [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
448         @"MenuTableViewPboardType",
449         @"AllTableViewPboardType",
450         nil]];
451     [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
452         @"MenuTableViewPboardType",
453         @"AllTableViewPboardType",
454         nil]];
455 }
456
457 - (void)setupMenuItems
458 {
459     NSEnumerator *itemEnum;
460     id            anItem;
461     // Set the list of items you can have.
462     availableItems = [[NSMutableArray alloc] initWithObjects:
463         @"Current Track Info",
464         @"Upcoming Songs",
465         @"Playlists",
466         @"EQ Presets",
467         @"Song Rating",
468         @"Play/Pause",
469         @"Next Track",
470         @"Previous Track",
471         @"Fast Forward",
472         @"Rewind",
473         @"Show Player",
474         @"<separator>",
475         nil];
476     
477     // Get our preferred menu
478     myItems = [[df arrayForKey:@"menu"] mutableCopy];
479     
480     // Delete items in the availableItems array that are already part of the menu
481     itemEnum = [myItems objectEnumerator];
482     while ( (anItem = [itemEnum nextObject]) ) {
483         if ( ! [anItem isEqualToString:@"<separator>"] ) {
484             [availableItems removeObject:anItem];
485         }
486     }
487     
488     // Items that show should a submenu image
489     submenuItems = [[NSArray alloc] initWithObjects:
490         @"Upcoming Songs",
491         @"Playlists",
492         @"EQ Presets",
493         @"Song Rating",
494         nil];
495 }
496
497 - (void)setupUI
498 {
499     NSMutableDictionary *loginwindow;
500     NSMutableArray *loginarray;
501     NSEnumerator *loginEnum;
502     id anItem;
503     
504     // Fill in the number of songs in advance to show field
505     [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
506     
507     // Fill in hot key buttons
508     if ([df objectForKey:@"PlayPause"]){
509         anItem = [df keyComboForKey:@"PlayPause"];
510         [hotKeysDictionary setObject:anItem forKey:@"PlayPause"];
511         [playPauseButton setTitle:[anItem userDisplayRep]];
512     } else {
513         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"PlayPause"];
514     }
515     
516     if ([df objectForKey:@"NextTrack"]) {
517         anItem = [df keyComboForKey:@"NextTrack"];
518         [hotKeysDictionary setObject:anItem forKey:@"NextTrack"];
519         [nextTrackButton setTitle:[anItem userDisplayRep]];
520     } else {
521         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"NextTrack"];
522     }
523     
524     if ([df objectForKey:@"PrevTrack"]) {
525         anItem = [df keyComboForKey:@"PrevTrack"];
526         [hotKeysDictionary setObject:anItem forKey:@"PrevTrack"];
527         [previousTrackButton setTitle:[anItem userDisplayRep]];
528     } else {
529         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"PrevTrack"];
530     }
531     
532     if ([df objectForKey:@"ToggleVisualizer"]) {
533         anItem = [df keyComboForKey:@"ToggleVisualizer"];
534         [hotKeysDictionary setObject:anItem forKey:@"ToggleVisualizer"];
535         [visualizerButton setTitle:[anItem userDisplayRep]];
536     } else {
537         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"ToggleVisualizer"];
538     }
539     
540     if ([df objectForKey:@"TrackInfo"]) {
541         anItem = [df keyComboForKey:@"TrackInfo"];
542         [hotKeysDictionary setObject:anItem forKey:@"TrackInfo"];
543         [trackInfoButton setTitle:[anItem userDisplayRep]];
544     } else {
545         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"TrackInfo"];
546     }
547     
548     if ([df objectForKey:@"UpcomingSongs"]) {
549         anItem = [df keyComboForKey:@"UpcomingSongs"];
550         [hotKeysDictionary setObject:anItem forKey:@"UpcomingSongs"];
551         [upcomingSongsButton setTitle:[anItem userDisplayRep]];
552     } else {
553         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"UpcomingSongs"];
554     }
555     
556     if ([df objectForKey:@"IncrementVolume"]) {
557         anItem = [df keyComboForKey:@"IncrementVolume"];
558         [hotKeysDictionary setObject:anItem forKey:@"IncrementVolume"];
559         [volumeIncrementButton setTitle:[anItem userDisplayRep]];
560     } else {
561         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"IncrementVolume"];
562     }
563     
564     if ([df objectForKey:@"DecrementVolume"]) {
565         anItem = [df keyComboForKey:@"DecrementVolume"];
566         [hotKeysDictionary setObject:anItem forKey:@"DecrementVolume"];
567         [volumeDecrementButton setTitle:[anItem userDisplayRep]];
568     } else {
569         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"DecrementVolume"];
570     }
571     
572     if ([df objectForKey:@"IncrementRating"]) {
573         anItem = [df keyComboForKey:@"IncrementRating"];
574         [hotKeysDictionary setObject:anItem forKey:@"IncrementRating"];
575         [ratingIncrementButton setTitle:[anItem userDisplayRep]];
576     } else {
577         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"IncrementRating"];
578     }
579     
580     if ([df objectForKey:@"DecrementRating"]) {
581         anItem = [df keyComboForKey:@"DecrementRating"];
582         [hotKeysDictionary setObject:anItem forKey:@"DecrementRating"];
583         [ratingDecrementButton setTitle:[anItem userDisplayRep]];
584     } else {
585         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"DecrementRating"];
586     }
587     
588     if ([df objectForKey:@"ToggleLoop"]) {
589         anItem = [df keyComboForKey:@"ToggleLoop"];
590         [hotKeysDictionary setObject:anItem forKey:@"ToggleLoop"];
591         [toggleLoopButton setTitle:[anItem userDisplayRep]];
592     } else {
593         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"ToggleLoop"];
594     }
595     
596     if ([df objectForKey:@"ToggleShuffle"]) {
597         anItem = [df keyComboForKey:@"ToggleShuffle"];
598         [hotKeysDictionary setObject:anItem forKey:@"ToggleShuffle"];
599         [toggleShuffleButton setTitle:[anItem userDisplayRep]];
600     } else {
601         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"ToggleShuffle"];
602     }
603     
604     // Check current track info buttons
605     [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
606     [nameCheckbox setState:NSOnState];  // Song info will ALWAYS show song title.
607     [nameCheckbox setEnabled:NO];  // Song info will ALWAYS show song title.
608     [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
609     [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
610     
611     // Set the launch at login checkbox state
612     [df synchronize];
613     loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
614     loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
615     
616     loginEnum = [loginarray objectEnumerator];
617     while ( (anItem = [loginEnum nextObject]) ) {
618         if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
619             [launchAtLoginCheckbox setState:NSOnState];
620         }
621     }
622 }
623
624 - (IBAction)changeMenus:(id)sender
625 {
626     [df setObject:myItems forKey:@"menu"];
627     [df synchronize];
628     [controller rebuildMenu];
629 }
630
631 - (void)setLaunchesAtLogin:(BOOL)flag
632 {
633     if ( flag ) {
634         NSMutableDictionary *loginwindow;
635         NSMutableArray *loginarray;
636         ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);;
637         int i;
638         BOOL skip = NO;
639
640         [df synchronize];
641         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
642         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
643
644         for (i = 0; i < [loginarray count]; i++) {
645             NSDictionary *tempDict = [loginarray objectAtIndex:i];
646             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
647                 skip = YES;
648             }
649         }
650
651         if (!skip) {
652             AEDesc scriptDesc, resultDesc;
653             NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
654
655             AECreateDesc(typeChar, [script cString], [script cStringLength],
656                          &scriptDesc);
657
658             OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
659
660             AEDisposeDesc(&scriptDesc);
661             AEDisposeDesc(&resultDesc);
662             CloseComponent(temp);
663         }
664
665     } else {
666         NSMutableDictionary *loginwindow;
667         NSMutableArray *loginarray;
668         int i;
669
670         [df synchronize];
671         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
672         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
673
674         for (i = 0; i < [loginarray count]; i++) {
675             NSDictionary *tempDict = [loginarray objectAtIndex:i];
676             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
677                 [loginarray removeObjectAtIndex:i];
678                 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
679                 [df synchronize];
680                 break;
681             }
682         }
683     }
684 }
685
686
687 /*************************************************************************/
688 #pragma mark -
689 #pragma mark NSWindow DELEGATE METHODS
690 /*************************************************************************/
691
692 - (void)windowWillClose:(NSNotification *)note
693 {
694     [(MainController *)controller closePreferences]; 
695 }
696
697
698 /*************************************************************************/
699 #pragma mark -
700 #pragma mark NSTableView DATASOURCE METHODS
701 /*************************************************************************/
702
703 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
704 {
705     if (aTableView == menuTableView) {
706         return [myItems count];
707     } else {
708         return [availableItems count];
709     }
710 }
711
712 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
713 {
714     if (aTableView == menuTableView) {
715         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
716             NSString *object = [myItems objectAtIndex:rowIndex];
717             if ([object isEqualToString:@"Show Player"]) {
718                 return [NSString stringWithFormat:@"Show %@", [[controller currentRemote] playerSimpleName]];
719             }
720             return object;
721         } else {
722             if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
723             {
724                 return [NSImage imageNamed:@"submenu"];
725             } else {
726                 return nil;
727             }
728         }
729     } else {
730         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
731             return [availableItems objectAtIndex:rowIndex];
732         } else {
733             if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
734                 return [NSImage imageNamed:@"submenu"];
735             } else {
736                 return nil;
737             }
738         }
739     }
740 }
741
742 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
743 {
744     if (tableView == menuTableView) {
745         [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
746         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
747         return YES;
748     }
749     
750     if (tableView == allTableView) {
751         [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
752         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
753         return YES;
754     }
755     return NO;
756 }
757
758 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
759 {
760     NSPasteboard *pb;
761     int dragRow;
762     NSString *dragData, *temp;
763     
764     pb = [info draggingPasteboard];
765     
766     if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
767         dragData = [pb stringForType:@"MenuTableViewPboardType"];
768         dragRow = [dragData intValue];
769         temp = [myItems objectAtIndex:dragRow];
770         [myItems removeObjectAtIndex:dragRow];
771         
772         if (tableView == menuTableView) {
773             if (row > dragRow) {
774                 [myItems insertObject:temp atIndex:row - 1];
775             } else {
776                 [myItems insertObject:temp atIndex:row];
777             }
778         } else {
779             if (![temp isEqualToString:@"<separator>"]) {
780                 [availableItems addObject:temp];
781             }
782         }
783     } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
784         dragData = [pb stringForType:@"AllTableViewPboardType"];
785         dragRow = [dragData intValue];
786         temp = [availableItems objectAtIndex:dragRow];
787         
788         if (![temp isEqualToString:@"<separator>"]) {
789             [availableItems removeObjectAtIndex:dragRow];
790         }
791         [myItems insertObject:temp atIndex:row];
792     }
793     
794     [menuTableView reloadData];
795     [allTableView reloadData];
796     [self changeMenus:self];
797     return YES;
798 }
799
800 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
801 {
802     if (tableView == allTableView) {
803         if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
804             return NSDragOperationNone;
805         }
806         
807         if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
808             NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
809             if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
810                 return NSDragOperationNone;
811             }
812         }
813         
814         [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
815         return NSDragOperationGeneric;
816     }
817     
818     if (operation == NSTableViewDropOn || row == -1)
819     {
820         return NSDragOperationNone;
821     }
822     
823     return NSDragOperationGeneric;
824 }
825
826
827 /*************************************************************************/
828 #pragma mark -
829 #pragma mark DEALLOCATION METHODS
830 /*************************************************************************/
831
832 - (void)dealloc
833 {
834     [self setKeyCombo:nil];
835     [hotKeysDictionary release];
836     [keyComboPanel release];
837     [menuTableView setDataSource:nil];
838     [allTableView setDataSource:nil];
839     [controller release];
840     [availableItems release];
841     [submenuItems release];
842     [myItems release];
843     [df release];
844 }
845
846
847 @end