Introduced MPDatabaseController to handle file load/saveing

This commit is contained in:
michael starke
2013-02-13 02:02:34 +01:00
parent 737ccf0ade
commit cdaa61f2e6
8 changed files with 181 additions and 58 deletions

View File

@@ -44,6 +44,7 @@
4C669BA116760ED100DD0774 /* Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C669B7916760ED100DD0774 /* Utils.m */; }; 4C669BA116760ED100DD0774 /* Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C669B7916760ED100DD0774 /* Utils.m */; };
4C669BA216760ED100DD0774 /* UUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C669B7B16760ED100DD0774 /* UUID.m */; }; 4C669BA216760ED100DD0774 /* UUID.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C669B7B16760ED100DD0774 /* UUID.m */; };
4C6B0E8C16C9B99B00A9ED23 /* PasswordView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C6B0E8B16C9B99B00A9ED23 /* PasswordView.xib */; }; 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 */; }; 4C77E36715B84A240093A587 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4C77E36615B84A240093A587 /* Cocoa.framework */; };
4C77E37115B84A240093A587 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4C77E36F15B84A240093A587 /* InfoPlist.strings */; }; 4C77E37115B84A240093A587 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4C77E36F15B84A240093A587 /* InfoPlist.strings */; };
4C77E37315B84A240093A587 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C77E37215B84A240093A587 /* main.m */; }; 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 = "<group>"; }; 4C669B7A16760ED100DD0774 /* UUID.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UUID.h; sourceTree = "<group>"; };
4C669B7B16760ED100DD0774 /* UUID.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UUID.m; sourceTree = "<group>"; }; 4C669B7B16760ED100DD0774 /* UUID.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UUID.m; sourceTree = "<group>"; };
4C6B0E8B16C9B99B00A9ED23 /* PasswordView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PasswordView.xib; sourceTree = "<group>"; }; 4C6B0E8B16C9B99B00A9ED23 /* PasswordView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PasswordView.xib; sourceTree = "<group>"; };
4C75CE3A16CB128700F61A4D /* MPDatabaseController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDatabaseController.h; sourceTree = "<group>"; };
4C75CE3B16CB128700F61A4D /* MPDatabaseController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPDatabaseController.m; sourceTree = "<group>"; };
4C77E36215B84A240093A587 /* MacPass.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MacPass.app; sourceTree = BUILT_PRODUCTS_DIR; }; 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; }; 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; }; 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 */, 4CA0B2FB15BCAF8600654E32 /* MPSettingsController.m */,
4C83814015BF4677001AE468 /* MPMainWindowController.h */, 4C83814015BF4677001AE468 /* MPMainWindowController.h */,
4C83814115BF4677001AE468 /* MPMainWindowController.m */, 4C83814115BF4677001AE468 /* MPMainWindowController.m */,
4C75CE3A16CB128700F61A4D /* MPDatabaseController.h */,
4C75CE3B16CB128700F61A4D /* MPDatabaseController.m */,
); );
name = Controller; name = Controller;
sourceTree = "<group>"; sourceTree = "<group>";
@@ -601,6 +606,7 @@
4C669BA116760ED100DD0774 /* Utils.m in Sources */, 4C669BA116760ED100DD0774 /* Utils.m in Sources */,
4C669BA216760ED100DD0774 /* UUID.m in Sources */, 4C669BA216760ED100DD0774 /* UUID.m in Sources */,
4C90D9B416CA4B18003081E7 /* MPMainWindowDelegate.m in Sources */, 4C90D9B416CA4B18003081E7 /* MPMainWindowDelegate.m in Sources */,
4C75CE3C16CB128700F61A4D /* MPDatabaseController.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };

View File

@@ -10,10 +10,13 @@
#import "MPMainWindowController.h" #import "MPMainWindowController.h"
#import "MPSettingsController.h" #import "MPSettingsController.h"
#import "MPDatabaseController.h"
@interface MPAppDelegate () @interface MPAppDelegate ()
@property (retain) MPSettingsController *settingsController; @property (retain) MPSettingsController *settingsController;
@property (retain) MPMainWindowController *mainWindowController; @property (retain) MPMainWindowController *mainWindowController;
- (IBAction)showPreferences:(id)sender; - (IBAction)showPreferences:(id)sender;
@end @end
@@ -25,7 +28,7 @@
[_mainWindowController showWindow:[_mainWindowController window]]; [_mainWindowController showWindow:[_mainWindowController window]];
} }
#pragma mark IBActions #pragma mark Menu Actions
- (void)showPreferences:(id)sender { - (void)showPreferences:(id)sender {
if(_settingsController == nil) { if(_settingsController == nil) {
_settingsController = [[MPSettingsController alloc] init]; _settingsController = [[MPSettingsController alloc] init];
@@ -33,4 +36,17 @@
[_settingsController showWindow:_settingsController.window]; [_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 @end

View File

@@ -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 <Foundation/Foundation.h>
@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

View File

@@ -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

View File

@@ -11,9 +11,22 @@
APPKIT_EXTERN NSString *const MPDidLoadDataBaseNotification; APPKIT_EXTERN NSString *const MPDidLoadDataBaseNotification;
APPKIT_EXTERN NSString *const MPDataBaseDocumentDocumentKey; APPKIT_EXTERN NSString *const MPDataBaseDocumentDocumentKey;
@class KdbPassword;
@interface MPDatabaseDocument : NSObject @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; - (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 @end

View File

@@ -13,66 +13,89 @@ NSString *const MPDidLoadDataBaseNotification = @"DidLoadDataBaseNotification";
@interface MPDatabaseDocument () @interface MPDatabaseDocument ()
@property (retain) KdbTree *tree; @property (retain) KdbTree *tree;
@property (retain) NSURL *file;
@property (retain) KdbPassword *password;
@end @end
@implementation MPDatabaseDocument @implementation MPDatabaseDocument
- (id)init { + (id)documentWithFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key {
// no appropriate init method return [[[MPDatabaseDocument alloc] initWithFile:file password:password keyfile:key] autorelease];
return nil;
} }
- (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 - (id)initWithFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key
{ {
self = [super init]; self = [super init];
if (self) { if (self) {
BOOL sucess = [self openFile:file password:password keyfile:key]; /*
// Check if load was successfull and return nil of not Create an empty file
if( NO == sucess ) { */
[self release]; if(!file) {
return nil; self.tree = [[[KdbTree alloc] init] autorelease];
self.tree.root = [[[KdbGroup alloc] init] autorelease];
[self.tree.root setName:NSLocalizedString(@"INITIAL_GROUP", @"")];
} }
} /*
return self; Try to load a given file
} */
else {
- (BOOL) openFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key{ self.file = file;
// Try to load the file
KdbPassword *dbPassword = nil;
const BOOL hasPassword = (password != nil); const BOOL hasPassword = (password != nil);
const BOOL hasKeyfile = (key != nil); const BOOL hasKeyfile = (key != nil);
// Create the password for the given parameters // Create the password for the given parameters
if( hasPassword && hasKeyfile) { if( hasPassword && hasKeyfile) {
dbPassword = [[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding keyfile:[key path]]; self.password = [[[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding keyfile:[key path]] autorelease];
} }
else if( hasPassword ) { else if( hasPassword ) {
dbPassword = [[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding]; self.password = [[[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding] autorelease];
} }
else if( hasKeyfile ) { else if( hasKeyfile ) {
dbPassword = [[KdbPassword alloc] initWithKeyfile:[key path]]; self.password = [[[KdbPassword alloc] initWithKeyfile:[key path]] autorelease];
} }
else { else {
NSLog(@"Error: No password or keyfile given!"); NSLog(@"Error: No password or keyfile given!");
return NO;
} }
@try { @try {
self.tree = [KdbReaderFactory load:[file path] withPassword:dbPassword]; self.tree = [KdbReaderFactory load:[self.file path] withPassword:self.password];
} }
@catch (NSException *exception) { @catch (NSException *exception) {
NSLog(@"%@", [exception description]); NSLog(@"%@", [exception description]);
} }
// Cleanup
if( dbPassword != nil ) {
[dbPassword release];
}
if( self.tree != nil) {
return YES;
} }
return NO; }
// Test if something went wrong and nil out if so
if( self.tree == nil) {
[self release];
self = nil;
}
return self;
} }
- (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;
}
}
- (BOOL)saveAsFile:(NSURL *)file withPassword:(NSString *)password keyfile:(NSURL *)key {
return NO;
}
@end @end

View File

@@ -9,7 +9,6 @@
#import "MPMainWindowController.h" #import "MPMainWindowController.h"
#import "MPOutlineDataSource.h" #import "MPOutlineDataSource.h"
#import "MPOutlineViewDelegate.h" #import "MPOutlineViewDelegate.h"
#import "MPDatabaseDocument.h"
#import "MPMainWindowDelegate.h" #import "MPMainWindowDelegate.h"
NSString *const kColumnIdentifier = @"OutlineColumn"; NSString *const kColumnIdentifier = @"OutlineColumn";
@@ -22,7 +21,6 @@ NSString *const kOutlineViewIdentifier = @"OutlineView";
@property (retain) MPOutlineDataSource *datasource; @property (retain) MPOutlineDataSource *datasource;
@property (retain) MPOutlineViewDelegate *outlineDelegate; @property (retain) MPOutlineViewDelegate *outlineDelegate;
@property (retain) MPMainWindowDelegate *windowDelegate; @property (retain) MPMainWindowDelegate *windowDelegate;
@property (retain) MPDatabaseDocument *database;
- (void)updateData; - (void)updateData;

View File

@@ -10,16 +10,5 @@
@implementation MPMainWindowDelegate @implementation MPMainWindowDelegate
- (void)newDocument:(id)sender {
NSLog(@"New");
}
- (void)performClose:(id)sender {
NSLog(@"Close");
}
- (void)openDocument:(id)sender {
NSLog(@"New Document");
}
@end @end