1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * gimp-help-lookup - a standalone gimp-help ID to filename mapper
5  * Copyright (C)  2004-2008 Sven Neumann <sven@gimp.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "libgimp/gimp.h"
27 
28 #include "gimphelp.h"
29 
30 
31 static void               show_version (void) G_GNUC_NORETURN;
32 
33 static gchar            * lookup       (const gchar *help_domain,
34                                         const gchar *help_locales,
35                                         const gchar *help_id);
36 
37 static GimpHelpProgress * progress_new (void);
38 
39 
40 static const gchar  *help_base    = NULL;
41 static       gchar  *help_root    = NULL;
42 static const gchar  *help_locales = NULL;
43 static const gchar **help_ids     = NULL;
44 
45 static gboolean      be_verbose   = FALSE;
46 
47 
48 static const GOptionEntry entries[] =
49 {
50   { "version", 'v', 0,
51     G_OPTION_ARG_CALLBACK, (GOptionArgFunc) show_version,
52     "Show version information and exit", NULL
53   },
54   { "base", 'b', 0,
55     G_OPTION_ARG_STRING, &help_base,
56     "Specifies base URI", "URI"
57   },
58   { "root", 'r', 0,
59     G_OPTION_ARG_FILENAME, &help_root,
60     "Specifies root directory for index files", "DIR"
61   },
62   { "lang", 'l', 0,
63     G_OPTION_ARG_STRING, &help_locales,
64     "Specifies help language", "LANG"
65   },
66   {
67     "verbose", 0, 0,
68     G_OPTION_ARG_NONE, &be_verbose,
69     "Be more verbose", NULL
70   },
71   {
72     G_OPTION_REMAINING, 0, 0,
73     G_OPTION_ARG_STRING_ARRAY, &help_ids,
74     NULL, NULL
75   },
76   { NULL }
77 };
78 
79 
80 gint
main(gint argc,gchar * argv[])81 main (gint   argc,
82       gchar *argv[])
83 {
84   GOptionContext *context;
85   gchar          *uri;
86   GError         *error = NULL;
87 
88   help_base = g_getenv (GIMP_HELP_ENV_URI);
89   help_root = g_build_filename (gimp_data_directory (), GIMP_HELP_PREFIX, NULL);
90 
91   context = g_option_context_new ("HELP-ID");
92   g_option_context_add_main_entries (context, entries, NULL);
93 
94   if (! g_option_context_parse (context, &argc, &argv, &error))
95     {
96       g_print ("%s\n", error->message);
97       g_error_free (error);
98       return EXIT_FAILURE;
99     }
100 
101   if (help_base)
102     uri = g_strdup (help_base);
103   else
104     uri = g_filename_to_uri (help_root, NULL, NULL);
105 
106   gimp_help_register_domain (GIMP_HELP_DEFAULT_DOMAIN, uri);
107   g_free (uri);
108 
109   uri = lookup (GIMP_HELP_DEFAULT_DOMAIN,
110                 help_locales ? help_locales : GIMP_HELP_DEFAULT_LOCALE,
111                 help_ids     ? help_ids[0]  : GIMP_HELP_DEFAULT_ID);
112 
113   if (uri)
114     {
115       g_print ("%s\n", uri);
116       g_free (uri);
117     }
118 
119   g_option_context_free (context);
120   g_free (help_root);
121 
122   return uri ? EXIT_SUCCESS : EXIT_FAILURE;
123 }
124 
125 static gchar *
lookup(const gchar * help_domain,const gchar * help_locales,const gchar * help_id)126 lookup (const gchar *help_domain,
127         const gchar *help_locales,
128         const gchar *help_id)
129 {
130   GimpHelpDomain *domain = gimp_help_lookup_domain (help_domain);
131 
132   if (domain)
133     {
134       GimpHelpProgress *progress = progress_new ();
135       GList            *locales;
136       gchar            *full_uri;
137 
138       locales  = gimp_help_parse_locales (help_locales);
139       full_uri = gimp_help_domain_map (domain, locales, help_id, progress,
140                                        NULL, NULL);
141 
142       gimp_help_progress_free (progress);
143 
144       g_list_free_full (locales, (GDestroyNotify) g_free);
145 
146       return full_uri;
147     }
148 
149   return NULL;
150 }
151 
152 static void
show_version(void)153 show_version (void)
154 {
155   g_print ("gimp-help-lookup version %s\n", GIMP_VERSION);
156   exit (EXIT_SUCCESS);
157 }
158 
159 
160 static void
progress_start(const gchar * message,gboolean cancelable,gpointer user_data)161 progress_start (const gchar *message,
162                 gboolean     cancelable,
163                 gpointer     user_data)
164 {
165   if (be_verbose)
166     g_printerr ("\n%s\n", message);
167 }
168 
169 static void
progress_end(gpointer user_data)170 progress_end (gpointer user_data)
171 {
172   if (be_verbose)
173     g_printerr ("done\n");
174 }
175 
176 static void
progress_set_value(gdouble percentage,gpointer user_data)177 progress_set_value (gdouble  percentage,
178                     gpointer user_data)
179 {
180   if (be_verbose)
181     g_printerr (".");
182 }
183 
184 static GimpHelpProgress *
progress_new(void)185 progress_new (void)
186 {
187   const GimpHelpProgressVTable vtable =
188     {
189       progress_start,
190       progress_end,
191       progress_set_value
192     };
193 
194   return gimp_help_progress_new (&vtable, NULL);
195 }
196