-
Notifications
You must be signed in to change notification settings - Fork 5
/
StartupItemController.m
123 lines (94 loc) · 2.19 KB
/
StartupItemController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// StartupItemController.m
// Isolator
//
// Created by Ben Willmore on 12/02/2007.
// Copyright 2007 Ben Willmore. All rights reserved.
//
#import "StartupItemController.h"
@implementation StartupItemController
-(id) init
{
[super init];
return self;
}
-(int) getIdx:(NSArray*)itemArray
{
// see if our bundle identifier is present in the startupitems array
// return index if so (starting at zero) or -1 if not
int idx = -1;
NSEnumerator* enumerator = [itemArray objectEnumerator];
NSDictionary* item;
NSURL* url;
NSString* path;
NSBundle* bundle;
NSString* bundleID;
int thisIdx = 0;
while( (item = [enumerator nextObject]) && (idx==-1) ) {
url = [item objectForKey:@"URL"];
path = [url path];
if (path) {
bundle = [NSBundle bundleWithPath:path];
if (bundle) {
bundleID = [bundle bundleIdentifier];
if ([bundleID isEqual:[[NSBundle mainBundle] bundleIdentifier]])
idx = thisIdx;
}
}
thisIdx++;
}
return idx;
}
-(BOOL) enabled
{
// return YES if number of startup items with our bundle identifier is >0
OSStatus err;
NSArray* itemArray = NULL;
int idx = -1;
err = LIAECopyLoginItems((CFArrayRef*)&itemArray);
if (err != noErr)
return NO;
idx = [self getIdx:itemArray];
[itemArray release];
if (idx>=0)
return YES;
else
return NO;
}
-(void) setEnabled:(BOOL)value
{
// remove all existing startup items with our bundle identifier, and then
// add one new one if value==YES
if ([self enabled])
[self removeStartupItem];
if (value)
[self addStartupItem];
}
-(void) removeStartupItem
{
// remove all existing startup items with our bundle identifier
OSStatus err;
NSArray* itemArray = NULL;
int idx = 0;
while (idx>=0) {
err = LIAECopyLoginItems((CFArrayRef*)&itemArray);
if (err != noErr)
return;
idx = [self getIdx:itemArray];
[itemArray release];
itemArray = NULL;
if (idx>=0)
LIAERemove(idx);
}
}
-(void) addStartupItem
{
// add startup item
NSURL * theURL = [[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]] retain];
if ([theURL isEqual:nil])
NSLog(@"Isolator: Attempted to add nil path to loginitems");
else
LIAEAddURLAtEnd((CFURLRef)theURL, NO);
[theURL release];
}
@end