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