From 494226553237e2e19d882339180f8be26153eea4 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Mon, 27 Apr 2009 13:31:44 +0000 Subject: =?UTF-8?q?=E2=80=A2=20MySQL=20Help=20improved=20-=20better=20HTML?= =?UTF-8?q?=20view,=20list=20topics=20for=20multi-matching=20keywords=20-?= =?UTF-8?q?=20for=20test=20cases=20invoke=20it=20by=20CTRL+H=20in=20the=20?= =?UTF-8?q?Custom=20Query=20textView=20-=20GUI=20is=20really=20tentative!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CustomQuery.h | 11 ++++ Source/CustomQuery.m | 180 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 174 insertions(+), 17 deletions(-) (limited to 'Source') diff --git a/Source/CustomQuery.h b/Source/CustomQuery.h index e1906fd1..4f8fd468 100644 --- a/Source/CustomQuery.h +++ b/Source/CustomQuery.h @@ -60,6 +60,8 @@ IBOutlet NSWindow *helpWebViewWindow; IBOutlet id helpWebView; + IBOutlet NSSearchField *helpSearchField; + NSString *lastHelpString; NSArray *queryResult; NSUserDefaults *prefs; @@ -80,6 +82,10 @@ - (IBAction)closeSheet:(id)sender; - (IBAction)gearMenuItemSelected:(id)sender; - (IBAction)getHelpForCurrentWord:(id)sender; +- (IBAction)showHelpForSearchString:(id)sender; +- (IBAction)showHelpContent:(id)sender; +- (IBAction)showLastHelp:(id)sender; + // queryFavoritesSheet methods - (IBAction)addQueryFavorite:(id)sender; @@ -96,10 +102,15 @@ // Accessors - (NSArray *)currentResult; +// MySQL Help +- (void)showHelpFor:(NSString *)aString; +- (NSString *)getHTMLHelpFor:(NSString *)aString; + // Other - (void)setConnection:(CMMCPConnection *)theConnection; - (void)setFavorites; - (void)doPerformQueryService:(NSString *)query; - (NSString *)usedQuery; + @end diff --git a/Source/CustomQuery.m b/Source/CustomQuery.m index 51880119..d0f0f606 100644 --- a/Source/CustomQuery.m +++ b/Source/CustomQuery.m @@ -114,72 +114,218 @@ } /* - * Return the help string formatted from executing "HELP 'aString'" + * Retrieve and show the data for "HELP 'search word'" + */ + +- (IBAction)showHelpForSearchString:(id)sender +{ + [self showHelpFor:[[helpSearchField stringValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]]; +} + +/* + * Retrieve and show the data for "HELP 'currentWord'" */ - (IBAction)getHelpForCurrentWord:(id)sender { NSString *aString = [[textView string] substringWithRange:[textView getRangeForCurrentWord]]; - if(![aString length]) return; + // if(![aString length]) return; + + [self showHelpFor:aString]; + +} + +/* + * Retrieve and show the data for "HELP 'contents' (MySQL help root)". + */ + +- (IBAction)showHelpContent:(id)sender +{ + [self showHelpFor:@"contents"]; +} + +- (IBAction)showLastHelp:(id)sender +{ + NSLog(@"%@", lastHelpString); + if([lastHelpString length]) + [self showHelpFor:lastHelpString]; +} + + +/* + * Retrieve and show the data for "HELP 'aString'". + */ +- (void)showHelpFor:(NSString *)aString +{ + NSString * helpString = [self getHTMLHelpFor:aString]; + lastHelpString = [[NSString stringWithString:aString] retain]; + // Order out the Help window + if(![helpWebViewWindow isVisible]) + { + + // Get the major MySQL server version in the form of x.x, which is basically the first 3 characters of the returned version string + //get mysql version + CMMCPResult *theResult = nil; + NSString *mySQLVersion; + theResult = [mySQLConnection queryString:@"SHOW VARIABLES LIKE 'version'"]; + NSString *version = [[theResult fetchRowAsArray] objectAtIndex:1]; + if ( [version isKindOfClass:[NSData class]] ) { + // starting with MySQL 4.1.14 the mysql variables are returned as nsdata + mySQLVersion = [[NSString alloc] initWithData:version encoding:[mySQLConnection encoding]]; + } else { + mySQLVersion = [[NSString stringWithString:version] retain]; + } + + [helpWebViewWindow setTitle:[NSString stringWithFormat:@"%@ (%@ %@)", NSLocalizedString(@"MySQL Help", @"mysql help"), NSLocalizedString(@"version", @"version"), [mySQLVersion substringToIndex:3]]]; + [helpWebViewWindow orderFront:helpWebView]; + } + + if(![helpString length]) return; + + [[helpWebView mainFrame] loadHTMLString:helpString baseURL:nil]; + +} + + +/* + * Return the help string HTML formatted from executing "HELP 'aString'". + * If more than one help topic was found return a link list. + */ +- (NSString *)getHTMLHelpFor:(NSString *)aString +{ + + if(![aString length]) return(@""); CMMCPResult *theResult = nil; NSDictionary *tableDetails; + NSRange aRange; NSMutableString *theHelp = [NSMutableString string]; [theHelp setString: @"" @"" @" " @"" @"" + ]; theResult = [mySQLConnection queryString:[NSString stringWithFormat:@"HELP '%@'", aString]]; - if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] || ![theResult numOfRows]) return; + if ( ![[mySQLConnection getLastErrorMessage] isEqualToString:@""] || ![theResult numOfRows]) return @""; tableDetails = [[NSDictionary alloc] initWithDictionary:[theResult fetchRowAsDictionary]]; - if ([tableDetails objectForKey:@"description"]) { // help found + if ([tableDetails objectForKey:@"description"]) { // one single help topic found if ([tableDetails objectForKey:@"name"]) { - [theHelp appendString:@"

"]; + [theHelp appendString:@"

  "]; [theHelp appendString:[[[tableDetails objectForKey:@"name"] copy] autorelease]]; [theHelp appendString:@"

"]; } if ([tableDetails objectForKey:@"description"]) { + NSMutableString *desc = [NSMutableString string]; + NSError *err1 = NULL; + NSString *aUrl; + + [desc setString:[[[tableDetails objectForKey:@"description"] copy] autorelease]]; + + // Look for an URL and create an HTML link out of it + aRange = [desc rangeOfRegex:@"URL: +(.*)" options:RKLNoOptions inRange:NSMakeRange(0, [desc length]) capture:1 error:&err1]; + if(aRange.location != NSNotFound) { + aUrl = [desc substringWithRange:aRange]; + [desc replaceCharactersInRange:aRange withString:[NSString stringWithFormat:@"%@", aUrl, aUrl]]; + } + // aRange = NSMakeRange(0,0); + // int safeCnt = 0; + // while(1){ + // aRange = [desc rangeOfRegex:@"(http.*?)\\s" options:RKLNoOptions inRange:NSMakeRange(aRange.location+aRange.length, [desc length]-aRange.location-aRange.length) capture:1 error:&err1]; + // if(aRange.location != NSNotFound) { + // aUrl = [desc substringWithRange:aRange]; + // [desc replaceCharactersInRange:aRange withString:[NSString stringWithFormat:@"%@", aUrl, aUrl]]; + // } + // else + // break; + // safeCnt++; + // if(safeCnt > 20) + // break; + // } + [theHelp appendString:@"
"];
-			[theHelp appendString:[[[tableDetails objectForKey:@"description"] copy] autorelease]];
+			[theHelp appendString:desc];
 			[theHelp appendString:@"
"]; } if([tableDetails objectForKey:@"example"]){ NSString *examples = [[[tableDetails objectForKey:@"example"] copy] autorelease]; if([examples length]){ - [theHelp appendString:@"

Example:
"];
+				[theHelp appendString:@"

Example:

"];
 				[theHelp appendString:examples];
 				[theHelp appendString:@"
"]; } } - [theHelp appendString:@""]; - + } else { // list all found topics + int i; + int r = [theResult numOfRows]; + if (r) [theResult dataSeek:0]; + + if(![aString isEqualToString:@"contents"]) + [theHelp appendString:[NSString stringWithFormat:@"
%@ “%@”
", NSLocalizedString(@"Help topics for", @"help topics for"), aString]]; + else + [theHelp appendString:[NSString stringWithFormat:@"
%@:
", NSLocalizedString(@"MySQL Help – Categories", @"mysql help categories"), aString]]; + + [theHelp appendString:@""]; } - + [theHelp appendString:@""]; + [tableDetails release]; - [[helpWebView mainFrame] loadHTMLString:theHelp baseURL:nil]; - [helpWebViewWindow orderFront:self]; + return theHelp; + +} +/* + * Link detector: If user clicked at an http link open it in the default browser, + * otherwise search for it in the MySQL help. + */ +- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id)listener +{ + int navigationType = [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue]; + if([[[request URL] scheme] isEqualToString:@"applewebdata"]){ + [self showHelpFor:[[[request URL] path] substringWithRange:NSMakeRange(1,[[[request URL] path] length]-1)]]; + [listener ignore]; + } else { + if (navigationType == WebNavigationTypeOther) { + [listener use]; + } else if (navigationType == WebNavigationTypeLinkClicked) { + [[NSWorkspace sharedWorkspace] openURL:[actionInformation objectForKey:WebActionOriginalURLKey]]; + [listener ignore]; + } else { + // Ignore WebNavigationTypeFormSubmitted, WebNavigationTypeBackForward, + // WebNavigationTypeReload and WebNavigationTypeFormResubmitted. + [listener ignore]; + } + } } -- cgit v1.2.3