Core/Source/GrowlPreferencesController.m
changeset 4270 5c1d8f5e64c2
parent 4261 48b7c994f6c8
child 4273 b846a7b597a6
     1.1 --- a/Core/Source/GrowlPreferencesController.m	Sat Aug 01 20:50:32 2009 -0400
     1.2 +++ b/Core/Source/GrowlPreferencesController.m	Sun Aug 02 13:03:53 2009 -0700
     1.3 @@ -60,12 +60,14 @@
     1.4  															selector:@selector(growlPreferencesChanged:)
     1.5  																name:GrowlPreferencesChanged
     1.6  															  object:nil];
     1.7 +		loginItems = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, /*options*/ NULL);
     1.8  	}
     1.9  	return self;
    1.10  }
    1.11  
    1.12  - (void) destroy {
    1.13  	[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
    1.14 +	CFRelease(loginItems);
    1.15  
    1.16  	[super destroy];
    1.17  }
    1.18 @@ -158,24 +160,28 @@
    1.19  #pragma mark Start-at-login control
    1.20  
    1.21  - (BOOL) shouldStartGrowlAtLogin {
    1.22 -	OSStatus   status;
    1.23  	Boolean    foundIt = false;
    1.24 -	CFArrayRef loginItems = NULL;
    1.25  
    1.26  	//get the prefpane bundle and find GHA within it.
    1.27  	NSString *pathToGHA      = [[NSBundle bundleWithIdentifier:GROWL_PREFPANE_BUNDLE_IDENTIFIER] pathForResource:@"GrowlHelperApp" ofType:@"app"];
    1.28  	//get the file url to GHA.
    1.29  	CFURLRef urlToGHA = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)pathToGHA, kCFURLPOSIXPathStyle, true);
    1.30  
    1.31 -	status = LIAECopyLoginItems(&loginItems);
    1.32 -	if (status == noErr) {
    1.33 -		for (CFIndex i=0, count=CFArrayGetCount(loginItems); i<count; ++i) {
    1.34 -			CFDictionaryRef loginItem = CFArrayGetValueAtIndex(loginItems, i);
    1.35 -			foundIt = CFEqual(CFDictionaryGetValue(loginItem, kLIAEURL), urlToGHA);
    1.36 +	UInt32 seed = 0U;
    1.37 +	NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
    1.38 +	for (id itemObject in currentLoginItems) {
    1.39 +		LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
    1.40 +
    1.41 +		UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
    1.42 +		CFURLRef URL = NULL;
    1.43 +		OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);
    1.44 +		if (err == noErr) {
    1.45 +			foundIt = CFEqual(URL, urlToGHA);
    1.46 +			CFRelease(URL);
    1.47 +
    1.48  			if (foundIt)
    1.49  				break;
    1.50  		}
    1.51 -		CFRelease(loginItems);
    1.52  	}
    1.53  
    1.54  	CFRelease(urlToGHA);
    1.55 @@ -191,31 +197,47 @@
    1.56  
    1.57  - (void) setStartAtLogin:(NSString *)path enabled:(BOOL)enabled {
    1.58  	OSStatus status;
    1.59 -	CFArrayRef loginItems = NULL;
    1.60 -	NSURL *url = [NSURL fileURLWithPath:path];
    1.61 -	NSInteger existingLoginItemIndex = -1;
    1.62 -
    1.63 -	status = LIAECopyLoginItems(&loginItems);
    1.64 -
    1.65 -	if (status == noErr) {
    1.66 -		NSEnumerator *enumerator = [(NSArray *)loginItems objectEnumerator];
    1.67 -		NSDictionary *loginItemDict;
    1.68 -
    1.69 -		while ((loginItemDict = [enumerator nextObject])) {
    1.70 -			if ([[loginItemDict objectForKey:(NSString *)kLIAEURL] isEqual:url]) {
    1.71 -				existingLoginItemIndex = [(NSArray *)loginItems indexOfObjectIdenticalTo:loginItemDict];
    1.72 +	CFURLRef URLToToggle = (CFURLRef)[NSURL fileURLWithPath:path];
    1.73 +	LSSharedFileListItemRef existingItem = NULL;
    1.74 +
    1.75 +	UInt32 seed = 0U;
    1.76 +	NSArray *currentLoginItems = [NSMakeCollectable(LSSharedFileListCopySnapshot(loginItems, &seed)) autorelease];
    1.77 +	for (id itemObject in currentLoginItems) {
    1.78 +		LSSharedFileListItemRef item = (LSSharedFileListItemRef)itemObject;
    1.79 +
    1.80 +		UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
    1.81 +		CFURLRef URL = NULL;
    1.82 +		OSStatus err = LSSharedFileListItemResolve(item, resolutionFlags, &URL, /*outRef*/ NULL);
    1.83 +		if (err == noErr) {
    1.84 +			Boolean foundIt = CFEqual(URL, URLToToggle);
    1.85 +			CFRelease(URL);
    1.86 +
    1.87 +			if (foundIt) {
    1.88 +				existingItem = item;
    1.89  				break;
    1.90  			}
    1.91  		}
    1.92  	}
    1.93  
    1.94 -	if (enabled && (existingLoginItemIndex == -1))
    1.95 -		LIAEAddURLAtEnd((CFURLRef)url, false);
    1.96 -	else if (!enabled && (existingLoginItemIndex != -1))
    1.97 -		LIAERemove(existingLoginItemIndex);
    1.98 -
    1.99 -	if(loginItems)
   1.100 -		CFRelease(loginItems);
   1.101 +	if (enabled && (existingItem == NULL)) {
   1.102 +		NSString *displayName = [[NSFileManager defaultManager] displayNameAtPath:path];
   1.103 +		IconRef icon = NULL;
   1.104 +		FSRef ref;
   1.105 +		Boolean gotRef = CFURLGetFSRef(URLToToggle, &ref);
   1.106 +		if (gotRef) {
   1.107 +			status = GetIconRefFromFileInfo(&ref,
   1.108 +											/*fileNameLength*/ 0, /*fileName*/ NULL,
   1.109 +											kFSCatInfoNone, /*catalogInfo*/ NULL,
   1.110 +											kIconServicesNormalUsageFlag,
   1.111 +											&icon,
   1.112 +											/*outLabel*/ NULL);
   1.113 +			if (status != noErr)
   1.114 +				icon = NULL;
   1.115 +		}
   1.116 +
   1.117 +		LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst, (CFStringRef)displayName, icon, URLToToggle, /*propertiesToSet*/ NULL, /*propertiesToClear*/ NULL);
   1.118 +	} else if (!enabled && (existingItem != NULL))
   1.119 +		LSSharedFileListItemRemove(loginItems, existingItem);
   1.120  }
   1.121  
   1.122  #pragma mark -