2012-06-04 11:27:02 +02:00
|
|
|
//
|
|
|
|
// MPElementEntities.m
|
|
|
|
// MasterPassword-iOS
|
|
|
|
//
|
|
|
|
// Created by Maarten Billemont on 31/05/12.
|
|
|
|
// Copyright (c) 2012 Lyndir. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "MPEntities.h"
|
2013-04-27 00:34:28 -04:00
|
|
|
#import "MPAppDelegate_Shared.h"
|
2014-04-06 23:34:18 -04:00
|
|
|
#import "MPAppDelegate_Store.h"
|
2012-06-04 11:27:02 +02:00
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
@implementation NSManagedObjectContext(MP)
|
2013-01-31 00:42:32 -05:00
|
|
|
|
2013-04-13 13:40:17 -04:00
|
|
|
- (BOOL)saveToStore {
|
2013-01-31 00:42:32 -05:00
|
|
|
|
2013-09-13 08:14:58 -04:00
|
|
|
__block BOOL success = YES;
|
2014-04-06 23:34:18 -04:00
|
|
|
if ([self hasChanges]) {
|
2013-09-13 08:14:58 -04:00
|
|
|
[self performBlockAndWait:^{
|
|
|
|
@try {
|
|
|
|
NSError *error = nil;
|
|
|
|
if (!(success = [self save:&error]))
|
|
|
|
err(@"While saving: %@", error);
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
success = NO;
|
|
|
|
err(@"While saving: %@", exception);
|
|
|
|
}
|
|
|
|
}];
|
2014-04-06 23:34:18 -04:00
|
|
|
}
|
2013-09-13 08:14:58 -04:00
|
|
|
|
|
|
|
return success && (!self.parentContext || [self.parentContext saveToStore]);
|
2013-01-31 00:42:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
@implementation MPElementEntity(MP)
|
2012-06-04 11:27:02 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
- (MPElementType)type {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2014-04-26 14:03:44 -04:00
|
|
|
return (MPElementType)[self.type_ unsignedIntegerValue];
|
2012-06-05 00:55:02 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 16:15:49 +02:00
|
|
|
- (void)setType:(MPElementType)aType {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.type_ = @(aType);
|
2012-06-05 00:55:02 +02:00
|
|
|
}
|
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
- (NSString *)typeName {
|
|
|
|
|
|
|
|
return [self.algorithm nameOfType:self.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)typeShortName {
|
|
|
|
|
|
|
|
return [self.algorithm shortNameOfType:self.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)typeClassName {
|
|
|
|
|
|
|
|
return [self.algorithm classNameOfType:self.type];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Class)typeClass {
|
|
|
|
|
|
|
|
return [self.algorithm classOfType:self.type];
|
|
|
|
}
|
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
- (NSUInteger)uses {
|
|
|
|
|
|
|
|
return [self.uses_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUses:(NSUInteger)anUses {
|
2012-06-04 11:27:02 +02:00
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.uses_ = @(anUses);
|
2012-06-05 00:55:02 +02:00
|
|
|
}
|
|
|
|
|
2012-07-12 08:41:18 +02:00
|
|
|
- (NSUInteger)version {
|
|
|
|
|
|
|
|
return [self.version_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setVersion:(NSUInteger)version {
|
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.version_ = @(version);
|
2012-07-12 08:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)requiresExplicitMigration {
|
|
|
|
|
|
|
|
return [self.requiresExplicitMigration_ boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRequiresExplicitMigration:(BOOL)requiresExplicitMigration {
|
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.requiresExplicitMigration_ = @(requiresExplicitMigration);
|
2012-07-12 08:41:18 +02:00
|
|
|
}
|
2012-06-05 00:55:02 +02:00
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
- (id<MPAlgorithm>)algorithm {
|
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
return MPAlgorithmForVersion( self.version );
|
2012-07-17 22:57:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
- (NSUInteger)use {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
self.lastUsed = [NSDate date];
|
|
|
|
return ++self.uses;
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2014-04-26 14:03:44 -04:00
|
|
|
return strf( @"%@:%@", [self class], [self name] );
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)debugDescription {
|
2012-06-08 23:46:13 +02:00
|
|
|
|
2014-04-26 14:03:44 -04:00
|
|
|
return strf( @"{%@: name=%@, user=%@, type=%lu, uses=%ld, lastUsed=%@, version=%ld, loginName=%@, requiresExplicitMigration=%d}",
|
2014-02-22 18:27:14 -05:00
|
|
|
NSStringFromClass( [self class] ), self.name, self.user.name, (long)self.type, (long)self.uses, self.lastUsed, (long)self.version,
|
2013-04-20 14:11:19 -04:00
|
|
|
self.loginName, self.requiresExplicitMigration );
|
2012-07-17 22:57:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)migrateExplicitly:(BOOL)explicit {
|
|
|
|
|
|
|
|
while (self.version < MPAlgorithmDefaultVersion)
|
2013-04-20 14:11:19 -04:00
|
|
|
if ([MPAlgorithmForVersion( self.version + 1 ) migrateElement:self explicit:explicit])
|
2012-10-30 22:54:34 -04:00
|
|
|
inf(@"%@ migration to version: %ld succeeded for element: %@", explicit? @"Explicit": @"Automatic", (long)self.version + 1, self);
|
2012-07-17 22:57:11 +02:00
|
|
|
else {
|
2012-10-30 22:54:34 -04:00
|
|
|
wrn(@"%@ migration to version: %ld failed for element: %@", explicit? @"Explicit": @"Automatic", (long)self.version + 1, self);
|
2012-07-17 22:57:11 +02:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
2012-06-04 11:27:02 +02:00
|
|
|
}
|
|
|
|
|
2014-03-20 07:15:37 -04:00
|
|
|
- (NSString *)resolveContentUsingKey:(MPKey *)key {
|
|
|
|
|
|
|
|
return [self.algorithm resolveContentForElement:self usingKey:key];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resolveContentUsingKey:(MPKey *)key result:(void (^)(NSString *))result {
|
|
|
|
|
|
|
|
[self.algorithm resolveContentForElement:self usingKey:key result:result];
|
|
|
|
}
|
|
|
|
|
2012-06-04 11:27:02 +02:00
|
|
|
@end
|
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
@implementation MPElementGeneratedEntity(MP)
|
2012-06-04 11:27:02 +02:00
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
- (NSUInteger)counter {
|
|
|
|
|
|
|
|
return [self.counter_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCounter:(NSUInteger)aCounter {
|
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.counter_ = @(aCounter);
|
2012-06-05 00:55:02 +02:00
|
|
|
}
|
|
|
|
|
2012-06-04 11:27:02 +02:00
|
|
|
@end
|
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
@implementation MPElementStoredEntity(MP)
|
2012-06-04 11:27:02 +02:00
|
|
|
|
|
|
|
@end
|
2012-06-05 00:55:02 +02:00
|
|
|
|
2013-04-20 14:11:19 -04:00
|
|
|
@implementation MPUserEntity(MP)
|
2012-06-05 00:55:02 +02:00
|
|
|
|
|
|
|
- (NSUInteger)avatar {
|
|
|
|
|
|
|
|
return [self.avatar_ unsignedIntegerValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAvatar:(NSUInteger)anAvatar {
|
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.avatar_ = @(anAvatar);
|
2012-06-05 00:55:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)saveKey {
|
|
|
|
|
|
|
|
return [self.saveKey_ boolValue];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSaveKey:(BOOL)aSaveKey {
|
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.saveKey_ = @(aSaveKey);
|
2012-06-05 00:55:02 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 16:15:49 +02:00
|
|
|
- (MPElementType)defaultType {
|
|
|
|
|
2014-04-06 23:34:18 -04:00
|
|
|
return IfElse((MPElementType)[self.defaultType_ unsignedIntegerValue], MPElementTypeGeneratedLong);
|
2012-06-11 16:15:49 +02:00
|
|
|
}
|
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
- (void)setDefaultType:(MPElementType)aDefaultType {
|
2012-06-14 21:56:54 +02:00
|
|
|
|
2012-08-19 20:02:23 +02:00
|
|
|
self.defaultType_ = @(aDefaultType);
|
2012-06-14 21:56:54 +02:00
|
|
|
}
|
|
|
|
|
2012-07-17 22:57:11 +02:00
|
|
|
- (NSString *)userID {
|
|
|
|
|
|
|
|
return [MPUserEntity idFor:self.name];
|
2012-06-11 16:15:49 +02:00
|
|
|
}
|
|
|
|
|
2012-06-14 21:56:54 +02:00
|
|
|
+ (NSString *)idFor:(NSString *)userName {
|
|
|
|
|
|
|
|
return [[userName hashWith:PearlHashSHA1] encodeHex];
|
|
|
|
}
|
|
|
|
|
2012-06-05 00:55:02 +02:00
|
|
|
@end
|