Fixed menu reordering crashing in preferences thingy yay.
[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     if ( [sender tag] == 1010) {
99         [self setLaunchesAtLogin:SENDER_STATE];
100     } else if ( [sender tag] == 1020) {
101         [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
102     } else if ( [sender tag] == 1030) {
103         [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
104
105     } else if ( [sender tag] == 1040) {
106         // This will not be executed.  Song info always shows the title of the song.
107         // [df setBool:SENDER_STATE forKey:@"showName"];
108     } else if ( [sender tag] == 1050) {
109         [df setBool:SENDER_STATE forKey:@"showArtist"];
110     } else if ( [sender tag] == 1060) {
111         [df setBool:SENDER_STATE forKey:@"showAlbum"];
112     } else if ( [sender tag] == 1070) {
113         [df setBool:SENDER_STATE forKey:@"showTime"];
114     } else if ( [sender tag] == 1080) {
115         [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
116     } else if ( [sender tag] == 1090) {
117         [df setBool:SENDER_STATE forKey:@"showTrackRating"];
118     }
119     [df synchronize];
120 }
121
122 - (IBAction)changeStatusWindowSetting:(id)sender
123 {
124     if ( [sender tag] == 2010) {
125         [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
126         [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
127         // update the window's position here
128     } else if ( [sender tag] == 2020) {
129         // update screen selection
130     } else if ( [sender tag] == 2030) {
131         // Update appearance effect
132     } else if ( [sender tag] == 2040) {
133         // Update Vanish Effect
134     } else if ( [sender tag] == 2050) {
135         // Update appearance speed
136     } else if ( [sender tag] == 2060) {
137         // Update vanish speed
138     } else if ( [sender tag] == 2070) {
139         // Update vanish delay
140     } else if ( [sender tag] == 2080) {
141         // Update "Song Info window when song changes" setting.
142         [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
143     }
144 }
145
146 - (IBAction)changeHotKey:(id)sender
147 {
148     switch ([sender tag])
149     {
150         case 4010:
151             [self setKeyCombo:[hotKeysDictionary objectForKey:@"PlayPause"]];
152             [self setCurrentHotKey:@"PlayPause"];
153             break;
154         case 4020:
155             [self setKeyCombo:[hotKeysDictionary objectForKey:@"NextTrack"]];
156             [self setCurrentHotKey:@"NextTrack"];
157             break;
158         case 4030:
159             [self setKeyCombo:[hotKeysDictionary objectForKey:@"PrevTrack"]];
160             [self setCurrentHotKey:@"PrevTrack"];
161             break;
162         case 4035:
163             [self setKeyCombo:[hotKeysDictionary objectForKey:@"ShowPlayer"]];
164             [self setCurrentHotKey:@"ShowPlayer"];
165             break;
166         case 4040:
167             [self setKeyCombo:[hotKeysDictionary objectForKey:@"ToggleLoop"]];
168             [self setCurrentHotKey:@"ToggleLoop"];
169             break;
170         case 4050:
171             [self setKeyCombo:[hotKeysDictionary objectForKey:@"ToggleShuffle"]];
172             [self setCurrentHotKey:@"ToggleShuffle"];
173             break;
174         case 4060:
175             [self setKeyCombo:[hotKeysDictionary objectForKey:@"TrackInfo"]];
176             [self setCurrentHotKey:@"TrackInfo"];
177             break;
178         case 4070:
179             [self setKeyCombo:[hotKeysDictionary objectForKey:@"UpcomingSongs"]];
180             [self setCurrentHotKey:@"UpcomingSongs"];
181             break;
182         case 4080:
183             [self setKeyCombo:[hotKeysDictionary objectForKey:@"IncrementVolume"]];
184             [self setCurrentHotKey:@"IncrementVolume"];
185             break;
186         case 4090:
187             [self setKeyCombo:[hotKeysDictionary objectForKey:@"DecrementVolume"]];
188             [self setCurrentHotKey:@"DecrementVolume"];
189             break;
190         case 4100:
191             [self setKeyCombo:[hotKeysDictionary objectForKey:@"IncrementRating"]];
192             [self setCurrentHotKey:@"IncrementRating"];
193             break;
194         case 4110:
195             [self setKeyCombo:[hotKeysDictionary objectForKey:@"DecrementRating"]];
196             [self setCurrentHotKey:@"DecrementRating"];
197             break;
198     }
199 }
200
201 - (void)registerDefaults
202 {
203     BOOL found = NO;
204     NSMutableDictionary *loginWindow;
205     NSMutableArray *loginArray;
206     NSEnumerator *loginEnum;
207     id anItem;
208
209     [df setObject:[NSArray arrayWithObjects:
210         @"playPause",
211         @"prevTrack",
212         @"nextTrack",
213         @"fastForward",
214         @"rewind",
215         @"showPlayer",
216         @"separator",
217         @"songRating",
218         @"eqPresets",
219         @"playlists",
220         @"upcomingSongs",
221         @"separator",
222         @"preferences",
223         @"quit",
224         @"separator",
225         @"trackInfo",
226         nil] forKey:@"menu"];
227
228     [df setInteger:5 forKey:@"SongsInAdvance"];
229     // [df setBool:YES forKey:@"showName"];  // Song info will always show song title.
230     [df setBool:YES forKey:@"showArtist"];
231     [df setBool:NO forKey:@"showAlbum"];
232     [df setBool:NO forKey:@"showTime"];
233
234     [df synchronize];
235     
236     loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
237     loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
238     loginEnum = [loginArray objectEnumerator];
239
240     while ( (anItem = [loginEnum nextObject]) ) {
241         if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
242             found = YES;
243         }
244     }
245
246     [loginWindow release];
247     
248     // This is teh sux
249     // We must fix it so it is no longer suxy
250     if (!found) {
251         if (NSRunInformationalAlertPanel(NSLocalizedString(@"autolaunch", @"Auto-launch MenuTunes"), NSLocalizedString(@"autolaunch_msg", @"Would you like MenuTunes to automatically launch at login?"), @"Yes", @"No", nil) == NSOKButton) {
252             AEDesc scriptDesc, resultDesc;
253             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]];
254             ComponentInstance asComponent = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);
255
256             AECreateDesc(typeChar, [script cString], [script cStringLength],
257                          &scriptDesc);
258
259             OSADoScript(asComponent, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
260
261             AEDisposeDesc(&scriptDesc);
262             AEDisposeDesc(&resultDesc);
263
264             CloseComponent(asComponent);
265         }
266     }
267 }
268
269 - (IBAction)cancelHotKey:(id)sender
270 {
271     [[NSNotificationCenter defaultCenter] removeObserver:self];
272     [NSApp endSheet:keyComboPanel];
273     [keyComboPanel orderOut:nil];
274 }
275
276 - (IBAction)clearHotKey:(id)sender
277 {
278     [self setKeyCombo:[KeyCombo clearKeyCombo]];
279 }
280
281 - (IBAction)okHotKey:(id)sender
282 {
283     NSString *string = [combo userDisplayRep];
284     NSEnumerator *enumerator = [hotKeysDictionary keyEnumerator];
285     NSString *enumKey;
286     
287     if (string == nil) {
288         string = @"";
289     }
290     
291     while ( (enumKey = [enumerator nextObject]) ) {
292         if (![enumKey isEqualToString:currentHotKey]) {
293             if (![combo isEqual:[KeyCombo clearKeyCombo]] &&
294                  [combo isEqual:[hotKeysDictionary objectForKey:enumKey]]) {
295                 [window setLevel:NSNormalWindowLevel];
296                 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) ) {
297                     [hotKeysDictionary setObject:[KeyCombo clearKeyCombo] forKey:currentHotKey];
298                     if ([enumKey isEqualToString:@"PlayPause"]) {
299                         [playPauseButton setTitle:@""];
300                     } else if ([enumKey isEqualToString:@"NextTrack"]) {
301                         [nextTrackButton setTitle:@""];
302                     } else if ([enumKey isEqualToString:@"PrevTrack"]) {
303                         [previousTrackButton setTitle:@""];
304                     } else if ([enumKey isEqualToString:@"ShowPlayer"]) {
305                         [showPlayerButton setTitle:@""];
306                     } else if ([enumKey isEqualToString:@"TrackInfo"]) {
307                         [trackInfoButton setTitle:@""];
308                     } else if ([enumKey isEqualToString:@"UpcomingSongs"]) {
309                         [upcomingSongsButton setTitle:@""];
310                     } else if ([enumKey isEqualToString:@"IncrementVolume"]) {
311                         [volumeIncrementButton setTitle:@""];
312                     } else if ([enumKey isEqualToString:@"DecrementVolume"]) {
313                         [volumeDecrementButton setTitle:@""];
314                     } else if ([enumKey isEqualToString:@"IncrementRating"]) {
315                         [ratingIncrementButton setTitle:@""];
316                     } else if ([enumKey isEqualToString:@"DecrementRating"]) {
317                         [ratingDecrementButton setTitle:@""];
318                     } else if ([enumKey isEqualToString:@"ToggleShuffle"]) {
319                         [toggleShuffleButton setTitle:@""];
320                     } else if ([enumKey isEqualToString:@"ToggleLoop"]) {
321                         [toggleLoopButton setTitle:@""];
322                     }
323                     [df setKeyCombo:[KeyCombo clearKeyCombo] forKey:enumKey];
324                 } else {
325                     return;
326                 }
327                 [window setLevel:NSStatusWindowLevel];
328             }
329         }
330     }
331     
332     [hotKeysDictionary setObject:combo forKey:currentHotKey];
333     [df setKeyCombo:combo forKey:currentHotKey];
334     
335     if ([currentHotKey isEqualToString:@"PlayPause"]) {
336         [playPauseButton setTitle:string];
337         [[HotKeyCenter sharedCenter] addHotKey:@"PlayPause" combo:combo target:[MainController sharedController] action:@selector(playPause)];
338     } else if ([currentHotKey isEqualToString:@"NextTrack"]) {
339         [nextTrackButton setTitle:string];
340         [[HotKeyCenter sharedCenter] addHotKey:@"NextTrack" combo:combo target:[MainController sharedController] action:@selector(nextSong)];
341     } else if ([currentHotKey isEqualToString:@"PrevTrack"]) {
342         [previousTrackButton setTitle:string];
343         [[HotKeyCenter sharedCenter] addHotKey:@"PrevTrack" combo:combo target:[MainController sharedController] action:@selector(prevSong)];
344     } else if ([currentHotKey isEqualToString:@"ShowPlayer"]) {
345         [showPlayerButton setTitle:string];
346         [[HotKeyCenter sharedCenter] addHotKey:@"ShowPlayer" combo:combo target:[MainController sharedController] action:@selector(showPlayer)];
347     } else if ([currentHotKey isEqualToString:@"TrackInfo"]) {
348         [trackInfoButton setTitle:string];
349         [[HotKeyCenter sharedCenter] addHotKey:@"TrackInfo" combo:combo target:[MainController sharedController] action:@selector(showCurrentTrackInfo)];
350     } else if ([currentHotKey isEqualToString:@"UpcomingSongs"]) {
351         [upcomingSongsButton setTitle:string];
352         [[HotKeyCenter sharedCenter] addHotKey:@"UpcomingSongs" combo:combo target:[MainController sharedController] action:@selector(showUpcomingSongs)];
353     } else if ([currentHotKey isEqualToString:@"IncrementVolume"]) {
354         [volumeIncrementButton setTitle:string];
355         [[HotKeyCenter sharedCenter] addHotKey:@"IncrementVolume" combo:combo target:[MainController sharedController] action:@selector(incrementVolume)];
356     } else if ([currentHotKey isEqualToString:@"DecrementVolume"]) {
357         [volumeDecrementButton setTitle:string];
358         [[HotKeyCenter sharedCenter] addHotKey:@"DecrementVolume" combo:combo target:[MainController sharedController] action:@selector(decrementVolume)];
359     } else if ([currentHotKey isEqualToString:@"IncrementRating"]) {
360         [ratingIncrementButton setTitle:string];
361         [[HotKeyCenter sharedCenter] addHotKey:@"IncrementRating" combo:combo target:[MainController sharedController] action:@selector(incrementRating)];
362     } else if ([currentHotKey isEqualToString:@"DecrementRating"]) {
363         [ratingDecrementButton setTitle:string];
364         [[HotKeyCenter sharedCenter] addHotKey:@"DecrementRating" combo:combo target:[MainController sharedController] action:@selector(decrementRating)];
365     } else if ([currentHotKey isEqualToString:@"ToggleShuffle"]) {
366         [toggleShuffleButton setTitle:string];
367         [[HotKeyCenter sharedCenter] addHotKey:@"ToggleShuffle" combo:combo target:[MainController sharedController] action:@selector(toggleShuffle)];
368     } else if ([currentHotKey isEqualToString:@"ToggleLoop"]) {
369         [toggleLoopButton setTitle:string];
370         [[HotKeyCenter sharedCenter] addHotKey:@"ToggleLoop" combo:combo target:[MainController sharedController] action:@selector(toggleLoop)];
371     }
372     [self cancelHotKey:sender];
373 }
374
375
376
377 /*************************************************************************/
378 #pragma mark -
379 #pragma mark HOTKEY SUPPORT METHODS
380 /*************************************************************************/
381
382 - (void)setCurrentHotKey:(NSString *)key
383 {
384     [currentHotKey autorelease];
385     currentHotKey = [key copy];
386     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
387     [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
388 }
389
390 - (void)keyEvent:(NSNotification *)note
391 {
392     NSDictionary *info = [note userInfo];
393     short keyCode;
394     long modifiers;
395     KeyCombo *newCombo;
396     
397     keyCode = [[info objectForKey:@"KeyCode"] shortValue];
398     modifiers = [[info objectForKey:@"Modifiers"] longValue];
399     
400     newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
401     [self setKeyCombo:newCombo];
402 }
403
404 - (void)setKeyCombo:(KeyCombo *)newCombo
405 {
406     NSString *string;
407     [combo release];
408     combo = [newCombo copy];
409     
410     string = [combo userDisplayRep];
411     if (string == nil) {
412         string = @"";
413     }
414     [keyComboField setStringValue:string];
415 }
416
417
418 /*************************************************************************/
419 #pragma mark -
420 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
421 /*************************************************************************/
422
423 - (void)setupWindow
424 {
425     if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
426         NSLog(@"MenuTunes: Failed to load Preferences.nib");
427         NSBeep();
428         return;
429     }
430 }
431
432 - (void)setupCustomizationTables
433 {
434     NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
435     
436     // Set the table view cells up
437     [imgCell setImageScaling:NSScaleNone];
438     [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
439     [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
440
441     // Register for drag and drop
442     [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
443         @"MenuTableViewPboardType",
444         @"AllTableViewPboardType",
445         nil]];
446     [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
447         @"MenuTableViewPboardType",
448         @"AllTableViewPboardType",
449         nil]];
450 }
451
452 - (void)setupMenuItems
453 {
454     NSEnumerator *itemEnum;
455     id            anItem;
456     // Set the list of items you can have.
457     availableItems = [[NSMutableArray alloc] initWithObjects:
458         @"trackInfo",
459         @"upcomingSongs",
460         @"playlists",
461         @"eqPresets",
462         @"songRating",
463         @"playPause",
464         @"nextTrack",
465         @"prevTrack",
466         @"fastForward",
467         @"rewind",
468         @"showPlayer",
469         @"separator",
470         nil];
471     
472     // Get our preferred menu
473     myItems = [[df arrayForKey:@"menu"] mutableCopy];
474     
475     // Delete items in the availableItems array that are already part of the menu
476     itemEnum = [myItems objectEnumerator];
477     while ( (anItem = [itemEnum nextObject]) ) {
478         if (![anItem isEqualToString:@"separator"]) {
479             [availableItems removeObject:anItem];
480         }
481     }
482     
483     // Items that show should a submenu image
484     submenuItems = [[NSArray alloc] initWithObjects:
485         @"upcomingSongs",
486         @"playlists",
487         @"eqPresets",
488         @"songRating",
489         nil];
490 }
491
492 - (void)setupUI
493 {
494     NSMutableDictionary *loginwindow;
495     NSMutableArray *loginarray;
496     NSEnumerator *loginEnum;
497     id anItem;
498     
499     // Fill in the number of songs in advance to show field
500     [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
501     
502     // Fill in hot key buttons
503     if ([df objectForKey:@"PlayPause"]) {
504         anItem = [df keyComboForKey:@"PlayPause"];
505         [hotKeysDictionary setObject:anItem forKey:@"PlayPause"];
506         [playPauseButton setTitle:[anItem userDisplayRep]];
507     } else {
508         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"PlayPause"];
509     }
510     
511     if ([df objectForKey:@"NextTrack"]) {
512         anItem = [df keyComboForKey:@"NextTrack"];
513         [hotKeysDictionary setObject:anItem forKey:@"NextTrack"];
514         [nextTrackButton setTitle:[anItem userDisplayRep]];
515     } else {
516         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"NextTrack"];
517     }
518     
519     if ([df objectForKey:@"PrevTrack"]) {
520         anItem = [df keyComboForKey:@"PrevTrack"];
521         [hotKeysDictionary setObject:anItem forKey:@"PrevTrack"];
522         [previousTrackButton setTitle:[anItem userDisplayRep]];
523     } else {
524         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"PrevTrack"];
525     }
526     
527     if ([df objectForKey:@"ShowPlayer"]) {
528         anItem = [df keyComboForKey:@"ShowPlayer"];
529         [hotKeysDictionary setObject:anItem forKey:@"ShowPlayer"];
530         [showPlayerButton setTitle:[anItem userDisplayRep]];
531     } else {
532         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"ShowPlayer"];
533     }
534     
535     if ([df objectForKey:@"TrackInfo"]) {
536         anItem = [df keyComboForKey:@"TrackInfo"];
537         [hotKeysDictionary setObject:anItem forKey:@"TrackInfo"];
538         [trackInfoButton setTitle:[anItem userDisplayRep]];
539     } else {
540         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"TrackInfo"];
541     }
542     
543     if ([df objectForKey:@"UpcomingSongs"]) {
544         anItem = [df keyComboForKey:@"UpcomingSongs"];
545         [hotKeysDictionary setObject:anItem forKey:@"UpcomingSongs"];
546         [upcomingSongsButton setTitle:[anItem userDisplayRep]];
547     } else {
548         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"UpcomingSongs"];
549     }
550     
551     if ([df objectForKey:@"IncrementVolume"]) {
552         anItem = [df keyComboForKey:@"IncrementVolume"];
553         [hotKeysDictionary setObject:anItem forKey:@"IncrementVolume"];
554         [volumeIncrementButton setTitle:[anItem userDisplayRep]];
555     } else {
556         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"IncrementVolume"];
557     }
558     
559     if ([df objectForKey:@"DecrementVolume"]) {
560         anItem = [df keyComboForKey:@"DecrementVolume"];
561         [hotKeysDictionary setObject:anItem forKey:@"DecrementVolume"];
562         [volumeDecrementButton setTitle:[anItem userDisplayRep]];
563     } else {
564         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"DecrementVolume"];
565     }
566     
567     if ([df objectForKey:@"IncrementRating"]) {
568         anItem = [df keyComboForKey:@"IncrementRating"];
569         [hotKeysDictionary setObject:anItem forKey:@"IncrementRating"];
570         [ratingIncrementButton setTitle:[anItem userDisplayRep]];
571     } else {
572         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"IncrementRating"];
573     }
574     
575     if ([df objectForKey:@"DecrementRating"]) {
576         anItem = [df keyComboForKey:@"DecrementRating"];
577         [hotKeysDictionary setObject:anItem forKey:@"DecrementRating"];
578         [ratingDecrementButton setTitle:[anItem userDisplayRep]];
579     } else {
580         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"DecrementRating"];
581     }
582     
583     if ([df objectForKey:@"ToggleLoop"]) {
584         anItem = [df keyComboForKey:@"ToggleLoop"];
585         [hotKeysDictionary setObject:anItem forKey:@"ToggleLoop"];
586         [toggleLoopButton setTitle:[anItem userDisplayRep]];
587     } else {
588         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"ToggleLoop"];
589     }
590     
591     if ([df objectForKey:@"ToggleShuffle"]) {
592         anItem = [df keyComboForKey:@"ToggleShuffle"];
593         [hotKeysDictionary setObject:anItem forKey:@"ToggleShuffle"];
594         [toggleShuffleButton setTitle:[anItem userDisplayRep]];
595     } else {
596         [hotKeysDictionary setObject:[KeyCombo keyCombo] forKey:@"ToggleShuffle"];
597     }
598     
599     // Check current track info buttons
600     [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
601     [nameCheckbox setState:NSOnState];  // Song info will ALWAYS show song title.
602     [nameCheckbox setEnabled:NO];  // Song info will ALWAYS show song title.
603     [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
604     [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
605     [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
606     
607     // Set the launch at login checkbox state
608     [df synchronize];
609     loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
610     loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
611     
612     loginEnum = [loginarray objectEnumerator];
613     while ( (anItem = [loginEnum nextObject]) ) {
614         if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
615             [launchAtLoginCheckbox setState:NSOnState];
616         }
617     }
618 }
619
620 - (IBAction)changeMenus:(id)sender
621 {
622     [df setObject:myItems forKey:@"menu"];
623     [df synchronize];
624 }
625
626 - (void)setLaunchesAtLogin:(BOOL)flag
627 {
628     if ( flag ) {
629         NSMutableDictionary *loginwindow;
630         NSMutableArray *loginarray;
631         ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);;
632         int i;
633         BOOL skip = NO;
634
635         [df synchronize];
636         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
637         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
638
639         for (i = 0; i < [loginarray count]; i++) {
640             NSDictionary *tempDict = [loginarray objectAtIndex:i];
641             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
642                 skip = YES;
643             }
644         }
645
646         if (!skip) {
647             AEDesc scriptDesc, resultDesc;
648             NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
649
650             AECreateDesc(typeChar, [script cString], [script cStringLength],
651                          &scriptDesc);
652
653             OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
654
655             AEDisposeDesc(&scriptDesc);
656             AEDisposeDesc(&resultDesc);
657             CloseComponent(temp);
658         }
659
660     } else {
661         NSMutableDictionary *loginwindow;
662         NSMutableArray *loginarray;
663         int i;
664
665         [df synchronize];
666         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
667         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
668
669         for (i = 0; i < [loginarray count]; i++) {
670             NSDictionary *tempDict = [loginarray objectAtIndex:i];
671             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
672                 [loginarray removeObjectAtIndex:i];
673                 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
674                 [df synchronize];
675                 break;
676             }
677         }
678     }
679 }
680
681
682 /*************************************************************************/
683 #pragma mark -
684 #pragma mark NSWindow DELEGATE METHODS
685 /*************************************************************************/
686
687 - (void)windowWillClose:(NSNotification *)note
688 {
689     [(MainController *)controller closePreferences]; 
690 }
691
692
693 /*************************************************************************/
694 #pragma mark -
695 #pragma mark NSTableView DATASOURCE METHODS
696 /*************************************************************************/
697
698 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
699 {
700     if (aTableView == menuTableView) {
701         return [myItems count];
702     } else {
703         return [availableItems count];
704     }
705 }
706
707 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
708 {
709     if (aTableView == menuTableView) {
710         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
711             NSString *object = [myItems objectAtIndex:rowIndex];
712             if ([object isEqualToString:@"Show Player"]) {
713                 return [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
714             }
715             return NSLocalizedString(object, @"ERROR");
716         } else {
717             if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
718             {
719                 return [NSImage imageNamed:@"submenu"];
720             } else {
721                 return nil;
722             }
723         }
724     } else {
725         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
726             return NSLocalizedString([availableItems objectAtIndex:rowIndex], @"ERROR");
727         } else {
728             if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
729                 return [NSImage imageNamed:@"submenu"];
730             } else {
731                 return nil;
732             }
733         }
734     }
735 }
736
737 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
738 {
739     if (tableView == menuTableView) {
740         [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
741         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
742         return YES;
743     }
744     
745     if (tableView == allTableView) {
746         [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
747         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
748         return YES;
749     }
750     return NO;
751 }
752
753 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
754 {
755     NSPasteboard *pb;
756     int dragRow;
757     NSString *dragData, *temp;
758     
759     pb = [info draggingPasteboard];
760     
761     if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
762         dragData = [pb stringForType:@"MenuTableViewPboardType"];
763         dragRow = [dragData intValue];
764         temp = [myItems objectAtIndex:dragRow];
765         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                 [myItems removeObjectAtIndex:dragRow];
776             }
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