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 [[NetworkController sharedController] resetServerName];
248 } else if ( [sender tag] == 5020 ) {
249 [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
250 } else if ( [sender tag] == 5030 ) {
251 //Set the server password
252 const char *instring = [[sender stringValue] UTF8String];
253 const char *password = "password";
254 unsigned char *result;
255 NSData *hashedPass, *passwordStringHash;
256 result = SHA1(instring, strlen(instring), NULL);
257 hashedPass = [NSData dataWithBytes:result length:strlen(result)];
258 result = SHA1(password, strlen(password), NULL);
259 passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
260 if (![hashedPass isEqualToData:passwordStringHash]) {
261 [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
262 [sender setStringValue:@"password"];
264 } else if ( [sender tag] == 5040 ) {
265 BOOL state = SENDER_STATE;
266 [df setBool:state forKey:@"useSharedPlayer"];
267 //Disable/enable the use of sharing options
268 [shareMenuTunesCheckbox setEnabled:!state];
269 [usePasswordCheckbox setEnabled:NO];
270 [passwordTextField setEnabled:NO];
271 [nameTextField setEnabled:NO];
272 [selectSharedPlayerButton setEnabled:state];
274 if (state && ([controller connectToServer] == 1)) {
275 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
276 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
278 [selectedPlayerTextField setStringValue:@"No shared player selected."];
279 [locationTextField setStringValue:@"-"];
280 if ([[NetworkController sharedController] isConnectedToServer]) {
281 [controller disconnectFromServer];
285 } else if ( [sender tag] == 5050 ) {
286 //If no player is selected in the table view, turn off OK button.
287 if ([sender clickedRow] == -1 ) {
288 [sharingPanelOKButton setEnabled:NO];
290 [sharingPanelOKButton setEnabled:YES];
292 } else if ( [sender tag] == 5051 ) {
293 [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
294 } else if ( [sender tag] == 5060 ) {
295 //Set OK button state
296 if (([selectPlayerBox contentView] == zeroConfView && [sharingTableView selectedRow] == -1) ||
297 ([selectPlayerBox contentView] == manualView && [[hostTextField stringValue] length] == 0)) {
298 [sharingPanelOKButton setEnabled:NO];
300 [sharingPanelOKButton setEnabled:YES];
302 //Show selection sheet
303 [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
304 } else if ( [sender tag] == 5100 ) {
306 if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
307 NSRect frame = [selectPlayerSheet frame];
308 frame.origin.y -= 58;
309 frame.size.height = 273;
310 if ([sharingTableView selectedRow] == -1) {
311 [sharingPanelOKButton setEnabled:NO];
313 [selectPlayerBox setContentView:zeroConfView];
314 [selectPlayerSheet setFrame:frame display:YES animate:YES];
315 } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
316 NSRect frame = [selectPlayerSheet frame];
317 frame.origin.y += 58;
318 frame.size.height = 215;
319 if ([[hostTextField stringValue] length] == 0) {
320 [sharingPanelOKButton setEnabled:NO];
322 [sharingPanelOKButton setEnabled:YES];
324 [selectPlayerBox setContentView:manualView];
325 [selectPlayerSheet setFrame:frame display:YES animate:YES];
326 [hostTextField selectText:nil];
328 } else if ( [sender tag] == 5150 ) {
329 const char *instring = [[sender stringValue] UTF8String];
330 unsigned char *result;
331 result = SHA1(instring, strlen(instring), NULL);
332 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
333 } else if ( [sender tag] == 5110 ) {
335 [NSApp endSheet:selectPlayerSheet];
336 [selectPlayerSheet orderOut:nil];
337 if ([selectPlayerBox contentView] == manualView) {
338 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
341 } else if ( [sender tag] == 5120 ) {
343 [NSApp endSheet:selectPlayerSheet];
344 [selectPlayerSheet orderOut:nil];
346 [self changeSharingSetting:clientPasswordTextField];
348 if ([selectPlayerBox contentView] == manualView) {
349 [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
351 if ([sharingTableView selectedRow] > -1) {
352 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
356 if ([controller connectToServer] == 1) {
357 [useSharedMenuTunesCheckbox setState:NSOnState];
358 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
359 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
361 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);
363 } else if ( [sender tag] == 6010 ) {
364 //Cancel password entry
365 [passwordPanel orderOut:nil];
366 [NSApp stopModalWithCode:0];
367 } else if ( [sender tag] == 6020 ) {
368 //OK password entry, retry connect
369 const char *instring = [[passwordPanelTextField stringValue] UTF8String];
370 unsigned char *result;
371 result = SHA1(instring, strlen(instring), NULL);
372 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
373 [passwordPanel orderOut:nil];
374 [NSApp stopModalWithCode:1];
379 - (IBAction)changeStatusWindowSetting:(id)sender
381 StatusWindow *sw = [StatusWindow sharedWindow];
382 ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
384 if ( [sender tag] == 2010) {
386 BOOL entryEffectValid = YES;
387 BOOL exitEffectValid = YES;
389 [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
390 [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
391 [sw setVerticalPosition:[sender selectedRow]];
392 [sw setHorizontalPosition:[sender selectedColumn]];
394 // Enable/disable the items in the popups.
395 [self repopulateEffectPopupsForVerticalPosition:[sw verticalPosition]
396 horizontalPosition:[sw horizontalPosition]];
398 // Make sure the effects support the new position.
399 entryEffectValid = ( [self effect:[[sw entryEffect] class]
400 supportsVerticalPosition:[sw verticalPosition]
401 withHorizontalPosition:[sw horizontalPosition]] );
402 exitEffectValid = ( [self effect:[[sw exitEffect] class]
403 supportsVerticalPosition:[sw verticalPosition]
404 withHorizontalPosition:[sw horizontalPosition]] );
406 if ( ! entryEffectValid ) {
407 [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITCutWindowEffect")]];
408 [self setStatusWindowEntryEffect:NSClassFromString(@"ITCutWindowEffect")];
410 [appearanceEffectPopup selectItemAtIndex:[[appearanceEffectPopup menu] indexOfItemWithRepresentedObject:[[sw entryEffect] class]]];
413 if ( ! exitEffectValid ) {
414 [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:NSClassFromString(@"ITDissolveWindowEffect")]];
415 [self setStatusWindowExitEffect:NSClassFromString(@"ITDissolveWindowEffect")];
417 [vanishEffectPopup selectItemAtIndex:[[vanishEffectPopup menu] indexOfItemWithRepresentedObject:[[sw exitEffect] class]]];
420 [(MainController *)controller showCurrentTrackInfo];
422 } else if ( [sender tag] == 2020) {
424 // Update screen selection.
426 } else if ( [sender tag] == 2030) {
428 [self setStatusWindowEntryEffect:[[sender selectedItem] representedObject]];
430 } else if ( [sender tag] == 2040) {
432 [self setStatusWindowExitEffect:[[sender selectedItem] representedObject]];
434 } else if ( [sender tag] == 2050) {
435 float newTime = ( -([sender floatValue]) );
436 [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
437 [[sw entryEffect] setEffectTime:newTime];
438 } else if ( [sender tag] == 2060) {
439 float newTime = ( -([sender floatValue]) );
440 [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
441 [[sw exitEffect] setEffectTime:newTime];
442 } else if ( [sender tag] == 2070) {
443 [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
444 [sw setExitDelay:[sender floatValue]];
445 } else if ( [sender tag] == 2080) {
446 [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
447 } else if ( [sender tag] == 2090) {
449 int setting = [sender indexOfSelectedItem];
451 if ( setting == 0 ) {
452 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundApple];
453 [backgroundColorWell setEnabled:NO];
454 [backgroundColorPopup setEnabled:NO];
455 } else if ( setting == 1 ) {
456 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundReadable];
457 [backgroundColorWell setEnabled:NO];
458 [backgroundColorPopup setEnabled:NO];
459 } else if ( setting == 2 ) {
460 [(ITTSWBackgroundView *)[sw contentView] setBackgroundMode:ITTSWBackgroundColored];
461 [backgroundColorWell setEnabled:YES];
462 [backgroundColorPopup setEnabled:YES];
465 [df setInteger:setting forKey:@"statusWindowBackgroundMode"];
467 } else if ( [sender tag] == 2091) {
468 [self setCustomColor:[sender color] updateWell:NO];
469 } else if ( [sender tag] == 2092) {
471 int selectedItem = [sender indexOfSelectedItem];
473 if ( selectedItem == 1 ) { // An NSPopUpButton in PullDown mode uses item 0 as its title. Its first selectable item is 1.
474 [self setCustomColor:[NSColor colorWithCalibratedRed:0.92549 green:0.686275 blue:0.0 alpha:1.0] updateWell:YES];
475 } else if ( selectedItem == 2 ) {
476 [self setCustomColor:[NSColor colorWithCalibratedRed:0.380392 green:0.670588 blue:0.0 alpha:1.0] updateWell:YES];
477 } else if ( selectedItem == 3 ) {
478 [self setCustomColor:[NSColor colorWithCalibratedRed:0.443137 green:0.231373 blue:0.619608 alpha:1.0] updateWell:YES];
479 } else if ( selectedItem == 4 ) {
480 [self setCustomColor:[NSColor colorWithCalibratedRed:0.831373 green:0.12549 blue:0.509804 alpha:1.0] updateWell:YES];
481 } else if ( selectedItem == 5 ) {
482 [self setCustomColor:[NSColor colorWithCalibratedRed:0.00784314 green:0.611765 blue:0.662745 alpha:1.0] updateWell:YES];
484 [self setCustomColor:[NSColor colorWithCalibratedWhite:0.15 alpha:0.70] updateWell:YES];
487 } else if ( [sender tag] == 2095) {
488 [df setInteger:[sender indexOfSelectedItem] forKey:@"statusWindowSizing"];
489 [(MainController *)controller showCurrentTrackInfo];
495 - (void)registerDefaults
498 NSMutableDictionary *loginWindow;
499 NSMutableArray *loginArray;
500 NSEnumerator *loginEnum;
502 ITDebugLog(@"Registering defaults.");
503 [df setObject:[NSArray arrayWithObjects:
515 nil] forKey:@"menu"];
517 [df setInteger:5 forKey:@"SongsInAdvance"];
518 // [df setBool:YES forKey:@"showName"]; // Song info will always show song title.
519 [df setBool:YES forKey:@"showArtist"];
520 [df setBool:NO forKey:@"showAlbum"];
521 [df setBool:NO forKey:@"showTime"];
523 [df setObject:@"ITCutWindowEffect" forKey:@"statusWindowAppearanceEffect"];
524 [df setObject:@"ITDissolveWindowEffect" forKey:@"statusWindowVanishEffect"];
525 [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
526 [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
527 [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
528 [df setInteger:(int)ITWindowPositionBottom forKey:@"statusWindowVerticalPosition"];
529 [df setInteger:(int)ITWindowPositionLeft forKey:@"statusWindowHorizontalPosition"];
530 [df setBool:YES forKey:@"showSongInfoOnChange"];
532 [df setObject:[NSArchiver archivedDataWithRootObject:[NSColor blueColor]] forKey:@"statusWindowBackgroundColor"];
536 loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
537 loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
538 loginEnum = [loginArray objectEnumerator];
540 while ( (anItem = [loginEnum nextObject]) ) {
541 if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
545 [loginWindow release];
548 [[StatusWindowController sharedController] showSetupQueryWindow];
554 [[StatusWindow sharedWindow] setLocked:NO];
555 [[StatusWindow sharedWindow] vanish:self];
556 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
558 [self setLaunchesAtLogin:YES];
561 - (void)autoLaunchCancel
563 [[StatusWindow sharedWindow] setLocked:NO];
564 [[StatusWindow sharedWindow] vanish:self];
565 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
568 - (void)deletePressedInTableView:(NSTableView *)tableView
570 if (tableView == menuTableView) {
571 int selRow = [tableView selectedRow];
572 ITDebugLog(@"Delete pressed in menu table view.");
574 NSString *object = [myItems objectAtIndex:selRow];
576 if ([object isEqualToString:@"preferences"]) {
581 if (![object isEqualToString:@"separator"])
582 [availableItems addObject:object];
583 ITDebugLog(@"Removing object named %@", object);
584 [myItems removeObjectAtIndex:selRow];
585 [menuTableView reloadData];
586 [allTableView reloadData];
588 [self changeMenus:self];
592 - (void)resetRemotePlayerTextFields
594 if ([[NetworkController sharedController] isConnectedToServer]) {
595 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
596 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
598 [selectedPlayerTextField setStringValue:@"No shared player selected."];
599 [locationTextField setStringValue:@"-"];
603 /*************************************************************************/
605 #pragma mark HOTKEY SUPPORT METHODS
606 /*************************************************************************/
608 - (IBAction)clearHotKey:(id)sender
610 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
611 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
612 [controller setupHotKeys];
613 [hotKeysTableView reloadData];
616 - (IBAction)editHotKey:(id)sender
618 ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
619 NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
620 ITKeyCombo *keyCombo;
622 ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
623 [controller clearHotKeys];
624 [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
625 [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
626 if ([panel runModal] == NSOKButton) {
627 NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
629 keyCombo = [panel keyCombo];
631 //Check for duplicate key combo
632 while ( (nextKey = [keyEnumerator nextObject]) ) {
633 if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
634 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
635 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
637 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
642 [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
643 [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
644 [controller setupHotKeys];
645 [hotKeysTableView reloadData];
646 ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
648 ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
652 - (void)hotKeysTableViewDoubleClicked:(id)sender
654 if ([sender clickedRow] > -1) {
655 [self editHotKey:sender];
659 /*************************************************************************/
661 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
662 /*************************************************************************/
666 ITDebugLog(@"Loading Preferences.nib.");
667 if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
668 ITDebugLog(@"Failed to load Preferences.nib.");
674 - (void)setupCustomizationTables
676 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
677 ITDebugLog(@"Setting up table views.");
678 // Set the table view cells up
679 [imgCell setImageScaling:NSScaleNone];
680 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
681 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
683 // Register for drag and drop
684 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
685 @"MenuTableViewPboardType",
686 @"AllTableViewPboardType",
688 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
689 @"MenuTableViewPboardType",
690 @"AllTableViewPboardType",
694 - (void)setupMenuItems
696 NSEnumerator *itemEnum;
698 ITDebugLog(@"Setting up table view arrays.");
699 // Set the list of items you can have.
700 availableItems = [[NSMutableArray alloc] initWithObjects:
716 // Get our preferred menu
717 myItems = [[df arrayForKey:@"menu"] mutableCopy];
719 // Delete items in the availableItems array that are already part of the menu
720 itemEnum = [myItems objectEnumerator];
721 while ( (anItem = [itemEnum nextObject]) ) {
722 if (![anItem isEqualToString:@"separator"]) {
723 [availableItems removeObject:anItem];
727 // Items that show should a submenu image
728 submenuItems = [[NSArray alloc] initWithObjects:
738 NSMutableDictionary *loginwindow;
739 NSMutableArray *loginarray;
740 NSEnumerator *loginEnum;
741 NSEnumerator *keyArrayEnum;
742 NSString *serverName;
747 ITDebugLog(@"Setting up preferences UI.");
748 // Fill in the number of songs in advance to show field
749 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
751 // Fill hot key array
752 keyArrayEnum = [hotKeysArray objectEnumerator];
754 while ( (anItem = [keyArrayEnum nextObject]) ) {
755 if ([df objectForKey:anItem]) {
756 ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
757 [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
759 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
763 ITDebugLog(@"Setting up track info checkboxes.");
764 // Check current track info buttons
765 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
766 [nameCheckbox setState:NSOnState]; // Song info will ALWAYS show song title.
767 [nameCheckbox setEnabled:NO]; // Song info will ALWAYS show song title.
768 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
769 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
770 [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
771 [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
773 // Set the launch at login checkbox state
774 ITDebugLog(@"Setting launch at login state.");
776 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
777 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
779 loginEnum = [loginarray objectEnumerator];
780 while ( (anItem = [loginEnum nextObject]) ) {
781 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
782 [launchAtLoginCheckbox setState:NSOnState];
786 // Set the launch player checkbox state
787 ITDebugLog(@"Setting launch player with MenuTunes state.");
788 [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
790 // Setup the positioning controls
791 [positionMatrix selectCellAtRow:[df integerForKey:@"statusWindowVerticalPosition"]
792 column:[df integerForKey:@"statusWindowHorizontalPosition"]];
794 // Setup effects controls
795 // Populate the effects popups
796 [appearanceEffectPopup setAutoenablesItems:NO];
797 [vanishEffectPopup setAutoenablesItems:NO];
798 [self repopulateEffectPopupsForVerticalPosition:[df integerForKey:@"statusWindowVerticalPosition"]
799 horizontalPosition:[df integerForKey:@"statusWindowHorizontalPosition"]];
801 // Attempt to find the pref'd effect in the list.
802 // If it's not there, use cut/dissolve.
803 if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])] ) {
804 [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowAppearanceEffect"])]];
806 [appearanceEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
809 if ( [effectClasses containsObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])] ) {
810 [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString([df stringForKey:@"statusWindowVanishEffect"])]];
812 [vanishEffectPopup selectItemAtIndex:[effectClasses indexOfObject:NSClassFromString(@"ITCutWindowEffect")]];
815 [appearanceSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowAppearanceSpeed"]) )];
816 [vanishSpeedSlider setFloatValue:( -([df floatForKey:@"statusWindowVanishSpeed"]) )];
817 [vanishDelaySlider setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
819 // Setup General Controls
820 selectedBGStyle = [df integerForKey:@"statusWindowBackgroundMode"];
821 [backgroundStylePopup selectItem:[backgroundStylePopup itemAtIndex:[backgroundStylePopup indexOfItemWithTag:selectedBGStyle]]];
823 if ( selectedBGStyle == ITTSWBackgroundColored ) {
824 [backgroundColorWell setEnabled:YES];
825 [backgroundColorPopup setEnabled:YES];
827 [backgroundColorWell setEnabled:NO];
828 [backgroundColorPopup setEnabled:NO];
831 colorData = [df dataForKey:@"statusWindowBackgroundColor"];
834 [backgroundColorWell setColor:(NSColor *)[NSUnarchiver unarchiveObjectWithData:colorData]];
836 [backgroundColorWell setColor:[NSColor blueColor]];
839 [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
841 [windowSizingPopup selectItem:[windowSizingPopup itemAtIndex:[windowSizingPopup indexOfItemWithTag:[df integerForKey:@"statusWindowSizing"]]]];
843 // Setup the sharing controls
844 if ([df boolForKey:@"enableSharing"]) {
845 [shareMenuTunesCheckbox setState:NSOnState];
846 [useSharedMenuTunesCheckbox setEnabled:NO];
847 [selectSharedPlayerButton setEnabled:NO];
848 [passwordTextField setEnabled:YES];
849 [usePasswordCheckbox 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 [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
867 if ([df dataForKey:@"sharedPlayerPassword"]) {
868 [passwordTextField setStringValue:@"password"];
870 if ([df stringForKey:@"sharedPlayerHost"]) {
871 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
874 if ([[NetworkController sharedController] isConnectedToServer]) {
875 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
876 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
878 [selectedPlayerTextField setStringValue:@"No shared player selected."];
879 [locationTextField setStringValue:@"-"];
883 - (void)setStatusWindowEntryEffect:(Class)effectClass
885 StatusWindow *sw = [StatusWindow sharedWindow];
887 float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
888 [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowAppearanceEffect"];
890 [sw setEntryEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
891 [[sw entryEffect] setEffectTime:time];
894 - (void)setStatusWindowExitEffect:(Class)effectClass
896 StatusWindow *sw = [StatusWindow sharedWindow];
898 float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
899 [df setObject:NSStringFromClass(effectClass) forKey:@"statusWindowVanishEffect"];
901 [sw setExitEffect:[[[effectClass alloc] initWithWindow:sw] autorelease]];
902 [[sw exitEffect] setEffectTime:time];
905 - (void)setCustomColor:(NSColor *)color updateWell:(BOOL)update
907 [(ITTSWBackgroundView *)[[StatusWindow sharedWindow] contentView] setBackgroundColor:color];
908 [df setObject:[NSArchiver archivedDataWithRootObject:color] forKey:@"statusWindowBackgroundColor"];
911 [backgroundColorWell setColor:color];
915 - (void)repopulateEffectPopupsForVerticalPosition:(ITVerticalWindowPosition)vPos horizontalPosition:(ITHorizontalWindowPosition)hPos
917 NSEnumerator *effectEnum = [effectClasses objectEnumerator];
920 [appearanceEffectPopup removeAllItems];
921 [vanishEffectPopup removeAllItems];
923 while ( (anItem = [effectEnum nextObject]) ) {
924 [appearanceEffectPopup addItemWithTitle:[anItem effectName]];
925 [vanishEffectPopup addItemWithTitle:[anItem effectName]];
927 [[appearanceEffectPopup lastItem] setRepresentedObject:anItem];
928 [[vanishEffectPopup lastItem] setRepresentedObject:anItem];
930 if ( [self effect:anItem supportsVerticalPosition:vPos withHorizontalPosition:hPos] ) {
931 [[appearanceEffectPopup lastItem] setEnabled:YES];
932 [[vanishEffectPopup lastItem] setEnabled:YES];
934 [[appearanceEffectPopup lastItem] setEnabled:NO];
935 [[vanishEffectPopup lastItem] setEnabled:NO];
941 - (BOOL)effect:(Class)effectClass supportsVerticalPosition:(ITVerticalWindowPosition)vPos withHorizontalPosition:(ITHorizontalWindowPosition)hPos
945 if ( vPos == ITWindowPositionTop ) {
946 if ( hPos == ITWindowPositionLeft ) {
947 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Left"] boolValue] ) ;
948 } else if ( hPos == ITWindowPositionCenter ) {
949 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Center"] boolValue] );
950 } else if ( hPos == ITWindowPositionRight ) {
951 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Top"] objectForKey:@"Right"] boolValue] );
953 } else if ( vPos == ITWindowPositionMiddle ) {
954 if ( hPos == ITWindowPositionLeft ) {
955 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Left"] boolValue] );
956 } else if ( hPos == ITWindowPositionCenter ) {
957 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Center"] boolValue] );
958 } else if ( hPos == ITWindowPositionRight ) {
959 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Middle"] objectForKey:@"Right"] boolValue] );
961 } else if ( vPos == ITWindowPositionBottom ) {
962 if ( hPos == ITWindowPositionLeft ) {
963 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Left"] boolValue] );
964 } else if ( hPos == ITWindowPositionCenter ) {
965 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Center"] boolValue] );
966 } else if ( hPos == ITWindowPositionRight ) {
967 valid = ( [[[[effectClass supportedPositions] objectForKey:@"Bottom"] objectForKey:@"Right"] boolValue] );
974 - (IBAction)changeMenus:(id)sender
976 ITDebugLog(@"Synchronizing menus");
977 [df setObject:myItems forKey:@"menu"];
981 - (void)setLaunchesAtLogin:(BOOL)flag
983 NSMutableDictionary *loginwindow;
984 NSMutableArray *loginarray;
985 ITDebugLog(@"Setting launches at login: %i", flag);
987 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
988 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
991 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
992 [[NSBundle mainBundle] bundlePath], @"Path",
993 [NSNumber numberWithInt:0], @"Hide", nil];
994 [loginarray addObject:itemDict];
997 for (i = 0; i < [loginarray count]; i++) {
998 NSDictionary *tempDict = [loginarray objectAtIndex:i];
999 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
1000 [loginarray removeObjectAtIndex:i];
1005 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
1007 [loginwindow release];
1008 ITDebugLog(@"Finished setting launches at login.");
1012 /*************************************************************************/
1014 #pragma mark NSWindow DELEGATE METHODS
1015 /*************************************************************************/
1017 - (void)windowWillClose:(NSNotification *)note
1019 [(MainController *)controller closePreferences];
1022 /*************************************************************************/
1024 #pragma mark NSTextField DELEGATE METHODS
1025 /*************************************************************************/
1027 - (void)controlTextDidChange:(NSNotification*)note
1029 if ([note object] == hostTextField) {
1030 if ([[hostTextField stringValue] length] == 0) {
1031 [sharingPanelOKButton setEnabled:NO];
1033 [sharingPanelOKButton setEnabled:YES];
1038 /*************************************************************************/
1040 #pragma mark NSTableView DATASOURCE METHODS
1041 /*************************************************************************/
1043 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
1045 if (aTableView == menuTableView) {
1046 return [myItems count];
1047 } else if (aTableView == allTableView) {
1048 return [availableItems count];
1049 } else if (aTableView == hotKeysTableView) {
1050 return [hotKeysArray count];
1052 return [[[NetworkController sharedController] remoteServices] count];
1056 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
1058 if (aTableView == menuTableView) {
1059 NSString *object = [myItems objectAtIndex:rowIndex];
1060 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1061 if ([object isEqualToString:@"showPlayer"]) {
1062 NSString *string = nil;
1064 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1066 [controller networkError:localException];
1070 return NSLocalizedString(object, @"ERROR");
1072 if ([submenuItems containsObject:object])
1074 return [NSImage imageNamed:@"submenu"];
1079 } else if (aTableView == allTableView) {
1080 NSString *object = [availableItems objectAtIndex:rowIndex];
1081 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1082 if ([object isEqualToString:@"showPlayer"]) {
1083 NSString *string = nil;
1085 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
1087 [controller networkError:localException];
1091 return NSLocalizedString(object, @"ERROR");
1093 if ([submenuItems containsObject:object]) {
1094 return [NSImage imageNamed:@"submenu"];
1099 } else if (aTableView == hotKeysTableView) {
1100 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
1101 return [hotKeyNamesArray objectAtIndex:rowIndex];
1103 return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
1106 return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
1110 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
1112 if (tableView == menuTableView) {
1113 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
1114 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
1118 if (tableView == allTableView) {
1119 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
1120 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
1126 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
1130 NSString *dragData, *temp;
1132 pb = [info draggingPasteboard];
1134 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
1135 dragData = [pb stringForType:@"MenuTableViewPboardType"];
1136 dragRow = [dragData intValue];
1137 temp = [myItems objectAtIndex:dragRow];
1139 if (tableView == menuTableView) {
1140 [myItems insertObject:temp atIndex:row];
1141 if (row > dragRow) {
1142 [myItems removeObjectAtIndex:dragRow];
1144 [myItems removeObjectAtIndex:dragRow + 1];
1146 } else if (tableView == allTableView) {
1147 if (![temp isEqualToString:@"separator"]) {
1148 [availableItems addObject:temp];
1150 [myItems removeObjectAtIndex:dragRow];
1152 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
1153 dragData = [pb stringForType:@"AllTableViewPboardType"];
1154 dragRow = [dragData intValue];
1155 temp = [availableItems objectAtIndex:dragRow];
1157 [myItems insertObject:temp atIndex:row];
1159 if (![temp isEqualToString:@"separator"]) {
1160 [availableItems removeObjectAtIndex:dragRow];
1164 [menuTableView reloadData];
1165 [allTableView reloadData];
1166 [self changeMenus:self];
1170 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
1172 if (tableView == allTableView) {
1173 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
1174 return NSDragOperationNone;
1177 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
1178 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
1179 if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
1180 return NSDragOperationNone;
1184 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
1185 return NSDragOperationGeneric;
1188 if (operation == NSTableViewDropOn || row == -1)
1190 return NSDragOperationNone;
1192 return NSDragOperationGeneric;
1196 /*************************************************************************/
1198 #pragma mark DEALLOCATION METHODS
1199 /*************************************************************************/
1203 [hotKeysArray release];
1204 [hotKeysDictionary release];
1205 [effectClasses release];
1206 [menuTableView setDataSource:nil];
1207 [allTableView setDataSource:nil];
1208 [controller release];
1209 [availableItems release];
1210 [submenuItems release];