Extracted search into own class

This commit is contained in:
michael starke
2014-08-27 12:13:24 +02:00
parent 554da9dfc7
commit 0286ed4e5c
10 changed files with 135 additions and 36 deletions

View File

@@ -7,13 +7,15 @@
//
#import "MPContextBarViewController.h"
#import "HNHGradientView.h"
#import "KPKEntry.h"
#import "MPDocument+HistoryBrowsing.h"
#import "MPDocument+Search.h"
#import "MPFlagsHelper.h"
#import "MPEntrySearchContext.h"
#import "NSButton+HNHTextColor.h"
#import "MPFlagsHelper.h"
#import "HNHGradientView.h"
#import "HNHCommon.h"
NSUInteger const MPContextBarViewControllerActiveFilterMenuItemTag = 1000;
@@ -122,12 +124,13 @@ typedef NS_ENUM(NSUInteger, MPContextTab) {
- (void)_updateFilterButtons {
MPDocument *document = [[self windowController] document];
[self.duplicatePasswordsButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchDoublePasswords, document.activeFlags))];
[self.notesButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchNotes, document.activeFlags))];
[self.passwordButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchPasswords, document.activeFlags))];
[self.titleButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchTitles, document.activeFlags))];
[self.urlButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchUrls, document.activeFlags))];
[self.usernameButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchUsernames, document.activeFlags))];
MPEntrySearchFlags currentFlags = document.searchContext.searchFlags;
[self.duplicatePasswordsButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchDoublePasswords, currentFlags))];
[self.notesButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchNotes, currentFlags))];
[self.passwordButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchPasswords, currentFlags))];
[self.titleButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchTitles, currentFlags))];
[self.urlButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchUrls, currentFlags))];
[self.usernameButton setState:HNHStateForBool(MPTestFlagInOptions(MPEntrySearchUsernames, currentFlags))];
}
@end

View File

@@ -8,6 +8,8 @@
#import "MPDocument.h"
@class MPEntrySearchContext;
FOUNDATION_EXTERN NSString *const MPDocumentDidEnterSearchNotification;
FOUNDATION_EXTERN NSString *const MPDocumentDidChangeSearchFlags;
FOUNDATION_EXTERN NSString *const MPDocumentDidExitSearchNotification;
@@ -30,4 +32,6 @@ FOUNDATION_EXTERN NSString *const kMPDocumentSearchResultsKey;
/* called by the filter toggle buttons */
- (IBAction)toggleSearchFlags:(id)sender;
- (NSArray *)entriesMatchingSearch:(MPEntrySearchContext *)search;
@end

View File

@@ -30,13 +30,17 @@ NSString *const kMPDocumentSearchResultsKey = @"kMPDocumentSearchResul
- (void)performFindPanelAction:(id)sender {
self.hasSearch = YES;
/* the search context is loaded via defaults */
self.searchContext = [[MPEntrySearchContext alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidEnterSearchNotification object:self];
[self updateSearch:self];
}
- (void)updateSearch:(id)sender {
MPDocumentWindowController *windowController = [self windowControllers][0];
self.searchString = [windowController.searchField stringValue];
NSString *searchString = [windowController.searchField stringValue];
/* Update the search string */
self.searchContext = [[MPEntrySearchContext alloc] initWithString:searchString flags:self.searchContext.searchFlags];
if(NO == self.hasSearch) {
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidEnterSearchNotification object:self];
}
@@ -53,8 +57,10 @@ NSString *const kMPDocumentSearchResultsKey = @"kMPDocumentSearchResul
}
- (void)exitSearch:(id)sender {
self.searchString = nil;
self.searchContext = nil;
/*self.searchString = nil;
self.hasSearch = NO;
*/
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidExitSearchNotification object:self];
}
@@ -97,6 +103,10 @@ NSString *const kMPDocumentSearchResultsKey = @"kMPDocumentSearchResul
}
}
- (NSArray *)entriesMatchingSearch:(MPEntrySearchContext *)search {
return nil;
}
#pragma mark Search
- (NSArray *)_findEntriesMatchingCurrentSearch {
/* Filter double passwords */

View File

@@ -22,6 +22,7 @@
#import <Cocoa/Cocoa.h>
#import "KPKVersion.h"
#import "MPEntrySearchContext.h"
/**
* Posted when a new group was added to the document.
@@ -58,24 +59,6 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey;
@class KPKNode;
@class MPEditSession;
typedef NS_OPTIONS(NSUInteger, MPEntrySearchFlags) {
MPEntrySearchNone = 0,
MPEntrySearchUrls = (1<<0),
MPEntrySearchUsernames = (1<<1),
MPEntrySearchTitles = (1<<2),
MPEntrySearchPasswords = (1<<3),
MPEntrySearchNotes = (1<<4),
MPEntrySearchAllAttributes = (1<<5),
MPEntrySearchDoublePasswords = (1<<6), // Unused in GUI for now
MPEntrySearchAllFlags = (MPEntrySearchDoublePasswords |
MPEntrySearchNotes |
MPEntrySearchPasswords |
MPEntrySearchTitles |
MPEntrySearchUrls |
MPEntrySearchUsernames |
MPEntrySearchAllAttributes )
};
@interface MPDocument : NSDocument
@property (nonatomic, readonly, assign) BOOL encrypted;
@@ -102,9 +85,8 @@ typedef NS_OPTIONS(NSUInteger, MPEntrySearchFlags) {
/*
Search - see MPDocument+Search for further details
*/
@property (nonatomic, assign) MPEntrySearchFlags activeFlags;
@property (nonatomic, copy) NSString *searchString;
@property (nonatomic, assign) BOOL hasSearch;
@property (nonatomic, readonly, assign) BOOL hasSearch;
@property (nonatomic, copy) MPEntrySearchContext *searchContext;
@property (nonatomic, strong) NSArray *searchResult;
/*

View File

@@ -123,9 +123,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
_encryptedData = nil;
_didLockFile = NO;
_readOnly = NO;
_activeFlags = MPEntrySearchTitles;
_hasSearch = NO;
_unlockCount = 0;
self.tree = [KPKTree templateTree];
self.tree.metaData.rounds = [[NSUserDefaults standardUserDefaults] integerForKey:kMPSettingsKeyDefaultPasswordRounds];
}

View File

@@ -0,0 +1,41 @@
//
// MPEntrySearch.h
// MacPass
//
// Created by Michael Starke on 26.06.14.
// Copyright (c) 2014 HicknHack Software GmbH. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_OPTIONS(NSUInteger, MPEntrySearchFlags) {
MPEntrySearchNone = 0,
MPEntrySearchUrls = (1<<0),
MPEntrySearchUsernames = (1<<1),
MPEntrySearchTitles = (1<<2),
MPEntrySearchPasswords = (1<<3),
MPEntrySearchNotes = (1<<4),
MPEntrySearchAllAttributes = (1<<5),
MPEntrySearchDoublePasswords = (1<<6), // Unused in GUI for now
MPEntrySearchExpiredEntries = (1<<7), // Unused for now
MPEntrySearchAllFlags = (MPEntrySearchDoublePasswords |
MPEntrySearchExpiredEntries |
MPEntrySearchNotes |
MPEntrySearchPasswords |
MPEntrySearchTitles |
MPEntrySearchUrls |
MPEntrySearchUsernames |
MPEntrySearchAllAttributes )
};
/* Wrap serach criteria to be able to store them */
@interface MPEntrySearchContext : NSObject <NSSecureCoding>
+ (instancetype)defaultContext;
- (instancetype)initWithString:(NSString *)searchString flags:(MPEntrySearchFlags)flags;
@property (readonly, assign) NSInteger searchFlags;
@property (readonly, copy) NSString *searchString;
@end

View File

@@ -0,0 +1,55 @@
//
// MPEntrySearch.m
// MacPass
//
// Created by Michael Starke on 26.06.14.
// Copyright (c) 2014 HicknHack Software GmbH. All rights reserved.
//
#import "MPEntrySearchContext.h"
@interface MPEntrySearchContext ()
@property (assign) NSInteger searchFlags;
@property (copy) NSString *searchString;
@end
@implementation MPEntrySearchContext
+ (BOOL)supportsSecureCoding {
return YES;
}
+ (instancetype)defaultContext {
}
- (instancetype)init {
self = [self initWithString:nil flags:MPEntrySearchNone];
return self;
}
- (instancetype)initWithString:(NSString *)searchString flags:(MPEntrySearchFlags)flags {
self = [super init];
if(self) {
self.searchFlags = flags;
self.searchString = searchString;
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInteger:self.searchFlags forKey:NSStringFromSelector(@selector(searchFlags))];
[aCoder encodeObject:self.searchString forKey:NSStringFromSelector(@selector(searchString))];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [self init];
self.searchString = [aDecoder decodeObjectOfClass:[NSString class] forKey:NSStringFromSelector(@selector(searchString))];
self.searchFlags = [aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(searchFlags))];
return self;
}
@end

View File

@@ -51,7 +51,7 @@ APPKIT_EXTERN NSString *const kMPSettingsKeyEnableGlobalAutotype; //
APPKIT_EXTERN NSString *const kMPSettingsKeyGlobalAutotypeKeyDataKey; // The stored Data for the useder defined global autotype key
/* Search */
APPKIT_EXTERN NSString *const kMPSettingsKeyEntrySearchFilterMode;
APPKIT_EXTERN NSString *const kMPSettingsKeyEntrySearchFilterContext;
/* Quicklook */
APPKIT_EXTERN NSString *const kMPSettingsKeyEnableQuicklookPreview;

View File

@@ -37,7 +37,7 @@ NSString *const kMPSettingsKeySendCommandForControlKey = @"SendCo
NSString *const kMPSettingsKeyEnableGlobalAutotype = @"EnableGlobalAutotype";
NSString *const kMPSettingsKeyGlobalAutotypeKeyDataKey = @"GlobalAutotypeKeyDataKey";
NSString *const kMPSettingsKeyEntrySearchFilterMode = @"EntrySearchFilterMode";
NSString *const kMPSettingsKeyEntrySearchFilterContext = @"EntrySearchFilterContext";
NSString *const kMPSettingsKeyEnableQuicklookPreview = @"EnableQuicklookPreview";
@@ -49,6 +49,7 @@ NSString *const kMPSettingsKeyPasswordCharacterFlags = @"Passwo
NSString *const kMPSettingsKeyPasswordUseCustomString = @"PasswordUseCustomString";
NSString *const kMPSettingsKeyPasswordCustomString = @"PasswordCustomString";
/* Depricated */
NSString *const kMPSettingsKeyDoubleClickURLAction = @"DoubleClickURLAction";
NSString *const kMPSettingsKeyDoubleClickTitleAction = @"DoubleClickTitleAction";