PreferencesViewController.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // PreferencesViewController.m
  3. // ZeroTier One
  4. //
  5. // Created by Grant Limberg on 8/7/16.
  6. // Copyright © 2016 ZeroTier, Inc. All rights reserved.
  7. //
  8. #import "PreferencesViewController.h"
  9. @interface PreferencesViewController ()
  10. @end
  11. @implementation PreferencesViewController
  12. - (void)viewDidLoad {
  13. [super viewDidLoad];
  14. if([self isLaunchAtStartup]) {
  15. self.startupCheckBox.state = NSOnState;
  16. }
  17. else {
  18. self.startupCheckBox.state = NSOffState;
  19. }
  20. }
  21. - (IBAction)onStartupCheckBoxChanged:(NSButton *)sender
  22. {
  23. if(sender.state == NSOnState) {
  24. [self setLaunchAtLoginEnabled:YES];
  25. }
  26. else {
  27. [self setLaunchAtLoginEnabled:NO];
  28. }
  29. }
  30. - (void)setLaunchAtLoginEnabled:(BOOL)enabled
  31. {
  32. LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  33. if (enabled) {
  34. // Add the app to the LoginItems list.
  35. CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
  36. LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
  37. if (itemRef) CFRelease(itemRef);
  38. }
  39. else {
  40. // Remove the app from the LoginItems list.
  41. LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
  42. LSSharedFileListItemRemove(loginItemsRef,itemRef);
  43. if (itemRef != nil) CFRelease(itemRef);
  44. }
  45. }
  46. - (BOOL)isLaunchAtStartup {
  47. // See if the app is currently in LoginItems.
  48. LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
  49. // Store away that boolean.
  50. BOOL isInList = itemRef != nil;
  51. // Release the reference if it exists.
  52. if (itemRef != nil) CFRelease(itemRef);
  53. return isInList;
  54. }
  55. - (LSSharedFileListItemRef)itemRefInLoginItems {
  56. LSSharedFileListItemRef itemRef = nil;
  57. NSString * appPath = [[NSBundle mainBundle] bundlePath];
  58. // This will retrieve the path for the application
  59. // For example, /Applications/test.app
  60. CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
  61. // Create a reference to the shared file list.
  62. LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  63. if (loginItems) {
  64. UInt32 seedValue;
  65. //Retrieve the list of Login Items and cast them to
  66. // a NSArray so that it will be easier to iterate.
  67. NSArray *loginItemsArray = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);
  68. for(int i = 0; i< [loginItemsArray count]; i++){
  69. LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItemsArray
  70. objectAtIndex:i];
  71. //Resolve the item with URL
  72. if (LSSharedFileListItemResolve(currentItemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
  73. NSString * urlPath = [(__bridge NSURL*)url path];
  74. if ([urlPath compare:appPath] == NSOrderedSame){
  75. itemRef = currentItemRef;
  76. }
  77. }
  78. }
  79. }
  80. CFRelease(loginItems);
  81. return itemRef;
  82. }
  83. @end