source: branches/port5/AWT/AWT_www.cxx

Last change on this file was 6589, checked in by westram, 15 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.0 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <memory.h>
4// #include <malloc.h>
5#include <string.h>
6
7#include <aw_root.hxx>
8#include "aw_global_awars.hxx"
9#include <aw_device.hxx>
10#include <aw_window.hxx>
11#include <aw_awars.hxx>
12
13#include <arbdbt.h>
14#include <awt.hxx>
15#include <inline.h>
16#include <static_assert.h>
17
18#include "awt_config_manager.hxx"
19
20#define WWW_COUNT                10
21#define AWAR_WWW_SELECT          "www/url_select"
22#define AWAR_WWW_SELECT_TEMPLATE "www/url_%i/select"
23#define AWAR_WWW_TEMPLATE        "www/url_%i/srt"
24#define AWAR_WWW_DESC_TEMPLATE   "www/url_%i/desc"
25
26inline char *extract_url_host(const char *templ) {
27    const char *url_start = strstr(templ, "\"http://");
28    if (url_start) {
29        const char *host_start = url_start+8;
30        const char *slash      = strchr(host_start, '/');
31
32        if (slash) return GB_strpartdup(host_start, slash-1);
33    }
34    return NULL;
35}
36
37inline bool url_host_matches(const char *templ1, const char *templ2) {
38    bool  matches = false;
39    char *url1    = extract_url_host(templ1);
40    if (url1) {
41        char *url2 = extract_url_host(templ2);
42        matches = url1 && url2 && ARB_stricmp(url1, url2) == 0;
43        free(url2);
44    }
45    free(url1);
46    return matches;
47}
48
49void awt_create_aww_vars(AW_root *aw_root, AW_default aw_def) {
50    struct Example {
51        const char *descr;
52        const char *templ;
53    } example[] = {
54        { "EMBL example",   "\"http://www.ebi.ac.uk/ena/data/view/\";readdb(acc)" },
55        { "SILVA example",  "\"http://www.arb-silva.de/browser/ssu/\";readdb(acc)" },
56        { "Google example", "\"http://www.google.com/search?q=\";readdb(full_name);|srt(\": =+\")" }
57    }, empty = { "", "" };
58
59    const int DEFAULT_SELECT = 1; // SILVA
60    const int EXAMPLE_COUNT  = sizeof(example)/sizeof(*example);
61    COMPILE_ASSERT(EXAMPLE_COUNT <= WWW_COUNT);
62
63    bool example_url_seen[EXAMPLE_COUNT];
64    for (int x = 0; x<EXAMPLE_COUNT; ++x) example_url_seen[x] = false;
65
66    AW_awar *awar_templ[WWW_COUNT];
67    AW_awar *awar_descr[WWW_COUNT];
68    bool     is_empty[WWW_COUNT];
69
70    for (int i = 0; i<WWW_COUNT; ++i) {
71        const Example&  curr = i<EXAMPLE_COUNT ? example[i] : empty;
72        const char     *awar_name;
73
74        awar_name     = GBS_global_string(AWAR_WWW_TEMPLATE, i);
75        awar_templ[i] = aw_root->awar_string(awar_name, curr.templ, aw_def);
76
77        awar_name     = GBS_global_string(AWAR_WWW_DESC_TEMPLATE, i);
78        awar_descr[i] = aw_root->awar_string(awar_name, curr.descr, aw_def);
79
80        const char *templ = awar_templ[i]->read_char_pntr();
81        const char *descr = awar_descr[i]->read_char_pntr();
82
83        is_empty[i] = !templ[0] && !descr[0];
84        if (!is_empty[i]) {
85            for (int x = 0; x<EXAMPLE_COUNT; ++x) {
86                if (!example_url_seen[x]) {
87                    example_url_seen[x] = url_host_matches(templ, example[x].templ);
88                }
89            }
90        }
91
92        awar_name = GBS_global_string(AWAR_WWW_SELECT_TEMPLATE, i);
93        aw_root->awar_int(awar_name, 0, aw_def);
94    }
95
96    // insert missing examples
97    for (int x = 0; x<EXAMPLE_COUNT; ++x) {
98        if (!example_url_seen[x]) {
99            for (int i = 0; i<WWW_COUNT; ++i) {
100                if (is_empty[i]) {
101                    awar_templ[i]->write_string(example[x].templ);
102                    awar_descr[i]->write_string(example[x].descr);
103                    is_empty[i] = false;
104                    break;
105                }
106            }
107        }
108    }
109
110    aw_root->awar_int(AWAR_WWW_SELECT, DEFAULT_SELECT, aw_def);
111    awt_assert(ARB_global_awars_initialized());
112}
113
114GB_ERROR awt_openURL(AW_root *aw_root, GBDATA *gb_main, const char *url) {
115    // if gb_main == 0 -> normal system call is used
116
117    GB_ERROR  error   = 0;
118    GB_CSTR   ka;
119    char     *browser = aw_root->awar(AWAR_WWW_BROWSER)->read_string();
120
121    while ( (ka = GBS_find_string(browser,"$(URL)",0)) ) {
122        char *start       = GB_strpartdup(browser, ka-1);
123        char *new_browser = GBS_global_string_copy("%s%s%s", start, url, ka+6);
124
125        free(start);
126        free(browser);
127
128        browser = new_browser;
129    }
130
131    if (gb_main) {
132        if (GBCMC_system(gb_main, browser)) {
133            error = GB_await_error();
134        }
135    }
136    else {
137        char *command = GBS_global_string_copy("(%s)&", browser);
138        printf("Action: '%s'\n", command);
139        if (system(command)) aw_message(GBS_global_string("'%s' failed", command));
140        free(command);
141    }
142
143    free(browser);
144
145    return error;
146}
147
148
149GB_ERROR awt_open_ACISRT_URL_by_gbd(AW_root *aw_root,GBDATA *gb_main, GBDATA *gbd, const char *name, const char *url_srt){
150    GB_ERROR        error = 0;
151    GB_transaction  tscope(gb_main);
152    char           *url   = GB_command_interpreter(gb_main,name,url_srt,gbd, 0);
153
154    if (!url) error = GB_await_error();
155    else error      = awt_openURL(aw_root, gb_main, url);
156
157    free(url);
158
159    return error;
160}
161
162GB_ERROR awt_openURL_by_gbd(AW_root *aw_root,GBDATA *gb_main, GBDATA *gbd, const char *name){
163    GB_ERROR        error        = 0;
164    GB_transaction  tscope(gb_main);
165    int             url_selected = aw_root->awar(AWAR_WWW_SELECT)->read_int();
166    const char     *awar_url     = GBS_global_string(AWAR_WWW_TEMPLATE,url_selected);
167    char           *url_srt      = aw_root->awar(awar_url)->read_string();
168    error                        = awt_open_ACISRT_URL_by_gbd(aw_root, gb_main, gbd, name, url_srt);
169
170    free(url_srt);
171    return error;
172}
173
174void awt_openDefaultURL_on_species(AW_window *aww,GBDATA *gb_main){
175    GB_transaction  tscope(gb_main);
176    AW_root        *aw_root          = aww->get_root();
177    GB_ERROR        error            = 0;
178    char           *selected_species = aw_root->awar(AWAR_SPECIES_NAME)->read_string();
179    GBDATA         *gb_species       = GBT_find_species(gb_main,selected_species);
180
181    if (!gb_species){
182        error = GB_export_errorf("Cannot find species '%s'",selected_species);
183    }else{
184        error = awt_openURL_by_gbd(aw_root,gb_main, gb_species, selected_species);
185    }
186    if (error) aw_message(error);
187    delete selected_species;
188}
189/* a crazy implementation of a toggle field */
190void awt_www_select_change(AW_window *aww,AW_CL selected){
191    int i;
192    AW_root *aw_root = aww->get_root();
193    for (i=0;i<WWW_COUNT;i++){
194        const char *awar_name = GBS_global_string(AWAR_WWW_SELECT_TEMPLATE,i);
195        aw_root->awar(awar_name)->write_int( (i==selected)? 1:0);
196    }
197    aw_root->awar(AWAR_WWW_SELECT)->write_int(selected);
198}
199
200static void www_init_config(AWT_config_definition& cdef) {
201    for (int i=0;i<WWW_COUNT; i++) {
202        char buf[256];
203        sprintf(buf, AWAR_WWW_SELECT_TEMPLATE, i); cdef.add(buf, "active",     i);
204        sprintf(buf, AWAR_WWW_DESC_TEMPLATE,   i); cdef.add(buf, "desciption", i);
205        sprintf(buf, AWAR_WWW_TEMPLATE,        i); cdef.add(buf, "template",   i);
206    }
207}
208static char *www_store_config(AW_window *aww, AW_CL /*cl1*/, AW_CL /*cl2*/) {
209    AWT_config_definition cdef(aww->get_root());
210    www_init_config(cdef);
211    return cdef.read();
212}
213static void www_restore_config(AW_window *aww, const char *stored_string, AW_CL /*cl1*/, AW_CL /*cl2*/) {
214    AWT_config_definition cdef(aww->get_root());
215    www_init_config(cdef);
216    cdef.write(stored_string);
217}
218
219AW_window *AWT_open_www_window(AW_root *aw_root,AW_CL cgb_main){
220
221    AW_window_simple *aws = new AW_window_simple;
222    aws->init( aw_root, "WWW_PROPS", "WWW");
223    aws->load_xfig("awt/www.fig");
224    aws->auto_space(10,5);
225
226    aws->at("close");
227    aws->callback(AW_POPDOWN);
228    aws->create_button("CLOSE", "CLOSE","C");
229
230    aws->at("help");
231    aws->callback(AW_POPUP_HELP,(AW_CL)"props_www.hlp");
232    aws->create_button("HELP", "HELP","H");
233
234    aws->at("action");
235    aws->callback((AW_CB1)awt_openDefaultURL_on_species,cgb_main);
236    aws->create_button("WWW", "WWW", "W");
237
238    aws->button_length(13);
239    int dummy,closey;
240    aws->at_newline();
241    aws->get_at_position( &dummy,&closey );
242    aws->at_newline();
243
244    aws->create_button(0,"K");
245
246    aws->at_newline();
247
248    int fieldselectx,srtx,descx;
249
250
251    int i;
252    aws->get_at_position( &fieldselectx,&dummy );
253
254    aws->auto_space(10,2);
255
256    for (i=0;i<WWW_COUNT; i++) {
257        char buf[256];
258        sprintf(buf,AWAR_WWW_SELECT_TEMPLATE,i);
259        aws->callback(awt_www_select_change,i);
260        aws->create_toggle(buf);
261
262        sprintf(buf,AWAR_WWW_DESC_TEMPLATE,i);
263        aws->get_at_position( &descx,&dummy );
264        aws->create_input_field(buf,15);
265
266        aws->get_at_position( &srtx,&dummy );
267        sprintf(buf,AWAR_WWW_TEMPLATE,i);
268        aws->create_input_field(buf,80);
269
270        aws->at_newline();
271    }
272    aws->at_newline();
273
274    aws->create_input_field(AWAR_WWW_BROWSER,100);
275
276    aws->at(fieldselectx,closey);
277
278    aws->at_x(fieldselectx);
279    aws->create_button(0,"SEL");
280
281    aws->at_x(descx);
282    aws->create_button(0,"DESCRITPION");
283
284    aws->at_x(srtx);
285    aws->create_button(0,"URL");
286
287    aws->at("config");
288    AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "www", www_store_config, www_restore_config, 0, 0);
289
290    awt_www_select_change(aws,aw_root->awar(AWAR_WWW_SELECT)->read_int());
291    return (AW_window *)aws;
292}
Note: See TracBrowser for help on using the repository browser.