mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 03:32:40 +00:00
Introduced MPDatabaseController to handle file load/saveing
This commit is contained in:
@@ -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;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
29
MacPass/MPDatabaseController.h
Normal file
29
MacPass/MPDatabaseController.h
Normal 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
|
||||||
49
MacPass/MPDatabaseController.m
Normal file
49
MacPass/MPDatabaseController.m
Normal 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
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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", @"")];
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
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;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (BOOL) openFile:(NSURL *)file password:(NSString *)password keyfile:(NSURL *)key{
|
- (BOOL)save {
|
||||||
// Try to load the file
|
NSError *fileError;
|
||||||
KdbPassword *dbPassword = nil;
|
if( self.password && [self.file checkResourceIsReachableAndReturnError:&fileError] ) {
|
||||||
const BOOL hasPassword = (password != nil);
|
@try {
|
||||||
const BOOL hasKeyfile = (key != nil);
|
[KdbWriterFactory persist:self.tree file:[self.file path] withPassword:self.password];
|
||||||
|
}
|
||||||
// Create the password for the given parameters
|
@catch (NSException *exception) {
|
||||||
if( hasPassword && hasKeyfile) {
|
NSLog(@"%@", [exception description]);
|
||||||
dbPassword = [[KdbPassword alloc] initWithPassword:password encoding:NSUTF8StringEncoding keyfile:[key path]];
|
return NO;
|
||||||
}
|
}
|
||||||
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) {
|
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
return NO;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)saveAsFile:(NSURL *)file withPassword:(NSString *)password keyfile:(NSURL *)key {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user