Changes for prefs stuff and random tweaks too
[MenuTunes.git] / PreferencesController.m
1 #import "PreferencesController.h"
2 #import "MainController.h"
3 #import "HotKeyCenter.h"
4
5 /*************************************************************************/
6 #pragma mark -
7 #pragma mark PRIVATE INTERFACE
8 /*************************************************************************/
9
10 @interface PreferencesController (Private)
11 - (void)setupWindow;
12 - (void)setupCustomizationTables;
13 - (void)setupMenuItems;
14 - (void)setupUI;
15 @end
16
17
18 @implementation PreferencesController
19
20
21 /*************************************************************************/
22 #pragma mark -
23 #pragma mark STATIC VARIABLES
24 /*************************************************************************/
25
26 static PreferencesController *prefs = nil;
27
28
29 /*************************************************************************/
30 #pragma mark -
31 #pragma mark INITIALIZATION METHODS
32 /*************************************************************************/
33
34 + (PreferencesController *)sharedPrefs;
35 {
36     if (! prefs) {
37         prefs = [[self alloc] init];
38     }
39     return prefs;
40 }
41
42 - (id)init
43 {
44     if ( (self = [super init]) ) {
45         df = [[NSUserDefaults standardUserDefaults] retain];
46         controller = nil;
47     }
48     return self;
49 }
50
51
52 /*************************************************************************/
53 #pragma mark -
54 #pragma mark ACCESSOR METHODS
55 /*************************************************************************/
56
57 - (id)controller
58 {
59     return controller;
60 }
61
62 - (void)setController:(id)object
63 {
64 NSLog(@"foo");
65     [controller autorelease];
66     controller = [object retain];
67 NSLog(@"bar");
68 }
69
70
71 /*************************************************************************/
72 #pragma mark -
73 #pragma mark INSTANCE METHODS
74 /*************************************************************************/
75
76
77 - (IBAction)showPrefsWindow:(id)sender
78 {
79     if (! window) {  // If window does not exist yet, then the nib hasn't been loaded.
80         [self setupWindow];  // Load in the nib, and perform any initial setup.
81         [self setupCustomizationTables];  // Setup the DnD manu config tables.
82         [self setupMenuItems];  // Setup the arrays of menu items
83         [self setupUI]; // Sets up additional UI
84         [window setDelegate:self];
85     }
86     
87     [window setLevel:NSStatusWindowLevel];
88     [window center];
89     [window makeKeyAndOrderFront:self];
90     [NSApp activateIgnoringOtherApps:YES];
91 }
92
93 - (IBAction)apply:(id)sender
94 {
95     [df setObject:myItems forKey:@"menu"];
96     
97     //Set key combos
98     [df setKeyCombo:playPauseCombo forKey:@"PlayPause"];
99     [df setKeyCombo:nextTrackCombo forKey:@"NextTrack"];
100     [df setKeyCombo:prevTrackCombo forKey:@"PrevTrack"];
101     [df setKeyCombo:trackInfoCombo forKey:@"TrackInfo"];
102     [df setKeyCombo:upcomingSongsCombo forKey:@"UpcomingSongs"];
103     
104     //Set info checkboxes
105     [df setBool:[albumCheckbox state] forKey:@"showAlbum"];
106     [df setBool:[nameCheckbox state] forKey:@"showName"];
107     [df setBool:[artistCheckbox state] forKey:@"showArtist"];
108     [df setBool:[trackTimeCheckbox state] forKey:@"showTime"];
109     
110     //Here we set whether we will launch at login by modifying loginwindow.plist
111     if ([launchAtLoginCheckbox state] == NSOnState) {
112         NSMutableDictionary *loginwindow;
113         NSMutableArray *loginarray;
114         ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);;
115         int i;
116         BOOL skip = NO;
117         
118         [df synchronize];
119         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
120         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
121         
122         for (i = 0; i < [loginarray count]; i++) {
123             NSDictionary *tempDict = [loginarray objectAtIndex:i];
124             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
125                 skip = YES;
126             }
127         }
128         
129         if (!skip) {
130             AEDesc scriptDesc, resultDesc;
131             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]];
132             
133             AECreateDesc(typeChar, [script cString], [script cStringLength], 
134         &scriptDesc);
135             
136             OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
137             
138             AEDisposeDesc(&scriptDesc);
139             AEDisposeDesc(&resultDesc);
140             CloseComponent(temp);
141         }
142     } else {
143         NSMutableDictionary *loginwindow;
144         NSMutableArray *loginarray;
145         int i;
146         
147         [df synchronize];
148         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
149         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
150         
151         for (i = 0; i < [loginarray count]; i++) {
152             NSDictionary *tempDict = [loginarray objectAtIndex:i];
153             if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
154                 [loginarray removeObjectAtIndex:i];
155                 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
156                 [df synchronize];
157                 break;
158             }
159         }
160     }
161     
162     //Set songs in advance
163     if ([songsInAdvance intValue]) {
164         [df setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
165     } else {
166         [df setInteger:5 forKey:@"SongsInAdvance"];
167     }
168     
169     /*{
170         NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
171         int i;
172         
173         for (i = 0; i < [apps count]; i++) {
174             if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
175                     isEqualToString:@"iTunes"]) {
176                 [controller rebuildMenu];
177             }
178         }
179     }*/
180     [controller clearHotKeys];
181 }
182
183 - (IBAction)cancel:(id)sender
184 {
185     [window close];
186     [controller closePreferences];
187 }
188
189 - (IBAction)cancelHotKey:(id)sender
190 {
191     [[NSNotificationCenter defaultCenter] removeObserver:self];
192     [NSApp endSheet:keyComboPanel];
193     [keyComboPanel orderOut:nil];
194 }
195
196 - (IBAction)clearHotKey:(id)sender
197 {
198     [self setKeyCombo:[KeyCombo clearKeyCombo]];
199 }
200
201 - (IBAction)okHotKey:(id)sender
202 {
203     NSString *string = [combo userDisplayRep];
204     
205     if (string == nil) {
206         string = @"None";
207     }
208     if ([setHotKey isEqualToString:@"PlayPause"]) {
209         if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
210             [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
211             !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
212             
213             [window setLevel:NSNormalWindowLevel];
214             NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
215             [window setLevel:NSStatusWindowLevel];
216             return;
217         }
218         playPauseCombo = [combo copy];
219         [playPauseButton setTitle:string];
220     } else if ([setHotKey isEqualToString:@"NextTrack"]) {
221         if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
222             [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) && 
223             !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
224             
225             [window setLevel:NSNormalWindowLevel];
226             NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
227             [window setLevel:NSStatusWindowLevel];
228             return;
229         }
230         nextTrackCombo = [combo copy];
231         [nextTrackButton setTitle:string];
232     } else if ([setHotKey isEqualToString:@"PrevTrack"]) {
233         if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
234             [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) && 
235             !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
236             
237             [window setLevel:NSNormalWindowLevel];
238             NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
239             [window setLevel:NSStatusWindowLevel];
240             return;
241         }
242         prevTrackCombo = [combo copy];
243         [previousTrackButton setTitle:string];
244     } else if ([setHotKey isEqualToString:@"TrackInfo"]) {
245         if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
246             [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) && 
247             !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
248             
249             [window setLevel:NSNormalWindowLevel];
250             NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
251             [window setLevel:NSStatusWindowLevel];
252             return;
253         }
254         trackInfoCombo = [combo copy];
255         [trackInfoButton setTitle:string];
256     } else if ([setHotKey isEqualToString:@"UpcomingSongs"]) {
257         if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
258             [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) && 
259             !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
260             
261             [window setLevel:NSNormalWindowLevel];
262             NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
263             [window setLevel:NSStatusWindowLevel];
264             return;
265         }
266         upcomingSongsCombo = [combo copy];
267         [upcomingSongsButton setTitle:string];
268     }
269     [self cancelHotKey:sender];
270 }
271
272 - (IBAction)save:(id)sender
273 {
274     [self apply:nil];
275     [window close];
276     [controller closePreferences];
277 }
278
279 - (IBAction)setCurrentTrackInfo:(id)sender
280 {
281     [self setKeyCombo:trackInfoCombo];
282     [self setHotKey:@"TrackInfo"];
283 }
284
285 - (IBAction)setNextTrack:(id)sender
286 {
287     [self setKeyCombo:nextTrackCombo];
288     [self setHotKey:@"NextTrack"];
289 }
290
291 - (IBAction)setPlayPause:(id)sender
292 {
293     [self setKeyCombo:playPauseCombo];
294     [self setHotKey:@"PlayPause"];
295 }
296
297 - (IBAction)setPreviousTrack:(id)sender
298 {
299     [self setKeyCombo:prevTrackCombo];
300     [self setHotKey:@"PrevTrack"];
301 }
302
303 - (IBAction)setUpcomingSongs:(id)sender
304 {
305     [self setKeyCombo:upcomingSongsCombo];
306     [self setHotKey:@"UpcomingSongs"];
307 }
308
309 - (void)setHotKey:(NSString *)key
310 {
311     setHotKey = key;
312     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
313     [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
314 }
315
316 - (void)keyEvent:(NSNotification *)note
317 {
318     NSDictionary *info = [note userInfo];
319     short keyCode;
320     long modifiers;
321     KeyCombo *newCombo;
322     
323     keyCode = [[info objectForKey:@"KeyCode"] shortValue];
324     modifiers = [[info objectForKey:@"Modifiers"] longValue];
325     
326     newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
327     [self setKeyCombo:newCombo];
328 }
329
330 - (void)setKeyCombo:(KeyCombo *)newCombo
331 {
332     NSString *string;
333     [combo release];
334     combo = [newCombo copy];
335     
336     string = [combo userDisplayRep];
337     if (string == nil) {
338         string = @"";
339     }
340     [keyComboField setStringValue:string];
341 }
342
343
344 /*************************************************************************/
345 #pragma mark -
346 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
347 /*************************************************************************/
348
349 - (void)setupWindow
350 {
351     if ( ! [NSBundle loadNibNamed:@"Preferences" owner:self] ) {
352         NSLog( @"Failed to load Preferences.nib" );
353         NSBeep();
354         return;
355     }
356 }
357
358 - (void)setupCustomizationTables
359 {
360     NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
361     
362     // Set the table view cells up
363     [imgCell setImageScaling:NSScaleNone];
364     [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
365     [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
366
367     // Register for drag and drop
368     [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
369         @"MenuTableViewPboardType",
370         @"AllTableViewPboardType",
371         nil]];
372     [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
373         @"MenuTableViewPboardType",
374         @"AllTableViewPboardType",
375         nil]];
376 }
377
378 - (void)setupMenuItems
379 {
380     NSEnumerator *itemEnum;
381     id            anItem;
382     // Set the list of items you can have.
383     availableItems = [[NSMutableArray alloc] initWithObjects:
384         @"Current Track Info",
385         @"Upcoming Songs",
386         @"Playlists",
387         @"EQ Presets",
388         @"Song Rating",
389         @"Play/Pause",
390         @"Next Track",
391         @"Previous Track",
392         @"Fast Forward",
393         @"Rewind",
394         @"<separator>",
395         nil];
396     
397     // Get our preferred menu
398     myItems = [[df arrayForKey:@"menu"] mutableCopy];
399     
400     // Delete items in the availableItems array that are already part of the menu
401     itemEnum = [myItems objectEnumerator];
402     while ( (anItem = [itemEnum nextObject]) ) {
403         if ( ! [anItem isEqualToString:@"<separator>"] ) {
404             [availableItems removeObject:anItem];
405         }
406     }
407     
408     // Items that show should a submenu image
409     submenuItems = [[NSArray alloc] initWithObjects:
410         @"Upcoming Songs",
411         @"Playlists",
412         @"EQ Presets",
413         @"Song Rating",
414         nil];
415 }
416
417 - (void)setupUI
418 {
419     NSMutableDictionary *loginwindow;
420     NSMutableArray *loginarray;
421     NSEnumerator *loginEnum;
422     id anItem;
423
424     // Fill in the number of songs in advance to show field
425     [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
426     
427     // Fill in hot key buttons
428     if ([df objectForKey:@"PlayPause"]){
429         playPauseCombo = [df keyComboForKey:@"PlayPause"];
430         [playPauseButton setTitle:[playPauseCombo userDisplayRep]];
431     } else {
432         playPauseCombo = [[KeyCombo alloc] init];
433     }
434
435     if ([df objectForKey:@"NextTrack"]) {
436         nextTrackCombo = [df keyComboForKey:@"NextTrack"];
437         [nextTrackButton setTitle:[nextTrackCombo userDisplayRep]];
438     } else {
439         nextTrackCombo = [[KeyCombo alloc] init];
440     }
441
442     if ([df objectForKey:@"PrevTrack"]) {
443         prevTrackCombo = [df keyComboForKey:@"PrevTrack"];
444         [previousTrackButton setTitle:[prevTrackCombo userDisplayRep]];
445     } else {
446         prevTrackCombo = [[KeyCombo alloc] init];
447     }
448
449     if ([df objectForKey:@"TrackInfo"]) {
450         trackInfoCombo = [df keyComboForKey:@"TrackInfo"];
451         [trackInfoButton setTitle:[trackInfoCombo userDisplayRep]];
452     } else {
453         trackInfoCombo = [[KeyCombo alloc] init];
454     }
455
456     if ([df objectForKey:@"UpcomingSongs"]) {
457         upcomingSongsCombo = [df keyComboForKey:@"UpcomingSongs"];
458         [upcomingSongsButton setTitle:[upcomingSongsCombo userDisplayRep]];
459     } else {
460         upcomingSongsCombo = [[KeyCombo alloc] init];
461     }
462     
463     // Check current track info buttons
464     [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
465     [nameCheckbox setState:[df boolForKey:@"showName"] ? NSOnState : NSOffState];
466     [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
467     [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
468     
469     // Set the launch at login checkbox state
470     [df synchronize];
471     loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
472     loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
473
474     loginEnum = [loginarray objectEnumerator];
475     while ( (anItem = [loginEnum nextObject]) ) {
476         if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
477             [launchAtLoginCheckbox setState:NSOnState];
478         }
479     }
480 }
481
482
483 /*************************************************************************/
484 #pragma mark -
485 #pragma mark NSWindow DELEGATE METHODS
486 /*************************************************************************/
487
488 - (void)windowWillClose:(NSNotification *)note
489 {
490     [(MainController *)controller closePreferences]; 
491 }
492
493
494 /*************************************************************************/
495 #pragma mark -
496 #pragma mark NSTableView DATASOURCE METHODS
497 /*************************************************************************/
498
499 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
500 {
501     if (aTableView == menuTableView) {
502         return [myItems count];
503     } else {
504         return [availableItems count];
505     }
506 }
507
508 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
509 {
510     if (aTableView == menuTableView) {
511         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
512             return [myItems objectAtIndex:rowIndex];
513         } else {
514             if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
515             {
516                 return [NSImage imageNamed:@"submenu"];
517             } else {
518                 return nil;
519             }
520         }
521     } else {
522         if ([[aTableColumn identifier] isEqualToString:@"name"]) {
523             return [availableItems objectAtIndex:rowIndex];
524         } else {
525             if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
526                 return [NSImage imageNamed:@"submenu"];
527             } else {
528                 return nil;
529             }
530         }
531     }
532 }
533
534 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
535 {
536     if (tableView == menuTableView) {
537         [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
538         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
539         return YES;
540     }
541     
542     if (tableView == allTableView) {
543         [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
544         [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
545         return YES;
546     }
547     return NO;
548 }
549
550 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
551 {
552     NSPasteboard *pb;
553     int dragRow;
554     NSString *dragData, *temp;
555     
556     pb = [info draggingPasteboard];
557     
558     if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
559         dragData = [pb stringForType:@"MenuTableViewPboardType"];
560         dragRow = [dragData intValue];
561         temp = [myItems objectAtIndex:dragRow];
562         [myItems removeObjectAtIndex:dragRow];
563         
564         if (tableView == menuTableView) {
565             if (row > dragRow) {
566                 [myItems insertObject:temp atIndex:row - 1];
567             } else {
568                 [myItems insertObject:temp atIndex:row];
569             }
570         } else {
571             if (![temp isEqualToString:@"<separator>"]) {
572                 [availableItems addObject:temp];
573             }
574         }
575     } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
576         dragData = [pb stringForType:@"AllTableViewPboardType"];
577         dragRow = [dragData intValue];
578         temp = [availableItems objectAtIndex:dragRow];
579         
580         if (![temp isEqualToString:@"<separator>"]) {
581             [availableItems removeObjectAtIndex:dragRow];
582         }
583         [myItems insertObject:temp atIndex:row];
584     }
585     
586     [menuTableView reloadData];
587     [allTableView reloadData];
588     return YES;
589 }
590
591 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
592 {
593     if (tableView == allTableView) {
594         if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
595             return NSDragOperationNone;
596         }
597         
598         if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
599             NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
600             if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
601                 return NSDragOperationNone;
602             }
603         }
604         
605         [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
606         return NSDragOperationGeneric;
607     }
608     
609     if (operation == NSTableViewDropOn || row == -1)
610     {
611         return NSDragOperationNone;
612     }
613     
614     return NSDragOperationGeneric;
615 }
616
617
618 /*************************************************************************/
619 #pragma mark -
620 #pragma mark DEALLOCATION METHODS
621 /*************************************************************************/
622
623 - (void)dealloc
624 {
625     [self setKeyCombo:nil];
626     [playPauseCombo release];
627     [nextTrackCombo release];
628     [prevTrackCombo release];
629     [trackInfoCombo release];
630     [upcomingSongsCombo release];
631     [keyComboPanel release];
632     [menuTableView setDataSource:nil];
633     [allTableView setDataSource:nil];
634     [controller release];
635     [availableItems release];
636     [submenuItems release];
637     [myItems release];
638     [df release];
639 }
640
641
642 @end