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