1 | // ================================================================ // |
---|
2 | // // |
---|
3 | // File : AWT_www.cxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ================================================================ // |
---|
10 | |
---|
11 | #include "awt_config_manager.hxx" |
---|
12 | #include "awt.hxx" |
---|
13 | |
---|
14 | #include <aw_window.hxx> |
---|
15 | #include <aw_global_awars.hxx> |
---|
16 | #include <aw_awars.hxx> |
---|
17 | #include <aw_root.hxx> |
---|
18 | #include <aw_msg.hxx> |
---|
19 | |
---|
20 | #include <arbdbt.h> |
---|
21 | #include <gb_aci.h> |
---|
22 | |
---|
23 | #include <arb_defs.h> |
---|
24 | |
---|
25 | #define WWW_COUNT 10 |
---|
26 | #define AWAR_WWW_SELECT "www/url_select" |
---|
27 | #define AWAR_WWW_SELECT_TEMPLATE "www/url_%i/select" |
---|
28 | #define AWAR_WWW_TEMPLATE "www/url_%i/srt" |
---|
29 | #define AWAR_WWW_DESC_TEMPLATE "www/url_%i/desc" |
---|
30 | |
---|
31 | inline char *extract_url_host(const char *templ) { |
---|
32 | const char *url_start = strstr(templ, "\"http://"); |
---|
33 | if (url_start) { |
---|
34 | const char *host_start = url_start+8; |
---|
35 | const char *slash = strchr(host_start, '/'); |
---|
36 | |
---|
37 | if (slash) return ARB_strpartdup(host_start, slash-1); |
---|
38 | } |
---|
39 | return NULp; |
---|
40 | } |
---|
41 | |
---|
42 | inline bool url_host_matches(const char *templ1, const char *templ2) { |
---|
43 | bool matches = false; |
---|
44 | char *url1 = extract_url_host(templ1); |
---|
45 | if (url1) { |
---|
46 | char *url2 = extract_url_host(templ2); |
---|
47 | matches = url1 && url2 && ARB_stricmp(url1, url2) == 0; |
---|
48 | free(url2); |
---|
49 | } |
---|
50 | free(url1); |
---|
51 | return matches; |
---|
52 | } |
---|
53 | |
---|
54 | void awt_create_aww_vars(AW_root *aw_root, AW_default aw_def) { |
---|
55 | struct Example { |
---|
56 | const char *descr; |
---|
57 | const char *templ; |
---|
58 | } example[] = { |
---|
59 | { "EMBL example", "\"http://www.ebi.ac.uk/ena/data/view/\";readdb(acc)" }, |
---|
60 | { "SILVA example", "\"http://www.arb-silva.de/browser/ssu/\";readdb(acc)" }, |
---|
61 | { "Google example", "\"http://www.google.com/search?q=\";readdb(full_name);|srt(\": =+\")" }, |
---|
62 | { "Wikipedia example", "\"http://en.wikipedia.org/wiki/Special:Search?search=\";dd" } |
---|
63 | }, empty = { "", "" }; |
---|
64 | |
---|
65 | const int DEFAULT_SELECT = 1; // SILVA |
---|
66 | const int EXAMPLE_COUNT = ARRAY_ELEMS(example); |
---|
67 | STATIC_ASSERT(EXAMPLE_COUNT <= WWW_COUNT); |
---|
68 | |
---|
69 | bool example_url_seen[EXAMPLE_COUNT]; |
---|
70 | for (int x = 0; x<EXAMPLE_COUNT; ++x) example_url_seen[x] = false; |
---|
71 | |
---|
72 | AW_awar *awar_templ[WWW_COUNT]; |
---|
73 | AW_awar *awar_descr[WWW_COUNT]; |
---|
74 | bool is_empty[WWW_COUNT]; |
---|
75 | |
---|
76 | for (int i = 0; i<WWW_COUNT; ++i) { |
---|
77 | const Example& curr = i<EXAMPLE_COUNT ? example[i] : empty; |
---|
78 | const char *awar_name; |
---|
79 | |
---|
80 | awar_name = GBS_global_string(AWAR_WWW_TEMPLATE, i); |
---|
81 | awar_templ[i] = aw_root->awar_string(awar_name, curr.templ, aw_def); |
---|
82 | |
---|
83 | awar_name = GBS_global_string(AWAR_WWW_DESC_TEMPLATE, i); |
---|
84 | awar_descr[i] = aw_root->awar_string(awar_name, curr.descr, aw_def); |
---|
85 | |
---|
86 | const char *templ = awar_templ[i]->read_char_pntr(); |
---|
87 | const char *descr = awar_descr[i]->read_char_pntr(); |
---|
88 | |
---|
89 | is_empty[i] = !templ[0] && !descr[0]; |
---|
90 | if (!is_empty[i]) { |
---|
91 | for (int x = 0; x<EXAMPLE_COUNT; ++x) { |
---|
92 | if (!example_url_seen[x]) { |
---|
93 | example_url_seen[x] = url_host_matches(templ, example[x].templ); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | awar_name = GBS_global_string(AWAR_WWW_SELECT_TEMPLATE, i); |
---|
99 | aw_root->awar_int(awar_name, 0, aw_def); |
---|
100 | } |
---|
101 | |
---|
102 | // insert missing examples |
---|
103 | for (int x = 0; x<EXAMPLE_COUNT; ++x) { |
---|
104 | if (!example_url_seen[x]) { |
---|
105 | for (int i = 0; i<WWW_COUNT; ++i) { |
---|
106 | if (is_empty[i]) { |
---|
107 | awar_templ[i]->write_string(example[x].templ); |
---|
108 | awar_descr[i]->write_string(example[x].descr); |
---|
109 | is_empty[i] = false; |
---|
110 | break; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | aw_root->awar_int(AWAR_WWW_SELECT, DEFAULT_SELECT, aw_def); |
---|
117 | awt_assert(ARB_global_awars_initialized()); |
---|
118 | } |
---|
119 | |
---|
120 | GB_ERROR awt_open_ACI_URL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item, const char *url_aci) { |
---|
121 | GB_ERROR error = NULp; |
---|
122 | GB_transaction tscope(gb_main); |
---|
123 | |
---|
124 | const char *item_name = NULp; // name of database item (used as input for ACI) |
---|
125 | if (gb_item) { |
---|
126 | GBDATA *gb_name = GB_entry(gb_item, "group_name"); |
---|
127 | if (!gb_name) gb_name = GB_entry(gb_item, "full_name"); |
---|
128 | if (!gb_name) gb_name = GB_entry(gb_item, "name"); |
---|
129 | |
---|
130 | item_name = gb_name ? GB_read_char_pntr(gb_name) : "NoNameDetected"; |
---|
131 | } |
---|
132 | else { |
---|
133 | item_name = "NoItemSelected"; |
---|
134 | } |
---|
135 | |
---|
136 | GBL_env env(gb_main, NULp); // @@@ pass from a caller who knows the tree? |
---|
137 | GBL_call_env callEnv(gb_item, env); |
---|
138 | char *url = GB_command_interpreter_in_env(item_name, url_aci, callEnv); |
---|
139 | |
---|
140 | if (!url) error = GB_await_error(); |
---|
141 | else AW_openURL(aw_root, url); |
---|
142 | |
---|
143 | free(url); |
---|
144 | |
---|
145 | return error; |
---|
146 | } |
---|
147 | |
---|
148 | GB_ERROR awt_openDefaultURL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item) { |
---|
149 | GB_transaction tscope(gb_main); |
---|
150 | int url_selected = aw_root->awar(AWAR_WWW_SELECT)->read_int(); |
---|
151 | const char *awar_urlaci = GBS_global_string(AWAR_WWW_TEMPLATE, url_selected); |
---|
152 | char *url_aci = aw_root->awar(awar_urlaci)->read_string(); |
---|
153 | GB_ERROR error = awt_open_ACI_URL_with_item(aw_root, gb_main, gb_item, url_aci); |
---|
154 | |
---|
155 | free(url_aci); |
---|
156 | return error; |
---|
157 | } |
---|
158 | |
---|
159 | static void awt_openDefaultURL_on_selected_species(AW_window *aww, GBDATA *gb_main) { |
---|
160 | GB_transaction tscope(gb_main); |
---|
161 | AW_root *aw_root = aww->get_root(); |
---|
162 | GB_ERROR error = NULp; |
---|
163 | char *selected_species = aw_root->awar(AWAR_SPECIES_NAME)->read_string(); |
---|
164 | GBDATA *gb_species = GBT_find_species(gb_main, selected_species); |
---|
165 | |
---|
166 | if (!gb_species) { |
---|
167 | error = GBS_global_string("Cannot find species '%s'", selected_species); |
---|
168 | } |
---|
169 | else { |
---|
170 | error = awt_openDefaultURL_with_item(aw_root, gb_main, gb_species); |
---|
171 | } |
---|
172 | if (error) aw_message(error); |
---|
173 | delete selected_species; |
---|
174 | } |
---|
175 | |
---|
176 | static void awt_www_select_change(AW_window *aww, int selected) { |
---|
177 | AW_root *aw_root = aww->get_root(); |
---|
178 | for (int i=0; i<WWW_COUNT; i++) { |
---|
179 | const char *awar_name = GBS_global_string(AWAR_WWW_SELECT_TEMPLATE, i); |
---|
180 | aw_root->awar(awar_name)->write_int((i==selected) ? 1 : 0); |
---|
181 | } |
---|
182 | aw_root->awar(AWAR_WWW_SELECT)->write_int(selected); |
---|
183 | } |
---|
184 | |
---|
185 | static void www_setup_config(AWT_config_definition& cdef) { |
---|
186 | for (int i=0; i<WWW_COUNT; i++) { |
---|
187 | char buf[256]; |
---|
188 | sprintf(buf, AWAR_WWW_SELECT_TEMPLATE, i); cdef.add(buf, "active", i); |
---|
189 | sprintf(buf, AWAR_WWW_DESC_TEMPLATE, i); cdef.add(buf, "description", i); |
---|
190 | sprintf(buf, AWAR_WWW_TEMPLATE, i); cdef.add(buf, "template", i); |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | AW_window *AWT_create_www_window(AW_root *aw_root, GBDATA *gb_main) { |
---|
195 | AW_window_simple *aws = new AW_window_simple; |
---|
196 | aws->init(aw_root, "WWW_PROPS", "WWW"); |
---|
197 | aws->load_xfig("awt/www.fig"); |
---|
198 | aws->auto_space(10, 5); |
---|
199 | |
---|
200 | aws->at("close"); |
---|
201 | aws->callback(AW_POPDOWN); |
---|
202 | aws->create_button("CLOSE", "CLOSE", "C"); |
---|
203 | |
---|
204 | aws->at("help"); |
---|
205 | aws->callback(makeHelpCallback("props_www.hlp")); |
---|
206 | aws->create_button("HELP", "HELP", "H"); |
---|
207 | |
---|
208 | aws->at("action"); |
---|
209 | aws->callback(makeWindowCallback(awt_openDefaultURL_on_selected_species, gb_main)); |
---|
210 | aws->create_button("WWW", "WWW", "W"); |
---|
211 | |
---|
212 | aws->button_length(13); |
---|
213 | int dummy, closey; |
---|
214 | aws->at_newline(); |
---|
215 | aws->get_at_position(&dummy, &closey); |
---|
216 | aws->at_newline(); |
---|
217 | |
---|
218 | aws->create_button(NULp, "K"); |
---|
219 | |
---|
220 | aws->at_newline(); |
---|
221 | |
---|
222 | int fieldselectx, srtx, descx; |
---|
223 | |
---|
224 | |
---|
225 | int i; |
---|
226 | aws->get_at_position(&fieldselectx, &dummy); |
---|
227 | |
---|
228 | aws->auto_space(10, 2); |
---|
229 | |
---|
230 | for (i=0; i<WWW_COUNT; i++) { |
---|
231 | char buf[256]; |
---|
232 | sprintf(buf, AWAR_WWW_SELECT_TEMPLATE, i); |
---|
233 | aws->callback(makeWindowCallback(awt_www_select_change, i)); // @@@ used as TOGGLE_CLICK_CB (see #559) |
---|
234 | aws->create_toggle(buf); |
---|
235 | |
---|
236 | sprintf(buf, AWAR_WWW_DESC_TEMPLATE, i); |
---|
237 | aws->get_at_position(&descx, &dummy); |
---|
238 | aws->create_input_field(buf, 15); |
---|
239 | |
---|
240 | aws->get_at_position(&srtx, &dummy); |
---|
241 | sprintf(buf, AWAR_WWW_TEMPLATE, i); |
---|
242 | aws->create_input_field(buf, 80); |
---|
243 | |
---|
244 | aws->at_newline(); |
---|
245 | } |
---|
246 | aws->at_newline(); |
---|
247 | |
---|
248 | aws->create_input_field(AWAR_WWW_BROWSER, 100); |
---|
249 | |
---|
250 | aws->at(fieldselectx, closey); |
---|
251 | |
---|
252 | aws->at_x(fieldselectx); |
---|
253 | aws->create_button(NULp, "SEL"); |
---|
254 | |
---|
255 | aws->at_x(descx); |
---|
256 | aws->create_button(NULp, "DESCRIPTION"); |
---|
257 | |
---|
258 | aws->at_x(srtx); |
---|
259 | aws->create_button(NULp, "URL"); |
---|
260 | |
---|
261 | aws->at("config"); |
---|
262 | AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "www", makeConfigSetupCallback(www_setup_config)); |
---|
263 | |
---|
264 | awt_www_select_change(aws, aw_root->awar(AWAR_WWW_SELECT)->read_int()); |
---|
265 | return aws; |
---|
266 | } |
---|
267 | |
---|
268 | void AWT_openURL(AW_window *aww, const char *url) { |
---|
269 | AW_openURL(aww->get_root(), url); |
---|
270 | } |
---|
271 | |
---|