All the hotkeys now use the ITHotKey/ITKeyCombo classes now, which makes
[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:@""];
307                     } else if ([enumKey isEqualToString:@"NextTrack"]) {
308                         [nextTrackButton setTitle:@""];
309                     } else if ([enumKey isEqualToString:@"PrevTrack"]) {
310                         [previousTrackButton setTitle:@""];
311                     } else if ([enumKey isEqualToString:@"ShowPlayer"]) {
312                         [showPlayerButton setTitle:@""];
313                     } else if ([enumKey isEqualToString:@"TrackInfo"]) {
314                         [trackInfoButton setTitle:@""];
315                     } else if ([enumKey isEqualToString:@"UpcomingSongs"]) {
316                         [upcomingSongsButton setTitle:@""];
317                     } else if ([enumKey isEqualToString:@"IncrementVolume"]) {
318                         [volumeIncrementButton setTitle:@""];
319                     } else if ([enumKey isEqualToString:@"DecrementVolume"]) {
320                         [volumeDecrementButton setTitle:@""];
321                     } else if ([enumKey isEqualToString:@"IncrementRating"]) {
322                         [ratingIncrementButton setTitle:@""];
323                     } else if ([enumKey isEqualToString:@"DecrementRating"]) {
324                         [ratingDecrementButton setTitle:@""];
325                     } else if ([enumKey isEqualToString:@"ToggleShuffle"]) {
326                         [toggleShuffleButton setTitle:@""];
327                     } else if ([enumKey isEqualToString:@"ToggleLoop"]) {
328                         [toggleLoopButton setTitle:@""];
329                     }
330                     [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:enumKey];
331                 } else {
332                     return;
333                 }
334                 [window setLevel:NSStatusWindowLevel];
335             }
336         }
337     }
338     
339     [hotKeysDictionary setObject:combo forKey:currentHotKey];
340     [df setObject:[combo plistRepresentation] forKey:currentHotKey];
341     
342     if ([currentHotKey isEqualToString:@"PlayPause"]) {
343         [playPauseButton setTitle:string];
344     } else if ([currentHotKey isEqualToString:@"NextTrack"]) {
345         [nextTrackButton setTitle:string];
346     } else if ([currentHotKey isEqualToString:@"PrevTrack"]) {
347         [previousTrackButton setTitle:string];
348     } else if ([currentHotKey isEqualToString:@"ShowPlayer"]) {
349         [showPlayerButton setTitle:string];
350     } else if ([currentHotKey isEqualToString:@"TrackInfo"]) {
351         [trackInfoButton setTitle:string];
352     } else if ([currentHotKey isEqualToString:@"UpcomingSongs"]) {
353         [upcomingSongsButton setTitle:string];
354     } else if ([currentHotKey isEqualToString:@"IncrementVolume"]) {
355         [volumeIncrementButton setTitle:string];
356     } else if ([currentHotKey isEqualToString:@"DecrementVolume"]) {
357         [volumeDecrementButton setTitle:string];
358     } else if ([currentHotKey isEqualToString:@"IncrementRating"]) {
359         [ratingIncrementButton setTitle:string];
360     } else if ([currentHotKey isEqualToString:@"DecrementRating"]) {
361         [ratingDecrementButton setTitle:string];
362     } else if ([currentHotKey isEqualToString:@"ToggleShuffle"]) {
363         [toggleShuffleButton setTitle:string];
364     } else if ([currentHotKey isEqualToString:@"ToggleLoop"]) {
365         [toggleLoopButton setTitle:string];
366     }
367     [controller setupHotKeys];
368     [self cancelHotKey:sender];
369 }
370
371
372
373 /*************************************************************************/
374 #pragma mark -
375 #pragma mark HOTKEY SUPPORT METHODS
376 /*************************************************************************/
377
378 - (void)setCurrentHotKey:(NSString *)key
379 {
380     [currentHotKey autorelease];
381     currentHotKey = [key copy];
382     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:ITKeyBroadcasterKeyEvent object:nil];
383     [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
384 }
385
386 - (void)keyEvent:(NSNotification *)note
387 {
388     [self setKeyCombo:[[[note userInfo] objectForKey:@"keyCombo"] copy]];
389 }
390
391 - (void)setKeyCombo:(ITKeyCombo *)newCombo
392 {
393     NSString *string;
394     [combo release];
395     combo = [newCombo copy];
396     
397     string = [combo description];
398     if (string == nil) {
399         string = @"";
400     }
401     [keyComboField setStringValue:string];
402 }
403
404
405 /*************************************************************************/
406 #pragma mark -
407 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
408 /*************************************************************************/
409
410 - (void)setupWindow
411 {
412     if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
413         NSLog(@"MenuTunes: Failed to load Preferences.nib");
414         NSBeep();
415         return;
416     }
417 }
418
419 - (void)setupCustomizationTables
420 {
421     NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
422     
423     // Set the table view cells up
424     [imgCell setImageScaling:NSScaleNone];
425     [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
426     [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
427
428     // Register for drag and drop
429     [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
430         @"MenuTableViewPboardType",
431         @"AllTableViewPboardType",
432         nil]];
433     [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
434         @"MenuTableViewPboardType",
435         @"AllTableViewPboardType",
436         nil]];
437 }
438
439 - (void)setupMenuItems
440 {
441     NSEnumerator *itemEnum;
442     id            anItem;
443     // Set the list of items you can have.
444     availableItems = [[NSMutableArray alloc] initWithObjects:
445         @"trackInfo",
446         @"upcomingSongs",
447         @"playlists",
448         @"eqPresets",
449         @"songRating",
450         @"playPause",
451         @"nextTrack",
452         @"prevTrack",
453         @"fastForward",
454         @"rewind",
455         @"showPlayer",
456         @"separator",
457         nil];
458     
459     // Get our preferred menu
460     myItems = [[df arrayForKey:@"menu"] mutableCopy];
461     
462     // Delete items in the availableItems array that are already part of the menu
463     itemEnum = [myItems objectEnumerator];
464     while ( (anItem = [itemEnum nextObject]) ) {
465         if (![anItem isEqualToString:@"separator"]) {
466             [availableItems removeObject:anItem];
467         }
468     }
469     
470     // Items that show should a submenu image
471     submenuItems = [[NSArray alloc] initWithObjects:
472         @"upcomingSongs",
473         @"playlists",
474         @"eqPresets",
475         @"songRating",
476         nil];
477 }
478
479 - (void)setupUI
480 {
481     NSMutableDictionary *loginwindow;
482     NSMutableArray *loginarray;
483     NSEnumerator *loginEnum;
484     id anItem;
485     
486     // Fill in the number of songs in advance to show field
487     [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
488     
489     // Fill in hot key buttons
490     if ([df objectForKey:@"PlayPause"]) {
491         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PlayPause"]];
492         [hotKeysDictionary setObject:anItem forKey:@"PlayPause"];
493         [playPauseButton setTitle:[anItem description]];
494     } else {
495         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"PlayPause"];
496         [playPauseButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
497     }
498     
499     if ([df objectForKey:@"NextTrack"]) {
500         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"NextTrack"]];
501         [hotKeysDictionary setObject:anItem forKey:@"NextTrack"];
502         [nextTrackButton setTitle:[anItem description]];
503     } else {
504         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"NextTrack"];
505         [nextTrackButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
506     }
507     
508     if ([df objectForKey:@"PrevTrack"]) {
509         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"PrevTrack"]];
510         [hotKeysDictionary setObject:anItem forKey:@"PrevTrack"];
511         [previousTrackButton setTitle:[anItem description]];
512     } else {
513         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"PrevTrack"];
514         [previousTrackButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
515     }
516     
517     if ([df objectForKey:@"ShowPlayer"]) {
518         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ShowPlayer"]];
519         [hotKeysDictionary setObject:anItem forKey:@"ShowPlayer"];
520         [showPlayerButton setTitle:[anItem description]];
521     } else {
522         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"ShowPlayer"];
523         [showPlayerButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
524     }
525     
526     if ([df objectForKey:@"TrackInfo"]) {
527         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"TrackInfo"]];
528         [hotKeysDictionary setObject:anItem forKey:@"TrackInfo"];
529         [trackInfoButton setTitle:[anItem description]];
530     } else {
531         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"TrackInfo"];
532         [trackInfoButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
533     }
534     
535     if ([df objectForKey:@"UpcomingSongs"]) {
536         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"UpcomingSongs"]];
537         [hotKeysDictionary setObject:anItem forKey:@"UpcomingSongs"];
538         [upcomingSongsButton setTitle:[anItem description]];
539     } else {
540         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"UpcomingSongs"];
541         [upcomingSongsButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
542     }
543     
544     if ([df objectForKey:@"IncrementVolume"]) {
545         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementVolume"]];
546         [hotKeysDictionary setObject:anItem forKey:@"IncrementVolume"];
547         [volumeIncrementButton setTitle:[anItem description]];
548     } else {
549         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"IncrementVolume"];
550         [volumeIncrementButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
551     }
552     
553     if ([df objectForKey:@"DecrementVolume"]) {
554         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementVolume"]];
555         [hotKeysDictionary setObject:anItem forKey:@"DecrementVolume"];
556         [volumeDecrementButton setTitle:[anItem description]];
557     } else {
558         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"DecrementVolume"];
559         [volumeDecrementButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
560     }
561     
562     if ([df objectForKey:@"IncrementRating"]) {
563         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"IncrementRating"]];
564         [hotKeysDictionary setObject:anItem forKey:@"IncrementRating"];
565         [ratingIncrementButton setTitle:[anItem description]];
566     } else {
567         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"IncrementRating"];
568         [ratingIncrementButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
569     }
570     
571     if ([df objectForKey:@"DecrementRating"]) {
572         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"DecrementRating"]];
573         [hotKeysDictionary setObject:anItem forKey:@"DecrementRating"];
574         [ratingDecrementButton setTitle:[anItem description]];
575     } else {
576         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"DecrementRating"];
577         [ratingDecrementButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
578     }
579     
580     if ([df objectForKey:@"ToggleLoop"]) {
581         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleLoop"]];
582         [hotKeysDictionary setObject:anItem forKey:@"ToggleLoop"];
583         [toggleLoopButton setTitle:[anItem description]];
584     } else {
585         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"ToggleLoop"];
586         [toggleLoopButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
587     }
588     
589     if ([df objectForKey:@"ToggleShuffle"]) {
590         anItem = [ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:@"ToggleShuffle"]];
591         [hotKeysDictionary setObject:anItem forKey:@"ToggleShuffle"];
592         [toggleShuffleButton setTitle:[anItem description]];
593     } else {
594         [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:@"ToggleShuffle"];
595         [toggleShuffleButton setTitle:[[ITKeyCombo clearKeyCombo] description]];
596     }
597     
598     // Check current track info buttons
599     [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
600     [nameCheckbox setState:NSOnState];  // Song info will ALWAYS show song title.
601     [nameCheckbox setEnabled:NO];  // Song info will ALWAYS show song title.
602     [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
603     [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
604     [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
605     
606     // Set the launch at login checkbox state
607     [df synchronize];
608     loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
609     loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
610     
611     loginEnum = [loginarray objectEnumerator];
612     while ( (anItem = [loginEnum nextObject]) ) {
613         if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
614             [launchAtLoginCheckbox setState:NSOnState];
615         }
616     }
617 }
618
619 - (IBAction)changeMenus:(id)sender
620 {
621     [df setObject:myItems forKey:@"menu"];
622     [df synchronize];
623 }
624
625 - (void)setLaunchesAtLogin:(BOOL)flag
626 {
627     if ( flag ) {
628         NSMutableDictionary *loginwindow;
629         NSMutableArray *loginarray;
630         ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
631         int i;
632         BOOL skip = NO;
633
634         [df synchronize];
635         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
636         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
637
638         for (i = 0; i < [loginarray count]; i++) {
639             NSDictionary *tempDict = [loginarray objectAtIndex:i];
640             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
641                 skip = YES;
642             }
643         }
644
645         if (!skip) {
646             AEDesc scriptDesc, resultDesc;
647             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]];
648
649             AECreateDesc(typeChar, [script cString], [script cStringLength],
650                          &scriptDesc);
651
652             OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
653
654             AEDisposeDesc(&scriptDesc);
655             AEDisposeDesc(&resultDesc);
656             CloseComponent(temp);
657         }
658
659     } else {
660         NSMutableDictionary *loginwindow;
661         NSMutableArray *loginarray;
662         int i;
663
664         [df synchronize];
665         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
666         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
667
668         for (i = 0; i < [loginarray count]; i++) {
669             NSDictionary *tempDict = [loginarray objectAtIndex:i];
670             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
671                 [loginarray removeObjectAtIndex:i];
672                 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
673                 [df synchronize];
674                 break;
675             }
676         }
677     }
678 }
679
680
681 /*************************************************************************/
682 #pragma mark -
683 #pragma mark NSWindow DELEGATE METHODS
684 /*************************************************************************/
685
686 - (void)windowWillClose:(NSNotification *)note
687 {
688     [(MainController *)controller closePreferences]; 
689 }
690
691
692 /*************************************************************************/
693 #pragma mark -
694 #pragma mark NSTableView DATASOURCE METHODS
695 /*************************************************************************/
696
697 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
698 {
699     if (aTableView == menuTableView) {
700         return [myItems count];
701     } else {
702         return [availableItems count];
703     }
704 }
705
706 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
707 {
708     if (aTableView == menuTableView) {
709         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
710             NSString *object = [myItems objectAtIndex:rowIndex];
711             if ([object isEqualToString:@"Show Player"]) {
712                 return [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
713             }
714             return NSLocalizedString(object, @"ERROR");
715         } else {
716             if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
717             {
718                 return [NSImage imageNamed:@"submenu"];
719             } else {
720                 return nil;
721             }
722         }
723     } else {
724         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
725             return NSLocalizedString([availableItems objectAtIndex:rowIndex], @"ERROR");
726         } else {
727             if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
728                 return [NSImage imageNamed:@"submenu"];
729             } else {
730                 return nil;
731             }
732         }
733     }
734 }
735
736 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
737 {
738     if (tableView == menuTableView) {
739         [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
740         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
741         return YES;
742     }
743     
744     if (tableView == allTableView) {
745         [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
746         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
747         return YES;
748     }
749     return NO;
750 }
751
752 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
753 {
754     NSPasteboard *pb;
755     int dragRow;
756     NSString *dragData, *temp;
757     
758     pb = [info draggingPasteboard];
759     
760     if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
761         dragData = [pb stringForType:@"MenuTableViewPboardType"];
762         dragRow = [dragData intValue];
763         temp = [myItems objectAtIndex:dragRow];
764         
765         if (tableView == menuTableView) {
766             [myItems insertObject:temp atIndex:row];
767             if (row > dragRow) {
768                 [myItems removeObjectAtIndex:dragRow];
769             } else {
770                 [myItems removeObjectAtIndex:dragRow + 1];
771             }
772         } else {
773             if (![temp isEqualToString:@"separator"]) {
774                 [availableItems addObject:temp];
775             }
776             [myItems removeObjectAtIndex:dragRow];
777         }
778     } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
779         dragData = [pb stringForType:@"AllTableViewPboardType"];
780         dragRow = [dragData intValue];
781         temp = [availableItems objectAtIndex:dragRow];
782         
783         [myItems insertObject:temp atIndex:row];
784         
785         if (![temp isEqualToString:@"separator"]) {
786             [availableItems removeObjectAtIndex:dragRow];
787         }
788     }
789     
790     [menuTableView reloadData];
791     [allTableView reloadData];
792     [self changeMenus:self];
793     return YES;
794 }
795
796 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
797 {
798     if (tableView == allTableView) {
799         if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
800             return NSDragOperationNone;
801         }
802         
803         if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
804             NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
805             if ([item isEqualToString:@"Preferences"] || [item isEqualToString:@"Quit"]) {
806                 return NSDragOperationNone;
807             }
808         }
809         
810         [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
811         return NSDragOperationGeneric;
812     }
813     
814     if (operation == NSTableViewDropOn || row == -1)
815     {
816         return NSDragOperationNone;
817     }
818     
819     return NSDragOperationGeneric;
820 }
821
822
823 /*************************************************************************/
824 #pragma mark -
825 #pragma mark DEALLOCATION METHODS
826 /*************************************************************************/
827
828 - (void)dealloc
829 {
830     [self setKeyCombo:nil];
831     [hotKeysDictionary release];
832     [keyComboPanel release];
833     [menuTableView setDataSource:nil];
834     [allTableView setDataSource:nil];
835     [controller release];
836     [availableItems release];
837     [submenuItems release];
838     [myItems release];
839     [df release];
840 }
841
842
843 @end