PreferencesViewController.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #import "PreferencesViewController.h"
  19. @interface PreferencesViewController ()
  20. @end
  21. @implementation PreferencesViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. if([self isLaunchAtStartup]) {
  25. self.startupCheckBox.state = NSOnState;
  26. }
  27. else {
  28. self.startupCheckBox.state = NSOffState;
  29. }
  30. }
  31. - (IBAction)onStartupCheckBoxChanged:(NSButton *)sender
  32. {
  33. if(sender.state == NSOnState) {
  34. [self setLaunchAtLoginEnabled:YES];
  35. }
  36. else {
  37. [self setLaunchAtLoginEnabled:NO];
  38. }
  39. }
  40. - (void)setLaunchAtLoginEnabled:(BOOL)enabled
  41. {
  42. LSSharedFileListRef loginItemsRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  43. if (enabled) {
  44. // Add the app to the LoginItems list.
  45. CFURLRef appUrl = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
  46. LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsRef, kLSSharedFileListItemLast, NULL, NULL, appUrl, NULL, NULL);
  47. if (itemRef) CFRelease(itemRef);
  48. }
  49. else {
  50. // Remove the app from the LoginItems list.
  51. LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
  52. LSSharedFileListItemRemove(loginItemsRef,itemRef);
  53. if (itemRef != nil) CFRelease(itemRef);
  54. }
  55. }
  56. - (BOOL)isLaunchAtStartup {
  57. // See if the app is currently in LoginItems.
  58. LSSharedFileListItemRef itemRef = [self itemRefInLoginItems];
  59. // Store away that boolean.
  60. BOOL isInList = itemRef != nil;
  61. // Release the reference if it exists.
  62. if (itemRef != nil) CFRelease(itemRef);
  63. return isInList;
  64. }
  65. - (LSSharedFileListItemRef)itemRefInLoginItems {
  66. LSSharedFileListItemRef itemRef = nil;
  67. NSString * appPath = [[NSBundle mainBundle] bundlePath];
  68. // This will retrieve the path for the application
  69. // For example, /Applications/test.app
  70. CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:appPath];
  71. // Create a reference to the shared file list.
  72. LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  73. if (loginItems) {
  74. UInt32 seedValue;
  75. //Retrieve the list of Login Items and cast them to
  76. // a NSArray so that it will be easier to iterate.
  77. NSArray *loginItemsArray = (__bridge NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);
  78. for(int i = 0; i< [loginItemsArray count]; i++){
  79. LSSharedFileListItemRef currentItemRef = (__bridge LSSharedFileListItemRef)[loginItemsArray
  80. objectAtIndex:i];
  81. //Resolve the item with URL
  82. if (LSSharedFileListItemResolve(currentItemRef, 0, (CFURLRef*) &url, NULL) == noErr) {
  83. NSString * urlPath = [(__bridge NSURL*)url path];
  84. if ([urlPath compare:appPath] == NSOrderedSame){
  85. itemRef = currentItemRef;
  86. }
  87. }
  88. }
  89. }
  90. CFRelease(loginItems);
  91. return itemRef;
  92. }
  93. @end