Using KPKDatabaseType instead of KPKVersion.

This commit is contained in:
michael starke
2016-09-04 16:50:30 +02:00
parent 024e9cf41a
commit f0d4b2f835
12 changed files with 46 additions and 34 deletions

View File

@@ -1,3 +1,3 @@
github "sparkle-project/Sparkle" ~> 1.13.1 github "sparkle-project/Sparkle" ~> 1.13.1
github "mstarke/KeePassKit" "d66c888b299358481da2ba2672ec7c644cce56ec" github "mstarke/KeePassKit" "5c98755e9954549a74f421d90af8cd6b5c0e01cc"
github "mstarke/HNHUi" ~> 1.1 github "mstarke/HNHUi" ~> 1.1

View File

@@ -1,3 +1,3 @@
github "mstarke/HNHUi" "1.1" github "mstarke/HNHUi" "1.1"
github "mstarke/KeePassKit" "d66c888b299358481da2ba2672ec7c644cce56ec" github "mstarke/KeePassKit" "5c98755e9954549a74f421d90af8cd6b5c0e01cc"
github "sparkle-project/Sparkle" "1.14.0" github "sparkle-project/Sparkle" "1.14.0"

View File

@@ -17,16 +17,16 @@
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
typeToUTI = @{ typeToUTI = @{
@(KPKLegacyVersion) : MPLegacyDocumentUTI, @(KPKDatabaseTypeBinary) : MPLegacyDocumentUTI,
@(KPKXmlVersion) : MPXMLDocumentUTI @(KPKDatabaseTypeXml) : MPXMLDocumentUTI
}; };
}); });
return typeToUTI; return typeToUTI;
} }
- (NSString *)typeForData:(NSData *)data { - (NSString *)typeForData:(NSData *)data {
KPKVersion version = [self databaseVersionForData:data]; KPKFileInfo fileInfo = [self fileInfoForData:data];
return [self _typeToUTIdictionary][@(version)]; return [self _typeToUTIdictionary][@(fileInfo.type)];
} }
- (NSString *)typeForContentOfURL:(NSURL *)url { - (NSString *)typeForContentOfURL:(NSURL *)url {

View File

@@ -37,6 +37,9 @@
return _tokens; return _tokens;
} }
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (NSString *)nibName { - (NSString *)nibName {
return @"AutotypeBuilderView"; return @"AutotypeBuilderView";
@@ -46,6 +49,15 @@
[super viewDidLoad]; [super viewDidLoad];
self.tokenField.editable = NO; self.tokenField.editable = NO;
self.tokenField.objectValue = self.tokens; self.tokenField.objectValue = self.tokens;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_textViewDidChangeSelection:) name:NSTextViewDidChangeSelectionNotification object:nil];
} }
- (void)_textViewDidChangeSelection:(NSNotification *)notification {
NSTextView *tv = [self.tokenField.cell fieldEditorForView:self.tokenField];
if(tv) {
NSArray *selectionRanges = tv.selectedRanges;
}
}
@end @end

View File

@@ -78,7 +78,7 @@ FOUNDATION_EXPORT NSString *const MPDocumentGroupKey;
@property (nonatomic, strong, readonly) KPKCompositeKey *compositeKey; @property (nonatomic, strong, readonly) KPKCompositeKey *compositeKey;
@property (assign, readonly, getter = isReadOnly) BOOL readOnly; @property (assign, readonly, getter = isReadOnly) BOOL readOnly;
@property (nonatomic, readonly, assign) KPKVersion versionForFileType; @property (nonatomic, readonly, assign) KPKDatabaseType versionForFileType;
/* /*
State (active group/entry) State (active group/entry)
@@ -96,8 +96,8 @@ FOUNDATION_EXPORT NSString *const MPDocumentGroupKey;
@property (nonatomic, copy) MPEntrySearchContext *searchContext; @property (nonatomic, copy) MPEntrySearchContext *searchContext;
@property (nonatomic, strong, readonly) NSArray *searchResult; @property (nonatomic, strong, readonly) NSArray *searchResult;
+ (KPKVersion)versionForFileType:(NSString *)fileType; + (KPKDatabaseType)versionForFileType:(NSString *)fileType;
+ (NSString *)fileTypeForVersion:(KPKVersion)version; + (NSString *)fileTypeForVersion:(KPKDatabaseType)version;
#pragma mark Lock/Decrypt #pragma mark Lock/Decrypt
- (IBAction)lockDatabase:(id)sender; - (IBAction)lockDatabase:(id)sender;

View File

@@ -89,22 +89,22 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
return [NSSet setWithObject:NSStringFromSelector(@selector(tree))]; return [NSSet setWithObject:NSStringFromSelector(@selector(tree))];
} }
+ (KPKVersion)versionForFileType:(NSString *)fileType { + (KPKDatabaseType)versionForFileType:(NSString *)fileType {
if( NSOrderedSame == [fileType compare:MPLegacyDocumentUTI options:NSCaseInsensitiveSearch]) { if( NSOrderedSame == [fileType compare:MPLegacyDocumentUTI options:NSCaseInsensitiveSearch]) {
return KPKLegacyVersion; return KPKDatabaseTypeBinary;
} }
if( NSOrderedSame == [fileType compare:MPXMLDocumentUTI options:NSCaseInsensitiveSearch]) { if( NSOrderedSame == [fileType compare:MPXMLDocumentUTI options:NSCaseInsensitiveSearch]) {
return KPKXmlVersion; return KPKDatabaseTypeXml;
} }
return KPKUnknownVersion; return KPKDatabaseTypeUnknown;
} }
+ (NSString *)fileTypeForVersion:(KPKVersion)version { + (NSString *)fileTypeForVersion:(KPKDatabaseType)version {
switch(version) { switch(version) {
case KPKLegacyVersion: case KPKDatabaseTypeBinary:
return MPLegacyDocumentUTI; return MPLegacyDocumentUTI;
case KPKXmlVersion: case KPKDatabaseTypeXml:
return MPXMLDocumentUTI; return MPXMLDocumentUTI;
default: default:
@@ -182,8 +182,8 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
return nil; // Saving without a password/key is not possible return nil; // Saving without a password/key is not possible
} }
NSString *fileType = self.fileTypeFromLastRunSavePanel; NSString *fileType = self.fileTypeFromLastRunSavePanel;
KPKVersion version = [self.class versionForFileType:fileType]; KPKDatabaseType version = [self.class versionForFileType:fileType];
if(version == KPKUnknownVersion) { if(version == KPKDatabaseTypeUnknown) {
if(outError != NULL) { if(outError != NULL) {
NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: NSLocalizedString(@"UNKNOWN_FILE_VERSION", "") }; NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: NSLocalizedString(@"UNKNOWN_FILE_VERSION", "") };
*outError = [NSError errorWithDomain:MPErrorDomain code:0 userInfo:userInfo]; *outError = [NSError errorWithDomain:MPErrorDomain code:0 userInfo:userInfo];
@@ -325,7 +325,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
NSError *error; NSError *error;
/* FIXME: User feedback is ignored */ /* FIXME: User feedback is ignored */
[self saveDocument:sender]; [self saveDocument:sender];
self.encryptedData = [self.tree encryptWithPassword:self.compositeKey forVersion:KPKXmlVersion error:&error]; self.encryptedData = [self.tree encryptWithPassword:self.compositeKey forVersion:KPKDatabaseTypeXml error:&error];
self.tree = nil; self.tree = nil;
[self.undoManager removeAllActions]; [self.undoManager removeAllActions];
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidLockDatabaseNotification object:self]; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidLockDatabaseNotification object:self];
@@ -389,8 +389,8 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
} }
#pragma mark Properties #pragma mark Properties
- (KPKVersion)versionForFileType { - (KPKDatabaseType)versionForFileType {
return [[self class] versionForFileType:[self fileType]]; return [[self class] versionForFileType:self.fileType];
} }
- (BOOL)encrypted { - (BOOL)encrypted {

View File

@@ -193,7 +193,7 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
NSString *fileType = document.fileType; NSString *fileType = document.fileType;
/* we did open as legacy */ /* we did open as legacy */
if([fileType isEqualToString:MPLegacyDocumentUTI]) { if([fileType isEqualToString:MPLegacyDocumentUTI]) {
if(document.tree.minimumVersion != KPKLegacyVersion) { if(document.tree.minimumType != KPKDatabaseTypeBinary) {
NSAlert *alert = [[NSAlert alloc] init]; NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSWarningAlertStyle; alert.alertStyle = NSWarningAlertStyle;
alert.messageText = NSLocalizedString(@"WARNING_ON_LOSSY_SAVE", ""); alert.messageText = NSLocalizedString(@"WARNING_ON_LOSSY_SAVE", "");

View File

@@ -117,7 +117,7 @@
- (IBAction)generateKey:(id)sender { - (IBAction)generateKey:(id)sender {
MPDocument *document = self.document; MPDocument *document = self.document;
NSData *data = [NSData generateKeyfiledataForVersion:document.tree.minimumVersion]; NSData *data = [NSData generateKeyfiledataForVersion:document.tree.minimumType];
if(data) { if(data) {
NSSavePanel *savePanel = [NSSavePanel savePanel]; NSSavePanel *savePanel = [NSSavePanel savePanel];
savePanel.allowedFileTypes = @[@"key", @"xml"]; savePanel.allowedFileTypes = @[@"key", @"xml"];

View File

@@ -15,7 +15,7 @@
@property (nonatomic, weak) NSSavePanel *savePanel; @property (nonatomic, weak) NSSavePanel *savePanel;
@property (nonatomic, weak) MPDocument *document; @property (nonatomic, weak) MPDocument *document;
@property (nonatomic, assign, readonly) KPKVersion selectedVersion; @property (nonatomic, assign, readonly) KPKDatabaseType selectedVersion;
@property (nonatomic, weak) IBOutlet NSPopUpButton *fileTypePopupButton; @property (nonatomic, weak) IBOutlet NSPopUpButton *fileTypePopupButton;
@property (nonatomic, weak) IBOutlet NSTextField *infoTextField; @property (nonatomic, weak) IBOutlet NSTextField *infoTextField;

View File

@@ -13,7 +13,7 @@
#import "KeePassKit/KeePassKit.h" #import "KeePassKit/KeePassKit.h"
@interface MPSavePanelAccessoryViewController () @interface MPSavePanelAccessoryViewController ()
@property (readwrite, assign) KPKVersion selectedVersion; @property (readwrite, assign) KPKDatabaseType selectedVersion;
@end @end
@implementation MPSavePanelAccessoryViewController @implementation MPSavePanelAccessoryViewController
@@ -42,10 +42,10 @@
- (IBAction)setFileType:(id)sender { - (IBAction)setFileType:(id)sender {
NSString *uti = self.fileTypePopupButton.selectedItem.representedObject; NSString *uti = self.fileTypePopupButton.selectedItem.representedObject;
if([uti isEqualToString:MPLegacyDocumentUTI]) { if([uti isEqualToString:MPLegacyDocumentUTI]) {
self.selectedVersion = KPKLegacyVersion; self.selectedVersion = KPKDatabaseTypeBinary;
} }
else if([uti isEqualToString:MPXMLDocumentUTI]) { else if([uti isEqualToString:MPXMLDocumentUTI]) {
self.selectedVersion = KPKXmlVersion; self.selectedVersion = KPKDatabaseTypeXml;
} }
NSAssert(uti != nil, @"UTI cannot be nil"); NSAssert(uti != nil, @"UTI cannot be nil");
[self _updateNote]; [self _updateNote];
@@ -66,13 +66,13 @@
NSView *view = self.view; NSView *view = self.view;
NSAssert(view != nil, @"View has to be loaded at this point"); NSAssert(view != nil, @"View has to be loaded at this point");
switch(self.document.versionForFileType) { switch(self.document.versionForFileType) {
case KPKLegacyVersion: case KPKDatabaseTypeBinary:
[self.fileTypePopupButton selectItemAtIndex:1]; [self.fileTypePopupButton selectItemAtIndex:1];
break; break;
case KPKXmlVersion: case KPKDatabaseTypeXml:
[self.fileTypePopupButton selectItemAtIndex:0]; [self.fileTypePopupButton selectItemAtIndex:0];
break; break;
case KPKUnknownVersion: default:
NSAssert(NO, @"Minimum Version should always be valid"); NSAssert(NO, @"Minimum Version should always be valid");
break; break;
} }
@@ -82,8 +82,8 @@
- (void)_updateNote { - (void)_updateNote {
NSString *uti = [[self.fileTypePopupButton selectedItem] representedObject]; NSString *uti = [[self.fileTypePopupButton selectedItem] representedObject];
BOOL showInfoText = (self.document.tree.minimumVersion == KPKXmlVersion && [uti isEqualToString:MPLegacyDocumentUTI]); BOOL showInfoText = (self.document.tree.minimumType == KPKDatabaseTypeXml && [uti isEqualToString:MPLegacyDocumentUTI]);
[self.infoTextField setHidden:!showInfoText]; self.infoTextField.hidden = !showInfoText;
} }
@end @end

View File

@@ -28,7 +28,7 @@
XCTAssertTrue(document.encrypted, @"Loaded but unencrypted should be not decrypted"); XCTAssertTrue(document.encrypted, @"Loaded but unencrypted should be not decrypted");
XCTAssertTrue([document unlockWithPassword:@"1234" keyFileURL:nil error:&error], @"Should decrypt with password"); XCTAssertTrue([document unlockWithPassword:@"1234" keyFileURL:nil error:&error], @"Should decrypt with password");
XCTAssertNil(error, @"No Error should occur on unlocking with correct password"); XCTAssertNil(error, @"No Error should occur on unlocking with correct password");
XCTAssertTrue((document.tree.minimumVersion = KPKLegacyVersion), @"Minimal Version should not increase with KDB File loaded"); XCTAssertTrue((document.tree.minimumType = KPKDatabaseTypeBinary), @"Minimal Version should not increase with KDB File loaded");
//STAssertTrue([document.fileType isEqualToString:[MPDocument fileTypeForVersion:KPKLegacyVersion]], @"File type needs to match opened file"); //STAssertTrue([document.fileType isEqualToString:[MPDocument fileTypeForVersion:KPKLegacyVersion]], @"File type needs to match opened file");
} }

View File

@@ -28,7 +28,7 @@
- (void)testCreateUntitledDocument { - (void)testCreateUntitledDocument {
MPDocument *document = [[MPDocument alloc] initWithType:@"" error:nil]; MPDocument *document = [[MPDocument alloc] initWithType:@"" error:nil];
XCTAssertNotNil(document, @"Document should be created"); XCTAssertNotNil(document, @"Document should be created");
XCTAssertTrue(document.tree.minimumVersion == KPKLegacyVersion, @"Tree should be Legacy Version in default case"); XCTAssertTrue(document.tree.minimumType == KPKDatabaseTypeBinary, @"Tree should be Legacy Version in default case");
XCTAssertFalse(document.encrypted, @"Document cannot be encrypted at creation"); XCTAssertFalse(document.encrypted, @"Document cannot be encrypted at creation");
XCTAssertFalse(document.compositeKey.hasPasswordOrKeyFile, @"Document has no Password/Keyfile and thus is not secured"); XCTAssertFalse(document.compositeKey.hasPasswordOrKeyFile, @"Document has no Password/Keyfile and thus is not secured");