2
0
MasterPassword/MasterPassword/ObjC/Mac/MPAppDelegate.m

336 lines
12 KiB
Mathematica
Raw Normal View History

//
// MPAppDelegate.m
// MasterPassword
//
// Created by Maarten Billemont on 04/03/12.
// Copyright (c) 2012 Lyndir. All rights reserved.
//
2012-05-13 08:24:19 +00:00
#import "MPAppDelegate.h"
#import "MPAppDelegate_Key.h"
#import "MPAppDelegate_Store.h"
#import <Carbon/Carbon.h>
@implementation MPAppDelegate
2013-04-20 18:11:19 +00:00
@synthesize statusItem;
@synthesize lockItem;
@synthesize showItem;
@synthesize statusMenu;
@synthesize useICloudItem;
@synthesize rememberPasswordItem;
@synthesize savePasswordItem;
@synthesize passwordWindow;
@synthesize key;
2012-06-24 14:29:51 +00:00
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfour-char-constants"
2013-04-20 18:11:19 +00:00
static EventHotKeyID MPShowHotKey = { .signature = 'show', .id = 1 };
static EventHotKeyID MPLockHotKey = { .signature = 'lock', .id = 1 };
2012-06-24 14:29:51 +00:00
#pragma clang diagnostic pop
+ (void)initialize {
2012-06-08 21:46:13 +00:00
static dispatch_once_t initialize;
2013-04-20 18:11:19 +00:00
dispatch_once( &initialize, ^{
[MPMacConfig get];
2012-06-08 21:46:13 +00:00
2013-04-20 18:11:19 +00:00
#ifdef DEBUG
[PearlLogger get].printLevel = PearlLogLevelDebug;//Trace;
2013-04-20 18:11:19 +00:00
#endif
} );
}
2012-06-08 21:46:13 +00:00
static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
// Extract the hotkey ID.
2012-06-08 21:46:13 +00:00
EventHotKeyID hotKeyID;
2013-04-20 18:11:19 +00:00
GetEventParameter( theEvent, kEventParamDirectObject, typeEventHotKeyID,
NULL, sizeof(hotKeyID), NULL, &hotKeyID );
2012-06-08 21:46:13 +00:00
// Check which hotkey this was.
if (hotKeyID.signature == MPShowHotKey.signature && hotKeyID.id == MPShowHotKey.id) {
[((__bridge MPAppDelegate *)userData) activate:nil];
return noErr;
}
if (hotKeyID.signature == MPLockHotKey.signature && hotKeyID.id == MPLockHotKey.id) {
[((__bridge MPAppDelegate *)userData) lock:nil];
return noErr;
}
2012-06-08 21:46:13 +00:00
return eventNotHandledErr;
}
- (void)updateUsers {
2013-04-20 18:11:19 +00:00
[[[self.usersItem submenu] itemArray] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if (idx > 1)
[[self.usersItem submenu] removeItem:obj];
}];
NSManagedObjectContext *moc = [MPAppDelegate managedObjectContextForThreadIfReady];
if (!moc) {
self.createUserItem.title = @"New User (Not ready)";
self.createUserItem.enabled = NO;
self.createUserItem.toolTip = @"Please wait until the app is fully loaded.";
[self.usersItem.submenu addItemWithTitle:@"Loading..." action:NULL keyEquivalent:@""].enabled = NO;
return;
}
self.createUserItem.title = @"New User";
self.createUserItem.enabled = YES;
self.createUserItem.toolTip = nil;
2013-04-20 18:11:19 +00:00
NSError *error = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass( [MPUserEntity class] )];
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"lastUsed" ascending:NO] ];
NSArray *users = [moc executeFetchRequest:fetchRequest error:&error];
if (!users)
2013-04-20 18:11:19 +00:00
err(@"Failed to load users: %@", error);
if (![users count]) {
NSMenuItem *noUsersItem = [self.usersItem.submenu addItemWithTitle:@"No users" action:NULL keyEquivalent:@""];
noUsersItem.enabled = NO;
noUsersItem.toolTip = @"Use the iOS app to create users and make sure iCloud is enabled in its preferences as well. "
2013-04-20 18:11:19 +00:00
@"Then give iCloud some time to sync the new user to your Mac.";
}
2013-04-20 18:11:19 +00:00
for (MPUserEntity *user in users) {
NSMenuItem *userItem = [[NSMenuItem alloc] initWithTitle:user.name action:@selector(selectUser:) keyEquivalent:@""];
[userItem setTarget:self];
[userItem setRepresentedObject:[user objectID]];
[[self.usersItem submenu] addItem:userItem];
2013-04-20 18:11:19 +00:00
if ([user.name isEqualToString:[MPMacConfig get].usedUserName])
[self selectUser:userItem];
}
}
- (void)selectUser:(NSMenuItem *)item {
2013-04-20 18:11:19 +00:00
self.activeUser = (MPUserEntity *)[[MPAppDelegate managedObjectContextForThreadIfReady] objectRegisteredForID:[item representedObject]];
}
- (void)showMenu {
2012-06-08 21:46:13 +00:00
[self updateMenuItems];
2012-06-08 21:46:13 +00:00
[self.statusItem popUpStatusItemMenu:self.statusMenu];
}
- (IBAction)activate:(id)sender {
2012-06-08 21:46:13 +00:00
2013-04-21 21:05:59 +00:00
if (![self activeUserForThread])
2013-04-20 18:11:19 +00:00
// No user, can't activate.
return;
if ([[NSApplication sharedApplication] isActive])
[self applicationDidBecomeActive:nil];
else
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
- (IBAction)togglePreference:(NSMenuItem *)sender {
2012-06-08 21:46:13 +00:00
if (sender == useICloudItem)
2013-04-21 21:05:59 +00:00
[self storeManager].cloudEnabled = sender.state == NSOnState;
if (sender == rememberPasswordItem)
[MPConfig get].rememberLogin = [NSNumber numberWithBool:![[MPConfig get].rememberLogin boolValue]];
if (sender == savePasswordItem) {
2013-04-21 21:05:59 +00:00
MPUserEntity *activeUser = [[MPAppDelegate get] activeUserForThread];
if ((activeUser.saveKey = !activeUser.saveKey))
[[MPAppDelegate get] storeSavedKeyFor:activeUser];
else
[[MPAppDelegate get] forgetSavedKeyFor:activeUser];
2013-04-21 21:05:59 +00:00
[activeUser.managedObjectContext saveToStore];
}
}
- (IBAction)newUser:(NSMenuItem *)sender {
}
- (IBAction)signOut:(id)sender {
2013-04-20 18:11:19 +00:00
[self signOutAnimated:YES];
}
- (IBAction)lock:(id)sender {
self.key = nil;
}
- (void)didUpdateConfigForKey:(SEL)configKey fromValue:(id)oldValue {
2013-04-20 18:11:19 +00:00
[[NSNotificationCenter defaultCenter]
postNotificationName:MPCheckConfigNotification object:NSStringFromSelector( configKey ) userInfo:nil];
}
#pragma mark - NSApplicationDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
2012-06-08 21:46:13 +00:00
// Setup delegates and listeners.
[MPConfig get].delegate = self;
__weak id weakSelf = self;
[self addObserverBlock:^(NSString *keyPath, id object, NSDictionary *change, void *context) {
[weakSelf updateMenuItems];
2013-04-20 18:11:19 +00:00
} forKeyPath:@"key" options:NSKeyValueObservingOptionInitial context:nil];
[self addObserverBlock:^(NSString *keyPath, id object, NSDictionary *change, void *context) {
[weakSelf updateMenuItems];
2013-04-20 18:11:19 +00:00
} forKeyPath:@"activeUser" options:NSKeyValueObservingOptionInitial context:nil];
2012-06-08 21:46:13 +00:00
// Status item.
2013-04-20 18:11:19 +00:00
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
self.statusItem.image = [NSImage imageNamed:@"menu-icon"];
self.statusItem.highlightMode = YES;
2013-04-20 18:11:19 +00:00
self.statusItem.target = self;
self.statusItem.action = @selector(showMenu);
__weak MPAppDelegate *wSelf = self;
[self addObserverBlock:^(NSString *keyPath, id object, NSDictionary *change, void *context) {
2013-04-21 21:05:59 +00:00
MPUserEntity *activeUser = [wSelf activeUserForThread];
[[[wSelf.usersItem submenu] itemArray] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([[obj representedObject] isEqual:[activeUser objectID]])
[obj setState:NSOnState];
else
[obj setState:NSOffState];
}];
[MPMacConfig get].usedUserName = activeUser.name;
} forKeyPath:@"activeUserObjectID" options:0 context:nil];
[[NSNotificationCenter defaultCenter] addObserverForName:UbiquityManagedStoreDidChangeNotification object:nil queue:nil usingBlock:
2013-04-20 18:11:19 +00:00
^(NSNotification *note) {
[self updateUsers];
}];
[[NSNotificationCenter defaultCenter]
addObserverForName:UbiquityManagedStoreDidImportChangesNotification object:nil queue:nil usingBlock:
^(NSNotification *note) {
[self updateUsers];
}];
[[NSNotificationCenter defaultCenter] addObserverForName:MPCheckConfigNotification object:nil queue:nil usingBlock:
2013-04-20 18:11:19 +00:00
^(NSNotification *note) {
self.rememberPasswordItem.state = [[MPConfig get].rememberLogin boolValue]? NSOnState: NSOffState;
2013-04-21 21:05:59 +00:00
self.savePasswordItem.state = [[MPAppDelegate get] activeUserForThread].saveKey? NSOnState: NSOffState;
2013-04-20 18:11:19 +00:00
}];
[self updateUsers];
2012-06-08 21:46:13 +00:00
// Global hotkey.
EventHotKeyRef hotKeyRef;
2013-04-20 18:11:19 +00:00
EventTypeSpec hotKeyEvents[1] = { { .eventClass = kEventClassKeyboard, .eventKind = kEventHotKeyPressed } };
OSStatus status = InstallApplicationEventHandler(NewEventHandlerUPP( MPHotKeyHander ), GetEventTypeCount( hotKeyEvents ),
hotKeyEvents,
(__bridge void *)self, NULL);
2012-06-08 21:46:13 +00:00
if (status != noErr)
err(@"Error installing application event handler: %d", status);
2013-04-20 18:11:19 +00:00
status = RegisterEventHotKey( 35 /* p */, controlKey + cmdKey, MPShowHotKey, GetApplicationEventTarget(), 0, &hotKeyRef );
2012-06-08 21:46:13 +00:00
if (status != noErr)
err(@"Error registering 'show' hotkey: %d", status);
2013-04-20 18:11:19 +00:00
status = RegisterEventHotKey( 35 /* p */, controlKey + optionKey + cmdKey, MPLockHotKey, GetApplicationEventTarget(), 0, &hotKeyRef );
if (status != noErr)
err(@"Error registering 'lock' hotkey: %d", status);
}
- (void)updateMenuItems {
2013-04-21 21:05:59 +00:00
MPUserEntity *activeUser = [self activeUserForThread];
if (!(self.showItem.enabled = ![self.passwordWindow.window isVisible])) {
2013-04-20 18:11:19 +00:00
self.showItem.title = @"Show (Showing)";
self.showItem.toolTip = @"Master Password is already showing.";
2013-04-20 18:11:19 +00:00
}
2013-04-21 21:05:59 +00:00
else if (!(self.showItem.enabled = (activeUser != nil))) {
2013-04-20 18:11:19 +00:00
self.showItem.title = @"Show (No user)";
self.showItem.toolTip = @"First select the user to show passwords for.";
2013-04-20 18:11:19 +00:00
}
else {
self.showItem.title = @"Show";
self.showItem.toolTip = nil;
}
if (self.key) {
2013-04-20 18:11:19 +00:00
self.lockItem.title = @"Lock";
self.lockItem.enabled = YES;
self.lockItem.toolTip = nil;
2013-04-20 18:11:19 +00:00
}
else {
self.lockItem.title = @"Lock (Locked)";
self.lockItem.enabled = NO;
self.lockItem.toolTip = @"Master Password is currently locked.";
}
self.rememberPasswordItem.state = [[MPConfig get].rememberLogin boolValue]? NSOnState: NSOffState;
2013-04-21 21:05:59 +00:00
self.savePasswordItem.state = activeUser.saveKey? NSOnState: NSOffState;
if (!activeUser) {
2013-04-20 18:11:19 +00:00
self.savePasswordItem.title = @"Save Password (No user)";
self.savePasswordItem.enabled = NO;
self.savePasswordItem.toolTip = @"First select your user and unlock by showing the Master Password window.";
2013-04-20 18:11:19 +00:00
}
else if (!self.key) {
self.savePasswordItem.title = @"Save Password (Locked)";
self.savePasswordItem.enabled = NO;
self.savePasswordItem.toolTip = @"First unlock by showing the Master Password window.";
2013-04-20 18:11:19 +00:00
}
else {
self.savePasswordItem.title = @"Save Password";
self.savePasswordItem.enabled = YES;
self.savePasswordItem.toolTip = nil;
}
2013-04-21 21:05:59 +00:00
self.useICloudItem.state = self.storeManager.cloudEnabled? NSOnState: NSOffState;
self.useICloudItem.enabled = !self.storeManager.cloudEnabled;
if (self.storeManager.cloudEnabled) {
2013-04-20 18:11:19 +00:00
self.useICloudItem.title = @"Use iCloud (Required)";
self.useICloudItem.toolTip = @"iCloud is required in this version. Future versions will work without iCloud as well.";
}
else {
2013-04-20 18:11:19 +00:00
self.useICloudItem.title = @"Use iCloud (Required)";
self.useICloudItem.toolTip = nil;
}
}
- (void)applicationWillBecomeActive:(NSNotification *)notification {
2012-06-08 21:46:13 +00:00
if (!self.passwordWindow)
self.passwordWindow = [[MPPasswordWindowController alloc] initWithWindowNibName:@"MPPasswordWindowController"];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
2012-06-08 21:46:13 +00:00
[self.passwordWindow showWindow:self];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
2012-06-08 21:46:13 +00:00
if (![[MPConfig get].rememberLogin boolValue])
self.key = nil;
}
2012-06-08 21:46:13 +00:00
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
// Save changes in the application's managed object context before the application terminates.
2012-06-08 21:46:13 +00:00
NSManagedObjectContext *moc = [MPAppDelegate managedObjectContextForThreadIfReady];
if (!moc)
return NSTerminateNow;
2012-06-08 21:46:13 +00:00
if (![moc commitEditing])
return NSTerminateCancel;
2012-06-08 21:46:13 +00:00
if (![moc hasChanges])
return NSTerminateNow;
2012-06-08 21:46:13 +00:00
2013-04-21 21:05:59 +00:00
[moc saveToStore];
return NSTerminateNow;
}
#pragma mark - UbiquityStoreManagerDelegate
2013-04-21 21:05:59 +00:00
- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager willLoadStoreIsCloud:(BOOL)isCloudStore {
2013-04-21 21:05:59 +00:00
manager.cloudEnabled = YES;
2012-06-08 21:46:13 +00:00
2013-04-21 21:05:59 +00:00
[super ubiquityStoreManager:manager willLoadStoreIsCloud:isCloudStore];
}
@end