4 * Copyright (C) 2000-2010 Red Hat Software
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.
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.
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.
25 #include <glib/gprintf.h>
37 #define SOEXT ("." G_MODULE_SUFFIX)
40 #include "gtk/gtkrc.h"
41 #include "gtk/gtkimmodule.h"
42 #include "gtk/gtkversion.h"
45 escape_string (GString *contents, const char *str)
56 g_string_append (contents, "\\n");
59 g_string_append (contents, "\\\"");
62 /* Replace backslashes in path with forward slashes, so that
63 * it reads in without problems.
66 g_string_append (contents, "/");
70 g_string_append_c (contents, c);
78 print_escaped (GString *contents, const char *str)
80 g_string_append_c (contents, '"');
81 escape_string (contents, str);
82 g_string_append_c (contents, '"');
83 g_string_append_c (contents, ' ');
87 query_module (const char *dir, const char *name, GString *contents)
89 void (*list) (const GtkIMContextInfo ***contexts,
91 void (*init) (GTypeModule *type_module);
93 GtkIMContext *(*create) (const gchar *context_id);
102 gboolean error = FALSE;
104 if (g_path_is_absolute (name))
105 path = g_strdup (name);
107 path = g_build_filename (dir, name, NULL);
109 module = g_module_open (path, 0);
113 g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
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))
123 const GtkIMContextInfo **contexts;
132 print_escaped (contents, path);
133 g_string_append_c (contents, '\n');
135 (*list) (&contexts, &n_contexts);
137 for (i = 0; i < n_contexts; i++)
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');
146 g_string_append_c (contents, '\n');
150 g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
157 g_module_close (module);
162 int main (int argc, char **argv)
167 gboolean error = FALSE;
168 gchar *cache_file = NULL;
172 if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
174 cache_file = gtk_rc_get_im_module_file ();
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"
185 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
187 if (argc == first_file) /* No file arguments given */
191 GHashTable *dirs_done;
193 path = gtk_rc_get_im_module_path ();
195 g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
197 dirs = pango_split_file_list (path);
198 dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
200 for (i = 0; dirs[i]; i++)
201 if (!g_hash_table_lookup (dirs_done, dirs[i]))
203 GDir *dir = g_dir_open (dirs[i], 0, NULL);
208 while ((dent = g_dir_read_name (dir)))
210 if (g_str_has_suffix (dent, SOEXT))
211 error |= query_module (dirs[i], dent, contents);
217 g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
220 g_hash_table_destroy (dirs_done);
224 cwd = g_get_current_dir ();
226 for (i = first_file; i < argc; i++)
227 error |= query_module (cwd, argv[i], contents);
239 if (!g_file_set_contents (cache_file, contents->str, -1, &err))
241 g_fprintf (stderr, "%s\n", err->message);
246 g_print ("%s\n", contents->str);
249 return error ? 1 : 0;