2
0
MasterPassword/platform-darwin/Source/MPAppDelegate_Shared.m

134 lines
3.9 KiB
Mathematica
Raw Normal View History

2017-04-05 20:56:22 +00:00
//==============================================================================
// This file is part of Master Password.
// Copyright (c) 2011-2017, Maarten Billemont.
//
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.
//
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.
//
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/>.
//==============================================================================
#import "MPAppDelegate_Shared.h"
#import "MPAppDelegate_Store.h"
#import "MPAppDelegate_Key.h"
#import "NSManagedObjectModel+KCOrderedAccessorFix.h"
#import <Countly/Countly.h>
2017-04-01 04:30:25 +00:00
@interface MPAppDelegate_Shared()
@property(strong, atomic) MPKey *key;
@property(strong, atomic) NSManagedObjectID *activeUserOID;
@property(strong, atomic) NSPersistentStoreCoordinator *storeCoordinator;
@end
MPLogSink mpw_log_sink_pearl;
2020-04-15 23:08:11 +00:00
bool 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)];
2020-04-15 23:08:11 +00:00
return YES;
}
@implementation MPAppDelegate_Shared
static MPAppDelegate_Shared *instance;
+ (MPAppDelegate_Shared *)get {
2012-06-08 21:46:13 +00:00
return instance;
}
- (instancetype)init {
if (!(self = instance = [super init]))
return nil;
[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 );
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
[model kc_generateOrderedSetAccessors];
self.storeCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
return self;
}
- (MPUserEntity *)activeUserForMainThread {
return [self activeUserInContext:[MPAppDelegate_Shared managedObjectContextForMainThreadIfReady]];
}
- (MPUserEntity *)activeUserInContext:(NSManagedObjectContext *)context {
NSManagedObjectID *activeUserOID = self.activeUserOID;
if (!activeUserOID || !context)
return nil;
MPUserEntity *activeUser = [MPUserEntity existingObjectWithID:activeUserOID inContext:context];
if (!activeUser)
[self signOut];
return activeUser;
}
- (void)setActiveUser:(MPUserEntity *)activeUser {
NSManagedObjectID *activeUserOID = activeUser.permanentObjectID;
if (self.activeUserOID == activeUserOID || [self.activeUserOID isEqual:activeUserOID])
return;
if (self.key)
self.key = nil;
if ([[MPConfig get].sendInfo boolValue])
[Countly.sharedInstance userLoggedOut];
self.activeUserOID = activeUserOID;
[[NSNotificationCenter defaultCenter] postNotificationName:MPSignedOutNotification object:self];
}
2014-09-22 03:11:05 +00:00
- (void)handleCoordinatorError:(NSError *)error {
}
@end