Skip to content
Permalink
Browse files

#63: Add basic support for multiple filters

  • Loading branch information...
dmoagx committed May 7, 2018
1 parent a54ecfa commit 66e8dc21b8dfe25f83872036a1268d4e014f16eb

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -39,7 +39,7 @@

SPDatabaseDocument *tableDocumentInstance;
#ifndef SP_CODA /* ivars */
NSURL *delegatesFileURL;
NSURL *documentFileURL;
#endif

IBOutlet id encodingPopUp;
@@ -65,7 +65,7 @@
NSString *filterType;
}

- (id)initWithDelegate:(id)managerDelegate forFilterType:(NSString *)compareType;
- (id)initWithDatabaseDocument:(SPDatabaseDocument *)document forFilterType:(NSString *)compareType;

// Accessors
- (NSMutableArray *)contentFilterForFileURL:(NSURL *)fileURL;
@@ -48,31 +48,29 @@
@implementation SPContentFilterManager

/**
* Initialize the manager with the supplied delegate
* Initialize the manager with the supplied document
*/
- (id)initWithDelegate:(id)managerDelegate forFilterType:(NSString *)compareType
- (id)initWithDatabaseDocument:(SPDatabaseDocument *)document forFilterType:(NSString *)compareType
{
if ((self = [super initWithWindowNibName:@"ContentFilterManager"])) {
if (document == nil) {
NSBeep();
NSLog(@"ContentFilterManager was called without a document.");

return nil;
}

if ((self = [super initWithWindowNibName:@"ContentFilterManager"])) {
#ifndef SP_CODA
prefs = [NSUserDefaults standardUserDefaults];
#endif

contentFilters = [[NSMutableArray alloc] init];

if (managerDelegate == nil) {
NSBeep();
NSLog(@"ContentFilterManager was called without a delegate.");

return nil;
}

tableDocumentInstance = [managerDelegate valueForKeyPath:@"tableDocumentInstance"];
tableDocumentInstance = document;
#ifndef SP_CODA
delegatesFileURL = [tableDocumentInstance fileURL];
documentFileURL = [[tableDocumentInstance fileURL] copy];
#endif

filterType = [NSString stringWithString:compareType];
filterType = [compareType copy];
}

return self;
@@ -113,13 +111,13 @@ - (void)awakeFromNib

// Build doc-based filters
[contentFilters addObject:[NSDictionary dictionaryWithObjectsAndKeys:
[[[delegatesFileURL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent], @"MenuLabel",
[delegatesFileURL absoluteString], @"headerOfFileURL",
[[[documentFileURL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent], @"MenuLabel",
[documentFileURL absoluteString], @"headerOfFileURL",
@"", @"Clause",
nil]];

if ([[SPQueryController sharedQueryController] contentFilterForFileURL:delegatesFileURL]) {
id filters = [[SPQueryController sharedQueryController] contentFilterForFileURL:delegatesFileURL];
if ([[SPQueryController sharedQueryController] contentFilterForFileURL:documentFileURL]) {
id filters = [[SPQueryController sharedQueryController] contentFilterForFileURL:documentFileURL];
if([filters objectForKey:filterType])
for(id fav in [filters objectForKey:filterType])
[contentFilters addObject:[[fav mutableCopy] autorelease]];
@@ -395,7 +393,7 @@ - (IBAction)closeContentFilterManagerSheet:(id)sender
#ifndef SP_CODA
// Update current document's content filters in the SPQueryController
[[SPQueryController sharedQueryController] replaceContentFilterByArray:
[self contentFilterForFileURL:delegatesFileURL] ofType:filterType forFileURL:delegatesFileURL];
[self contentFilterForFileURL:documentFileURL] ofType:filterType forFileURL:documentFileURL];

// Update global preferences' list
id cf = [[prefs objectForKey:SPContentFilters] mutableCopy];
@@ -968,6 +966,8 @@ - (void)savePanelDidEnd:(NSSavePanel *)panel returnCode:(NSInteger)returnCode co
- (void)dealloc
{
SPClear(contentFilters);
SPClear(filterType);
SPClear(documentFileURL);

[super dealloc];
}
@@ -4711,7 +4711,7 @@ - (NSDictionary *) stateIncludingDetails:(NSDictionary *)detailsToReturn
[sessionState setObject:[NSNumber numberWithInteger:[tableContentInstance pageNumber]] forKey:@"contentPageNumber"];
[sessionState setObject:NSStringFromRect([tableContentInstance viewport]) forKey:@"contentViewport"];
NSDictionary *filterSettings = [tableContentInstance filterSettings];
if (filterSettings) [sessionState setObject:filterSettings forKey:@"contentFilter"];
if (filterSettings) [sessionState setObject:filterSettings forKey:@"contentFilterV2"];

NSDictionary *contentSelectedRows = [tableContentInstance selectionDetailsAllowingIndexSelection:YES];
if (contentSelectedRows) {
@@ -5110,7 +5110,7 @@ - (void)restoreSession
if([spfSession objectForKey:@"contentSortCol"]) [tableContentInstance setSortColumnNameToRestore:[spfSession objectForKey:@"contentSortCol"] isAscending:[[spfSession objectForKey:@"contentSortColIsAsc"] boolValue]];
if([spfSession objectForKey:@"contentPageNumber"]) [tableContentInstance setPageToRestore:[[spfSession objectForKey:@"pageNumber"] integerValue]];
if([spfSession objectForKey:@"contentViewport"]) [tableContentInstance setViewportToRestore:NSRectFromString([spfSession objectForKey:@"contentViewport"])];
if([spfSession objectForKey:@"contentFilter"]) [tableContentInstance setFiltersToRestore:[spfSession objectForKey:@"contentFilter"]];
if([spfSession objectForKey:@"contentFilterV2"]) [tableContentInstance setFiltersToRestore:[spfSession objectForKey:@"contentFilterV2"]];

// Select table
[tablesListInstance selectTableAtIndex:[NSNumber numberWithInteger:[tables indexOfObject:[spfSession objectForKey:@"table"]]]];
@@ -264,7 +264,7 @@ - (void) updateHistoryEntries
nil];
if (contentSortCol) [contentState setObject:contentSortCol forKey:@"sortCol"];
if (contentSelectedRows) [contentState setObject:contentSelectedRows forKey:@"selection"];
if (contentFilter) [contentState setObject:contentFilter forKey:@"filter"];
if (contentFilter) [contentState setObject:contentFilter forKey:@"filterV2"];
if (filterTableData) [contentState setObject:filterTableData forKey:@"filterTable"];

// Update the table content states with this information - used when switching tables to restore last used view.
@@ -278,38 +278,45 @@ - (void) updateHistoryEntries
} else if (historyPosition != NSNotFound && historyPosition == [history count] - 1) {
NSMutableDictionary *currentHistoryEntry = [history objectAtIndex:historyPosition];

BOOL databaseIsTheSame = [[currentHistoryEntry objectForKey:@"database"] isEqualToString:theDatabase];
BOOL tableIsTheSame = [[currentHistoryEntry objectForKey:@"table"] isEqualToString:theTable];
BOOL viewIsTheSame = ([[currentHistoryEntry objectForKey:@"view"] unsignedIntegerValue] == theView);
// If the table is the same, and the filter settings haven't changed, delete the
// last entry so it can be replaced. This updates navigation within a table, rather than
// creating a new entry every time detail is changed.
if ([[currentHistoryEntry objectForKey:@"database"] isEqualToString:theDatabase]
&& [[currentHistoryEntry objectForKey:@"table"] isEqualToString:theTable]
&& ([[currentHistoryEntry objectForKey:@"view"] unsignedIntegerValue] != theView
|| ((![currentHistoryEntry objectForKey:@"contentFilter"] && !contentFilter)
|| (![currentHistoryEntry objectForKey:@"contentFilter"]
&& ![(NSString *)[contentFilter objectForKey:@"filterValue"] length]
&& ![[contentFilter objectForKey:@"filterComparison"] isEqualToString:@"IS NULL"]
&& ![[contentFilter objectForKey:@"filterComparison"] isEqualToString:@"IS NOT NULL"])
|| [[currentHistoryEntry objectForKey:@"contentFilter"] isEqualToDictionary:contentFilter])))
{
if (
databaseIsTheSame &&
tableIsTheSame &&
(
!viewIsTheSame ||
(
(![currentHistoryEntry objectForKey:@"contentFilterV2"] && !contentFilter) ||
[[currentHistoryEntry objectForKey:@"contentFilterV2"] isEqualToDictionary:contentFilter]
)
)
) {
[history removeLastObject];

}
// If the only db/table/view are the same, but the filter settings have changed, also store the
// position details on the *previous* history item
} else if ([[currentHistoryEntry objectForKey:@"database"] isEqualToString:theDatabase]
&& [[currentHistoryEntry objectForKey:@"table"] isEqualToString:theTable]
&& ([[currentHistoryEntry objectForKey:@"view"] unsignedIntegerValue] == theView
|| ((![currentHistoryEntry objectForKey:@"contentFilter"] && contentFilter)
|| ![[currentHistoryEntry objectForKey:@"contentFilter"] isEqualToDictionary:contentFilter])))
{
else if (
databaseIsTheSame &&
tableIsTheSame &&
(
viewIsTheSame ||
(
(![currentHistoryEntry objectForKey:@"contentFilterV2"] && contentFilter) ||
![[currentHistoryEntry objectForKey:@"contentFilterV2"] isEqualToDictionary:contentFilter]
)
)
) {
[currentHistoryEntry setObject:[NSValue valueWithRect:contentViewport] forKey:@"contentViewport"];
if (contentSelectedRows) [currentHistoryEntry setObject:contentSelectedRows forKey:@"contentSelection"];

}
// Special case: if the last history item is currently active, and has no table,
// but the new selection does - delete the last entry, in order to replace it.
// This improves history flow.
} else if ([[currentHistoryEntry objectForKey:@"database"] isEqualToString:theDatabase]
&& ![currentHistoryEntry objectForKey:@"table"])
{
else if (databaseIsTheSame && ![currentHistoryEntry objectForKey:@"table"]) {
[history removeLastObject];
}
}
@@ -325,7 +332,7 @@ - (void) updateHistoryEntries
nil];
if (contentSortCol) [newEntry setObject:contentSortCol forKey:@"contentSortCol"];
if (contentSelectedRows) [newEntry setObject:contentSelectedRows forKey:@"contentSelection"];
if (contentFilter) [newEntry setObject:contentFilter forKey:@"contentFilter"];
if (contentFilter) [newEntry setObject:contentFilter forKey:@"contentFilterV2"];

[history addObject:newEntry];

@@ -379,7 +386,7 @@ - (void) loadEntryTaskWithPosition:(NSNumber *)positionNumber
[tableContentInstance setPageToRestore:[[historyEntry objectForKey:@"contentPageNumber"] integerValue]];
[tableContentInstance setSelectionToRestore:[historyEntry objectForKey:@"contentSelection"]];
[tableContentInstance setViewportToRestore:[[historyEntry objectForKey:@"contentViewport"] rectValue]];
[tableContentInstance setFiltersToRestore:[historyEntry objectForKey:@"contentFilter"]];
[tableContentInstance setFiltersToRestore:[historyEntry objectForKey:@"contentFilterV2"]];

// If the database, table, and view are the same and content - just trigger a table reload (filters)
if (
@@ -495,7 +502,7 @@ - (void) restoreViewStates
[tableContentInstance setPageToRestore:[[contentState objectForKey:@"page"] unsignedIntegerValue]];
[tableContentInstance setSelectionToRestore:[contentState objectForKey:@"selection"]];
[tableContentInstance setViewportToRestore:[[contentState objectForKey:@"viewport"] rectValue]];
[tableContentInstance setFiltersToRestore:[contentState objectForKey:@"filter"]];
[tableContentInstance setFiltersToRestore:[contentState objectForKey:@"filterV2"]];
}

#pragma mark -
@@ -529,21 +536,14 @@ - (NSString *) nameForHistoryEntryDetails:(NSDictionary *)theEntry

[theName appendFormat:@"/%@", [theEntry objectForKey:@"table"]];

if ([theEntry objectForKey:@"contentFilter"]) {
NSDictionary *filterSettings = [theEntry objectForKey:@"contentFilter"];
if ([filterSettings objectForKey:@"filterField"]) {
if([filterSettings objectForKey:@"menuLabel"]) {
theName = [NSMutableString stringWithFormat:NSLocalizedString(@"%@ (Filtered by %@)", @"History item filtered by values label"),
theName, [filterSettings objectForKey:@"menuLabel"]];
}
}
if ([theEntry objectForKey:@"contentFilterV2"]) {
theName = [NSMutableString stringWithFormat:NSLocalizedString(@"%@ (Filtered)", @"History item filtered by values label"), theName];
}

if ([theEntry objectForKey:@"contentPageNumber"]) {
NSUInteger pageNumber = [[theEntry objectForKey:@"contentPageNumber"] unsignedIntegerValue];
if (pageNumber > 1) {
theName = [NSMutableString stringWithFormat:NSLocalizedString(@"%@ (Page %lu)", @"History item with page number label"),
theName, (unsigned long)pageNumber];
theName = [NSMutableString stringWithFormat:NSLocalizedString(@"%@ (Page %lu)", @"History item with page number label"), theName, (unsigned long)pageNumber];
}
}

@@ -43,13 +43,18 @@
@class SPDatabaseDocument;
@class SPTablesList;
@class SPTableStructure;
@class SPTableList;
@class SPContentFilterManager;
#ifndef SP_CODA
@class SPSplitView;
#endif
@class SPTableContentFilterController;

typedef NS_ENUM(NSInteger, SPTableContentFilterSource) {
SPTableContentFilterSourceNone = -1,
SPTableContentFilterSourceRuleFilter = 0,
SPTableContentFilterSourceTableFilter = 1,
SPTableContentFilterSourceURLScheme = 2,
};

#import "SPDatabaseContentViewDelegate.h"

@interface SPTableContent : NSObject <NSTableViewDelegate, NSTableViewDataSource, NSComboBoxDataSource, NSComboBoxDelegate, SPDatabaseContentViewDelegate>
@@ -65,10 +70,9 @@
#endif

IBOutlet SPCopyTable *tableContentView;
IBOutlet NSPopUpButton *fieldField;
IBOutlet id compareField;
IBOutlet id argumentField;
IBOutlet id filterButton;

IBOutlet NSButton *filterButton;
IBOutlet NSButton *toggleRuleFilterButton;
IBOutlet id addButton;
IBOutlet id duplicateButton;
IBOutlet id removeButton;
@@ -80,9 +84,6 @@
IBOutlet id limitRowsButton;
IBOutlet id limitRowsStepper;
#endif
IBOutlet id firstBetweenField;
IBOutlet id secondBetweenField;
IBOutlet id betweenTextField;

IBOutlet NSButton *paginationPreviousButton;
#ifndef SP_CODA
@@ -115,9 +116,6 @@
IBOutlet NSPanel *filterTableSetDefaultOperatorSheet;
IBOutlet NSComboBox* filterTableSetDefaultOperatorValue;

// Temporary to avoid nib conflicts during WIP
IBOutlet SPSplitView *contentSplitView;

IBOutlet SPTableContentFilterController *filterControllerInstance;
#endif
SPMySQLConnection *mySQLConnection;
@@ -133,7 +131,6 @@
SPDataStorage *tableValues;
NSMutableArray *dataColumns, *keys, *oldRow;
NSUInteger tableRowsCount, previousTableRowsCount;
NSString *compareType;
NSNumber *sortCol;
BOOL isEditingRow, isEditingNewRow, isSavingRow, isDesc, setLimit;
BOOL isFiltered, isLimited, isInterruptedLoad, maxNumRowsIsEstimate;
@@ -142,8 +139,6 @@

NSMutableDictionary *contentFilters;
NSMutableDictionary *numberOfDefaultFilters;
NSUInteger lastSelectedContentFilterIndex;
SPContentFilterManager *contentFilterManager;
NSUInteger contentPage;

#ifndef SP_CODA
@@ -153,7 +148,7 @@
BOOL filterTableIsSwapped;
NSString *filterTableDefaultOperator;
NSString *lastEditedFilterTableValue;
NSInteger activeFilter; // 0 = default filter; 1 = filter table; 2 = sequelpro url scheme
SPTableContentFilterSource activeFilter;
NSString *schemeFilter;
#endif

@@ -163,7 +158,6 @@
NSUInteger pageToRestore;
NSDictionary *selectionToRestore;
NSRect selectionViewportToRestore;
NSString *filterFieldToRestore, *filterComparisonToRestore, *filterValueToRestore, *firstBetweenValueToRestore, *secondBetweenValueToRestore;

#ifndef SP_CODA
NSInteger paginationViewHeight;
@@ -173,7 +167,6 @@
NSUInteger tableLoadInterfaceUpdateInterval, tableLoadTimerTicksSinceLastUpdate, tableLoadLastRowCount, tableLoadTargetRowCount;

NSArray *cqColumnDefinition;
NSString *fieldIDQueryString;
BOOL isFirstChangeInView;

NSString *kCellEditorErrorNoMatch;
@@ -186,17 +179,20 @@
NSColor *whiteColor;

SPFieldEditorController *fieldEditor;
NSRange fieldEditorSelectedRange;

// this represents the visible area of the whole content view at runtime.
// we use it as a positioning aide for the other two views below
IBOutlet NSView *contentAreaContainer;
IBOutlet NSView *filterRuleEditorContainer;
IBOutlet NSView *tableContentContainer;

BOOL showFilterRuleEditor;

NSDictionary *filtersToRestore;
}

#ifdef SP_CODA /* glue */
@property (assign) id filterButton;
@property (assign) id fieldField;
@property (assign) id compareField;
@property (assign) id betweenTextField;
@property (assign) id firstBetweenField;
@property (assign) id secondBetweenField;
@property (assign) id argumentField;
@property (assign) NSButton* addButton;
@property (assign) NSButton* duplicateButton;
@property (assign) NSButton* removeButton;
@@ -229,9 +225,10 @@
- (IBAction)reloadTable:(id)sender;
- (void)reloadTableTask;
- (IBAction)filterTable:(id)sender;
- (IBAction)toggleRuleEditorVisible:(id)sender;
- (void)filterTableTask;
- (IBAction)toggleFilterField:(id)sender;
- (void)setUsedQuery:(NSString *)query;
- (NSString *)selectedTable;

// Pagination
- (IBAction)navigatePaginationFromButton:(id)sender;
@@ -269,7 +266,6 @@
- (void)setConnection:(SPMySQLConnection *)theConnection;
- (void)clickLinkArrow:(SPTextAndLinkCell *)theArrowCell;
- (void)clickLinkArrowTask:(SPTextAndLinkCell *)theArrowCell;
- (IBAction)setCompareTypes:(id)sender;
- (void)updateResultStore:(SPMySQLStreamingResultStore *)theResultStore approximateRowCount:(NSUInteger)targetRowCount;
- (BOOL)saveRowToTable;
- (void) addRowErrorSheetDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
@@ -305,7 +301,6 @@
- (NSData *)filterTableData;

//- (NSString *)escapeFilterArgument:(NSString *)argument againstClause:(NSString *)clause;
- (void)openContentFilterManager;

- (NSArray *)fieldEditStatusForRow:(NSInteger)rowIndex andColumn:(NSInteger)columnIndex;

Oops, something went wrong.

0 comments on commit 66e8dc2

Please sign in to comment.
You can’t perform that action at this time.