LaunchAtLoginController.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // LaunchAtLoginController.m
  3. //
  4. // Copyright 2011 Tomáš Znamenáček
  5. // Copyright 2010 Ben Clark-Robinson
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining
  8. // a copy of this software and associated documentation files (the ‘Software’),
  9. // to deal in the Software without restriction, including without limitation
  10. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. // and/or sell copies of the Software, and to permit persons to whom the
  12. // Software is furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. #import "LaunchAtLoginController.h"
  25. static NSString *const StartAtLoginKey = @"launchAtLogin";
  26. @interface LaunchAtLoginController ()
  27. @property(assign) LSSharedFileListRef loginItems;
  28. @end
  29. @implementation LaunchAtLoginController
  30. @synthesize loginItems;
  31. #pragma mark Change Observing
  32. void sharedFileListDidChange(LSSharedFileListRef inList, void *context)
  33. {
  34. LaunchAtLoginController *self = (__bridge id) context;
  35. [self willChangeValueForKey:StartAtLoginKey];
  36. [self didChangeValueForKey:StartAtLoginKey];
  37. }
  38. #pragma mark Initialization
  39. - (id) init
  40. {
  41. self = [super init];
  42. if(self) {
  43. loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
  44. LSSharedFileListAddObserver(loginItems, CFRunLoopGetMain(),
  45. (CFStringRef)NSDefaultRunLoopMode, sharedFileListDidChange, (__bridge void*)self);
  46. }
  47. return self;
  48. }
  49. - (void) dealloc
  50. {
  51. LSSharedFileListRemoveObserver(loginItems, CFRunLoopGetMain(),
  52. (CFStringRef)NSDefaultRunLoopMode, sharedFileListDidChange, (__bridge void*)self);
  53. CFRelease(loginItems);
  54. }
  55. #pragma mark Launch List Control
  56. - (LSSharedFileListItemRef) findItemWithURL: (NSURL*) wantedURL inFileList: (LSSharedFileListRef) fileList
  57. {
  58. if (wantedURL == NULL || fileList == NULL)
  59. return NULL;
  60. NSArray *listSnapshot = (__bridge_transfer NSArray*)LSSharedFileListCopySnapshot(fileList, NULL);
  61. for (id itemObject in listSnapshot) {
  62. LSSharedFileListItemRef item = (__bridge LSSharedFileListItemRef) itemObject;
  63. UInt32 resolutionFlags = kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes;
  64. CFURLRef currentItemURL = NULL;
  65. LSSharedFileListItemResolve(item, resolutionFlags, &currentItemURL, NULL);
  66. if (currentItemURL && CFEqual(currentItemURL, (__bridge CFTypeRef)wantedURL)) {
  67. CFRelease(currentItemURL);
  68. return item;
  69. }
  70. if (currentItemURL)
  71. CFRelease(currentItemURL);
  72. }
  73. return NULL;
  74. }
  75. - (BOOL) willLaunchAtLogin: (NSURL*) itemURL
  76. {
  77. return !![self findItemWithURL:itemURL inFileList:loginItems];
  78. }
  79. - (void) setLaunchAtLogin: (BOOL) enabled forURL: (NSURL*) itemURL
  80. {
  81. LSSharedFileListItemRef appItem = [self findItemWithURL:itemURL inFileList:loginItems];
  82. if (enabled && !appItem) {
  83. LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst,
  84. NULL, NULL, (__bridge CFURLRef)itemURL, NULL, NULL);
  85. } else if (!enabled && appItem)
  86. LSSharedFileListItemRemove(loginItems, appItem);
  87. }
  88. #pragma mark Basic Interface
  89. - (NSURL*) appURL
  90. {
  91. return [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
  92. }
  93. - (void) setLaunchAtLogin: (BOOL) enabled
  94. {
  95. [self willChangeValueForKey:StartAtLoginKey];
  96. [self setLaunchAtLogin:enabled forURL:[self appURL]];
  97. [self didChangeValueForKey:StartAtLoginKey];
  98. }
  99. - (BOOL) launchAtLogin
  100. {
  101. return [self willLaunchAtLogin:[self appURL]];
  102. }
  103. @end