1 #import "PreferencesController.h"
3 #import "HotKeyCenter.h"
5 @implementation PreferencesController
7 - (id)initWithMenuTunes:(MenuTunes *)tunes;
9 if ( (self = [super init]) ) {
11 NSImageCell *imgCell = [[[NSImageCell alloc] init] autorelease];
12 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
18 [NSBundle loadNibNamed:@"Preferences" owner:self];
21 [window setLevel:NSStatusWindowLevel];
23 [window makeKeyAndOrderFront:nil];
25 //Set the table view cells up
26 [imgCell setImageScaling:NSScaleNone];
27 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
28 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
30 //Register for drag and drop
31 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", @"AllTableViewPboardType", nil]];
32 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", @"AllTableViewPboardType", nil]];
34 //Set the list of items you can have.
35 availableItems = [[NSMutableArray alloc] initWithObjects:@"Current Track Info", @"Upcoming Songs", @"Playlists", @"EQ Presets", @"Play/Pause", @"Next Track", @"Previous Track", @"Fast Forward", @"Rewind", @"<separator>", nil];
37 //Get our preferred menu
38 myItems = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"] mutableCopy];
41 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];
42 [[NSUserDefaults standardUserDefaults] setObject:myItems forKey:@"menu"];
45 //Delete items in the availableItems array that are already part of the menu
46 for (i = 0; i < [myItems count]; i++) {
47 NSString *item = [myItems objectAtIndex:i];
48 if (![item isEqualToString:@"<separator>"])
50 [availableItems removeObject:item];
54 //Items that show should a submenu image
55 submenuItems = [[NSArray alloc] initWithObjects:@"Upcoming Songs", @"Playlists", @"EQ Presets", nil];
57 //Fill in the number of songs in advance to show field
58 if ([defaults integerForKey:@"SongsInAdvance"]) {
59 [songsInAdvance setIntValue:[defaults integerForKey:@"SongsInAdvance"]];
61 [songsInAdvance setIntValue:5];
64 //Fill in hot key buttons
65 if ([defaults objectForKey:@"PlayPause"]){
66 playPauseCombo = [defaults keyComboForKey:@"PlayPause"];
67 [playPauseButton setTitle:[playPauseCombo userDisplayRep]];
69 playPauseCombo = [[KeyCombo alloc] init];
72 if ([defaults objectForKey:@"NextTrack"]) {
73 nextTrackCombo = [defaults keyComboForKey:@"NextTrack"];
74 [nextTrackButton setTitle:[nextTrackCombo userDisplayRep]];
76 nextTrackCombo = [[KeyCombo alloc] init];
79 if ([defaults objectForKey:@"PrevTrack"]) {
80 prevTrackCombo = [defaults keyComboForKey:@"PrevTrack"];
81 [previousTrackButton setTitle:[prevTrackCombo userDisplayRep]];
83 prevTrackCombo = [[KeyCombo alloc] init];
86 if ([defaults objectForKey:@"TrackInfo"]) {
87 trackInfoCombo = [defaults keyComboForKey:@"TrackInfo"];
88 [trackInfoButton setTitle:[trackInfoCombo userDisplayRep]];
90 trackInfoCombo = [[KeyCombo alloc] init];
93 if ([defaults objectForKey:@"UpcomingSongs"]) {
94 upcomingSongsCombo = [defaults keyComboForKey:@"UpcomingSongs"];
95 [upcomingSongsButton setTitle:[upcomingSongsCombo userDisplayRep]];
97 upcomingSongsCombo = [[KeyCombo alloc] init];
100 //Check current track info buttons
102 //Album and name get special treatment because they are defaults
103 if ( (temp = [defaults stringForKey:@"showAlbum"]) ) {
104 if ((temp == nil) || [temp isEqualToString:@"1"]) {
105 [albumCheckbox setState:NSOnState];
107 [albumCheckbox setState:NSOffState];
111 if ( (temp = [defaults stringForKey:@"showName"]) ) {
112 if ((temp == nil) || [temp isEqualToString:@"1"]) {
113 [nameCheckbox setState:NSOnState];
115 [nameCheckbox setState:NSOffState];
119 [artistCheckbox setState:[defaults boolForKey:@"showArtist"] ? NSOnState : NSOffState];
120 [trackTimeCheckbox setState:[defaults boolForKey:@"showTime"] ? NSOnState : NSOffState];
122 //Set the launch at login checkbox state
124 NSMutableDictionary *loginwindow;
125 NSMutableArray *loginarray;
128 [defaults synchronize];
129 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
130 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
132 for (i = 0; i < [loginarray count]; i++) {
133 NSDictionary *tempDict = [loginarray objectAtIndex:i];
135 //Here we are seeing if our program is already in loginwindow.plist.
136 //See the problem below for this problem here. We will do the same thing.
138 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:@"VocabularyBuilder.app"]) {
139 [launchAtLoginCheckbox setState:NSOnState];
149 [self setKeyCombo:nil];
150 [playPauseCombo release];
151 [nextTrackCombo release];
152 [prevTrackCombo release];
153 [trackInfoCombo release];
154 [upcomingSongsCombo release];
155 [keyComboPanel release];
156 [menuTableView setDataSource:nil];
157 [allTableView setDataSource:nil];
159 [availableItems release];
160 [submenuItems release];
164 - (IBAction)apply:(id)sender
166 ProcessSerialNumber psn;
167 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
168 [defaults setObject:myItems forKey:@"menu"];
171 [defaults setKeyCombo:playPauseCombo forKey:@"PlayPause"];
172 [defaults setKeyCombo:nextTrackCombo forKey:@"NextTrack"];
173 [defaults setKeyCombo:prevTrackCombo forKey:@"PrevTrack"];
174 [defaults setKeyCombo:trackInfoCombo forKey:@"TrackInfo"];
175 [defaults setKeyCombo:upcomingSongsCombo forKey:@"UpcomingSongs"];
177 //Set info checkboxes
178 [defaults setBool:[albumCheckbox state] forKey:@"showAlbum"];
179 [defaults setBool:[nameCheckbox state] forKey:@"showName"];
180 [defaults setBool:[artistCheckbox state] forKey:@"showArtist"];
181 [defaults setBool:[trackTimeCheckbox state] forKey:@"showTime"];
183 //Here we set whether we will launch at login by modifying loginwindow.plist
184 if ([launchAtLoginCheckbox state] == NSOnState) {
185 NSMutableDictionary *loginwindow;
186 NSMutableArray *loginarray;
190 [defaults synchronize];
191 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
192 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
194 for (i = 0; i < [loginarray count]; i++) {
195 NSDictionary *tempDict = [loginarray objectAtIndex:i];
197 //Here we are seeing if our program is already in loginwindow.plist.
198 //See the problem below for this problem here. We will do the same thing.
200 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:@"VocabularyBuilder.app"]) {
206 [loginarray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], @"Hide", [[NSBundle mainBundle] bundlePath], @"Path", nil]];
208 [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
209 [defaults synchronize];
212 NSMutableDictionary *loginwindow;
213 NSMutableArray *loginarray;
216 [[NSUserDefaults standardUserDefaults] synchronize];
217 loginwindow = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
218 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
220 for (i = 0; i < [loginarray count]; i++) {
221 NSDictionary *tempDict = [loginarray objectAtIndex:i];
223 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:@"VocabularyBuilder.app"]) {
224 [loginarray removeObjectAtIndex:i];
225 [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
226 [defaults synchronize];
232 //Set songs in advance
233 if ([songsInAdvance intValue]) {
234 [defaults setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
236 [defaults setInteger:5 forKey:@"SongsInAdvance"];
239 psn = [mt iTunesPSN];
240 if (!((psn.highLongOfPSN == kNoProcess) && (psn.lowLongOfPSN == 0))) {
246 - (IBAction)cancel:(id)sender
249 [mt closePreferences];
252 - (IBAction)cancelHotKey:(id)sender
254 [[NSNotificationCenter defaultCenter] removeObserver:self];
255 [NSApp endSheet:keyComboPanel];
256 [keyComboPanel orderOut:nil];
259 - (IBAction)clearHotKey:(id)sender
261 [self setKeyCombo:[KeyCombo clearKeyCombo]];
264 - (IBAction)okHotKey:(id)sender
267 if (([combo modifiers] <= 0) && ([combo keyCode] >= 0)) {
268 [window setLevel:NSNormalWindowLevel];
269 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);
270 [window setLevel:NSStatusWindowLevel];
274 string = [combo userDisplayRep];
279 if ([setHotKey isEqualToString:@"PlayPause"]) {
280 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
281 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
282 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
284 [window setLevel:NSNormalWindowLevel];
285 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
286 [window setLevel:NSStatusWindowLevel];
289 playPauseCombo = [combo copy];
290 [playPauseButton setTitle:string];
292 else if ([setHotKey isEqualToString:@"NextTrack"])
294 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
295 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
296 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
298 [window setLevel:NSNormalWindowLevel];
299 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
300 [window setLevel:NSStatusWindowLevel];
303 nextTrackCombo = [combo copy];
304 [nextTrackButton setTitle:string];
306 else if ([setHotKey isEqualToString:@"PrevTrack"])
308 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
309 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
310 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
312 [window setLevel:NSNormalWindowLevel];
313 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
314 [window setLevel:NSStatusWindowLevel];
317 prevTrackCombo = [combo copy];
318 [previousTrackButton setTitle:string];
320 else if ([setHotKey isEqualToString:@"TrackInfo"])
322 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
323 [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) &&
324 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
326 [window setLevel:NSNormalWindowLevel];
327 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
328 [window setLevel:NSStatusWindowLevel];
331 trackInfoCombo = [combo copy];
332 [trackInfoButton setTitle:string];
334 else if ([setHotKey isEqualToString:@"UpcomingSongs"])
336 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
337 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
338 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
340 [window setLevel:NSNormalWindowLevel];
341 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
342 [window setLevel:NSStatusWindowLevel];
345 upcomingSongsCombo = [combo copy];
346 [upcomingSongsButton setTitle:string];
348 [self cancelHotKey:sender];
351 - (IBAction)save:(id)sender
355 [mt closePreferences];
358 - (IBAction)setCurrentTrackInfo:(id)sender
360 [self setKeyCombo:trackInfoCombo];
361 [self setHotKey:@"TrackInfo"];
364 - (IBAction)setNextTrack:(id)sender
366 [self setKeyCombo:nextTrackCombo];
367 [self setHotKey:@"NextTrack"];
370 - (IBAction)setPlayPause:(id)sender
372 [self setKeyCombo:playPauseCombo];
373 [self setHotKey:@"PlayPause"];
376 - (IBAction)setPreviousTrack:(id)sender
378 [self setKeyCombo:prevTrackCombo];
379 [self setHotKey:@"PrevTrack"];
382 - (IBAction)setUpcomingSongs:(id)sender
384 [self setKeyCombo:upcomingSongsCombo];
385 [self setHotKey:@"UpcomingSongs"];
388 - (void)setHotKey:(NSString *)key
391 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
392 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
395 - (void)keyEvent:(NSNotification *)note
397 NSDictionary *info = [note userInfo];
402 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
403 modifiers = [[info objectForKey:@"Modifiers"] longValue];
405 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
406 [self setKeyCombo:newCombo];
409 - (void)setKeyCombo:(KeyCombo *)newCombo
413 combo = [newCombo copy];
415 string = [combo userDisplayRep];
419 [keyComboField setStringValue:string];
424 // Text Field Delegate
428 - (void)controlTextDidChange:(NSNotification *)note
430 NSLog(@"%@", [note userInfo]);
435 // Table View Datasource Methods
439 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
441 if (aTableView == menuTableView) {
442 return [myItems count];
444 return [availableItems count];
448 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
450 if (aTableView == menuTableView) {
451 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
452 return [myItems objectAtIndex:rowIndex];
454 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
456 return [NSImage imageNamed:@"submenu"];
462 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
463 return [availableItems objectAtIndex:rowIndex];
465 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
466 return [NSImage imageNamed:@"submenu"];
474 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
476 if (tableView == menuTableView) {
477 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
478 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
482 if (tableView == allTableView) {
483 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
484 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
490 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
494 NSString *dragData, *temp;
496 pb = [info draggingPasteboard];
498 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
499 dragData = [pb stringForType:@"MenuTableViewPboardType"];
500 dragRow = [dragData intValue];
501 temp = [myItems objectAtIndex:dragRow];
502 [myItems removeObjectAtIndex:dragRow];
504 if (tableView == menuTableView) {
506 [myItems insertObject:temp atIndex:row - 1];
508 [myItems insertObject:temp atIndex:row];
511 if (![temp isEqualToString:@"<separator>"]) {
512 [availableItems addObject:temp];
515 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
516 dragData = [pb stringForType:@"AllTableViewPboardType"];
517 dragRow = [dragData intValue];
518 temp = [availableItems objectAtIndex:dragRow];
520 if (![temp isEqualToString:@"<separator>"]) {
521 [availableItems removeObjectAtIndex:dragRow];
523 [myItems insertObject:temp atIndex:row];
526 [menuTableView reloadData];
527 [allTableView reloadData];
531 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
533 if (tableView == allTableView) {
534 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
535 return NSDragOperationNone;
538 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
539 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
540 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
541 return NSDragOperationNone;
545 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
546 return NSDragOperationGeneric;
549 if (operation == NSTableViewDropOn || row == -1)
551 return NSDragOperationNone;
554 return NSDragOperationGeneric;