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 */; };
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 = "<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>"; };
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; };
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 = "<group>";
@@ -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;
};

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