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)cancel:(id)sender
186 [controller closePreferences];
189 - (IBAction)cancelHotKey:(id)sender
191 [[NSNotificationCenter defaultCenter] removeObserver:self];
192 [NSApp endSheet:keyComboPanel];
193 [keyComboPanel orderOut:nil];
196 - (IBAction)clearHotKey:(id)sender
198 [self setKeyCombo:[KeyCombo clearKeyCombo]];
201 - (IBAction)okHotKey:(id)sender
203 NSString *string = [combo userDisplayRep];
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))) {
213 [window setLevel:NSNormalWindowLevel];
214 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
215 [window setLevel:NSStatusWindowLevel];
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))) {
225 [window setLevel:NSNormalWindowLevel];
226 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
227 [window setLevel:NSStatusWindowLevel];
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))) {
237 [window setLevel:NSNormalWindowLevel];
238 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
239 [window setLevel:NSStatusWindowLevel];
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))) {
249 [window setLevel:NSNormalWindowLevel];
250 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
251 [window setLevel:NSStatusWindowLevel];
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))) {
261 [window setLevel:NSNormalWindowLevel];
262 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
263 [window setLevel:NSStatusWindowLevel];
266 upcomingSongsCombo = [combo copy];
267 [upcomingSongsButton setTitle:string];
269 [self cancelHotKey:sender];
272 - (IBAction)save:(id)sender
276 [controller closePreferences];
279 - (IBAction)setCurrentTrackInfo:(id)sender
281 [self setKeyCombo:trackInfoCombo];
282 [self setHotKey:@"TrackInfo"];
285 - (IBAction)setNextTrack:(id)sender
287 [self setKeyCombo:nextTrackCombo];
288 [self setHotKey:@"NextTrack"];
291 - (IBAction)setPlayPause:(id)sender
293 [self setKeyCombo:playPauseCombo];
294 [self setHotKey:@"PlayPause"];
297 - (IBAction)setPreviousTrack:(id)sender
299 [self setKeyCombo:prevTrackCombo];
300 [self setHotKey:@"PrevTrack"];
303 - (IBAction)setUpcomingSongs:(id)sender
305 [self setKeyCombo:upcomingSongsCombo];
306 [self setHotKey:@"UpcomingSongs"];
309 - (void)setHotKey:(NSString *)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];
316 - (void)keyEvent:(NSNotification *)note
318 NSDictionary *info = [note userInfo];
323 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
324 modifiers = [[info objectForKey:@"Modifiers"] longValue];
326 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
327 [self setKeyCombo:newCombo];
330 - (void)setKeyCombo:(KeyCombo *)newCombo
334 combo = [newCombo copy];
336 string = [combo userDisplayRep];
340 [keyComboField setStringValue:string];
344 /*************************************************************************/
346 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
347 /*************************************************************************/
351 if ( ! [NSBundle loadNibNamed:@"Preferences" owner:self] ) {
352 NSLog( @"Failed to load Preferences.nib" );
358 - (void)setupCustomizationTables
360 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
362 // Set the table view cells up
363 [imgCell setImageScaling:NSScaleNone];
364 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
365 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
367 // Register for drag and drop
368 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
369 @"MenuTableViewPboardType",
370 @"AllTableViewPboardType",
372 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
373 @"MenuTableViewPboardType",
374 @"AllTableViewPboardType",
378 - (void)setupMenuItems
380 NSEnumerator *itemEnum;
382 // Set the list of items you can have.
383 availableItems = [[NSMutableArray alloc] initWithObjects:
384 @"Current Track Info",
397 // Get our preferred menu
398 myItems = [[df arrayForKey:@"menu"] mutableCopy];
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];
408 // Items that show should a submenu image
409 submenuItems = [[NSArray alloc] initWithObjects:
419 NSMutableDictionary *loginwindow;
420 NSMutableArray *loginarray;
421 NSEnumerator *loginEnum;
424 // Fill in the number of songs in advance to show field
425 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
427 // Fill in hot key buttons
428 if ([df objectForKey:@"PlayPause"]){
429 playPauseCombo = [df keyComboForKey:@"PlayPause"];
430 [playPauseButton setTitle:[playPauseCombo userDisplayRep]];
432 playPauseCombo = [[KeyCombo alloc] init];
435 if ([df objectForKey:@"NextTrack"]) {
436 nextTrackCombo = [df keyComboForKey:@"NextTrack"];
437 [nextTrackButton setTitle:[nextTrackCombo userDisplayRep]];
439 nextTrackCombo = [[KeyCombo alloc] init];
442 if ([df objectForKey:@"PrevTrack"]) {
443 prevTrackCombo = [df keyComboForKey:@"PrevTrack"];
444 [previousTrackButton setTitle:[prevTrackCombo userDisplayRep]];
446 prevTrackCombo = [[KeyCombo alloc] init];
449 if ([df objectForKey:@"TrackInfo"]) {
450 trackInfoCombo = [df keyComboForKey:@"TrackInfo"];
451 [trackInfoButton setTitle:[trackInfoCombo userDisplayRep]];
453 trackInfoCombo = [[KeyCombo alloc] init];
456 if ([df objectForKey:@"UpcomingSongs"]) {
457 upcomingSongsCombo = [df keyComboForKey:@"UpcomingSongs"];
458 [upcomingSongsButton setTitle:[upcomingSongsCombo userDisplayRep]];
460 upcomingSongsCombo = [[KeyCombo alloc] init];
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];
469 // Set the launch at login checkbox state
471 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
472 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
474 loginEnum = [loginarray objectEnumerator];
475 while ( (anItem = [loginEnum nextObject]) ) {
476 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
477 [launchAtLoginCheckbox setState:NSOnState];
483 /*************************************************************************/
485 #pragma mark NSWindow DELEGATE METHODS
486 /*************************************************************************/
488 - (void)windowWillClose:(NSNotification *)note
490 [(MainController *)controller closePreferences];
494 /*************************************************************************/
496 #pragma mark NSTableView DATASOURCE METHODS
497 /*************************************************************************/
499 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
501 if (aTableView == menuTableView) {
502 return [myItems count];
504 return [availableItems count];
508 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
510 if (aTableView == menuTableView) {
511 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
512 return [myItems objectAtIndex:rowIndex];
514 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
516 return [NSImage imageNamed:@"submenu"];
522 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
523 return [availableItems objectAtIndex:rowIndex];
525 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
526 return [NSImage imageNamed:@"submenu"];
534 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
536 if (tableView == menuTableView) {
537 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
538 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
542 if (tableView == allTableView) {
543 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
544 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
550 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
554 NSString *dragData, *temp;
556 pb = [info draggingPasteboard];
558 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
559 dragData = [pb stringForType:@"MenuTableViewPboardType"];
560 dragRow = [dragData intValue];
561 temp = [myItems objectAtIndex:dragRow];
562 [myItems removeObjectAtIndex:dragRow];
564 if (tableView == menuTableView) {
566 [myItems insertObject:temp atIndex:row - 1];
568 [myItems insertObject:temp atIndex:row];
571 if (![temp isEqualToString:@"<separator>"]) {
572 [availableItems addObject:temp];
575 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
576 dragData = [pb stringForType:@"AllTableViewPboardType"];
577 dragRow = [dragData intValue];
578 temp = [availableItems objectAtIndex:dragRow];
580 if (![temp isEqualToString:@"<separator>"]) {
581 [availableItems removeObjectAtIndex:dragRow];
583 [myItems insertObject:temp atIndex:row];
586 [menuTableView reloadData];
587 [allTableView reloadData];
591 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
593 if (tableView == allTableView) {
594 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
595 return NSDragOperationNone;
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;
605 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
606 return NSDragOperationGeneric;
609 if (operation == NSTableViewDropOn || row == -1)
611 return NSDragOperationNone;
614 return NSDragOperationGeneric;
618 /*************************************************************************/
620 #pragma mark DEALLOCATION METHODS
621 /*************************************************************************/
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];