]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
Let query utilities update the cache file directly
[~andy/gtk] / gtk / queryimmodules.c
1 /* GTK+
2  * querymodules.c:
3  *
4  * Copyright (C) 2000-2010 Red Hat Software
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23
24 #include <glib.h>
25 #include <glib/gprintf.h>
26 #include <gmodule.h>
27
28 #include <errno.h>
29 #include <string.h>
30 #ifdef HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33
34 #ifdef USE_LA_MODULES
35 #define SOEXT ".la"
36 #else
37 #define SOEXT ("." G_MODULE_SUFFIX)
38 #endif
39
40 #include "gtk/gtkrc.h"
41 #include "gtk/gtkimmodule.h"
42 #include "gtk/gtkversion.h"
43
44 static void
45 escape_string (GString *contents, const char *str)
46 {
47   while (TRUE)
48     {
49       char c = *str++;
50
51       switch (c)
52         {
53         case '\0':
54           goto done;
55         case '\n':
56           g_string_append (contents, "\\n");
57           break;
58         case '\"':
59           g_string_append (contents, "\\\"");
60           break;
61 #ifdef G_OS_WIN32
62                 /* Replace backslashes in path with forward slashes, so that
63                  * it reads in without problems.
64                  */
65         case '\\':
66           g_string_append (contents, "/");
67           break;
68 #endif
69         default:
70           g_string_append_c (contents, c);
71         }
72     }
73
74  done:;
75 }
76
77 static void
78 print_escaped (GString *contents, const char *str)
79 {
80   g_string_append_c (contents, '"');
81   escape_string (contents, str);
82   g_string_append_c (contents, '"');
83   g_string_append_c (contents, ' ');
84 }
85
86 static gboolean
87 query_module (const char *dir, const char *name, GString *contents)
88 {
89   void          (*list)   (const GtkIMContextInfo ***contexts,
90                            guint                    *n_contexts);
91   void          (*init)   (GTypeModule              *type_module);
92   void          (*exit)   (void);
93   GtkIMContext *(*create) (const gchar             *context_id);
94
95   gpointer list_ptr;
96   gpointer init_ptr;
97   gpointer exit_ptr;
98   gpointer create_ptr;
99
100   GModule *module;
101   gchar *path;
102   gboolean error = FALSE;
103
104   if (g_path_is_absolute (name))
105     path = g_strdup (name);
106   else
107     path = g_build_filename (dir, name, NULL);
108
109   module = g_module_open (path, 0);
110
111   if (!module)
112     {
113       g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
114       error = TRUE;
115     }
116
117   if (module &&
118       g_module_symbol (module, "im_module_list", &list_ptr) &&
119       g_module_symbol (module, "im_module_init", &init_ptr) &&
120       g_module_symbol (module, "im_module_exit", &exit_ptr) &&
121       g_module_symbol (module, "im_module_create", &create_ptr))
122     {
123       const GtkIMContextInfo **contexts;
124       guint n_contexts;
125       int i;
126
127       list = list_ptr;
128       init = init_ptr;
129       exit = exit_ptr;
130       create = create_ptr;
131
132       print_escaped (contents, path);
133       g_string_append_c (contents, '\n');
134
135       (*list) (&contexts, &n_contexts);
136
137       for (i = 0; i < n_contexts; i++)
138         {
139           print_escaped (contents, contexts[i]->context_id);
140           print_escaped (contents, contexts[i]->context_name);
141           print_escaped (contents, contexts[i]->domain);
142           print_escaped (contents, contexts[i]->domain_dirname);
143           print_escaped (contents, contexts[i]->default_locales);
144           g_string_append_c (contents, '\n');
145         }
146       g_string_append_c (contents, '\n');
147     }
148   else
149     {
150       g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
151                  g_module_error ());
152       error = TRUE;
153     }
154
155   g_free (path);
156   if (module)
157     g_module_close (module);
158
159   return error;
160 }
161
162 int main (int argc, char **argv)
163 {
164   char *cwd;
165   int i;
166   char *path;
167   gboolean error = FALSE;
168   gchar *cache_file = NULL;
169   gint first_file = 1;
170   GString *contents;
171
172   if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
173     {
174       cache_file = gtk_rc_get_im_module_file ();
175       first_file = 2;
176     }
177
178   contents = g_string_new ("");
179   g_string_append_printf (contents,
180                           "# GTK+ Input Method Modules file\n"
181                           "# Automatically generated file, do not edit\n"
182                           "# Created by %s from gtk+-%d.%d.%d\n"
183                           "#\n",
184                           argv[0],
185                           GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
186
187   if (argc == first_file)  /* No file arguments given */
188     {
189       char **dirs;
190       int i;
191       GHashTable *dirs_done;
192
193       path = gtk_rc_get_im_module_path ();
194
195       g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
196
197       dirs = pango_split_file_list (path);
198       dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
199
200       for (i = 0; dirs[i]; i++)
201         if (!g_hash_table_lookup (dirs_done, dirs[i]))
202           {
203             GDir *dir = g_dir_open (dirs[i], 0, NULL);
204             if (dir)
205               {
206                 const char *dent;
207
208                 while ((dent = g_dir_read_name (dir)))
209                   {
210                     if (g_str_has_suffix (dent, SOEXT))
211                       error |= query_module (dirs[i], dent, contents);
212                   }
213
214                 g_dir_close (dir);
215               }
216
217             g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
218           }
219
220       g_hash_table_destroy (dirs_done);
221     }
222   else
223     {
224       cwd = g_get_current_dir ();
225
226       for (i = first_file; i < argc; i++)
227         error |= query_module (cwd, argv[i], contents);
228
229       g_free (cwd);
230     }
231
232   if (!error)
233     {
234       if (cache_file)
235         {
236           GError *err;
237
238           err = NULL;
239           if (!g_file_set_contents (cache_file, contents->str, -1, &err))
240             {
241                 g_fprintf (stderr, "%s\n", err->message);
242                 error = 1;
243             }
244         }
245       else
246         g_print ("%s\n", contents->str);
247     }
248
249   return error ? 1 : 0;
250 }