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 <ITKit/ITHotKeyCenter.h>
14 #import <ITKit/ITKeyCombo.h>
15 #import <ITKit/ITKeyComboPanel.h>
16 #import <ITKit/ITWindowPositioning.h>
17 #import <ITKit/ITKeyBroadcaster.h>
19 #import <ITKit/ITCutWindowEffect.h>
20 #import <ITKit/ITDissolveWindowEffect.h>
21 #import <ITKit/ITSlideHorizontallyWindowEffect.h>
22 #import <ITKit/ITSlideVerticallyWindowEffect.h>
23 #import <ITKit/ITPivotWindowEffect.h>
26 #define SENDER_STATE (([sender state] == NSOnState) ? YES : NO)
28 /*************************************************************************/
30 #pragma mark PRIVATE INTERFACE
31 /*************************************************************************/
33 @interface PreferencesController (Private)
35 - (void)setupCustomizationTables;
36 - (void)setupMenuItems;
38 - (IBAction)changeMenus:(id)sender;
39 - (void)setLaunchesAtLogin:(BOOL)flag;
43 @implementation PreferencesController
46 /*************************************************************************/
48 #pragma mark STATIC VARIABLES
49 /*************************************************************************/
51 static PreferencesController *prefs = nil;
54 /*************************************************************************/
56 #pragma mark INITIALIZATION METHODS
57 /*************************************************************************/
59 + (PreferencesController *)sharedPrefs;
62 prefs = [[self alloc] init];
69 if ( (self = [super init]) ) {
70 ITDebugLog(@"Preferences initialized.");
71 df = [[NSUserDefaults standardUserDefaults] retain];
72 hotKeysArray = [[NSArray alloc] initWithObjects:@"PlayPause",
86 hotKeyNamesArray = [[NSArray alloc] initWithObjects:@"Play/Pause",
99 hotKeysDictionary = [[NSMutableDictionary alloc] init];
102 [self setupWindow]; // Load in the nib, and perform any initial setup.
108 /*************************************************************************/
110 #pragma mark ACCESSOR METHODS
111 /*************************************************************************/
118 - (void)setController:(id)object
120 [controller autorelease];
121 controller = [object retain];
125 /*************************************************************************/
127 #pragma mark INSTANCE METHODS
128 /*************************************************************************/
130 - (BOOL)showPasswordPanel
132 [passwordPanel setLevel:NSStatusWindowLevel];
133 [passwordPanelOKButton setTitle:@"Connect"];
134 [passwordPanelTitle setStringValue:@"Password Required"];
135 [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"Please enter a password for access to the MenuTunes player named %@ at %@.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
136 [passwordPanel center];
137 [passwordPanel setLevel:NSStatusWindowLevel];
138 [passwordPanel makeKeyAndOrderFront:nil];
139 if ([NSApp runModalForWindow:passwordPanel]) {
146 - (BOOL)showInvalidPasswordPanel
148 [passwordPanel setLevel:NSStatusWindowLevel];
149 [passwordPanelOKButton setTitle:@"Retry"];
150 [passwordPanelTitle setStringValue:@"Invalid Password"];
151 [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]]];
152 [passwordPanel center];
153 [passwordPanel setLevel:NSStatusWindowLevel];
154 [passwordPanel makeKeyAndOrderFront:nil];
155 if ([NSApp runModalForWindow:passwordPanel]) {
162 - (IBAction)showPrefsWindow:(id)sender
164 ITDebugLog(@"Showing preferences window.");
165 if (!myItems) { // If menu array does not exist yet, then the window hasn't been setup.
166 ITDebugLog(@"Window doesn't exist, initial setup.");
167 [self setupCustomizationTables]; // Setup the DnD manu config tables.
168 [self setupMenuItems]; // Setup the arrays of menu items
169 [self setupUI]; // Sets up additional UI
170 [window setDelegate:self];
171 [menuTableView reloadData];
172 [hotKeysTableView setDoubleAction:@selector(hotKeysTableViewDoubleClicked:)];
174 //Change the launch player checkbox to the proper name
176 [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
178 [controller networkError:localException];
183 [NSApp activateIgnoringOtherApps:YES];
184 [window performSelector:@selector(makeKeyAndOrderFront:) withObject:self afterDelay:0.0];
187 - (IBAction)changeGeneralSetting:(id)sender
189 ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
190 if ( [sender tag] == 1010) {
191 [self setLaunchesAtLogin:SENDER_STATE];
192 } else if ( [sender tag] == 1020) {
193 [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
194 } else if ( [sender tag] == 1030) {
195 [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
197 } else if ( [sender tag] == 1040) {
198 // This will not be executed. Song info always shows the title of the song.
199 // [df setBool:SENDER_STATE forKey:@"showName"];
200 } else if ( [sender tag] == 1050) {
201 [df setBool:SENDER_STATE forKey:@"showArtist"];
202 } else if ( [sender tag] == 1060) {
203 [df setBool:SENDER_STATE forKey:@"showAlbum"];
204 } else if ( [sender tag] == 1070) {
205 [df setBool:SENDER_STATE forKey:@"showTime"];
206 } else if ( [sender tag] == 1080) {
207 [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
208 } else if ( [sender tag] == 1090) {
209 [df setBool:SENDER_STATE forKey:@"showTrackRating"];
214 - (IBAction)changeSharingSetting:(id)sender
216 ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
217 if ( [sender tag] == 5010 ) {
218 BOOL state = SENDER_STATE;
219 [df setBool:state forKey:@"enableSharing"];
220 //Disable/enable the use of shared player options
221 [useSharedMenuTunesCheckbox setEnabled:!state];
222 [usePasswordCheckbox setEnabled:state];
223 [passwordTextField setEnabled:state];
224 [nameTextField setEnabled:state];
225 [selectSharedPlayerButton setEnabled:NO];
226 [controller setServerStatus:state]; //Set server status
227 } else if ( [sender tag] == 5015 ) {
228 [df setObject:[sender stringValue] forKey:@"sharedPlayerName"];
229 } else if ( [sender tag] == 5020 ) {
230 [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
231 } else if ( [sender tag] == 5030 ) {
232 //Set the server password
233 const char *instring = [[sender stringValue] UTF8String];
234 const char *password = "password";
235 unsigned char *result;
236 NSData *hashedPass, *passwordStringHash;
237 result = SHA1(instring, strlen(instring), NULL);
238 hashedPass = [NSData dataWithBytes:result length:strlen(result)];
239 result = SHA1(password, strlen(password), NULL);
240 passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
241 if (![hashedPass isEqualToData:passwordStringHash]) {
242 [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
243 [sender setStringValue:@"password"];
245 } else if ( [sender tag] == 5040 ) {
246 BOOL state = SENDER_STATE;
247 [df setBool:state forKey:@"useSharedPlayer"];
248 //Disable/enable the use of sharing options
249 [shareMenuTunesCheckbox setEnabled:!state];
250 [usePasswordCheckbox setEnabled:NO];
251 [passwordTextField setEnabled:NO];
252 [nameTextField setEnabled:NO];
253 [selectSharedPlayerButton setEnabled:state];
255 if (state && ([controller connectToServer] == 1)) {
256 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
257 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
259 [selectedPlayerTextField setStringValue:@"No shared player selected."];
260 [locationTextField setStringValue:@"-"];
261 if ([[NetworkController sharedController] isConnectedToServer]) {
262 [controller disconnectFromServer];
266 } else if ( [sender tag] == 5050 ) {
267 //If no player is selected in the table view, turn off OK button.
268 if ([sender clickedRow] == -1 ) {
269 [sharingPanelOKButton setEnabled:NO];
271 [sharingPanelOKButton setEnabled:YES];
273 } else if ( [sender tag] == 5051 ) {
274 [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
275 } else if ( [sender tag] == 5060 ) {
276 //Set OK button state
277 if (([selectPlayerBox contentView] == zeroConfView && [sharingTableView selectedRow] == -1) ||
278 ([selectPlayerBox contentView] == manualView && [[hostTextField stringValue] length] == 0)) {
279 [sharingPanelOKButton setEnabled:NO];
281 [sharingPanelOKButton setEnabled:YES];
283 //Show selection sheet
284 [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
285 } else if ( [sender tag] == 5100 ) {
287 if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
288 NSRect frame = [selectPlayerSheet frame];
289 frame.origin.y -= 58;
290 frame.size.height = 273;
291 if ([sharingTableView selectedRow] == -1) {
292 [sharingPanelOKButton setEnabled:NO];
294 [selectPlayerBox setContentView:zeroConfView];
295 [selectPlayerSheet setFrame:frame display:YES animate:YES];
296 } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
297 NSRect frame = [selectPlayerSheet frame];
298 frame.origin.y += 58;
299 frame.size.height = 215;
300 if ([[hostTextField stringValue] length] == 0) {
301 [sharingPanelOKButton setEnabled:NO];
303 [sharingPanelOKButton setEnabled:YES];
305 [selectPlayerBox setContentView:manualView];
306 [selectPlayerSheet setFrame:frame display:YES animate:YES];
307 [hostTextField selectText:nil];
309 } else if ( [sender tag] == 5150 ) {
310 const char *instring = [[sender stringValue] UTF8String];
311 unsigned char *result;
312 result = SHA1(instring, strlen(instring), NULL);
313 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
314 } else if ( [sender tag] == 5110 ) {
316 [NSApp endSheet:selectPlayerSheet];
317 [selectPlayerSheet orderOut:nil];
318 if ([selectPlayerBox contentView] == manualView) {
319 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
322 } else if ( [sender tag] == 5120 ) {
324 [NSApp endSheet:selectPlayerSheet];
325 [selectPlayerSheet orderOut:nil];
327 [self changeSharingSetting:clientPasswordTextField];
329 if ([selectPlayerBox contentView] == manualView) {
330 [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
332 if ([sharingTableView selectedRow] > -1) {
333 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
337 if ([controller connectToServer] == 1) {
338 [useSharedMenuTunesCheckbox setState:NSOnState];
339 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
340 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
342 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);
344 } else if ( [sender tag] == 6010 ) {
345 //Cancel password entry
346 [passwordPanel orderOut:nil];
347 [NSApp stopModalWithCode:0];
348 } else if ( [sender tag] == 6020 ) {
349 //OK password entry, retry connect
350 const char *instring = [[passwordPanelTextField stringValue] UTF8String];
351 unsigned char *result;
352 result = SHA1(instring, strlen(instring), NULL);
353 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
354 [passwordPanel orderOut:nil];
355 [NSApp stopModalWithCode:1];
360 - (IBAction)changeStatusWindowSetting:(id)sender
362 StatusWindow *sw = [StatusWindow sharedWindow];
363 ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
364 if ( [sender tag] == 2010) {
365 [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
366 [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
367 [sw setHorizontalPosition:[sender selectedColumn]];
368 [sw setVerticalPosition:[sender selectedRow]];
369 // update the window's position here
370 } else if ( [sender tag] == 2020) {
371 // update screen selection
372 } else if ( [sender tag] == 2030) {
373 int effectTag = [[sender selectedItem] tag];
374 float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
375 [df setInteger:effectTag forKey:@"statusWindowAppearanceEffect"];
377 if ( effectTag == 2100 ) {
378 [sw setEntryEffect:[[[ITCutWindowEffect alloc] initWithWindow:sw] autorelease]];
379 } else if ( effectTag == 2101 ) {
380 [sw setEntryEffect:[[[ITDissolveWindowEffect alloc] initWithWindow:sw] autorelease]];
381 } else if ( effectTag == 2102 ) {
382 [sw setEntryEffect:[[[ITSlideVerticallyWindowEffect alloc] initWithWindow:sw] autorelease]];
383 } else if ( effectTag == 2103 ) {
384 [sw setEntryEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
385 } else if ( effectTag == 2104 ) {
386 [sw setEntryEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
389 [[sw entryEffect] setEffectTime:time];
391 } else if ( [sender tag] == 2040) {
392 int effectTag = [[sender selectedItem] tag];
393 float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
395 [df setInteger:[[sender selectedItem] tag] forKey:@"statusWindowVanishEffect"];
397 if ( effectTag == 2100 ) {
398 [sw setExitEffect:[[[ITCutWindowEffect alloc] initWithWindow:sw] autorelease]];
399 } else if ( effectTag == 2101 ) {
400 [sw setExitEffect:[[[ITDissolveWindowEffect alloc] initWithWindow:sw] autorelease]];
401 } else if ( effectTag == 2102 ) {
402 [sw setExitEffect:[[[ITSlideVerticallyWindowEffect alloc] initWithWindow:sw] autorelease]];
403 } else if ( effectTag == 2103 ) {
404 [sw setExitEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
405 } else if ( effectTag == 2104 ) {
406 [sw setExitEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
409 [[sw exitEffect] setEffectTime:time];
411 } else if ( [sender tag] == 2050) {
412 float newTime = (-([sender floatValue]));
413 [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
414 [[sw entryEffect] setEffectTime:newTime];
415 } else if ( [sender tag] == 2060) {
416 float newTime = (-([sender floatValue]));
417 [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
418 [[sw exitEffect] setEffectTime:newTime];
419 } else if ( [sender tag] == 2070) {
420 [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
421 [sw setExitDelay:[sender floatValue]];
422 } else if ( [sender tag] == 2080) {
423 [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
428 - (void)registerDefaults
431 NSMutableDictionary *loginWindow;
432 NSMutableArray *loginArray;
433 NSEnumerator *loginEnum;
435 ITDebugLog(@"Registering defaults.");
436 [df setObject:[NSArray arrayWithObjects:
448 nil] forKey:@"menu"];
450 [df setInteger:5 forKey:@"SongsInAdvance"];
451 // [df setBool:YES forKey:@"showName"]; // Song info will always show song title.
452 [df setBool:YES forKey:@"showArtist"];
453 [df setBool:NO forKey:@"showAlbum"];
454 [df setBool:NO forKey:@"showTime"];
456 [df setInteger:2100 forKey:@"statusWindowAppearanceEffect"];
457 [df setInteger:2101 forKey:@"statusWindowVanishEffect"];
458 [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
459 [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
460 [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
461 [df setBool:YES forKey:@"showSongInfoOnChange"];
465 loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
466 loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
467 loginEnum = [loginArray objectEnumerator];
469 while ( (anItem = [loginEnum nextObject]) ) {
470 if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
474 [loginWindow release];
477 [[StatusWindowController sharedController] showSetupQueryWindow];
483 [[StatusWindow sharedWindow] setLocked:NO];
484 [[StatusWindow sharedWindow] vanish:self];
485 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
487 [self setLaunchesAtLogin:YES];
490 - (void)autoLaunchCancel
492 [[StatusWindow sharedWindow] setLocked:NO];
493 [[StatusWindow sharedWindow] vanish:self];
494 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
497 - (void)deletePressedInTableView:(NSTableView *)tableView
499 if (tableView == menuTableView) {
500 int selRow = [tableView selectedRow];
501 ITDebugLog(@"Delete pressed in menu table view.");
503 NSString *object = [myItems objectAtIndex:selRow];
505 if ([object isEqualToString:@"preferences"]) {
510 if (![object isEqualToString:@"separator"])
511 [availableItems addObject:object];
512 ITDebugLog(@"Removing object named %@", object);
513 [myItems removeObjectAtIndex:selRow];
514 [menuTableView reloadData];
515 [allTableView reloadData];
517 [self changeMenus:self];
521 - (void)resetRemotePlayerTextFields
523 if ([[NetworkController sharedController] isConnectedToServer]) {
524 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
525 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
527 [selectedPlayerTextField setStringValue:@"No shared player selected."];
528 [locationTextField setStringValue:@"-"];
532 /*************************************************************************/
534 #pragma mark HOTKEY SUPPORT METHODS
535 /*************************************************************************/
537 - (IBAction)clearHotKey:(id)sender
539 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
540 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
541 [controller setupHotKeys];
542 [hotKeysTableView reloadData];
545 - (IBAction)editHotKey:(id)sender
547 ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
548 NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
549 ITKeyCombo *keyCombo;
551 ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
552 [controller clearHotKeys];
553 [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
554 [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
555 if ([panel runModal] == NSOKButton) {
556 NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
558 keyCombo = [panel keyCombo];
560 //Check for duplicate key combo
561 while ( (nextKey = [keyEnumerator nextObject]) ) {
562 if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
563 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
564 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
566 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
571 [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
572 [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
573 [controller setupHotKeys];
574 [hotKeysTableView reloadData];
575 ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
577 ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
581 - (void)hotKeysTableViewDoubleClicked:(id)sender
583 if ([sender clickedRow] > -1) {
584 [self editHotKey:sender];
588 /*************************************************************************/
590 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
591 /*************************************************************************/
595 ITDebugLog(@"Loading Preferences.nib.");
596 if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
597 ITDebugLog(@"Failed to load Preferences.nib.");
603 - (void)setupCustomizationTables
605 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
606 ITDebugLog(@"Setting up table views.");
607 // Set the table view cells up
608 [imgCell setImageScaling:NSScaleNone];
609 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
610 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
612 // Register for drag and drop
613 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
614 @"MenuTableViewPboardType",
615 @"AllTableViewPboardType",
617 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
618 @"MenuTableViewPboardType",
619 @"AllTableViewPboardType",
623 - (void)setupMenuItems
625 NSEnumerator *itemEnum;
627 ITDebugLog(@"Setting up table view arrays.");
628 // Set the list of items you can have.
629 availableItems = [[NSMutableArray alloc] initWithObjects:
645 // Get our preferred menu
646 myItems = [[df arrayForKey:@"menu"] mutableCopy];
648 // Delete items in the availableItems array that are already part of the menu
649 itemEnum = [myItems objectEnumerator];
650 while ( (anItem = [itemEnum nextObject]) ) {
651 if (![anItem isEqualToString:@"separator"]) {
652 [availableItems removeObject:anItem];
656 // Items that show should a submenu image
657 submenuItems = [[NSArray alloc] initWithObjects:
667 NSMutableDictionary *loginwindow;
668 NSMutableArray *loginarray;
669 NSEnumerator *loginEnum, *keyArrayEnum;
670 NSString *serverName;
673 ITDebugLog(@"Setting up preferences UI.");
674 // Fill in the number of songs in advance to show field
675 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
677 // Fill hot key array
678 keyArrayEnum = [hotKeysArray objectEnumerator];
680 while ( (anItem = [keyArrayEnum nextObject]) ) {
681 if ([df objectForKey:anItem]) {
682 ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
683 [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
685 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
689 ITDebugLog(@"Setting up track info checkboxes.");
690 // Check current track info buttons
691 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
692 [nameCheckbox setState:NSOnState]; // Song info will ALWAYS show song title.
693 [nameCheckbox setEnabled:NO]; // Song info will ALWAYS show song title.
694 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
695 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
696 [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
697 [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
699 // Set the launch at login checkbox state
700 ITDebugLog(@"Setting launch at login state.");
702 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
703 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
705 loginEnum = [loginarray objectEnumerator];
706 while ( (anItem = [loginEnum nextObject]) ) {
707 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
708 [launchAtLoginCheckbox setState:NSOnState];
712 // Set the launch player checkbox state
713 ITDebugLog(@"Setting launch player with MenuTunes state.");
714 [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
716 // Setup the positioning controls
718 // Setup effects controls
719 [appearanceEffectPopup selectItem:[appearanceEffectPopup itemAtIndex:[appearanceEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowAppearanceEffect"]]]];
720 [vanishEffectPopup selectItem:[vanishEffectPopup itemAtIndex:[vanishEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowVanishEffect"]]]];
721 [appearanceSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowAppearanceSpeed"])];
722 [vanishSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowVanishSpeed"])];
723 [vanishDelaySlider setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
724 [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
726 // Setup the sharing controls
727 if ([df boolForKey:@"enableSharing"]) {
728 [shareMenuTunesCheckbox setState:NSOnState];
729 [useSharedMenuTunesCheckbox setEnabled:NO];
730 [selectSharedPlayerButton setEnabled:NO];
731 [passwordTextField setEnabled:YES];
732 [usePasswordCheckbox setEnabled:YES];
733 [nameTextField setEnabled:YES];
734 } else if ([df boolForKey:@"useSharedPlayer"]) {
735 [useSharedMenuTunesCheckbox setState:NSOnState];
736 [shareMenuTunesCheckbox setEnabled:NO];
737 [selectSharedPlayerButton setEnabled:YES];
740 [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
742 serverName = [df stringForKey:@"sharedPlayerName"];
743 if (!serverName || [serverName length] == 0) {
744 serverName = @"MenuTunes Shared Player";
746 [nameTextField setStringValue:serverName];
748 [selectPlayerBox setContentView:zeroConfView];
749 [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
750 if ([df dataForKey:@"sharedPlayerPassword"]) {
751 [passwordTextField setStringValue:@"password"];
753 if ([df stringForKey:@"sharedPlayerHost"]) {
754 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
757 if ([[NetworkController sharedController] isConnectedToServer]) {
758 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
759 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
761 [selectedPlayerTextField setStringValue:@"No shared player selected."];
762 [locationTextField setStringValue:@"-"];
766 - (IBAction)changeMenus:(id)sender
768 ITDebugLog(@"Synchronizing menus");
769 [df setObject:myItems forKey:@"menu"];
773 - (void)setLaunchesAtLogin:(BOOL)flag
775 NSMutableDictionary *loginwindow;
776 NSMutableArray *loginarray;
777 ITDebugLog(@"Setting launches at login: %i", flag);
779 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
780 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
783 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
784 [[NSBundle mainBundle] bundlePath], @"Path",
785 [NSNumber numberWithInt:0], @"Hide", nil];
786 [loginarray addObject:itemDict];
789 for (i = 0; i < [loginarray count]; i++) {
790 NSDictionary *tempDict = [loginarray objectAtIndex:i];
791 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
792 [loginarray removeObjectAtIndex:i];
797 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
799 [loginwindow release];
800 ITDebugLog(@"Finished setting launches at login.");
804 /*************************************************************************/
806 #pragma mark NSWindow DELEGATE METHODS
807 /*************************************************************************/
809 - (void)windowWillClose:(NSNotification *)note
811 [(MainController *)controller closePreferences];
814 /*************************************************************************/
816 #pragma mark NSTextField DELEGATE METHODS
817 /*************************************************************************/
819 - (void)controlTextDidChange:(NSNotification*)note
821 if ([note object] == hostTextField) {
822 if ([[hostTextField stringValue] length] == 0) {
823 [sharingPanelOKButton setEnabled:NO];
825 [sharingPanelOKButton setEnabled:YES];
830 /*************************************************************************/
832 #pragma mark NSTableView DATASOURCE METHODS
833 /*************************************************************************/
835 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
837 if (aTableView == menuTableView) {
838 return [myItems count];
839 } else if (aTableView == allTableView) {
840 return [availableItems count];
841 } else if (aTableView == hotKeysTableView) {
842 return [hotKeysArray count];
844 return [[[NetworkController sharedController] remoteServices] count];
848 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
850 if (aTableView == menuTableView) {
851 NSString *object = [myItems objectAtIndex:rowIndex];
852 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
853 if ([object isEqualToString:@"showPlayer"]) {
856 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
858 [controller networkError:localException];
862 return NSLocalizedString(object, @"ERROR");
864 if ([submenuItems containsObject:object])
866 return [NSImage imageNamed:@"submenu"];
871 } else if (aTableView == allTableView) {
872 NSString *object = [availableItems objectAtIndex:rowIndex];
873 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
874 if ([object isEqualToString:@"showPlayer"]) {
877 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
879 [controller networkError:localException];
883 return NSLocalizedString(object, @"ERROR");
885 if ([submenuItems containsObject:object]) {
886 return [NSImage imageNamed:@"submenu"];
891 } else if (aTableView == hotKeysTableView) {
892 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
893 return [hotKeyNamesArray objectAtIndex:rowIndex];
895 return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
898 return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
902 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
904 if (tableView == menuTableView) {
905 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
906 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
910 if (tableView == allTableView) {
911 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
912 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
918 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
922 NSString *dragData, *temp;
924 pb = [info draggingPasteboard];
926 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
927 dragData = [pb stringForType:@"MenuTableViewPboardType"];
928 dragRow = [dragData intValue];
929 temp = [myItems objectAtIndex:dragRow];
931 if (tableView == menuTableView) {
932 [myItems insertObject:temp atIndex:row];
934 [myItems removeObjectAtIndex:dragRow];
936 [myItems removeObjectAtIndex:dragRow + 1];
938 } else if (tableView == allTableView) {
939 if (![temp isEqualToString:@"separator"]) {
940 [availableItems addObject:temp];
942 [myItems removeObjectAtIndex:dragRow];
944 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
945 dragData = [pb stringForType:@"AllTableViewPboardType"];
946 dragRow = [dragData intValue];
947 temp = [availableItems objectAtIndex:dragRow];
949 [myItems insertObject:temp atIndex:row];
951 if (![temp isEqualToString:@"separator"]) {
952 [availableItems removeObjectAtIndex:dragRow];
956 [menuTableView reloadData];
957 [allTableView reloadData];
958 [self changeMenus:self];
962 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
964 if (tableView == allTableView) {
965 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
966 return NSDragOperationNone;
969 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
970 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
971 if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
972 return NSDragOperationNone;
976 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
977 return NSDragOperationGeneric;
980 if (operation == NSTableViewDropOn || row == -1)
982 return NSDragOperationNone;
984 return NSDragOperationGeneric;
988 /*************************************************************************/
990 #pragma mark DEALLOCATION METHODS
991 /*************************************************************************/
995 [hotKeysArray release];
996 [hotKeysDictionary release];
997 [menuTableView setDataSource:nil];
998 [allTableView setDataSource:nil];
999 [controller release];
1000 [availableItems release];
1001 [submenuItems release];