Fixed a crash when Core Image was available.
[ITKit.git] / ITLoginItem.m
1 #import "ITLoginItem.h"
2 #import <Carbon/Carbon.h>
3 #import <CoreServices/CoreServices.h>
4 #import <ITFoundation/ITFoundation.h>
5
6 void ITSetApplicationLaunchOnLogin(NSString *path, BOOL flag) {
7         if ((flag && ITDoesApplicationLaunchOnLogin(path)) || ![[NSFileManager defaultManager] fileExistsAtPath:path]) {
8                 return;
9         }
10         NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
11         NSMutableDictionary *loginwindow;
12         NSMutableArray *loginarray;
13
14         ITDebugLog(@"Set if \"%@\" launches at login to %i.", path, flag);
15         [df synchronize];
16         loginwindow = [[df persistentDomainForName:@"loginwindow"] mutableCopy];
17         loginarray = [[loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];
18
19         if (flag) {
20                 /*FSRef fileRef;
21                 AliasHandle alias;
22                 NSData *aliasData;
23                 FSPathMakeRef([path UTF8String], &fileRef, NULL);
24                 FSNewAlias(NULL, &fileRef, &alias);
25                 aliasData = [NSData dataWithBytes:&alias length:GetHandleSize((Handle)alias)];*/
26
27                 if (!loginarray) { //If there is no loginarray of autolaunch items, create one
28                         loginarray = [[[NSMutableArray alloc] init] autorelease];
29                 }
30                 NSDictionary *itemDict = [NSDictionary dictionaryWithObjectsAndKeys:
31                         [[NSBundle mainBundle] bundlePath], @"Path",
32                         [NSNumber numberWithInt:0], @"Hide",
33                         [NSData data], @"AliasData", nil, nil];
34                 [loginarray addObject:itemDict];
35         } else {
36                 int i;
37                 for (i = 0;i < [loginarray count];i++) {
38                         NSDictionary *tempDict = [loginarray objectAtIndex:i];
39                         if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[path lastPathComponent]]) {
40                                 [loginarray removeObjectAtIndex:i];
41                                 break;
42                         }
43                 }
44         }
45         [loginwindow setObject:loginarray forKey:@"AutoLaunchedApplicationDictionary"];
46         [df setPersistentDomain:loginwindow forName:@"loginwindow"];
47         [df synchronize];
48         [loginwindow release];
49         [loginarray release];
50 }
51
52 BOOL ITDoesApplicationLaunchOnLogin(NSString *path) {
53         NSUserDefaults *df = [NSUserDefaults standardUserDefaults];
54         NSDictionary *loginwindow;
55         NSMutableArray *loginarray;
56         NSEnumerator *loginEnum;
57         id anItem;
58         ITDebugLog(@"Checking if \"%@\" launches at login.", path);
59         [df synchronize];
60         loginwindow = [df persistentDomainForName:@"loginwindow"];
61         loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
62
63         loginEnum = [loginarray objectEnumerator];
64         while ((anItem = [loginEnum nextObject])) {
65                 if ([[[anItem objectForKey:@"Path"] lastPathComponent] isEqualToString:[path lastPathComponent]]) {
66                         return YES;
67                 }
68         }
69         return NO;
70 }