Fixes #153 (mentioned in #152)

Save as now asks for a Password if none was set.
This commit is contained in:
michael starke
2014-03-09 18:18:13 +01:00
parent 2e0613dad5
commit f0964d04b9
3 changed files with 37 additions and 9 deletions

View File

@@ -130,6 +130,14 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
[self addWindowController:windowController]; [self addWindowController:windowController];
} }
- (void)saveDocumentAs:(id)sender {
[super saveDocumentAs:sender];
}
- (void)saveDocument:(id)sender {
[super saveDocument:sender];
}
- (void)windowControllerDidLoadNib:(NSWindowController *)aController - (void)windowControllerDidLoadNib:(NSWindowController *)aController
{ {
[super windowControllerDidLoadNib:aController]; [super windowControllerDidLoadNib:aController];
@@ -137,13 +145,18 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
- (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError { - (BOOL)writeToURL:(NSURL *)url ofType:(NSString *)typeName error:(NSError **)outError {
if(!self.compositeKey.hasPasswordOrKeyFile) { if(!self.compositeKey.hasPasswordOrKeyFile) {
if(outError != NULL) {
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: NSLocalizedString(@"NO_PASSWORD_OR_KEY_SET", "") };
*outError = [NSError errorWithDomain:MPErrorDomain code:0 userInfo:userInfo];
}
return NO; // No password or key. No save possible return NO; // No password or key. No save possible
} }
NSString *fileType = [self fileTypeFromLastRunSavePanel]; NSString *fileType = [self fileTypeFromLastRunSavePanel];
KPKVersion version = [[self class] versionForFileType:fileType]; KPKVersion version = [[self class] versionForFileType:fileType];
if(version == KPKUnknownVersion) { if(version == KPKUnknownVersion) {
if(outError != NULL) { if(outError != NULL) {
*outError = [NSError errorWithDomain:MPErrorDomain code:0 userInfo:nil]; NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: NSLocalizedString(@"UNKNOWN_FILE_VERSION", "") };
*outError = [NSError errorWithDomain:MPErrorDomain code:0 userInfo:userInfo];
} }
return NO; return NO;
} }

View File

@@ -32,6 +32,7 @@
#pragma mark Actions #pragma mark Actions
- (IBAction)saveDocument:(id)sender; - (IBAction)saveDocument:(id)sender;
- (IBAction)saveDocumentAs:(id)sender;
- (IBAction)editPassword:(id)sender; - (IBAction)editPassword:(id)sender;
- (IBAction)showDatabaseSettings:(id)sender; - (IBAction)showDatabaseSettings:(id)sender;

View File

@@ -29,10 +29,11 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
MPAlertLossySaveWarning, MPAlertLossySaveWarning,
}; };
typedef void (^MPPasswordChangedBlock)(void);
@interface MPDocumentWindowController () { @interface MPDocumentWindowController () {
@private @private
id _firstResponder; id _firstResponder;
BOOL _saveAfterPasswordChange;
} }
@property (strong) IBOutlet NSSplitView *splitView; @property (strong) IBOutlet NSSplitView *splitView;
@@ -48,6 +49,8 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
@property (strong) MPPasswordEditWindowController *passwordEditWindowController; @property (strong) MPPasswordEditWindowController *passwordEditWindowController;
@property (strong) MPToolbarDelegate *toolbarDelegate; @property (strong) MPToolbarDelegate *toolbarDelegate;
@property (nonatomic, copy) MPPasswordChangedBlock passwordChangedBlock;
@end @end
@implementation MPDocumentWindowController @implementation MPDocumentWindowController
@@ -56,7 +59,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
self = [super initWithWindowNibName:@"DocumentWindow" owner:self]; self = [super initWithWindowNibName:@"DocumentWindow" owner:self];
if( self ) { if( self ) {
_firstResponder = nil; _firstResponder = nil;
_saveAfterPasswordChange = NO;
_toolbarDelegate = [[MPToolbarDelegate alloc] init]; _toolbarDelegate = [[MPToolbarDelegate alloc] init];
_outlineViewController = [[MPOutlineViewController alloc] init]; _outlineViewController = [[MPOutlineViewController alloc] init];
_entryViewController = [[MPEntryViewController alloc] init]; _entryViewController = [[MPEntryViewController alloc] init];
@@ -167,7 +169,7 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
#pragma mark Actions #pragma mark Actions
- (void)saveDocument:(id)sender { - (void)saveDocument:(id)sender {
_saveAfterPasswordChange = NO; self.passwordChangedBlock = nil;
MPDocument *document = [self document]; MPDocument *document = [self document];
NSString *fileType = [document fileType]; NSString *fileType = [document fileType];
/* we did open as legacy */ /* we did open as legacy */
@@ -187,13 +189,25 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
} }
} }
else if(!document.compositeKey) { else if(!document.compositeKey) {
_saveAfterPasswordChange = YES; __weak MPDocument *weakDocument = [self document];
self.passwordChangedBlock = ^void(void){[weakDocument saveDocument:sender];};
[self editPassword:sender]; [self editPassword:sender];
return; return;
} }
/* All set and good ready to save */ /* All set and good ready to save */
[[self document] saveDocument:sender]; [[self document] saveDocument:sender];
} }
- (void)saveDocumentAs:(id)sender {
self.passwordChangedBlock = nil;
MPDocument *document = [self document];
if(!document.compositeKey) {
__weak MPDocument *weakDocument = [self document];
self.passwordChangedBlock = ^void(void){[weakDocument saveDocumentAs:sender];};
[self editPassword:sender];
return;
}
[[self document] saveDocumentAs:sender];
}
- (void)exportAsXML:(id)sender { - (void)exportAsXML:(id)sender {
NSSavePanel *savePanel = [NSSavePanel savePanel]; NSSavePanel *savePanel = [NSSavePanel savePanel];
@@ -238,7 +252,7 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
self.passwordEditWindowController.delegate = self; self.passwordEditWindowController.delegate = self;
} }
/* Disallow empty password if we want to save afterwards, otherwise the dialog keeps poping up */ /* Disallow empty password if we want to save afterwards, otherwise the dialog keeps poping up */
self.passwordEditWindowController.allowsEmptyPasswordOrKey = !_saveAfterPasswordChange; self.passwordEditWindowController.allowsEmptyPasswordOrKey = (self.passwordChangedBlock == nil);
[NSApp beginSheet:[self.passwordEditWindowController window] modalForWindow:[self window] modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; [NSApp beginSheet:[self.passwordEditWindowController window] modalForWindow:[self window] modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
} }
@@ -375,10 +389,10 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
#pragma mark MPPasswordEditWindowDelegate #pragma mark MPPasswordEditWindowDelegate
- (void)didFinishPasswordEditing:(BOOL)changedPasswordOrKey { - (void)didFinishPasswordEditing:(BOOL)changedPasswordOrKey {
if(changedPasswordOrKey && _saveAfterPasswordChange) { if(changedPasswordOrKey && self.passwordChangedBlock) {
[self saveDocument:nil]; self.passwordChangedBlock();
} }
_saveAfterPasswordChange = NO; self.passwordChangedBlock = nil;
} }
#pragma mark Alert Delegate #pragma mark Alert Delegate