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