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];
15 [mt registerDefaultsIfNeeded];
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];
40 //Delete items in the availableItems array that are already part of the menu
41 for (i = 0; i < [myItems count]; i++) {
42 NSString *item = [myItems objectAtIndex:i];
43 if (![item isEqualToString:@"<separator>"])
45 [availableItems removeObject:item];
49 //Items that show should a submenu image
50 submenuItems = [[NSArray alloc] initWithObjects:@"Upcoming Songs", @"Playlists", @"EQ Presets", nil];
52 //Fill in the number of songs in advance to show field
53 [songsInAdvance setIntValue:[defaults integerForKey:@"SongsInAdvance"]];
55 //Fill in hot key buttons
56 if ([defaults objectForKey:@"PlayPause"]){
57 playPauseCombo = [defaults keyComboForKey:@"PlayPause"];
58 [playPauseButton setTitle:[playPauseCombo userDisplayRep]];
60 playPauseCombo = [[KeyCombo alloc] init];
63 if ([defaults objectForKey:@"NextTrack"]) {
64 nextTrackCombo = [defaults keyComboForKey:@"NextTrack"];
65 [nextTrackButton setTitle:[nextTrackCombo userDisplayRep]];
67 nextTrackCombo = [[KeyCombo alloc] init];
70 if ([defaults objectForKey:@"PrevTrack"]) {
71 prevTrackCombo = [defaults keyComboForKey:@"PrevTrack"];
72 [previousTrackButton setTitle:[prevTrackCombo userDisplayRep]];
74 prevTrackCombo = [[KeyCombo alloc] init];
77 if ([defaults objectForKey:@"TrackInfo"]) {
78 trackInfoCombo = [defaults keyComboForKey:@"TrackInfo"];
79 [trackInfoButton setTitle:[trackInfoCombo userDisplayRep]];
81 trackInfoCombo = [[KeyCombo alloc] init];
84 if ([defaults objectForKey:@"UpcomingSongs"]) {
85 upcomingSongsCombo = [defaults keyComboForKey:@"UpcomingSongs"];
86 [upcomingSongsButton setTitle:[upcomingSongsCombo userDisplayRep]];
88 upcomingSongsCombo = [[KeyCombo alloc] init];
91 //Check current track info buttons
92 [albumCheckbox setState:[defaults boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
93 [nameCheckbox setState:[defaults boolForKey:@"showName"] ? NSOnState : NSOffState];
94 [artistCheckbox setState:[defaults boolForKey:@"showArtist"] ? NSOnState : NSOffState];
95 [trackTimeCheckbox setState:[defaults boolForKey:@"showTime"] ? NSOnState : NSOffState];
97 //Set the launch at login checkbox state
99 NSMutableDictionary *loginwindow;
100 NSMutableArray *loginarray;
103 [defaults synchronize];
104 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
105 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
107 for (i = 0; i < [loginarray count]; i++) {
108 NSDictionary *tempDict = [loginarray objectAtIndex:i];
109 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
110 [launchAtLoginCheckbox setState:NSOnState];
120 [self setKeyCombo:nil];
121 [playPauseCombo release];
122 [nextTrackCombo release];
123 [prevTrackCombo release];
124 [trackInfoCombo release];
125 [upcomingSongsCombo release];
126 [keyComboPanel release];
127 [menuTableView setDataSource:nil];
128 [allTableView setDataSource:nil];
130 [availableItems release];
131 [submenuItems release];
135 - (IBAction)apply:(id)sender
137 ProcessSerialNumber psn;
138 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
139 [defaults setObject:myItems forKey:@"menu"];
142 [defaults setKeyCombo:playPauseCombo forKey:@"PlayPause"];
143 [defaults setKeyCombo:nextTrackCombo forKey:@"NextTrack"];
144 [defaults setKeyCombo:prevTrackCombo forKey:@"PrevTrack"];
145 [defaults setKeyCombo:trackInfoCombo forKey:@"TrackInfo"];
146 [defaults setKeyCombo:upcomingSongsCombo forKey:@"UpcomingSongs"];
148 //Set info checkboxes
149 [defaults setBool:[albumCheckbox state] forKey:@"showAlbum"];
150 [defaults setBool:[nameCheckbox state] forKey:@"showName"];
151 [defaults setBool:[artistCheckbox state] forKey:@"showArtist"];
152 [defaults setBool:[trackTimeCheckbox state] forKey:@"showTime"];
154 //Here we set whether we will launch at login by modifying loginwindow.plist
155 if ([launchAtLoginCheckbox state] == NSOnState) {
156 NSMutableDictionary *loginwindow;
157 NSMutableArray *loginarray;
158 ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);;
162 [defaults synchronize];
163 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
164 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
166 for (i = 0; i < [loginarray count]; i++) {
167 NSDictionary *tempDict = [loginarray objectAtIndex:i];
168 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
174 AEDesc scriptDesc, resultDesc;
175 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]];
177 AECreateDesc(typeChar, [script cString], [script cStringLength],
180 OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
182 AEDisposeDesc(&scriptDesc);
183 AEDisposeDesc(&resultDesc);
184 CloseComponent(temp);
187 NSMutableDictionary *loginwindow;
188 NSMutableArray *loginarray;
191 [[NSUserDefaults standardUserDefaults] synchronize];
192 loginwindow = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
193 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
195 for (i = 0; i < [loginarray count]; i++) {
196 NSDictionary *tempDict = [loginarray objectAtIndex:i];
197 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
198 [loginarray removeObjectAtIndex:i];
199 [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
200 [defaults synchronize];
206 //Set songs in advance
207 if ([songsInAdvance intValue]) {
208 [defaults setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
210 [defaults setInteger:5 forKey:@"SongsInAdvance"];
213 psn = [mt iTunesPSN];
214 if (!((psn.highLongOfPSN == kNoProcess) && (psn.lowLongOfPSN == 0))) {
220 - (IBAction)cancel:(id)sender
223 [mt closePreferences];
226 - (IBAction)cancelHotKey:(id)sender
228 [[NSNotificationCenter defaultCenter] removeObserver:self];
229 [NSApp endSheet:keyComboPanel];
230 [keyComboPanel orderOut:nil];
233 - (IBAction)clearHotKey:(id)sender
235 [self setKeyCombo:[KeyCombo clearKeyCombo]];
238 - (IBAction)okHotKey:(id)sender
241 if (([combo modifiers] <= 0) && ([combo keyCode] >= 0)) {
242 [window setLevel:NSNormalWindowLevel];
243 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);
244 [window setLevel:NSStatusWindowLevel];
248 string = [combo userDisplayRep];
253 if ([setHotKey isEqualToString:@"PlayPause"]) {
254 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
255 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
256 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
258 [window setLevel:NSNormalWindowLevel];
259 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
260 [window setLevel:NSStatusWindowLevel];
263 playPauseCombo = [combo copy];
264 [playPauseButton setTitle:string];
266 else if ([setHotKey isEqualToString:@"NextTrack"])
268 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
269 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
270 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
272 [window setLevel:NSNormalWindowLevel];
273 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
274 [window setLevel:NSStatusWindowLevel];
277 nextTrackCombo = [combo copy];
278 [nextTrackButton setTitle:string];
280 else if ([setHotKey isEqualToString:@"PrevTrack"])
282 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
283 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
284 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
286 [window setLevel:NSNormalWindowLevel];
287 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
288 [window setLevel:NSStatusWindowLevel];
291 prevTrackCombo = [combo copy];
292 [previousTrackButton setTitle:string];
294 else if ([setHotKey isEqualToString:@"TrackInfo"])
296 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
297 [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) &&
298 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
300 [window setLevel:NSNormalWindowLevel];
301 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
302 [window setLevel:NSStatusWindowLevel];
305 trackInfoCombo = [combo copy];
306 [trackInfoButton setTitle:string];
308 else if ([setHotKey isEqualToString:@"UpcomingSongs"])
310 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
311 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
312 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
314 [window setLevel:NSNormalWindowLevel];
315 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
316 [window setLevel:NSStatusWindowLevel];
319 upcomingSongsCombo = [combo copy];
320 [upcomingSongsButton setTitle:string];
322 [self cancelHotKey:sender];
325 - (IBAction)save:(id)sender
329 [mt closePreferences];
332 - (IBAction)setCurrentTrackInfo:(id)sender
334 [self setKeyCombo:trackInfoCombo];
335 [self setHotKey:@"TrackInfo"];
338 - (IBAction)setNextTrack:(id)sender
340 [self setKeyCombo:nextTrackCombo];
341 [self setHotKey:@"NextTrack"];
344 - (IBAction)setPlayPause:(id)sender
346 [self setKeyCombo:playPauseCombo];
347 [self setHotKey:@"PlayPause"];
350 - (IBAction)setPreviousTrack:(id)sender
352 [self setKeyCombo:prevTrackCombo];
353 [self setHotKey:@"PrevTrack"];
356 - (IBAction)setUpcomingSongs:(id)sender
358 [self setKeyCombo:upcomingSongsCombo];
359 [self setHotKey:@"UpcomingSongs"];
362 - (void)setHotKey:(NSString *)key
365 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
366 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
369 - (void)keyEvent:(NSNotification *)note
371 NSDictionary *info = [note userInfo];
376 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
377 modifiers = [[info objectForKey:@"Modifiers"] longValue];
379 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
380 [self setKeyCombo:newCombo];
383 - (void)setKeyCombo:(KeyCombo *)newCombo
387 combo = [newCombo copy];
389 string = [combo userDisplayRep];
393 [keyComboField setStringValue:string];
398 // Table View Datasource Methods
402 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
404 if (aTableView == menuTableView) {
405 return [myItems count];
407 return [availableItems count];
411 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
413 if (aTableView == menuTableView) {
414 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
415 return [myItems objectAtIndex:rowIndex];
417 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
419 return [NSImage imageNamed:@"submenu"];
425 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
426 return [availableItems objectAtIndex:rowIndex];
428 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
429 return [NSImage imageNamed:@"submenu"];
437 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
439 if (tableView == menuTableView) {
440 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
441 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
445 if (tableView == allTableView) {
446 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
447 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
453 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
457 NSString *dragData, *temp;
459 pb = [info draggingPasteboard];
461 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
462 dragData = [pb stringForType:@"MenuTableViewPboardType"];
463 dragRow = [dragData intValue];
464 temp = [myItems objectAtIndex:dragRow];
465 [myItems removeObjectAtIndex:dragRow];
467 if (tableView == menuTableView) {
469 [myItems insertObject:temp atIndex:row - 1];
471 [myItems insertObject:temp atIndex:row];
474 if (![temp isEqualToString:@"<separator>"]) {
475 [availableItems addObject:temp];
478 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
479 dragData = [pb stringForType:@"AllTableViewPboardType"];
480 dragRow = [dragData intValue];
481 temp = [availableItems objectAtIndex:dragRow];
483 if (![temp isEqualToString:@"<separator>"]) {
484 [availableItems removeObjectAtIndex:dragRow];
486 [myItems insertObject:temp atIndex:row];
489 [menuTableView reloadData];
490 [allTableView reloadData];
494 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
496 if (tableView == allTableView) {
497 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
498 return NSDragOperationNone;
501 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
502 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
503 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
504 return NSDragOperationNone;
508 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
509 return NSDragOperationGeneric;
512 if (operation == NSTableViewDropOn || row == -1)
514 return NSDragOperationNone;
517 return NSDragOperationGeneric;