From 9683c9df3cb0b1128429f42f108d4e03ca4550f1 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Fri, 4 Sep 2009 17:40:56 +0000 Subject: =?UTF-8?q?=E2=80=A2=20improved=20behavior=20while=20closing=20a?= =?UTF-8?q?=20document=20window=20-=20if=20not=20connected=20close=20it=20?= =?UTF-8?q?without=20any=20checks=20-=20if=20an=20Untitled=20document=20co?= =?UTF-8?q?ntains=20stored=20query=20favorites=20-=20What=20should=20be=20?= =?UTF-8?q?done=3F=20-=20up=20to=20now=20it=20saves=20them=20to=20the=20gl?= =?UTF-8?q?obal=20prefs=20=E2=80=A2=20header=20in=20query=20favorites=20li?= =?UTF-8?q?st=20keep=20visible=20while=20filtering=20=E2=80=A2=20code=20cl?= =?UTF-8?q?eaning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CustomQuery.h | 26 +++++++++++++++++++----- Source/CustomQuery.m | 55 ++++++++++++++++++++++++++------------------------ Source/TableDocument.m | 27 +++++++++++++++++++++---- 3 files changed, 73 insertions(+), 35 deletions(-) (limited to 'Source') diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index 9d1559e6..a9d4f670 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -32,6 +32,20 @@ #import "RegexKitLite.h" #define SP_HELP_TOC_SEARCH_STRING @"contents" +#define SP_HELP_SEARCH_IN_MYSQL 0 +#define SP_HELP_SEARCH_IN_PAGE 1 +#define SP_HELP_SEARCH_IN_WEB 2 +#define SP_HELP_GOBACK_BUTTON 0 +#define SP_HELP_SHOW_TOC_BUTTON 1 +#define SP_HELP_GOFORWARD_BUTTON 2 +#define SP_HELP_NOT_AVAILABLE @"__no_help_available" + +#define SP_MYSQL_DEV_SEARCH_URL @"http://search.mysql.com/search?q=%@&site=refman-%@" + +#define SP_SAVE_ALL_FAVORTITE_MENUITEM_TAG 100001 +#define SP_SAVE_SELECTION_FAVORTITE_MENUITEM_TAG 100000 +#define SP_FAVORITE_HEADER_MENUITEM_TAG 200000 + @class SPQueryFavoriteManager; @@ -43,8 +57,15 @@ IBOutlet id queryFavoritesButton; IBOutlet NSMenuItem *queryFavoritesSearchMenuItem; IBOutlet NSMenuItem *queryFavoritesSaveAsMenuItem; + IBOutlet NSMenuItem *queryFavoritesSaveAllMenuItem; IBOutlet id queryFavoritesSearchFieldView; IBOutlet NSSearchField *queryFavoritesSearchField; + + IBOutlet NSWindow *queryFavoritesSheet; + IBOutlet NSButton *saveQueryFavoriteButton; + IBOutlet NSTextField *queryFavoriteNameTextField; + IBOutlet id saveQueryFavoriteGlobal; + IBOutlet id queryHistoryButton; IBOutlet NSMenuItem *queryHistorySearchMenuItem; IBOutlet id queryHistorySearchFieldView; @@ -73,11 +94,6 @@ IBOutlet NSMenuItem *commentCurrentQueryMenuItem; IBOutlet NSMenuItem *commentLineOrSelectionMenuItem; - IBOutlet NSWindow *queryFavoritesSheet; - IBOutlet NSButton *saveQueryFavoriteButton; - IBOutlet NSTextField *queryFavoriteNameTextField; - IBOutlet id saveQueryFavoriteGlobal; - IBOutlet NSWindow *helpWebViewWindow; IBOutlet WebView *helpWebView; IBOutlet NSSearchField *helpSearchField; diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 67741669..155113d9 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -40,15 +40,6 @@ #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 -#define SP_HELP_SEARCH_IN_PAGE 1 -#define SP_HELP_SEARCH_IN_WEB 2 -#define SP_HELP_GOBACK_BUTTON 0 -#define SP_HELP_SHOW_TOC_BUTTON 1 -#define SP_HELP_GOFORWARD_BUTTON 2 -#define SP_HELP_NOT_AVAILABLE @"__no_help_available" - @implementation CustomQuery #pragma mark IBAction methods @@ -2426,34 +2417,32 @@ // Insert new favorites and allow menu items with the same name NSMenu *menu = [queryFavoritesButton menu]; - int i = 6; - NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Global" action:NULL keyEquivalent:@""]; - [item setTag:200000]; - [item setToolTip:@"All global favorites"]; + NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:@"Document" action:NULL keyEquivalent:@""]; + [item setTag:SP_FAVORITE_HEADER_MENUITEM_TAG]; + [item setToolTip:[NSString stringWithFormat:@"‘%@’ based favorites", + [[[[tableDocumentInstance fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] lastPathComponent]]]; [item setIndentationLevel:0]; [menu addItem:item]; [item release]; - for (NSDictionary *favorite in globalFavorites) { + for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:[tableDocumentInstance fileURL]]) { NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithString:[favorite objectForKey:@"name"]] action:NULL keyEquivalent:@""]; - [item setTag:i++]; [item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]]; [item setIndentationLevel:1]; [menu addItem:item]; [item release]; } - item = [[NSMenuItem alloc] initWithTitle:@"Document" action:NULL keyEquivalent:@""]; - [item setTag:200000]; - [item setToolTip:@"All document-based favorites"]; + item = [[NSMenuItem alloc] initWithTitle:@"Global" action:NULL keyEquivalent:@""]; + [item setTag:SP_FAVORITE_HEADER_MENUITEM_TAG]; + [item setToolTip:@"Globally stored favorites"]; [item setIndentationLevel:0]; [menu addItem:item]; [item release]; - for (NSDictionary *favorite in [[SPQueryController sharedQueryController] favoritesForFileURL:[tableDocumentInstance fileURL]]) { + for (NSDictionary *favorite in globalFavorites) { NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:[NSString stringWithString:[favorite objectForKey:@"name"]] action:NULL keyEquivalent:@""]; - [item setTag:i++]; [item setToolTip:[NSString stringWithString:[favorite objectForKey:@"query"]]]; [item setIndentationLevel:1]; [menu addItem:item]; @@ -2545,8 +2534,9 @@ */ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - // Control "Save ... to Favorites" = tag is set to 1000 - if ( [menuItem tag] == 100000 ) { + + // Control "Save ... to Favorites" + if ( [menuItem tag] == SP_SAVE_SELECTION_FAVORTITE_MENUITEM_TAG ) { if ([[textView string] length] < 1) return NO; if([textView selectedRange].length) [menuItem setTitle:NSLocalizedString(@"Save Selection to Favorites",@"Save Selection to Favorites")]; @@ -2555,14 +2545,19 @@ else [menuItem setTitle:NSLocalizedString(@"Save All to Favorites",@"Save All to Favorites")]; } - // Control "Save All to Favorites" = tag is set to 1001 - if ( [menuItem tag] == 100001 ) { + + // Control "Save All to Favorites" + if ( [menuItem tag] == SP_SAVE_ALL_FAVORTITE_MENUITEM_TAG ) { if ([[textView string] length] < 1) return NO; } - if ( [menuItem tag] == 200000 ) { + + // Avoid selecting button list headers + if ( [menuItem tag] == SP_FAVORITE_HEADER_MENUITEM_TAG ) { return NO; } + return YES; + } @@ -2609,8 +2604,10 @@ { int i; NSMenu *menu = [queryFavoritesButton menu]; + NSString *searchPattern = [queryFavoritesSearchField stringValue]; for (i=6; i< [menu numberOfItems]; i++) - [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", [queryFavoritesSearchField stringValue]]])]; + [[menu itemAtIndex:i] setHidden:([[menu itemAtIndex:i] tag] != SP_FAVORITE_HEADER_MENUITEM_TAG + && ![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", searchPattern]])]; } @@ -2618,6 +2615,7 @@ { int i; NSMenu *menu = [queryHistoryButton menu]; + NSString *searchPattern = [queryHistorySearchField stringValue]; for (i=2; i< [menu numberOfItems]; i++) [[menu itemAtIndex:i] setHidden:(![[[menu itemAtIndex:i] title] isMatchedByRegex:[NSString stringWithFormat:@"(?i).*%@.*", [queryHistorySearchField stringValue]]])]; @@ -2625,6 +2623,11 @@ - (void)awakeFromNib { + + // Set pre-defined menu tags + [queryFavoritesSaveAsMenuItem setTag:SP_SAVE_SELECTION_FAVORTITE_MENUITEM_TAG]; + [queryFavoritesSaveAllMenuItem setTag:SP_SAVE_ALL_FAVORTITE_MENUITEM_TAG]; + // Set the structure and index view's vertical gridlines if required [customQueryView setGridStyleMask:([prefs boolForKey:@"DisplayTableViewVerticalGridlines"]) ? NSTableViewSolidVerticalGridLineMask : NSTableViewGridNone]; } diff --git a/Source/TableDocument.m b/Source/TableDocument.m index 9c56ff04..a546333a 100644 --- a/Source/TableDocument.m +++ b/Source/TableDocument.m @@ -1925,7 +1925,7 @@ { // Auto-save preferences to spf file based connection if([self fileURL] && [[[self fileURL] absoluteString] length] && [[[self fileURL] absoluteString] hasPrefix:@"/"]) - if(![self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]) { + if(_isConnected && ![self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]) { NSLog(@"Preference data for file ‘%@’ could not be saved.", [[[self fileURL] absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]); NSBeep(); } @@ -2128,9 +2128,12 @@ // Update only query favourites, history, etc. by reading the file again if(saveOnlyPreferences) { - // Check for save file URL - // TODO maybe alert ? - if(![[[self fileURL] absoluteString] length] && ![[[self fileURL] absoluteString] hasPrefix:@"/"]) return NO; + // Check URL for safety reasons + if(![[[self fileURL] absoluteString] length] || ![[[self fileURL] absoluteString] hasPrefix:@"/"]) { + NSLog(@"Couldn't save data. No file URL found!"); + NSBeep(); + return NO; + } NSError *readError = nil; NSString *convError = nil; @@ -2976,13 +2979,29 @@ if ( ![tablesListInstance selectionShouldChangeInTableView:nil] ) { return NO; } else { + + if(!_isConnected) return YES; + // Auto-save spf file based connection if([self fileURL] && [[[self fileURL] absoluteString] length] && [[[self fileURL] absoluteString] hasPrefix:@"/"]) { BOOL isSaved = [self saveDocumentWithFilePath:nil inBackground:YES onlyPreferences:YES]; if(isSaved) [[SPQueryController sharedQueryController] removeRegisteredDocumentWithFileURL:[self fileURL]]; return isSaved; + + // Before removing an Untitled doc check if it contains any defined query favorites. + // If so save them globally. TODO: How to do it better ? } else if([self fileURL] && [[[self fileURL] absoluteString] length] && ![[[self fileURL] absoluteString] hasPrefix:@"/"]) { + if([[[SPQueryController sharedQueryController] favoritesForFileURL:[self fileURL]] count]) { + + NSMutableArray *favs = [[[NSMutableArray alloc] init] autorelease]; + [favs addObjectsFromArray:[prefs objectForKey:@"queryFavorites"]]; + [favs addObjectsFromArray:[[SPQueryController sharedQueryController] favoritesForFileURL:[self fileURL]]]; + [prefs setObject:favs forKey:@"queryFavorites"]; + + if(![prefs synchronize]) + NSLog(@"Sorry, couldn't backup query favorites from Untitled document."); + } [[SPQueryController sharedQueryController] removeRegisteredDocumentWithFileURL:[self fileURL]]; return YES; } -- cgit v1.2.3