ARB
www.cxx
Go to the documentation of this file.
1 // ================================================================ //
2 // //
3 // File : www.cxx //
4 // Purpose : item specific web lookup //
5 // //
6 // Institute of Microbiology (Technical University Munich) //
7 // http://www.arb-home.de/ //
8 // //
9 // ================================================================ //
10 
11 #include "config_manager.hxx"
12 
13 #include <aw_window.hxx>
14 #include <aw_global_awars.hxx>
15 #include <aw_awars.hxx>
16 #include <aw_root.hxx>
17 #include <aw_msg.hxx>
18 
19 #include <arbdbt.h>
20 #include <gb_aci.h>
21 
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 ARB_strpartdup(host_start, slash-1);
37  }
38  return NULp;
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  { "Wikipedia example", "\"http://en.wikipedia.org/wiki/Special:Search?search=\";dd" }
62  }, empty = { "", "" };
63 
64  const int DEFAULT_SELECT = 1; // SILVA
65  const int EXAMPLE_COUNT = ARRAY_ELEMS(example);
66  STATIC_ASSERT(EXAMPLE_COUNT <= WWW_COUNT);
67 
68  bool example_url_seen[EXAMPLE_COUNT];
69  for (int x = 0; x<EXAMPLE_COUNT; ++x) example_url_seen[x] = false;
70 
71  AW_awar *awar_templ[WWW_COUNT];
72  AW_awar *awar_descr[WWW_COUNT];
73  bool is_empty[WWW_COUNT];
74 
75  for (int i = 0; i<WWW_COUNT; ++i) {
76  const Example& curr = i<EXAMPLE_COUNT ? example[i] : empty;
77  const char *awar_name;
78 
79  awar_name = GBS_global_string(AWAR_WWW_TEMPLATE, i);
80  awar_templ[i] = aw_root->awar_string(awar_name, curr.templ, aw_def);
81 
83  awar_descr[i] = aw_root->awar_string(awar_name, curr.descr, aw_def);
84 
85  const char *templ = awar_templ[i]->read_char_pntr();
86  const char *descr = awar_descr[i]->read_char_pntr();
87 
88  is_empty[i] = !templ[0] && !descr[0];
89  if (!is_empty[i]) {
90  for (int x = 0; x<EXAMPLE_COUNT; ++x) {
91  if (!example_url_seen[x]) {
92  example_url_seen[x] = url_host_matches(templ, example[x].templ);
93  }
94  }
95  }
96 
98  aw_root->awar_int(awar_name, 0, aw_def);
99  }
100 
101  // insert missing examples
102  for (int x = 0; x<EXAMPLE_COUNT; ++x) {
103  if (!example_url_seen[x]) {
104  for (int i = 0; i<WWW_COUNT; ++i) {
105  if (is_empty[i]) {
106  awar_templ[i]->write_string(example[x].templ);
107  awar_descr[i]->write_string(example[x].descr);
108  is_empty[i] = false;
109  break;
110  }
111  }
112  }
113  }
114 
115  aw_root->awar_int(AWAR_WWW_SELECT, DEFAULT_SELECT, aw_def);
117 }
118 
119 GB_ERROR awt_open_ACI_URL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item, const char *url_aci) {
120  GB_ERROR error = NULp;
121  GB_transaction tscope(gb_main);
122 
123  const char *item_name = NULp; // name of database item (used as input for ACI)
124  if (gb_item) {
125  GBDATA *gb_name = GB_entry(gb_item, "group_name");
126  if (!gb_name) gb_name = GB_entry(gb_item, "full_name");
127  if (!gb_name) gb_name = GB_entry(gb_item, "name");
128 
129  item_name = gb_name ? GB_read_char_pntr(gb_name) : "NoNameDetected";
130  }
131  else {
132  item_name = "NoItemSelected";
133  }
134 
135  GBL_env env(gb_main, NULp); // @@@ pass from a caller who knows the tree?
136  GBL_call_env callEnv(gb_item, env);
137  char *url = GB_command_interpreter_in_env(item_name, url_aci, callEnv);
138 
139  if (!url) error = GB_await_error();
140  else AW_openURL(aw_root, url);
141 
142  free(url);
143 
144  return error;
145 }
146 
148  GB_transaction tscope(gb_main);
149  int url_selected = aw_root->awar(AWAR_WWW_SELECT)->read_int();
150  const char *awar_urlaci = GBS_global_string(AWAR_WWW_TEMPLATE, url_selected);
151  char *url_aci = aw_root->awar(awar_urlaci)->read_string();
152  GB_ERROR error = awt_open_ACI_URL_with_item(aw_root, gb_main, gb_item, url_aci);
153 
154  free(url_aci);
155  return error;
156 }
157 
159  GB_transaction tscope(gb_main);
160  AW_root *aw_root = aww->get_root();
161  GB_ERROR error = NULp;
162  char *selected_species = aw_root->awar(AWAR_SPECIES_NAME)->read_string();
163  GBDATA *gb_species = GBT_find_species(gb_main, selected_species);
164 
165  if (!gb_species) {
166  error = GBS_global_string("Cannot find species '%s'", selected_species);
167  }
168  else {
169  error = awt_openDefaultURL_with_item(aw_root, gb_main, gb_species);
170  }
171  if (error) aw_message(error);
172  delete selected_species;
173 }
174 
175 static void awt_www_select_change(AW_window *aww, int selected) {
176  AW_root *aw_root = aww->get_root();
177  for (int i=0; i<WWW_COUNT; i++) {
178  const char *awar_name = GBS_global_string(AWAR_WWW_SELECT_TEMPLATE, i);
179  aw_root->awar(awar_name)->write_int((i==selected) ? 1 : 0);
180  }
181  aw_root->awar(AWAR_WWW_SELECT)->write_int(selected);
182 }
183 
185  for (int i=0; i<WWW_COUNT; i++) {
186  char buf[256];
187  sprintf(buf, AWAR_WWW_SELECT_TEMPLATE, i); cdef.add(buf, "active", i);
188  sprintf(buf, AWAR_WWW_DESC_TEMPLATE, i); cdef.add(buf, "description", i);
189  sprintf(buf, AWAR_WWW_TEMPLATE, i); cdef.add(buf, "template", i);
190  }
191 }
192 
194  AW_window_simple *aws = new AW_window_simple;
195  aws->init(aw_root, "WWW_PROPS", "WWW");
196  aws->load_xfig("awt/www.fig");
197  aws->auto_space(10, 5);
198 
199  aws->at("close");
200  aws->callback(AW_POPDOWN);
201  aws->create_button("CLOSE", "CLOSE", "C");
202 
203  aws->at("help");
204  aws->callback(makeHelpCallback("props_www.hlp"));
205  aws->create_button("HELP", "HELP", "H");
206 
207  aws->at("action");
208  aws->callback(makeWindowCallback(awt_openDefaultURL_on_selected_species, gb_main));
209  aws->create_button("WWW", "WWW", "W");
210 
211  aws->button_length(13);
212  int dummy, closey;
213  aws->at_newline();
214  aws->get_at_position(&dummy, &closey);
215  aws->at_newline();
216 
217  aws->create_button(NULp, "K");
218 
219  aws->at_newline();
220 
221  int fieldselectx, srtx, descx;
222 
223 
224  int i;
225  aws->get_at_position(&fieldselectx, &dummy);
226 
227  aws->auto_space(10, 2);
228 
229  for (i=0; i<WWW_COUNT; i++) {
230  char buf[256];
231  sprintf(buf, AWAR_WWW_SELECT_TEMPLATE, i);
232  aws->callback(makeWindowCallback(awt_www_select_change, i)); // @@@ used as TOGGLE_CLICK_CB (see #559)
233  aws->create_toggle(buf);
234 
235  sprintf(buf, AWAR_WWW_DESC_TEMPLATE, i);
236  aws->get_at_position(&descx, &dummy);
237  aws->create_input_field(buf, 15);
238 
239  aws->get_at_position(&srtx, &dummy);
240  sprintf(buf, AWAR_WWW_TEMPLATE, i);
241  aws->create_input_field(buf, 80);
242 
243  aws->at_newline();
244  }
245  aws->at_newline();
246 
247  aws->create_input_field(AWAR_WWW_BROWSER, 100);
248 
249  aws->at(fieldselectx, closey);
250 
251  aws->at_x(fieldselectx);
252  aws->create_button(NULp, "SEL");
253 
254  aws->at_x(descx);
255  aws->create_button(NULp, "DESCRIPTION");
256 
257  aws->at_x(srtx);
258  aws->create_button(NULp, "URL");
259 
260  aws->at("config");
261  AWT_insert_config_manager(aws, AW_ROOT_DEFAULT, "www", makeConfigSetupCallback(www_setup_config));
262 
264  return aws;
265 }
266 
267 void AWT_openURL(AW_window *aww, const char *url) {
268  AW_openURL(aww->get_root(), url);
269 }
270 
static void www_setup_config(AWT_config_definition &cdef)
Definition: www.cxx:184
#define awt_assert(cond)
Definition: TreeAwars.cxx:26
const char * GB_ERROR
Definition: arb_core.h:25
void add(const char *awar_name, const char *config_name)
void AWT_insert_config_manager(AW_window *aww, AW_default default_file_, const char *id, const StoreConfigCallback &store_cb, const RestoreConfigCallback &load_or_reset_cb, const char *macro_id, const AWT_predefined_config *predef)
#define AWAR_WWW_TEMPLATE
Definition: www.cxx:27
void load_xfig(const char *file, bool resize=true)
Definition: AW_window.cxx:720
GB_ERROR awt_open_ACI_URL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item, const char *url_aci)
Definition: www.cxx:119
char * extract_url_host(const char *templ)
Definition: www.cxx:30
int ARB_stricmp(const char *s1, const char *s2)
Definition: arb_str.h:28
long read_int() const
Definition: AW_awar.cxx:184
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
char * ARB_strpartdup(const char *start, const char *end)
Definition: arb_string.h:51
bool url_host_matches(const char *templ1, const char *templ2)
Definition: www.cxx:41
static void awt_openDefaultURL_on_selected_species(AW_window *aww, GBDATA *gb_main)
Definition: www.cxx:158
#define ARRAY_ELEMS(array)
Definition: arb_defs.h:19
static void awt_www_select_change(AW_window *aww, int selected)
Definition: www.cxx:175
const char * read_char_pntr() const
Definition: AW_awar.cxx:168
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
#define AWAR_WWW_SELECT
Definition: www.cxx:25
#define AWAR_WWW_SELECT_TEMPLATE
Definition: www.cxx:26
AW_window * AWT_create_www_window(AW_root *aw_root, GBDATA *gb_main)
Definition: www.cxx:193
static void error(const char *msg)
Definition: mkptypes.cxx:96
void awt_create_aww_vars(AW_root *aw_root, AW_default aw_def)
Definition: www.cxx:53
#define AWAR_SPECIES_NAME
char * read_string() const
Definition: AW_awar.cxx:198
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
GB_ERROR awt_openDefaultURL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item)
Definition: www.cxx:147
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:580
#define AWAR_WWW_DESC_TEMPLATE
Definition: www.cxx:28
bool ARB_global_awars_initialized()
void aw_message(const char *msg)
Definition: AW_status.cxx:1142
AW_root * get_root()
Definition: aw_window.hxx:359
#define NULp
Definition: cxxforward.h:116
GBDATA * GBT_find_species(GBDATA *gb_main, const char *name)
Definition: aditem.cxx:139
#define AWAR_WWW_BROWSER
GB_ERROR write_string(const char *aw_string)
NOT4PERL char * GB_command_interpreter_in_env(const char *str, const char *commands, const GBL_call_env &callEnv)
Definition: gb_aci.cxx:361
GB_CSTR GB_read_char_pntr(GBDATA *gbd)
Definition: arbdb.cxx:904
GBDATA * gb_main
Definition: adname.cxx:32
#define WWW_COUNT
Definition: www.cxx:24
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
void AW_openURL(AW_root *aw_root, const char *url)
Definition: AW_help.cxx:53
#define STATIC_ASSERT(const_expression)
Definition: static_assert.h:37
#define AW_ROOT_DEFAULT
Definition: aw_base.hxx:106
GB_ERROR write_int(long aw_int)
GBDATA * GB_entry(GBDATA *father, const char *key)
Definition: adquery.cxx:334
void AWT_openURL(AW_window *aww, const char *url)
Definition: www.cxx:267