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/gtkimmodule.h"
41 #include "gtk/gtkversion.h"
44 escape_string (GString *contents, const char *str)
55 g_string_append (contents, "\\n");
58 g_string_append (contents, "\\\"");
61 /* Replace backslashes in path with forward slashes, so that
62 * it reads in without problems.
65 g_string_append (contents, "/");
69 g_string_append_c (contents, c);
77 print_escaped (GString *contents, const char *str)
79 g_string_append_c (contents, '"');
80 escape_string (contents, str);
81 g_string_append_c (contents, '"');
82 g_string_append_c (contents, ' ');
86 query_module (const char *dir, const char *name, GString *contents)
88 void (*list) (const GtkIMContextInfo ***contexts,
98 gboolean error = FALSE;
100 if (g_path_is_absolute (name))
101 path = g_strdup (name);
103 path = g_build_filename (dir, name, NULL);
105 module = g_module_open (path, 0);
109 g_fprintf (stderr, "Cannot load module %s: %s\n", path, g_module_error());
114 g_module_symbol (module, "im_module_list", &list_ptr) &&
115 g_module_symbol (module, "im_module_init", &init_ptr) &&
116 g_module_symbol (module, "im_module_exit", &exit_ptr) &&
117 g_module_symbol (module, "im_module_create", &create_ptr))
119 const GtkIMContextInfo **contexts;
125 print_escaped (contents, path);
126 g_string_append_c (contents, '\n');
128 (*list) (&contexts, &n_contexts);
130 for (i = 0; i < n_contexts; i++)
132 print_escaped (contents, contexts[i]->context_id);
133 print_escaped (contents, contexts[i]->context_name);
134 print_escaped (contents, contexts[i]->domain);
135 print_escaped (contents, contexts[i]->domain_dirname);
136 print_escaped (contents, contexts[i]->default_locales);
137 g_string_append_c (contents, '\n');
139 g_string_append_c (contents, '\n');
143 g_fprintf (stderr, "%s does not export GTK+ IM module API: %s\n", path,
150 g_module_close (module);
155 int main (int argc, char **argv)
160 gboolean error = FALSE;
161 gchar *cache_file = NULL;
165 if (argc > 1 && strcmp (argv[1], "--update-cache") == 0)
167 cache_file = gtk_rc_get_im_module_file ();
171 contents = g_string_new ("");
172 g_string_append_printf (contents,
173 "# GTK+ Input Method Modules file\n"
174 "# Automatically generated file, do not edit\n"
175 "# Created by %s from gtk+-%d.%d.%d\n"
178 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
180 if (argc == first_file) /* No file arguments given */
184 GHashTable *dirs_done;
186 path = gtk_rc_get_im_module_path ();
188 g_string_append_printf (contents, "# ModulesPath = %s\n#\n", path);
190 dirs = pango_split_file_list (path);
191 dirs_done = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
193 for (i = 0; dirs[i]; i++)
194 if (!g_hash_table_lookup (dirs_done, dirs[i]))
196 GDir *dir = g_dir_open (dirs[i], 0, NULL);
201 while ((dent = g_dir_read_name (dir)))
203 if (g_str_has_suffix (dent, SOEXT))
204 error |= query_module (dirs[i], dent, contents);
210 g_hash_table_insert (dirs_done, dirs[i], GUINT_TO_POINTER (TRUE));
213 g_hash_table_destroy (dirs_done);
217 cwd = g_get_current_dir ();
219 for (i = first_file; i < argc; i++)
220 error |= query_module (cwd, argv[i], contents);
232 if (!g_file_set_contents (cache_file, contents->str, -1, &err))
234 g_fprintf (stderr, "%s\n", err->message);
239 g_print ("%s\n", contents->str);
242 return error ? 1 : 0;