From de9bca67824b192ede852ae74eab175dacb2f686 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 5 Jan 2010 11:46:41 +0000 Subject: =?UTF-8?q?=E2=80=A2=20added=20Save/Copy=20History=20List=20to=20"?= =?UTF-8?q?Query=20History"'s=20popup=20menu=20=E2=80=A2=20moved=20"Clear?= =?UTF-8?q?=20History"=20from=20gear=20menu=20to=20"Query=20History"'s=20p?= =?UTF-8?q?opup=20menu=20and=20added=20a=20confirmation=20sheet=20to=20it?= =?UTF-8?q?=20=E2=80=A2=20"Clear=20History"=20now=20distinguishes=20betwee?= =?UTF-8?q?n=20Untitled=20and=20SPF=20doc=20-=20if=20Untitled:=20clear=20g?= =?UTF-8?q?lobal=20history=20list=20stored=20in=20the=20Prefs=20-=20if=20S?= =?UTF-8?q?PF=20doc:=20clear=20the=20document-based=20history=20list=20onl?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CustomQuery.h | 16 +++++- Source/CustomQuery.m | 153 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 150 insertions(+), 19 deletions(-) (limited to 'Source') diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index 121b4853..960859d0 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -45,6 +45,9 @@ #define SP_SAVE_ALL_FAVORTITE_MENUITEM_TAG 100001 #define SP_SAVE_SELECTION_FAVORTITE_MENUITEM_TAG 100000 #define SP_FAVORITE_HEADER_MENUITEM_TAG 200000 +#define SP_HISTORY_COPY_MENUITEM_TAG 300000 +#define SP_HISTORY_SAVE_MENUITEM_TAG 300001 +#define SP_HISTORY_CLEAR_MENUITEM_TAG 300002 @class SPQueryFavoriteManager; @@ -69,7 +72,11 @@ IBOutlet NSMenuItem *queryHistorySearchMenuItem; IBOutlet id queryHistorySearchFieldView; IBOutlet NSSearchField *queryHistorySearchField; - + IBOutlet NSMenuItem *clearHistoryMenuItem; + IBOutlet NSMenuItem *saveHistoryMenuItem; + IBOutlet NSMenuItem *copyHistoryMenuItem; + IBOutlet NSPopUpButton *encodingPopUp; + IBOutlet CMTextView *textView; IBOutlet CMCopyTable *customQueryView; IBOutlet NSScrollView *customQueryScrollView; @@ -83,7 +90,6 @@ IBOutlet NSMenuItem *runSelectionMenuItem; IBOutlet NSMenuItem *runAllMenuItem; - IBOutlet NSMenuItem *clearHistoryMenuItem; IBOutlet NSMenuItem *shiftLeftMenuItem; IBOutlet NSMenuItem *shiftRightMenuItem; IBOutlet NSMenuItem *completionListMenuItem; @@ -161,6 +167,9 @@ - (IBAction)helpSelectHelpTargetWeb:(id)sender; - (IBAction)filterQueryFavorites:(id)sender; - (IBAction)filterQueryHistory:(id)sender; +- (IBAction)saveQueryHistory:(id)sender; +- (IBAction)copyQueryHistory:(id)sender; +- (IBAction)clearQueryHistory:(id)sender; // Query actions - (void)performQueries:(NSArray *)queries withCallback:(SEL)customQueryCallbackMethod; @@ -195,6 +204,7 @@ - (void)commentOutCurrentQueryTakingSelection:(BOOL)takeSelection; - (NSString *)usedQuery; - (NSString *)argumentForRow:(NSUInteger)rowIndex ofTable:(NSString *)tableForColumn andDatabase:(NSString *)database; -- (unsigned int)numberOfQueries; +- (NSUInteger)numberOfQueries; +- (NSString *)buildHistoryString; @end diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 4772823e..358361b6 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -40,6 +40,7 @@ #import "SPQueryFavoriteManager.h" #import "SPQueryController.h" #import "SPConstants.h" +#import "SPEncodingPopupAccessory.h" @implementation CustomQuery @@ -225,7 +226,7 @@ [prefs synchronize]; // Choose history item - if ([queryHistoryButton indexOfSelectedItem] > 1) { + if ([queryHistoryButton indexOfSelectedItem] > 6) { BOOL replaceContent = [prefs boolForKey:SPQueryHistoryReplacesContent]; @@ -253,17 +254,6 @@ */ - (IBAction)gearMenuItemSelected:(id)sender { - // "Clear History" menu item - clear query history - if (sender == clearHistoryMenuItem) { - - // Remove all history buttons except the search field and separator beginning from the end - while([queryHistoryButton numberOfItems] > 3) - [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; - - // 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. if (sender == shiftRightMenuItem) { @@ -338,6 +328,67 @@ } } +- (IBAction)saveQueryHistory:(id)sender +{ + NSSavePanel *panel = [NSSavePanel savePanel]; + + [panel setRequiredFileType:SPFileExtensionSQL]; + + [panel setExtensionHidden:NO]; + [panel setAllowsOtherFileTypes:YES]; + [panel setCanSelectHiddenExtension:YES]; + [panel setCanCreateDirectories:YES]; + + [panel setAccessoryView:[SPEncodingPopupAccessory encodingAccessory:[prefs integerForKey:SPLastSQLFileEncoding] includeDefaultEntry:NO encodingPopUp:&encodingPopUp]]; + + [encodingPopUp setEnabled:YES]; + + [panel beginSheetForDirectory:nil file:@"history" modalForWindow:tableWindow modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:@"saveHistory"]; +} + +- (IBAction)copyQueryHistory:(id)sender +{ + + NSPasteboard *pb = [NSPasteboard generalPasteboard]; + + [pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; + [pb setString:[self buildHistoryString] forType:NSStringPboardType]; + +} + +// "Clear History" menu item - clear query history +- (IBAction)clearQueryHistory:(id)sender +{ + + NSString *infoString; + + if ([tableDocumentInstance isUntitled]) + infoString = NSLocalizedString(@"Are you sure you want to clear the global history list? This action cannot be undone.", @"clear global history list informative message"); + else + infoString = [NSString stringWithFormat:NSLocalizedString(@"Are you sure you want to clear the history list for “%@”? This action cannot be undone.", @"clear history list for “%@” informative message"), [tableDocumentInstance displayName]]; + + NSAlert *alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Clear History?", @"clear history message") + defaultButton:NSLocalizedString(@"Clear", @"clear button") + alternateButton:NSLocalizedString(@"Cancel", @"cancel button") + otherButton:nil + informativeTextWithFormat:infoString]; + + [alert setAlertStyle:NSCriticalAlertStyle]; + + NSArray *buttons = [alert buttons]; + + // Change the alert's cancel button to have the key equivalent of return + [[buttons objectAtIndex:0] setKeyEquivalent:@"r"]; + [[buttons objectAtIndex:0] setKeyEquivalentModifierMask:NSCommandKeyMask]; + [[buttons objectAtIndex:1] setKeyEquivalent:@"\r"]; + + [alert beginSheetModalForWindow:tableWindow + modalDelegate:self + didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) + contextInfo:@"clearHistory"]; + +} + /* * Set font panel's valid modes */ @@ -618,11 +669,14 @@ [[SPQueryController sharedQueryController] addHistory:usedQuery forFileURL:[tableDocumentInstance fileURL]]; // Add it to the document's current popup list - [queryHistoryButton insertItemWithTitle:usedQuery atIndex:3]; + if([queryHistoryButton numberOfItems] > 8) + [queryHistoryButton insertItemWithTitle:usedQuery atIndex:7]; + else + [queryHistoryButton addItemWithTitle:usedQuery]; // Check for max history NSUInteger maxHistoryItems = [[prefs objectForKey:SPCustomQueryMaxHistoryItems] intValue]; - while ( [queryHistoryButton numberOfItems] > maxHistoryItems + 3 ) + while ( [queryHistoryButton numberOfItems] > maxHistoryItems + 6 ) [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; } @@ -2719,6 +2773,19 @@ return numberOfQueries; } +- (NSString *)buildHistoryString +{ + NSMutableString *history = [NSMutableString string]; + NSMenu *menu = [queryHistoryButton menu]; + NSInteger i; + + for (i = 7; i < [menu numberOfItems]; i++) { + [history appendString:[[menu itemAtIndex:i] title]]; + [history appendString:@";\n"]; + } + + return history; +} /* * This method is called as part of Key Value Observing which is used to watch for prefernce changes which effect the interface. */ @@ -2743,10 +2810,29 @@ } /** - * Called when the save query favorite sheet is dismissed. + * Called when the save query favorite/clear history sheet is dismissed. */ - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo { + + if ([contextInfo isEqualToString:@"clearHistory"]) { + if (returnCode == NSOKButton) { + + // Remove all history buttons up to the search field and separator beginning from the end + while([queryHistoryButton numberOfItems] > 7) + [queryHistoryButton removeItemAtIndex:[queryHistoryButton numberOfItems]-1]; + + // Clear the global history list if doc is Untitled + if ([tableDocumentInstance isUntitled]) + [prefs setObject:[NSArray array] forKey:SPQueryHistory]; + // otherwise remove all document-based history items from the queryController + else + [[SPQueryController sharedQueryController] replaceHistoryByArray:[NSMutableArray array] forFileURL:[tableDocumentInstance fileURL]]; + + } + return; + } + if ([contextInfo isEqualToString:@"addAllToNewQueryFavorite"] || [contextInfo isEqualToString:@"addSelectionToNewQueryFavorite"]) { if (returnCode == NSOKButton) { @@ -2793,6 +2879,25 @@ [queryFavoriteNameTextField setStringValue:@""]; } +- (void)savePanelDidEnd:(NSSavePanel *)panel returnCode:(NSInteger)returnCode contextInfo:(id)contextInfo +{ + if([contextInfo isEqualToString:@"saveHistory"]) { + if (returnCode == NSOKButton) { + NSError *error = nil; + + [prefs setInteger:[[encodingPopUp selectedItem] tag] forKey:SPLastSQLFileEncoding]; + [prefs synchronize]; + + [[self buildHistoryString] writeToFile:[panel filename] + atomically:YES + encoding:[[encodingPopUp selectedItem] tag] + error:&error]; + + if (error) [[NSAlert alertWithError:error] runModal]; + } + } +} + /** * Menu item validation. */ @@ -2819,6 +2924,22 @@ return NO; } + // Control Clear History menu item title according to isUntitled + if ( [menuItem tag] == SP_HISTORY_CLEAR_MENUITEM_TAG ) { + if ( [tableDocumentInstance isUntitled] ) { + [menuItem setTitle:NSLocalizedString(@"Clear global History", @"clear global history menu item title")]; + [menuItem setToolTip:NSLocalizedString(@"Clear the global history list", @"clear the global history list tooltip message")]; + } else { + [menuItem setTitle:[NSString stringWithFormat:NSLocalizedString(@"Clear History for “%@”", @"clear history for “%@” menu title"), [tableDocumentInstance displayName]]]; + [menuItem setToolTip:NSLocalizedString(@"Clear the document-based history list", @"clear the document-based history list tooltip message")]; + } + } + + // Check for History items + if ( [menuItem tag] >= SP_HISTORY_COPY_MENUITEM_TAG && [menuItem tag] <= SP_HISTORY_CLEAR_MENUITEM_TAG ) { + return ([queryHistoryButton numberOfItems]-7); + } + return YES; } @@ -2890,7 +3011,7 @@ NSMenu *menu = [queryHistoryButton menu]; NSString *searchPattern = [queryHistorySearchField stringValue]; - for (i = 3; i < [menu numberOfItems]; i++) + for (i = 7; i < [menu numberOfItems]; i++) { [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", searchPattern]])]; } -- cgit v1.2.3