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", @"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", @"<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", 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 [songRatingCheckbox setState:[defaults boolForKey:@"showRating"] ? NSOnState : NSOffState];
149 [trackNumberCheckbox setState:[defaults boolForKey:@"showTrackNum"] ? NSOnState : NSOffState];
150 [trackTimeCheckbox setState:[defaults boolForKey:@"showTime"] ? NSOnState : NSOffState];
151 [yearCheckbox setState:[defaults boolForKey:@"showYear"] ? NSOnState : NSOffState];
158 [self setKeyCombo:nil];
159 [playPauseCombo release];
160 [nextTrackCombo release];
161 [prevTrackCombo release];
162 [trackInfoCombo release];
163 [upcomingSongsCombo release];
164 [keyComboPanel release];
165 [menuTableView setDataSource:nil];
166 [allTableView setDataSource:nil];
168 [availableItems release];
169 [submenuItems release];
173 - (IBAction)apply:(id)sender
175 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
176 [defaults setObject:myItems forKey:@"menu"];
179 [defaults setKeyCombo:playPauseCombo forKey:@"PlayPause"];
180 [defaults setKeyCombo:nextTrackCombo forKey:@"NextTrack"];
181 [defaults setKeyCombo:prevTrackCombo forKey:@"PrevTrack"];
182 [defaults setKeyCombo:trackInfoCombo forKey:@"TrackInfo"];
183 [defaults setKeyCombo:upcomingSongsCombo forKey:@"UpcomingSongs"];
185 //Set info checkboxes
186 [defaults setBool:[albumCheckbox state] forKey:@"showAlbum"];
187 [defaults setBool:[nameCheckbox state] forKey:@"showName"];
188 [defaults setBool:[artistCheckbox state] forKey:@"showArtist"];
189 [defaults setBool:[songRatingCheckbox state] forKey:@"showRating"];
190 [defaults setBool:[trackNumberCheckbox state] forKey:@"showTrackNum"];
191 [defaults setBool:[trackTimeCheckbox state] forKey:@"showTime"];
192 [defaults setBool:[yearCheckbox state] forKey:@"showYear"];
194 //Set songs in advance
195 if ([songsInAdvance intValue])
197 [defaults setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
201 [defaults setInteger:5 forKey:@"SongsInAdvance"];
208 - (IBAction)cancel:(id)sender
211 [mt closePreferences];
214 - (IBAction)cancelHotKey:(id)sender
216 [[NSNotificationCenter defaultCenter] removeObserver:self];
217 [NSApp endSheet:keyComboPanel];
218 [keyComboPanel orderOut:nil];
221 - (IBAction)clearHotKey:(id)sender
223 [self setKeyCombo:[KeyCombo clearKeyCombo]];
226 - (IBAction)okHotKey:(id)sender
229 if (([combo modifiers] <= 0) && ([combo keyCode] >= 0))
231 [window setLevel:NSNormalWindowLevel];
232 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);
233 [window setLevel:NSStatusWindowLevel];
237 string = [combo userDisplayRep];
243 if ([setHotKey isEqualToString:@"PlayPause"])
245 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
246 [combo isEqual:trackInfoCombo] || [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 playPauseCombo = [combo copy];
255 [playPauseButton setTitle:string];
257 else if ([setHotKey isEqualToString:@"NextTrack"])
259 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
260 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
261 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
263 [window setLevel:NSNormalWindowLevel];
264 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
265 [window setLevel:NSStatusWindowLevel];
268 nextTrackCombo = [combo copy];
269 [nextTrackButton setTitle:string];
271 else if ([setHotKey isEqualToString:@"PrevTrack"])
273 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
274 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
275 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
277 [window setLevel:NSNormalWindowLevel];
278 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
279 [window setLevel:NSStatusWindowLevel];
282 prevTrackCombo = [combo copy];
283 [previousTrackButton setTitle:string];
285 else if ([setHotKey isEqualToString:@"TrackInfo"])
287 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
288 [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) &&
289 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
291 [window setLevel:NSNormalWindowLevel];
292 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
293 [window setLevel:NSStatusWindowLevel];
296 trackInfoCombo = [combo copy];
297 [trackInfoButton setTitle:string];
299 else if ([setHotKey isEqualToString:@"UpcomingSongs"])
301 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
302 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
303 !(([combo modifiers] == -1) && ([combo keyCode] == -1)))
305 [window setLevel:NSNormalWindowLevel];
306 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
307 [window setLevel:NSStatusWindowLevel];
310 upcomingSongsCombo = [combo copy];
311 [upcomingSongsButton setTitle:string];
313 [self cancelHotKey:sender];
316 - (IBAction)save:(id)sender
320 [mt closePreferences];
323 - (IBAction)setCurrentTrackInfo:(id)sender
325 [self setKeyCombo:trackInfoCombo];
326 [self setHotKey:@"TrackInfo"];
329 - (IBAction)setNextTrack:(id)sender
331 [self setKeyCombo:nextTrackCombo];
332 [self setHotKey:@"NextTrack"];
335 - (IBAction)setPlayPause:(id)sender
337 [self setKeyCombo:playPauseCombo];
338 [self setHotKey:@"PlayPause"];
341 - (IBAction)setPreviousTrack:(id)sender
343 [self setKeyCombo:prevTrackCombo];
344 [self setHotKey:@"PrevTrack"];
347 - (IBAction)setUpcomingSongs:(id)sender
349 [self setKeyCombo:upcomingSongsCombo];
350 [self setHotKey:@"UpcomingSongs"];
353 - (void)setHotKey:(NSString *)key
356 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
357 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
360 - (void)keyEvent:(NSNotification *)note
362 NSDictionary *info = [note userInfo];
367 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
368 modifiers = [[info objectForKey:@"Modifiers"] longValue];
370 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
371 [self setKeyCombo:newCombo];
374 - (void)setKeyCombo:(KeyCombo *)newCombo
378 combo = [newCombo copy];
380 string = [combo userDisplayRep];
385 [keyComboField setStringValue:string];
390 // Table View Datasource Methods
394 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
396 if (aTableView == menuTableView)
398 return [myItems count];
402 return [availableItems count];
406 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
408 if (aTableView == menuTableView)
410 if ([[aTableColumn identifier] isEqualToString:@"name"])
412 return [myItems objectAtIndex:rowIndex];
416 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
418 return [NSImage imageNamed:@"submenu"];
428 if ([[aTableColumn identifier] isEqualToString:@"name"])
430 return [availableItems objectAtIndex:rowIndex];
434 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]])
436 return [NSImage imageNamed:@"submenu"];
446 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
448 if (tableView == menuTableView)
450 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
451 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
455 if (tableView == allTableView)
457 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
458 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
464 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
468 NSString *dragData, *temp;
470 pb = [info draggingPasteboard];
472 if ([[pb types] containsObject:@"MenuTableViewPboardType"])
474 dragData = [pb stringForType:@"MenuTableViewPboardType"];
475 dragRow = [dragData intValue];
476 temp = [myItems objectAtIndex:dragRow];
477 [myItems removeObjectAtIndex:dragRow];
479 if (tableView == menuTableView)
483 [myItems insertObject:temp atIndex:row - 1];
487 [myItems insertObject:temp atIndex:row];
492 if (![temp isEqualToString:@"<separator>"])
494 [availableItems addObject:temp];
498 else if ([[pb types] containsObject:@"AllTableViewPboardType"])
500 dragData = [pb stringForType:@"AllTableViewPboardType"];
501 dragRow = [dragData intValue];
502 temp = [availableItems objectAtIndex:dragRow];
504 if (![temp isEqualToString:@"<separator>"])
506 [availableItems removeObjectAtIndex:dragRow];
508 [myItems insertObject:temp atIndex:row];
511 [menuTableView reloadData];
512 [allTableView reloadData];
516 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
518 if (tableView == allTableView)
520 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"])
522 return NSDragOperationNone;
525 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"])
527 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
528 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"])
530 return NSDragOperationNone;
534 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
535 return NSDragOperationGeneric;
538 if (operation == NSTableViewDropOn || row == -1)
540 return NSDragOperationNone;
543 return NSDragOperationGeneric;