-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathQuickGoogle.m
67 lines (55 loc) · 2.33 KB
/
QuickGoogle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// coded by theiostream
// Google fix by @fr0st -- code taken from iAndroid.
#import <UIKit/UIKit.h>
#import <libactivator.h>
@interface QGActivator : NSObject <LAListener, UIAlertViewDelegate, UITextFieldDelegate> {
UIAlertView *searchAlert;
UITextField *searchField;
}
@end
@interface UIAlertView (QuickGoogle)
- (void)addTextFieldWithValue:(NSString *)value label:(id)label;
- (UITextField *)textFieldAtIndex:(NSUInteger)index;
@end
@interface UIApplication (QuickGoogle)
- (void)applicationOpenURL:(id)url;
@end
// From StatusGoogle (yeah, I rewrote it before quickgoogle)
static NSURL *SGURL(NSString *url) {
NSArray *arr = [NSArray arrayWithObjects:@"http://",@"https://",@"www.",@".com",@".co",@".net",@".org",@".us",@".me",@".it",@".uk",@".de",@".br",nil];
for (NSString *k in arr) {
if ([url rangeOfString:k].location != NSNotFound) {
NSURL *r = [NSURL URLWithString:url];
if (![[r scheme] length]) r = [NSURL URLWithString:[@"http://" stringByAppendingString:url]];
return r;
}
}
return [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@&ie=utf-8&oe=utf-8", url]];
}
@implementation QGActivator
- (void)activator:(LAActivator *)activator receiveEvent:(LAEvent *)event {
searchAlert = [[[UIAlertView alloc] initWithTitle:@"QuickGoogle!" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Google", nil] autorelease];
[searchAlert addTextFieldWithValue:[NSString string] label:nil];
searchField = [searchAlert textFieldAtIndex:0];
[searchField setDelegate:self];
[searchField setClearButtonMode:UITextFieldViewModeWhileEditing];
[searchField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[searchField setAutocorrectionType:UITextAutocorrectionTypeNo];
[searchAlert show];
[event setHandled:YES];
}
- (void)alertView:(UIAlertView *)alert willDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alert isEqual:searchAlert]) {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] applicationOpenURL:SGURL([[searchField text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding])];
}
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[searchAlert dismissWithClickedButtonIndex:1 animated:YES];
return YES;
}
+ (void)load {
[[LAActivator sharedInstance] registerListener:[self new] forName:@"am.theiostre.quickgoogle"];
}
@end