2017-04-05 16:56:22 -04:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2012-05-07 22:18:01 +02:00
|
|
|
//
|
2017-04-05 16:56:22 -04:00
|
|
|
// Master Password is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2012-05-07 22:18:01 +02:00
|
|
|
//
|
2017-04-05 16:56:22 -04:00
|
|
|
// Master Password is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2012-05-07 22:18:01 +02:00
|
|
|
//
|
2017-04-05 16:56:22 -04:00
|
|
|
// You can find a copy of the GNU General Public License in the
|
|
|
|
// LICENSE file. Alternatively, see <http://www.gnu.org/licenses/>.
|
|
|
|
//==============================================================================
|
2012-05-07 22:18:01 +02:00
|
|
|
|
|
|
|
#import "MPAppDelegate_Shared.h"
|
2012-08-18 15:37:24 +02:00
|
|
|
#import "MPAppDelegate_Store.h"
|
2013-09-14 14:04:06 -04:00
|
|
|
#import "MPAppDelegate_Key.h"
|
2014-10-28 00:53:16 -04:00
|
|
|
#import "NSManagedObjectModel+KCOrderedAccessorFix.h"
|
2012-05-07 22:18:01 +02:00
|
|
|
|
2017-04-01 00:30:25 -04:00
|
|
|
@interface MPAppDelegate_Shared()
|
2014-09-21 10:29:18 -04:00
|
|
|
|
|
|
|
@property(strong, nonatomic) MPKey *key;
|
|
|
|
@property(strong, nonatomic) NSManagedObjectID *activeUserOID;
|
2014-10-28 00:53:16 -04:00
|
|
|
@property(strong, nonatomic) NSPersistentStoreCoordinator *storeCoordinator;
|
2014-09-21 10:29:18 -04:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2014-03-20 07:15:37 -04:00
|
|
|
@implementation MPAppDelegate_Shared
|
2012-05-11 22:45:05 +02:00
|
|
|
|
|
|
|
+ (MPAppDelegate_Shared *)get {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-05-11 17:04:51 +02:00
|
|
|
#if TARGET_OS_IPHONE
|
2013-09-18 00:21:52 -04:00
|
|
|
return (MPAppDelegate_Shared *)UIApp.delegate;
|
2012-05-07 22:18:01 +02:00
|
|
|
#elif defined (__MAC_OS_X_VERSION_MIN_REQUIRED)
|
2012-05-11 22:45:05 +02:00
|
|
|
return (MPAppDelegate_Shared *)[NSApplication sharedApplication].delegate;
|
2012-05-07 22:18:01 +02:00
|
|
|
#else
|
|
|
|
#error Unsupported OS.
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-10-28 00:53:16 -04:00
|
|
|
- (instancetype)init {
|
|
|
|
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
|
|
|
|
[model kc_generateOrderedSetAccessors];
|
|
|
|
self.storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-06-16 12:39:52 -04:00
|
|
|
- (MPUserEntity *)activeUserForMainThread {
|
2012-08-18 15:37:24 +02:00
|
|
|
|
2013-06-16 12:39:52 -04:00
|
|
|
return [self activeUserInContext:[MPAppDelegate_Shared managedObjectContextForMainThreadIfReady]];
|
2013-01-31 00:42:32 -05:00
|
|
|
}
|
|
|
|
|
2014-05-13 07:27:11 -04:00
|
|
|
- (MPUserEntity *)activeUserInContext:(NSManagedObjectContext *)context {
|
2013-01-31 00:42:32 -05:00
|
|
|
|
2014-04-06 23:34:18 -04:00
|
|
|
NSManagedObjectID *activeUserOID = self.activeUserOID;
|
2014-05-13 07:27:11 -04:00
|
|
|
if (!activeUserOID || !context)
|
2012-08-19 09:34:49 +02:00
|
|
|
return nil;
|
|
|
|
|
2014-05-13 07:27:11 -04:00
|
|
|
MPUserEntity *activeUser = [MPUserEntity existingObjectWithID:activeUserOID inContext:context];
|
|
|
|
if (!activeUser)
|
2013-09-14 14:04:06 -04:00
|
|
|
[self signOutAnimated:YES];
|
2013-01-31 00:42:32 -05:00
|
|
|
|
|
|
|
return activeUser;
|
2012-08-18 15:37:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setActiveUser:(MPUserEntity *)activeUser {
|
|
|
|
|
2013-04-30 00:32:30 -04:00
|
|
|
NSError *error;
|
|
|
|
if (activeUser.objectID.isTemporaryID && ![activeUser.managedObjectContext obtainPermanentIDsForObjects:@[ activeUser ] error:&error])
|
2017-04-01 00:30:25 -04:00
|
|
|
err( @"Failed to obtain a permanent object ID after setting active user: %@", [error fullDescription] );
|
2013-04-30 00:32:30 -04:00
|
|
|
|
2014-03-20 07:15:37 -04:00
|
|
|
self.activeUserOID = activeUser.objectID;
|
2012-08-18 15:37:24 +02:00
|
|
|
}
|
|
|
|
|
2014-09-21 23:11:05 -04:00
|
|
|
- (void)handleCoordinatorError:(NSError *)error {
|
|
|
|
}
|
|
|
|
|
2012-05-07 22:18:01 +02:00
|
|
|
@end
|