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