2017-04-05 20:56:22 +00:00
|
|
|
//==============================================================================
|
|
|
|
// This file is part of Master Password.
|
|
|
|
// Copyright (c) 2011-2017, Maarten Billemont.
|
2012-05-07 20:18:01 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00: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 20:18:01 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00: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 20:18:01 +00:00
|
|
|
//
|
2017-04-05 20:56:22 +00: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 20:18:01 +00:00
|
|
|
|
|
|
|
#import "MPAppDelegate_Shared.h"
|
2012-08-18 13:37:24 +00:00
|
|
|
#import "MPAppDelegate_Store.h"
|
2013-09-14 18:04:06 +00:00
|
|
|
#import "MPAppDelegate_Key.h"
|
2014-10-28 04:53:16 +00:00
|
|
|
#import "NSManagedObjectModel+KCOrderedAccessorFix.h"
|
2012-05-07 20:18:01 +00:00
|
|
|
|
2020-04-04 20:30:14 +00:00
|
|
|
#import <Countly/Countly.h>
|
|
|
|
|
2017-04-01 04:30:25 +00:00
|
|
|
@interface MPAppDelegate_Shared()
|
2014-09-21 14:29:18 +00:00
|
|
|
|
2017-04-30 03:03:50 +00:00
|
|
|
@property(strong, atomic) MPKey *key;
|
|
|
|
@property(strong, atomic) NSManagedObjectID *activeUserOID;
|
|
|
|
@property(strong, atomic) NSPersistentStoreCoordinator *storeCoordinator;
|
2014-09-21 14:29:18 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2020-04-06 23:27:17 +00:00
|
|
|
MPLogSink mpw_log_sink_pearl;
|
|
|
|
void mpw_log_sink_pearl(const MPLogEvent *record) {
|
|
|
|
|
|
|
|
PearlLogLevel level = PearlLogLevelInfo;
|
|
|
|
switch (record->level) {
|
|
|
|
case LogLevelTrace:
|
|
|
|
level = PearlLogLevelTrace;
|
|
|
|
break;
|
|
|
|
case LogLevelDebug:
|
|
|
|
level = PearlLogLevelDebug;
|
|
|
|
break;
|
|
|
|
case LogLevelInfo:
|
|
|
|
level = PearlLogLevelInfo;
|
|
|
|
break;
|
|
|
|
case LogLevelWarning:
|
|
|
|
level = PearlLogLevelWarn;
|
|
|
|
break;
|
|
|
|
case LogLevelError:
|
|
|
|
level = PearlLogLevelError;
|
|
|
|
break;
|
|
|
|
case LogLevelFatal:
|
|
|
|
level = PearlLogLevelFatal;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
[[PearlLogger get] inFile:[@(record->file) lastPathComponent] atLine:record->line fromFunction:@(record->function)
|
|
|
|
withLevel:level text:@(record->message)];
|
|
|
|
}
|
|
|
|
|
2014-03-20 11:15:37 +00:00
|
|
|
@implementation MPAppDelegate_Shared
|
2012-05-11 20:45:05 +00:00
|
|
|
|
2020-03-13 16:15:24 +00:00
|
|
|
static MPAppDelegate_Shared *instance;
|
|
|
|
|
2012-05-11 20:45:05 +00:00
|
|
|
+ (MPAppDelegate_Shared *)get {
|
2012-06-08 21:46:13 +00:00
|
|
|
|
2020-03-13 16:15:24 +00:00
|
|
|
return instance;
|
2012-05-07 20:18:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 04:53:16 +00:00
|
|
|
- (instancetype)init {
|
|
|
|
|
2020-03-13 16:15:24 +00:00
|
|
|
if (!(self = instance = [super init]))
|
2014-10-28 04:53:16 +00:00
|
|
|
return nil;
|
|
|
|
|
2020-04-06 23:27:17 +00:00
|
|
|
[PearlLogger get].historyLevel = PearlLogLevelInfo;
|
|
|
|
#ifdef DEBUG
|
|
|
|
[PearlLogger get].printLevel = PearlLogLevelDebug;
|
|
|
|
#else
|
|
|
|
[PearlLogger get].printLevel = PearlLogLevelInfo;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
mpw_verbosity = LogLevelTrace;
|
|
|
|
mpw_log_sink_register( &mpw_log_sink_pearl );
|
|
|
|
|
2014-10-28 04:53:16 +00:00
|
|
|
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
|
|
|
|
[model kc_generateOrderedSetAccessors];
|
|
|
|
self.storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
- (MPUserEntity *)activeUserForMainThread {
|
2012-08-18 13:37:24 +00:00
|
|
|
|
2013-06-16 16:39:52 +00:00
|
|
|
return [self activeUserInContext:[MPAppDelegate_Shared managedObjectContextForMainThreadIfReady]];
|
2013-01-31 05:42:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
- (MPUserEntity *)activeUserInContext:(NSManagedObjectContext *)context {
|
2013-01-31 05:42:32 +00:00
|
|
|
|
2014-04-07 03:34:18 +00:00
|
|
|
NSManagedObjectID *activeUserOID = self.activeUserOID;
|
2014-05-13 11:27:11 +00:00
|
|
|
if (!activeUserOID || !context)
|
2012-08-19 07:34:49 +00:00
|
|
|
return nil;
|
|
|
|
|
2014-05-13 11:27:11 +00:00
|
|
|
MPUserEntity *activeUser = [MPUserEntity existingObjectWithID:activeUserOID inContext:context];
|
|
|
|
if (!activeUser)
|
2020-04-04 20:30:14 +00:00
|
|
|
[self signOut];
|
2013-01-31 05:42:32 +00:00
|
|
|
|
|
|
|
return activeUser;
|
2012-08-18 13:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setActiveUser:(MPUserEntity *)activeUser {
|
|
|
|
|
2020-04-04 20:30:14 +00:00
|
|
|
NSManagedObjectID *activeUserOID = activeUser.permanentObjectID;
|
2020-04-11 16:10:42 +00:00
|
|
|
if (self.activeUserOID == activeUserOID || [self.activeUserOID isEqual:activeUserOID])
|
2020-04-04 20:30:14 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (self.key)
|
|
|
|
self.key = nil;
|
|
|
|
|
|
|
|
if ([[MPConfig get].sendInfo boolValue])
|
|
|
|
[Countly.sharedInstance userLoggedOut];
|
|
|
|
|
|
|
|
self.activeUserOID = activeUserOID;
|
2020-04-11 16:10:42 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:MPSignedOutNotification object:self];
|
2012-08-18 13:37:24 +00:00
|
|
|
}
|
|
|
|
|
2014-09-22 03:11:05 +00:00
|
|
|
- (void)handleCoordinatorError:(NSError *)error {
|
|
|
|
}
|
|
|
|
|
2012-05-07 20:18:01 +00:00
|
|
|
@end
|