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
|
//
// PreferencesViewController.m
// ZeroTier One
//
// Created by Grant Limberg on 8/7/16.
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
//
#import "PreferencesViewController.h"
@interface PreferencesViewController ()
@end
@implementation PreferencesViewController
- (void)viewDidLoad {
[super viewDidLoad];
if([self isLaunchAtStartup]) {
self.startupCheckBox.state = NSOnState;
}
else {
self.startupCheckBox.state = NSOffState;
}
}
- (IBAction)onStartupCheckBoxChanged:(NSButton *)sender
{
if(sender.state == NSOnState) {
[self setLaunchAtLoginEnabled:YES];
}
else {
[self setLaunchAtLoginEnabled:NO];
}
}
- (void)setLaunchAtLoginEnabled:(BOOL)enabled
{
LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (enabled) {
// Add the app to the LoginItems list.
CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
if (itemRef) CFRelease(itemRef);
}
else {
// Remove the app from the LoginItems list.
LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
LSSharedFileListItemRemove(loginItemsRef,itemRef);
if (itemRef != nil) CFRelease(itemRef);
}
}
- (BOOL)isLaunchAtStartup {
// See if the app is currently in LoginItems.
LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
// Store away that boolean.
BOOL isInList = itemRef != nil;
// Release the reference if it exists.
if (itemRef != nil) CFRelease(itemRef);
return isInList;
}
- (LSSharedFileListItemRef)itemRefInLoginItems {
LSSharedFileListItemRef itemRef = nil;
NSString * appPath = [[NSBundle mainBundle] bundlePath];
// This will retrieve the path for the application
// For example, /Applications/test.app
CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
// Create a reference to the shared file list.
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seedValue;
//Retrieve the list of Login Items and cast them to
// a NSArray so that it will be easier to iterate.
NSArray *loginItemsArray = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);
for(int i = 0; i< [loginItemsArray count]; i++){
LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItemsArray
objectAtIndex:i];
//Resolve the item with URL
if (LSSharedFileListItemResolve(currentItemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
NSString * urlPath = [(__bridge NSURL*)url path];
if ([urlPath compare:appPath] == NSOrderedSame){
itemRef = currentItemRef;
}
}
}
}
CFRelease(loginItems);
return itemRef;
}
@end
|