diff options
-rw-r--r-- | Source/CustomQuery.h | 5 | ||||
-rw-r--r-- | Source/CustomQuery.m | 50 | ||||
-rw-r--r-- | Source/SPQueryController.h | 25 | ||||
-rw-r--r-- | Source/SPQueryController.m | 243 | ||||
-rw-r--r-- | Source/TableDocument.m | 35 |
5 files changed, 219 insertions, 139 deletions
diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index e6c9ad43..cedccab0 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -114,9 +114,6 @@ unsigned int numberOfQueries; - NSMutableArray *localFavorites; - NSMutableArray *localHistoryItems; - } // IBAction methods @@ -147,8 +144,6 @@ // Accessors - (NSArray *)currentResult; - (void)processResultIntoDataStorage:(MCPStreamingResult *)theResult; -- (NSArray *)localFavorites; -- (NSArray *)localHistoryItems; // MySQL Help - (NSString *)getHTMLformattedMySQLHelpFor:(NSString *)aString; diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 8b8a8968..d679a1de 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -38,6 +38,7 @@ #import "SPTextAndLinkCell.h" #import "SPTooltip.h" #import "SPQueryFavoriteManager.h" +#import "SPQueryController.h" #define SP_MYSQL_DEV_SEARCH_URL @"http://search.mysql.com/search?q=%@&site=refman-%@" #define SP_HELP_SEARCH_IN_MYSQL 0 @@ -238,13 +239,14 @@ { // "Clear History" menu item - clear query history if (sender == clearHistoryMenuItem) { - // Remove all history buttons beginning from the end + + // Remove all history buttons except the search field beginning from the end while([queryHistoryButton numberOfItems] > 2) [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; - // [queryHistoryButton addItemWithTitle:NSLocalizedString(@"Query History…",@"Title of query history popup button")]; - [prefs setObject:[NSArray array] forKey:@"queryHistory"]; - [prefs synchronize]; + // Remove all items from the queryController + [[SPQueryController sharedQueryController] replaceHistoryByArray:[NSMutableArray array] forFileURL:[tableDocumentInstance fileURL]]; + } // "Shift Right" menu item - indent the selection with an additional tab. @@ -340,7 +342,6 @@ NSArray *theColumns; NSTableColumn *theCol; MCPStreamingResult *streamingResult = nil; - NSMutableArray *menuItems = [NSMutableArray array]; NSMutableString *errors = [NSMutableString string]; int i, totalQueriesRun = 0, totalAffectedRows = 0; @@ -491,17 +492,17 @@ // if(!queriesSeparatedByDelimiter) { // TODO only add to history if no “delimiter” command was used if(!tableReloadAfterEditing && [usedQuery length]) { - [queryHistoryButton insertItemWithTitle:usedQuery atIndex:2]; + // Register new history item + [[SPQueryController sharedQueryController] addHistory:usedQuery forFileURL:[tableDocumentInstance fileURL]]; - int maxHistoryItems = [[prefs objectForKey:@"CustomQueryMaxHistoryItems"] intValue]; + // Add it to the document's current popup list + [queryHistoryButton insertItemWithTitle:usedQuery atIndex:2]; + // Check for max history + NSUInteger maxHistoryItems = [[prefs objectForKey:@"CustomQueryMaxHistoryItems"] intValue]; while ( [queryHistoryButton numberOfItems] > maxHistoryItems + 2 ) [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; - for ( i = 2 ; i < [queryHistoryButton numberOfItems] ; i++ ) - [menuItems addObject:[queryHistoryButton itemTitleAtIndex:i]]; - - [prefs setObject:menuItems forKey:@"queryHistory"]; } // Error checking @@ -1057,23 +1058,6 @@ return currentResult; } -/* - * Return the document-based favourites - */ -- (NSArray *)localFavorites -{ - return localFavorites; -} - -/* - * Return the document-based history items - */ -- (NSArray *)localHistoryItems -{ - return localHistoryItems; -} - - #pragma mark - #pragma mark Additional methods @@ -1113,9 +1097,9 @@ [autouppercaseKeywordsMenuItem setState:([prefs boolForKey:@"CustomQueryAutoUppercaseKeywords"]?NSOnState:NSOffState)]; [textView setAutouppercaseKeywords:[prefs boolForKey:@"CustomQueryAutoUppercaseKeywords"]]; - if ( [prefs objectForKey:@"queryHistory"] ) + if ( [[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]] ) { - [queryHistoryButton addItemsWithTitles:[prefs objectForKey:@"queryHistory"]]; + [queryHistoryButton addItemsWithTitles:[[SPQueryController sharedQueryController] historyForFileURL:[tableDocumentInstance fileURL]]]; } // Disable runSelectionMenuItem in the gear menu @@ -2575,10 +2559,6 @@ // init tableView's data source fullResult = [[NSMutableArray alloc] init]; - // init local favorite and history item array - localHistoryItems = [[NSMutableArray alloc] init]; - localFavorites = [[NSMutableArray alloc] init]; - prefs = [NSUserDefaults standardUserDefaults]; } @@ -2627,8 +2607,6 @@ [usedQuery release]; [fullResult release]; [favoritesManager release]; - if (localFavorites) [localFavorites release]; - if (localHistoryItems) [localHistoryItems release]; if (sortField) [sortField release]; [super dealloc]; diff --git a/Source/SPQueryController.h b/Source/SPQueryController.h index a06c2fd8..bce03109 100644 --- a/Source/SPQueryController.h +++ b/Source/SPQueryController.h @@ -27,6 +27,8 @@ @interface SPQueryController : NSWindowController { + + // QueryConsoleController IBOutlet NSView *saveLogView; IBOutlet NSTableView *consoleTableView; IBOutlet NSSearchField *consoleSearchField; @@ -43,9 +45,13 @@ NSMutableString *activeFilterString; + // DocumentsController NSUInteger untitledDocumentCounter; NSMutableDictionary *favoritesContainer; NSMutableDictionary *historyContainer; + NSUInteger numberOfMaxAllowedHistory; + + NSUserDefaults *prefs; } @@ -53,6 +59,9 @@ + (SPQueryController *)sharedQueryController; +#pragma mark ---------------------- + +// QueryConsoleController - (IBAction)copy:(id)sender; - (IBAction)clearConsole:(id)sender; - (IBAction)saveConsoleAs:(id)sender; @@ -65,14 +74,18 @@ - (void)showMessageInConsole:(NSString *)message; - (void)showErrorInConsole:(NSString *)error; +- (NSUInteger)consoleMessageCount; + +#pragma mark ---------------------- + +// DocumentsController - (NSURL *)registerDocumentWithFileURL:(NSURL *)fileURL andContextInfo:(NSMutableDictionary *)contextInfo; - (void)removeRegisteredDocumentWithFileURL:(NSURL *)fileURL; -- (void)addFavorite:(NSString *)favorite forFileURL:(NSURL *)fileURL; +- (void)addFavorite:(NSDictionary *)favorite forFileURL:(NSURL *)fileURL; +- (void)replaceFavoritesByArray:(NSArray *)favoritesArray forFileURL:(NSURL *)fileURL; - (void)addHistory:(NSString *)history forFileURL:(NSURL *)fileURL; -- (void)favoritesForFileURL:(NSURL *)fileURL; -- (void)historyForFileURL:(NSURL *)fileURL; - - -- (NSUInteger)consoleMessageCount; +- (void)replaceHistoryByArray:(NSArray *)historyArray forFileURL:(NSURL *)fileURL; +- (NSMutableArray *)favoritesForFileURL:(NSURL *)fileURL; +- (NSMutableArray *)historyForFileURL:(NSURL *)fileURL; @end diff --git a/Source/SPQueryController.m b/Source/SPQueryController.m index 94bc7b12..807f5444 100644 --- a/Source/SPQueryController.m +++ b/Source/SPQueryController.m @@ -97,9 +97,11 @@ static SPQueryController *sharedQueryController = nil; messagesVisibleSet = messagesFullSet; untitledDocumentCounter = 1; + numberOfMaxAllowedHistory = 100; favoritesContainer = [[NSMutableDictionary alloc] init]; historyContainer = [[NSMutableDictionary alloc] init]; + } return self; @@ -124,7 +126,7 @@ static SPQueryController *sharedQueryController = nil; */ - (void)awakeFromNib { - NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; + prefs = [NSUserDefaults standardUserDefaults]; [self setWindowFrameAutosaveName:CONSOLE_WINDOW_AUTO_SAVE_NAME]; [[consoleTableView tableColumnWithIdentifier:TABLEVIEW_DATE_COLUMN_IDENTIFIER] setHidden:![prefs boolForKey:@"ConsoleShowTimestamps"]]; @@ -137,6 +139,27 @@ static SPQueryController *sharedQueryController = nil; } /** + * Standard dealloc. + */ +- (void)dealloc +{ + messagesVisibleSet = nil; + + [messagesFullSet release], messagesFullSet = nil; + [messagesFilteredSet release], messagesFilteredSet = nil; + [activeFilterString release], activeFilterString = nil; + + [favoritesContainer release]; + [historyContainer release]; + + [super dealloc]; +} + +#pragma mark ---------------------- +#pragma mark QueryConsoleController + + +/** * Copy implementation for console table view. */ - (void)copy:(id)sender @@ -325,7 +348,65 @@ static SPQueryController *sharedQueryController = nil; return [[[NSAttributedString alloc] initWithString:returnValue attributes:stringAtributes] autorelease]; } + #pragma mark - +#pragma mark Other + +/** + * Called whenver the test within the search field changes. + */ +- (void)controlTextDidChange:(NSNotification *)notification +{ + id object = [notification object]; + + if ([object isEqualTo:consoleSearchField]) { + + // Store the state of the text filter and the current filter string for later quick reference + [activeFilterString setString:[[object stringValue] lowercaseString]]; + filterIsActive = [activeFilterString length]?YES:NO; + + [self _updateFilterState]; + } +} + +/** + * This method is called as part of Key Value Observing which is used to watch for prefernce changes which effect the interface. + */ +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if ([keyPath isEqualToString:@"ConsoleEnableLogging"]) { + [loggingDisabledTextField setStringValue:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? @"" : @"Query logging is currently disabled"]; + } +} + +/** + * Menu item validation for console table view contextual menu. + */ +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + if ([menuItem action] == @selector(copy:)) { + return ([consoleTableView numberOfSelectedRows] > 0); + } + + if ([menuItem action] == @selector(clearConsole:)) { + return ([self consoleMessageCount] > 0); + } + + return [[self window] validateMenuItem:menuItem]; +} + +- (void)updateEntries +{ + [consoleTableView reloadData]; + [consoleTableView scrollRowToVisible:([messagesVisibleSet count] - 1)]; +} + +- (NSString *)windowFrameAutosaveName +{ + return @"QueryConsole"; +} + +#pragma mark ---------------------- #pragma mark DocumentsController - (NSURL *)registerDocumentWithFileURL:(NSURL *)fileURL andContextInfo:(NSMutableDictionary *)contextInfo @@ -336,16 +417,32 @@ static SPQueryController *sharedQueryController = nil; NSURL *new = [NSURL URLWithString:[[NSString stringWithFormat:@"Untitled %d", untitledDocumentCounter] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; untitledDocumentCounter++; - if(![favoritesContainer objectForKey:[new absoluteString]]) - [favoritesContainer setObject:[NSMutableArray array] forKey:[new absoluteString]]; - if(![historyContainer objectForKey:[new absoluteString]]) - [historyContainer setObject:[NSMutableArray array] forKey:[new absoluteString]]; - + if(![favoritesContainer objectForKey:[new absoluteString]]) { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [favoritesContainer setObject:arr forKey:[new absoluteString]]; + [arr release]; + } + + // Set the global history coming from the Prefs as default if available + if(![historyContainer objectForKey:[new absoluteString]]) { + if([prefs objectForKey:@"queryHistory"]) { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [arr addObjectsFromArray:[prefs objectForKey:@"queryHistory"]]; + [historyContainer setObject:arr forKey:[new absoluteString]]; + [arr release]; + } else { + NSMutableArray *arr = [[NSMutableArray alloc] init]; + [historyContainer setObject:[NSMutableArray array] forKey:[new absoluteString]]; + [arr release]; + } + } + return new; + } // Register a spf file to manage all query favorites and query history items - // file path based in a dictionary whereby the key represents the file name. + // file path based (incl. Untitled docs) in a dictionary whereby the key represents the file URL as string. if(![favoritesContainer objectForKey:[fileURL absoluteString]]) { if(contextInfo != nil && [contextInfo objectForKey:@"queryFavorites"] && [[contextInfo objectForKey:@"queryFavorites"] count]) { NSMutableArray *arr = [[NSMutableArray alloc] init]; @@ -377,106 +474,98 @@ static SPQueryController *sharedQueryController = nil; - (void)removeRegisteredDocumentWithFileURL:(NSURL *)fileURL { - + + // Check for multiple instance of the same document. + // Remove it if only one instance was registerd. + NSArray *allDocs = [[NSDocumentController sharedDocumentController] documents]; + NSMutableArray *allURLs = [NSMutableArray array]; + for(id doc in allDocs) { + if([allURLs containsObject:[doc fileURL]]) + return; + else + [allURLs addObject:[doc fileURL]]; + } + if([favoritesContainer objectForKey:[fileURL absoluteString]]) [favoritesContainer removeObjectForKey:[fileURL absoluteString]]; if([historyContainer objectForKey:[fileURL absoluteString]]) [historyContainer removeObjectForKey:[fileURL absoluteString]]; - + } -- (void)addFavorite:(NSString *)favorite forFileURL:(NSURL *)fileURL +- (void)addFavorite:(NSDictionary *)favorite forFileURL:(NSURL *)fileURL { } -- (void)addHistory:(NSString *)history forFileURL:(NSURL *)fileURL +- (void)replaceFavoritesByArray:(NSArray *)favoritesArray forFileURL:(NSURL *)fileURL { - + if([favoritesContainer objectForKey:[fileURL absoluteString]]) + [favoritesContainer setObject:favoritesArray forKey:[fileURL absoluteString]]; } -- (void)favoritesForFileURL:(NSURL *)fileURL +- (void)replaceHistoryByArray:(NSArray *)historyArray forFileURL:(NSURL *)fileURL { - + if([historyContainer objectForKey:[fileURL absoluteString]]) + [historyContainer setObject:historyArray forKey:[fileURL absoluteString]]; } -- (void)historyForFileURL:(NSURL *)fileURL +- (void)addHistory:(NSString *)history forFileURL:(NSURL *)fileURL { - -} -#pragma mark - -#pragma mark Other + NSUInteger maxHistoryItems = [[prefs objectForKey:@"CustomQueryMaxHistoryItems"] intValue]; -/** - * Called whenver the test within the search field changes. - */ -- (void)controlTextDidChange:(NSNotification *)notification -{ - id object = [notification object]; - - if ([object isEqualTo:consoleSearchField]) { - - // Store the state of the text filter and the current filter string for later quick reference - [activeFilterString setString:[[object stringValue] lowercaseString]]; - filterIsActive = [activeFilterString length]?YES:NO; - - [self _updateFilterState]; - } -} + // Save each history item due to its document source + if([historyContainer objectForKey:[fileURL absoluteString]]) { -/** - * This method is called as part of Key Value Observing which is used to watch for prefernce changes which effect the interface. - */ -- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context -{ - if ([keyPath isEqualToString:@"ConsoleEnableLogging"]) { - [loggingDisabledTextField setStringValue:([[change objectForKey:NSKeyValueChangeNewKey] boolValue]) ? @"" : @"Query logging is currently disabled"]; - } -} + // Remove all duplicates by using a NSPopUpButton + NSPopUpButton *uniquifier = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0,0,0,0) pullsDown:YES]; + [uniquifier addItemsWithTitles:[historyContainer objectForKey:[fileURL absoluteString]]]; + [uniquifier insertItemWithTitle:history atIndex:0]; + + while ( [uniquifier numberOfItems] > maxHistoryItems ) + [uniquifier removeItemAtIndex:[uniquifier numberOfItems]-1]; + + [self replaceHistoryByArray:[uniquifier itemTitles] forFileURL:fileURL]; + [uniquifier release]; -/** - * Menu item validation for console table view contextual menu. - */ -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem -{ - if ([menuItem action] == @selector(copy:)) { - return ([consoleTableView numberOfSelectedRows] > 0); } - - if ([menuItem action] == @selector(clearConsole:)) { - return ([self consoleMessageCount] > 0); + + // Save history items coming from each Untitled document in the global Preferences successively + // regardingless of the source document. + if(![[fileURL absoluteString] hasPrefix:@"/"]) { + + // Remove all duplicates by using a NSPopUpButton + NSPopUpButton *uniquifier = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0,0,0,0) pullsDown:YES]; + [uniquifier addItemsWithTitles:[prefs objectForKey:@"queryHistory"]]; + [uniquifier insertItemWithTitle:history atIndex:0]; + + while ( [uniquifier numberOfItems] > maxHistoryItems ) + [uniquifier removeItemAtIndex:[uniquifier numberOfItems]-1]; + + [prefs setObject:[uniquifier itemTitles] forKey:@"queryHistory"]; + [uniquifier release]; + } - - return [[self window] validateMenuItem:menuItem]; -} -- (void)updateEntries -{ - [consoleTableView reloadData]; - [consoleTableView scrollRowToVisible:([messagesVisibleSet count] - 1)]; } -- (NSString *)windowFrameAutosaveName +- (NSMutableArray *)favoritesForFileURL:(NSURL *)fileURL { - return @"QueryConsole"; + if([favoritesContainer objectForKey:[fileURL absoluteString]]) + return [favoritesContainer objectForKey:[fileURL absoluteString]]; + + return [NSMutableArray array]; + } -/** - * Standard dealloc. - */ -- (void)dealloc +- (NSMutableArray *)historyForFileURL:(NSURL *)fileURL { - messagesVisibleSet = nil; - - [messagesFullSet release], messagesFullSet = nil; - [messagesFilteredSet release], messagesFilteredSet = nil; - [activeFilterString release], activeFilterString = nil; - - [favoritesContainer release]; - [historyContainer release]; - - [super dealloc]; + if([historyContainer objectForKey:[fileURL absoluteString]]) + return [historyContainer objectForKey:[fileURL absoluteString]]; + + return [NSMutableArray array]; + } @end diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 35df953c..9c56ff04 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -507,7 +507,12 @@ _isConnected = YES; mySQLConnection = [theConnection retain]; - + + // Set the fileURL and init the preferences (query favs and history) if available for that URL + [self setFileURL:[[SPQueryController sharedQueryController] registerDocumentWithFileURL:[self fileURL] andContextInfo:[spfPreferences retain]]]; + + [spfPreferences release]; + // Set the connection encoding NSString *encodingName = [prefs objectForKey:@"DefaultEncoding"]; if ( [encodingName isEqualToString:@"Autodetect"] ) { @@ -597,11 +602,6 @@ else [tableWindow makeFirstResponder:[tablesListInstance valueForKeyPath:@"tablesListView"]]; - NSURL *anURL = [[SPQueryController sharedQueryController] registerDocumentWithFileURL:[self fileURL] andContextInfo:[spfPreferences retain]]; - [self setFileURL:anURL]; - - [spfPreferences release]; - if(spfSession != nil) [self restoreSession]; @@ -2163,8 +2163,8 @@ } // Update the keys - [spf setObject:[customQueryInstance localFavorites] forKey:@"queryFavorites"]; - [spf setObject:[customQueryInstance localHistoryItems] forKey:@"queryHistory"]; + [spf setObject:[[SPQueryController sharedQueryController] favoritesForFileURL:[self fileURL]] forKey:@"queryFavorites"]; + [spf setObject:[[SPQueryController sharedQueryController] historyForFileURL:[self fileURL]] forKey:@"queryHistory"]; // Save it again NSString *err = nil; @@ -2212,9 +2212,9 @@ [spfdata setObject:@"mysql" forKey:@"rdbms_type"]; [spfdata setObject:[self mySQLVersion] forKey:@"rdbms_version"]; - // Store the preferences - [spfdata setObject:[customQueryInstance localFavorites] forKey:@"queryFavorites"]; - [spfdata setObject:[customQueryInstance localHistoryItems] forKey:@"queryHistory"]; + // Store the preferences - take them from the current document URL to catch renaming + [spfdata setObject:[[SPQueryController sharedQueryController] favoritesForFileURL:[self fileURL]] forKey:@"queryFavorites"]; + [spfdata setObject:[[SPQueryController sharedQueryController] historyForFileURL:[self fileURL]] forKey:@"queryHistory"]; [spfdata setObject:[spfDocData_temp objectForKey:@"encrypted"] forKey:@"encrypted"]; @@ -2360,17 +2360,22 @@ return NO; } - // TODO take favs and history frm untitle or old file name with me - [[SPQueryController sharedQueryController] registerDocumentWithFileURL:[NSURL URLWithString:[fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] andContextInfo:nil]; + // Register and update query favorites and history for the (new) file URL + NSMutableDictionary *preferences = [[NSMutableDictionary alloc] init]; + [preferences setObject:[spfdata objectForKey:@"queryHistory"] forKey:@"queryHistory"]; + [preferences setObject:[spfdata objectForKey:@"queryFavorites"] forKey:@"queryFavorites"]; + [[SPQueryController sharedQueryController] registerDocumentWithFileURL:[NSURL URLWithString:[fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] andContextInfo:preferences]; [self setFileURL:[NSURL URLWithString:[fileName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; [tableWindow setTitle:[self displayName]]; - + // Store doc data permanently [spfDocData removeAllObjects]; [spfDocData addEntriesFromDictionary:spfDocData_temp]; - + + [preferences release]; + return YES; } |