diff --git a/MasterPassword/MPAppDelegate_Store.m b/MasterPassword/MPAppDelegate_Store.m index 3bbbf35d..e4488a40 100644 --- a/MasterPassword/MPAppDelegate_Store.m +++ b/MasterPassword/MPAppDelegate_Store.m @@ -181,6 +181,34 @@ static NSDateFormatter *rfc3339DateFormatter = nil; dbg(@"StoreManager: %@", message); } +- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didEncounterError:(NSError *)error cause:(UbiquityStoreManagerErrorCause)cause context:(id)context { + + err(@"StoreManager: cause=%d, context=%@, error=%@", cause, context, error); + + switch (cause) { + case UbiquityStoreManagerErrorCauseDeleteStore: + case UbiquityStoreManagerErrorCauseDeleteLogs: + case UbiquityStoreManagerErrorCauseCreateStorePath: + case UbiquityStoreManagerErrorCauseClearStore: + break; + case UbiquityStoreManagerErrorCauseOpenLocalStore: { + wrn(@"Local store could not be opened, resetting it."); + manager.hardResetEnabled = YES; + [manager hardResetLocalStorage]; + + [NSException raise:NSGenericException format:@"Local store was reset, application must be restarted to use it."]; + return; + } + case UbiquityStoreManagerErrorCauseOpenCloudStore: { + wrn(@"iCloud store could not be opened, resetting it."); + manager.hardResetEnabled = YES; + [manager hardResetCloudStorage]; + [manager useiCloudStore:YES alertUser:NO]; + break; + } + } +} + #pragma mark - Import / Export - (void)loadRFC3339DateFormatter { diff --git a/MasterPassword/Mac/MPAppDelegate.m b/MasterPassword/Mac/MPAppDelegate.m index b1877ade..7c00a897 100644 --- a/MasterPassword/Mac/MPAppDelegate.m +++ b/MasterPassword/Mac/MPAppDelegate.m @@ -80,22 +80,49 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven - (IBAction)togglePreference:(NSMenuItem *)sender { if (sender == useICloudItem) - [self.storeManager useiCloudStore:sender.state == NSOffState]; + [self.storeManager useiCloudStore:sender.state == NSOffState alertUser:YES]; if (sender == rememberPasswordItem) [MPConfig get].rememberKey = [NSNumber numberWithBool:![[MPConfig get].rememberKey boolValue]]; if (sender == savePasswordItem) [MPConfig get].saveKey = [NSNumber numberWithBool:![[MPConfig get].saveKey boolValue]]; } +- (void)didUpdateConfigForKey:(SEL)configKey fromValue:(id)oldValue { + + if (configKey == @selector(rememberKey)) + self.rememberPasswordItem.state = [[MPConfig get].rememberKey boolValue]? NSOnState: NSOffState; + if (configKey == @selector(saveKey)) + self.savePasswordItem.state = [[MPConfig get].saveKey boolValue]? NSOnState: NSOffState; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + + if ([keyPath isEqualToString:@"key"]) { + if (self.key) + [self.lockItem setEnabled:YES]; + else { + [self.lockItem setEnabled:NO]; + [self.passwordWindow close]; + } + } +} + +- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window { + + return [[self managedObjectContext] undoManager]; +} + +#pragma mark - NSApplicationDelegate + - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Setup delegates and listeners. [MPConfig get].delegate = self; [self addObserver:self forKeyPath:@"key" options:0 context:nil]; - + // Initially, use iCloud. if ([[MPConfig get].firstRun boolValue]) - [[self storeManager] useiCloudStore:YES]; + [[self storeManager] useiCloudStore:YES alertUser:YES]; // Status item. self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; @@ -137,36 +164,6 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven self.key = nil; } -- (void)didUpdateConfigForKey:(SEL)configKey fromValue:(id)oldValue { - - if (configKey == @selector(rememberKey)) - self.rememberPasswordItem.state = [[MPConfig get].rememberKey boolValue]? NSOnState: NSOffState; - if (configKey == @selector(saveKey)) - self.savePasswordItem.state = [[MPConfig get].saveKey boolValue]? NSOnState: NSOffState; -} - -- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didSwitchToiCloud:(BOOL)didSwitch { - - self.useICloudItem.state = didSwitch? NSOnState: NSOffState; -} - -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { - - if ([keyPath isEqualToString:@"key"]) { - if (self.key) - [self.lockItem setEnabled:YES]; - else { - [self.lockItem setEnabled:NO]; - [self.passwordWindow close]; - } - } -} - -- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window { - - return [[self managedObjectContext] undoManager]; -} - - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { // Save changes in the application's managed object context before the application terminates. @@ -213,4 +210,11 @@ static OSStatus MPHotKeyHander(EventHandlerCallRef nextHandler, EventRef theEven return NSTerminateNow; } +#pragma mark - UbiquityStoreManagerDelegate + +- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didSwitchToiCloud:(BOOL)didSwitch { + + self.useICloudItem.state = didSwitch? NSOnState: NSOffState; +} + @end diff --git a/MasterPassword/Mac/MPPasswordWindowController.m b/MasterPassword/Mac/MPPasswordWindowController.m index 966c5be7..a9c244d7 100644 --- a/MasterPassword/Mac/MPPasswordWindowController.m +++ b/MasterPassword/Mac/MPPasswordWindowController.m @@ -113,14 +113,10 @@ if (![query length] || ![MPAppDelegate get].keyID) return nil; - NSFetchRequest *fetchRequest = [MPAppDelegate.managedObjectModel - fetchRequestFromTemplateWithName:@"MPElements" - substitutionVariables:[NSDictionary dictionaryWithObjectsAndKeys: - query, @"query", - [MPAppDelegate get].keyID, @"keyID", - nil]]; - [fetchRequest setSortDescriptors: - [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"uses" ascending:NO]]]; + NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([MPElementEntity class])]; + fetchRequest.sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"uses" ascending:NO]]; + fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(%@ == '' OR name BEGINSWITH[cd] %@) AND keyID == %@", + query, query, [MPAppDelegate get].keyID]; NSError *error = nil; self.siteResults = [[MPAppDelegate managedObjectContext] executeFetchRequest:fetchRequest error:&error]; diff --git a/MasterPassword/iOS/MPAppDelegate.m b/MasterPassword/iOS/MPAppDelegate.m index 5dbc9eb0..61aa326e 100644 --- a/MasterPassword/iOS/MPAppDelegate.m +++ b/MasterPassword/iOS/MPAppDelegate.m @@ -401,33 +401,6 @@ inf(@"didSwitchToiCloud: %d", didSwitch); } -- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didEncounterError:(NSError *)error cause:(UbiquityStoreManagerErrorCause)cause context:(id)context { - - err(@"StoreManager: cause=%d, context=%@, error=%@", cause, context, error); - - switch (cause) { - case UbiquityStoreManagerErrorCauseDeleteStore: - case UbiquityStoreManagerErrorCauseDeleteLogs: - case UbiquityStoreManagerErrorCauseCreateStorePath: - case UbiquityStoreManagerErrorCauseClearStore: - break; - case UbiquityStoreManagerErrorCauseOpenLocalStore: { - wrn(@"Local store could not be opened, resetting it."); - manager.hardResetEnabled = YES; - [manager hardResetLocalStorage]; - exit(1); - break; - } - case UbiquityStoreManagerErrorCauseOpenCloudStore: { - wrn(@"iCloud store could not be opened, resetting it."); - manager.hardResetEnabled = YES; - [manager hardResetCloudStorage]; - [manager useiCloudStore:YES alertUser:NO]; - break; - } - } -} - #pragma mark - TestFlight