Skip to content

Commit

Permalink
Added auto launch on login
Browse files Browse the repository at this point in the history
And rearranged the code a bit.
Uses this project to do the auto launching:
https://github.com/Mozketo/LaunchAtLoginController
  • Loading branch information
kylehowells committed Oct 9, 2014
1 parent c7d0067 commit da4e103
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 29 deletions.
20 changes: 18 additions & 2 deletions LIFX Menu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
C721971119E5F79700FCA5CB /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = C721970F19E5F79700FCA5CB /* MainMenu.xib */; };
C721971D19E5F79700FCA5CB /* LIFX_MenuTests.m in Sources */ = {isa = PBXBuildFile; fileRef = C721971C19E5F79700FCA5CB /* LIFX_MenuTests.m */; };
C721973719E5FA1200FCA5CB /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = C721973519E5FA1200FCA5CB /* libz.dylib */; };
C721973819E5FA1200FCA5CB /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C721973619E5FA1200FCA5CB /* SystemConfiguration.framework */; };
C721974219E607BD00FCA5CB /* lifx-icon.png in Resources */ = {isa = PBXBuildFile; fileRef = C721974019E607BD00FCA5CB /* lifx-icon.png */; };
C721974319E607BD00FCA5CB /* lifx-icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = C721974119E607BD00FCA5CB /* lifx-icon@2x.png */; };
C721974819E622D400FCA5CB /* LIFXKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C721974619E622B200FCA5CB /* LIFXKit.framework */; };
C721974A19E622FD00FCA5CB /* LIFXKit.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = C721974619E622B200FCA5CB /* LIFXKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
C721975319E631D400FCA5CB /* LaunchAtLoginController.m in Sources */ = {isa = PBXBuildFile; fileRef = C721975219E631D400FCA5CB /* LaunchAtLoginController.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
C721975419E631FD00FCA5CB /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C721973619E5FA1200FCA5CB /* SystemConfiguration.framework */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -59,15 +60,18 @@
C721974019E607BD00FCA5CB /* lifx-icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "lifx-icon.png"; sourceTree = "<group>"; };
C721974119E607BD00FCA5CB /* lifx-icon@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "lifx-icon@2x.png"; sourceTree = "<group>"; };
C721974619E622B200FCA5CB /* LIFXKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = LIFXKit.framework; sourceTree = "<group>"; };
C721974F19E62DC800FCA5CB /* ServiceManagement.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ServiceManagement.framework; path = System/Library/Frameworks/ServiceManagement.framework; sourceTree = SDKROOT; };
C721975119E631D400FCA5CB /* LaunchAtLoginController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LaunchAtLoginController.h; sourceTree = "<group>"; };
C721975219E631D400FCA5CB /* LaunchAtLoginController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LaunchAtLoginController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
C721970019E5F79600FCA5CB /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
C721975419E631FD00FCA5CB /* SystemConfiguration.framework in Frameworks */,
C721973719E5FA1200FCA5CB /* libz.dylib in Frameworks */,
C721973819E5FA1200FCA5CB /* SystemConfiguration.framework in Frameworks */,
C721974819E622D400FCA5CB /* LIFXKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -105,6 +109,7 @@
children = (
C721970A19E5F79600FCA5CB /* AppDelegate.h */,
C721970B19E5F79600FCA5CB /* AppDelegate.m */,
C721974E19E6292A00FCA5CB /* Launch at login */,
C721970D19E5F79600FCA5CB /* Images.xcassets */,
C721970F19E5F79700FCA5CB /* MainMenu.xib */,
C721970619E5F79600FCA5CB /* Supporting Files */,
Expand Down Expand Up @@ -145,12 +150,22 @@
isa = PBXGroup;
children = (
C721974619E622B200FCA5CB /* LIFXKit.framework */,
C721974F19E62DC800FCA5CB /* ServiceManagement.framework */,
C721973619E5FA1200FCA5CB /* SystemConfiguration.framework */,
C721973519E5FA1200FCA5CB /* libz.dylib */,
);
name = Frameworks;
sourceTree = "<group>";
};
C721974E19E6292A00FCA5CB /* Launch at login */ = {
isa = PBXGroup;
children = (
C721975119E631D400FCA5CB /* LaunchAtLoginController.h */,
C721975219E631D400FCA5CB /* LaunchAtLoginController.m */,
);
name = "Launch at login";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -254,6 +269,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
C721975319E631D400FCA5CB /* LaunchAtLoginController.m in Sources */,
C721970C19E5F79600FCA5CB /* AppDelegate.m in Sources */,
C721970919E5F79600FCA5CB /* main.m in Sources */,
);
Expand Down
116 changes: 89 additions & 27 deletions LIFX Menu/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
// Copyright (c) 2014 Kyle Howells. All rights reserved.
//

#import "AppDelegate.h"
#import <LIFXKit/LIFXKit.h>
#import "AppDelegate.h"
#import "LaunchAtLoginController.h"


@interface AppDelegate () <LFXLightCollectionObserver, LFXLightObserver>
Expand All @@ -23,10 +24,22 @@ @interface AppDelegate () <LFXLightCollectionObserver, LFXLightObserver>



@implementation AppDelegate
@implementation AppDelegate{
LaunchAtLoginController *loginController;
NSMenuItem *autorunItem;
}

#pragma mark - Application Delegate methods

-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// User defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:@{ @"AutoLaunch" : @YES }];


// Variable setup
self.lightItems = [NSMutableArray array];
loginController = [[LaunchAtLoginController alloc] init];


// Status bar item
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
Expand All @@ -42,18 +55,31 @@ -(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.menu = [[NSMenu alloc] init];

// Always there buttons
[self.menu addItemWithTitle:@"Turn lights ON" action:@selector(allLightsOn) keyEquivalent:@""];
[self.menu addItemWithTitle:@"Turn lights OFF" action:@selector(allLightsOff) keyEquivalent:@""];
[self.menu addItemWithTitle:@"Turn all lights on" action:@selector(allLightsOn) keyEquivalent:@""];
[self.menu addItemWithTitle:@"Turn all lights off" action:@selector(allLightsOff) keyEquivalent:@""];

// Separator to the section with the individual lights
[self.menu addItem:[NSMenuItem separatorItem]];


[self.menu addItem:[NSMenuItem separatorItem]];
autorunItem = [[NSMenuItem alloc] initWithTitle:@"Launch at login" action:@selector(autoLaunchPressed) keyEquivalent:@""];
[self.menu addItem:autorunItem];
[self updateAutoLaunch];

self.statusItem.menu = self.menu;


// Monitor for changes
[[[LFXClient sharedClient] localNetworkContext].allLightsCollection addLightCollectionObserver:self];
}

-(void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}






Expand Down Expand Up @@ -96,7 +122,7 @@ -(void)addLight:(LFXLight*)light{
[item setRepresentedObject:light];
[self updateLightMenuItem:item];

[self.menu addItem:item];
[self.menu insertItem:item atIndex:(self.menu.numberOfItems - 2)];
[self.lightItems addObject:item];

[light addLightObserver:self];
Expand Down Expand Up @@ -143,6 +169,34 @@ -(void)updateLightMenuItem:(NSMenuItem*)item{






#pragma mark - LFXLightCollectionObserver

-(void)lightCollection:(LFXLightCollection *)lightCollection didAddLight:(LFXLight *)light{
[self addLight:light];
}
-(void)lightCollection:(LFXLightCollection *)lightCollection didRemoveLight:(LFXLight *)light{
[self removeLight:light];
}


#pragma mark - LFXLightObserver

-(void)light:(LFXLight *)light didChangeLabel:(NSString *)label{
[self updateLight:light];
}
-(void)light:(LFXLight *)light didChangePowerState:(LFXPowerState)powerState{
[self updateLight:light];
}







#pragma mark - Helper methods

-(NSMenuItem*)menuItemForLight:(LFXLight*)light{
Expand All @@ -166,34 +220,42 @@ -(NSString*)titleForLight:(LFXLight*)light{



#pragma mark - Auto launch methods



#pragma mark - LFXLightCollectionObserver

-(void)lightCollection:(LFXLightCollection *)lightCollection didAddLight:(LFXLight *)light{
[self addLight:light];
-(BOOL)autoLaunch{
id object = [[NSUserDefaults standardUserDefaults] objectForKey:@"AutoLaunch"];
return (object ? [object boolValue] : YES);
}
-(void)lightCollection:(LFXLightCollection *)lightCollection didRemoveLight:(LFXLight *)light{
[self removeLight:light];
-(void)setAutoLaunch:(BOOL)autoLaunch{
[[NSUserDefaults standardUserDefaults] setBool:autoLaunch forKey:@"AutoLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];

[self updateAutoLaunch];
}


#pragma mark - LFXLightObserver

-(void)light:(LFXLight *)light didChangeLabel:(NSString *)label{
[self updateLight:light];
}
-(void)light:(LFXLight *)light didChangePowerState:(LFXPowerState)powerState{
[self updateLight:light];
-(void)updateAutoLaunch{
if ([self autoLaunch]) {
if (![loginController launchAtLogin]) {
[loginController setLaunchAtLogin:YES];
[autorunItem setState:NSOnState];
}
}
else {
if ([loginController launchAtLogin]) {
[loginController setLaunchAtLogin:NO];
[autorunItem setState:NSOffState];
}
}
}





-(void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
-(void)autoLaunchPressed{
if (autorunItem.state == NSOnState) {
[self setAutoLaunch:NO];
}
else {
[self setAutoLaunch:YES];
}
}


@end
34 changes: 34 additions & 0 deletions LIFX Menu/LaunchAtLoginController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// LaunchAtLoginController.h
//
// Copyright 2011 Tomáš Znamenáček
// Copyright 2010 Ben Clark-Robinson
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the ‘Software’),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@import Foundation;

@interface LaunchAtLoginController : NSObject {}

@property(assign) BOOL launchAtLogin;

- (BOOL) willLaunchAtLogin: (NSURL*) itemURL;
- (void) setLaunchAtLogin: (BOOL) enabled forURL: (NSURL*) itemURL;

@end
Loading

0 comments on commit da4e103

Please sign in to comment.