From 580787353e3f51ab2d3b237426d4ecd5e30a2171 Mon Sep 17 00:00:00 2001 From: michael starke Date: Sun, 5 Apr 2015 21:15:48 +0200 Subject: [PATCH] More use of properties. Fixed wrongfull controller bindings --- MacPass/MPDatabaseSettingsWindowController.m | 19 ++++++++++--------- MacPass/MPDocument.h | 1 - MacPass/MPDocument.m | 17 ++++++++--------- MacPass/MPEntryViewController.m | 14 ++++++-------- MacPass/MPSavePanelAccessoryViewController.h | 4 ++-- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/MacPass/MPDatabaseSettingsWindowController.m b/MacPass/MPDatabaseSettingsWindowController.m index c9d42afc..7871ee64 100644 --- a/MacPass/MPDatabaseSettingsWindowController.m +++ b/MacPass/MPDatabaseSettingsWindowController.m @@ -29,9 +29,10 @@ @interface MPDatabaseSettingsWindowController () { - MPDocument *_document; NSString *_missingFeature; } + +@property (nonatomic, weak) MPDocument *document; @property (nonatomic,assign) BOOL trashEnabled; @end @@ -50,7 +51,7 @@ - (id)initWithDocument:(MPDocument *)document { self = [super initWithWindow:nil]; if(self) { - _document = document; + self.document = document; _missingFeature = NSLocalizedString(@"KDBX_ONLY_FEATURE", "Feature only available in kdbx databases"); } return self; @@ -59,7 +60,7 @@ - (void)windowDidLoad { [super windowDidLoad]; - NSAssert(_document != nil, @"Document needs to be present"); + NSAssert(self.document != nil, @"Document needs to be present"); [self.sectionTabView setDelegate:self]; [self.encryptionRoundsTextField setFormatter:[[MPNumericalInputFormatter alloc] init]]; @@ -69,7 +70,7 @@ - (IBAction)save:(id)sender { /* General */ - KPKMetaData *metaData = _document.tree.metaData; + KPKMetaData *metaData = self.document.tree.metaData; metaData.databaseDescription = [self.databaseDescriptionTextView string]; metaData.databaseName = [self.databaseNameTextField stringValue]; @@ -89,11 +90,11 @@ metaData.recycleBinEnabled = self.trashEnabled; NSMenuItem *trashMenuItem = [self.selectRecycleBinGroupPopUpButton selectedItem]; KPKGroup *trashGroup = [trashMenuItem representedObject]; - _document.trash = trashGroup; + self.document.trash = trashGroup; NSMenuItem *templateMenuItem = [self.templateGroupPopUpButton selectedItem]; KPKGroup *templateGroup = [templateMenuItem representedObject]; - _document.templates = templateGroup; + self.document.templates = templateGroup; BOOL enforceMasterKeyChange = HNHBoolForState([self.enforceKeyChangeCheckButton state]); @@ -130,7 +131,7 @@ metaData.rounds = MAX(0,[self.encryptionRoundsTextField integerValue]); /* Register an action to enable promts when user cloeses without saving */ - [_document updateChangeCount:NSChangeDone]; + [self.document updateChangeCount:NSChangeDone]; [self close:nil]; } @@ -151,10 +152,10 @@ return; } /* Update all stuff that might have changed */ - KPKMetaData *metaData = _document.tree.metaData; + KPKMetaData *metaData = self.document.tree.metaData; [self _setupDatabaseTab:metaData]; [self _setupProtectionTab:metaData]; - [self _setupAdvancedTab:_document.tree]; + [self _setupAdvancedTab:self.document.tree]; self.isDirty = NO; } diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 5203c30b..55dd4261 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -62,7 +62,6 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; @interface MPDocument : NSDocument - @property (nonatomic, readonly, assign) BOOL encrypted; @property (nonatomic, readonly, assign) NSUInteger unlockCount; // Amount of times the Document was unlocked; diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 3fbfaf3a..33cc8f2a 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -67,8 +67,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey @interface MPDocument () { @private BOOL _didLockFile; - NSData *_encryptedData; - MPTreeDelegate *_treeDelgate; } @property (nonatomic, assign) NSUInteger unlockCount; @@ -78,6 +76,9 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey @property (strong, nonatomic) KPKTree *tree; @property (weak, nonatomic) KPKGroup *root; @property (nonatomic, strong) KPKCompositeKey *compositeKey; +@property (nonatomic, strong) NSData *encryptedData; +@property (nonatomic, strong) MPTreeDelegate *treeDelgate; + @property (assign) BOOL readOnly; @property (strong) NSURL *lockFileURL; @@ -87,7 +88,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey @end - @implementation MPDocument + (NSSet *)keyPathsForValuesAffectingRoot { @@ -124,10 +124,9 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey - (id)init { self = [super init]; if(self) { - _encryptedData = nil; _didLockFile = NO; _readOnly = NO; - self.tree = [KPKTree templateTree]; + self.tree = [KPKTree allocTemplateTree]; self.tree.metaData.rounds = [[NSUserDefaults standardUserDefaults] integerForKey:kMPSettingsKeyDefaultPasswordRounds]; } return self; @@ -209,7 +208,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey Delete our old Tree, and just grab the data */ self.tree = nil; - _encryptedData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:outError]; + self.encryptedData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:outError]; return YES; } @@ -271,7 +270,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey NSError *error; self.tree = [[KPKTree alloc] initWithXmlContentsOfURL:url error:&error]; self.compositeKey = nil; - _encryptedData = Nil; + self.encryptedData = nil; } #pragma mark Lock/Unlock/Decrypt @@ -284,14 +283,14 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey [self exitSearch:self]; NSError *error; /* Locking needs to be lossless hence just use the XML format */ - _encryptedData = [self.tree encryptWithPassword:self.compositeKey forVersion:KPKXmlVersion error:&error]; + self.encryptedData = [self.tree encryptWithPassword:self.compositeKey forVersion:KPKXmlVersion error:&error]; self.tree = nil; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidLockDatabaseNotification object:self]; } - (BOOL)unlockWithPassword:(NSString *)password keyFileURL:(NSURL *)keyFileURL error:(NSError *__autoreleasing*)error{ self.compositeKey = [[KPKCompositeKey alloc] initWithPassword:password key:keyFileURL]; - self.tree = [[KPKTree alloc] initWithData:_encryptedData password:self.compositeKey error:error]; + self.tree = [[KPKTree alloc] initWithData:self.encryptedData password:self.compositeKey error:error]; BOOL isUnlocked = (nil != self.tree); if(isUnlocked) { diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index 60f47519..da37f889 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -368,8 +368,8 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; if(!document.selectedGroup && !document.hasSearch) { /* no group selection out of search is wrong */ - [self.entryArrayController unbind:NSContentArrayBinding]; - [self.entryArrayController setContent:nil]; + //[self.entryArrayController unbind:NSContentArrayBinding]; + self.entryArrayController.content = nil; return; } /* @@ -387,7 +387,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; return; // we are showing the correct object right now. } } - [self.entryArrayController bind:NSContentArrayBinding toObject:document.selectedGroup withKeyPath:NSStringFromSelector(@selector(entries)) options:nil]; + self.entryArrayController.content = document.selectedGroup.entries; } [self _updateContextBar]; } @@ -422,8 +422,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; NSArray *result = [notification userInfo][kMPDocumentSearchResultsKey]; NSAssert(result != nil, @"Resutls should never be nil"); self.filteredEntries = result; - [self.entryArrayController unbind:NSContentArrayBinding]; - [self.entryArrayController setContent:self.filteredEntries]; + self.entryArrayController.content = self.filteredEntries; [[self.entryTable tableColumnWithIdentifier:MPEntryTableParentColumnIdentifier] setHidden:NO]; } @@ -433,8 +432,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; MPDocument *document = [[self windowController] document]; document.selectedItem = document.selectedGroup; if( nil == document.selectedItem && nil == document.selectedGroup ) { - [self.entryArrayController unbind:NSContentArrayBinding]; - [self.entryArrayController setContent:nil]; + self.entryArrayController.content = nil; } [self _updateContextBar]; } @@ -456,7 +454,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; [self _showContextBar]; /* TODO: Show modification date column if not present? */ MPDocument *document = [[self windowController] document]; - [self.entryArrayController bind:NSContentArrayBinding toObject:document.selectedEntry withKeyPath:NSStringFromSelector(@selector(history)) options:nil]; + self.entryArrayController.content = document.selectedEntry.history; } - (void)_didExitHistory:(NSNotification *)notification { diff --git a/MacPass/MPSavePanelAccessoryViewController.h b/MacPass/MPSavePanelAccessoryViewController.h index a806c5da..40544758 100644 --- a/MacPass/MPSavePanelAccessoryViewController.h +++ b/MacPass/MPSavePanelAccessoryViewController.h @@ -13,8 +13,8 @@ @interface MPSavePanelAccessoryViewController : MPViewController -@property (nonatomic, assign) NSSavePanel *savePanel; -@property (nonatomic, assign) MPDocument *document; +@property (nonatomic, weak) NSSavePanel *savePanel; +@property (nonatomic, weak) MPDocument *document; @property (nonatomic, assign, readonly) KPKVersion selectedVersion; @property (nonatomic, weak) IBOutlet NSPopUpButton *fileTypePopupButton;