2012-05-07 22:18:01 +02:00
|
|
|
//
|
|
|
|
// MPAppDelegate.m
|
|
|
|
// MasterPassword
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 24/11/11.
|
|
|
|
// Copyright (c) 2011 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPAppDelegate_Shared.h"
|
2012-08-18 15:37:24 +02:00
|
|
|
#import "MPAppDelegate_Store.h"
|
2012-05-07 22:18:01 +02:00
|
|
|
|
2013-01-31 00:42:32 -05:00
|
|
|
@implementation MPAppDelegate_Shared {
|
|
|
|
NSManagedObjectID *_activeUserOID;
|
|
|
|
}
|
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
|
2012-05-11 22:45:05 +02:00
|
|
|
return (MPAppDelegate_Shared *)[UIApplication sharedApplication].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
|
|
|
|
}
|
|
|
|
|
2013-04-17 22:00:15 -04:00
|
|
|
- (MPUserEntity *)activeUserForThread {
|
2012-08-18 15:37:24 +02:00
|
|
|
|
2013-04-28 00:33:28 -04:00
|
|
|
return [self activeUserInContext:[MPAppDelegate_Shared managedObjectContextForThreadIfReady]];
|
2013-01-31 00:42:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
- (MPUserEntity *)activeUserInContext:(NSManagedObjectContext *)moc {
|
|
|
|
|
|
|
|
if (!_activeUserOID || !moc)
|
2012-08-19 09:34:49 +02:00
|
|
|
return nil;
|
|
|
|
|
2013-01-31 00:42:32 -05:00
|
|
|
NSError *error;
|
|
|
|
MPUserEntity *activeUser = (MPUserEntity *)[moc existingObjectWithID:_activeUserOID error:&error];
|
|
|
|
if (!activeUser)
|
2013-04-20 14:11:19 -04:00
|
|
|
err(@"Failed to retrieve active user: %@", error);
|
2013-01-31 00:42:32 -05:00
|
|
|
|
|
|
|
return activeUser;
|
2012-08-18 15:37:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setActiveUser:(MPUserEntity *)activeUser {
|
|
|
|
|
2013-01-31 00:42:32 -05:00
|
|
|
_activeUserOID = activeUser.objectID;
|
2012-08-18 15:37:24 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 22:18:01 +02:00
|
|
|
@end
|