|
7 | 7 | // |
8 | 8 |
|
9 | 9 | #import "org_cryptomator_macos_autostart_MacLaunchServices_Native.h" |
10 | | -#import <Foundation/Foundation.h> |
11 | | -#import <ServiceManagement/ServiceManagement.h> |
12 | | - |
13 | | -static void migrateSharedFileListLoginItem(void) { |
14 | | - LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
15 | | - NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
16 | | - if (sharedFileList) { |
17 | | - UInt32 seedValue; |
18 | | - NSArray *sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue)); |
19 | | - for (id sharedFile in sharedFileListArray) { |
20 | | - LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile; |
21 | | - CFURLRef applicationPathURL; |
22 | | - if (LSSharedFileListItemResolve(sharedFileListItem, 0, &applicationPathURL, NULL) == noErr) { |
23 | | - NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path]; |
24 | | - if ([resolvedApplicationPath compare:applicationPath] == NSOrderedSame) { |
25 | | - LSSharedFileListItemRemove(sharedFileList, sharedFileListItem); |
26 | | - NSError* error = nil; |
27 | | - if (![[SMAppService mainAppService] registerAndReturnError: & error]) { |
28 | | - NSLog(@"Failed to add login item: %@", error.localizedDescription); |
29 | | - } |
30 | | - } |
31 | | - CFRelease(applicationPathURL); |
32 | | - } |
33 | | - } |
34 | | - CFRelease(sharedFileList); |
35 | | - } else { |
36 | | - NSLog(@"Unable to create the shared file list."); |
37 | | - } |
38 | | -} |
| 10 | +#import "SKYLaunchService.h" |
39 | 11 |
|
40 | 12 | JNIEXPORT jboolean JNICALL Java_org_cryptomator_macos_autostart_MacLaunchServices_00024Native_isLoginItemEnabled(JNIEnv *env, jobject thisObj) { |
41 | | - if (@available(macOS 13, *)) { |
42 | | - if ([[SMAppService mainAppService] status] == SMAppServiceStatusEnabled) { |
43 | | - return YES; |
44 | | - } |
45 | | - migrateSharedFileListLoginItem(); |
46 | | - return [[SMAppService mainAppService] status] == SMAppServiceStatusEnabled ? YES : NO; |
47 | | - } else { // macOS < 13 |
48 | | - LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
49 | | - NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
50 | | - if (sharedFileList) { |
51 | | - UInt32 seedValue; |
52 | | - NSArray *sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue)); |
53 | | - for (id sharedFile in sharedFileListArray) { |
54 | | - LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile; |
55 | | - CFURLRef applicationPathURL = NULL; |
56 | | - LSSharedFileListItemResolve(sharedFileListItem, 0, (CFURLRef *)&applicationPathURL, NULL); |
57 | | - if (applicationPathURL != NULL) { |
58 | | - NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path]; |
59 | | - CFRelease(applicationPathURL); |
60 | | - if ([resolvedApplicationPath compare:applicationPath] == NSOrderedSame) { |
61 | | - CFRelease(sharedFileList); |
62 | | - return YES; |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - CFRelease(sharedFileList); |
67 | | - } else { |
68 | | - NSLog(@"Unable to create the shared file list."); |
69 | | - } |
70 | | - return NO; |
71 | | - } |
| 13 | + return [SKYLaunchService isLoginItemEnabled]; |
72 | 14 | } |
73 | 15 |
|
74 | 16 | JNIEXPORT jboolean JNICALL Java_org_cryptomator_macos_autostart_MacLaunchServices_00024Native_enableLoginItem(JNIEnv *env, jobject thisObj) { |
75 | | - if (@available(macOS 13, *)) { |
76 | | - NSError* error = nil; |
77 | | - if (![[SMAppService mainAppService] registerAndReturnError: & error]) { |
78 | | - NSLog(@"Failed to add login item: %@", error.localizedDescription); |
79 | | - return NO; |
80 | | - } else { |
81 | | - return YES; |
82 | | - } |
83 | | - } else { // macOS < 13 |
84 | | - LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
85 | | - NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
86 | | - NSURL *applicationPathURL = [NSURL fileURLWithPath:applicationPath]; |
87 | | - if (sharedFileList) { |
88 | | - LSSharedFileListItemRef sharedFileListItem = LSSharedFileListInsertItemURL(sharedFileList, kLSSharedFileListItemLast, NULL, NULL, (__bridge CFURLRef)applicationPathURL, NULL, NULL); |
89 | | - if (sharedFileListItem) { |
90 | | - CFRelease(sharedFileListItem); |
91 | | - } |
92 | | - CFRelease(sharedFileList); |
93 | | - return YES; |
94 | | - } else { |
95 | | - NSLog(@"Unable to create the shared file list."); |
96 | | - return NO; |
97 | | - } |
98 | | - } |
| 17 | + return [SKYLaunchService enableLoginItem]; |
99 | 18 | } |
100 | 19 |
|
101 | 20 | JNIEXPORT jboolean JNICALL Java_org_cryptomator_macos_autostart_MacLaunchServices_00024Native_disableLoginItem(JNIEnv *env, jobject thisObj) { |
102 | | - if (@available(macOS 13, *)) { |
103 | | - NSError* error = nil; |
104 | | - if (![[SMAppService mainAppService] unregisterAndReturnError: & error]) { |
105 | | - NSLog(@"Failed to remove login item: %@", error.localizedDescription); |
106 | | - return NO; |
107 | | - } else { |
108 | | - return YES; |
109 | | - } |
110 | | - } else { // macOS < 13 |
111 | | - LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
112 | | - NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
113 | | - if (sharedFileList) { |
114 | | - UInt32 seedValue; |
115 | | - NSArray *sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue)); |
116 | | - for (id sharedFile in sharedFileListArray) { |
117 | | - LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile; |
118 | | - CFURLRef applicationPathURL; |
119 | | - if (LSSharedFileListItemResolve(sharedFileListItem, 0, &applicationPathURL, NULL) == noErr) { |
120 | | - NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path]; |
121 | | - if ([resolvedApplicationPath compare:applicationPath] == NSOrderedSame) { |
122 | | - LSSharedFileListItemRemove(sharedFileList, sharedFileListItem); |
123 | | - } |
124 | | - CFRelease(applicationPathURL); |
125 | | - } |
126 | | - } |
127 | | - CFRelease(sharedFileList); |
128 | | - return YES; |
129 | | - } else { |
130 | | - NSLog(@"Unable to create the shared file list."); |
131 | | - return NO; |
132 | | - } |
133 | | - } |
| 21 | + return [SKYLaunchService disableLoginItem]; |
134 | 22 | } |
0 commit comments