]> Pileus Git - ~andy/gtk/blob - gtk/queryimmodules.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "config.h"
21
22 #include <glib.h>
23 #include <glib/gprintf.h>
24
25 #include <errno.h>
26 #include <string.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #ifdef USE_LA_MODULES
32 #define SOEXT ".la"
33 #else
34 #define SOEXT ("." G_MODULE_SUFFIX)
35 #endif
36
37 #include "gtk/gtkimcontextinfo.h"
38 #include "gtk/gtkversion.h"
39
40 #define GDK_DISABLE_DEPRECATION_WARNINGS
41
42 #include "gtk/deprecated/gtkrc.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
92   gpointer list_ptr;
93   gpointer init_ptr;
94   gpointer exit_ptr;
95   gpointer create_ptr;
96
97   GModule *module;
98   gchar *path;
99   gboolean error = FALSE;
100
101   if (g_path_is_absolute (name))
102     path = g_strdup (name);
103   else
104     path = g_build_filename (dir, name, NULL);
105
106   module = g_module_open (path, 0);
107
108   if (!module)
109     {
110       g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
111       error = TRUE;
112     }
113
114   if (module &&
115       g_module_symbol (module, "im_module_list", &list_ptr) &&
116       g_module_symbol (module, "im_module_init", &init_ptr) &&
117       g_module_symbol (module, "im_module_exit", &exit_ptr) &&
118       g_module_symbol (module, "im_module_create", &create_ptr))
119     {
120       const GtkIMContextInfo **contexts;
121       guint n_contexts;
122       int i;
123
124       list = list_ptr;
125
126       print_escaped (contents, path);
127       g_string_append_c (contents, '\n');
128
129       (*list) (&contexts, &n_contexts);
130
131       for (i = 0; i < n_contexts; i++)
132         {
133           print_escaped (contents, contexts[i]->context_id);
134           print_escaped (contents, contexts[i]->context_name);
135           print_escaped (contents, contexts[i]->domain);
136           print_escaped (contents, contexts[i]->domain_dirname);
137           print_escaped (contents, contexts[i]->default_locales);
138           g_string_append_c (contents, '\n');
139         }
140       g_string_append_c (contents, '\n');
141     }
142   else
143     {
144       g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
145                  g_module_error ());
146       error = TRUE;
147     }
148
149   g_free (path);
150   if (module)
151     g_module_close (module);
152
153   return error;
154 }
155
156 int main (int argc, char **argv)
157 {
158   char *cwd;
159   int i;
160   char *path;
161   gboolean error = FALSE;
162   gchar *cache_file = NULL;
163   gint first_file = 1;
164   GString *contents;
165
166   if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
167     {
168       cache_file = gtk_rc_get_im_module_file ();
169       first_file = 2;
170     }
171
172   contents = g_string_new ("");
173   g_string_append_printf (contents,
174                           "# GTK+ Input Method Modules file\n"
175                           "# Automatically generated file, do not edit\n"
176                           "# Created by %s from gtk+-%d.%d.%d\n"
177                           "#\n",
178                           argv[0],
179                           GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
180
181   if (argc == first_file)  /* No file arguments given */
182     {
183       char **dirs;
184       int i;
185       GHashTable *dirs_done;
186
187       path = gtk_rc_get_im_module_path ();
188
189       g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
190
191       dirs = pango_split_file_list (path);
192       dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
193
194       for (i = 0; dirs[i]; i++)
195         if (!g_hash_table_lookup (dirs_done, dirs[i]))
196           {
197             GDir *dir = g_dir_open (dirs[i], 0, NULL);
198             if (dir)
199               {
200                 const char *dent;
201
202                 while ((dent = g_dir_read_name (dir)))
203                   {
204                     if (g_str_has_suffix (dent, SOEXT))
205                       error |= query_module (dirs[i], dent, contents);
206                   }
207
208                 g_dir_close (dir);
209               }
210
211             g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
212           }
213
214       g_hash_table_destroy (dirs_done);
215     }
216   else
217     {
218       cwd = g_get_current_dir ();
219
220       for (i = first_file; i < argc; i++)
221         error |= query_module (cwd, argv[i], contents);
222
223       g_free (cwd);
224     }
225
226   if (!error)
227     {
228       if (cache_file)
229         {
230           GError *err;
231
232           err = NULL;
233           if (!g_file_set_contents (cache_file, contents->str, -1, &err))
234             {
235                 g_fprintf (stderr, "%s\n", err->message);
236                 error = 1;
237             }
238         }
239       else
240         g_print ("%s\n", contents->str);
241     }
242
243   return error ? 1 : 0;
244 }