1 #import "PreferencesController.h"
2 #import "MainController.h"
3 #import "NetworkController.h"
4 #import "NetworkObject.h"
5 #import "StatusWindow.h"
6 #import "StatusWindowController.h"
7 #import "CustomMenuTableView.h"
11 #import <openssl/sha.h>
13 #import <ITFoundation/ITFoundation.h>
15 #import <ITKit/ITHotKeyCenter.h>
16 #import <ITKit/ITKeyCombo.h>
17 #import <ITKit/ITKeyComboPanel.h>
18 #import <ITKit/ITWindowPositioning.h>
19 #import <ITKit/ITKeyBroadcaster.h>
21 #import <ITKit/ITTSWBackgroundView.h>
22 #import <ITKit/ITWindowEffect.h>
23 #import <ITKit/ITCutWindowEffect.h>
24 #import <ITKit/ITDissolveWindowEffect.h>
25 #import <ITKit/ITSlideHorizontallyWindowEffect.h>
26 #import <ITKit/ITSlideVerticallyWindowEffect.h>
27 #import <ITKit/ITPivotWindowEffect.h>
30 #define SENDER_STATE (([sender state] == NSOnState) ? YES : NO)
32 /*************************************************************************/
34 #pragma mark PRIVATE INTERFACE
35 /*************************************************************************/
37 @interface PreferencesController (Private)
39 - (void)setupCustomizationTables;
40 - (void)setupMenuItems;
42 - (void)setStatusWindowEntryEffect:(Class)effectClass;
43 - (void)setStatusWindowExitEffect:(Class)effectClass;
44 - (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update;
45 - (void)repopulateEffectPopupsForVerticalPosition:(ITVerticalWindowPosition)vPos horizontalPosition:(ITHorizontalWindowPosition)hPos;
46 - (BOOL)effect:(Class)effectClass supportsVerticalPosition:(ITVerticalWindowPosition)vPos withHorizontalPosition:(ITHorizontalWindowPosition)hPos;
47 - (IBAction)changeMenus:(id)sender;
48 - (void)setLaunchesAtLogin:(BOOL)flag;
52 @implementation PreferencesController
55 /*************************************************************************/
57 #pragma mark STATIC VARIABLES
58 /*************************************************************************/
60 static PreferencesController *prefs = nil;
63 /*************************************************************************/
65 #pragma mark INITIALIZATION METHODS
66 /*************************************************************************/
68 + (PreferencesController *)sharedPrefs;
71 prefs = [[self alloc] init];
78 if ( (self = [super init]) ) {
79 ITDebugLog(@"Preferences initialized.");
80 df = [[NSUserDefaults standardUserDefaults] retain];
82 effectClasses = [[ITWindowEffect effectClasses] retain];
84 hotKeysArray = [[NSArray alloc] initWithObjects:@"PlayPause",
98 hotKeyNamesArray = [[NSArray alloc] initWithObjects:@"Play/Pause",
111 hotKeysDictionary = [[NSMutableDictionary alloc] init];
114 [self setupWindow]; // Load in the nib, and perform any initial setup.
115 [[NSColorPanel sharedColorPanel] setShowsAlpha:YES];
121 /*************************************************************************/
123 #pragma mark ACCESSOR METHODS
124 /*************************************************************************/
131 - (void)setController:(id)object
133 [controller autorelease];
134 controller = [object retain];
138 /*************************************************************************/
140 #pragma mark INSTANCE METHODS
141 /*************************************************************************/
143 - (BOOL)showPasswordPanel
145 [passwordPanel setLevel:NSStatusWindowLevel];
146 [passwordPanelOKButton setTitle:@"Connect"];
147 [passwordPanelTitle setStringValue:@"Password Required"];
148 [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"Please enter a password for access to the MenuTunes player named %@ at %@.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
149 [passwordPanel center];
150 [passwordPanel setLevel:NSStatusWindowLevel];
151 [passwordPanel makeKeyAndOrderFront:nil];
152 if ([NSApp runModalForWindow:passwordPanel]) {
159 - (BOOL)showInvalidPasswordPanel
161 [passwordPanel setLevel:NSStatusWindowLevel];
162 [passwordPanelOKButton setTitle:@"Retry"];
163 [passwordPanelTitle setStringValue:@"Invalid Password"];
164 [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"The password entered for access to the MenuTunes player named %@ at %@ is invalid. Please provide a new password.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
165 [passwordPanel center];
166 [passwordPanel setLevel:NSStatusWindowLevel];
167 [passwordPanel makeKeyAndOrderFront:nil];
168 if ([NSApp runModalForWindow:passwordPanel]) {
175 - (IBAction)showPrefsWindow:(id)sender
177 ITDebugLog(@"Showing preferences window.");
178 if (!myItems) { // If menu array does not exist yet, then the window hasn't been setup.
179 ITDebugLog(@"Window doesn't exist, initial setup.");
180 [self setupCustomizationTables]; // Setup the DnD manu config tables.
181 [self setupMenuItems]; // Setup the arrays of menu items
182 [self setupUI]; // Sets up additional UI
183 [window setDelegate:self];
184 [menuTableView reloadData];
185 [hotKeysTableView setDoubleAction:@selector(hotKeysTableViewDoubleClicked:)];
187 //Change the launch player checkbox to the proper name
189 [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
191 [controller networkError:localException];
196 [NSApp activateIgnoringOtherApps:YES];
197 [window performSelector:@selector(makeKeyAndOrderFront:) withObject:self afterDelay:0.0];
200 - (IBAction)showTestWindow:(id)sender
202 [controller showTestWindow];
205 - (IBAction)changeGeneralSetting:(id)sender
207 ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
208 if ( [sender tag] == 1010) {
209 [self setLaunchesAtLogin:SENDER_STATE];
210 } else if ( [sender tag] == 1020) {
211 [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
212 } else if ( [sender tag] == 1030) {
213 [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
215 } else if ( [sender tag] == 1040) {
216 // This will not be executed. Song info always shows the title of the song.
217 // [df setBool:SENDER_STATE forKey:@"showName"];
218 } else if ( [sender tag] == 1050) {
219 [df setBool:SENDER_STATE forKey:@"showArtist"];
220 } else if ( [sender tag] == 1060) {
221 [df setBool:SENDER_STATE forKey:@"showAlbum"];
222 } else if ( [sender tag] == 1070) {
223 [df setBool:SENDER_STATE forKey:@"showTime"];
224 } else if ( [sender tag] == 1080) {
225 [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
226 } else if ( [sender tag] == 1090) {
227 [df setBool:SENDER_STATE forKey:@"showTrackRating"];
232 - (IBAction)changeSharingSetting:(id)sender
234 ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
235 if ( [sender tag] == 5010 ) {
236 BOOL state = SENDER_STATE;
237 [df setBool:state forKey:@"enableSharing"];
238 //Disable/enable the use of shared player options
239 [useSharedMenuTunesCheckbox setEnabled:!state];
240 [usePasswordCheckbox setEnabled:state];
241 [passwordTextField setEnabled:state];
242 [nameTextField setEnabled:state];
243 [selectSharedPlayerButton setEnabled:NO];
244 [controller setServerStatus:state]; //Set server status
245 } else if ( [sender tag] == 5015 ) {
246 [df setObject:[sender stringValue] forKey:@"sharedPlayerName"];
247 } else if ( [sender tag] == 5020 ) {
248 [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
249 } else if ( [sender tag] == 5030 ) {
250 //Set the server password
251 const char *instring = [[sender stringValue] UTF8String];
252 const char *password = "password";
253 unsigned char *result;
254 NSData *hashedPass, *passwordStringHash;
255 result = SHA1(instring, strlen(instring), NULL);
256 hashedPass = [NSData dataWithBytes:result length:strlen(result)];
257 result = SHA1(password, strlen(password), NULL);
258 passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
259 if (![hashedPass isEqualToData:passwordStringHash]) {
260 [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
261 [sender setStringValue:@"password"];
263 } else if ( [sender tag] == 5040 ) {
264 BOOL state = SENDER_STATE;
265 [df setBool:state forKey:@"useSharedPlayer"];
266 //Disable/enable the use of sharing options
267 [shareMenuTunesCheckbox setEnabled:!state];
268 [usePasswordCheckbox setEnabled:NO];
269 [passwordTextField setEnabled:NO];
270 [nameTextField setEnabled:NO];
271 [selectSharedPlayerButton setEnabled:state];
273 if (state && ([controller connectToServer] == 1)) {
274 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
275 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
277 [selectedPlayerTextField setStringValue:@"No shared player selected."];
278 [locationTextField setStringValue:@"-"];
279 if ([[NetworkController sharedController] isConnectedToServer]) {
280 [controller disconnectFromServer];
284 } else if ( [sender tag] == 5050 ) {
285 //If no player is selected in the table view, turn off OK button.
286 if ([sender clickedRow] == -1 ) {
287 [sharingPanelOKButton setEnabled:NO];
289 [sharingPanelOKButton setEnabled:YES];
291 } else if ( [sender tag] == 5051 ) {
292 [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
293 } else if ( [sender tag] == 5060 ) {
294 //Set OK button state
295 if (([selectPlayerBox contentView] == zeroConfView && [sharingTableView selectedRow] == -1) ||
296 ([selectPlayerBox contentView] == manualView && [[hostTextField stringValue] length] == 0)) {
297 [sharingPanelOKButton setEnabled:NO];
299 [sharingPanelOKButton setEnabled:YES];
301 //Show selection sheet
302 [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
303 } else if ( [sender tag] == 5100 ) {
305 if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
306 NSRect frame = [selectPlayerSheet frame];
307 frame.origin.y -= 58;
308 frame.size.height = 273;
309 if ([sharingTableView selectedRow] == -1) {
310 [sharingPanelOKButton setEnabled:NO];
312 [selectPlayerBox setContentView:zeroConfView];
313 [selectPlayerSheet setFrame:frame display:YES animate:YES];
314 } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
315 NSRect frame = [selectPlayerSheet frame];
316 frame.origin.y += 58;
317 frame.size.height = 215;
318 if ([[hostTextField stringValue] length] == 0) {
319 [sharingPanelOKButton setEnabled:NO];
321 [sharingPanelOKButton setEnabled:YES];
323 [selectPlayerBox setContentView:manualView];
324 [selectPlayerSheet setFrame:frame display:YES animate:YES];
325 [hostTextField selectText:nil];
327 } else if ( [sender tag] == 5150 ) {
328 const char *instring = [[sender stringValue] UTF8String];
329 unsigned char *result;
330 result = SHA1(instring, strlen(instring), NULL);
331 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
332 } else if ( [sender tag] == 5110 ) {
334 [NSApp endSheet:selectPlayerSheet];
335 [selectPlayerSheet orderOut:nil];
336 if ([selectPlayerBox contentView] == manualView) {
337 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
340 } else if ( [sender tag] == 5120 ) {
342 [NSApp endSheet:selectPlayerSheet];
343 [selectPlayerSheet orderOut:nil];
345 [self changeSharingSetting:clientPasswordTextField];
347 if ([selectPlayerBox contentView] == manualView) {
348 [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
350 if ([sharingTableView selectedRow] > -1) {
351 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
355 if ([controller connectToServer] == 1) {
356 [useSharedMenuTunesCheckbox setState:NSOnState];
357 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
358 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
360 NSRunAlertPanel(@"Connection error.", @"The MenuTunes server you attempted to connect to was not responding. MenuTunes will revert back to the local player.", @"OK", nil, nil);
362 } else if ( [sender tag] == 6010 ) {
363 //Cancel password entry
364 [passwordPanel orderOut:nil];
365 [NSApp stopModalWithCode:0];
366 } else if ( [sender tag] == 6020 ) {
367 //OK password entry, retry connect
368 const char *instring = [[passwordPanelTextField stringValue] UTF8String];
369 unsigned char *result;
370 result = SHA1(instring, strlen(instring), NULL);
371 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
372 [passwordPanel orderOut:nil];
373 [NSApp stopModalWithCode:1];
378 - (IBAction)changeStatusWindowSetting:(id)sender
380 StatusWindow *sw = [StatusWindow sharedWindow];
381 ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
383 if ( [sender tag] == 2010) {
385 BOOL entryEffectValid = YES;
386 BOOL exitEffectValid = YES;
388 [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
389 [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
390 [sw setVerticalPosition:[sender selectedRow]];
391 [sw setHorizontalPosition:[sender selectedColumn]];
393 // Enable/disable the items in the popups.
394 [self repopulateEffectPopupsForVerticalPosition:[sw verticalPosition]
395 horizontalPosition:[sw horizontalPosition]];
397 // Make sure the effects support the new position.
398 entryEffectValid = ( [self effect:[[sw entryEffect] class]
399 supportsVerticalPosition:[sw verticalPosition]
400 withHorizontalPosition:[sw horizontalPosition]] );
401 exitEffectValid = ( [self effect:[[sw exitEffect] class]
402 supportsVerticalPosition:[sw verticalPosition]
403 withHorizontalPosition:[sw horizontalPosition]] );
405 if ( ! entryEffectValid ) {
406 [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITCutWindowEffect")]];
407 [self setStatusWindowEntryEffect:NSClassFromString(@"ITCutWindowEffect")];
409 [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:[[sw entryEffect] class]]];
412 if ( ! exitEffectValid ) {
413 [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITDissolveWindowEffect")]];
414 [self setStatusWindowExitEffect:NSClassFromString(@"ITDissolveWindowEffect")];
416 [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:[[sw exitEffect] class]]];
419 [(MainController *)controller showCurrentTrackInfo];
421 } else if ( [sender tag] == 2020) {
423 // Update screen selection.
425 } else if ( [sender tag] == 2030) {
427 [self setStatusWindowEntryEffect:[[sender selectedItem] representedObject]];
429 } else if ( [sender tag] == 2040) {
431 [self setStatusWindowExitEffect:[[sender selectedItem] representedObject]];
433 } else if ( [sender tag] == 2050) {
434 float newTime = ( -([sender floatValue]) );
435 [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
436 [[sw entryEffect] setEffectTime:newTime];
437 } else if ( [sender tag] == 2060) {
438 float newTime = ( -([sender floatValue]) );
439 [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
440 [[sw exitEffect] setEffectTime:newTime];
441 } else if ( [sender tag] == 2070) {
442 [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
443 [sw setExitDelay:[sender floatValue]];
444 } else if ( [sender tag] == 2080) {
445 [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
446 } else if ( [sender tag] == 2090) {
448 int setting = [sender indexOfSelectedItem];
450 if ( setting == 0 ) {
451 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundApple];
452 [backgroundColorWell setEnabled:NO];
453 [backgroundColorPopup setEnabled:NO];
454 } else if ( setting == 1 ) {
455 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundReadable];
456 [backgroundColorWell setEnabled:NO];
457 [backgroundColorPopup setEnabled:NO];
458 } else if ( setting == 2 ) {
459 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundColored];
460 [backgroundColorWell setEnabled:YES];
461 [backgroundColorPopup setEnabled:YES];
464 [df setInteger:setting forKey:@"statusWindowBackgroundMode"];
466 } else if ( [sender tag] == 2091) {
467 [self setCustomColor:[sender color] updateWell:NO];
468 } else if ( [sender tag] == 2092) {
470 int selectedItem = [sender indexOfSelectedItem];
472 if ( selectedItem == 1 ) { // An NSPopUpButton in PullDown mode uses item 0 as its title. Its first selectable item is 1.
473 [self setCustomColor:[NSColor colorWithCalibratedRed:0.92549 green:0.686275 blue:0.0 alpha:1.0] updateWell:YES];
474 } else if ( selectedItem == 2 ) {
475 [self setCustomColor:[NSColor colorWithCalibratedRed:0.380392 green:0.670588 blue:0.0 alpha:1.0] updateWell:YES];
476 } else if ( selectedItem == 3 ) {
477 [self setCustomColor:[NSColor colorWithCalibratedRed:0.443137 green:0.231373 blue:0.619608 alpha:1.0] updateWell:YES];
478 } else if ( selectedItem == 4 ) {
479 [self setCustomColor:[NSColor colorWithCalibratedRed:0.831373 green:0.12549 blue:0.509804 alpha:1.0] updateWell:YES];
480 } else if ( selectedItem == 5 ) {
481 [self setCustomColor:[NSColor colorWithCalibratedRed:0.00784314 green:0.611765 blue:0.662745 alpha:1.0] updateWell:YES];
483 [self setCustomColor:[NSColor colorWithCalibratedWhite:0.15 alpha:0.70] updateWell:YES];
486 } else if ( [sender tag] == 2095) {
487 [df setInteger:[sender indexOfSelectedItem] forKey:@"statusWindowSizing"];
488 [(MainController *)controller showCurrentTrackInfo];
494 - (void)registerDefaults
497 NSMutableDictionary *loginWindow;
498 NSMutableArray *loginArray;
499 NSEnumerator *loginEnum;
501 ITDebugLog(@"Registering defaults.");
502 [df setObject:[NSArray arrayWithObjects:
514 nil] forKey:@"menu"];
516 [df setInteger:5 forKey:@"SongsInAdvance"];
517 // [df setBool:YES forKey:@"showName"]; // Song info will always show song title.
518 [df setBool:YES forKey:@"showArtist"];
519 [df setBool:NO forKey:@"showAlbum"];
520 [df setBool:NO forKey:@"showTime"];
522 [df setObject:@"ITCutWindowEffect" forKey:@"statusWindowAppearanceEffect"];
523 [df setObject:@"ITDissolveWindowEffect" forKey:@"statusWindowVanishEffect"];
524 [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
525 [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
526 [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
527 [df setInteger:(int)ITWindowPositionBottom forKey:@"statusWindowVerticalPosition"];
528 [df setInteger:(int)ITWindowPositionLeft forKey:@"statusWindowHorizontalPosition"];
529 [df setBool:YES forKey:@"showSongInfoOnChange"];
531 [df setObject:[NSArchiver archivedDataWithRootObject:[NSColor blueColor]] forKey:@"statusWindowBackgroundColor"];
535 loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
536 loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
537 loginEnum = [loginArray objectEnumerator];
539 while ( (anItem = [loginEnum nextObject]) ) {
540 if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
544 [loginWindow release];
547 [[StatusWindowController sharedController] showSetupQueryWindow];
553 [[StatusWindow sharedWindow] setLocked:NO];
554 [[StatusWindow sharedWindow] vanish:self];
555 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
557 [self setLaunchesAtLogin:YES];
560 - (void)autoLaunchCancel
562 [[StatusWindow sharedWindow] setLocked:NO];
563 [[StatusWindow sharedWindow] vanish:self];
564 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
567 - (void)deletePressedInTableView:(NSTableView *)tableView
569 if (tableView == menuTableView) {
570 int selRow = [tableView selectedRow];
571 ITDebugLog(@"Delete pressed in menu table view.");
573 NSString *object = [myItems objectAtIndex:selRow];
575 if ([object isEqualToString:@"preferences"]) {
580 if (![object isEqualToString:@"separator"])
581 [availableItems addObject:object];
582 ITDebugLog(@"Removing object named %@", object);
583 [myItems removeObjectAtIndex:selRow];
584 [menuTableView reloadData];
585 [allTableView reloadData];
587 [self changeMenus:self];
591 - (void)resetRemotePlayerTextFields
593 if ([[NetworkController sharedController] isConnectedToServer]) {
594 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
595 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
597 [selectedPlayerTextField setStringValue:@"No shared player selected."];
598 [locationTextField setStringValue:@"-"];
602 /*************************************************************************/
604 #pragma mark HOTKEY SUPPORT METHODS
605 /*************************************************************************/
607 - (IBAction)clearHotKey:(id)sender
609 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
610 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
611 [controller setupHotKeys];
612 [hotKeysTableView reloadData];
615 - (IBAction)editHotKey:(id)sender
617 ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
618 NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
619 ITKeyCombo *keyCombo;
621 ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
622 [controller clearHotKeys];
623 [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
624 [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
625 if ([panel runModal] == NSOKButton) {
626 NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
628 keyCombo = [panel keyCombo];
630 //Check for duplicate key combo
631 while ( (nextKey = [keyEnumerator nextObject]) ) {
632 if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
633 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
634 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
636 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
641 [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
642 [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
643 [controller setupHotKeys];
644 [hotKeysTableView reloadData];
645 ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
647 ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
651 - (void)hotKeysTableViewDoubleClicked:(id)sender
653 if ([sender clickedRow] > -1) {
654 [self editHotKey:sender];
658 /*************************************************************************/
660 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
661 /*************************************************************************/
665 ITDebugLog(@"Loading Preferences.nib.");
666 if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
667 ITDebugLog(@"Failed to load Preferences.nib.");
673 - (void)setupCustomizationTables
675 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
676 ITDebugLog(@"Setting up table views.");
677 // Set the table view cells up
678 [imgCell setImageScaling:NSScaleNone];
679 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
680 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
682 // Register for drag and drop
683 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
684 @"MenuTableViewPboardType",
685 @"AllTableViewPboardType",
687 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
688 @"MenuTableViewPboardType",
689 @"AllTableViewPboardType",
693 - (void)setupMenuItems
695 NSEnumerator *itemEnum;
697 ITDebugLog(@"Setting up table view arrays.");
698 // Set the list of items you can have.
699 availableItems = [[NSMutableArray alloc] initWithObjects:
715 // Get our preferred menu
716 myItems = [[df arrayForKey:@"menu"] mutableCopy];
718 // Delete items in the availableItems array that are already part of the menu
719 itemEnum = [myItems objectEnumerator];
720 while ( (anItem = [itemEnum nextObject]) ) {
721 if (![anItem isEqualToString:@"separator"]) {
722 [availableItems removeObject:anItem];
726 // Items that show should a submenu image
727 submenuItems = [[NSArray alloc] initWithObjects:
737 NSMutableDictionary *loginwindow;
738 NSMutableArray *loginarray;
739 NSEnumerator *loginEnum;
740 NSEnumerator *keyArrayEnum;
741 NSString *serverName;
746 ITDebugLog(@"Setting up preferences UI.");
747 // Fill in the number of songs in advance to show field
748 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
750 // Fill hot key array
751 keyArrayEnum = [hotKeysArray objectEnumerator];
753 while ( (anItem = [keyArrayEnum nextObject]) ) {
754 if ([df objectForKey:anItem]) {
755 ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
756 [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
758 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
762 ITDebugLog(@"Setting up track info checkboxes.");
763 // Check current track info buttons
764 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
765 [nameCheckbox setState:NSOnState]; // Song info will ALWAYS show song title.
766 [nameCheckbox setEnabled:NO]; // Song info will ALWAYS show song title.
767 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
768 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
769 [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
770 [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
772 // Set the launch at login checkbox state
773 ITDebugLog(@"Setting launch at login state.");
775 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
776 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
778 loginEnum = [loginarray objectEnumerator];
779 while ( (anItem = [loginEnum nextObject]) ) {
780 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
781 [launchAtLoginCheckbox setState:NSOnState];
785 // Set the launch player checkbox state
786 ITDebugLog(@"Setting launch player with MenuTunes state.");
787 [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
789 // Setup the positioning controls
790 [positionMatrix selectCellAtRow:[df integerForKey:@"statusWindowVerticalPosition"]
791 column:[df integerForKey:@"statusWindowHorizontalPosition"]];
793 // Setup effects controls
794 // Populate the effects popups
795 [appearanceEffectPopup setAutoenablesItems:NO];
796 [vanishEffectPopup setAutoenablesItems:NO];
797 [self repopulateEffectPopupsForVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"]
798 horizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
800 // Attempt to find the pref'd effect in the list.
801 // If it's not there, use cut/dissolve.
802 if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])] ) {
803 [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])]];
805 [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
808 if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])] ) {
809 [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])]];
811 [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
814 [appearanceSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowAppearanceSpeed"]) )];
815 [vanishSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowVanishSpeed"]) )];
816 [vanishDelaySlider setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
818 // Setup General Controls
819 selectedBGStyle = [df integerForKey:@"statusWindowBackgroundMode"];
820 [backgroundStylePopup selectItem:[backgroundStylePopup itemAtIndex:[backgroundStylePopup indexOfItemWithTag:selectedBGStyle]]];
822 if ( selectedBGStyle == ITTSWBackgroundColored ) {
823 [backgroundColorWell setEnabled:YES];
824 [backgroundColorPopup setEnabled:YES];
826 [backgroundColorWell setEnabled:NO];
827 [backgroundColorPopup setEnabled:NO];
830 colorData = [df dataForKey:@"statusWindowBackgroundColor"];
833 [backgroundColorWell setColor:(NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
835 [backgroundColorWell setColor:[NSColor blueColor]];
838 [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
840 [windowSizingPopup selectItem:[windowSizingPopup itemAtIndex:[windowSizingPopup indexOfItemWithTag:[df integerForKey:@"statusWindowSizing"]]]];
842 // Setup the sharing controls
843 if ([df boolForKey:@"enableSharing"]) {
844 [shareMenuTunesCheckbox setState:NSOnState];
845 [useSharedMenuTunesCheckbox setEnabled:NO];
846 [selectSharedPlayerButton setEnabled:NO];
847 [passwordTextField setEnabled:YES];
848 [usePasswordCheckbox setEnabled:YES];
849 [nameTextField setEnabled:YES];
850 } else if ([df boolForKey:@"useSharedPlayer"]) {
851 [useSharedMenuTunesCheckbox setState:NSOnState];
852 [shareMenuTunesCheckbox setEnabled:NO];
853 [selectSharedPlayerButton setEnabled:YES];
856 [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
858 serverName = [df stringForKey:@"sharedPlayerName"];
859 if (!serverName || [serverName length] == 0) {
860 serverName = @"MenuTunes Shared Player";
862 [nameTextField setStringValue:serverName];
864 [selectPlayerBox setContentView:zeroConfView];
865 [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
866 if ([df dataForKey:@"sharedPlayerPassword"]) {
867 [passwordTextField setStringValue:@"password"];
869 if ([df stringForKey:@"sharedPlayerHost"]) {
870 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
873 if ([[NetworkController sharedController] isConnectedToServer]) {
874 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
875 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
877 [selectedPlayerTextField setStringValue:@"No shared player selected."];
878 [locationTextField setStringValue:@"-"];
882 - (void)setStatusWindowEntryEffect:(Class)effectClass
884 StatusWindow *sw = [StatusWindow sharedWindow];
886 float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
887 [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowAppearanceEffect"];
889 [sw setEntryEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
890 [[sw entryEffect] setEffectTime:time];
893 - (void)setStatusWindowExitEffect:(Class)effectClass
895 StatusWindow *sw = [StatusWindow sharedWindow];
897 float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
898 [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowVanishEffect"];
900 [sw setExitEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
901 [[sw exitEffect] setEffectTime:time];
904 - (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update
906 [(ITTSWBackgroundView *)[[StatusWindow sharedWindow] contentView] setBackgroundColor:color];
907 [df setObject:[NSArchiver archivedDataWithRootObject:color] forKey:@"statusWindowBackgroundColor"];
910 [backgroundColorWell setColor:color];
914 - (void)repopulateEffectPopupsForVerticalPosition:(ITVerticalWindowPosition)vPos horizontalPosition:(ITHorizontalWindowPosition)hPos
916 NSEnumerator *effectEnum = [effectClasses objectEnumerator];
919 [appearanceEffectPopup removeAllItems];
920 [vanishEffectPopup removeAllItems];
922 while ( (anItem = [effectEnum nextObject]) ) {
923 [appearanceEffectPopup addItemWithTitle:[anItem effectName]];
924 [vanishEffectPopup addItemWithTitle:[anItem effectName]];
926 [[appearanceEffectPopup lastItem] setRepresentedObject:anItem];
927 [[vanishEffectPopup lastItem] setRepresentedObject:anItem];
929 if ( [self effect:anItem supportsVerticalPosition:vPos withHorizontalPosition:hPos] ) {
930 [[appearanceEffectPopup lastItem] setEnabled:YES];
931 [[vanishEffectPopup lastItem] setEnabled:YES];
933 [[appearanceEffectPopup lastItem] setEnabled:NO];
934 [[vanishEffectPopup lastItem] setEnabled:NO];
940 - (BOOL)effect:(Class)effectClass supportsVerticalPosition:(ITVerticalWindowPosition)vPos withHorizontalPosition:(ITHorizontalWindowPosition)hPos
944 if ( vPos == ITWindowPositionTop ) {
945 if ( hPos == ITWindowPositionLeft ) {
946 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Left"] boolValue] ) ;
947 } else if ( hPos == ITWindowPositionCenter ) {
948 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Center"] boolValue] );
949 } else if ( hPos == ITWindowPositionRight ) {
950 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Right"] boolValue] );
952 } else if ( vPos == ITWindowPositionMiddle ) {
953 if ( hPos == ITWindowPositionLeft ) {
954 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Left"] boolValue] );
955 } else if ( hPos == ITWindowPositionCenter ) {
956 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Center"] boolValue] );
957 } else if ( hPos == ITWindowPositionRight ) {
958 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Right"] boolValue] );
960 } else if ( vPos == ITWindowPositionBottom ) {
961 if ( hPos == ITWindowPositionLeft ) {
962 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Left"] boolValue] );
963 } else if ( hPos == ITWindowPositionCenter ) {
964 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Center"] boolValue] );
965 } else if ( hPos == ITWindowPositionRight ) {
966 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Right"] boolValue] );
973 - (IBAction)changeMenus:(id)sender
975 ITDebugLog(@"Synchronizing menus");
976 [df setObject:myItems forKey:@"menu"];
980 - (void)setLaunchesAtLogin:(BOOL)flag
982 NSMutableDictionary *loginwindow;
983 NSMutableArray *loginarray;
984 ITDebugLog(@"Setting launches at login: %i", flag);
986 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
987 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
990 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
991 [[NSBundle mainBundle] bundlePath], @"Path",
992 [NSNumber numberWithInt:0], @"Hide", nil];
993 [loginarray addObject:itemDict];
996 for (i = 0; i < [loginarray count]; i++) {
997 NSDictionary *tempDict = [loginarray objectAtIndex:i];
998 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
999 [loginarray removeObjectAtIndex:i];
1004 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
1006 [loginwindow release];
1007 ITDebugLog(@"Finished setting launches at login.");
1011 /*************************************************************************/
1013 #pragma mark NSWindow DELEGATE METHODS
1014 /*************************************************************************/
1016 - (void)windowWillClose:(NSNotification *)note
1018 [(MainController *)controller closePreferences];
1021 /*************************************************************************/
1023 #pragma mark NSTextField DELEGATE METHODS
1024 /*************************************************************************/
1026 - (void)controlTextDidChange:(NSNotification*)note
1028 if ([note object] == hostTextField) {
1029 if ([[hostTextField stringValue] length] == 0) {
1030 [sharingPanelOKButton setEnabled:NO];
1032 [sharingPanelOKButton setEnabled:YES];
1037 /*************************************************************************/
1039 #pragma mark NSTableView DATASOURCE METHODS
1040 /*************************************************************************/
1042 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1044 if (aTableView == menuTableView) {
1045 return [myItems count];
1046 } else if (aTableView == allTableView) {
1047 return [availableItems count];
1048 } else if (aTableView == hotKeysTableView) {
1049 return [hotKeysArray count];
1051 return [[[NetworkController sharedController] remoteServices] count];
1055 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
1057 if (aTableView == menuTableView) {
1058 NSString *object = [myItems objectAtIndex:rowIndex];
1059 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1060 if ([object isEqualToString:@"showPlayer"]) {
1061 NSString *string = nil;
1063 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1065 [controller networkError:localException];
1069 return NSLocalizedString(object, @"ERROR");
1071 if ([submenuItems containsObject:object])
1073 return [NSImage imageNamed:@"submenu"];
1078 } else if (aTableView == allTableView) {
1079 NSString *object = [availableItems objectAtIndex:rowIndex];
1080 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1081 if ([object isEqualToString:@"showPlayer"]) {
1082 NSString *string = nil;
1084 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1086 [controller networkError:localException];
1090 return NSLocalizedString(object, @"ERROR");
1092 if ([submenuItems containsObject:object]) {
1093 return [NSImage imageNamed:@"submenu"];
1098 } else if (aTableView == hotKeysTableView) {
1099 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1100 return [hotKeyNamesArray objectAtIndex:rowIndex];
1102 return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
1105 return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
1109 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
1111 if (tableView == menuTableView) {
1112 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
1113 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
1117 if (tableView == allTableView) {
1118 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
1119 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
1125 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
1129 NSString *dragData, *temp;
1131 pb = [info draggingPasteboard];
1133 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
1134 dragData = [pb stringForType:@"MenuTableViewPboardType"];
1135 dragRow = [dragData intValue];
1136 temp = [myItems objectAtIndex:dragRow];
1138 if (tableView == menuTableView) {
1139 [myItems insertObject:temp atIndex:row];
1140 if (row > dragRow) {
1141 [myItems removeObjectAtIndex:dragRow];
1143 [myItems removeObjectAtIndex:dragRow + 1];
1145 } else if (tableView == allTableView) {
1146 if (![temp isEqualToString:@"separator"]) {
1147 [availableItems addObject:temp];
1149 [myItems removeObjectAtIndex:dragRow];
1151 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
1152 dragData = [pb stringForType:@"AllTableViewPboardType"];
1153 dragRow = [dragData intValue];
1154 temp = [availableItems objectAtIndex:dragRow];
1156 [myItems insertObject:temp atIndex:row];
1158 if (![temp isEqualToString:@"separator"]) {
1159 [availableItems removeObjectAtIndex:dragRow];
1163 [menuTableView reloadData];
1164 [allTableView reloadData];
1165 [self changeMenus:self];
1169 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
1171 if (tableView == allTableView) {
1172 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
1173 return NSDragOperationNone;
1176 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
1177 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
1178 if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
1179 return NSDragOperationNone;
1183 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
1184 return NSDragOperationGeneric;
1187 if (operation == NSTableViewDropOn || row == -1)
1189 return NSDragOperationNone;
1191 return NSDragOperationGeneric;
1195 /*************************************************************************/
1197 #pragma mark DEALLOCATION METHODS
1198 /*************************************************************************/
1202 [hotKeysArray release];
1203 [hotKeysDictionary release];
1204 [effectClasses release];
1205 [menuTableView setDataSource:nil];
1206 [allTableView setDataSource:nil];
1207 [controller release];
1208 [availableItems release];
1209 [submenuItems release];