1 #import "PreferencesController.h"
2 #import "MainController.h"
3 #import "HotKeyCenter.h"
5 /*************************************************************************/
7 #pragma mark PRIVATE INTERFACE
8 /*************************************************************************/
10 @interface PreferencesController (Private)
12 - (void)setupCustomizationTables;
13 - (void)setupMenuItems;
18 @implementation PreferencesController
21 /*************************************************************************/
23 #pragma mark STATIC VARIABLES
24 /*************************************************************************/
26 static PreferencesController *prefs = nil;
29 /*************************************************************************/
31 #pragma mark INITIALIZATION METHODS
32 /*************************************************************************/
34 + (PreferencesController *)sharedPrefs;
37 prefs = [[self alloc] init];
44 if ( (self = [super init]) ) {
45 df = [[NSUserDefaults standardUserDefaults] retain];
52 /*************************************************************************/
54 #pragma mark ACCESSOR METHODS
55 /*************************************************************************/
62 - (void)setController:(id)object
65 [controller autorelease];
66 controller = [object retain];
71 /*************************************************************************/
73 #pragma mark INSTANCE METHODS
74 /*************************************************************************/
77 - (IBAction)showPrefsWindow:(id)sender
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];
87 [window setLevel:NSStatusWindowLevel];
89 [window makeKeyAndOrderFront:self];
90 [NSApp activateIgnoringOtherApps:YES];
93 - (IBAction)apply:(id)sender
95 [df setObject:myItems forKey:@"menu"];
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"];
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"];
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);;
119 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
120 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
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]]) {
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]];
133 AECreateDesc(typeChar, [script cString], [script cStringLength],
136 OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
138 AEDisposeDesc(&scriptDesc);
139 AEDisposeDesc(&resultDesc);
140 CloseComponent(temp);
143 NSMutableDictionary *loginwindow;
144 NSMutableArray *loginarray;
148 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
149 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
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"];
162 //Set songs in advance
163 if ([songsInAdvance intValue]) {
164 [df setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
166 [df setInteger:5 forKey:@"SongsInAdvance"];
170 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
173 for (i = 0; i < [apps count]; i++) {
174 if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
175 isEqualToString:@"iTunes"]) {
176 [controller rebuildMenu];
180 [controller clearHotKeys];
183 - (IBAction)cancelHotKey:(id)sender
185 [[NSNotificationCenter defaultCenter] removeObserver:self];
186 [NSApp endSheet:keyComboPanel];
187 [keyComboPanel orderOut:nil];
190 - (IBAction)clearHotKey:(id)sender
192 [self setKeyCombo:[KeyCombo clearKeyCombo]];
195 - (IBAction)okHotKey:(id)sender
197 NSString *string = [combo userDisplayRep];
202 if ([setHotKey isEqualToString:@"PlayPause"]) {
203 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
204 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
205 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
207 [window setLevel:NSNormalWindowLevel];
208 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
209 [window setLevel:NSStatusWindowLevel];
212 playPauseCombo = [combo copy];
213 [playPauseButton setTitle:string];
214 } else if ([setHotKey isEqualToString:@"NextTrack"]) {
215 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
216 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
217 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
219 [window setLevel:NSNormalWindowLevel];
220 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
221 [window setLevel:NSStatusWindowLevel];
224 nextTrackCombo = [combo copy];
225 [nextTrackButton setTitle:string];
226 } else if ([setHotKey isEqualToString:@"PrevTrack"]) {
227 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
228 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
229 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
231 [window setLevel:NSNormalWindowLevel];
232 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
233 [window setLevel:NSStatusWindowLevel];
236 prevTrackCombo = [combo copy];
237 [previousTrackButton setTitle:string];
238 } else if ([setHotKey isEqualToString:@"TrackInfo"]) {
239 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
240 [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) &&
241 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
243 [window setLevel:NSNormalWindowLevel];
244 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
245 [window setLevel:NSStatusWindowLevel];
248 trackInfoCombo = [combo copy];
249 [trackInfoButton setTitle:string];
250 } else if ([setHotKey isEqualToString:@"UpcomingSongs"]) {
251 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
252 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
253 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
255 [window setLevel:NSNormalWindowLevel];
256 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
257 [window setLevel:NSStatusWindowLevel];
260 upcomingSongsCombo = [combo copy];
261 [upcomingSongsButton setTitle:string];
263 [self cancelHotKey:sender];
266 - (IBAction)setCurrentTrackInfo:(id)sender
268 [self setKeyCombo:trackInfoCombo];
269 [self setHotKey:@"TrackInfo"];
272 - (IBAction)setNextTrack:(id)sender
274 [self setKeyCombo:nextTrackCombo];
275 [self setHotKey:@"NextTrack"];
278 - (IBAction)setPlayPause:(id)sender
280 [self setKeyCombo:playPauseCombo];
281 [self setHotKey:@"PlayPause"];
284 - (IBAction)setPreviousTrack:(id)sender
286 [self setKeyCombo:prevTrackCombo];
287 [self setHotKey:@"PrevTrack"];
290 - (IBAction)setUpcomingSongs:(id)sender
292 [self setKeyCombo:upcomingSongsCombo];
293 [self setHotKey:@"UpcomingSongs"];
297 /*************************************************************************/
299 #pragma mark HOTKEY SUPPORT METHODS
300 /*************************************************************************/
302 - (void)setHotKey:(NSString *)key
305 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
306 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
309 - (void)keyEvent:(NSNotification *)note
311 NSDictionary *info = [note userInfo];
316 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
317 modifiers = [[info objectForKey:@"Modifiers"] longValue];
319 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
320 [self setKeyCombo:newCombo];
323 - (void)setKeyCombo:(KeyCombo *)newCombo
327 combo = [newCombo copy];
329 string = [combo userDisplayRep];
333 [keyComboField setStringValue:string];
337 /*************************************************************************/
339 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
340 /*************************************************************************/
344 if ( ! [NSBundle loadNibNamed:@"Preferences" owner:self] ) {
345 NSLog( @"Failed to load Preferences.nib" );
351 - (void)setupCustomizationTables
353 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
355 // Set the table view cells up
356 [imgCell setImageScaling:NSScaleNone];
357 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
358 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
360 // Register for drag and drop
361 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
362 @"MenuTableViewPboardType",
363 @"AllTableViewPboardType",
365 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
366 @"MenuTableViewPboardType",
367 @"AllTableViewPboardType",
371 - (void)setupMenuItems
373 NSEnumerator *itemEnum;
375 // Set the list of items you can have.
376 availableItems = [[NSMutableArray alloc] initWithObjects:
377 @"Current Track Info",
390 // Get our preferred menu
391 myItems = [[df arrayForKey:@"menu"] mutableCopy];
393 // Delete items in the availableItems array that are already part of the menu
394 itemEnum = [myItems objectEnumerator];
395 while ( (anItem = [itemEnum nextObject]) ) {
396 if ( ! [anItem isEqualToString:@"<separator>"] ) {
397 [availableItems removeObject:anItem];
401 // Items that show should a submenu image
402 submenuItems = [[NSArray alloc] initWithObjects:
412 NSMutableDictionary *loginwindow;
413 NSMutableArray *loginarray;
414 NSEnumerator *loginEnum;
417 // Fill in the number of songs in advance to show field
418 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
420 // Fill in hot key buttons
421 if ([df objectForKey:@"PlayPause"]){
422 playPauseCombo = [df keyComboForKey:@"PlayPause"];
423 [playPauseButton setTitle:[playPauseCombo userDisplayRep]];
425 playPauseCombo = [[KeyCombo alloc] init];
428 if ([df objectForKey:@"NextTrack"]) {
429 nextTrackCombo = [df keyComboForKey:@"NextTrack"];
430 [nextTrackButton setTitle:[nextTrackCombo userDisplayRep]];
432 nextTrackCombo = [[KeyCombo alloc] init];
435 if ([df objectForKey:@"PrevTrack"]) {
436 prevTrackCombo = [df keyComboForKey:@"PrevTrack"];
437 [previousTrackButton setTitle:[prevTrackCombo userDisplayRep]];
439 prevTrackCombo = [[KeyCombo alloc] init];
442 if ([df objectForKey:@"TrackInfo"]) {
443 trackInfoCombo = [df keyComboForKey:@"TrackInfo"];
444 [trackInfoButton setTitle:[trackInfoCombo userDisplayRep]];
446 trackInfoCombo = [[KeyCombo alloc] init];
449 if ([df objectForKey:@"UpcomingSongs"]) {
450 upcomingSongsCombo = [df keyComboForKey:@"UpcomingSongs"];
451 [upcomingSongsButton setTitle:[upcomingSongsCombo userDisplayRep]];
453 upcomingSongsCombo = [[KeyCombo alloc] init];
456 // Check current track info buttons
457 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
458 [nameCheckbox setState:[df boolForKey:@"showName"] ? NSOnState : NSOffState];
459 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
460 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
462 // Set the launch at login checkbox state
464 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
465 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
467 loginEnum = [loginarray objectEnumerator];
468 while ( (anItem = [loginEnum nextObject]) ) {
469 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
470 [launchAtLoginCheckbox setState:NSOnState];
476 /*************************************************************************/
478 #pragma mark NSWindow DELEGATE METHODS
479 /*************************************************************************/
481 - (void)windowWillClose:(NSNotification *)note
483 [(MainController *)controller closePreferences];
487 /*************************************************************************/
489 #pragma mark NSTableView DATASOURCE METHODS
490 /*************************************************************************/
492 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
494 if (aTableView == menuTableView) {
495 return [myItems count];
497 return [availableItems count];
501 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
503 if (aTableView == menuTableView) {
504 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
505 return [myItems objectAtIndex:rowIndex];
507 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
509 return [NSImage imageNamed:@"submenu"];
515 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
516 return [availableItems objectAtIndex:rowIndex];
518 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
519 return [NSImage imageNamed:@"submenu"];
527 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
529 if (tableView == menuTableView) {
530 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
531 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
535 if (tableView == allTableView) {
536 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
537 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
543 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
547 NSString *dragData, *temp;
549 pb = [info draggingPasteboard];
551 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
552 dragData = [pb stringForType:@"MenuTableViewPboardType"];
553 dragRow = [dragData intValue];
554 temp = [myItems objectAtIndex:dragRow];
555 [myItems removeObjectAtIndex:dragRow];
557 if (tableView == menuTableView) {
559 [myItems insertObject:temp atIndex:row - 1];
561 [myItems insertObject:temp atIndex:row];
564 if (![temp isEqualToString:@"<separator>"]) {
565 [availableItems addObject:temp];
568 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
569 dragData = [pb stringForType:@"AllTableViewPboardType"];
570 dragRow = [dragData intValue];
571 temp = [availableItems objectAtIndex:dragRow];
573 if (![temp isEqualToString:@"<separator>"]) {
574 [availableItems removeObjectAtIndex:dragRow];
576 [myItems insertObject:temp atIndex:row];
579 [menuTableView reloadData];
580 [allTableView reloadData];
584 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
586 if (tableView == allTableView) {
587 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
588 return NSDragOperationNone;
591 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
592 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
593 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
594 return NSDragOperationNone;
598 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
599 return NSDragOperationGeneric;
602 if (operation == NSTableViewDropOn || row == -1)
604 return NSDragOperationNone;
607 return NSDragOperationGeneric;
611 /*************************************************************************/
613 #pragma mark DEALLOCATION METHODS
614 /*************************************************************************/
618 [self setKeyCombo:nil];
619 [playPauseCombo release];
620 [nextTrackCombo release];
621 [prevTrackCombo release];
622 [trackInfoCombo release];
623 [upcomingSongsCombo release];
624 [keyComboPanel release];
625 [menuTableView setDataSource:nil];
626 [allTableView setDataSource:nil];
627 [controller release];
628 [availableItems release];
629 [submenuItems release];