Skip to content

Commit

Permalink
Used separate LoginItemManager class.
Browse files Browse the repository at this point in the history
I wrote this in another project.  Fixed bugs and stuff.
  • Loading branch information
dustin committed Mar 24, 2011
1 parent 88b0c9c commit 2a88c66
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 94 deletions.
6 changes: 6 additions & 0 deletions Membase.xcodeproj/project.pbxproj
Expand Up @@ -20,6 +20,7 @@
DA379BE91339314B008B7BBC /* Membase-status.png in Resources */ = {isa = PBXBuildFile; fileRef = DA379BE81339314B008B7BBC /* Membase-status.png */; };
DA379BEB13393162008B7BBC /* Membase.icns in Resources */ = {isa = PBXBuildFile; fileRef = DA379BEA13393162008B7BBC /* Membase.icns */; };
DA379BED13393242008B7BBC /* Credits.html in Resources */ = {isa = PBXBuildFile; fileRef = DA379BEC13393242008B7BBC /* Credits.html */; };
DA61C6D8133AFA770095F5CF /* LoginItemManager.m in Sources */ = {isa = PBXBuildFile; fileRef = DA61C6D7133AFA770095F5CF /* LoginItemManager.m */; };
DA7620051339DC3E0062715D /* Membase-status-shutdown.png in Resources */ = {isa = PBXBuildFile; fileRef = DA7620041339DC3E0062715D /* Membase-status-shutdown.png */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -56,6 +57,8 @@
DA379BE81339314B008B7BBC /* Membase-status.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Membase-status.png"; sourceTree = "<group>"; };
DA379BEA13393162008B7BBC /* Membase.icns */ = {isa = PBXFileReference; lastKnownFileType = image.icns; path = Membase.icns; sourceTree = SOURCE_ROOT; };
DA379BEC13393242008B7BBC /* Credits.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = Credits.html; sourceTree = "<group>"; };
DA61C6D6133AFA770095F5CF /* LoginItemManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LoginItemManager.h; sourceTree = "<group>"; };
DA61C6D7133AFA770095F5CF /* LoginItemManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoginItemManager.m; sourceTree = "<group>"; };
DA7620041339DC3E0062715D /* Membase-status-shutdown.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Membase-status-shutdown.png"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -120,6 +123,8 @@
DA28357313392962009A4AEF /* Membase */ = {
isa = PBXGroup;
children = (
DA61C6D6133AFA770095F5CF /* LoginItemManager.h */,
DA61C6D7133AFA770095F5CF /* LoginItemManager.m */,
DA28357F13392962009A4AEF /* MembaseAppDelegate.h */,
DA28358013392962009A4AEF /* MembaseAppDelegate.m */,
DA28358213392962009A4AEF /* MainMenu.xib */,
Expand Down Expand Up @@ -309,6 +314,7 @@
files = (
DA28357B13392962009A4AEF /* main.m in Sources */,
DA28358113392962009A4AEF /* MembaseAppDelegate.m in Sources */,
DA61C6D8133AFA770095F5CF /* LoginItemManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
20 changes: 20 additions & 0 deletions Membase/LoginItemManager.h
@@ -0,0 +1,20 @@
//
// LoginItemManager.h
// AppHider
//
// Created by Dustin Sallings on 3/23/11.
//

#import <Foundation/Foundation.h>


@interface LoginItemManager : NSObject {
@private

}

-(BOOL)inLoginItems;
-(void)removeLoginItem:(id)sender;
-(void)addToLoginItems:(id)sender;

@end
115 changes: 115 additions & 0 deletions Membase/LoginItemManager.m
@@ -0,0 +1,115 @@
//
// LoginItemManager.m
// AppHider
//
// Created by Dustin Sallings on 3/23/11.
//

#import "LoginItemManager.h"


@implementation LoginItemManager

- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}

return self;
}

- (void)dealloc
{
[super dealloc];
}

-(BOOL) inLoginItems {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL rv = NO;

[defaults addSuiteNamed:@"loginwindow"];

NSMutableArray *loginItems=[[[defaults
persistentDomainForName:@"loginwindow"]
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];

// Remove anything that looks like the current login item.
NSString *myName=[[[NSBundle mainBundle] bundlePath] lastPathComponent];
NSEnumerator *e=[loginItems objectEnumerator];
id current=nil;
while( (current=[e nextObject]) != nil) {
if([[current valueForKey:@"Path"] hasSuffix:myName]) {
rv = YES;
}
}

return rv;
}

-(void) removeLoginItem:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults addSuiteNamed:@"loginwindow"];

NSMutableArray *loginItems=[[[defaults
persistentDomainForName:@"loginwindow"]
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];

// Remove anything that looks like the current login item.
NSString *myName=[[[NSBundle mainBundle] bundlePath] lastPathComponent];
NSEnumerator *e=[loginItems objectEnumerator];
id current=nil;
while( (current=[e nextObject]) != nil) {
if([[current valueForKey:@"Path"] hasSuffix:myName]) {
NSLog(@"Removing login item: %@", [current valueForKey:@"Path"]);
[loginItems removeObject:current];
break;
}
}

[defaults removeObjectForKey:@"AutoLaunchedApplicationDictionary"];
[defaults setObject:loginItems forKey:
@"AutoLaunchedApplicationDictionary"];

// Use the corefoundation API since I can't figure out the other one.
CFPreferencesSetValue((CFStringRef)@"AutoLaunchedApplicationDictionary",
loginItems, (CFStringRef)@"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);

}

-(void)addToLoginItems:(id)sender {

[self removeLoginItem: self];

NSMutableDictionary *myDict=[[NSMutableDictionary alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

[defaults addSuiteNamed:@"loginwindow"];

NSLog(@"Adding login item: %@", [[NSBundle mainBundle] bundlePath]);
[myDict setObject:[NSNumber numberWithBool:NO] forKey:@"Hide"];
[myDict setObject:[[NSBundle mainBundle] bundlePath]
forKey:@"Path"];

NSMutableArray *loginItems=[[[defaults
persistentDomainForName:@"loginwindow"]
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];

[loginItems addObject:myDict];
[defaults removeObjectForKey:@"AutoLaunchedApplicationDictionary"];
[defaults setObject:loginItems forKey:
@"AutoLaunchedApplicationDictionary"];

// Use the corefoundation API since I can't figure out the other one.
CFPreferencesSetValue((CFStringRef)@"AutoLaunchedApplicationDictionary",
loginItems, (CFStringRef)@"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);

[myDict release];
[loginItems release];
}

@end
3 changes: 3 additions & 0 deletions Membase/MembaseAppDelegate.h
Expand Up @@ -8,13 +8,16 @@

#import <Cocoa/Cocoa.h>

#import "LoginItemManager.h"

@interface MembaseAppDelegate : NSObject <NSApplicationDelegate> {
@private
NSStatusItem *statusBar;
IBOutlet NSMenu *statusMenu;

IBOutlet NSMenuItem *launchBrowserItem;
IBOutlet NSMenuItem *launchAtStartupItem;
IBOutlet LoginItemManager *loginItems;

NSTask *task;
NSTimer *taskKiller;
Expand Down
96 changes: 3 additions & 93 deletions Membase/MembaseAppDelegate.m
Expand Up @@ -332,105 +332,15 @@ -(IBAction)setLaunchPref:(id)sender {
[[NSUserDefaults standardUserDefaults] synchronize];
}

-(bool) isInLoginItems {
NSUserDefaults * defaults = [[NSUserDefaults alloc] init];
BOOL rv = NO;

[defaults addSuiteNamed:@"loginwindow"];

NSMutableArray *loginItems=[[[defaults
persistentDomainForName:@"loginwindow"]
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];

// Remove anything that looks like the current login item.
NSString *myName=[[[NSBundle mainBundle] bundlePath] lastPathComponent];
NSEnumerator *e=[loginItems objectEnumerator];
id current=nil;
while( (current=[e nextObject]) != nil) {
if([[current valueForKey:@"Path"] hasSuffix:myName]) {
rv = YES;
}
}

[defaults release];
return rv;
}

-(void) updateAddItemButtonState {
[launchAtStartupItem setState:[self isInLoginItems] ? NSOnState : NSOffState];
}

-(void) removeLoginItem:(id)sender {
NSUserDefaults * defaults = [[NSUserDefaults alloc] init];

[defaults addSuiteNamed:@"loginwindow"];

NSMutableArray *loginItems=[[[defaults
persistentDomainForName:@"loginwindow"]
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];

// Remove anything that looks like the current login item.
NSString *myName=[[[NSBundle mainBundle] bundlePath] lastPathComponent];
NSEnumerator *e=[loginItems objectEnumerator];
id current=nil;
while( (current=[e nextObject]) != nil) {
if([[current valueForKey:@"Path"] hasSuffix:myName]) {
NSLog(@"Removing login item: %@", [current valueForKey:@"Path"]);
[loginItems removeObject:current];
}
}

[defaults removeObjectForKey:@"AutoLaunchedApplicationDictionary"];
[defaults setObject:loginItems forKey:
@"AutoLaunchedApplicationDictionary"];

// Use the corefoundation API since I can't figure out the other one.
CFPreferencesSetValue((CFStringRef)@"AutoLaunchedApplicationDictionary",
loginItems, (CFStringRef)@"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);

[defaults release];
}

// XXX: I need to make this be able to add or remove, and validate the current user wishes.
-(void)addToLoginItems:(id)sender {

[self removeLoginItem: self];

NSMutableDictionary * myDict=[[NSMutableDictionary alloc] init];
NSUserDefaults * defaults = [[NSUserDefaults alloc] init];

[defaults addSuiteNamed:@"loginwindow"];

NSLog(@"Adding login item: %@", [[NSBundle mainBundle] bundlePath]);
[myDict setObject:[NSNumber numberWithBool:NO] forKey:@"Hide"];
[myDict setObject:[[NSBundle mainBundle] bundlePath]
forKey:@"Path"];

NSMutableArray *loginItems=[[[defaults
persistentDomainForName:@"loginwindow"]
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy];

[loginItems addObject:myDict];
[defaults removeObjectForKey:@"AutoLaunchedApplicationDictionary"];
[defaults setObject:loginItems forKey:
@"AutoLaunchedApplicationDictionary"];

// Use the corefoundation API since I can't figure out the other one.
CFPreferencesSetValue((CFStringRef)@"AutoLaunchedApplicationDictionary",
loginItems, (CFStringRef)@"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost);

[defaults release];
[myDict release];
[loginItems release];
[launchAtStartupItem setState:[loginItems inLoginItems] ? NSOnState : NSOffState];
}

-(IBAction)changeLoginItems:(id)sender {
if([sender state] == NSOffState) {
[self addToLoginItems:self];
[loginItems addToLoginItems:self];
} else {
[self removeLoginItem:self];
[loginItems removeLoginItem:self];
}
[self updateAddItemButtonState];
}
Expand Down

0 comments on commit 2a88c66

Please sign in to comment.