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
240 NSString *string = [combo userDisplayRep];
245 if ([setHotKey isEqualToString:@"PlayPause"]) {
246 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
247 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
248 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
250 [window setLevel:NSNormalWindowLevel];
251 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
252 [window setLevel:NSStatusWindowLevel];
255 playPauseCombo = [combo copy];
256 [playPauseButton setTitle:string];
258 else if ([setHotKey isEqualToString:@"NextTrack"])
260 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
261 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
262 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
264 [window setLevel:NSNormalWindowLevel];
265 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
266 [window setLevel:NSStatusWindowLevel];
269 nextTrackCombo = [combo copy];
270 [nextTrackButton setTitle:string];
272 else if ([setHotKey isEqualToString:@"PrevTrack"])
274 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
275 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
276 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
278 [window setLevel:NSNormalWindowLevel];
279 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
280 [window setLevel:NSStatusWindowLevel];
283 prevTrackCombo = [combo copy];
284 [previousTrackButton setTitle:string];
286 else if ([setHotKey isEqualToString:@"TrackInfo"])
288 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
289 [combo isEqual:playPauseCombo] || [combo isEqual:upcomingSongsCombo]) &&
290 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
292 [window setLevel:NSNormalWindowLevel];
293 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
294 [window setLevel:NSStatusWindowLevel];
297 trackInfoCombo = [combo copy];
298 [trackInfoButton setTitle:string];
300 else if ([setHotKey isEqualToString:@"UpcomingSongs"])
302 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
303 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
304 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
306 [window setLevel:NSNormalWindowLevel];
307 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
308 [window setLevel:NSStatusWindowLevel];
311 upcomingSongsCombo = [combo copy];
312 [upcomingSongsButton setTitle:string];
314 [self cancelHotKey:sender];
317 - (IBAction)save:(id)sender
321 [mt closePreferences];
324 - (IBAction)setCurrentTrackInfo:(id)sender
326 [self setKeyCombo:trackInfoCombo];
327 [self setHotKey:@"TrackInfo"];
330 - (IBAction)setNextTrack:(id)sender
332 [self setKeyCombo:nextTrackCombo];
333 [self setHotKey:@"NextTrack"];
336 - (IBAction)setPlayPause:(id)sender
338 [self setKeyCombo:playPauseCombo];
339 [self setHotKey:@"PlayPause"];
342 - (IBAction)setPreviousTrack:(id)sender
344 [self setKeyCombo:prevTrackCombo];
345 [self setHotKey:@"PrevTrack"];
348 - (IBAction)setUpcomingSongs:(id)sender
350 [self setKeyCombo:upcomingSongsCombo];
351 [self setHotKey:@"UpcomingSongs"];
354 - (void)setHotKey:(NSString *)key
357 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
358 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
361 - (void)keyEvent:(NSNotification *)note
363 NSDictionary *info = [note userInfo];
368 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
369 modifiers = [[info objectForKey:@"Modifiers"] longValue];
371 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
372 [self setKeyCombo:newCombo];
375 - (void)setKeyCombo:(KeyCombo *)newCombo
379 combo = [newCombo copy];
381 string = [combo userDisplayRep];
385 [keyComboField setStringValue:string];
390 // Table View Datasource Methods
394 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
396 if (aTableView == menuTableView) {
397 return [myItems count];
399 return [availableItems count];
403 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
405 if (aTableView == menuTableView) {
406 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
407 return [myItems objectAtIndex:rowIndex];
409 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
411 return [NSImage imageNamed:@"submenu"];
417 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
418 return [availableItems objectAtIndex:rowIndex];
420 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
421 return [NSImage imageNamed:@"submenu"];
429 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
431 if (tableView == menuTableView) {
432 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
433 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
437 if (tableView == allTableView) {
438 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
439 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
445 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
449 NSString *dragData, *temp;
451 pb = [info draggingPasteboard];
453 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
454 dragData = [pb stringForType:@"MenuTableViewPboardType"];
455 dragRow = [dragData intValue];
456 temp = [myItems objectAtIndex:dragRow];
457 [myItems removeObjectAtIndex:dragRow];
459 if (tableView == menuTableView) {
461 [myItems insertObject:temp atIndex:row - 1];
463 [myItems insertObject:temp atIndex:row];
466 if (![temp isEqualToString:@"<separator>"]) {
467 [availableItems addObject:temp];
470 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
471 dragData = [pb stringForType:@"AllTableViewPboardType"];
472 dragRow = [dragData intValue];
473 temp = [availableItems objectAtIndex:dragRow];
475 if (![temp isEqualToString:@"<separator>"]) {
476 [availableItems removeObjectAtIndex:dragRow];
478 [myItems insertObject:temp atIndex:row];
481 [menuTableView reloadData];
482 [allTableView reloadData];
486 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
488 if (tableView == allTableView) {
489 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
490 return NSDragOperationNone;
493 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
494 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
495 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
496 return NSDragOperationNone;
500 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
501 return NSDragOperationGeneric;
504 if (operation == NSTableViewDropOn || row == -1)
506 return NSDragOperationNone;
509 return NSDragOperationGeneric;