2012-05-07 20:18:01 +00:00
|
|
|
//
|
|
|
|
// MPAppDelegate.m
|
|
|
|
// MasterPassword
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 24/11/11.
|
|
|
|
// Copyright (c) 2011 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPAppDelegate_Store.h"
|
2012-06-11 21:39:50 +00:00
|
|
|
#import "LocalyticsSession.h"
|
2012-05-07 20:18:01 +00:00
|
|
|
|
2012-05-11 20:45:05 +00:00
|
|
|
@implementation MPAppDelegate_Shared (Store)
|
2012-05-07 20:18:01 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
static NSDateFormatter *rfc3339DateFormatter = nil;
|
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
#pragma mark - Core Data setup
|
2012-05-09 08:11:34 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
+ (NSManagedObjectContext *)managedObjectContext {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
return [[self get] managedObjectContext];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSManagedObjectModel *)managedObjectModel {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
return [[self get] managedObjectModel];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSManagedObjectModel *)managedObjectModel {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
static NSManagedObjectModel *managedObjectModel = nil;
|
|
|
|
if (managedObjectModel)
|
|
|
|
return managedObjectModel;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MasterPassword" withExtension:@"momd"];
|
|
|
|
return managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSManagedObjectContext *)managedObjectContext {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
static NSManagedObjectContext *managedObjectContext = nil;
|
|
|
|
if (managedObjectContext)
|
|
|
|
return managedObjectContext;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-06-10 19:43:18 +00:00
|
|
|
managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
|
|
|
|
[managedObjectContext performBlockAndWait:^{
|
|
|
|
managedObjectContext.persistentStoreCoordinator = [self persistentStoreCoordinator];
|
|
|
|
managedObjectContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy;
|
2012-05-07 20:18:01 +00:00
|
|
|
}];
|
2012-06-10 19:43:18 +00:00
|
|
|
|
|
|
|
return managedObjectContext;
|
2012-05-07 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-27 12:26:13 +00:00
|
|
|
// Start loading the store.
|
|
|
|
[self storeManager];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-06-10 19:43:18 +00:00
|
|
|
// Wait until the storeManager is ready.
|
|
|
|
while (![self storeManager].isReady)
|
|
|
|
[NSThread sleepForTimeInterval:0.1];
|
|
|
|
|
|
|
|
return [self storeManager].persistentStoreCoordinator;
|
2012-05-07 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (UbiquityStoreManager *)storeManager {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
static UbiquityStoreManager *storeManager = nil;
|
|
|
|
if (storeManager)
|
|
|
|
return storeManager;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
storeManager = [[UbiquityStoreManager alloc] initWithManagedObjectModel:[self managedObjectModel]
|
2012-06-08 21:46:13 +00:00
|
|
|
localStoreURL:[[self applicationFilesDirectory] URLByAppendingPathComponent:@"MasterPassword.sqlite"]
|
|
|
|
containerIdentifier:@"HL3Q45LX9N.com.lyndir.lhunath.MasterPassword.shared"
|
2012-05-07 20:18:01 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
2012-06-08 21:46:13 +00:00
|
|
|
additionalStoreOptions:[NSDictionary dictionaryWithObject:NSFileProtectionComplete
|
|
|
|
forKey:NSPersistentStoreFileProtectionKey]
|
2012-05-07 20:18:01 +00:00
|
|
|
#else
|
|
|
|
additionalStoreOptions:nil
|
|
|
|
#endif
|
2012-06-08 21:46:13 +00:00
|
|
|
];
|
2012-05-07 20:18:01 +00:00
|
|
|
storeManager.delegate = self;
|
|
|
|
#ifdef DEBUG
|
|
|
|
storeManager.hardResetEnabled = YES;
|
|
|
|
#endif
|
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification
|
|
|
|
object:[UIApplication sharedApplication] queue:nil
|
2012-06-08 21:46:13 +00:00
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
[storeManager checkiCloudStatus];
|
|
|
|
}];
|
2012-05-07 20:18:01 +00:00
|
|
|
#else
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationWillBecomeActiveNotification
|
|
|
|
object:[NSApplication sharedApplication] queue:nil
|
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
[storeManager checkiCloudStatus];
|
|
|
|
}];
|
2012-05-09 08:11:34 +00:00
|
|
|
#endif
|
2012-05-07 20:18:01 +00:00
|
|
|
#if TARGET_OS_IPHONE
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillTerminateNotification
|
|
|
|
object:[UIApplication sharedApplication] queue:nil
|
2012-06-08 21:46:13 +00:00
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
[self saveContext];
|
|
|
|
}];
|
2012-05-07 20:18:01 +00:00
|
|
|
#else
|
2012-05-09 08:11:34 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationWillTerminateNotification
|
|
|
|
object:[NSApplication sharedApplication] queue:nil
|
2012-05-07 20:18:01 +00:00
|
|
|
usingBlock:^(NSNotification *note) {
|
|
|
|
[self saveContext];
|
|
|
|
}];
|
2012-05-09 08:11:34 +00:00
|
|
|
#endif
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
return storeManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)saveContext {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
[self.managedObjectContext performBlock:^{
|
|
|
|
NSError *error = nil;
|
|
|
|
if ([self.managedObjectContext hasChanges])
|
|
|
|
if (![self.managedObjectContext save:&error])
|
2012-06-08 21:46:13 +00:00
|
|
|
err(@"While saving context: %@", error);
|
2012-05-07 20:18:01 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
#pragma mark - UbiquityStoreManagerDelegate
|
|
|
|
|
|
|
|
- (NSManagedObjectContext *)managedObjectContextForUbiquityStoreManager:(UbiquityStoreManager *)usm {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
return self.managedObjectContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager log:(NSString *)message {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-12 16:31:05 +00:00
|
|
|
dbg(@"[StoreManager] %@", message);
|
2012-05-09 17:34:00 +00:00
|
|
|
}
|
|
|
|
|
2012-05-20 18:52:13 +00:00
|
|
|
- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didSwitchToiCloud:(BOOL)iCloudEnabled {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-20 18:52:13 +00:00
|
|
|
// manager.iCloudEnabled is more reliable (eg. iOS tampers with didSwitch a bit)
|
|
|
|
iCloudEnabled = manager.iCloudEnabled;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-11 20:45:05 +00:00
|
|
|
#ifdef TESTFLIGHT_SDK_VERSION
|
2012-06-11 21:39:50 +00:00
|
|
|
[TestFlight passCheckpoint:iCloudEnabled? MPCheckpointCloudEnabled: MPCheckpointCloudDisabled];
|
2012-05-11 20:45:05 +00:00
|
|
|
#endif
|
2012-06-11 21:39:50 +00:00
|
|
|
[[LocalyticsSession sharedLocalyticsSession] tagEvent:iCloudEnabled? MPCheckpointCloudEnabled: MPCheckpointCloudDisabled
|
|
|
|
attributes:nil];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-20 18:52:13 +00:00
|
|
|
inf(@"Using iCloud? %@", iCloudEnabled? @"YES": @"NO");
|
|
|
|
[MPConfig get].iCloud = [NSNumber numberWithBool:iCloudEnabled];
|
2012-05-11 20:45:05 +00:00
|
|
|
}
|
|
|
|
|
2012-06-08 21:46:13 +00:00
|
|
|
- (void)ubiquityStoreManager:(UbiquityStoreManager *)manager didEncounterError:(NSError *)error cause:(UbiquityStoreManagerErrorCause)cause
|
|
|
|
context:(id)context {
|
|
|
|
|
2012-05-11 20:45:05 +00:00
|
|
|
#ifdef TESTFLIGHT_SDK_VERSION
|
2012-06-11 21:39:50 +00:00
|
|
|
[TestFlight passCheckpoint:PearlString(@"MPCheckpointMPErrorUbiquity_%d", cause)];
|
2012-05-11 15:04:51 +00:00
|
|
|
#endif
|
2012-05-09 23:02:55 +00:00
|
|
|
err(@"StoreManager: cause=%d, context=%@, error=%@", cause, context, error);
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 23:02:55 +00:00
|
|
|
switch (cause) {
|
|
|
|
case UbiquityStoreManagerErrorCauseDeleteStore:
|
|
|
|
case UbiquityStoreManagerErrorCauseDeleteLogs:
|
|
|
|
case UbiquityStoreManagerErrorCauseCreateStorePath:
|
|
|
|
case UbiquityStoreManagerErrorCauseClearStore:
|
|
|
|
break;
|
|
|
|
case UbiquityStoreManagerErrorCauseOpenLocalStore: {
|
2012-05-11 20:45:05 +00:00
|
|
|
#ifdef TESTFLIGHT_SDK_VERSION
|
2012-06-11 21:39:50 +00:00
|
|
|
[TestFlight passCheckpoint:MPCheckpointLocalStoreIncompatible];
|
2012-05-11 15:04:51 +00:00
|
|
|
#endif
|
2012-06-11 21:39:50 +00:00
|
|
|
[[LocalyticsSession sharedLocalyticsSession] tagEvent:MPCheckpointLocalStoreIncompatible
|
|
|
|
attributes:nil];
|
2012-05-09 23:02:55 +00:00
|
|
|
wrn(@"Local store could not be opened, resetting it.");
|
|
|
|
manager.hardResetEnabled = YES;
|
|
|
|
[manager hardResetLocalStorage];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 23:02:55 +00:00
|
|
|
[NSException raise:NSGenericException format:@"Local store was reset, application must be restarted to use it."];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
case UbiquityStoreManagerErrorCauseOpenCloudStore: {
|
2012-05-11 20:45:05 +00:00
|
|
|
#ifdef TESTFLIGHT_SDK_VERSION
|
2012-06-11 21:39:50 +00:00
|
|
|
[TestFlight passCheckpoint:MPCheckpointCloudStoreIncompatible];
|
2012-05-11 15:04:51 +00:00
|
|
|
#endif
|
2012-06-11 21:39:50 +00:00
|
|
|
[[LocalyticsSession sharedLocalyticsSession] tagEvent:MPCheckpointCloudStoreIncompatible
|
|
|
|
attributes:nil];
|
2012-05-09 23:02:55 +00:00
|
|
|
wrn(@"iCloud store could not be opened, resetting it.");
|
|
|
|
manager.hardResetEnabled = YES;
|
|
|
|
[manager hardResetCloudStorage];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
#pragma mark - Import / Export
|
|
|
|
|
|
|
|
- (void)loadRFC3339DateFormatter {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
if (rfc3339DateFormatter)
|
|
|
|
return;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 17:34:00 +00:00
|
|
|
rfc3339DateFormatter = [NSDateFormatter new];
|
|
|
|
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
|
|
|
|
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
|
|
|
|
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
|
|
|
|
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
|
|
|
|
}
|
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
- (MPImportResult)importSites:(NSString *)importedSitesString withPassword:(NSString *)password
|
|
|
|
askConfirmation:(BOOL(^)(NSUInteger importCount, NSUInteger deleteCount))confirmation {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
[self loadRFC3339DateFormatter];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
static NSRegularExpression *headerPattern, *sitePattern;
|
|
|
|
__autoreleasing NSError *error;
|
|
|
|
if (!headerPattern) {
|
|
|
|
headerPattern = [[NSRegularExpression alloc]
|
2012-06-08 21:46:13 +00:00
|
|
|
initWithPattern:@"^#[[:space:]]*([^:]+): (.*)"
|
|
|
|
options:0 error:&error];
|
2012-05-09 08:11:34 +00:00
|
|
|
if (error)
|
2012-06-08 21:46:13 +00:00
|
|
|
err(@"Error loading the header pattern: %@", error);
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
|
|
|
if (!sitePattern) {
|
|
|
|
sitePattern = [[NSRegularExpression alloc]
|
2012-06-08 21:46:13 +00:00
|
|
|
initWithPattern:@"^([^[:space:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([[:digit:]]+)[[:space:]]+([^\t]+)\t(.*)"
|
|
|
|
options:0 error:&error];
|
2012-05-09 08:11:34 +00:00
|
|
|
if (error)
|
2012-06-08 21:46:13 +00:00
|
|
|
err(@"Error loading the site pattern: %@", error);
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
|
|
|
if (!headerPattern || !sitePattern)
|
|
|
|
return MPImportResultInternalError;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-06-04 09:27:02 +00:00
|
|
|
NSString *keyIDHex = nil, *userName = nil;
|
|
|
|
MPUserEntity *user = nil;
|
2012-05-09 08:11:34 +00:00
|
|
|
BOOL headerStarted = NO, headerEnded = NO;
|
2012-06-08 21:46:13 +00:00
|
|
|
NSArray *importedSiteLines = [importedSitesString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
|
|
|
|
NSMutableSet *elementsToDelete = [NSMutableSet set];
|
2012-05-09 08:11:34 +00:00
|
|
|
NSMutableArray *importedSiteElements = [NSMutableArray arrayWithCapacity:[importedSiteLines count]];
|
2012-06-08 21:46:13 +00:00
|
|
|
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([MPElementEntity class])];
|
|
|
|
for (NSString *importedSiteLine in importedSiteLines) {
|
2012-05-09 08:11:34 +00:00
|
|
|
if ([importedSiteLine hasPrefix:@"#"]) {
|
|
|
|
// Comment or header
|
|
|
|
if (!headerStarted) {
|
|
|
|
if ([importedSiteLine isEqualToString:@"##"])
|
|
|
|
headerStarted = YES;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (headerEnded)
|
|
|
|
continue;
|
|
|
|
if ([importedSiteLine isEqualToString:@"##"]) {
|
|
|
|
headerEnded = YES;
|
|
|
|
continue;
|
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Header
|
2012-05-11 07:05:11 +00:00
|
|
|
if ([headerPattern numberOfMatchesInString:importedSiteLine options:0 range:NSMakeRange(0, [importedSiteLine length])] != 1) {
|
2012-05-09 08:11:34 +00:00
|
|
|
err(@"Invalid header format in line: %@", importedSiteLine);
|
|
|
|
return MPImportResultMalformedInput;
|
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
NSTextCheckingResult *headerElements = [[headerPattern matchesInString:importedSiteLine options:0
|
|
|
|
range:NSMakeRange(0, [importedSiteLine length])] lastObject];
|
|
|
|
NSString *key = [importedSiteLine substringWithRange:[headerElements rangeAtIndex:1]];
|
|
|
|
NSString *value = [importedSiteLine substringWithRange:[headerElements rangeAtIndex:2]];
|
2012-06-04 09:27:02 +00:00
|
|
|
if ([key isEqualToString:@"User Name"]) {
|
|
|
|
userName = value;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-06-04 09:27:02 +00:00
|
|
|
NSFetchRequest *userFetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([MPUserEntity class])];
|
|
|
|
userFetchRequest.predicate = [NSPredicate predicateWithFormat:@"name == %@", userName];
|
|
|
|
user = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] lastObject];
|
|
|
|
}
|
2012-05-09 08:11:34 +00:00
|
|
|
if ([key isEqualToString:@"Key ID"]) {
|
2012-06-06 22:12:38 +00:00
|
|
|
if (![(keyIDHex = value) isEqualToString:[keyIDForPassword(password, userName) encodeHex]])
|
2012-05-09 08:11:34 +00:00
|
|
|
return MPImportResultInvalidPassword;
|
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!headerEnded)
|
|
|
|
continue;
|
2012-06-04 09:27:02 +00:00
|
|
|
if (!keyIDHex || ![userName length])
|
2012-05-09 08:11:34 +00:00
|
|
|
return MPImportResultMalformedInput;
|
|
|
|
if (![importedSiteLine length])
|
|
|
|
continue;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Site
|
2012-05-11 07:05:11 +00:00
|
|
|
if ([sitePattern numberOfMatchesInString:importedSiteLine options:0 range:NSMakeRange(0, [importedSiteLine length])] != 1) {
|
2012-05-09 08:11:34 +00:00
|
|
|
err(@"Invalid site format in line: %@", importedSiteLine);
|
|
|
|
return MPImportResultMalformedInput;
|
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
NSTextCheckingResult *siteElements = [[sitePattern matchesInString:importedSiteLine options:0
|
|
|
|
range:NSMakeRange(0, [importedSiteLine length])] lastObject];
|
|
|
|
NSString *lastUsed = [importedSiteLine substringWithRange:[siteElements rangeAtIndex:1]];
|
|
|
|
NSString *uses = [importedSiteLine substringWithRange:[siteElements rangeAtIndex:2]];
|
|
|
|
NSString *type = [importedSiteLine substringWithRange:[siteElements rangeAtIndex:3]];
|
|
|
|
NSString *name = [importedSiteLine substringWithRange:[siteElements rangeAtIndex:4]];
|
|
|
|
NSString *exportContent = [importedSiteLine substringWithRange:[siteElements rangeAtIndex:5]];
|
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Find existing site.
|
2012-06-04 09:27:02 +00:00
|
|
|
if (user) {
|
|
|
|
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"name == %@ AND user == %@", name, user];
|
|
|
|
NSArray *existingSites = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
|
|
|
|
if (error)
|
2012-06-08 21:46:13 +00:00
|
|
|
err(@"Couldn't search existing sites: %@", error);
|
2012-06-04 09:27:02 +00:00
|
|
|
if (!existingSites)
|
|
|
|
return MPImportResultInternalError;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-06-04 09:27:02 +00:00
|
|
|
[elementsToDelete addObjectsFromArray:existingSites];
|
|
|
|
[importedSiteElements addObject:[NSArray arrayWithObjects:lastUsed, uses, type, name, exportContent, nil]];
|
|
|
|
}
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Ask for confirmation to import these sites.
|
|
|
|
if (!confirmation([importedSiteElements count], [elementsToDelete count]))
|
|
|
|
return MPImportResultCancelled;
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Delete existing sites.
|
|
|
|
[elementsToDelete enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
|
2012-05-12 16:31:05 +00:00
|
|
|
inf(@"Deleting site: %@, it will be replaced by an imported site.", [obj name]);
|
2012-05-09 08:11:34 +00:00
|
|
|
[self.managedObjectContext deleteObject:obj];
|
|
|
|
}];
|
2012-05-11 07:05:11 +00:00
|
|
|
[self saveContext];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Import new sites.
|
2012-06-04 09:27:02 +00:00
|
|
|
if (!user) {
|
2012-06-08 21:46:13 +00:00
|
|
|
user = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([MPUserEntity class])
|
|
|
|
inManagedObjectContext:self.managedObjectContext];
|
|
|
|
user.name = userName;
|
2012-06-04 09:27:02 +00:00
|
|
|
user.keyID = [keyIDHex decodeHex];
|
|
|
|
}
|
2012-05-09 08:11:34 +00:00
|
|
|
for (NSArray *siteElements in importedSiteElements) {
|
2012-06-08 21:46:13 +00:00
|
|
|
NSDate *lastUsed = [rfc3339DateFormatter dateFromString:[siteElements objectAtIndex:0]];
|
|
|
|
NSUInteger uses = (unsigned)[[siteElements objectAtIndex:1] integerValue];
|
|
|
|
MPElementType type = (MPElementType)[[siteElements objectAtIndex:2] integerValue];
|
2012-05-09 08:11:34 +00:00
|
|
|
NSString *name = [siteElements objectAtIndex:3];
|
|
|
|
NSString *exportContent = [siteElements objectAtIndex:4];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Create new site.
|
|
|
|
MPElementEntity *element = [NSEntityDescription insertNewObjectForEntityForName:ClassNameFromMPElementType(type)
|
|
|
|
inManagedObjectContext:self.managedObjectContext];
|
2012-06-08 21:46:13 +00:00
|
|
|
element.name = name;
|
|
|
|
element.user = user;
|
|
|
|
element.type = type;
|
|
|
|
element.uses = uses;
|
2012-06-04 09:27:02 +00:00
|
|
|
element.lastUsed = lastUsed;
|
2012-05-09 08:11:34 +00:00
|
|
|
if ([exportContent length])
|
|
|
|
[element importContent:exportContent];
|
|
|
|
}
|
2012-05-11 07:05:11 +00:00
|
|
|
[self saveContext];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-11 20:45:05 +00:00
|
|
|
#ifdef TESTFLIGHT_SDK_VERSION
|
2012-06-11 21:39:50 +00:00
|
|
|
[TestFlight passCheckpoint:MPCheckpointSitesImported];
|
2012-05-11 15:04:51 +00:00
|
|
|
#endif
|
2012-06-11 21:39:50 +00:00
|
|
|
[[LocalyticsSession sharedLocalyticsSession] tagEvent:MPCheckpointSitesImported
|
|
|
|
attributes:nil];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
return MPImportResultSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)exportSitesShowingPasswords:(BOOL)showPasswords {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
[self loadRFC3339DateFormatter];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Header.
|
|
|
|
NSMutableString *export = [NSMutableString new];
|
|
|
|
[export appendFormat:@"# Master Password site export\n"];
|
|
|
|
if (showPasswords)
|
|
|
|
[export appendFormat:@"# Export of site names and passwords in clear-text.\n"];
|
|
|
|
else
|
|
|
|
[export appendFormat:@"# Export of site names and stored passwords (unless device-private) encrypted with the master key.\n"];
|
|
|
|
[export appendFormat:@"# \n"];
|
|
|
|
[export appendFormat:@"##\n"];
|
|
|
|
[export appendFormat:@"# Version: %@\n", [PearlInfoPlist get].CFBundleVersion];
|
2012-06-04 09:27:02 +00:00
|
|
|
[export appendFormat:@"# User Name: %@\n", self.activeUser.name];
|
|
|
|
[export appendFormat:@"# Key ID: %@\n", [self.activeUser.keyID encodeHex]];
|
2012-05-09 08:11:34 +00:00
|
|
|
[export appendFormat:@"# Date: %@\n", [rfc3339DateFormatter stringFromDate:[NSDate date]]];
|
|
|
|
if (showPasswords)
|
|
|
|
[export appendFormat:@"# Passwords: VISIBLE\n"];
|
|
|
|
else
|
|
|
|
[export appendFormat:@"# Passwords: PROTECTED\n"];
|
|
|
|
[export appendFormat:@"##\n"];
|
|
|
|
[export appendFormat:@"#\n"];
|
|
|
|
[export appendFormat:@"# Last Times Password Site\tSite\n"];
|
|
|
|
[export appendFormat:@"# used used type name\tpassword\n"];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Sites.
|
2012-06-04 09:27:02 +00:00
|
|
|
for (MPElementEntity *element in self.activeUser.elements) {
|
2012-06-08 21:46:13 +00:00
|
|
|
NSDate *lastUsed = element.lastUsed;
|
|
|
|
NSUInteger uses = element.uses;
|
|
|
|
MPElementType type = element.type;
|
|
|
|
NSString *name = element.name;
|
|
|
|
NSString *content = nil;
|
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
// Determine the content to export.
|
|
|
|
if (!(type & MPElementFeatureDevicePrivate)) {
|
|
|
|
if (showPasswords)
|
|
|
|
content = element.content;
|
2012-06-08 21:46:13 +00:00
|
|
|
else
|
|
|
|
if (type & MPElementFeatureExportContent)
|
|
|
|
content = element.exportContent;
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
[export appendFormat:@"%@ %8d %8d %20s\t%@\n",
|
2012-06-08 21:46:13 +00:00
|
|
|
[rfc3339DateFormatter stringFromDate:lastUsed], uses, type, [name cStringUsingEncoding:NSUTF8StringEncoding], content
|
|
|
|
? content: @""];
|
2012-05-09 08:11:34 +00:00
|
|
|
}
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-11 20:45:05 +00:00
|
|
|
#ifdef TESTFLIGHT_SDK_VERSION
|
2012-06-11 21:39:50 +00:00
|
|
|
[TestFlight passCheckpoint:MPCheckpointSitesExported];
|
2012-05-11 15:04:51 +00:00
|
|
|
#endif
|
2012-06-11 21:39:50 +00:00
|
|
|
[[LocalyticsSession sharedLocalyticsSession] tagEvent:MPCheckpointSitesExported
|
|
|
|
attributes:nil];
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2012-05-09 08:11:34 +00:00
|
|
|
return export;
|
|
|
|
}
|
2012-05-07 20:18:01 +00:00
|
|
|
|
|
|
|
@end
|