X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/843b18fea32ec5481a9f64a4da67d11b00f4cf35..82f3d7a2c51dd64ff663cdef7399d50cad4cca6b:/ITLoginItem.m?ds=inline diff --git a/ITLoginItem.m b/ITLoginItem.m index ec2e582..05a16b0 100755 --- a/ITLoginItem.m +++ b/ITLoginItem.m @@ -10,43 +10,70 @@ #import "ITLoginItem.h" #import "ITDebug.h" -BOOL ITSetLaunchApplicationOnLogin(NSString *path, BOOL flag) +void ITSetApplicationLaunchOnLogin(NSString *path, BOOL flag) { + if ( (flag && ITDoesApplicationLaunchOnLogin(path)) || ![[NSFileManager defaultManager] fileExistsAtPath:path] ) { + return; + } NSUserDefaults *df = [NSUserDefaults standardUserDefaults]; NSMutableDictionary *loginwindow; NSMutableArray *loginarray; - FSRef fileRef; - AliasHandle alias; - NSData *aliasData; - ITDebugLog(@"Set if MenuPrefs launches at login to %i.", flag); + ITDebugLog(@"Set if \"%@\" launches at login to %i.", path, flag); [df synchronize]; loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy]; - loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"]; - - //Create the alias data - FSPathMakeRef([path UTF8String], &fileRef, NULL); - FSNewAlias(NULL, &fileRef, &alias); - aliasData = [NSData dataWithBytes:&alias length:sizeof(alias)]; + loginarray = [[loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy]; if (flag) { + /*FSRef fileRef; + AliasHandle alias; + NSData *aliasData; + FSPathMakeRef([path UTF8String], &fileRef, NULL); + FSNewAlias(NULL, &fileRef, &alias); + aliasData = [NSData dataWithBytes:&alias length:GetHandleSize((Handle)alias)];*/ + + if (!loginarray) { //If there is no loginarray of autolaunch items, create one + loginarray = [[[NSMutableArray alloc] init] autorelease]; + } NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys: - [[NSBundle mainBundle] bundlePath], @"Path", - [NSNumber numberWithInt:0], @"Hide", - aliasData, @"AliasData", nil, nil]; + [[NSBundle mainBundle] bundlePath], @"Path", + [NSNumber numberWithInt:0], @"Hide", + [NSData data], @"AliasData", nil, nil]; [loginarray addObject:itemDict]; } else { int i; for (i = 0; i < [loginarray count]; i++) { NSDictionary *tempDict = [loginarray objectAtIndex:i]; - if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) { + if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[path lastPathComponent]]) { [loginarray removeObjectAtIndex:i]; break; } } } + [loginwindow setObject:loginarray forKey:@"AutoLaunchedApplicationDictionary"]; [df setPersistentDomain:loginwindow forName:@"loginwindow"]; [df synchronize]; [loginwindow release]; - return YES; + [loginarray release]; +} + +BOOL ITDoesApplicationLaunchOnLogin(NSString *path) +{ + NSUserDefaults *df = [NSUserDefaults standardUserDefaults]; + NSDictionary *loginwindow; + NSMutableArray *loginarray; + NSEnumerator *loginEnum; + id anItem; + ITDebugLog(@"Checking if \"%@\" launches at login.", path); + [df synchronize]; + loginwindow = [df persistentDomainForName:@"loginwindow"]; + loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"]; + + loginEnum = [loginarray objectEnumerator]; + while ( (anItem = [loginEnum nextObject]) ) { + if ( [[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[path lastPathComponent]] ) { + return YES; + } + } + return NO; } \ No newline at end of file