2
0
MasterPassword/MasterPassword/ObjC/MPEntities.m

211 lines
4.4 KiB
Mathematica
Raw Normal View History

//
// MPElementEntities.m
// MasterPassword-iOS
//
// Created by Maarten Billemont on 31/05/12.
// Copyright (c) 2012 Lyndir. All rights reserved.
//
#import "MPEntities.h"
#import "MPAppDelegate_Shared.h"
#import "MPAppDelegate_Store.h"
2013-04-20 18:11:19 +00:00
@implementation NSManagedObjectContext(MP)
- (BOOL)saveToStore {
__block BOOL success = YES;
if ([self hasChanges]) {
[self performBlockAndWait:^{
@try {
NSError *error = nil;
if (!(success = [self save:&error]))
err( @"While saving: %@", [error fullDescription] );
}
@catch (NSException *exception) {
success = NO;
err( @"While saving: %@", exception );
}
}];
}
return success && (!self.parentContext || [self.parentContext saveToStore]);
}
@end
2013-04-20 18:11:19 +00:00
@implementation MPElementEntity(MP)
- (MPElementType)type {
2012-06-08 21:46:13 +00:00
return (MPElementType)[self.type_ unsignedIntegerValue];
}
- (void)setType:(MPElementType)aType {
2012-06-08 21:46:13 +00:00
self.type_ = @(aType);
}
- (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];
}
- (NSUInteger)uses {
return [self.uses_ unsignedIntegerValue];
}
- (void)setUses:(NSUInteger)anUses {
self.uses_ = @(anUses);
}
- (NSUInteger)version {
return [self.version_ unsignedIntegerValue];
}
- (void)setVersion:(NSUInteger)version {
self.version_ = @(version);
}
- (BOOL)requiresExplicitMigration {
return [self.requiresExplicitMigration_ boolValue];
}
- (void)setRequiresExplicitMigration:(BOOL)requiresExplicitMigration {
self.requiresExplicitMigration_ = @(requiresExplicitMigration);
}
- (id<MPAlgorithm>)algorithm {
2013-04-20 18:11:19 +00:00
return MPAlgorithmForVersion( self.version );
}
- (NSUInteger)use {
2012-06-08 21:46:13 +00:00
self.lastUsed = [NSDate date];
return ++self.uses;
}
- (NSString *)description {
2012-06-08 21:46:13 +00:00
return strf( @"%@:%@", [self class], [self name] );
}
- (NSString *)debugDescription {
2012-06-08 21:46:13 +00:00
return strf( @"{%@: name=%@, user=%@, type=%lu, uses=%ld, lastUsed=%@, version=%ld, loginName=%@, requiresExplicitMigration=%d}",
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 );
}
- (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])
inf( @"%@ migration to version: %ld succeeded for element: %@",
explicit? @"Explicit": @"Automatic", (long)self.version + 1, self );
else {
wrn( @"%@ migration to version: %ld failed for element: %@",
explicit? @"Explicit": @"Automatic", (long)self.version + 1, self );
return NO;
}
return YES;
}
- (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];
}
@end
2013-04-20 18:11:19 +00:00
@implementation MPElementGeneratedEntity(MP)
- (NSUInteger)counter {
return [self.counter_ unsignedIntegerValue];
}
- (void)setCounter:(NSUInteger)aCounter {
self.counter_ = @(aCounter);
}
@end
2013-04-20 18:11:19 +00:00
@implementation MPElementStoredEntity(MP)
@end
2013-04-20 18:11:19 +00:00
@implementation MPUserEntity(MP)
- (NSUInteger)avatar {
return [self.avatar_ unsignedIntegerValue];
}
- (void)setAvatar:(NSUInteger)anAvatar {
self.avatar_ = @(anAvatar);
}
- (BOOL)saveKey {
return [self.saveKey_ boolValue];
}
- (void)setSaveKey:(BOOL)aSaveKey {
self.saveKey_ = @(aSaveKey);
}
- (MPElementType)defaultType {
return (MPElementType)[self.defaultType_ unsignedIntegerValue]?: MPElementTypeGeneratedLong;
}
- (void)setDefaultType:(MPElementType)aDefaultType {
self.defaultType_ = @(aDefaultType);
}
- (NSString *)userID {
return [MPUserEntity idFor:self.name];
}
+ (NSString *)idFor:(NSString *)userName {
return [[userName hashWith:PearlHashSHA1] encodeHex];
}
@end