Skip to content

Commit

Permalink
add option to start at login
Browse files Browse the repository at this point in the history
  • Loading branch information
ghewgill committed Apr 7, 2012
1 parent c42218f commit 75fa00d
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Stack Exchange Notifier/Stack_Exchange_NotifierAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,77 @@ -(void)fire

@end

/*
* These two functions are based on
* http://stackoverflow.com/questions/815063/how-do-you-make-your-app-open-at-login
*/
static BOOL willStartAtLogin()
{
NSURL *appurl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
BOOL found = NO;
LSSharedFileListRef items = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (items) {
UInt32 seed;
CFArrayRef itemsArray = LSSharedFileListCopySnapshot(items, &seed);
CFIndex count = CFArrayGetCount(itemsArray);
for (CFIndex i = 0; i < count; i++) {
LSSharedFileListItemRef a = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(itemsArray, i);
CFURLRef url = NULL;
OSStatus err = LSSharedFileListItemResolve(a, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, &url, NULL);
if (err == noErr) {
found = CFEqual(url, (__bridge CFURLRef)appurl);
CFRelease(url);
if (found) {
break;
}
}
}
CFRelease(items);
}
return found;
}

static void setStartAtLogin(BOOL enabled)
{
NSURL *appurl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
LSSharedFileListItemRef existing = NULL;
LSSharedFileListRef items = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (items) {
UInt32 seed;
CFArrayRef itemsArray = LSSharedFileListCopySnapshot(items, &seed);
CFIndex count = CFArrayGetCount(itemsArray);
for (CFIndex i = 0; i < count; i++) {
LSSharedFileListItemRef a = (LSSharedFileListItemRef)CFArrayGetValueAtIndex(itemsArray, i);
CFURLRef url = NULL;
OSStatus err = LSSharedFileListItemResolve(a, kLSSharedFileListNoUserInteraction | kLSSharedFileListDoNotMountVolumes, &url, NULL);
if (err == noErr) {
BOOL found = CFEqual(url, (__bridge CFURLRef)appurl);
CFRelease(url);
if (found) {
existing = a;
break;
}
}
}
if (enabled && existing == NULL) {
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(
items,
kLSSharedFileListItemLast,
NULL,
NULL,
(__bridge CFURLRef)appurl,
NULL,
NULL);
if (item) {
CFRelease(item);
}
} else if (!enabled && existing != NULL) {
LSSharedFileListItemRemove(items, existing);
}
CFRelease(items);
}
}

// Create the status item when needed. Called on program startup or
// when the icon is unhidden.
NSStatusItem *createStatusItem(NSImage* icon)
Expand Down Expand Up @@ -202,6 +273,9 @@ -(void)resetMenu
NSMenuItem *enableNotifications = [[NSMenuItem alloc] initWithTitle:@"Enable popup notifications (Growl)" action:@selector(changeNotifications) keyEquivalent:@""];
[enableNotifications setState:notificationsEnabled ? NSOnState : NSOffState];
[preferencesMenu addItem:enableNotifications];
NSMenuItem *enableStartAtLogin = [[NSMenuItem alloc] initWithTitle:@"Start at login" action:@selector(changeStartAtLogin) keyEquivalent:@""];
[enableStartAtLogin setState:willStartAtLogin() ? NSOnState : NSOffState];
[preferencesMenu addItem:enableStartAtLogin];
NSMenuItem *preferences = [[NSMenuItem alloc] initWithTitle:@"Preferences" action:nil keyEquivalent:@""];
[menu addItem:preferences];
[menu setSubmenu:preferencesMenu forItem:preferences];
Expand Down Expand Up @@ -577,4 +651,10 @@ -(void)changeNotifications
[self resetMenu];
}

-(void)changeStartAtLogin
{
setStartAtLogin(!willStartAtLogin());
[self resetMenu];
}

@end

0 comments on commit 75fa00d

Please sign in to comment.