source: tags/arb-6.0/WINDOW/AW_help.cxx

Last change on this file was 12267, checked in by westram, 10 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.0 KB
Line 
1// ================================================================ //
2//                                                                  //
3//   File      : AW_help.cxx                                        //
4//   Purpose   :                                                    //
5//                                                                  //
6//   Institute of Microbiology (Technical University Munich)        //
7//   http://www.arb-home.de/                                        //
8//                                                                  //
9// ================================================================ //
10
11#include "aw_awar.hxx"
12#include "aw_window.hxx"
13#include "aw_edit.hxx"
14#include "aw_root.hxx"
15#include "aw_global_awars.hxx"
16#include "aw_msg.hxx"
17#include "aw_select.hxx"
18
19#include <arb_file.h>
20
21#include <sys/stat.h>
22#include <arb_str.h>
23
24
25#define AWAR_HELP       "tmp/help/"
26#define AWAR_HELPFILE   AWAR_HELP "file"
27#define AWAR_HELPTEXT   AWAR_HELP "text"
28#define AWAR_HELPSEARCH AWAR_HELP "search"
29
30
31void AW_openURL(AW_root *aw_root, const char *url) {
32    GB_CSTR  ka;
33    char    *browser = aw_root->awar(AWAR_WWW_BROWSER)->read_string();
34
35    while ((ka = GBS_find_string(browser, "$(URL)", 0))) {
36        char *start       = GB_strpartdup(browser, ka-1);
37        char *new_browser = GBS_global_string_copy("%s%s%s", start, url, ka+6);
38
39        free(start);
40        free(browser);
41
42        browser = new_browser;
43    }
44
45    char *command = GBS_global_string_copy("(%s)&", browser);
46    printf("Action: '%s'\n", command);
47    if (system(command)) aw_message(GBS_global_string("'%s' failed", command));
48    free(command);
49
50    free(browser);
51}
52
53// --------------------
54//      help window
55
56static struct {
57    AW_selection_list *uplinks;
58    AW_selection_list *links;
59    char              *history;
60} HELP;
61
62static char *get_full_qualified_help_file_name(const char *helpfile, bool path_for_edit = false) {
63    GB_CSTR   result             = 0;
64    char     *user_doc_path      = strdup(GB_getenvDOCPATH());
65    char     *devel_doc_path     = strdup(GB_path_in_ARBHOME("HELP_SOURCE/oldhelp"));
66    size_t    user_doc_path_len  = strlen(user_doc_path);
67    size_t    devel_doc_path_len = strlen(devel_doc_path);
68
69    const char *rel_path = 0;
70    if (strncmp(helpfile, user_doc_path, user_doc_path_len) == 0 && helpfile[user_doc_path_len] == '/') {
71        rel_path = helpfile+user_doc_path_len+1;
72    }
73    else if (strncmp(helpfile, devel_doc_path, devel_doc_path_len) == 0 && helpfile[devel_doc_path_len] == '/') {
74        aw_assert(0);            // when does this happen ? never ?
75        rel_path = helpfile+devel_doc_path_len+1;
76    }
77
78    if (helpfile[0]=='/' && !rel_path) {
79        result = GBS_static_string(helpfile);
80    }
81    else {
82        if (!rel_path) rel_path = helpfile;
83
84        if (rel_path[0]) {
85            if (path_for_edit) {
86#if defined(DEBUG)
87                char *gen_doc_path = strdup(GB_path_in_ARBHOME("HELP_SOURCE/genhelp"));
88
89                char *devel_source = GBS_global_string_copy("%s/%s", devel_doc_path, rel_path);
90                char *gen_source   = GBS_global_string_copy("%s/%s", gen_doc_path, rel_path);
91
92                int devel_size = GB_size_of_file(devel_source);
93                int gen_size   = GB_size_of_file(gen_source);
94
95                aw_assert(devel_size <= 0 || gen_size <= 0); // only one of them shall exist
96
97                if (gen_size>0) {
98                    result = GBS_static_string(gen_source); // edit generated doc
99                }
100                else {
101                    result = GBS_static_string(devel_source); // use normal help source (may be non-existing)
102                }
103
104                free(gen_source);
105                free(devel_source);
106                free(gen_doc_path);
107#else
108                result = GBS_global_string("%s/%s", GB_getenvDOCPATH(), rel_path); // use real help file in RELEASE
109#endif // DEBUG
110            }
111            else {
112                result = GBS_global_string("%s/%s", GB_getenvDOCPATH(), rel_path);
113            }
114        }
115        else {
116            result = "";
117        }
118    }
119
120    free(devel_doc_path);
121    free(user_doc_path);
122
123    return strdup(result);
124}
125
126static char *get_full_qualified_help_file_name(AW_root *awr, bool path_for_edit = false) {
127    char *helpfile = awr->awar(AWAR_HELPFILE)->read_string();
128    char *qualified = get_full_qualified_help_file_name(helpfile, path_for_edit);
129    free(helpfile);
130    return qualified;
131}
132
133static char *get_local_help_url(AW_root *awr) {
134    char   *helpfile          = get_full_qualified_help_file_name(awr, false);
135    char   *user_doc_path     = strdup(GB_getenvDOCPATH());
136    char   *user_htmldoc_path = strdup(GB_getenvHTMLDOCPATH());
137    size_t  user_doc_path_len = strlen(user_doc_path);
138    char   *result            = 0;
139
140    if (strncmp(helpfile, user_doc_path, user_doc_path_len) == 0) { // "real" help file
141        result            = GBS_global_string_copy("%s%s_", user_htmldoc_path, helpfile+user_doc_path_len);
142        size_t result_len = strlen(result);
143
144        aw_assert(result_len > 5);
145
146        if (strcmp(result+result_len-5, ".hlp_") == 0) {
147            strcpy(result+result_len-5, ".html");
148        }
149        else {
150            freenull(result);
151            GB_export_error("Can't browse that file type.");
152        }
153    }
154    else { // on-the-fly-generated help file (e.g. search help)
155        GB_export_error("Can't browse temporary help node");
156    }
157
158    free(user_htmldoc_path);
159    free(user_doc_path);
160    free(helpfile);
161
162    return result;
163}
164
165#if defined(NDEBUG)
166static void store_helpfile_in_tarball(const char *path, const char *mode) {
167    GB_ERROR    error = NULL;
168    const char *base  = GB_path_in_ARBLIB("help");
169
170    if (ARB_strBeginsWith(path, base)) {
171        char *cmd = GBS_global_string_copy("arb_help_useredit.sh %s %s", path+strlen(base)+1, mode);
172        error     = GBK_system(cmd);
173    }
174    else {
175        error = "Unexpected helpfile name (in store_helpfile_in_tarball)";
176    }
177
178    if (error) aw_message(error);
179}
180static void aw_helpfile_modified_cb(const char *path, bool fileWasChanged, bool editorTerminated) {
181    static enum { UNMODIFIED, MODIFIED, NOTIFIED } state = UNMODIFIED;
182
183    if (fileWasChanged) {
184        store_helpfile_in_tarball(path, "end");
185        if (state == UNMODIFIED) state = MODIFIED;
186    }
187    if (editorTerminated) {
188        if (state == MODIFIED) {
189            aw_message("Your changes to ARB help have been stored in an archive.\n"
190                       "See console for what to send to ARB developers!");
191            state = NOTIFIED;
192        }
193    }
194}
195#endif
196
197static void aw_help_edit_help(AW_window *aww) {
198    char *helpfile = get_full_qualified_help_file_name(aww->get_root(), true);
199
200    if (GB_size_of_file(helpfile)<=0) {
201#if defined(NDEBUG)
202        const char *base = GB_path_in_ARBLIB("help");
203#else
204        const char *base = GB_path_in_ARBHOME("HELP_SOURCE/oldhelp");
205#endif
206
207        const char *copy_cmd = GBS_global_string("cp %s/FORM.hlp %s", base, helpfile); // uses_hlp_res("FORM.hlp"); see ../SOURCE_TOOLS/check_ressources.pl@uses_hlp_res
208        aw_message_if(GBK_system(copy_cmd));
209    }
210
211#if defined(NDEBUG)
212    store_helpfile_in_tarball(helpfile, "start");
213
214    if (!GB_is_writeablefile(helpfile)) {
215        aw_message("Warning: you do not have the permission to save changes to that helpfile\n"
216                   "(ask your admin to gain write access)");
217    }
218
219    GBDATA *get_globalawars_gbmain();
220    GBDATA *gbmain = get_globalawars_gbmain(); // hack -- really need main ARB DB here (properties DB does not work with notifications)
221    if (gbmain) {
222        AW_edit(helpfile, aw_helpfile_modified_cb, aww, gbmain);
223    }
224    else {
225        aw_message("Warning: Editing help not possible yet!\n"
226                   "To make it possible:\n"
227                   "- leave help window open,\n"
228                   "- open a database and\n"
229                   "- then click EDIT again.");
230    }
231#else
232    AW_edit(helpfile);
233#endif
234
235    free(helpfile);
236}
237
238static char *aw_ref_to_title(char *ref) {
239    if (!ref) return 0;
240
241    if (GBS_string_matches(ref, "*.ps", GB_IGNORE_CASE)) {   // Postscript file
242        return GBS_global_string_copy("Postscript: %s", ref);
243    }
244
245    char *result = 0;
246    char *file = 0;
247    {
248        char *helpfile = get_full_qualified_help_file_name(ref);
249        file = GB_read_file(helpfile);
250        free(helpfile);
251    }
252
253    if (file) {
254        result = GBS_string_eval(file, "*\nTITLE*\n*=*2:\t=", 0);
255        if (strcmp(file, result)==0) freenull(result);
256        free(file);
257    }
258    else {
259        GB_clear_error();
260    }
261
262    if (result==0) {
263        result = strdup(ref);
264    }
265
266    return result;
267}
268
269static void aw_help_select_newest_in_history(AW_root *aw_root) {
270    char *history = HELP.history;
271    if (history) {
272        const char *sep      = strchr(history, '#');
273        char       *lastHelp = sep ? GB_strpartdup(history, sep-1) : strdup(history);
274
275        aw_root->awar(AWAR_HELPFILE)->write_string(lastHelp);
276        free(lastHelp);
277    }
278}
279
280static void aw_help_back(AW_root *aw_root) {
281    char *history = HELP.history;
282    if (history) {
283        const char *sep = strchr(history, '#');
284        if (sep) {
285            char *first = GB_strpartdup(history, sep-1);
286
287            freeset(HELP.history, GBS_global_string_copy("%s#%s", sep+1, first)); // wrap first to end
288            free(first);
289            aw_help_select_newest_in_history(aw_root);
290        }
291    }
292}
293static void aw_help_back(AW_window *aww) { aw_help_back(aww->get_root()); }
294
295static GB_ERROR aw_help_show_external_format(const char *help_file, const char *viewer) {
296    // Called to show *.ps or *.pdf in external viewer.
297    // Can as well show *.suffix.gz (decompresses to temporary *.suffix)
298
299    struct stat st;
300    GB_ERROR    error = NULL;
301    char        sys[1024];
302
303    sys[0] = 0;
304
305    if (stat(help_file, &st) == 0) { // *.ps exists
306        GBS_global_string_to_buffer(sys, sizeof(sys), "%s %s &", viewer, help_file);
307    }
308    else {
309        char *compressed = GBS_global_string_copy("%s.gz", help_file);
310
311        if (stat(compressed, &st) == 0) { // *.ps.gz exists
312            char *name_ext;
313            GB_split_full_path(compressed, NULL, NULL, &name_ext, NULL);
314            // 'name_ext' contains xxx.ps or xxx.pdf
315            char *name, *suffix;
316            GB_split_full_path(name_ext, NULL, NULL, &name, &suffix);
317
318            char *tempname     = GB_unique_filename(name, suffix);
319            char *uncompressed = GB_create_tempfile(tempname);
320
321            GBS_global_string_to_buffer(sys, sizeof(sys),
322                                        "(gunzip <%s >%s ; %s %s ; rm %s) &",
323                                        compressed, uncompressed,
324                                        viewer, uncompressed,
325                                        uncompressed);
326
327            free(uncompressed);
328            free(tempname);
329            free(name);
330            free(suffix);
331            free(name_ext);
332        }
333        else {
334            error = GBS_global_string("Neither %s nor %s exists", help_file, compressed);
335        }
336        free(compressed);
337    }
338
339    if (sys[0] && !error) error = GBK_system(sys);
340
341    return error;
342}
343
344static void aw_help_helpfile_changed_cb(AW_root *awr) {
345    char *help_file = get_full_qualified_help_file_name(awr);
346
347    if (!strlen(help_file)) {
348        awr->awar(AWAR_HELPTEXT)->write_string("no help");
349    }
350    else if (GBS_string_matches(help_file, "*.ps", GB_IGNORE_CASE)) { // Postscript file
351        GB_ERROR error = aw_help_show_external_format(help_file, GB_getenvARB_GS());
352        if (error) aw_message(error);
353        aw_help_select_newest_in_history(awr);
354    }
355    else if (GBS_string_matches(help_file, "*.pdf", GB_IGNORE_CASE)) { // PDF file
356        GB_ERROR error = aw_help_show_external_format(help_file, GB_getenvARB_PDFVIEW());
357        if (error) aw_message(error);
358        aw_help_select_newest_in_history(awr);
359    }
360    else {
361        if (HELP.history) {
362            if (strncmp(help_file, HELP.history, strlen(help_file)) != 0) {
363                // remove current help from history (if present) and prefix it to history
364                char *comm = GBS_global_string_copy("*#%s*=*1*2:*=%s#*1", help_file, help_file);
365                char *h    = GBS_string_eval(HELP.history, comm, 0);
366
367                aw_assert(h);
368                freeset(HELP.history, h);
369                free(comm);
370            }
371        }
372        else {
373            HELP.history = strdup(help_file);
374        }
375
376        char *helptext = GB_read_file(help_file);
377        if (helptext) {
378            char *ptr;
379            char *h, *h2, *tok;
380
381            ptr = strdup(helptext);
382            HELP.uplinks->clear();
383            h2 = GBS_find_string(ptr, "\nUP", 0);
384            while ((h = h2)) {
385                h2 = GBS_find_string(h2+1, "\nUP", 0);
386                tok = strtok(h+3, " \n\t");  // now I got UP
387                char *title = aw_ref_to_title(tok);
388                if (tok) HELP.uplinks->insert(title, tok);
389                free(title);
390            }
391            free(ptr);
392            HELP.uplinks->insert_default("   ", "");
393            HELP.uplinks->update();
394
395            ptr = strdup(helptext);
396            HELP.links->clear();
397            h2 = GBS_find_string(ptr, "\nSUB", 0);
398            while ((h = h2)) {
399                h2 = GBS_find_string(h2+1, "\nSUB", 0);
400                tok = strtok(h+4, " \n\t");  // now I got SUB
401                char *title = aw_ref_to_title(tok);
402                if (tok) HELP.links->insert(title, tok);
403                free(title);
404            }
405            free(ptr);
406            HELP.links->insert_default("   ", "");
407            HELP.links->update();
408
409            ptr = GBS_find_string(helptext, "TITLE", 0);
410            if (!ptr) ptr = helptext;
411            ptr = GBS_string_eval(ptr, "{*\\:*}=*2", 0);
412
413            awr->awar(AWAR_HELPTEXT)->write_string(ptr);
414            free(ptr);
415            free(helptext);
416        }
417        else {
418            char *msg = GBS_global_string_copy("I cannot find the help file '%s'\n\n"
419                                               "Please help us to complete the ARB-Help by submitting\n"
420                                               "this missing helplink via ARB_NT/File/About/SubmitBug\n"
421                                               "Thank you.\n"
422                                               "\n"
423                                               "Details:\n"
424                                               "%s",
425                                               help_file, GB_await_error());
426            awr->awar(AWAR_HELPTEXT)->write_string(msg);
427            free(msg);
428        }
429    }
430    free(help_file);
431}
432
433static void aw_help_browse(AW_window *aww) {
434    char *help_url = get_local_help_url(aww->get_root());
435    if (help_url) {
436        AW_openURL(aww->get_root(), help_url);
437        free(help_url);
438    }
439    else {
440        aw_message(GBS_global_string("Can't detect URL of help file\n(Reason: %s)", GB_await_error()));
441    }
442}
443
444static void aw_help_search(AW_window *aww) {
445    GB_ERROR  error      = 0;
446    char     *searchtext = aww->get_root()->awar(AWAR_HELPSEARCH)->read_string();
447
448    if (searchtext[0]==0) error = "Empty searchstring";
449    else {
450        char        *helpfilename = 0;
451        static char *last_help; // tempfile containing last search result
452
453        // replace all spaces in 'searchtext' by '.*'
454        freeset(searchtext, GBS_string_eval(searchtext, " =.*", 0));
455
456        // grep .hlp for occurrences of 'searchtext'.
457        // write filenames of matching files into 'helpfilename'
458        {
459            {
460                char *helpname = GB_unique_filename("arb", "hlp");
461                helpfilename   = GB_create_tempfile(helpname);
462                free(helpname);
463            }
464
465            if (!helpfilename) error = GB_await_error();
466            else {
467                const char *gen_help_tmpl = "cd %s;grep -i '^[^#]*%s' `find . -name \"*.hlp\"` | arb_sed -e 'sI:.*IIg' -e 'sI^\\./IIg' | sort | uniq > %s";
468                char       *gen_help_cmd  = GBS_global_string_copy(gen_help_tmpl, GB_getenvDOCPATH(), searchtext, helpfilename);
469
470                error = GBK_system(gen_help_cmd);
471
472                free(gen_help_cmd);
473                GB_remove_on_exit(helpfilename);
474            }
475        }
476
477        if (!error) {
478            char *result       = GB_read_file(helpfilename);
479            if (!result) error = GB_await_error();
480            else {
481                // write temporary helpfile containing links to matches as subtopics
482
483                FILE *helpfp       = fopen(helpfilename, "wt");
484                if (!helpfp) error = GB_export_IO_error("writing helpfile", helpfilename);
485                else {
486                    fprintf(helpfp, "\nUP arb.hlp\n");
487                    if (last_help) fprintf(helpfp, "UP %s\n", last_help);
488                    fputc('\n', helpfp);
489
490                    int   results = 0;
491                    char *rp      = result;
492                    while (1) {
493                        char *eol = strchr(rp, '\n');
494                        if (!eol) {
495                            eol = rp;
496                            while (*eol) ++eol;
497                        }
498                        if (eol>rp) {
499                            char old = eol[0];
500                            eol[0] = 0;
501                            fprintf(helpfp, "SUB %s\n", rp);
502                            results++;
503                            eol[0] = old;
504                        }
505                        if (eol[0]==0) break; // all results inserted
506                        rp = eol+1;
507                    }
508
509                    fprintf(helpfp, "\nTITLE\t\tResult of search for '%s'\n\n", searchtext);
510                    if (results>0)  fprintf(helpfp, "\t\t%i results are shown as subtopics\n",  results);
511                    else            fprintf(helpfp, "\t\tThere are no results.\n");
512
513                    if (results>0) freedup(last_help, helpfilename);
514
515                    fclose(helpfp);
516                    aww->get_root()->awar(AWAR_HELPFILE)->write_string(helpfilename); // display results in aws
517                }
518                free(result);
519            }
520        }
521        free(helpfilename);
522    }
523
524    if (error) aw_message(error);
525
526    free(searchtext);
527}
528
529void AW_help_popup(AW_window *aw, const char *help_file) {
530    static AW_window_simple *aws = 0;
531
532    AW_root *awr = aw->get_root();
533
534    if (!aws) {
535        awr->awar_string(AWAR_HELPTEXT,   "", AW_ROOT_DEFAULT);
536        awr->awar_string(AWAR_HELPSEARCH, "", AW_ROOT_DEFAULT);
537        awr->awar_string(AWAR_HELPFILE,   "", AW_ROOT_DEFAULT);
538        awr->awar(AWAR_HELPFILE)->add_callback(aw_help_helpfile_changed_cb);
539
540        aws = new AW_window_simple;
541        aws->init(awr, "HELP", "HELP WINDOW");
542        aws->load_xfig("help.fig");
543
544        aws->button_length(10);
545
546        aws->at("close");
547        aws->callback((AW_CB0)AW_POPDOWN);
548        aws->create_button("CLOSE", "CLOSE", "C");
549
550        aws->at("back");
551        aws->callback(aw_help_back);
552        aws->create_button("BACK", "BACK", "B");
553
554
555        aws->at("super");
556        HELP.uplinks = aws->create_selection_list(AWAR_HELPFILE, false);
557        HELP.uplinks->insert_default("   ", "");
558        HELP.uplinks->update();
559
560        aws->at("sub");
561        HELP.links = aws->create_selection_list(AWAR_HELPFILE, false);
562        HELP.links->insert_default("   ", "");
563        HELP.links->update();
564        HELP.history = 0;
565
566        aws->at("text");
567        aws->create_text_field(AWAR_HELPTEXT, 3, 3);
568
569        aws->at("browse");
570        aws->callback(aw_help_browse);
571        aws->create_button("BROWSE", "BROWSE", "B");
572
573        aws->at("expression");
574        aws->create_input_field(AWAR_HELPSEARCH);
575
576        aws->at("search");
577        aws->callback(aw_help_search);
578        aws->create_button("SEARCH", "SEARCH", "S");
579
580        aws->at("edit");
581        aws->callback(aw_help_edit_help);
582        aws->create_button("EDIT", "EDIT", "E");
583
584    }
585
586    aw_assert(help_file);
587
588    awr->awar(AWAR_HELPFILE)->write_string(help_file);
589
590    if (!GBS_string_matches(help_file, "*.ps", GB_IGNORE_CASE) &&
591        !GBS_string_matches(help_file, "*.pdf", GB_IGNORE_CASE))
592    { // don't open help if postscript or pdf file
593        aws->activate();
594    }
595}
596
597
598
Note: See TracBrowser for help on using the repository browser.