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