|
| 1 | +// |
| 2 | +// SKYLaunchService.m |
| 3 | +// Integrations |
| 4 | +// |
| 5 | +// Created by Tobias Hagemann on 26.05.24. |
| 6 | +// Copyright © 2024 Cryptomator. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "SKYLaunchService.h" |
| 10 | +#import <ServiceManagement/ServiceManagement.h> |
| 11 | + |
| 12 | +@implementation SKYLaunchService |
| 13 | + |
| 14 | ++ (BOOL)isLoginItemEnabled { |
| 15 | + if (@available(macOS 13, *)) { |
| 16 | + if (SMAppService.mainAppService.status == SMAppServiceStatusEnabled) { |
| 17 | + return YES; |
| 18 | + } else if ([self isLegacyLoginItemEnabled]) { |
| 19 | + // migrate legacy login item |
| 20 | + [self disableLegacyLoginItem]; |
| 21 | + return [self enableLoginItem]; |
| 22 | + } else { |
| 23 | + return NO; |
| 24 | + } |
| 25 | + } else { // macOS < 13 |
| 26 | + return [self isLegacyLoginItemEnabled]; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | ++ (BOOL)enableLoginItem { |
| 31 | + if (@available(macOS 13, *)) { |
| 32 | + NSError *error; |
| 33 | + if ([SMAppService.mainAppService registerAndReturnError:&error]) { |
| 34 | + return YES; |
| 35 | + } else { |
| 36 | + NSLog(@"Failed to register login item: %@", error.localizedDescription); |
| 37 | + return NO; |
| 38 | + } |
| 39 | + } else { // macOS < 13 |
| 40 | + return [self enableLegacyLoginItem]; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | ++ (BOOL)disableLoginItem { |
| 45 | + if (@available(macOS 13, *)) { |
| 46 | + NSError *error; |
| 47 | + if ([SMAppService.mainAppService unregisterAndReturnError:&error]) { |
| 48 | + return YES; |
| 49 | + } else { |
| 50 | + NSLog(@"Failed to unregister login item: %@", error.localizedDescription); |
| 51 | + return NO; |
| 52 | + } |
| 53 | + } else { // macOS < 13 |
| 54 | + return [self disableLegacyLoginItem]; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#pragma mark - Legacy |
| 59 | + |
| 60 | ++ (BOOL)isLegacyLoginItemEnabled { |
| 61 | + LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 62 | + NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
| 63 | + if (sharedFileList) { |
| 64 | + UInt32 seedValue; |
| 65 | + NSArray *sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue)); |
| 66 | + for (id sharedFile in sharedFileListArray) { |
| 67 | + LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile; |
| 68 | + CFURLRef applicationPathURL = NULL; |
| 69 | + LSSharedFileListItemResolve(sharedFileListItem, 0, (CFURLRef *)&applicationPathURL, NULL); |
| 70 | + if (applicationPathURL != NULL) { |
| 71 | + NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path]; |
| 72 | + CFRelease(applicationPathURL); |
| 73 | + if ([resolvedApplicationPath compare:applicationPath] == NSOrderedSame) { |
| 74 | + CFRelease(sharedFileList); |
| 75 | + return YES; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + CFRelease(sharedFileList); |
| 80 | + } else { |
| 81 | + NSLog(@"Unable to create the shared file list."); |
| 82 | + } |
| 83 | + return NO; |
| 84 | +} |
| 85 | + |
| 86 | ++ (BOOL)enableLegacyLoginItem { |
| 87 | + LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 88 | + NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
| 89 | + NSURL *applicationPathURL = [NSURL fileURLWithPath:applicationPath]; |
| 90 | + if (sharedFileList) { |
| 91 | + LSSharedFileListItemRef sharedFileListItem = LSSharedFileListInsertItemURL(sharedFileList, kLSSharedFileListItemLast, NULL, NULL, (__bridge CFURLRef)applicationPathURL, NULL, NULL); |
| 92 | + if (sharedFileListItem) { |
| 93 | + CFRelease(sharedFileListItem); |
| 94 | + } |
| 95 | + CFRelease(sharedFileList); |
| 96 | + return YES; |
| 97 | + } else { |
| 98 | + NSLog(@"Unable to create the shared file list."); |
| 99 | + return NO; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | ++ (BOOL)disableLegacyLoginItem { |
| 104 | + LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL); |
| 105 | + NSString *applicationPath = NSBundle.mainBundle.bundlePath; |
| 106 | + if (sharedFileList) { |
| 107 | + UInt32 seedValue; |
| 108 | + NSArray *sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue)); |
| 109 | + for (id sharedFile in sharedFileListArray) { |
| 110 | + LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile; |
| 111 | + CFURLRef applicationPathURL; |
| 112 | + if (LSSharedFileListItemResolve(sharedFileListItem, 0, &applicationPathURL, NULL) == noErr) { |
| 113 | + NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path]; |
| 114 | + if ([resolvedApplicationPath compare:applicationPath] == NSOrderedSame) { |
| 115 | + LSSharedFileListItemRemove(sharedFileList, sharedFileListItem); |
| 116 | + } |
| 117 | + CFRelease(applicationPathURL); |
| 118 | + } |
| 119 | + } |
| 120 | + CFRelease(sharedFileList); |
| 121 | + return YES; |
| 122 | + } else { |
| 123 | + NSLog(@"Unable to create the shared file list."); |
| 124 | + return NO; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +@end |
0 commit comments