1 #import "PreferencesController.h"
2 #import "MainController.h"
3 #import "HotKeyCenter.h"
5 @implementation PreferencesController
7 - (id)initWithMenuTunes:(MainController *)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", @"Song Rating", @"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", @"Song Rating", 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 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
138 [defaults setObject:myItems forKey:@"menu"];
141 [defaults setKeyCombo:playPauseCombo forKey:@"PlayPause"];
142 [defaults setKeyCombo:nextTrackCombo forKey:@"NextTrack"];
143 [defaults setKeyCombo:prevTrackCombo forKey:@"PrevTrack"];
144 [defaults setKeyCombo:trackInfoCombo forKey:@"TrackInfo"];
145 [defaults setKeyCombo:upcomingSongsCombo forKey:@"UpcomingSongs"];
147 //Set info checkboxes
148 [defaults setBool:[albumCheckbox state] forKey:@"showAlbum"];
149 [defaults setBool:[nameCheckbox state] forKey:@"showName"];
150 [defaults setBool:[artistCheckbox state] forKey:@"showArtist"];
151 [defaults setBool:[trackTimeCheckbox state] forKey:@"showTime"];
153 //Here we set whether we will launch at login by modifying loginwindow.plist
154 if ([launchAtLoginCheckbox state] == NSOnState) {
155 NSMutableDictionary *loginwindow;
156 NSMutableArray *loginarray;
157 ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);;
161 [defaults synchronize];
162 loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
163 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
165 for (i = 0; i < [loginarray count]; i++) {
166 NSDictionary *tempDict = [loginarray objectAtIndex:i];
167 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
173 AEDesc scriptDesc, resultDesc;
174 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]];
176 AECreateDesc(typeChar, [script cString], [script cStringLength],
179 OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
181 AEDisposeDesc(&scriptDesc);
182 AEDisposeDesc(&resultDesc);
183 CloseComponent(temp);
186 NSMutableDictionary *loginwindow;
187 NSMutableArray *loginarray;
190 [[NSUserDefaults standardUserDefaults] synchronize];
191 loginwindow = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
192 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
194 for (i = 0; i < [loginarray count]; i++) {
195 NSDictionary *tempDict = [loginarray objectAtIndex:i];
196 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
197 [loginarray removeObjectAtIndex:i];
198 [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
199 [defaults synchronize];
205 //Set songs in advance
206 if ([songsInAdvance intValue]) {
207 [defaults setInteger:[songsInAdvance intValue] forKey:@"SongsInAdvance"];
209 [defaults setInteger:5 forKey:@"SongsInAdvance"];
213 NSArray *apps = [[NSWorkspace sharedWorkspace] launchedApplications];
216 for (i = 0; i < [apps count]; i++) {
217 if ([[[apps objectAtIndex:i] objectForKey:@"NSApplicationName"]
218 isEqualToString:@"iTunes"]) {
226 - (IBAction)cancel:(id)sender
229 [mt closePreferences];
232 - (IBAction)cancelHotKey:(id)sender
234 [[NSNotificationCenter defaultCenter] removeObserver:self];
235 [NSApp endSheet:keyComboPanel];
236 [keyComboPanel orderOut:nil];
239 - (IBAction)clearHotKey:(id)sender
241 [self setKeyCombo:[KeyCombo clearKeyCombo]];
244 - (IBAction)okHotKey:(id)sender
246 NSString *string = [combo userDisplayRep];
251 if ([setHotKey isEqualToString:@"PlayPause"]) {
252 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
253 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
254 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
256 [window setLevel:NSNormalWindowLevel];
257 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
258 [window setLevel:NSStatusWindowLevel];
261 playPauseCombo = [combo copy];
262 [playPauseButton setTitle:string];
264 else if ([setHotKey isEqualToString:@"NextTrack"])
266 if (([combo isEqual:playPauseCombo] || [combo isEqual:prevTrackCombo] ||
267 [combo isEqual:trackInfoCombo] || [combo isEqual:upcomingSongsCombo]) &&
268 !(([combo modifiers] == -1) && ([combo keyCode] == -1))) {
270 [window setLevel:NSNormalWindowLevel];
271 NSRunAlertPanel(@"Duplicate Key Combo", @"Please choose a unique key combo.", @"OK", nil, nil, nil);
272 [window setLevel:NSStatusWindowLevel];
275 nextTrackCombo = [combo copy];
276 [nextTrackButton setTitle:string];
278 else if ([setHotKey isEqualToString:@"PrevTrack"])
280 if (([combo isEqual:nextTrackCombo] || [combo isEqual:playPauseCombo] ||
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 prevTrackCombo = [combo copy];
290 [previousTrackButton setTitle:string];
292 else if ([setHotKey isEqualToString:@"TrackInfo"])
294 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
295 [combo isEqual:playPauseCombo] || [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 trackInfoCombo = [combo copy];
304 [trackInfoButton setTitle:string];
306 else if ([setHotKey isEqualToString:@"UpcomingSongs"])
308 if (([combo isEqual:nextTrackCombo] || [combo isEqual:prevTrackCombo] ||
309 [combo isEqual:trackInfoCombo] || [combo isEqual:playPauseCombo]) &&
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 upcomingSongsCombo = [combo copy];
318 [upcomingSongsButton setTitle:string];
320 [self cancelHotKey:sender];
323 - (IBAction)save:(id)sender
327 [mt closePreferences];
330 - (IBAction)setCurrentTrackInfo:(id)sender
332 [self setKeyCombo:trackInfoCombo];
333 [self setHotKey:@"TrackInfo"];
336 - (IBAction)setNextTrack:(id)sender
338 [self setKeyCombo:nextTrackCombo];
339 [self setHotKey:@"NextTrack"];
342 - (IBAction)setPlayPause:(id)sender
344 [self setKeyCombo:playPauseCombo];
345 [self setHotKey:@"PlayPause"];
348 - (IBAction)setPreviousTrack:(id)sender
350 [self setKeyCombo:prevTrackCombo];
351 [self setHotKey:@"PrevTrack"];
354 - (IBAction)setUpcomingSongs:(id)sender
356 [self setKeyCombo:upcomingSongsCombo];
357 [self setHotKey:@"UpcomingSongs"];
360 - (void)setHotKey:(NSString *)key
363 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyEvent:) name:@"KeyBroadcasterEvent" object:nil];
364 [NSApp beginSheet:keyComboPanel modalForWindow:window modalDelegate:self didEndSelector:nil contextInfo:nil];
367 - (void)keyEvent:(NSNotification *)note
369 NSDictionary *info = [note userInfo];
374 keyCode = [[info objectForKey:@"KeyCode"] shortValue];
375 modifiers = [[info objectForKey:@"Modifiers"] longValue];
377 newCombo = [[KeyCombo alloc] initWithKeyCode:keyCode andModifiers:modifiers];
378 [self setKeyCombo:newCombo];
381 - (void)setKeyCombo:(KeyCombo *)newCombo
385 combo = [newCombo copy];
387 string = [combo userDisplayRep];
391 [keyComboField setStringValue:string];
396 // Table View Datasource Methods
400 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
402 if (aTableView == menuTableView) {
403 return [myItems count];
405 return [availableItems count];
409 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
411 if (aTableView == menuTableView) {
412 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
413 return [myItems objectAtIndex:rowIndex];
415 if ([submenuItems containsObject:[myItems objectAtIndex:rowIndex]])
417 return [NSImage imageNamed:@"submenu"];
423 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
424 return [availableItems objectAtIndex:rowIndex];
426 if ([submenuItems containsObject:[availableItems objectAtIndex:rowIndex]]) {
427 return [NSImage imageNamed:@"submenu"];
435 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
437 if (tableView == menuTableView) {
438 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
439 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
443 if (tableView == allTableView) {
444 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
445 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
451 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
455 NSString *dragData, *temp;
457 pb = [info draggingPasteboard];
459 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
460 dragData = [pb stringForType:@"MenuTableViewPboardType"];
461 dragRow = [dragData intValue];
462 temp = [myItems objectAtIndex:dragRow];
463 [myItems removeObjectAtIndex:dragRow];
465 if (tableView == menuTableView) {
467 [myItems insertObject:temp atIndex:row - 1];
469 [myItems insertObject:temp atIndex:row];
472 if (![temp isEqualToString:@"<separator>"]) {
473 [availableItems addObject:temp];
476 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
477 dragData = [pb stringForType:@"AllTableViewPboardType"];
478 dragRow = [dragData intValue];
479 temp = [availableItems objectAtIndex:dragRow];
481 if (![temp isEqualToString:@"<separator>"]) {
482 [availableItems removeObjectAtIndex:dragRow];
484 [myItems insertObject:temp atIndex:row];
487 [menuTableView reloadData];
488 [allTableView reloadData];
492 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
494 if (tableView == allTableView) {
495 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
496 return NSDragOperationNone;
499 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
500 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
501 if ([item isEqualToString:@"PreferencesÉ"] || [item isEqualToString:@"Quit"]) {
502 return NSDragOperationNone;
506 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
507 return NSDragOperationGeneric;
510 if (operation == NSTableViewDropOn || row == -1)
512 return NSDragOperationNone;
515 return NSDragOperationGeneric;