1 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
5 @implementation PreferencesController
7 - (id)initWithMenuTunes:(MenuTunes *)tunes;
9 if ( (self = [super init]) )
12 NSImageCell *imgCell = [[[NSImageCell alloc] init] autorelease];
13 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
19 [NSBundle loadNibNamed:@"Preferences" owner:self];
22 [window setLevel:NSStatusWindowLevel];
24 [window makeKeyAndOrderFront:nil];
26 //Set the table view cells up
27 [imgCell setImageScaling:NSScaleNone];
28 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
29 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
31 //Register for drag and drop
32 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", @"AllTableViewPboardType", nil]];
33 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", @"AllTableViewPboardType", nil]];
35 //Set the list of items you can have.
36 availableItems = [[NSMutableArray alloc] initWithObjects:@"Current Track Info", @"Upcoming Songs", @"Playlists", @"EQ Presets", @"Play/Pause", @"Next Track", @"Previous Track", @"Fast Forward", @"Rewind", @"<separator>", nil];
38 //Get our preferred menu
39 myItems = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"] mutableCopy];
42 myItems = [[NSMutableArray alloc] initWithObjects:@"Play/Pause", @"Next Track", @"Previous Track", @"Fast Forward", @"Rewind", @"<separator>", @"Upcoming Songs", @"Playlists", @"EQ Presets", @"<separator>", @"PreferencesÉ", @"Quit", @"<separator>", @"Current Track Info", nil];
43 [[NSUserDefaults standardUserDefaults] setObject:myItems forKey:@"menu"];
46 //Delete items in the availableItems array that are already part of the menu
47 for (i = 0; i < [myItems count]; i++)
49 NSString *item = [myItems objectAtIndex:i];
50 if (![item isEqualToString:@"<separator>"])
52 [availableItems removeObject:item];
56 //Items that show should a submenu image
57 submenuItems = [[NSArray alloc] initWithObjects:@"Upcoming Songs", @"Playlists", @"EQ Presets", nil];
59 //Fill in the number of songs in advance to show field
60 if ([defaults integerForKey:@"SongsInAdvance"])
62 [songsInAdvance setIntValue:[defaults integerForKey:@"SongsInAdvance"]];
66 [songsInAdvance setIntValue:5];
69 //Fill in hot key buttons
70 if ([defaults objectForKey:@"PlayPause"])
72 playPauseCombo = [defaults keyComboForKey:@"PlayPause"];
73 [playPauseButton setTitle:[playPauseCombo userDisplayRep]];
77 playPauseCombo = [[KeyCombo alloc] init];
80 if ([defaults objectForKey:@"NextTrack"])
82 nextTrackCombo = [defaults keyComboForKey:@"NextTrack"];
83 [nextTrackButton setTitle:[nextTrackCombo userDisplayRep]];
87 nextTrackCombo = [[KeyCombo alloc] init];
90 if ([defaults objectForKey:@"PrevTrack"])
92 prevTrackCombo = [defaults keyComboForKey:@"PrevTrack"];
93 [previousTrackButton setTitle:[prevTrackCombo userDisplayRep]];
97 prevTrackCombo = [[KeyCombo alloc] init];
100 if ([defaults objectForKey:@"TrackInfo"])
102 trackInfoCombo = [defaults keyComboForKey:@"TrackInfo"];
103 [trackInfoButton setTitle:[trackInfoCombo userDisplayRep]];
107 trackInfoCombo = [[KeyCombo alloc] init];
110 if ([defaults objectForKey:@"UpcomingSongs"])
112 upcomingSongsCombo = [defaults keyComboForKey:@"UpcomingSongs"];
113 [upcomingSongsButton setTitle:[upcomingSongsCombo userDisplayRep]];
117 upcomingSongsCombo = [[KeyCombo alloc] init];
120 //Check current track info buttons
122 //Album and name get special treatment because they are defaults
123 if ( (temp = [defaults stringForKey:@"showAlbum"]) )
125 if ((temp == nil) || [temp isEqualToString:@"1"])
127 [albumCheckbox setState:NSOnState];
131 [albumCheckbox setState:NSOffState];
135 if ( (temp = [defaults stringForKey:@"showName"]) )
137 if ((temp == nil) || [temp isEqualToString:@"1"])
139 [nameCheckbox setState:NSOnState];
143 [nameCheckbox setState:NSOffState];
147 [artistCheckbox setState:[defaults boolForKey:@"showArtist"] ? NSOnState : NSOffState];
148 [trackTimeCheckbox setState:[defaults boolForKey:@"showTime"] ? NSOnState : NSOffState];
150 //Set the launch at login checkbox state
152 NSMutableDictionary *loginwindow;
153 NSMutableArray *loginarray;
156 [defaults synchronize];
157 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
158 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
160 for (i = 0; i < [loginarray count]; i++)
162 NSDictionary *tempDict = [loginarray objectAtIndex:i];
164 //Here we are seeing if our program is already in loginwindow.plist.
165 //See the problem below for this problem here. We will do the same thing.
167 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:@"VocabularyBuilder.app"])
169 [launchAtLoginCheckbox setState:NSOnState];
179 [self setKeyCombo:nil];
180 [playPauseCombo release];
181 [nextTrackCombo release];
182 [prevTrackCombo release];
183 [trackInfoCombo release];
184 [upcomingSongsCombo release];
185 [keyComboPanel release];
186 [menuTableView setDataSource:nil];
187 [allTableView setDataSource:nil];
189 [availableItems release];
190 [submenuItems release];
194 - (IBAction)apply:(id)sender
196 ProcessSerialNumber psn;
197 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
198 [defaults setObject:myItems forKey:@"menu"];
201 [defaults setKeyCombo:playPauseCombo forKey:@"PlayPause"];
202 [defaults setKeyCombo:nextTrackCombo forKey:@"NextTrack"];
203 [defaults setKeyCombo:prevTrackCombo forKey:@"PrevTrack"];
204 [defaults setKeyCombo:trackInfoCombo forKey:@"TrackInfo"];
205 [defaults setKeyCombo:upcomingSongsCombo forKey:@"UpcomingSongs"];
207 //Set info checkboxes
208 [defaults setBool:[albumCheckbox state] forKey:@"showAlbum"];
209 [defaults setBool:[nameCheckbox state] forKey:@"showName"];
210 [defaults setBool:[artistCheckbox state] forKey:@"showArtist"];
211 [defaults setBool:[trackTimeCheckbox state] forKey:@"showTime"];
213 //Here we set whether we will launch at login by modifying loginwindow.plist
214 if ([launchAtLoginCheckbox state] == NSOnState)
216 NSMutableDictionary *loginwindow;
217 NSMutableArray *loginarray;
221 [defaults synchronize];
222 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
223 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
225 for (i = 0; i < [loginarray count]; i++)
227 NSDictionary *tempDict = [loginarray objectAtIndex:i];
229 //Here we are seeing if our program is already in loginwindow.plist.
230 //See the problem below for this problem here. We will do the same thing.
232 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:@"VocabularyBuilder.app"])
240 [loginarray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], @"Hide", [[NSBundle mainBundle] bundlePath], @"Path", nil]];
242 [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
243 [defaults synchronize];
248 NSMutableDictionary *loginwindow;
249 NSMutableArray *loginarray;
252 [[NSUserDefaults standardUserDefaults] synchronize];
253 loginwindow = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
254 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
256 for (i = 0; i < [loginarray count]; i++)
258 NSDictionary *tempDict = [loginarray objectAtIndex:i];
260 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:@"VocabularyBuilder.app"])
262 [loginarray removeObjectAtIndex:i];
263 [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
264 [defaults synchronize];
270 //Set songs in advance
271 if ([songsInAdvance intValue])
273 [defaults setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
277 [defaults setInteger:5 forKey:@"SongsInAdvance"];
280 psn = [mt iTunesPSN];
281 if (!((psn.highLongOfPSN == kNoProcess) && (psn.lowLongOfPSN == 0)))
288 - (IBAction)cancel:(id)sender
291 [mt closePreferences];
294 - (IBAction)cancelHotKey:(id)sender
296 [[NSNotificationCenter defaultCenter] removeObserver:self];
297 [NSApp endSheet:keyComboPanel];
298 [keyComboPanel orderOut:nil];
301 - (IBAction)clearHotKey:(id)sender
303 [self setKeyCombo:[KeyCombo clearKeyCombo]];
306 - (IBAction)okHotKey:(id)sender
309 if (([combo modifiers] <= 0) && ([combo keyCode] >= 0))
311 [window setLevel:NSNormalWindowLevel];
312 NSRunAlertPanel(@"Bad Key Combo", @"Please enter a valid key combo. A valid combo must have a modifier key in it. (Command, option, shift, control).", @"OK", nil, nil, nil);
313 [window setLevel:NSStatusWindowLevel];
317 string = [combo userDisplayRep];
323 if ([setHotKey isEqualToString:@"PlayPause"])
325 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
326 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
327 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
329 [window setLevel:NSNormalWindowLevel];
330 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
331 [window setLevel:NSStatusWindowLevel];
334 playPauseCombo = [combo copy];
335 [playPauseButton setTitle:string];
337 else if ([setHotKey isEqualToString:@"NextTrack"])
339 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
340 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
341 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
343 [window setLevel:NSNormalWindowLevel];
344 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
345 [window setLevel:NSStatusWindowLevel];
348 nextTrackCombo = [combo copy];
349 [nextTrackButton setTitle:string];
351 else if ([setHotKey isEqualToString:@"PrevTrack"])
353 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
354 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
355 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
357 [window setLevel:NSNormalWindowLevel];
358 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
359 [window setLevel:NSStatusWindowLevel];
362 prevTrackCombo = [combo copy];
363 [previousTrackButton setTitle:string];
365 else if ([setHotKey isEqualToString:@"TrackInfo"])
367 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
368 [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) &&
369 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
371 [window setLevel:NSNormalWindowLevel];
372 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
373 [window setLevel:NSStatusWindowLevel];
376 trackInfoCombo = [combo copy];
377 [trackInfoButton setTitle:string];
379 else if ([setHotKey isEqualToString:@"UpcomingSongs"])
381 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
382 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
383 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
385 [window setLevel:NSNormalWindowLevel];
386 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
387 [window setLevel:NSStatusWindowLevel];
390 upcomingSongsCombo = [combo copy];
391 [upcomingSongsButton setTitle:string];
393 [self cancelHotKey:sender];
396 - (IBAction)save:(id)sender
400 [mt closePreferences];
403 - (IBAction)setCurrentTrackInfo:(id)sender
405 [self setKeyCombo:trackInfoCombo];
406 [self setHotKey:@"TrackInfo"];
409 - (IBAction)setNextTrack:(id)sender
411 [self setKeyCombo:nextTrackCombo];
412 [self setHotKey:@"NextTrack"];
415 - (IBAction)setPlayPause:(id)sender
417 [self setKeyCombo:playPauseCombo];
418 [self setHotKey:@"PlayPause"];
421 - (IBAction)setPreviousTrack:(id)sender
423 [self setKeyCombo:prevTrackCombo];
424 [self setHotKey:@"PrevTrack"];
427 - (IBAction)setUpcomingSongs:(id)sender
429 [self setKeyCombo:upcomingSongsCombo];
430 [self setHotKey:@"UpcomingSongs"];
433 - (void)setHotKey:(NSString *)key
436 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
437 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
440 - (void)keyEvent:(NSNotification *)note
442 NSDictionary *info = [note userInfo];
447 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
448 modifiers = [[info objectForKey:@"Modifiers"] longValue];
450 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
451 [self setKeyCombo:newCombo];
454 - (void)setKeyCombo:(KeyCombo *)newCombo
458 combo = [newCombo copy];
460 string = [combo userDisplayRep];
465 [keyComboField setStringValue:string];
470 // Text Field Delegate
474 - (void)controlTextDidChange:(NSNotification *)note
476 NSLog(@"%@", [note userInfo]);
481 // Table View Datasource Methods
485 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
487 if (aTableView == menuTableView)
489 return [myItems count];
493 return [availableItems count];
497 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
499 if (aTableView == menuTableView)
501 if ([[aTableColumn identifier] isEqualToString:@"name"])
503 return [myItems objectAtIndex:rowIndex];
507 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
509 return [NSImage imageNamed:@"submenu"];
519 if ([[aTableColumn identifier] isEqualToString:@"name"])
521 return [availableItems objectAtIndex:rowIndex];
525 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]])
527 return [NSImage imageNamed:@"submenu"];
537 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
539 if (tableView == menuTableView)
541 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
542 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
546 if (tableView == allTableView)
548 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
549 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
555 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
559 NSString *dragData, *temp;
561 pb = [info draggingPasteboard];
563 if ([[pb types] containsObject:@"MenuTableViewPboardType"])
565 dragData = [pb stringForType:@"MenuTableViewPboardType"];
566 dragRow = [dragData intValue];
567 temp = [myItems objectAtIndex:dragRow];
568 [myItems removeObjectAtIndex:dragRow];
570 if (tableView == menuTableView)
574 [myItems insertObject:temp atIndex:row - 1];
578 [myItems insertObject:temp atIndex:row];
583 if (![temp isEqualToString:@"<separator>"])
585 [availableItems addObject:temp];
589 else if ([[pb types] containsObject:@"AllTableViewPboardType"])
591 dragData = [pb stringForType:@"AllTableViewPboardType"];
592 dragRow = [dragData intValue];
593 temp = [availableItems objectAtIndex:dragRow];
595 if (![temp isEqualToString:@"<separator>"])
597 [availableItems removeObjectAtIndex:dragRow];
599 [myItems insertObject:temp atIndex:row];
602 [menuTableView reloadData];
603 [allTableView reloadData];
607 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
609 if (tableView == allTableView)
611 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"])
613 return NSDragOperationNone;
616 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"])
618 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
619 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"])
621 return NSDragOperationNone;
625 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
626 return NSDragOperationGeneric;
629 if (operation == NSTableViewDropOn || row == -1)
631 return NSDragOperationNone;
634 return NSDragOperationGeneric;