2012-03-04 14:31:26 +00:00
|
|
|
//
|
2013-04-25 01:23:53 +00:00
|
|
|
// MPMacAppDelegate.m
|
2012-03-04 14:31:26 +00:00
|
|
|
// MasterPassword
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 04/03/12.
|
|
|
|
// Copyright (c) 2012 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2013-04-25 01:23:53 +00:00
|
|
|
#import "MPMacAppDelegate.h"
|
2012-05-13 08:24:19 +00:00
|
|
|
#import "MPAppDelegate_Key.h"
|
|
|
|
#import "MPAppDelegate_Store.h"
|
2012-05-04 22:15:51 +00:00
|
|
|
#import <Carbon/Carbon.h>
|
2013-06-08 22:12:49 +00:00
|
|
|
#import <ServiceManagement/ServiceManagement.h>
|
|
|
|
|
|
|
|
#define LOGIN_HELPER_BUNDLE_ID @"com.lyndir.lhunath.MasterPassword.Mac.LoginHelper"
|
2012-03-04 14:31:26 +00:00
|
|
|
|
2014-02-22 23:27:14 +00:00
|
|
|
@interface UbiquityStoreManager (Private)
|
|
|
|
|
|
|
|
- (void)markCloudStoreCorrupted;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-05-19 20:55:43 +00:00
|
|
|
@interface MPMacAppDelegate()
|
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
@property(nonatomic, strong) NSWindowController *initialWindow;
|
2013-05-19 20:55:43 +00:00
|
|
|
@end
|
|
|
|
|
2013-04-25 01:23:53 +00:00
|
|
|
@implementation MPMacAppDelegate
|
2013-04-20 18:11:19 +00:00
|
|
|
|
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
|
2012-05-04 22:15:51 +00:00
|
|
|
|
2012-03-06 00:04:19 +00:00
|
|
|
+ (void)initialize {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-01-17 05:37:20 +00:00
|
|
|
static dispatch_once_t initialize;
|
2013-04-20 18:11:19 +00:00
|
|
|
dispatch_once( &initialize, ^{
|
2013-01-17 05:37:20 +00:00
|
|
|
[MPMacConfig get];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-04-20 18:11:19 +00:00
|
|
|
#ifdef DEBUG
|
2013-01-27 03:05:57 +00:00
|
|
|
[PearlLogger get].printLevel = PearlLogLevelDebug;//Trace;
|
2013-04-20 18:11:19 +00:00
|
|
|
#endif
|
|
|
|
} );
|
2012-03-06 00:04:19 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:46:13 +00:00
|
|
|
static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
|
|
|
|
|
2012-05-04 22:15:51 +00:00
|
|
|
// 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
|
|
|
|
2012-05-04 22:15:51 +00:00
|
|
|
// Check which hotkey this was.
|
|
|
|
if (hotKeyID.signature == MPShowHotKey.signature && hotKeyID.id == MPShowHotKey.id) {
|
2013-05-16 04:19:50 +00:00
|
|
|
[((__bridge MPMacAppDelegate *)userData) showPasswordWindow:nil];
|
2012-05-04 22:15:51 +00:00
|
|
|
return noErr;
|
|
|
|
}
|
2013-01-17 05:37:20 +00:00
|
|
|
if (hotKeyID.signature == MPLockHotKey.signature && hotKeyID.id == MPLockHotKey.id) {
|
2013-04-25 01:23:53 +00:00
|
|
|
[((__bridge MPMacAppDelegate *)userData) lock:nil];
|
2013-01-17 05:37:20 +00:00
|
|
|
return noErr;
|
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-04 22:15:51 +00:00
|
|
|
return eventNotHandledErr;
|
|
|
|
}
|
|
|
|
|
2012-10-31 02:54:34 +00:00
|
|
|
- (void)updateUsers {
|
2013-04-20 18:11:19 +00:00
|
|
|
|
2012-10-31 02:54:34 +00:00
|
|
|
[[[self.usersItem submenu] itemArray] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
|
|
|
if (idx > 1)
|
2013-01-17 05:37:20 +00:00
|
|
|
[[self.usersItem submenu] removeItem:obj];
|
2012-10-31 02:54:34 +00:00
|
|
|
}];
|
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
NSManagedObjectContext *context = [MPMacAppDelegate managedObjectContextForMainThreadIfReady];
|
|
|
|
if (!context) {
|
2013-01-17 05:37:20 +00:00
|
|
|
self.createUserItem.title = @"New User (Not ready)";
|
|
|
|
self.createUserItem.enabled = NO;
|
2012-11-01 14:55:11 +00:00
|
|
|
self.createUserItem.toolTip = @"Please wait until the app is fully loaded.";
|
2013-01-17 05:37:20 +00:00
|
|
|
[self.usersItem.submenu addItemWithTitle:@"Loading..." action:NULL keyEquivalent:@""].enabled = NO;
|
|
|
|
|
2012-10-31 02:54:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-01-17 05:37:20 +00:00
|
|
|
self.createUserItem.title = @"New User";
|
|
|
|
self.createUserItem.enabled = YES;
|
2012-11-01 14:55:11 +00:00
|
|
|
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] ];
|
2013-06-16 16:39:52 +00:00
|
|
|
NSArray *users = [context executeFetchRequest:fetchRequest error:&error];
|
2013-01-31 05:42:32 +00:00
|
|
|
if (!users)
|
2013-04-20 18:11:19 +00:00
|
|
|
err(@"Failed to load users: %@", error);
|
|
|
|
|
2013-01-31 05:42:32 +00:00
|
|
|
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-01-31 05:42:32 +00:00
|
|
|
}
|
2013-04-20 18:11:19 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
MPUserEntity *activeUser = [self activeUserInContext:context];
|
2013-01-31 05:42:32 +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
|
|
|
|
2013-05-10 15:13:55 +00:00
|
|
|
if (!activeUser && [user.name isEqualToString:[MPMacConfig get].usedUserName])
|
2013-01-31 05:42:32 +00:00
|
|
|
[self selectUser:userItem];
|
|
|
|
}
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-05-10 15:13:55 +00:00
|
|
|
[self updateMenuItems];
|
2012-10-31 02:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)selectUser:(NSMenuItem *)item {
|
2013-04-20 18:11:19 +00:00
|
|
|
|
2013-05-07 04:45:06 +00:00
|
|
|
[self signOutAnimated:NO];
|
|
|
|
|
2013-04-24 00:38:56 +00:00
|
|
|
NSError *error = nil;
|
2013-06-16 16:39:52 +00:00
|
|
|
NSManagedObjectContext *context = [MPMacAppDelegate managedObjectContextForMainThreadIfReady];
|
|
|
|
self.activeUser = (MPUserEntity *)[context existingObjectWithID:[item representedObject] error:&error];
|
2013-04-24 00:38:56 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
err(@"While looking up selected user: %@", error);
|
2012-10-31 02:54:34 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 11:32:09 +00:00
|
|
|
- (void)showMenu {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-01-17 05:37:20 +00:00
|
|
|
[self updateMenuItems];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-06-04 04:56:19 +00:00
|
|
|
[self.statusView popUpMenu];
|
2012-05-05 11:32:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
- (IBAction)togglePreference:(id)sender {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2014-02-22 23:27:14 +00:00
|
|
|
if (sender == self.enableCloudButton) {
|
|
|
|
if (([self storeManager].cloudEnabled = self.enableCloudButton.state == NSOnState)) {
|
|
|
|
NSAlert *alert = [NSAlert new];
|
|
|
|
alert.messageText = @"iCloud Enabled";
|
|
|
|
alert.informativeText = @"If you already have a user on another iCloud-enabled device, "
|
|
|
|
@"it may take a moment for that user to sync down to this device.";
|
|
|
|
[alert runModal];
|
|
|
|
}
|
|
|
|
}
|
2013-06-08 22:12:49 +00:00
|
|
|
if (sender == self.useCloudItem)
|
2014-02-22 23:27:14 +00:00
|
|
|
[self storeManager].cloudEnabled = self.useCloudItem.state != NSOnState;
|
2013-04-24 04:25:51 +00:00
|
|
|
if (sender == self.rememberPasswordItem)
|
2012-10-31 02:54:34 +00:00
|
|
|
[MPConfig get].rememberLogin = [NSNumber numberWithBool:![[MPConfig get].rememberLogin boolValue]];
|
2013-06-08 22:12:49 +00:00
|
|
|
if (sender == self.openAtLoginButton)
|
2014-02-22 23:27:14 +00:00
|
|
|
[self setLoginItemEnabled:self.openAtLoginButton.state == NSOnState];
|
2013-06-08 22:12:49 +00:00
|
|
|
if (sender == self.openAtLoginItem)
|
2014-02-22 23:27:14 +00:00
|
|
|
[self setLoginItemEnabled:self.openAtLoginItem.state != NSOnState];
|
2013-04-24 04:25:51 +00:00
|
|
|
if (sender == self.savePasswordItem) {
|
2013-05-16 02:42:21 +00:00
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlockAndWait:^(NSManagedObjectContext *context) {
|
|
|
|
MPUserEntity *activeUser = [[MPMacAppDelegate get] activeUserInContext:context];
|
|
|
|
if ((activeUser.saveKey = !activeUser.saveKey))
|
|
|
|
[[MPMacAppDelegate get] storeSavedKeyFor:activeUser];
|
|
|
|
else
|
|
|
|
[[MPMacAppDelegate get] forgetSavedKeyFor:activeUser];
|
|
|
|
[context saveToStore];
|
|
|
|
}];
|
2012-11-01 14:55:11 +00:00
|
|
|
}
|
2013-04-24 04:25:51 +00:00
|
|
|
if (sender == self.dialogStyleRegular)
|
|
|
|
[MPMacConfig get].dialogStyleHUD = @NO;
|
|
|
|
if (sender == self.dialogStyleHUD)
|
|
|
|
[MPMacConfig get].dialogStyleHUD = @YES;
|
2012-10-31 02:54:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)newUser:(NSMenuItem *)sender {
|
2013-05-03 00:40:12 +00:00
|
|
|
|
|
|
|
NSAlert *alert = [NSAlert alertWithMessageText:@"New User"
|
|
|
|
defaultButton:@"Create User" alternateButton:nil otherButton:@"Cancel"
|
|
|
|
informativeTextWithFormat:@"To begin, enter your full name.\n\n"
|
2013-06-08 22:12:49 +00:00
|
|
|
@"IMPORTANT: Enter your name correctly, including the right capitalization, "
|
|
|
|
@"as you would on an official document."];
|
2013-05-03 00:40:12 +00:00
|
|
|
NSTextField *nameField = [[NSTextField alloc] initWithFrame:NSMakeRect( 0, 0, 200, 22 )];
|
|
|
|
[alert setAccessoryView:nameField];
|
|
|
|
[alert layout];
|
|
|
|
[nameField becomeFirstResponder];
|
|
|
|
if ([alert runModal] != NSAlertDefaultReturn)
|
|
|
|
return;
|
|
|
|
|
|
|
|
NSString *name = [(NSSecureTextField *)alert.accessoryView stringValue];
|
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *moc) {
|
|
|
|
MPUserEntity *newUser = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass( [MPUserEntity class] )
|
|
|
|
inManagedObjectContext:moc];
|
|
|
|
newUser.name = name;
|
|
|
|
[moc saveToStore];
|
|
|
|
NSError *error = nil;
|
|
|
|
if (![moc obtainPermanentIDsForObjects:@[ newUser ] error:&error])
|
|
|
|
err(@"Failed to obtain permanent object ID for new user: %@", error);
|
|
|
|
|
|
|
|
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
|
|
|
|
[self updateUsers];
|
|
|
|
[self setActiveUser:newUser];
|
2013-05-16 04:19:50 +00:00
|
|
|
[self showPasswordWindow:nil];
|
2013-05-03 00:40:12 +00:00
|
|
|
}];
|
|
|
|
}];
|
2012-10-31 02:54:34 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 03:05:57 +00:00
|
|
|
- (IBAction)lock:(id)sender {
|
|
|
|
|
|
|
|
self.key = nil;
|
|
|
|
}
|
|
|
|
|
2013-05-10 15:13:55 +00:00
|
|
|
- (IBAction)rebuildCloud:(id)sender {
|
|
|
|
|
2014-02-22 23:27:14 +00:00
|
|
|
if ([[NSAlert alertWithMessageText:@"iCloud Truth Push" defaultButton:@"Continue"
|
2013-05-10 15:13:55 +00:00
|
|
|
alternateButton:nil otherButton:@"Cancel"
|
2014-02-22 23:27:14 +00:00
|
|
|
informativeTextWithFormat:@"This action will force all your iCloud enabled devices to switch to this device's version of the truth."
|
2013-05-10 15:13:55 +00:00
|
|
|
@"\n\nThis is only necessary if you notice that your devices aren't syncing properly anymore. "
|
|
|
|
"Any data on other devices not available from here will be lost."] runModal] == NSAlertDefaultReturn)
|
|
|
|
[self.storeManager rebuildCloudContentFromCloudStoreOrLocalStore:NO];
|
|
|
|
}
|
|
|
|
|
2014-02-22 23:27:14 +00:00
|
|
|
- (IBAction)corruptCloud:(id)sender {
|
|
|
|
|
|
|
|
if ([[NSAlert alertWithMessageText:@"iCloud Truth Pull" defaultButton:@"Continue"
|
|
|
|
alternateButton:nil otherButton:@"Cancel"
|
|
|
|
informativeTextWithFormat:@"This action will force another iCloud enabled device to push their version of the truth on all."
|
|
|
|
@"\n\nThis is only necessary if you notice that your devices aren't syncing properly anymore. "
|
|
|
|
"Any data on this device not available from the other will be lost."] runModal] == NSAlertDefaultReturn)
|
|
|
|
[self.storeManager markCloudStoreCorrupted];
|
|
|
|
}
|
|
|
|
|
2013-05-19 20:55:43 +00:00
|
|
|
- (IBAction)terminate:(id)sender {
|
|
|
|
|
|
|
|
[self.passwordWindow close];
|
|
|
|
self.passwordWindow = nil;
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-05-19 20:55:43 +00:00
|
|
|
[NSApp terminate:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (IBAction)iphoneAppStore:(id)sender {
|
|
|
|
|
|
|
|
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://itunes.apple.com/app/id510296984"]];
|
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
[self.initialWindow close];
|
|
|
|
self.initialWindow = nil;
|
2013-05-19 20:55:43 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 23:02:55 +00:00
|
|
|
- (void)didUpdateConfigForKey:(SEL)configKey fromValue:(id)oldValue {
|
|
|
|
|
2014-04-26 18:03:44 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MPCheckConfigNotification object:NSStringFromSelector( configKey )];
|
2012-05-09 23:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - NSApplicationDelegate
|
|
|
|
|
2012-03-05 21:19:05 +00:00
|
|
|
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
|
2013-04-24 04:25:51 +00:00
|
|
|
|
2012-05-08 11:41:54 +00:00
|
|
|
// Setup delegates and listeners.
|
|
|
|
[MPConfig get].delegate = self;
|
2013-01-17 05:37:20 +00:00
|
|
|
__weak id weakSelf = self;
|
2012-11-01 14:55:11 +00:00
|
|
|
[self addObserverBlock:^(NSString *keyPath, id object, NSDictionary *change, void *context) {
|
2013-06-27 00:23:02 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[weakSelf updateMenuItems];
|
|
|
|
});
|
2013-05-31 04:14:04 +00:00
|
|
|
} forKeyPath:@"key" options:0 context:nil];
|
2013-01-17 05:37:20 +00:00
|
|
|
[self addObserverBlock:^(NSString *keyPath, id object, NSDictionary *change, void *context) {
|
2013-06-27 00:23:02 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[weakSelf updateMenuItems];
|
|
|
|
});
|
2013-05-31 04:14:04 +00:00
|
|
|
} forKeyPath:@"activeUser" options:0 context:nil];
|
|
|
|
[self addObserverBlock:^(NSString *keyPath, id object, NSDictionary *change, void *context) {
|
2013-06-27 00:23:02 +00:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
[weakSelf updateMenuItems];
|
|
|
|
});
|
2013-05-31 04:14:04 +00:00
|
|
|
} forKeyPath:@"storeManager.cloudAvailable" options:0 context:nil];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-04 22:15:51 +00:00
|
|
|
// Status item.
|
2013-06-04 04:56:19 +00:00
|
|
|
self.statusView = [[RHStatusItemView alloc] initWithStatusBarItem:
|
2013-06-08 22:12:49 +00:00
|
|
|
[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]];
|
2013-06-04 04:56:19 +00:00
|
|
|
self.statusView.image = [NSImage imageNamed:@"menu-icon"];
|
|
|
|
self.statusView.menu = self.statusMenu;
|
|
|
|
self.statusView.target = self;
|
|
|
|
self.statusView.action = @selector(showMenu);
|
2012-11-01 14:55:11 +00:00
|
|
|
|
2013-06-27 00:23:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:USMStoreDidChangeNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:
|
2013-04-20 18:11:19 +00:00
|
|
|
^(NSNotification *note) {
|
|
|
|
[self updateUsers];
|
|
|
|
}];
|
2013-06-27 00:23:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:USMStoreDidImportChangesNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:
|
2013-04-20 18:11:19 +00:00
|
|
|
^(NSNotification *note) {
|
|
|
|
[self updateUsers];
|
|
|
|
}];
|
2013-06-27 00:23:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:MPCheckConfigNotification object:nil
|
|
|
|
queue:[NSOperationQueue mainQueue] usingBlock:
|
2013-04-20 18:11:19 +00:00
|
|
|
^(NSNotification *note) {
|
|
|
|
self.rememberPasswordItem.state = [[MPConfig get].rememberLogin boolValue]? NSOnState: NSOffState;
|
2013-06-16 16:39:52 +00:00
|
|
|
self.savePasswordItem.state = [[MPMacAppDelegate get] activeUserForMainThread].saveKey? NSOnState: NSOffState;
|
2013-04-24 04:25:51 +00:00
|
|
|
self.dialogStyleRegular.state = ![[MPMacConfig get].dialogStyleHUD boolValue]? NSOnState: NSOffState;
|
|
|
|
self.dialogStyleHUD.state = [[MPMacConfig get].dialogStyleHUD boolValue]? NSOnState: NSOffState;
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-04-24 04:25:51 +00:00
|
|
|
if ([note.object isEqual:NSStringFromSelector( @selector(dialogStyleHUD) )]) {
|
|
|
|
if (![self.passwordWindow.window isVisible])
|
|
|
|
self.passwordWindow = nil;
|
|
|
|
else {
|
|
|
|
[self.passwordWindow close];
|
|
|
|
self.passwordWindow = nil;
|
2013-05-16 04:19:50 +00:00
|
|
|
[self showPasswordWindow:nil];
|
2013-04-24 04:25:51 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-20 18:11:19 +00:00
|
|
|
}];
|
2012-10-31 02:54:34 +00:00
|
|
|
[self updateUsers];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-04 22:15:51 +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 ),
|
2013-06-08 22:12:49 +00:00
|
|
|
hotKeyEvents, (__bridge void *)self, NULL);
|
2012-06-08 21:46:13 +00:00
|
|
|
if (status != noErr)
|
2013-06-16 16:39:52 +00:00
|
|
|
err(@"Error installing application event handler: %i", (int)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)
|
2013-06-16 16:39:52 +00:00
|
|
|
err(@"Error registering 'show' hotkey: %i", (int)status);
|
2013-04-20 18:11:19 +00:00
|
|
|
status = RegisterEventHotKey( 35 /* p */, controlKey + optionKey + cmdKey, MPLockHotKey, GetApplicationEventTarget(), 0, &hotKeyRef );
|
2013-01-17 05:37:20 +00:00
|
|
|
if (status != noErr)
|
2013-06-16 16:39:52 +00:00
|
|
|
err(@"Error registering 'lock' hotkey: %i", (int)status);
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-06-04 04:56:19 +00:00
|
|
|
// Initial display.
|
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
2014-02-22 23:27:14 +00:00
|
|
|
if ([[MPMacConfig get].firstRun boolValue]) {
|
|
|
|
self.initialWindow = [[NSWindowController alloc] initWithWindowNibName:@"MPInitialWindow" owner:self];
|
|
|
|
[self.initialWindow.window setLevel:NSFloatingWindowLevel];
|
|
|
|
[self.initialWindow showWindow:self];
|
|
|
|
}
|
2013-01-17 05:37:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-24 00:38:56 +00:00
|
|
|
- (void)setActiveUser:(MPUserEntity *)activeUser {
|
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
[super setActiveUser:activeUser];
|
2013-04-24 00:38:56 +00:00
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
self.usersItem.state = NSMixedState;
|
2013-04-24 00:38:56 +00:00
|
|
|
[[[self.usersItem submenu] itemArray] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
|
2013-06-08 22:12:49 +00:00
|
|
|
if ([[obj representedObject] isEqual:[activeUser objectID]]) {
|
2013-04-24 00:38:56 +00:00
|
|
|
[obj setState:NSOnState];
|
2013-06-08 22:12:49 +00:00
|
|
|
self.usersItem.state = NSOffState;
|
|
|
|
}
|
2013-04-24 00:38:56 +00:00
|
|
|
else
|
|
|
|
[obj setState:NSOffState];
|
|
|
|
}];
|
|
|
|
|
|
|
|
[MPMacConfig get].usedUserName = activeUser.name;
|
|
|
|
}
|
|
|
|
|
2013-01-17 05:37:20 +00:00
|
|
|
- (void)updateMenuItems {
|
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
MPUserEntity *activeUser = [self activeUserForMainThread];
|
2013-05-16 04:19:50 +00:00
|
|
|
// if (!(self.showItem.enabled = ![self.passwordWindow.window isVisible])) {
|
|
|
|
// self.showItem.title = @"Show (Showing)";
|
|
|
|
// self.showItem.toolTip = @"Master Password is already showing.";
|
|
|
|
// }
|
|
|
|
// else if (!(self.showItem.enabled = (activeUser != nil))) {
|
|
|
|
// self.showItem.title = @"Show (No user)";
|
|
|
|
// self.showItem.toolTip = @"First select the user to show passwords for.";
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// self.showItem.title = @"Show";
|
|
|
|
// self.showItem.toolTip = nil;
|
|
|
|
// }
|
2013-01-17 05:37:20 +00:00
|
|
|
|
|
|
|
if (self.key) {
|
2013-04-20 18:11:19 +00:00
|
|
|
self.lockItem.title = @"Lock";
|
2013-01-17 05:37:20 +00:00
|
|
|
self.lockItem.enabled = YES;
|
|
|
|
self.lockItem.toolTip = nil;
|
2013-04-20 18:11:19 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.lockItem.title = @"Lock (Locked)";
|
2013-01-17 05:37:20 +00:00
|
|
|
self.lockItem.enabled = NO;
|
|
|
|
self.lockItem.toolTip = @"Master Password is currently locked.";
|
|
|
|
}
|
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
BOOL loginItemEnabled = [self loginItemEnabled];
|
|
|
|
self.openAtLoginItem.state = loginItemEnabled? NSOnState: NSOffState;
|
|
|
|
self.openAtLoginButton.state = loginItemEnabled? NSOnState: NSOffState;
|
2013-01-17 05:37:20 +00:00
|
|
|
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)";
|
2013-01-17 05:37:20 +00:00
|
|
|
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)";
|
2013-01-17 05:37:20 +00:00
|
|
|
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";
|
2013-01-17 05:37:20 +00:00
|
|
|
self.savePasswordItem.enabled = YES;
|
|
|
|
self.savePasswordItem.toolTip = nil;
|
|
|
|
}
|
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
self.useCloudItem.state = self.storeManager.cloudEnabled? NSOnState: NSOffState;
|
|
|
|
self.enableCloudButton.state = self.storeManager.cloudEnabled? NSOnState: NSOffState;
|
|
|
|
self.useCloudItem.enabled = self.storeManager.cloudAvailable;
|
2013-05-31 04:14:04 +00:00
|
|
|
if (self.storeManager.cloudAvailable) {
|
2013-06-08 22:12:49 +00:00
|
|
|
self.useCloudItem.title = @"Use iCloud";
|
|
|
|
self.useCloudItem.toolTip = nil;
|
2013-05-31 04:14:04 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-06-08 22:12:49 +00:00
|
|
|
self.useCloudItem.title = @"Use iCloud (Unavailable)";
|
|
|
|
self.useCloudItem.toolTip = @"iCloud is not set up for your Mac user.";
|
2013-05-31 04:14:04 +00:00
|
|
|
}
|
2012-03-12 17:14:01 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 04:19:50 +00:00
|
|
|
- (IBAction)showPasswordWindow:(id)sender {
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-06-05 01:31:02 +00:00
|
|
|
[NSApp activateIgnoringOtherApps:YES];
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-05-11 12:54:49 +00:00
|
|
|
// If no user, can't activate.
|
2013-06-16 16:39:52 +00:00
|
|
|
if (![self activeUserForMainThread]) {
|
2013-06-08 22:12:49 +00:00
|
|
|
[[NSAlert alertWithMessageText:@"No User Selected" defaultButton:[PearlStrings get].commonButtonOkay alternateButton:nil
|
|
|
|
otherButton:nil informativeTextWithFormat:
|
|
|
|
@"Begin by selecting or creating your user from the status menu (●●●|) next to the clock."]
|
|
|
|
runModal];
|
2013-06-15 05:39:24 +00:00
|
|
|
[self.statusView popUpMenu];
|
2013-05-11 12:54:49 +00:00
|
|
|
return;
|
2013-06-04 04:56:19 +00:00
|
|
|
}
|
2013-05-11 12:54:49 +00:00
|
|
|
|
2013-04-24 00:38:56 +00:00
|
|
|
// Don't show window if we weren't already running (ie. if we haven't been activated before).
|
2013-05-10 15:13:55 +00:00
|
|
|
if (!self.passwordWindow)
|
|
|
|
self.passwordWindow = [[MPPasswordWindowController alloc] initWithWindowNibName:@"MPPasswordWindowController"];
|
2013-06-08 22:12:49 +00:00
|
|
|
|
2013-05-10 15:13:55 +00:00
|
|
|
[self.passwordWindow showWindow:self];
|
2012-05-08 11:41:54 +00:00
|
|
|
}
|
|
|
|
|
2013-06-08 22:12:49 +00:00
|
|
|
- (void)setLoginItemEnabled:(BOOL)enabled {
|
|
|
|
|
|
|
|
BOOL loginItemEnabled = [self loginItemEnabled];
|
|
|
|
if (loginItemEnabled != enabled) {
|
|
|
|
if (SMLoginItemSetEnabled( (__bridge CFStringRef)LOGIN_HELPER_BUNDLE_ID, (Boolean)enabled ) == true)
|
|
|
|
loginItemEnabled = enabled;
|
|
|
|
else
|
|
|
|
wrn(@"Failed to set login item.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self.openAtLoginItem.state = loginItemEnabled? NSOnState: NSOffState;
|
|
|
|
self.openAtLoginButton.state = loginItemEnabled? NSOnState: NSOffState;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)loginItemEnabled {
|
|
|
|
|
|
|
|
// The easy and sane method (SMJobCopyDictionary) can pose problems when the app is sandboxed. -_-
|
|
|
|
NSArray *jobs = (__bridge_transfer NSArray *)SMCopyAllJobDictionaries( kSMDomainUserLaunchd );
|
|
|
|
|
|
|
|
for (NSDictionary *job in jobs)
|
|
|
|
if ([LOGIN_HELPER_BUNDLE_ID isEqualToString:[job objectForKey:@"Label"]]) {
|
|
|
|
dbg(@"loginItemEnabled: %@", @([[job objectForKey:@"OnDemand"] boolValue]));
|
|
|
|
return [[job objectForKey:@"OnDemand"] boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg(@"loginItemEnabled: not found");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2012-05-08 11:41:54 +00:00
|
|
|
- (void)applicationWillResignActive:(NSNotification *)notification {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-10-31 02:54:34 +00:00
|
|
|
if (![[MPConfig get].rememberLogin boolValue])
|
2013-04-25 01:23:53 +00:00
|
|
|
[self lock:nil];
|
2012-05-08 11:41:54 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:46:13 +00:00
|
|
|
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
|
2012-03-04 14:31:26 +00:00
|
|
|
// Save changes in the application's managed object context before the application terminates.
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
NSManagedObjectContext *context = [MPMacAppDelegate managedObjectContextForMainThreadIfReady];
|
|
|
|
if (!context)
|
2012-03-04 14:31:26 +00:00
|
|
|
return NSTerminateNow;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
if (![context commitEditing])
|
2012-03-04 14:31:26 +00:00
|
|
|
return NSTerminateCancel;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
if (![context hasChanges])
|
2012-03-04 14:31:26 +00:00
|
|
|
return NSTerminateNow;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
[context saveToStore];
|
2012-03-04 14:31:26 +00:00
|
|
|
return NSTerminateNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|