aboutsummaryrefslogtreecommitdiffstats
path: root/Source/SPQueryController.m
diff options
context:
space:
mode:
Diffstat (limited to 'Source/SPQueryController.m')
-rw-r--r--Source/SPQueryController.m243
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