From cdaa61f2e6c795df1a1deb7d54822b793b497ba3 Mon Sep 17 00:00:00 2001 From: michael starke Date: Wed, 13 Feb 2013 02:02:34 +0100 Subject: [PATCH] Introduced MPDatabaseController to handle file load/saveing --- MacPass.xcodeproj/project.pbxproj | 6 ++ MacPass/MPAppDelegate.m | 18 ++++- MacPass/MPDatabaseController.h | 29 ++++++++ MacPass/MPDatabaseController.m | 49 ++++++++++++++ MacPass/MPDatabaseDocument.h | 15 +++- MacPass/MPDatabaseDocument.m | 109 ++++++++++++++++++------------ MacPass/MPMainWindowController.m | 2 - MacPass/MPMainWindowDelegate.m | 11 --- 8 files changed, 181 insertions(+), 58 deletions(-) create mode 100644 MacPass/MPDatabaseController.h create mode 100644 MacPass/MPDatabaseController.m diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index be7efa28..1af3fe4b 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -44,6 +44,7 @@ 4C669BA116760ED100DD0774 /* Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C669B7916760ED100DD0774 /* Utils.m */; }; 4C669BA216760ED100DD0774 /* UUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C669B7B16760ED100DD0774 /* UUID.m */; }; 4C6B0E8C16C9B99B00A9ED23 /* PasswordView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C6B0E8B16C9B99B00A9ED23 /* PasswordView.xib */; }; + 4C75CE3C16CB128700F61A4D /* MPDatabaseController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C75CE3B16CB128700F61A4D /* MPDatabaseController.m */; }; 4C77E36715B84A240093A587 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C77E36615B84A240093A587 /* Cocoa.framework */; }; 4C77E37115B84A240093A587 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4C77E36F15B84A240093A587 /* InfoPlist.strings */; }; 4C77E37315B84A240093A587 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C77E37215B84A240093A587 /* main.m */; }; @@ -145,6 +146,8 @@ 4C669B7A16760ED100DD0774 /* UUID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UUID.h; sourceTree = ""; }; 4C669B7B16760ED100DD0774 /* UUID.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UUID.m; sourceTree = ""; }; 4C6B0E8B16C9B99B00A9ED23 /* PasswordView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PasswordView.xib; sourceTree = ""; }; + 4C75CE3A16CB128700F61A4D /* MPDatabaseController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDatabaseController.h; sourceTree = ""; }; + 4C75CE3B16CB128700F61A4D /* MPDatabaseController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPDatabaseController.m; sourceTree = ""; }; 4C77E36215B84A240093A587 /* MacPass.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MacPass.app; sourceTree = BUILT_PRODUCTS_DIR; }; 4C77E36615B84A240093A587 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; 4C77E36915B84A240093A587 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; @@ -411,6 +414,8 @@ 4CA0B2FB15BCAF8600654E32 /* MPSettingsController.m */, 4C83814015BF4677001AE468 /* MPMainWindowController.h */, 4C83814115BF4677001AE468 /* MPMainWindowController.m */, + 4C75CE3A16CB128700F61A4D /* MPDatabaseController.h */, + 4C75CE3B16CB128700F61A4D /* MPDatabaseController.m */, ); name = Controller; sourceTree = ""; @@ -601,6 +606,7 @@ 4C669BA116760ED100DD0774 /* Utils.m in Sources */, 4C669BA216760ED100DD0774 /* UUID.m in Sources */, 4C90D9B416CA4B18003081E7 /* MPMainWindowDelegate.m in Sources */, + 4C75CE3C16CB128700F61A4D /* MPDatabaseController.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index eadbe4d2..03cbd8e5 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -10,10 +10,13 @@ #import "MPMainWindowController.h" #import "MPSettingsController.h" +#import "MPDatabaseController.h" @interface MPAppDelegate () + @property (retain) MPSettingsController *settingsController; @property (retain) MPMainWindowController *mainWindowController; + - (IBAction)showPreferences:(id)sender; @end @@ -25,7 +28,7 @@ [_mainWindowController showWindow:[_mainWindowController window]]; } -#pragma mark IBActions +#pragma mark Menu Actions - (void)showPreferences:(id)sender { if(_settingsController == nil) { _settingsController = [[MPSettingsController alloc] init]; @@ -33,4 +36,17 @@ [_settingsController showWindow:_settingsController.window]; } +- (void)newDocument:(id)sender { + [[MPDatabaseController defaultController] createDatabase]; +} + +- (void)performClose:(id)sender { + NSLog(@"Close"); +} + +- (void)openDocument:(id)sender { + [[MPDatabaseController defaultController] openDatabase]; +} + + @end diff --git a/MacPass/MPDatabaseController.h b/MacPass/MPDatabaseController.h new file mode 100644 index 00000000..339b3a8e --- /dev/null +++ b/MacPass/MPDatabaseController.h @@ -0,0 +1,29 @@ +// +// MPDatabaseController.h +// MacPass +// +// Created by michael starke on 13.02.13. +// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved. +// + +#import + +@class MPDatabaseDocument; + + +typedef enum{ + MPDatabaseVersion1, + MPDatabaseVersion2 +} MPDatabaseVersion; + + +@interface MPDatabaseController : NSObject + +@property (retain, readonly) MPDatabaseDocument *database; + ++ (MPDatabaseController *)defaultController; + +- (MPDatabaseDocument *)createDatabase:(MPDatabaseVersion )version password:(NSString *)password keyfile:(NSURL *)key; +- (MPDatabaseDocument *)openDatabase:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key; + +@end diff --git a/MacPass/MPDatabaseController.m b/MacPass/MPDatabaseController.m new file mode 100644 index 00000000..528e62ce --- /dev/null +++ b/MacPass/MPDatabaseController.m @@ -0,0 +1,49 @@ +// +// MPDatabaseController.m +// MacPass +// +// Created by michael starke on 13.02.13. +// Copyright (c) 2013 HicknHack Software GmbH. All rights reserved. +// + +#import "MPDatabaseController.h" +#import "MPDatabaseDocument.h" + +@interface MPDatabaseController () + +@property (retain) MPDatabaseDocument *database; + +@end + +@implementation MPDatabaseController + ++ (MPDatabaseController *)defaultController { + static MPDatabaseController *sharedInstance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + sharedInstance = [[MPDatabaseController alloc] init]; + }); + + return sharedInstance; +} + +- (id)init +{ + self = [super init]; + if (self) { + // nothing to do; + } + return self; +} + +- (MPDatabaseDocument *)createDatabase:(MPDatabaseVersion)version password:(NSString *)password keyfile:(NSURL *)key { + + return self.database; +} + +- (MPDatabaseDocument *)openDatabase:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key { + self.database = [MPDatabaseDocument documentWithFile:file password:password keyfile:key]; + return self.database; +} + +@end diff --git a/MacPass/MPDatabaseDocument.h b/MacPass/MPDatabaseDocument.h index 697c2afa..679fbf4f 100644 --- a/MacPass/MPDatabaseDocument.h +++ b/MacPass/MPDatabaseDocument.h @@ -11,9 +11,22 @@ APPKIT_EXTERN NSString *const MPDidLoadDataBaseNotification; APPKIT_EXTERN NSString *const MPDataBaseDocumentDocumentKey; +@class KdbPassword; + @interface MPDatabaseDocument : NSObject +@property (retain, readonly) NSURL *file; +@property (retain, readonly) KdbPassword *password; ++ (id)documentWithFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key; + - (id)initWithFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key; -- (BOOL) openFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key; +/* + Saves the current database to the filesystem + Tries to use the stored password and file path + + If self.file and self.password aren't valid, the save does not get executed + */ +- (BOOL)save; +- (BOOL)saveAsFile:(NSURL *)file withPassword:(NSString *)password keyfile:(NSURL *)key; @end diff --git a/MacPass/MPDatabaseDocument.m b/MacPass/MPDatabaseDocument.m index a8b94658..7501f56b 100644 --- a/MacPass/MPDatabaseDocument.m +++ b/MacPass/MPDatabaseDocument.m @@ -13,66 +13,89 @@ NSString *const MPDidLoadDataBaseNotification = @"DidLoadDataBaseNotification"; @interface MPDatabaseDocument () @property (retain) KdbTree *tree; +@property (retain) NSURL *file; +@property (retain) KdbPassword *password; @end @implementation MPDatabaseDocument -- (id)init { - // no appropriate init method - return nil; ++ (id)documentWithFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key { + return [[[MPDatabaseDocument alloc] initWithFile:file password:password keyfile:key] autorelease]; } +- (id)init { + // create empty document + return [self initWithFile:nil password:nil keyfile:nil]; +} +/* + Designated initalizeder + */ - (id)initWithFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key { self = [super init]; if (self) { - BOOL sucess = [self openFile:file password:password keyfile:key]; - // Check if load was successfull and return nil of not - if( NO == sucess ) { - [self release]; - return nil; + /* + Create an empty file + */ + if(!file) { + self.tree = [[[KdbTree alloc] init] autorelease]; + self.tree.root = [[[KdbGroup alloc] init] autorelease]; + [self.tree.root setName:NSLocalizedString(@"INITIAL_GROUP", @"")]; } + /* + Try to load a given file + */ + else { + self.file = file; + const BOOL hasPassword = (password != nil); + const BOOL hasKeyfile = (key != nil); + + // Create the password for the given parameters + if( hasPassword && hasKeyfile) { + self.password = [[[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding keyfile:[key path]] autorelease]; + } + else if( hasPassword ) { + self.password = [[[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding] autorelease]; + } + else if( hasKeyfile ) { + self.password = [[[KdbPassword alloc] initWithKeyfile:[key path]] autorelease]; + } + else { + NSLog(@"Error: No password or keyfile given!"); + } + + @try { + self.tree = [KdbReaderFactory load:[self.file path] withPassword:self.password]; + } + @catch (NSException *exception) { + NSLog(@"%@", [exception description]); + } + + } + } + // Test if something went wrong and nil out if so + if( self.tree == nil) { + [self release]; + self = nil; } return self; } -- (BOOL) openFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key{ - // Try to load the file - KdbPassword *dbPassword = nil; - const BOOL hasPassword = (password != nil); - const BOOL hasKeyfile = (key != nil); - - // Create the password for the given parameters - if( hasPassword && hasKeyfile) { - dbPassword = [[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding keyfile:[key path]]; - } - else if( hasPassword ) { - dbPassword = [[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding]; - } - else if( hasKeyfile ) { - dbPassword = [[KdbPassword alloc] initWithKeyfile:[key path]]; - } - else { - NSLog(@"Error: No password or keyfile given!"); - return NO; - } - - @try { - self.tree = [KdbReaderFactory load:[file path] withPassword:dbPassword]; - } - @catch (NSException *exception) { - NSLog(@"%@", [exception description]); - } - // Cleanup - if( dbPassword != nil ) { - [dbPassword release]; - } - - if( self.tree != nil) { +- (BOOL)save { + NSError *fileError; + if( self.password && [self.file checkResourceIsReachableAndReturnError:&fileError] ) { + @try { + [KdbWriterFactory persist:self.tree file:[self.file path] withPassword:self.password]; + } + @catch (NSException *exception) { + NSLog(@"%@", [exception description]); + return NO; + } return YES; } - return NO; - } +- (BOOL)saveAsFile:(NSURL *)file withPassword:(NSString *)password keyfile:(NSURL *)key { + return NO; +} @end diff --git a/MacPass/MPMainWindowController.m b/MacPass/MPMainWindowController.m index d3012ff2..d6831a33 100644 --- a/MacPass/MPMainWindowController.m +++ b/MacPass/MPMainWindowController.m @@ -9,7 +9,6 @@ #import "MPMainWindowController.h" #import "MPOutlineDataSource.h" #import "MPOutlineViewDelegate.h" -#import "MPDatabaseDocument.h" #import "MPMainWindowDelegate.h" NSString *const kColumnIdentifier = @"OutlineColumn"; @@ -22,7 +21,6 @@ NSString *const kOutlineViewIdentifier = @"OutlineView"; @property (retain) MPOutlineDataSource *datasource; @property (retain) MPOutlineViewDelegate *outlineDelegate; @property (retain) MPMainWindowDelegate *windowDelegate; -@property (retain) MPDatabaseDocument *database; - (void)updateData; diff --git a/MacPass/MPMainWindowDelegate.m b/MacPass/MPMainWindowDelegate.m index de77be0c..5e82e4c5 100644 --- a/MacPass/MPMainWindowDelegate.m +++ b/MacPass/MPMainWindowDelegate.m @@ -10,16 +10,5 @@ @implementation MPMainWindowDelegate -- (void)newDocument:(id)sender { - NSLog(@"New"); -} - -- (void)performClose:(id)sender { - NSLog(@"Close"); -} - -- (void)openDocument:(id)sender { - NSLog(@"New Document"); -} @end