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];
106 /*************************************************************************/
108 #pragma mark ACCESSOR METHODS
109 /*************************************************************************/
116 - (void)setController:(id)object
118 [controller autorelease];
119 controller = [object retain];
123 /*************************************************************************/
125 #pragma mark INSTANCE METHODS
126 /*************************************************************************/
128 - (IBAction)showPrefsWindow:(id)sender
130 ITDebugLog(@"Showing preferences window.");
131 if (! window) { // If window does not exist yet, then the nib hasn't been loaded.
132 ITDebugLog(@"Window doesn't exist, initial setup.");
133 [self setupWindow]; // Load in the nib, and perform any initial setup.
134 [self setupCustomizationTables]; // Setup the DnD manu config tables.
135 [self setupMenuItems]; // Setup the arrays of menu items
136 [self setupUI]; // Sets up additional UI
137 [window setDelegate:self];
138 [menuTableView reloadData];
139 [hotKeysTableView setDoubleAction:@selector(hotKeysTableViewDoubleClicked:)];
141 //Change the launch player checkbox to the proper name
143 [launchPlayerAtLaunchCheckbox setTitle:[NSString stringWithFormat:@"Launch %@ when MenuTunes launches", [[controller currentRemote] playerSimpleName]]]; //This isn't localized...
145 [controller networkError:localException];
150 [NSApp activateIgnoringOtherApps:YES];
151 [window performSelector:@selector(makeKeyAndOrderFront:) withObject:self afterDelay:0.0];
154 - (IBAction)changeGeneralSetting:(id)sender
156 ITDebugLog(@"Changing general setting of tag %i.", [sender tag]);
157 if ( [sender tag] == 1010) {
158 [self setLaunchesAtLogin:SENDER_STATE];
159 } else if ( [sender tag] == 1020) {
160 [df setBool:SENDER_STATE forKey:@"LaunchPlayerWithMT"];
161 } else if ( [sender tag] == 1030) {
162 [df setInteger:[sender intValue] forKey:@"SongsInAdvance"];
164 } else if ( [sender tag] == 1040) {
165 // This will not be executed. Song info always shows the title of the song.
166 // [df setBool:SENDER_STATE forKey:@"showName"];
167 } else if ( [sender tag] == 1050) {
168 [df setBool:SENDER_STATE forKey:@"showArtist"];
169 } else if ( [sender tag] == 1060) {
170 [df setBool:SENDER_STATE forKey:@"showAlbum"];
171 } else if ( [sender tag] == 1070) {
172 [df setBool:SENDER_STATE forKey:@"showTime"];
173 } else if ( [sender tag] == 1080) {
174 [df setBool:SENDER_STATE forKey:@"showTrackNumber"];
175 } else if ( [sender tag] == 1090) {
176 [df setBool:SENDER_STATE forKey:@"showTrackRating"];
181 - (IBAction)changeSharingSetting:(id)sender
183 ITDebugLog(@"Changing sharing setting of tag %i.", [sender tag]);
184 if ( [sender tag] == 5010 ) {
185 BOOL state = SENDER_STATE;
186 [df setBool:state forKey:@"enableSharing"];
187 //Disable/enable the use of shared player options
188 [useSharedMenuTunesCheckbox setEnabled:!state];
189 [usePasswordCheckbox setEnabled:state];
190 [passwordTextField setEnabled:state];
191 [nameTextField setEnabled:state];
192 [selectSharedPlayerButton setEnabled:NO];
193 [controller setServerStatus:state]; //Set server status
194 } else if ( [sender tag] == 5015 ) {
195 [df setObject:[sender stringValue] forKey:@"sharedPlayerName"];
196 } else if ( [sender tag] == 5020 ) {
197 [df setBool:SENDER_STATE forKey:@"enableSharingPassword"];
198 } else if ( [sender tag] == 5030 ) {
199 //Set the server password
200 const char *instring = [[sender stringValue] UTF8String];
201 const char *password = "password";
202 unsigned char *result;
203 NSData *hashedPass, *passwordStringHash;
204 result = SHA1(instring, strlen(instring), NULL);
205 hashedPass = [NSData dataWithBytes:result length:strlen(result)];
206 result = SHA1(password, strlen(password), NULL);
207 passwordStringHash = [NSData dataWithBytes:result length:strlen(result)];
208 if (![hashedPass isEqualToData:passwordStringHash]) {
209 [df setObject:hashedPass forKey:@"sharedPlayerPassword"];
210 [sender setStringValue:@"password"];
212 } else if ( [sender tag] == 5040 ) {
213 BOOL state = SENDER_STATE;
214 [df setBool:state forKey:@"useSharedPlayer"];
215 //Disable/enable the use of sharing options
216 [shareMenuTunesCheckbox setEnabled:!state];
217 [usePasswordCheckbox setEnabled:NO];
218 [passwordTextField setEnabled:NO];
219 [nameTextField setEnabled:NO];
220 [selectSharedPlayerButton setEnabled:state];
223 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
224 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
225 [controller connectToServer];
227 [selectedPlayerTextField setStringValue:@"No shared player selected."];
228 [locationTextField setStringValue:@"-"];
229 [controller disconnectFromServer];
232 } else if ( [sender tag] == 5050 ) {
233 //Do nothing on table view click
234 } else if ( [sender tag] == 5051 ) {
235 [df setObject:[sender stringValue] forKey:@"sharedPlayerHost"];
236 } else if ( [sender tag] == 5060 ) {
237 //Show selection sheet
238 [NSApp beginSheet:selectPlayerSheet modalForWindow:window modalDelegate:self didEndSelector:NULL contextInfo:nil];
239 } else if ( [sender tag] == 5100 ) {
241 if ( ([sender indexOfItem:[sender selectedItem]] == 0) && ([selectPlayerBox contentView] != zeroConfView) ) {
242 NSRect frame = [selectPlayerSheet frame];
243 frame.origin.y -= 58;
244 frame.size.height = 273;
245 [selectPlayerBox setContentView:zeroConfView];
246 [selectPlayerSheet setFrame:frame display:YES animate:YES];
247 } else if ( ([sender indexOfItem:[sender selectedItem]] == 1) && ([selectPlayerBox contentView] != manualView) ){
248 NSRect frame = [selectPlayerSheet frame];
249 frame.origin.y += 58;
250 frame.size.height = 215;
251 //[window makeFirstResponder:hostTextField];
252 [selectPlayerBox setContentView:manualView];
253 [selectPlayerSheet setFrame:frame display:YES animate:YES];
254 [hostTextField selectText:nil];
256 } else if ( [sender tag] == 5150 ) {
257 const char *instring = [[sender stringValue] UTF8String];
258 unsigned char *result;
259 result = SHA1(instring, strlen(instring), NULL);
260 [df setObject:[NSData dataWithBytes:result length:strlen(result)] forKey:@"connectPassword"];
261 } else if ( [sender tag] == 5110 ) {
263 [NSApp endSheet:selectPlayerSheet];
264 [selectPlayerSheet orderOut:nil];
265 if ([selectPlayerBox contentView] == manualView) {
266 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
269 } else if ( [sender tag] == 5120 ) {
271 [NSApp endSheet:selectPlayerSheet];
272 [selectPlayerSheet orderOut:nil];
274 [self changeSharingSetting:clientPasswordTextField];
276 if ([selectPlayerBox contentView] == manualView) {
277 [df setObject:[hostTextField stringValue] forKey:@"sharedPlayerHost"];
279 if ([sharingTableView selectedRow] > -1) {
280 [df setObject:[NSString stringWithCString:inet_ntoa((*(struct sockaddr_in*)[[[[[[NetworkController sharedController] remoteServices] objectAtIndex:[sharingTableView selectedRow]] addresses] objectAtIndex:0] bytes]).sin_addr)] forKey:@"sharedPlayerHost"];
284 if ([controller connectToServer]) {
285 [useSharedMenuTunesCheckbox setState:NSOnState];
286 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
287 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
289 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);
295 - (IBAction)changeStatusWindowSetting:(id)sender
297 StatusWindow *sw = [StatusWindow sharedWindow];
298 ITDebugLog(@"Changing status window setting of tag %i", [sender tag]);
299 if ( [sender tag] == 2010) {
300 [df setInteger:[sender selectedRow] forKey:@"statusWindowVerticalPosition"];
301 [df setInteger:[sender selectedColumn] forKey:@"statusWindowHorizontalPosition"];
302 // update the window's position here
303 } else if ( [sender tag] == 2020) {
304 // update screen selection
305 } else if ( [sender tag] == 2030) {
306 int effectTag = [[sender selectedItem] tag];
307 float time = ([df floatForKey:@"statusWindowAppearanceSpeed"] ? [df floatForKey:@"statusWindowAppearanceSpeed"] : 0.8);
308 [df setInteger:effectTag forKey:@"statusWindowAppearanceEffect"];
310 if ( effectTag == 2100 ) {
311 [sw setEntryEffect:[[[ITCutWindowEffect alloc] initWithWindow:sw] autorelease]];
312 } else if ( effectTag == 2101 ) {
313 [sw setEntryEffect:[[[ITDissolveWindowEffect alloc] initWithWindow:sw] autorelease]];
314 } else if ( effectTag == 2102 ) {
315 [sw setEntryEffect:[[[ITSlideVerticallyWindowEffect alloc] initWithWindow:sw] autorelease]];
316 } else if ( effectTag == 2103 ) {
317 [sw setEntryEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
318 } else if ( effectTag == 2104 ) {
319 [sw setEntryEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
322 [[sw entryEffect] setEffectTime:time];
324 } else if ( [sender tag] == 2040) {
325 int effectTag = [[sender selectedItem] tag];
326 float time = ([df floatForKey:@"statusWindowVanishSpeed"] ? [df floatForKey:@"statusWindowVanishSpeed"] : 0.8);
328 [df setInteger:[[sender selectedItem] tag] forKey:@"statusWindowVanishEffect"];
330 if ( effectTag == 2100 ) {
331 [sw setExitEffect:[[[ITCutWindowEffect alloc] initWithWindow:sw] autorelease]];
332 } else if ( effectTag == 2101 ) {
333 [sw setExitEffect:[[[ITDissolveWindowEffect alloc] initWithWindow:sw] autorelease]];
334 } else if ( effectTag == 2102 ) {
335 [sw setExitEffect:[[[ITSlideVerticallyWindowEffect alloc] initWithWindow:sw] autorelease]];
336 } else if ( effectTag == 2103 ) {
337 [sw setExitEffect:[[[ITSlideHorizontallyWindowEffect alloc] initWithWindow:sw] autorelease]];
338 } else if ( effectTag == 2104 ) {
339 [sw setExitEffect:[[[ITPivotWindowEffect alloc] initWithWindow:sw] autorelease]];
342 [[sw exitEffect] setEffectTime:time];
344 } else if ( [sender tag] == 2050) {
345 float newTime = (-([sender floatValue]));
346 [df setFloat:newTime forKey:@"statusWindowAppearanceSpeed"];
347 [[sw entryEffect] setEffectTime:newTime];
348 } else if ( [sender tag] == 2060) {
349 float newTime = (-([sender floatValue]));
350 [df setFloat:newTime forKey:@"statusWindowVanishSpeed"];
351 [[sw exitEffect] setEffectTime:newTime];
352 } else if ( [sender tag] == 2070) {
353 [df setFloat:[sender floatValue] forKey:@"statusWindowVanishDelay"];
354 [sw setExitDelay:[sender floatValue]];
355 } else if ( [sender tag] == 2080) {
356 [df setBool:SENDER_STATE forKey:@"showSongInfoOnChange"];
361 - (void)registerDefaults
364 NSMutableDictionary *loginWindow;
365 NSMutableArray *loginArray;
366 NSEnumerator *loginEnum;
368 ITDebugLog(@"Registering defaults.");
369 [df setObject:[NSArray arrayWithObjects:
381 nil] forKey:@"menu"];
383 [df setInteger:5 forKey:@"SongsInAdvance"];
384 // [df setBool:YES forKey:@"showName"]; // Song info will always show song title.
385 [df setBool:YES forKey:@"showArtist"];
386 [df setBool:NO forKey:@"showAlbum"];
387 [df setBool:NO forKey:@"showTime"];
389 [df setInteger:2100 forKey:@"statusWindowAppearanceEffect"];
390 [df setInteger:2101 forKey:@"statusWindowVanishEffect"];
391 [df setFloat:0.8 forKey:@"statusWindowAppearanceSpeed"];
392 [df setFloat:0.8 forKey:@"statusWindowVanishSpeed"];
393 [df setFloat:4.0 forKey:@"statusWindowVanishDelay"];
394 [df setBool:YES forKey:@"showSongInfoOnChange"];
398 loginWindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
399 loginArray = [loginWindow objectForKey:@"AutoLaunchedApplicationDictionary"];
400 loginEnum = [loginArray objectEnumerator];
402 while ( (anItem = [loginEnum nextObject]) ) {
403 if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]] ) {
407 [loginWindow release];
410 [[StatusWindowController sharedController] showSetupQueryWindow];
416 [[StatusWindow sharedWindow] setLocked:NO];
417 [[StatusWindow sharedWindow] vanish:self];
418 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
420 [self setLaunchesAtLogin:YES];
423 - (void)autoLaunchCancel
425 [[StatusWindow sharedWindow] setLocked:NO];
426 [[StatusWindow sharedWindow] vanish:self];
427 [[StatusWindow sharedWindow] setIgnoresMouseEvents:YES];
430 - (void)deletePressedInTableView:(NSTableView *)tableView
432 if (tableView == menuTableView) {
433 int selRow = [tableView selectedRow];
434 ITDebugLog(@"Delete pressed in menu table view.");
436 NSString *object = [myItems objectAtIndex:selRow];
438 if ([object isEqualToString:@"preferences"]) {
443 if (![object isEqualToString:@"separator"])
444 [availableItems addObject:object];
445 ITDebugLog(@"Removing object named %@", object);
446 [myItems removeObjectAtIndex:selRow];
447 [menuTableView reloadData];
448 [allTableView reloadData];
450 [self changeMenus:self];
454 /*************************************************************************/
456 #pragma mark HOTKEY SUPPORT METHODS
457 /*************************************************************************/
459 - (IBAction)clearHotKey:(id)sender
461 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
462 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation] forKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]];
463 [controller setupHotKeys];
464 [hotKeysTableView reloadData];
467 - (IBAction)editHotKey:(id)sender
469 ITKeyComboPanel *panel = [ITKeyComboPanel sharedPanel];
470 NSString *keyComboKey = [hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]];
471 ITKeyCombo *keyCombo;
473 ITDebugLog(@"Setting key combo on hot key %@.", keyComboKey);
474 [controller clearHotKeys];
475 [panel setKeyCombo:[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:[hotKeysTableView selectedRow]]]];
476 [panel setKeyBindingName:[hotKeyNamesArray objectAtIndex:[hotKeysTableView selectedRow]]];
477 if ([panel runModal] == NSOKButton) {
478 NSEnumerator *keyEnumerator = [[hotKeysDictionary allKeys] objectEnumerator];
480 keyCombo = [panel keyCombo];
482 //Check for duplicate key combo
483 while ( (nextKey = [keyEnumerator nextObject]) ) {
484 if ([[hotKeysDictionary objectForKey:nextKey] isEqual:keyCombo] &&
485 ![keyCombo isEqual:[ITKeyCombo clearKeyCombo]]) {
486 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo]
488 [df setObject:[[ITKeyCombo clearKeyCombo] plistRepresentation]
493 [hotKeysDictionary setObject:keyCombo forKey:keyComboKey];
494 [df setObject:[keyCombo plistRepresentation] forKey:keyComboKey];
495 [controller setupHotKeys];
496 [hotKeysTableView reloadData];
497 ITDebugLog(@"Set combo %@ on hot key %@.", keyCombo, keyComboKey);
499 ITDebugLog(@"Hot key setting on hot key %@ cancelled.", keyComboKey);
503 - (void)hotKeysTableViewDoubleClicked:(id)sender
505 if ([sender clickedRow] > -1) {
506 [self editHotKey:sender];
510 /*************************************************************************/
512 #pragma mark PRIVATE METHOD IMPLEMENTATIONS
513 /*************************************************************************/
517 ITDebugLog(@"Loading Preferences.nib.");
518 if (![NSBundle loadNibNamed:@"Preferences" owner:self]) {
519 ITDebugLog(@"Failed to load Preferences.nib.");
525 - (void)setupCustomizationTables
527 NSImageCell *imgCell = [[[NSImageCell alloc] initImageCell:nil] autorelease];
528 ITDebugLog(@"Setting up table views.");
529 // Set the table view cells up
530 [imgCell setImageScaling:NSScaleNone];
531 [[menuTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
532 [[allTableView tableColumnWithIdentifier:@"submenu"] setDataCell:imgCell];
534 // Register for drag and drop
535 [menuTableView registerForDraggedTypes:[NSArray arrayWithObjects:
536 @"MenuTableViewPboardType",
537 @"AllTableViewPboardType",
539 [allTableView registerForDraggedTypes:[NSArray arrayWithObjects:
540 @"MenuTableViewPboardType",
541 @"AllTableViewPboardType",
545 - (void)setupMenuItems
547 NSEnumerator *itemEnum;
549 ITDebugLog(@"Setting up table view arrays.");
550 // Set the list of items you can have.
551 availableItems = [[NSMutableArray alloc] initWithObjects:
567 // Get our preferred menu
568 myItems = [[df arrayForKey:@"menu"] mutableCopy];
570 // Delete items in the availableItems array that are already part of the menu
571 itemEnum = [myItems objectEnumerator];
572 while ( (anItem = [itemEnum nextObject]) ) {
573 if (![anItem isEqualToString:@"separator"]) {
574 [availableItems removeObject:anItem];
578 // Items that show should a submenu image
579 submenuItems = [[NSArray alloc] initWithObjects:
589 NSMutableDictionary *loginwindow;
590 NSMutableArray *loginarray;
591 NSEnumerator *loginEnum, *keyArrayEnum;
592 NSString *serverName;
595 ITDebugLog(@"Setting up preferences UI.");
596 // Fill in the number of songs in advance to show field
597 [songsInAdvance setIntValue:[df integerForKey:@"SongsInAdvance"]];
599 // Fill hot key array
600 keyArrayEnum = [hotKeysArray objectEnumerator];
602 while ( (anItem = [keyArrayEnum nextObject]) ) {
603 if ([df objectForKey:anItem]) {
604 ITDebugLog(@"Setting up \"%@\" hot key.", anItem);
605 [hotKeysDictionary setObject:[ITKeyCombo keyComboWithPlistRepresentation:[df objectForKey:anItem]] forKey:anItem];
607 [hotKeysDictionary setObject:[ITKeyCombo clearKeyCombo] forKey:anItem];
611 ITDebugLog(@"Setting up track info checkboxes.");
612 // Check current track info buttons
613 [albumCheckbox setState:[df boolForKey:@"showAlbum"] ? NSOnState : NSOffState];
614 [nameCheckbox setState:NSOnState]; // Song info will ALWAYS show song title.
615 [nameCheckbox setEnabled:NO]; // Song info will ALWAYS show song title.
616 [artistCheckbox setState:[df boolForKey:@"showArtist"] ? NSOnState : NSOffState];
617 [trackTimeCheckbox setState:[df boolForKey:@"showTime"] ? NSOnState : NSOffState];
618 [trackNumberCheckbox setState:[df boolForKey:@"showTrackNumber"] ? NSOnState : NSOffState];
619 [ratingCheckbox setState:[df boolForKey:@"showTrackRating"] ? NSOnState : NSOffState];
621 // Set the launch at login checkbox state
622 ITDebugLog(@"Setting launch at login state.");
624 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
625 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
627 loginEnum = [loginarray objectEnumerator];
628 while ( (anItem = [loginEnum nextObject]) ) {
629 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
630 [launchAtLoginCheckbox setState:NSOnState];
634 // Set the launch player checkbox state
635 ITDebugLog(@"Setting launch player with MenuTunes state.");
636 [launchPlayerAtLaunchCheckbox setState:[df boolForKey:@"LaunchPlayerWithMT"] ? NSOnState : NSOffState];
638 // Setup the positioning controls
640 // Setup effects controls
641 [appearanceEffectPopup selectItem:[appearanceEffectPopup itemAtIndex:[appearanceEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowAppearanceEffect"]]]];
642 [vanishEffectPopup selectItem:[vanishEffectPopup itemAtIndex:[vanishEffectPopup indexOfItemWithTag:[df integerForKey:@"statusWindowVanishEffect"]]]];
643 [appearanceSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowAppearanceSpeed"])];
644 [vanishSpeedSlider setFloatValue:-([df floatForKey:@"statusWindowVanishSpeed"])];
645 [vanishDelaySlider setFloatValue:[df floatForKey:@"statusWindowVanishDelay"]];
646 [showOnChangeCheckbox setState:([df boolForKey:@"showSongInfoOnChange"] ? NSOnState : NSOffState)];
648 // Setup the sharing controls
649 if ([df boolForKey:@"enableSharing"]) {
650 [shareMenuTunesCheckbox setState:NSOnState];
651 [useSharedMenuTunesCheckbox setEnabled:NO];
652 [selectSharedPlayerButton setEnabled:NO];
653 [passwordTextField setEnabled:YES];
654 [usePasswordCheckbox setEnabled:YES];
655 [nameTextField setEnabled:YES];
656 } else if ([df boolForKey:@"useSharedPlayer"]) {
657 [useSharedMenuTunesCheckbox setState:NSOnState];
658 [shareMenuTunesCheckbox setEnabled:NO];
659 [selectSharedPlayerButton setEnabled:YES];
662 [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil];
664 serverName = [df stringForKey:@"sharedPlayerName"];
665 if (!serverName || [serverName length] == 0) {
666 serverName = @"MenuTunes Shared Player";
668 [nameTextField setStringValue:serverName];
670 [selectPlayerBox setContentView:zeroConfView];
671 [usePasswordCheckbox setState:([df boolForKey:@"enableSharingPassword"] ? NSOnState : NSOffState)];
672 if ([df dataForKey:@"sharedPlayerPassword"]) {
673 [passwordTextField setStringValue:@"password"];
675 if ([df stringForKey:@"sharedPlayerHost"]) {
676 [hostTextField setStringValue:[df stringForKey:@"sharedPlayerHost"]];
679 if ([[NetworkController sharedController] isConnectedToServer]) {
680 [selectedPlayerTextField setStringValue:[[[NetworkController sharedController] networkObject] serverName]];
681 [locationTextField setStringValue:[[NetworkController sharedController] remoteHost]];
683 [selectedPlayerTextField setStringValue:@"No shared player selected."];
684 [locationTextField setStringValue:@"-"];
688 - (IBAction)changeMenus:(id)sender
690 ITDebugLog(@"Synchronizing menus");
691 [df setObject:myItems forKey:@"menu"];
695 - (void)setLaunchesAtLogin:(BOOL)flag
697 NSMutableDictionary *loginwindow;
698 NSMutableArray *loginarray;
699 ITDebugLog(@"Setting launches at login: %i", flag);
701 loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
702 loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
705 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
706 [[NSBundle mainBundle] bundlePath], @"Path",
707 [NSNumber numberWithInt:0], @"Hide", nil];
708 [loginarray addObject:itemDict];
711 for (i = 0; i < [loginarray count]; i++) {
712 NSDictionary *tempDict = [loginarray objectAtIndex:i];
713 if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
714 [loginarray removeObjectAtIndex:i];
719 [df setPersistentDomain:loginwindow forName:@"loginwindow"];
721 [loginwindow release];
722 ITDebugLog(@"Finished setting launches at login.");
726 /*************************************************************************/
728 #pragma mark NSWindow DELEGATE METHODS
729 /*************************************************************************/
731 - (void)windowWillClose:(NSNotification *)note
733 [(MainController *)controller closePreferences];
737 /*************************************************************************/
739 #pragma mark NSTableView DATASOURCE METHODS
740 /*************************************************************************/
742 - (int)numberOfRowsInTableView:(NSTableView *)aTableView
744 if (aTableView == menuTableView) {
745 return [myItems count];
746 } else if (aTableView == allTableView) {
747 return [availableItems count];
748 } else if (aTableView == hotKeysTableView) {
749 return [hotKeysArray count];
751 return [[[NetworkController sharedController] remoteServices] count];
755 - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
757 if (aTableView == menuTableView) {
758 NSString *object = [myItems objectAtIndex:rowIndex];
759 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
760 if ([object isEqualToString:@"showPlayer"]) {
763 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
765 [controller networkError:localException];
769 return NSLocalizedString(object, @"ERROR");
771 if ([submenuItems containsObject:object])
773 return [NSImage imageNamed:@"submenu"];
778 } else if (aTableView == allTableView) {
779 NSString *object = [availableItems objectAtIndex:rowIndex];
780 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
781 if ([object isEqualToString:@"showPlayer"]) {
784 string = [NSString stringWithFormat:@"%@ %@", NSLocalizedString(@"show", @"Show"), [[controller currentRemote] playerSimpleName]];
786 [controller networkError:localException];
790 return NSLocalizedString(object, @"ERROR");
792 if ([submenuItems containsObject:object]) {
793 return [NSImage imageNamed:@"submenu"];
798 } else if (aTableView == hotKeysTableView) {
799 if ([[aTableColumn identifier] isEqualToString:@"name"]) {
800 return [hotKeyNamesArray objectAtIndex:rowIndex];
802 return [[hotKeysDictionary objectForKey:[hotKeysArray objectAtIndex:rowIndex]] description];
805 return [[[[NetworkController sharedController] remoteServices] objectAtIndex:rowIndex] name];
809 - (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray*)rows toPasteboard:(NSPasteboard*)pboard
811 if (tableView == menuTableView) {
812 [pboard declareTypes:[NSArray arrayWithObjects:@"MenuTableViewPboardType", nil] owner:self];
813 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"MenuTableViewPboardType"];
817 if (tableView == allTableView) {
818 [pboard declareTypes:[NSArray arrayWithObjects:@"AllTableViewPboardType", nil] owner:self];
819 [pboard setString:[[rows objectAtIndex:0] stringValue] forType:@"AllTableViewPboardType"];
825 - (BOOL)tableView:(NSTableView*)tableView acceptDrop:(id <NSDraggingInfo>)info row:(int)row dropOperation:(NSTableViewDropOperation)operation
829 NSString *dragData, *temp;
831 pb = [info draggingPasteboard];
833 if ([[pb types] containsObject:@"MenuTableViewPboardType"]) {
834 dragData = [pb stringForType:@"MenuTableViewPboardType"];
835 dragRow = [dragData intValue];
836 temp = [myItems objectAtIndex:dragRow];
838 if (tableView == menuTableView) {
839 [myItems insertObject:temp atIndex:row];
841 [myItems removeObjectAtIndex:dragRow];
843 [myItems removeObjectAtIndex:dragRow + 1];
845 } else if (tableView == allTableView) {
846 if (![temp isEqualToString:@"separator"]) {
847 [availableItems addObject:temp];
849 [myItems removeObjectAtIndex:dragRow];
851 } else if ([[pb types] containsObject:@"AllTableViewPboardType"]) {
852 dragData = [pb stringForType:@"AllTableViewPboardType"];
853 dragRow = [dragData intValue];
854 temp = [availableItems objectAtIndex:dragRow];
856 [myItems insertObject:temp atIndex:row];
858 if (![temp isEqualToString:@"separator"]) {
859 [availableItems removeObjectAtIndex:dragRow];
863 [menuTableView reloadData];
864 [allTableView reloadData];
865 [self changeMenus:self];
869 - (NSDragOperation)tableView:(NSTableView*)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(int)row proposedDropOperation:(NSTableViewDropOperation)operation
871 if (tableView == allTableView) {
872 if ([[[info draggingPasteboard] types] containsObject:@"AllTableViewPboardType"]) {
873 return NSDragOperationNone;
876 if ([[[info draggingPasteboard] types] containsObject:@"MenuTableViewPboardType"]) {
877 NSString *item = [myItems objectAtIndex:[[[info draggingPasteboard] stringForType:@"MenuTableViewPboardType"] intValue]];
878 if ([item isEqualToString:@"preferences"] || [item isEqualToString:@"quit"]) {
879 return NSDragOperationNone;
883 [tableView setDropRow:-1 dropOperation:NSTableViewDropOn];
884 return NSDragOperationGeneric;
887 if (operation == NSTableViewDropOn || row == -1)
889 return NSDragOperationNone;
891 return NSDragOperationGeneric;
895 /*************************************************************************/
897 #pragma mark DEALLOCATION METHODS
898 /*************************************************************************/
902 [hotKeysArray release];
903 [hotKeysDictionary release];
904 [menuTableView setDataSource:nil];
905 [allTableView setDataSource:nil];
906 [controller release];
907 [availableItems release];
908 [submenuItems release];