2014-02-12 05:13:12 +00:00
|
|
|
/**
|
2014-05-19 18:37:05 +00:00
|
|
|
* Copyright Maarten Billemont (http://www.lhunath.com, lhunath@lyndir.com)
|
|
|
|
*
|
|
|
|
* See the enclosed file LICENSE for license information (LGPLv3). If you did
|
|
|
|
* not receive this file, see http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*
|
|
|
|
* @author Maarten Billemont <lhunath@lyndir.com>
|
|
|
|
* @license http://www.gnu.org/licenses/lgpl-3.0.txt
|
|
|
|
*/
|
2014-02-12 05:13:12 +00:00
|
|
|
|
|
|
|
//
|
2014-09-21 15:47:53 +00:00
|
|
|
// MPSiteModel.h
|
|
|
|
// MPSiteModel
|
2014-02-12 05:13:12 +00:00
|
|
|
//
|
|
|
|
// Created by lhunath on 2/11/2014.
|
|
|
|
// Copyright, lhunath (Maarten Billemont) 2014. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
#import "MPSiteModel.h"
|
2014-09-21 14:39:09 +00:00
|
|
|
#import "MPSiteEntity.h"
|
2014-02-12 05:13:12 +00:00
|
|
|
#import "MPEntities.h"
|
|
|
|
#import "MPAppDelegate_Shared.h"
|
|
|
|
#import "MPAppDelegate_Store.h"
|
|
|
|
#import "MPMacAppDelegate.h"
|
|
|
|
|
2014-09-21 15:47:53 +00:00
|
|
|
@implementation MPSiteModel {
|
2014-05-19 18:37:05 +00:00
|
|
|
NSManagedObjectID *_entityOID;
|
|
|
|
BOOL _initialized;
|
2014-02-12 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
- (id)initWithEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups {
|
2014-02-12 05:13:12 +00:00
|
|
|
|
|
|
|
if (!(self = [super init]))
|
|
|
|
return nil;
|
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
[self setEntity:entity fuzzyGroups:fuzzyGroups];
|
2014-05-19 18:37:05 +00:00
|
|
|
_initialized = YES;
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
- (void)setEntity:(MPSiteEntity *)entity fuzzyGroups:(NSArray *)fuzzyGroups {
|
2014-05-19 18:37:05 +00:00
|
|
|
|
|
|
|
if ([_entityOID isEqual:entity.objectID])
|
|
|
|
return;
|
2014-02-21 05:19:24 +00:00
|
|
|
_entityOID = entity.objectID;
|
|
|
|
|
2014-10-30 01:24:35 +00:00
|
|
|
NSString *siteName = entity.name;
|
|
|
|
NSMutableAttributedString *attributedSiteName = [[NSMutableAttributedString alloc] initWithString:siteName];
|
|
|
|
for (NSUInteger f = 0, s = (NSUInteger)-1; f < [fuzzyGroups count]; ++f) {
|
|
|
|
s = [siteName rangeOfString:fuzzyGroups[f] options:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch
|
|
|
|
range:NSMakeRange( s + 1, [siteName length] - (s + 1) )].location;
|
|
|
|
if (s == NSNotFound)
|
|
|
|
break;
|
|
|
|
|
|
|
|
[attributedSiteName addAttribute:NSBackgroundColorAttributeName value:[NSColor alternateSelectedControlColor]
|
|
|
|
range:NSMakeRange( s, [fuzzyGroups[f] length] )];
|
|
|
|
}
|
|
|
|
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
|
|
paragraphStyle.alignment = NSCenterTextAlignment;
|
|
|
|
[attributedSiteName addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange( 0, [siteName length] )];
|
|
|
|
|
|
|
|
self.displayedName = attributedSiteName;
|
|
|
|
self.name = siteName;
|
2014-05-19 18:37:05 +00:00
|
|
|
self.algorithm = entity.algorithm;
|
|
|
|
self.lastUsed = entity.lastUsed;
|
|
|
|
self.type = entity.type;
|
|
|
|
self.typeName = entity.typeName;
|
|
|
|
self.uses = entity.uses_;
|
2014-09-21 15:47:53 +00:00
|
|
|
self.counter = [entity isKindOfClass:[MPGeneratedSiteEntity class]]? [(MPGeneratedSiteEntity *)entity counter]: 0;
|
2014-05-19 18:37:05 +00:00
|
|
|
|
2014-02-21 05:19:24 +00:00
|
|
|
// Find all password types and the index of the current type amongst them.
|
2014-06-27 03:13:21 +00:00
|
|
|
[self updateContent:entity];
|
2014-02-12 05:13:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (MPSiteEntity *)entityInContext:(NSManagedObjectContext *)moc {
|
2014-02-12 05:13:12 +00:00
|
|
|
|
|
|
|
if (!_entityOID)
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
NSError *error;
|
2014-09-21 14:39:09 +00:00
|
|
|
MPSiteEntity *entity = (MPSiteEntity *)[moc existingObjectWithID:_entityOID error:&error];
|
2014-02-12 05:13:12 +00:00
|
|
|
if (!entity)
|
2014-09-22 03:28:50 +00:00
|
|
|
err( @"Couldn't retrieve active site: %@", [error fullDescription] );
|
2014-02-12 05:13:12 +00:00
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
2014-02-21 05:19:24 +00:00
|
|
|
- (void)setCounter:(NSUInteger)counter {
|
|
|
|
|
|
|
|
if (counter == _counter)
|
|
|
|
return;
|
|
|
|
_counter = counter;
|
|
|
|
|
2014-05-19 18:37:05 +00:00
|
|
|
if (!_initialized)
|
|
|
|
// This wasn't a change to the entity.
|
|
|
|
return;
|
|
|
|
|
2014-02-21 05:19:24 +00:00
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2014-09-21 14:39:09 +00:00
|
|
|
MPSiteEntity *entity = [self entityInContext:context];
|
2014-09-21 15:47:53 +00:00
|
|
|
if ([entity isKindOfClass:[MPGeneratedSiteEntity class]]) {
|
|
|
|
((MPGeneratedSiteEntity *)entity).counter = counter;
|
2014-02-21 05:19:24 +00:00
|
|
|
[context saveToStore];
|
|
|
|
|
2014-06-27 03:13:21 +00:00
|
|
|
[self updateContent:entity];
|
2014-02-21 05:19:24 +00:00
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-06-26 05:19:42 +00:00
|
|
|
- (BOOL)generated {
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
return self.type & MPSiteTypeClassGenerated;
|
2014-06-26 05:19:42 +00:00
|
|
|
}
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2014-06-26 05:19:42 +00:00
|
|
|
- (BOOL)stored {
|
2014-02-21 05:19:24 +00:00
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
return self.type & MPSiteTypeClassStored;
|
2014-02-21 05:19:24 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 03:13:21 +00:00
|
|
|
- (void)updateContent {
|
|
|
|
|
|
|
|
[MPMacAppDelegate managedObjectContextPerformBlock:^(NSManagedObjectContext *context) {
|
2014-09-21 14:39:09 +00:00
|
|
|
[self updateContent:[MPSiteEntity existingObjectWithID:_entityOID inContext:context]];
|
2014-06-27 03:13:21 +00:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-09-21 14:39:09 +00:00
|
|
|
- (void)updateContent:(MPSiteEntity *)entity {
|
2014-06-27 03:13:21 +00:00
|
|
|
|
2014-06-29 03:45:06 +00:00
|
|
|
static NSRegularExpression *re_anyChar;
|
|
|
|
static dispatch_once_t once = 0;
|
|
|
|
dispatch_once( &once, ^{
|
|
|
|
re_anyChar = [NSRegularExpression regularExpressionWithPattern:@"." options:0 error:nil];
|
|
|
|
} );
|
|
|
|
|
2014-09-15 05:00:23 +00:00
|
|
|
[entity resolvePasswordUsingKey:[MPAppDelegate_Shared get].key result:^(NSString *result) {
|
2014-06-29 03:45:06 +00:00
|
|
|
NSString *displayResult = result;
|
2014-06-27 03:13:21 +00:00
|
|
|
if ([[MPConfig get].hidePasswords boolValue] && !([NSEvent modifierFlags] & NSAlternateKeyMask))
|
2014-06-29 03:45:06 +00:00
|
|
|
displayResult = [displayResult stringByReplacingMatchesOfExpression:re_anyChar withTemplate:@"●"];
|
2014-06-27 03:13:21 +00:00
|
|
|
|
2014-06-29 03:45:06 +00:00
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.content = result;
|
2014-10-30 01:24:35 +00:00
|
|
|
self.displayedContent = displayResult;
|
2014-06-29 03:45:06 +00:00
|
|
|
} );
|
2014-06-27 03:13:21 +00:00
|
|
|
}];
|
2014-10-28 04:53:16 +00:00
|
|
|
[entity resolveLoginUsingKey:[MPAppDelegate_Shared get].key result:^(NSString *result) {
|
|
|
|
PearlMainQueue( ^{
|
|
|
|
self.loginName = result;
|
|
|
|
} );
|
|
|
|
}];
|
2014-06-27 03:13:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 05:13:12 +00:00
|
|
|
@end
|