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];
195 [self resetRemotePlayerTextFields];
197 [NSApp activateIgnoringOtherApps:YES];
198 [window performSelector:@selector(makeKeyAndOrderFront:) withObject:self afterDelay:0.0];
201 - (IBAction)showTestWindow:(id)sender
203 [controller showTestWindow];
206 - (IBAction)changeGeneralSetting:(id)sender
208 ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
209 if ( [sender tag] == 1010) {
210 [self setLaunchesAtLogin:SENDER_STATE];
211 } else if ( [sender tag] == 1020) {
212 [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
213 } else if ( [sender tag] == 1030) {
214 [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
216 } else if ( [sender tag] == 1040) {
217 // This will not be executed. Song info always shows the title of the song.
218 // [df setBool:SENDER_STATE forKey:@"showName"];
219 } else if ( [sender tag] == 1050) {
220 [df setBool:SENDER_STATE forKey:@"showArtist"];
221 } else if ( [sender tag] == 1060) {
222 [df setBool:SENDER_STATE forKey:@"showAlbum"];
223 } else if ( [sender tag] == 1070) {
224 [df setBool:SENDER_STATE forKey:@"showTime"];
225 } else if ( [sender tag] == 1080) {
226 [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
227 } else if ( [sender tag] == 1090) {
228 [df setBool:SENDER_STATE forKey:@"showTrackRating"];
233 - (IBAction)changeSharingSetting:(id)sender
235 ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
236 if ( [sender tag] == 5010 ) {
237 BOOL state = SENDER_STATE;
238 [df setBool:state forKey:@"enableSharing"];
239 //Disable/enable the use of shared player options
240 [useSharedMenuTunesCheckbox 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 [[NetworkController sharedController] resetServerName];
248 } else if ( [sender tag] == 5030 ) {
249 //Set the server password
250 const char *instring = [[sender stringValue] UTF8String];
251 const char *password = "p4s5w0rdMT1.2";
252 unsigned char *result;
253 NSData *hashedPass, *passwordStringHash;
254 if ([[sender stringValue] length] == 0) {
255 [df setObject:[NSData data] forKey:@"sharedPlayerPassword"];
258 result = SHA1(instring, strlen(instring), NULL);
259 hashedPass = [NSData dataWithBytes:result length:strlen(result)];
260 result = SHA1(password, strlen(password), NULL);
261 passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
262 if (![hashedPass isEqualToData:passwordStringHash]) {
263 [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
264 [sender setStringValue:@"p4s5w0rdMT1.2"];
266 } else if ( [sender tag] == 5040 ) {
267 BOOL state = SENDER_STATE;
268 [df setBool:state forKey:@"useSharedPlayer"];
269 //Disable/enable the use of sharing options
270 [shareMenuTunesCheckbox setEnabled:!state];
271 [passwordTextField setEnabled:NO];
272 [nameTextField setEnabled:NO];
273 [selectSharedPlayerButton setEnabled:state];
275 if (state && ([controller connectToServer] == 1)) {
276 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
277 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
279 [selectedPlayerTextField setStringValue:@"No shared player selected."];
280 [locationTextField setStringValue:@"-"];
281 if ([[NetworkController sharedController] isConnectedToServer]) {
282 [controller disconnectFromServer];
286 } else if ( [sender tag] == 5050 ) {
287 //If no player is selected in the table view, turn off OK button.
288 if ([sender clickedRow] == -1 ) {
289 [sharingPanelOKButton setEnabled:NO];
291 [sharingPanelOKButton setEnabled:YES];
293 } else if ( [sender tag] == 5051 ) {
294 [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
295 } else if ( [sender tag] == 5060 ) {
296 //Set OK button state
297 if (([selectPlayerBox contentView] == zeroConfView && [sharingTableView selectedRow] == -1) ||
298 ([selectPlayerBox contentView] == manualView && [[hostTextField stringValue] length] == 0)) {
299 [sharingPanelOKButton setEnabled:NO];
301 [sharingPanelOKButton setEnabled:YES];
303 //Show selection sheet
304 [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
305 } else if ( [sender tag] == 5100 ) {
307 if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
308 NSRect frame = [selectPlayerSheet frame];
309 frame.origin.y -= 58;
310 frame.size.height = 273;
311 if ([sharingTableView selectedRow] == -1) {
312 [sharingPanelOKButton setEnabled:NO];
314 [selectPlayerBox setContentView:zeroConfView];
315 [selectPlayerSheet setFrame:frame display:YES animate:YES];
316 } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
317 NSRect frame = [selectPlayerSheet frame];
318 frame.origin.y += 58;
319 frame.size.height = 215;
320 if ([[hostTextField stringValue] length] == 0) {
321 [sharingPanelOKButton setEnabled:NO];
323 [sharingPanelOKButton setEnabled:YES];
325 [selectPlayerBox setContentView:manualView];
326 [selectPlayerSheet setFrame:frame display:YES animate:YES];
327 [hostTextField selectText:nil];
329 } else if ( [sender tag] == 5150 ) {
330 const char *instring = [[sender stringValue] UTF8String];
331 unsigned char *result;
332 result = SHA1(instring, strlen(instring), NULL);
333 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
334 } else if ( [sender tag] == 5110 ) {
336 [NSApp endSheet:selectPlayerSheet];
337 [selectPlayerSheet orderOut:nil];
338 if ([selectPlayerBox contentView] == manualView) {
339 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
342 } else if ( [sender tag] == 5120 ) {
344 [NSApp endSheet:selectPlayerSheet];
345 [selectPlayerSheet orderOut:nil];
347 [self changeSharingSetting:clientPasswordTextField];
349 if ([selectPlayerBox contentView] == manualView) {
350 [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
352 if ([sharingTableView selectedRow] > -1) {
353 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
357 if ([controller connectToServer] == 1) {
358 [useSharedMenuTunesCheckbox setState:NSOnState];
359 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
360 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
362 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);
364 } else if ( [sender tag] == 6010 ) {
365 //Cancel password entry
366 [passwordPanel orderOut:nil];
367 [NSApp stopModalWithCode:0];
368 } else if ( [sender tag] == 6020 ) {
369 //OK password entry, retry connect
370 const char *instring = [[passwordPanelTextField stringValue] UTF8String];
371 unsigned char *result;
372 result = SHA1(instring, strlen(instring), NULL);
373 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
374 [passwordPanel orderOut:nil];
375 [NSApp stopModalWithCode:1];
380 - (IBAction)changeStatusWindowSetting:(id)sender
382 StatusWindow *sw = [StatusWindow sharedWindow];
383 ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
385 if ( [sender tag] == 2010) {
387 BOOL entryEffectValid = YES;
388 BOOL exitEffectValid = YES;
390 [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
391 [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
392 [sw setVerticalPosition:[sender selectedRow]];
393 [sw setHorizontalPosition:[sender selectedColumn]];
395 // Enable/disable the items in the popups.
396 [self repopulateEffectPopupsForVerticalPosition:[sw verticalPosition]
397 horizontalPosition:[sw horizontalPosition]];
399 // Make sure the effects support the new position.
400 entryEffectValid = ( [self effect:[[sw entryEffect] class]
401 supportsVerticalPosition:[sw verticalPosition]
402 withHorizontalPosition:[sw horizontalPosition]] );
403 exitEffectValid = ( [self effect:[[sw exitEffect] class]
404 supportsVerticalPosition:[sw verticalPosition]
405 withHorizontalPosition:[sw horizontalPosition]] );
407 if ( ! entryEffectValid ) {
408 [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITCutWindowEffect")]];
409 [self setStatusWindowEntryEffect:NSClassFromString(@"ITCutWindowEffect")];
411 [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:[[sw entryEffect] class]]];
414 if ( ! exitEffectValid ) {
415 [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITDissolveWindowEffect")]];
416 [self setStatusWindowExitEffect:NSClassFromString(@"ITDissolveWindowEffect")];
418 [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:[[sw exitEffect] class]]];
421 [(MainController *)controller showCurrentTrackInfo];
423 } else if ( [sender tag] == 2020) {
425 // Update screen selection.
427 } else if ( [sender tag] == 2030) {
429 [self setStatusWindowEntryEffect:[[sender selectedItem] representedObject]];
431 } else if ( [sender tag] == 2040) {
433 [self setStatusWindowExitEffect:[[sender selectedItem] representedObject]];
435 } else if ( [sender tag] == 2050) {
436 float newTime = ( -([sender floatValue]) );
437 [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
438 [[sw entryEffect] setEffectTime:newTime];
439 } else if ( [sender tag] == 2060) {
440 float newTime = ( -([sender floatValue]) );
441 [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
442 [[sw exitEffect] setEffectTime:newTime];
443 } else if ( [sender tag] == 2070) {
444 [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
445 [sw setExitDelay:[sender floatValue]];
446 } else if ( [sender tag] == 2080) {
447 [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
448 } else if ( [sender tag] == 2090) {
450 int setting = [sender indexOfSelectedItem];
452 if ( setting == 0 ) {
453 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundApple];
454 [backgroundColorWell setEnabled:NO];
455 [backgroundColorPopup setEnabled:NO];
456 } else if ( setting == 1 ) {
457 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundReadable];
458 [backgroundColorWell setEnabled:NO];
459 [backgroundColorPopup setEnabled:NO];
460 } else if ( setting == 2 ) {
461 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundColored];
462 [backgroundColorWell setEnabled:YES];
463 [backgroundColorPopup setEnabled:YES];
466 [df setInteger:setting forKey:@"statusWindowBackgroundMode"];
468 } else if ( [sender tag] == 2091) {
469 [self setCustomColor:[sender color] updateWell:NO];
470 } else if ( [sender tag] == 2092) {
472 int selectedItem = [sender indexOfSelectedItem];
474 if ( selectedItem == 1 ) { // An NSPopUpButton in PullDown mode uses item 0 as its title. Its first selectable item is 1.
475 [self setCustomColor:[NSColor colorWithCalibratedRed:0.92549 green:0.686275 blue:0.0 alpha:1.0] updateWell:YES];
476 } else if ( selectedItem == 2 ) {
477 [self setCustomColor:[NSColor colorWithCalibratedRed:0.380392 green:0.670588 blue:0.0 alpha:1.0] updateWell:YES];
478 } else if ( selectedItem == 3 ) {
479 [self setCustomColor:[NSColor colorWithCalibratedRed:0.443137 green:0.231373 blue:0.619608 alpha:1.0] updateWell:YES];
480 } else if ( selectedItem == 4 ) {
481 [self setCustomColor:[NSColor colorWithCalibratedRed:0.831373 green:0.12549 blue:0.509804 alpha:1.0] updateWell:YES];
482 } else if ( selectedItem == 5 ) {
483 [self setCustomColor:[NSColor colorWithCalibratedRed:0.00784314 green:0.611765 blue:0.662745 alpha:1.0] updateWell:YES];
485 [self setCustomColor:[NSColor colorWithCalibratedWhite:0.15 alpha:0.70] updateWell:YES];
488 } else if ( [sender tag] == 2095) {
489 [df setInteger:[sender indexOfSelectedItem] forKey:@"statusWindowSizing"];
490 [(MainController *)controller showCurrentTrackInfo];
496 - (void)registerDefaults
499 NSMutableDictionary *loginWindow;
500 NSMutableArray *loginArray;
501 NSEnumerator *loginEnum;
503 ITDebugLog(@"Registering defaults.");
504 [df setObject:[NSArray arrayWithObjects:
516 nil] forKey:@"menu"];
518 [df setInteger:5 forKey:@"SongsInAdvance"];
519 // [df setBool:YES forKey:@"showName"]; // Song info will always show song title.
520 [df setBool:YES forKey:@"showArtist"];
521 [df setBool:NO forKey:@"showAlbum"];
522 [df setBool:NO forKey:@"showTime"];
524 [df setObject:@"ITCutWindowEffect" forKey:@"statusWindowAppearanceEffect"];
525 [df setObject:@"ITDissolveWindowEffect" forKey:@"statusWindowVanishEffect"];
526 [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
527 [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
528 [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
529 [df setInteger:(int)ITWindowPositionBottom forKey:@"statusWindowVerticalPosition"];
530 [df setInteger:(int)ITWindowPositionLeft forKey:@"statusWindowHorizontalPosition"];
531 [df setBool:YES forKey:@"showSongInfoOnChange"];
533 [df setObject:[NSArchiver archivedDataWithRootObject:[NSColor blueColor]] forKey:@"statusWindowBackgroundColor"];
537 loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
538 loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
539 loginEnum = [loginArray objectEnumerator];
541 while ( (anItem = [loginEnum nextObject]) ) {
542 if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
546 [loginWindow release];
549 [[StatusWindowController sharedController] showSetupQueryWindow];
555 [[StatusWindow sharedWindow] setLocked:NO];
556 [[StatusWindow sharedWindow] vanish:self];
557 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
559 [self setLaunchesAtLogin:YES];
562 - (void)autoLaunchCancel
564 [[StatusWindow sharedWindow] setLocked:NO];
565 [[StatusWindow sharedWindow] vanish:self];
566 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
569 - (void)deletePressedInTableView:(NSTableView *)tableView
571 if (tableView == menuTableView) {
572 int selRow = [tableView selectedRow];
573 ITDebugLog(@"Delete pressed in menu table view.");
575 NSString *object = [myItems objectAtIndex:selRow];
577 if ([object isEqualToString:@"preferences"]) {
582 if (![object isEqualToString:@"separator"])
583 [availableItems addObject:object];
584 ITDebugLog(@"Removing object named %@", object);
585 [myItems removeObjectAtIndex:selRow];
586 [menuTableView reloadData];
587 [allTableView reloadData];
589 [self changeMenus:self];
593 - (void)resetRemotePlayerTextFields
595 if ([[NetworkController sharedController] isConnectedToServer]) {
596 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
597 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
599 [selectedPlayerTextField setStringValue:@"No shared player selected."];
600 [locationTextField setStringValue:@"-"];
604 /*************************************************************************/
606 #pragma mark HOTKEY SUPPORT METHODS
607 /*************************************************************************/
609 - (IBAction)clearHotKey:(id)sender
611 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
612 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
613 [controller setupHotKeys];
614 [hotKeysTableView reloadData];
617 - (IBAction)editHotKey:(id)sender
619 ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
620 NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
621 ITKeyCombo *keyCombo;
623 ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
624 [controller clearHotKeys];
625 [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
626 [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
627 if ([panel runModal] == NSOKButton) {
628 NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
630 keyCombo = [panel keyCombo];
632 //Check for duplicate key combo
633 while ( (nextKey = [keyEnumerator nextObject]) ) {
634 if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
635 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
636 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
638 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
643 [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
644 [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
645 [controller setupHotKeys];
646 [hotKeysTableView reloadData];
647 ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
649 ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
653 - (void)hotKeysTableViewDoubleClicked:(id)sender
655 if ([sender clickedRow] > -1) {
656 [self editHotKey:sender];
660 /*************************************************************************/
662 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
663 /*************************************************************************/
667 ITDebugLog(@"Loading Preferences.nib.");
668 if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
669 ITDebugLog(@"Failed to load Preferences.nib.");
675 - (void)setupCustomizationTables
677 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
678 ITDebugLog(@"Setting up table views.");
679 // Set the table view cells up
680 [imgCell setImageScaling:NSScaleNone];
681 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
682 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
684 // Register for drag and drop
685 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
686 @"MenuTableViewPboardType",
687 @"AllTableViewPboardType",
689 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
690 @"MenuTableViewPboardType",
691 @"AllTableViewPboardType",
695 - (void)setupMenuItems
697 NSEnumerator *itemEnum;
699 ITDebugLog(@"Setting up table view arrays.");
700 // Set the list of items you can have.
701 availableItems = [[NSMutableArray alloc] initWithObjects:
717 // Get our preferred menu
718 myItems = [[df arrayForKey:@"menu"] mutableCopy];
720 // Delete items in the availableItems array that are already part of the menu
721 itemEnum = [myItems objectEnumerator];
722 while ( (anItem = [itemEnum nextObject]) ) {
723 if (![anItem isEqualToString:@"separator"]) {
724 [availableItems removeObject:anItem];
728 // Items that show should a submenu image
729 submenuItems = [[NSArray alloc] initWithObjects:
739 NSMutableDictionary *loginwindow;
740 NSMutableArray *loginarray;
741 NSEnumerator *loginEnum;
742 NSEnumerator *keyArrayEnum;
743 NSString *serverName;
748 ITDebugLog(@"Setting up preferences UI.");
749 // Fill in the number of songs in advance to show field
750 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
752 // Fill hot key array
753 keyArrayEnum = [hotKeysArray objectEnumerator];
755 while ( (anItem = [keyArrayEnum nextObject]) ) {
756 if ([df objectForKey:anItem]) {
757 ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
758 [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
760 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
764 ITDebugLog(@"Setting up track info checkboxes.");
765 // Check current track info buttons
766 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
767 [nameCheckbox setState:NSOnState]; // Song info will ALWAYS show song title.
768 [nameCheckbox setEnabled:NO]; // Song info will ALWAYS show song title.
769 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
770 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
771 [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
772 [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
774 // Set the launch at login checkbox state
775 ITDebugLog(@"Setting launch at login state.");
777 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
778 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
780 loginEnum = [loginarray objectEnumerator];
781 while ( (anItem = [loginEnum nextObject]) ) {
782 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
783 [launchAtLoginCheckbox setState:NSOnState];
787 // Set the launch player checkbox state
788 ITDebugLog(@"Setting launch player with MenuTunes state.");
789 [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
791 // Setup the positioning controls
792 [positionMatrix selectCellAtRow:[df integerForKey:@"statusWindowVerticalPosition"]
793 column:[df integerForKey:@"statusWindowHorizontalPosition"]];
795 // Setup effects controls
796 // Populate the effects popups
797 [appearanceEffectPopup setAutoenablesItems:NO];
798 [vanishEffectPopup setAutoenablesItems:NO];
799 [self repopulateEffectPopupsForVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"]
800 horizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
802 // Attempt to find the pref'd effect in the list.
803 // If it's not there, use cut/dissolve.
804 if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])] ) {
805 [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])]];
807 [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
810 if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])] ) {
811 [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])]];
813 [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
816 [appearanceSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowAppearanceSpeed"]) )];
817 [vanishSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowVanishSpeed"]) )];
818 [vanishDelaySlider setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
820 // Setup General Controls
821 selectedBGStyle = [df integerForKey:@"statusWindowBackgroundMode"];
822 [backgroundStylePopup selectItem:[backgroundStylePopup itemAtIndex:[backgroundStylePopup indexOfItemWithTag:selectedBGStyle]]];
824 if ( selectedBGStyle == ITTSWBackgroundColored ) {
825 [backgroundColorWell setEnabled:YES];
826 [backgroundColorPopup setEnabled:YES];
828 [backgroundColorWell setEnabled:NO];
829 [backgroundColorPopup setEnabled:NO];
832 colorData = [df dataForKey:@"statusWindowBackgroundColor"];
835 [backgroundColorWell setColor:(NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
837 [backgroundColorWell setColor:[NSColor blueColor]];
840 [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
842 [windowSizingPopup selectItem:[windowSizingPopup itemAtIndex:[windowSizingPopup indexOfItemWithTag:[df integerForKey:@"statusWindowSizing"]]]];
844 // Setup the sharing controls
845 if ([df boolForKey:@"enableSharing"]) {
846 [shareMenuTunesCheckbox setState:NSOnState];
847 [useSharedMenuTunesCheckbox setEnabled:NO];
848 [selectSharedPlayerButton setEnabled:NO];
849 [passwordTextField setEnabled:YES];
850 [nameTextField setEnabled:YES];
851 } else if ([df boolForKey:@"useSharedPlayer"]) {
852 [useSharedMenuTunesCheckbox setState:NSOnState];
853 [shareMenuTunesCheckbox setEnabled:NO];
854 [selectSharedPlayerButton setEnabled:YES];
857 [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
859 serverName = [df stringForKey:@"sharedPlayerName"];
860 if (!serverName || [serverName length] == 0) {
861 serverName = @"MenuTunes Shared Player";
863 [nameTextField setStringValue:serverName];
865 [selectPlayerBox setContentView:zeroConfView];
866 if ([[df dataForKey:@"sharedPlayerPassword"] length]) {
867 [passwordTextField setStringValue:@"p4s5w0rdMT1.2"];
869 [passwordTextField setStringValue:@""];
871 if ([df stringForKey:@"sharedPlayerHost"]) {
872 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
875 if ([[NetworkController sharedController] isConnectedToServer]) {
876 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
877 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
879 [selectedPlayerTextField setStringValue:@"No shared player selected."];
880 [locationTextField setStringValue:@"-"];
884 - (void)setStatusWindowEntryEffect:(Class)effectClass
886 StatusWindow *sw = [StatusWindow sharedWindow];
888 float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
889 [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowAppearanceEffect"];
891 [sw setEntryEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
892 [[sw entryEffect] setEffectTime:time];
895 - (void)setStatusWindowExitEffect:(Class)effectClass
897 StatusWindow *sw = [StatusWindow sharedWindow];
899 float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
900 [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowVanishEffect"];
902 [sw setExitEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
903 [[sw exitEffect] setEffectTime:time];
906 - (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update
908 [(ITTSWBackgroundView *)[[StatusWindow sharedWindow] contentView] setBackgroundColor:color];
909 [df setObject:[NSArchiver archivedDataWithRootObject:color] forKey:@"statusWindowBackgroundColor"];
912 [backgroundColorWell setColor:color];
916 - (void)repopulateEffectPopupsForVerticalPosition:(ITVerticalWindowPosition)vPos horizontalPosition:(ITHorizontalWindowPosition)hPos
918 NSEnumerator *effectEnum = [effectClasses objectEnumerator];
921 [appearanceEffectPopup removeAllItems];
922 [vanishEffectPopup removeAllItems];
924 while ( (anItem = [effectEnum nextObject]) ) {
925 [appearanceEffectPopup addItemWithTitle:[anItem effectName]];
926 [vanishEffectPopup addItemWithTitle:[anItem effectName]];
928 [[appearanceEffectPopup lastItem] setRepresentedObject:anItem];
929 [[vanishEffectPopup lastItem] setRepresentedObject:anItem];
931 if ( [self effect:anItem supportsVerticalPosition:vPos withHorizontalPosition:hPos] ) {
932 [[appearanceEffectPopup lastItem] setEnabled:YES];
933 [[vanishEffectPopup lastItem] setEnabled:YES];
935 [[appearanceEffectPopup lastItem] setEnabled:NO];
936 [[vanishEffectPopup lastItem] setEnabled:NO];
942 - (BOOL)effect:(Class)effectClass supportsVerticalPosition:(ITVerticalWindowPosition)vPos withHorizontalPosition:(ITHorizontalWindowPosition)hPos
946 if ( vPos == ITWindowPositionTop ) {
947 if ( hPos == ITWindowPositionLeft ) {
948 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Left"] boolValue] ) ;
949 } else if ( hPos == ITWindowPositionCenter ) {
950 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Center"] boolValue] );
951 } else if ( hPos == ITWindowPositionRight ) {
952 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Right"] boolValue] );
954 } else if ( vPos == ITWindowPositionMiddle ) {
955 if ( hPos == ITWindowPositionLeft ) {
956 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Left"] boolValue] );
957 } else if ( hPos == ITWindowPositionCenter ) {
958 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Center"] boolValue] );
959 } else if ( hPos == ITWindowPositionRight ) {
960 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Right"] boolValue] );
962 } else if ( vPos == ITWindowPositionBottom ) {
963 if ( hPos == ITWindowPositionLeft ) {
964 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Left"] boolValue] );
965 } else if ( hPos == ITWindowPositionCenter ) {
966 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Center"] boolValue] );
967 } else if ( hPos == ITWindowPositionRight ) {
968 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Right"] boolValue] );
975 - (IBAction)changeMenus:(id)sender
977 ITDebugLog(@"Synchronizing menus");
978 [df setObject:myItems forKey:@"menu"];
982 - (void)setLaunchesAtLogin:(BOOL)flag
984 NSMutableDictionary *loginwindow;
985 NSMutableArray *loginarray;
986 ITDebugLog(@"Setting launches at login: %i", flag);
988 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
989 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
992 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
993 [[NSBundle mainBundle] bundlePath], @"Path",
994 [NSNumber numberWithInt:0], @"Hide", nil];
995 [loginarray addObject:itemDict];
998 for (i = 0; i < [loginarray count]; i++) {
999 NSDictionary *tempDict = [loginarray objectAtIndex:i];
1000 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
1001 [loginarray removeObjectAtIndex:i];
1006 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
1008 [loginwindow release];
1009 ITDebugLog(@"Finished setting launches at login.");
1013 /*************************************************************************/
1015 #pragma mark NSWindow DELEGATE METHODS
1016 /*************************************************************************/
1018 - (void)windowWillClose:(NSNotification *)note
1020 [(MainController *)controller closePreferences];
1023 /*************************************************************************/
1025 #pragma mark NSTextField DELEGATE METHODS
1026 /*************************************************************************/
1028 - (void)controlTextDidChange:(NSNotification*)note
1030 if ([note object] == hostTextField) {
1031 if ([[hostTextField stringValue] length] == 0) {
1032 [sharingPanelOKButton setEnabled:NO];
1034 [sharingPanelOKButton setEnabled:YES];
1039 /*************************************************************************/
1041 #pragma mark NSTableView DATASOURCE METHODS
1042 /*************************************************************************/
1044 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1046 if (aTableView == menuTableView) {
1047 return [myItems count];
1048 } else if (aTableView == allTableView) {
1049 return [availableItems count];
1050 } else if (aTableView == hotKeysTableView) {
1051 return [hotKeysArray count];
1053 return [[[NetworkController sharedController] remoteServices] count];
1057 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
1059 if (aTableView == menuTableView) {
1060 NSString *object = [myItems objectAtIndex:rowIndex];
1061 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1062 if ([object isEqualToString:@"showPlayer"]) {
1063 NSString *string = nil;
1065 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1067 [controller networkError:localException];
1071 return NSLocalizedString(object, @"ERROR");
1073 if ([submenuItems containsObject:object])
1075 return [NSImage imageNamed:@"submenu"];
1080 } else if (aTableView == allTableView) {
1081 NSString *object = [availableItems objectAtIndex:rowIndex];
1082 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1083 if ([object isEqualToString:@"showPlayer"]) {
1084 NSString *string = nil;
1086 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1088 [controller networkError:localException];
1092 return NSLocalizedString(object, @"ERROR");
1094 if ([submenuItems containsObject:object]) {
1095 return [NSImage imageNamed:@"submenu"];
1100 } else if (aTableView == hotKeysTableView) {
1101 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1102 return [hotKeyNamesArray objectAtIndex:rowIndex];
1104 return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
1107 return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
1111 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
1113 if (tableView == menuTableView) {
1114 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
1115 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
1119 if (tableView == allTableView) {
1120 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
1121 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
1127 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
1131 NSString *dragData, *temp;
1133 pb = [info draggingPasteboard];
1135 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
1136 dragData = [pb stringForType:@"MenuTableViewPboardType"];
1137 dragRow = [dragData intValue];
1138 temp = [myItems objectAtIndex:dragRow];
1140 if (tableView == menuTableView) {
1141 [myItems insertObject:temp atIndex:row];
1142 if (row > dragRow) {
1143 [myItems removeObjectAtIndex:dragRow];
1145 [myItems removeObjectAtIndex:dragRow + 1];
1147 } else if (tableView == allTableView) {
1148 if (![temp isEqualToString:@"separator"]) {
1149 [availableItems addObject:temp];
1151 [myItems removeObjectAtIndex:dragRow];
1153 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
1154 dragData = [pb stringForType:@"AllTableViewPboardType"];
1155 dragRow = [dragData intValue];
1156 temp = [availableItems objectAtIndex:dragRow];
1158 [myItems insertObject:temp atIndex:row];
1160 if (![temp isEqualToString:@"separator"]) {
1161 [availableItems removeObjectAtIndex:dragRow];
1165 [menuTableView reloadData];
1166 [allTableView reloadData];
1167 [self changeMenus:self];
1171 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
1173 if (tableView == allTableView) {
1174 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
1175 return NSDragOperationNone;
1178 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
1179 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
1180 if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
1181 return NSDragOperationNone;
1185 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
1186 return NSDragOperationGeneric;
1189 if (operation == NSTableViewDropOn || row == -1)
1191 return NSDragOperationNone;
1193 return NSDragOperationGeneric;
1197 /*************************************************************************/
1199 #pragma mark DEALLOCATION METHODS
1200 /*************************************************************************/
1204 [hotKeysArray release];
1205 [hotKeysDictionary release];
1206 [effectClasses release];
1207 [menuTableView setDataSource:nil];
1208 [allTableView setDataSource:nil];
1209 [controller release];
1210 [availableItems release];
1211 [submenuItems release];