Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 203 lines (182 sloc) 7.582 kb
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
1 #import "KeywordSaveController.h"
2 #import "KeywordMapper.h"
3 #import "KeywordMapping.h"
4 #import "KeywurlPlugin.h"
5
6 @interface KeywordSaveController (Internal)
7
8 - (void) collectFields: (NSMutableArray*) fields
9 fromElement: (DOMElement*) element;
10 - (void) collectButtons: (NSMutableDictionary*) buttons
11 fromElement: (DOMElement*) element;
12
13 @end
14
15 @interface FormField : NSObject {
16 NSString* name;
17 NSString* value;
18 }
19
20 - initWithName: (NSString*) theName value: (NSString*) theValue;
21 - (NSString*) name;
22 - (NSString*) value;
23
24 @end
25
26 @implementation FormField
27
28 - initWithName: (NSString*) theName value: (NSString*) theValue {
29 name = [theName copy];
30 value = [theValue copy];
31 return self;
32 }
33
34 - (NSString*) name {
35 return name;
36 }
37
38 - (NSString*) value {
39 return value;
40 }
41
42 @end
43
44 @implementation KeywordSaveController
45
46 - (id) initWithUrl: (NSString*) theUrl
47 inputElement: (DOMElement*) theInputElement
48 formElement: (DOMElement*) theFormElement {
49 url = [theUrl copy];
8ae4bd9 Ben Trask Balance your retains and releases.
btrask authored
50 inputElement = [theInputElement retain];
51 formElement = [theFormElement retain];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
52 if (![NSBundle loadNibNamed: @"SaveKeywordWindow" owner: self]) {
53 NSLog(@"Could not load resource.");
54 return nil;
55 }
56 {
57 [buttonPopupButton removeAllItems];
58 NSMutableDictionary* buttons = [NSMutableDictionary new];
59 [self collectButtons: buttons fromElement: formElement];
60 if ([buttons count] <= 1) {
61 [buttonPopupButton setHidden: YES];
62 [buttonPopupLabel setHidden: YES];
63 } else {
64 NSEnumerator* keyEumerator = [buttons keyEnumerator];
65 NSString* name;
66 int index = 0;
67 while (name = [keyEumerator nextObject]) {
68 NSString* value = (NSString*) [buttons objectForKey: name];
69 if ([value length] == 0) {
70 value = @"Default button";
71 }
72 [buttonPopupButton insertItemWithTitle: value atIndex: index];
73 NSMenuItem* item = [buttonPopupButton itemAtIndex: index];
74 [item setRepresentedObject: name];
75 index++;
76 }
77 [buttonPopupButton selectItemAtIndex: 0];
78 [buttonPopupButton synchronizeTitleAndSelectedItem];
79 }
80 }
81 [NSApp beginSheet: window
82 modalForWindow: [NSApp keyWindow]
83 modalDelegate: self
84 didEndSelector: @selector(didEndSheet:returnCode:contextInfo:)
85 contextInfo: nil];
86 return self;
87 }
8ae4bd9 Ben Trask Balance your retains and releases.
btrask authored
88 - (void)dealloc
89 {
90 [url release];
91 [inputElement release];
92 [formElement release];
93 [super dealloc];
94 }
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
95
96 - (void) saveKeyword: (id) sender {
97 NSMutableArray* fields = [NSMutableArray new];
98 [self collectFields: fields fromElement: formElement];
99 NSURL* absoluteUrl = [NSURL URLWithString: url];
100 NSString* action = [formElement action];
101 NSURL* actionUrl = [NSURL URLWithString: action relativeToURL: absoluteUrl];
102 NSMutableString* expansion = [NSMutableString new];
103 [expansion appendString: [actionUrl absoluteString]];
104 [expansion appendString: @"?"];
105 for (int i = 0; i < [fields count]; i++) {
106 FormField* field = (FormField*) [fields objectAtIndex: i];
55eaa70 Alexander Staubo Keyword saving, continued.
atombender authored
107 [expansion appendString: [[field name] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
108 [expansion appendString: @"="];
55eaa70 Alexander Staubo Keyword saving, continued.
atombender authored
109 [expansion appendString: [[field value] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
110 [expansion appendString: @"&"];
111 }
112 NSMenuItem* buttonItem = [buttonPopupButton selectedItem];
113 if (buttonItem) {
55eaa70 Alexander Staubo Keyword saving, continued.
atombender authored
114 [expansion appendString: [[buttonItem representedObject] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
115 [expansion appendString: @"="];
55eaa70 Alexander Staubo Keyword saving, continued.
atombender authored
116 [expansion appendString: [[buttonItem title] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
117 [expansion appendString: @"&"];
118 }
119 [expansion appendString: [inputElement name]];
55eaa70 Alexander Staubo Keyword saving, continued.
atombender authored
120 [expansion appendString: @"={query}"];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
121 NSString* keyword = [nameTextField stringValue];
122 KeywordMapping* mapping = [[KeywordMapping alloc] initWithKeyword: keyword
123 expansion: expansion];
124 KeywordMapper* mapper = [[KeywurlPlugin sharedInstance] keywordMapper];
125 [mapper addKeyword: mapping];
211bacb Alexander Staubo Save mappings after creating keyword through the context menu.
atombender authored
126 [mapper saveMappings];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
127 [NSApp endSheet: window];
128
129 NSAlert *alert = [[NSAlert alloc] init];
130 [alert addButtonWithTitle: @"OK"];
55eaa70 Alexander Staubo Keyword saving, continued.
atombender authored
131 [alert setMessageText: @"Keyword added"];
132 [alert setInformativeText: [NSString stringWithFormat:
133 @"You can now type '%@ something' in Safari's address field to do a search for 'something'", keyword]];
1268790 Alexander Staubo Keyword saving GUI.
atombender authored
134 [alert setAlertStyle: NSInformationalAlertStyle];
135 [alert beginSheetModalForWindow: [NSApp keyWindow]
136 modalDelegate: nil
137 didEndSelector: nil
138 contextInfo: nil];
139 }
140
141 - (void) cancel: (id) sender {
142 [NSApp endSheet: window];
143 }
144
145 - (void) didEndSheet: (NSWindow*) sheet returnCode: (int) returnCode
146 contextInfo: (void*) contextInfo
147 {
148 [window orderOut: self];
149 }
150
151 - (void) collectFields: (NSMutableArray*) fields
152 fromElement: (DOMElement*) element {
153 DOMNodeList* children = [element childNodes];
154 for (int i = 0; i < [children length]; i++) {
155 DOMNode* node = [children item: i];
156 if ([node isKindOfClass: [DOMElement class]]) {
157 [self collectFields: fields fromElement: (DOMElement*) node];
158 }
159 }
160 if (element != inputElement && [element respondsToSelector: @selector(disabled)] &&
161 ![element disabled]) {
162 NSString* tag = [[element tagName] lowercaseString];
163 if ([tag isEqualToString: @"textarea"] || (
164 [tag isEqualToString: @"input"] &&
165 ([[element getAttribute: @"type"] isEqualToString: @"hidden"] ||
166 ([[element getAttribute: @"type"] isEqualToString: @"radio"] && [element checked]) ||
167 ([[element getAttribute: @"type"] isEqualToString: @"checkbox"] && [element checked])))) {
168 [fields addObject: [[FormField alloc] initWithName: [element name] value: [element value]]];
169 } else if ([tag isEqualToString: @"select"] &&
170 ![element multiple] && [element selectedIndex] != -1) {
171 [fields addObject: [[FormField alloc] initWithName: [element name] value: [element value]]];
172 } else if ([tag isEqualToString: @"select"] && [element multiple]) {
173 DOMHTMLOptionsCollection* options = [element options];
174 for (int i = 0; i < [options length]; i++) {
175 DOMHTMLOptionElement* option = [options item: i];
176 if ([option selected]) {
177 [fields addObject: [[FormField alloc] initWithName: [element name]
178 value: [option value]]];
179 }
180 }
181 }
182 }
183 }
184
185 - (void) collectButtons: (NSMutableDictionary*) buttons
186 fromElement: (DOMElement*) element {
187 DOMNodeList* children = [element childNodes];
188 for (int i = 0; i < [children length]; i++) {
189 DOMNode* node = [children item: i];
190 if ([node isKindOfClass: [DOMElement class]]) {
191 [self collectButtons: buttons fromElement: (DOMElement*) node];
192 }
193 }
194 NSString* tag = [[element tagName] lowercaseString];
195 if ([tag isEqualToString: @"input"] &&
196 ([[element getAttribute: @"type"] isEqualToString: @"submit"] ||
197 [[element getAttribute: @"type"] isEqualToString: @"image"])) {
198 [buttons setObject: [element value] forKey: [element name]];
199 }
200 }
201
202 @end
Something went wrong with that request. Please try again.