diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-09-04 08:45:47 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-09-04 08:45:47 +0000 |
commit | 931a46891ac3f0610363105419b01eb7b40b034f (patch) | |
tree | 26c68003589c07e2bdd8ebad53111e778c54f13d /Source/SPQueryController.m | |
parent | a8bee6d7815cf9431cc53522f74bed648087a674 (diff) | |
download | sequelpro-931a46891ac3f0610363105419b01eb7b40b034f.tar.gz sequelpro-931a46891ac3f0610363105419b01eb7b40b034f.tar.bz2 sequelpro-931a46891ac3f0610363105419b01eb7b40b034f.zip |
• implementation of a new history controller
- each history is doc-based even if you have opened not only one instance of the same spf file
- for each SPF file identified by its file URL SP remembers each history item regardless from which doc instance it comes from internally ( to make sure that after closing the last instance of a doc all executed queries are saved in that file - if the user wants to change that s/he has to save that file under a different name )
- the history list for each Untitled doc will be initialized by the items stored in the global SP's prefs
- the history list for each SPF doc will be initialized by the items stored in the SPF file unless an instance of the same file is already open - then the new instance inherits the history list from the opened one(s)
- all history items executed in any Untitled docs will be added to SP's prefs - in other words SP's global prefs plist is the historyrepository for each new Untitled doc
- if the user saves an untitled doc or rename an opened SPF file the _current_ history list for that doc will be saved to the chosen file unless an other doc instance is still open
- all history lists are saved automatically if SP quits or the user closes a doc window
Note: This should be tested for any logical pitfalls - each desired logic can be implemented :)
Diffstat (limited to 'Source/SPQueryController.m')
-rw-r--r-- | Source/SPQueryController.m | 243 |
1 files changed, 166 insertions, 77 deletions
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 |