Debian Codegraph @

gdraw/ghelp.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #include "gfile.h"
31 #include "ustring.h"
32
33 #include <gio/gio.h>
34 #include <stdbool.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 static char* SupportedLocale(const char *locale, const char *fullspec, const char *filename) {
39 /* If there's additional help files written for other languages, then check */
40 /* to see if this local matches the additional help message language. If so */
41 /* then report back that there's another language available to use for help */
42 /* NOTE: If Docs are not maintained very well, maybe comment-out lang here. */
43 int i;
44 /* list languages in specific to generic order, ie: en_CA, en_GB, en... */
45 static const char *supported[] = { "de","ja", NULL }; /* other html lang list */
46
47 for ( i=0; supported[i]!=NULL; ++i ) {
48 if ( strcmp(locale,supported[i])==0 ) {
49 const char *pt = strrchr(filename, '/');
50 return smprintf("%s/old/%s/%s", fullspec, supported[i], pt ? pt : filename);
51 }
52 }
53 return NULL;
54 }
55
56 static char* CheckSupportedLocale(const char *fullspec, const char *filename) {
57 /* Add Browser HELP for this local if there's more html docs for this local */
58
59 /* KANOU has provided a japanese translation of the docs */
60 /* Edward Lee is working on traditional chinese docs */
61 const char *loc = getenv("LC_ALL");
62 char buffer[40], *pt;
63
64 if ( loc==NULL ) loc = getenv("LC_CTYPE");
65 if ( loc==NULL ) loc = getenv("LANG");
66 if ( loc==NULL ) loc = getenv("LC_MESSAGES");
67 if ( loc==NULL )
68 return NULL;
69
70 /* first, try checking entire string */
71 strncpy(buffer,loc,sizeof(buffer));
72 buffer[sizeof(buffer)-1] = '\0';
73 pt = SupportedLocale(buffer, fullspec, filename);
74 if (pt) {
75 return pt;
76 }
77
78 /* parse possible suffixes, such as .UTF-8, then try again */
79 if ( (pt=strchr(buffer,'.'))!=NULL ) {
80 *pt = '\0';
81 pt = SupportedLocale(buffer, fullspec, filename);
82 if (pt) {
83 return pt;
84 }
85 }
86
87 /* parse possible suffixes such as _CA, _GB, and try again */
88 if ( (pt=strchr(buffer,'_'))!=NULL ) {
89 *pt = '\0';
90 return SupportedLocale(buffer, fullspec, filename);
91 }
92
93 return NULL;
94 }
95
96 void help(const char *file, const char *section) {
97 if (!file) {
98 return;
99 } else if (strstr(file, "://")) {
100 g_app_info_launch_default_for_uri(file, NULL, NULL);
101 return;
102 } else if (!section) {
103 section = "";
104 }
105
106 bool launched = false;
107 const char *helpdir = getHelpDir();
108 if (helpdir) {
109 char *path = CheckSupportedLocale(helpdir, file);
110 if (!path) {
111 path = smprintf("%s/%s", helpdir, file);
112 if (!path) {
113 return;
114 }
115 }
116
117 GFile *gfile = g_file_new_for_path(path);
118 free(path);
119
120 if (g_file_query_exists(gfile, NULL)) {
121 gchar* uri = g_file_get_uri(gfile);
122 path = smprintf("%s%s", uri, section);
123 launched = g_app_info_launch_default_for_uri(path, NULL, NULL);
124 g_free(uri);
125 free(path);
126 }
127 g_object_unref(gfile);
128 }
129
130 if (!launched) {
131 char *path = smprintf("https://fontforge.org/docs/%s%s", file, section);
132 g_app_info_launch_default_for_uri(path, NULL, NULL);
133 free(path);
134 }
135 }