// // PreferencesController.m // Top // // Created by Simon Brenner on 2008-01-01. // Copyright 2008 __MyCompanyName__. All rights reserved. // #import "PreferencesController.h" NSString *CDCPollRate=@"Poll Rate"; NSString *CDCStartAtLogin=@"Start At Login"; NSString *CDCPrefsChanged=@"CDCPrefsChanged"; @implementation PreferencesController -(id)init { if (self = [super initWithWindowNibName: @"Preferences"]) [self setWindowFrameAutosaveName:@"PrefWindow"]; return self; } -(void)windowDidLoad { [pollRateField setStringValue:[[NSUserDefaults standardUserDefaults] stringForKey:CDCPollRate]]; [startAtLoginButton setIntValue:[[[NSUserDefaults standardUserDefaults] stringForKey:CDCStartAtLogin] intValue]]; } -(IBAction)saveChanges:(id)sender { NSNotificationCenter *nc; nc=[NSNotificationCenter defaultCenter]; [[NSUserDefaults standardUserDefaults] setObject:[pollRateField stringValue] forKey:CDCPollRate]; [[NSUserDefaults standardUserDefaults] setObject:[startAtLoginButton stringValue] forKey:CDCStartAtLogin]; [[self window] performClose:nil]; [nc postNotificationName:CDCPrefsChanged object:self]; } -(void)windowWillClose:(NSNotification *)aNotification { [pollRateField abortEditing]; [pollRateField setStringValue:[[NSUserDefaults standardUserDefaults] stringForKey:CDCPollRate]]; } -(IBAction)setStartOnLogin:(id)sender { NSLog(@"%@\n",[startAtLoginButton stringValue]); BOOL startAtLogin = [startAtLoginButton intValue] ? 1 : 0; [self setMyAppInLoginItems: startAtLogin]; /*if([startAtLoginButton intValue]==1) { [self setMyAppInLoginItems:(BOOL)1]; } else { [self setMyAppInLoginItems:(BOOL)0]; }*/ } //Add or remove an application from one user's login items (YES to add, NO to remove) -(void)setMyAppInLoginItems:(BOOL)doAdd { // HACK, Don't add/remove from autostart until it's at least remotely usable // or stable return; // First, get the login items from loginwindow pref NSMutableArray* loginItems = (NSMutableArray*) CFPreferencesCopyValue((CFStringRef) @"AutoLaunchedApplicationDictionary", (CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); BOOL changed = NO, foundMyAppItem = NO; int myAppItemIndex = 0; NSString *kDTMyAppAppPath = [[NSBundle mainBundle] bundlePath]; if (loginItems) { NSEnumerator *enumer; NSDictionary *itemDict; // Detirmine if myApp is in list enumer=[loginItems objectEnumerator]; while (itemDict=[enumer nextObject]) { if ([[itemDict objectForKey:@"Path"] isEqualToString:kDTMyAppAppPath]) { foundMyAppItem = YES; break; } myAppItemIndex++; } } // If we're adding, we want to add if not found. If we're removing, we want to remove if found if (doAdd && !foundMyAppItem) { // OK, Create item and add it - should work even if no pref existed NSDictionary *myAppItem; FSRef myFSRef; OSStatus fsResult = FSPathMakeRef((const UInt8 *)[kDTMyAppAppPath fileSystemRepresentation], &myFSRef,NULL); if (loginItems) { loginItems = [[loginItems autorelease] mutableCopy]; // mutable copy we can work on, autorelease the original } else { loginItems = [[NSMutableArray alloc] init]; // didn't find this pref, make from scratch } // ref from path as NSString if (fsResult == noErr) { AliasHandle myAliasHndl = NULL; //make alias record, a handle of variable length fsResult = FSNewAlias(NULL, &myFSRef, &myAliasHndl); if (fsResult == noErr && myAliasHndl != NULL) { // Add the item myAppItem = [NSDictionary dictionaryWithObjectsAndKeys: [NSData dataWithBytes:*myAliasHndl length:GetHandleSize((Handle)myAliasHndl)], @"AliasData", [NSNumber numberWithBool:NO], @"Hide", kDTMyAppAppPath, @"Path", nil]; [loginItems addObject:myAppItem]; // release the new alias handle DisposeHandle((Handle)myAliasHndl); changed = YES; } } } else if (!doAdd && foundMyAppItem) { loginItems = [[loginItems autorelease] mutableCopy]; // mutable copy we can work on, autorelease the original [loginItems removeObjectAtIndex:myAppItemIndex]; // remove the MyApp item in the loginItems array changed=YES; } if (changed) { // Set new value in pref CFPreferencesSetValue((CFStringRef) @"AutoLaunchedApplicationDictionary", loginItems, (CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); } [loginItems release]; } @end