Respect search from url bar setting
This commit is contained in:
parent
ca1034b49e
commit
b70c88f2b6
|
@ -55,7 +55,13 @@
|
|||
|
||||
-(void)enterUrl: (id)sender {
|
||||
NSString *string = [sender stringValue];
|
||||
[self openUrlString: string];
|
||||
if ([[Preferences defaultPreferences] searchFromUrlBar]) {
|
||||
SearchProvider *searchProvider = [[Preferences defaultPreferences] searchProvider];
|
||||
Website *website = [searchProvider websiteForQuery: [sender stringValue]];
|
||||
[self openWebsite: website];
|
||||
} else {
|
||||
[self openUrlString: string];
|
||||
}
|
||||
}
|
||||
|
||||
-(void)enterSearch: (id)sender {
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
#import "Website.h"
|
||||
#import "SearchProvider.h"
|
||||
|
||||
@interface SearchProvider (Private)
|
||||
-(BOOL)isUrl: (NSString*)possibleUrlString;
|
||||
@end
|
||||
|
||||
@implementation SearchProvider
|
||||
|
||||
-(id)initWithName: (NSString*)aName searchUrl: (NSString*)aSearchUrl {
|
||||
|
@ -31,10 +35,13 @@
|
|||
}
|
||||
|
||||
-(Website*)websiteForQuery: (NSString*)queryString {
|
||||
NSString *url = [searchUrl stringByReplacingOccurrencesOfString: @"%s" withString:
|
||||
[queryString stringByAddingPercentEscapesUsingEncoding:
|
||||
NSUTF8StringEncoding]];
|
||||
return [[[Website alloc] initWithName: name url: url] autorelease];
|
||||
if ([self isUrl: queryString]) {
|
||||
return [[[Website alloc] initWithName: @"" url: queryString] autorelease];
|
||||
} else {
|
||||
NSString *url = [searchUrl stringByReplacingOccurrencesOfString: @"%s" withString:
|
||||
[queryString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
|
||||
return [[[Website alloc] initWithName: name url: url] autorelease];
|
||||
}
|
||||
}
|
||||
|
||||
-(NSDictionary*)dictionaryRepresentation {
|
||||
|
@ -51,4 +58,23 @@
|
|||
searchUrl: @"https://www.startpage.com/do/dsearch?query=%s"] autorelease];
|
||||
}
|
||||
|
||||
-(BOOL)isUrl: (NSString*)possibleUrlString {
|
||||
BOOL startsWithProtocol = [possibleUrlString hasPrefix: @"http://"] ||
|
||||
[possibleUrlString hasPrefix: @"https://"] ||
|
||||
[possibleUrlString hasPrefix: @"///"];
|
||||
if (startsWithProtocol)
|
||||
return YES;
|
||||
|
||||
NSError *err = nil;
|
||||
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:
|
||||
@"([a-zA-Z0-9]+\\.)*[a-zA-Z0-9]+\\.[a-zA-Z]+(/.+)?" options: 0 error: &err];
|
||||
if (err != nil) {
|
||||
NSLog(@"Error creating regexp");
|
||||
return YES;
|
||||
}
|
||||
NSUInteger matches = [regexp numberOfMatchesInString: possibleUrlString options: 0 range:
|
||||
NSMakeRange(0, [possibleUrlString length])];
|
||||
return matches > 0;
|
||||
}
|
||||
|
||||
@end
|
Loading…
Reference in New Issue