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