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

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

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

View File

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

View File

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

View File

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