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:@"OK"];
134 [passwordPanelMessage setStringValue:[NSString stringWithFormat:@"Please enter a password for access to the MenuTunes player named %@ at %@.", [[[NetworkController sharedController] networkObject] serverName], [[NetworkController sharedController] remoteHost]]];
135 [passwordPanel makeKeyAndOrderFront:nil];
136 if ([NSApp runModalForWindow:passwordPanel]) {
143 - (BOOL)showInvalidPasswordPanel
145 [passwordPanel setLevel:NSStatusWindowLevel];
146 [passwordPanelOKButton setTitle:@"Retry"];
147 [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]]];
148 [passwordPanel makeKeyAndOrderFront:nil];
149 if ([NSApp runModalForWindow:passwordPanel]) {
156 - (IBAction)showPrefsWindow:(id)sender
158 ITDebugLog(@"Showing preferences window.");
159 if (! window) { // If window does not exist yet, then the nib hasn't been loaded.
160 ITDebugLog(@"Window doesn't exist, initial setup.");
161 [self setupCustomizationTables]; // Setup the DnD manu config tables.
162 [self setupMenuItems]; // Setup the arrays of menu items
163 [self setupUI]; // Sets up additional UI
164 [window setDelegate:self];
165 [menuTableView reloadData];
166 [hotKeysTableView setDoubleAction:@selector(hotKeysTableViewDoubleClicked:)];
168 //Change the launch player checkbox to the proper name
170 [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
172 [controller networkError:localException];
177 [NSApp activateIgnoringOtherApps:YES];
178 [window performSelector:@selector(makeKeyAndOrderFront:) withObject:self afterDelay:0.0];
181 - (IBAction)changeGeneralSetting:(id)sender
183 ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
184 if ( [sender tag] == 1010) {
185 [self setLaunchesAtLogin:SENDER_STATE];
186 } else if ( [sender tag] == 1020) {
187 [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
188 } else if ( [sender tag] == 1030) {
189 [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
191 } else if ( [sender tag] == 1040) {
192 // This will not be executed. Song info always shows the title of the song.
193 // [df setBool:SENDER_STATE forKey:@"showName"];
194 } else if ( [sender tag] == 1050) {
195 [df setBool:SENDER_STATE forKey:@"showArtist"];
196 } else if ( [sender tag] == 1060) {
197 [df setBool:SENDER_STATE forKey:@"showAlbum"];
198 } else if ( [sender tag] == 1070) {
199 [df setBool:SENDER_STATE forKey:@"showTime"];
200 } else if ( [sender tag] == 1080) {
201 [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
202 } else if ( [sender tag] == 1090) {
203 [df setBool:SENDER_STATE forKey:@"showTrackRating"];
208 - (IBAction)changeSharingSetting:(id)sender
210 ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
211 if ( [sender tag] == 5010 ) {
212 BOOL state = SENDER_STATE;
213 [df setBool:state forKey:@"enableSharing"];
214 //Disable/enable the use of shared player options
215 [useSharedMenuTunesCheckbox setEnabled:!state];
216 [usePasswordCheckbox setEnabled:state];
217 [passwordTextField setEnabled:state];
218 [nameTextField setEnabled:state];
219 [selectSharedPlayerButton setEnabled:NO];
220 [controller setServerStatus:state]; //Set server status
221 } else if ( [sender tag] == 5015 ) {
222 [df setObject:[sender stringValue] forKey:@"sharedPlayerName"];
223 } else if ( [sender tag] == 5020 ) {
224 [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
225 } else if ( [sender tag] == 5030 ) {
226 //Set the server password
227 const char *instring = [[sender stringValue] UTF8String];
228 const char *password = "password";
229 unsigned char *result;
230 NSData *hashedPass, *passwordStringHash;
231 result = SHA1(instring, strlen(instring), NULL);
232 hashedPass = [NSData dataWithBytes:result length:strlen(result)];
233 result = SHA1(password, strlen(password), NULL);
234 passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
235 if (![hashedPass isEqualToData:passwordStringHash]) {
236 [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
237 [sender setStringValue:@"password"];
239 } else if ( [sender tag] == 5040 ) {
240 BOOL state = SENDER_STATE;
241 [df setBool:state forKey:@"useSharedPlayer"];
242 //Disable/enable the use of sharing options
243 [shareMenuTunesCheckbox setEnabled:!state];
244 [usePasswordCheckbox setEnabled:NO];
245 [passwordTextField setEnabled:NO];
246 [nameTextField setEnabled:NO];
247 [selectSharedPlayerButton setEnabled:state];
249 if (state && [controller connectToServer]) {
250 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
251 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
253 [selectedPlayerTextField setStringValue:@"No shared player selected."];
254 [locationTextField setStringValue:@"-"];
255 if ([[NetworkController sharedController] isConnectedToServer]) {
256 [controller disconnectFromServer];
260 } else if ( [sender tag] == 5050 ) {
261 //Do nothing on table view click
262 } else if ( [sender tag] == 5051 ) {
263 [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
264 } else if ( [sender tag] == 5060 ) {
265 //Show selection sheet
266 [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
267 } else if ( [sender tag] == 5100 ) {
269 if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
270 NSRect frame = [selectPlayerSheet frame];
271 frame.origin.y -= 58;
272 frame.size.height = 273;
273 [selectPlayerBox setContentView:zeroConfView];
274 [selectPlayerSheet setFrame:frame display:YES animate:YES];
275 } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
276 NSRect frame = [selectPlayerSheet frame];
277 frame.origin.y += 58;
278 frame.size.height = 215;
279 //[window makeFirstResponder:hostTextField];
280 [selectPlayerBox setContentView:manualView];
281 [selectPlayerSheet setFrame:frame display:YES animate:YES];
282 [hostTextField selectText:nil];
284 } else if ( [sender tag] == 5150 ) {
285 const char *instring = [[sender stringValue] UTF8String];
286 unsigned char *result;
287 result = SHA1(instring, strlen(instring), NULL);
288 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
289 } else if ( [sender tag] == 5110 ) {
291 [NSApp endSheet:selectPlayerSheet];
292 [selectPlayerSheet orderOut:nil];
293 if ([selectPlayerBox contentView] == manualView) {
294 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
297 } else if ( [sender tag] == 5120 ) {
299 [NSApp endSheet:selectPlayerSheet];
300 [selectPlayerSheet orderOut:nil];
302 [self changeSharingSetting:clientPasswordTextField];
304 if ([selectPlayerBox contentView] == manualView) {
305 [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
307 if ([sharingTableView selectedRow] > -1) {
308 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
312 if ([controller connectToServer]) {
313 [useSharedMenuTunesCheckbox setState:NSOnState];
314 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
315 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
317 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);
319 } else if ( [sender tag] == 6010 ) {
320 //Cancel password entry
321 [passwordPanel orderOut:nil];
322 [NSApp stopModalWithCode:0];
323 } else if ( [sender tag] == 6020 ) {
324 //OK password entry, retry connect
325 const char *instring = [[passwordPanelTextField stringValue] UTF8String];
326 unsigned char *result;
327 result = SHA1(instring, strlen(instring), NULL);
328 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
329 [passwordPanel orderOut:nil];
330 [NSApp stopModalWithCode:1];
335 - (IBAction)changeStatusWindowSetting:(id)sender
337 StatusWindow *sw = [StatusWindow sharedWindow];
338 ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
339 if ( [sender tag] == 2010) {
340 [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
341 [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
342 // update the window's position here
343 } else if ( [sender tag] == 2020) {
344 // update screen selection
345 } else if ( [sender tag] == 2030) {
346 int effectTag = [[sender selectedItem] tag];
347 float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
348 [df setInteger:effectTag forKey:@"statusWindowAppearanceEffect"];
350 if ( effectTag == 2100 ) {
351 [sw setEntryEffect:[[[ITCutWindowEffect alloc] initWithWindow:sw] autorelease]];
352 } else if ( effectTag == 2101 ) {
353 [sw setEntryEffect:[[[ITDissolveWindowEffect alloc] initWithWindow:sw] autorelease]];
354 } else if ( effectTag == 2102 ) {
355 [sw setEntryEffect:[[[ITSlideVerticallyWindowEffect alloc] initWithWindow:sw] autorelease]];
356 } else if ( effectTag == 2103 ) {
357 [sw setEntryEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
358 } else if ( effectTag == 2104 ) {
359 [sw setEntryEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
362 [[sw entryEffect] setEffectTime:time];
364 } else if ( [sender tag] == 2040) {
365 int effectTag = [[sender selectedItem] tag];
366 float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
368 [df setInteger:[[sender selectedItem] tag] forKey:@"statusWindowVanishEffect"];
370 if ( effectTag == 2100 ) {
371 [sw setExitEffect:[[[ITCutWindowEffect alloc] initWithWindow:sw] autorelease]];
372 } else if ( effectTag == 2101 ) {
373 [sw setExitEffect:[[[ITDissolveWindowEffect alloc] initWithWindow:sw] autorelease]];
374 } else if ( effectTag == 2102 ) {
375 [sw setExitEffect:[[[ITSlideVerticallyWindowEffect alloc] initWithWindow:sw] autorelease]];
376 } else if ( effectTag == 2103 ) {
377 [sw setExitEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
378 } else if ( effectTag == 2104 ) {
379 [sw setExitEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
382 [[sw exitEffect] setEffectTime:time];
384 } else if ( [sender tag] == 2050) {
385 float newTime = (-([sender floatValue]));
386 [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
387 [[sw entryEffect] setEffectTime:newTime];
388 } else if ( [sender tag] == 2060) {
389 float newTime = (-([sender floatValue]));
390 [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
391 [[sw exitEffect] setEffectTime:newTime];
392 } else if ( [sender tag] == 2070) {
393 [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
394 [sw setExitDelay:[sender floatValue]];
395 } else if ( [sender tag] == 2080) {
396 [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
401 - (void)registerDefaults
404 NSMutableDictionary *loginWindow;
405 NSMutableArray *loginArray;
406 NSEnumerator *loginEnum;
408 ITDebugLog(@"Registering defaults.");
409 [df setObject:[NSArray arrayWithObjects:
421 nil] forKey:@"menu"];
423 [df setInteger:5 forKey:@"SongsInAdvance"];
424 // [df setBool:YES forKey:@"showName"]; // Song info will always show song title.
425 [df setBool:YES forKey:@"showArtist"];
426 [df setBool:NO forKey:@"showAlbum"];
427 [df setBool:NO forKey:@"showTime"];
429 [df setInteger:2100 forKey:@"statusWindowAppearanceEffect"];
430 [df setInteger:2101 forKey:@"statusWindowVanishEffect"];
431 [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
432 [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
433 [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
434 [df setBool:YES forKey:@"showSongInfoOnChange"];
438 loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
439 loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
440 loginEnum = [loginArray objectEnumerator];
442 while ( (anItem = [loginEnum nextObject]) ) {
443 if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
447 [loginWindow release];
450 [[StatusWindowController sharedController] showSetupQueryWindow];
456 [[StatusWindow sharedWindow] setLocked:NO];
457 [[StatusWindow sharedWindow] vanish:self];
458 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
460 [self setLaunchesAtLogin:YES];
463 - (void)autoLaunchCancel
465 [[StatusWindow sharedWindow] setLocked:NO];
466 [[StatusWindow sharedWindow] vanish:self];
467 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
470 - (void)deletePressedInTableView:(NSTableView *)tableView
472 if (tableView == menuTableView) {
473 int selRow = [tableView selectedRow];
474 ITDebugLog(@"Delete pressed in menu table view.");
476 NSString *object = [myItems objectAtIndex:selRow];
478 if ([object isEqualToString:@"preferences"]) {
483 if (![object isEqualToString:@"separator"])
484 [availableItems addObject:object];
485 ITDebugLog(@"Removing object named %@", object);
486 [myItems removeObjectAtIndex:selRow];
487 [menuTableView reloadData];
488 [allTableView reloadData];
490 [self changeMenus:self];
494 /*************************************************************************/
496 #pragma mark HOTKEY SUPPORT METHODS
497 /*************************************************************************/
499 - (IBAction)clearHotKey:(id)sender
501 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
502 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
503 [controller setupHotKeys];
504 [hotKeysTableView reloadData];
507 - (IBAction)editHotKey:(id)sender
509 ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
510 NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
511 ITKeyCombo *keyCombo;
513 ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
514 [controller clearHotKeys];
515 [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
516 [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
517 if ([panel runModal] == NSOKButton) {
518 NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
520 keyCombo = [panel keyCombo];
522 //Check for duplicate key combo
523 while ( (nextKey = [keyEnumerator nextObject]) ) {
524 if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
525 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
526 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
528 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
533 [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
534 [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
535 [controller setupHotKeys];
536 [hotKeysTableView reloadData];
537 ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
539 ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
543 - (void)hotKeysTableViewDoubleClicked:(id)sender
545 if ([sender clickedRow] > -1) {
546 [self editHotKey:sender];
550 /*************************************************************************/
552 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
553 /*************************************************************************/
557 ITDebugLog(@"Loading Preferences.nib.");
558 if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
559 ITDebugLog(@"Failed to load Preferences.nib.");
565 - (void)setupCustomizationTables
567 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
568 ITDebugLog(@"Setting up table views.");
569 // Set the table view cells up
570 [imgCell setImageScaling:NSScaleNone];
571 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
572 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
574 // Register for drag and drop
575 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
576 @"MenuTableViewPboardType",
577 @"AllTableViewPboardType",
579 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
580 @"MenuTableViewPboardType",
581 @"AllTableViewPboardType",
585 - (void)setupMenuItems
587 NSEnumerator *itemEnum;
589 ITDebugLog(@"Setting up table view arrays.");
590 // Set the list of items you can have.
591 availableItems = [[NSMutableArray alloc] initWithObjects:
607 // Get our preferred menu
608 myItems = [[df arrayForKey:@"menu"] mutableCopy];
610 // Delete items in the availableItems array that are already part of the menu
611 itemEnum = [myItems objectEnumerator];
612 while ( (anItem = [itemEnum nextObject]) ) {
613 if (![anItem isEqualToString:@"separator"]) {
614 [availableItems removeObject:anItem];
618 // Items that show should a submenu image
619 submenuItems = [[NSArray alloc] initWithObjects:
629 NSMutableDictionary *loginwindow;
630 NSMutableArray *loginarray;
631 NSEnumerator *loginEnum, *keyArrayEnum;
632 NSString *serverName;
635 ITDebugLog(@"Setting up preferences UI.");
636 // Fill in the number of songs in advance to show field
637 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
639 // Fill hot key array
640 keyArrayEnum = [hotKeysArray objectEnumerator];
642 while ( (anItem = [keyArrayEnum nextObject]) ) {
643 if ([df objectForKey:anItem]) {
644 ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
645 [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
647 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
651 ITDebugLog(@"Setting up track info checkboxes.");
652 // Check current track info buttons
653 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
654 [nameCheckbox setState:NSOnState]; // Song info will ALWAYS show song title.
655 [nameCheckbox setEnabled:NO]; // Song info will ALWAYS show song title.
656 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
657 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
658 [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
659 [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
661 // Set the launch at login checkbox state
662 ITDebugLog(@"Setting launch at login state.");
664 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
665 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
667 loginEnum = [loginarray objectEnumerator];
668 while ( (anItem = [loginEnum nextObject]) ) {
669 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
670 [launchAtLoginCheckbox setState:NSOnState];
674 // Set the launch player checkbox state
675 ITDebugLog(@"Setting launch player with MenuTunes state.");
676 [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
678 // Setup the positioning controls
680 // Setup effects controls
681 [appearanceEffectPopup selectItem:[appearanceEffectPopup itemAtIndex:[appearanceEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowAppearanceEffect"]]]];
682 [vanishEffectPopup selectItem:[vanishEffectPopup itemAtIndex:[vanishEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowVanishEffect"]]]];
683 [appearanceSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowAppearanceSpeed"])];
684 [vanishSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowVanishSpeed"])];
685 [vanishDelaySlider setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
686 [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
688 // Setup the sharing controls
689 if ([df boolForKey:@"enableSharing"]) {
690 [shareMenuTunesCheckbox setState:NSOnState];
691 [useSharedMenuTunesCheckbox setEnabled:NO];
692 [selectSharedPlayerButton setEnabled:NO];
693 [passwordTextField setEnabled:YES];
694 [usePasswordCheckbox setEnabled:YES];
695 [nameTextField setEnabled:YES];
696 } else if ([df boolForKey:@"useSharedPlayer"]) {
697 [useSharedMenuTunesCheckbox setState:NSOnState];
698 [shareMenuTunesCheckbox setEnabled:NO];
699 [selectSharedPlayerButton setEnabled:YES];
702 [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
704 serverName = [df stringForKey:@"sharedPlayerName"];
705 if (!serverName || [serverName length] == 0) {
706 serverName = @"MenuTunes Shared Player";
708 [nameTextField setStringValue:serverName];
710 [selectPlayerBox setContentView:zeroConfView];
711 [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
712 if ([df dataForKey:@"sharedPlayerPassword"]) {
713 [passwordTextField setStringValue:@"password"];
715 if ([df stringForKey:@"sharedPlayerHost"]) {
716 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
719 if ([[NetworkController sharedController] isConnectedToServer]) {
720 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
721 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
723 [selectedPlayerTextField setStringValue:@"No shared player selected."];
724 [locationTextField setStringValue:@"-"];
728 - (IBAction)changeMenus:(id)sender
730 ITDebugLog(@"Synchronizing menus");
731 [df setObject:myItems forKey:@"menu"];
735 - (void)setLaunchesAtLogin:(BOOL)flag
737 NSMutableDictionary *loginwindow;
738 NSMutableArray *loginarray;
739 ITDebugLog(@"Setting launches at login: %i", flag);
741 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
742 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
745 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
746 [[NSBundle mainBundle] bundlePath], @"Path",
747 [NSNumber numberWithInt:0], @"Hide", nil];
748 [loginarray addObject:itemDict];
751 for (i = 0; i < [loginarray count]; i++) {
752 NSDictionary *tempDict = [loginarray objectAtIndex:i];
753 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
754 [loginarray removeObjectAtIndex:i];
759 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
761 [loginwindow release];
762 ITDebugLog(@"Finished setting launches at login.");
766 /*************************************************************************/
768 #pragma mark NSWindow DELEGATE METHODS
769 /*************************************************************************/
771 - (void)windowWillClose:(NSNotification *)note
773 [(MainController *)controller closePreferences];
777 /*************************************************************************/
779 #pragma mark NSTableView DATASOURCE METHODS
780 /*************************************************************************/
782 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
784 if (aTableView == menuTableView) {
785 return [myItems count];
786 } else if (aTableView == allTableView) {
787 return [availableItems count];
788 } else if (aTableView == hotKeysTableView) {
789 return [hotKeysArray count];
791 return [[[NetworkController sharedController] remoteServices] count];
795 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
797 if (aTableView == menuTableView) {
798 NSString *object = [myItems objectAtIndex:rowIndex];
799 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
800 if ([object isEqualToString:@"showPlayer"]) {
803 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
805 [controller networkError:localException];
809 return NSLocalizedString(object, @"ERROR");
811 if ([submenuItems containsObject:object])
813 return [NSImage imageNamed:@"submenu"];
818 } else if (aTableView == allTableView) {
819 NSString *object = [availableItems objectAtIndex:rowIndex];
820 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
821 if ([object isEqualToString:@"showPlayer"]) {
824 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
826 [controller networkError:localException];
830 return NSLocalizedString(object, @"ERROR");
832 if ([submenuItems containsObject:object]) {
833 return [NSImage imageNamed:@"submenu"];
838 } else if (aTableView == hotKeysTableView) {
839 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
840 return [hotKeyNamesArray objectAtIndex:rowIndex];
842 return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
845 return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
849 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
851 if (tableView == menuTableView) {
852 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
853 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
857 if (tableView == allTableView) {
858 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
859 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
865 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
869 NSString *dragData, *temp;
871 pb = [info draggingPasteboard];
873 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
874 dragData = [pb stringForType:@"MenuTableViewPboardType"];
875 dragRow = [dragData intValue];
876 temp = [myItems objectAtIndex:dragRow];
878 if (tableView == menuTableView) {
879 [myItems insertObject:temp atIndex:row];
881 [myItems removeObjectAtIndex:dragRow];
883 [myItems removeObjectAtIndex:dragRow + 1];
885 } else if (tableView == allTableView) {
886 if (![temp isEqualToString:@"separator"]) {
887 [availableItems addObject:temp];
889 [myItems removeObjectAtIndex:dragRow];
891 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
892 dragData = [pb stringForType:@"AllTableViewPboardType"];
893 dragRow = [dragData intValue];
894 temp = [availableItems objectAtIndex:dragRow];
896 [myItems insertObject:temp atIndex:row];
898 if (![temp isEqualToString:@"separator"]) {
899 [availableItems removeObjectAtIndex:dragRow];
903 [menuTableView reloadData];
904 [allTableView reloadData];
905 [self changeMenus:self];
909 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
911 if (tableView == allTableView) {
912 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
913 return NSDragOperationNone;
916 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
917 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
918 if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
919 return NSDragOperationNone;
923 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
924 return NSDragOperationGeneric;
927 if (operation == NSTableViewDropOn || row == -1)
929 return NSDragOperationNone;
931 return NSDragOperationGeneric;
935 /*************************************************************************/
937 #pragma mark DEALLOCATION METHODS
938 /*************************************************************************/
942 [hotKeysArray release];
943 [hotKeysDictionary release];
944 [menuTableView setDataSource:nil];
945 [allTableView setDataSource:nil];
946 [controller release];
947 [availableItems release];
948 [submenuItems release];