ARB
AWT_www.cxx
Go to the documentation of this file.
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 
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 
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);
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 
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 
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 
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 
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 
265  return aws;
266 }
267 
268 void AWT_openURL(AW_window *aww, const char *url) {
269  AW_openURL(aww->get_root(), url);
270 }
271 
const char * GB_ERROR
Definition: arb_core.h:25
void add(const char *awar_name, const char *config_name)
void load_xfig(const char *file, bool resize=true)
Definition: AW_window.cxx:720
int ARB_stricmp(const char *s1, const char *s2)
Definition: arb_str.h:28
#define awt_assert(cond)
Definition: awt.hxx:28
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)
GB_ERROR awt_open_ACI_URL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item, const char *url_aci)
Definition: AWT_www.cxx:120
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
#define ARRAY_ELEMS(array)
Definition: arb_defs.h:19
static void www_setup_config(AWT_config_definition &cdef)
Definition: AWT_www.cxx:185
static void awt_www_select_change(AW_window *aww, int selected)
Definition: AWT_www.cxx:176
const char * read_char_pntr() const
Definition: AW_awar.cxx:168
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
char * extract_url_host(const char *templ)
Definition: AWT_www.cxx:31
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
AW_window * AWT_create_www_window(AW_root *aw_root, GBDATA *gb_main)
Definition: AWT_www.cxx:194
void awt_create_aww_vars(AW_root *aw_root, AW_default aw_def)
Definition: AWT_www.cxx:54
#define WWW_COUNT
Definition: AWT_www.cxx:25
#define AWAR_WWW_SELECT_TEMPLATE
Definition: AWT_www.cxx:27
static void error(const char *msg)
Definition: mkptypes.cxx:96
static void awt_openDefaultURL_on_selected_species(AW_window *aww, GBDATA *gb_main)
Definition: AWT_www.cxx:159
#define AWAR_SPECIES_NAME
GB_ERROR awt_openDefaultURL_with_item(AW_root *aw_root, GBDATA *gb_main, GBDATA *gb_item)
Definition: AWT_www.cxx:148
char * read_string() const
Definition: AW_awar.cxx:198
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:580
bool url_host_matches(const char *templ1, const char *templ2)
Definition: AWT_www.cxx:42
#define AWAR_WWW_TEMPLATE
Definition: AWT_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
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 AWT_openURL(AW_window *aww, const char *url)
Definition: AWT_www.cxx:268
void AW_openURL(AW_root *aw_root, const char *url)
Definition: AW_help.cxx:35
#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
#define AWAR_WWW_SELECT
Definition: AWT_www.cxx:26
#define AWAR_WWW_DESC_TEMPLATE
Definition: AWT_www.cxx:29