Updated Spanish translation
[gnome-utils.git] / gnome-dictionary / src / gdict-app.c
blobc744920663c502bd1f7945604bc328fc431fbbbd
1 /* gdict-app.c - main application class
3 * This file is part of GNOME Dictionary
5 * Copyright (C) 2005 Emmanuele Bassi
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
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 GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <math.h>
31 #include <sys/stat.h>
33 #include <gtk/gtk.h>
34 #include <glib.h>
35 #include <glib/gi18n.h>
37 #include "gdict-common.h"
38 #include "gdict-pref-dialog.h"
39 #include "gdict-app.h"
41 static GdictApp *singleton = NULL;
43 struct _GdictAppClass
45 GObjectClass parent_class;
48 G_DEFINE_TYPE (GdictApp, gdict_app, G_TYPE_OBJECT);
50 static gchar **gdict_lookup_words = NULL;
51 static gchar **gdict_match_words = NULL;
53 static gchar *gdict_source_name = NULL;
54 static gchar *gdict_database_name = NULL;
55 static gchar *gdict_strategy_name = NULL;
57 static GOptionEntry gdict_app_goptions[] = {
59 "look-up", 0,
61 G_OPTION_ARG_STRING_ARRAY, &gdict_lookup_words,
62 N_("Words to look up"), N_("WORD")
65 "match", 0,
67 G_OPTION_ARG_STRING_ARRAY, &gdict_match_words,
68 N_("Words to match"), N_("WORD")
71 "source", 's',
73 G_OPTION_ARG_STRING, &gdict_source_name,
74 N_("Dictionary source to use"), N_("NAME")
77 "database", 'D',
79 G_OPTION_ARG_STRING, &gdict_database_name,
80 N_("Database to use"), N_("NAME")
83 "strategy", 'S',
85 G_OPTION_ARG_STRING, &gdict_strategy_name,
86 N_("Strategy to use"), N_("NAME")
89 G_OPTION_REMAINING, 0, 0,
90 G_OPTION_ARG_STRING_ARRAY, &gdict_lookup_words,
91 N_("Words to look up"), N_("WORDS")
93 { NULL },
96 static void
97 gdict_app_finalize (GObject *object)
99 GdictApp *app = GDICT_APP (object);
101 if (app->loader)
102 g_object_unref (app->loader);
104 G_OBJECT_CLASS (gdict_app_parent_class)->finalize (object);
107 static void
108 gdict_app_class_init (GdictAppClass *klass)
110 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
112 gobject_class->finalize = gdict_app_finalize;
115 static void
116 gdict_app_init (GdictApp *app)
120 static void
121 gdict_activate (GApplication *application,
122 GdictApp *gdict_app)
124 GtkWidget *window;
128 static int
129 gdict_command_line (GApplication *application,
130 GApplicationCommandLine *cmd_line,
131 GdictApp *gdict_app)
133 GOptionContext *context;
134 GError *error;
135 GSList *l;
136 gsize words_len, i;
137 gint argc;
138 char **argv;
140 argv = g_application_command_line_get_arguments (cmd_line, &argc);
142 /* create the new option context */
143 context = g_option_context_new (N_(" - Look up words in dictionaries"));
145 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
146 g_option_context_add_main_entries (context, gdict_app_goptions, GETTEXT_PACKAGE);
147 g_option_context_add_group (context, gdict_get_option_group ());
148 g_option_context_add_group (context, gtk_get_option_group (FALSE));
150 error = NULL;
151 if (!g_option_context_parse (context, &argc, &argv, &error))
153 g_critical ("Failed to parse argument: %s", error->message);
154 g_error_free (error);
155 g_option_context_free (context);
156 return 1;
159 g_option_context_free (context);
161 if (gdict_lookup_words == NULL &&
162 gdict_match_words == NULL)
164 GtkWidget *window = gdict_window_new (GDICT_WINDOW_ACTION_CLEAR,
165 singleton->loader,
166 gdict_source_name,
167 gdict_database_name,
168 gdict_strategy_name,
169 NULL);
170 gtk_window_set_application (GTK_WINDOW (window), singleton->app);
171 gtk_widget_show (window);
173 return 0;
176 if (gdict_lookup_words != NULL)
177 words_len = g_strv_length (gdict_lookup_words);
178 else
179 words_len = 0;
181 for (i = 0; i < words_len; i++)
183 const gchar *word = gdict_lookup_words[i];
184 GtkWidget *window;
186 window = gdict_window_new (GDICT_WINDOW_ACTION_LOOKUP,
187 singleton->loader,
188 gdict_source_name,
189 gdict_database_name,
190 gdict_strategy_name,
191 word);
193 gtk_window_set_application (GTK_WINDOW (window), singleton->app);
194 gtk_widget_show (window);
197 if (gdict_match_words != NULL)
198 words_len = g_strv_length (gdict_match_words);
199 else
200 words_len = 0;
202 for (i = 0; i < words_len; i++)
204 const gchar *word = gdict_match_words[i];
205 GtkWidget *window;
207 window = gdict_window_new (GDICT_WINDOW_ACTION_MATCH,
208 singleton->loader,
209 gdict_source_name,
210 gdict_database_name,
211 gdict_strategy_name,
212 word);
214 gtk_window_set_application (GTK_WINDOW (window), singleton->app);
215 gtk_widget_show (window);
218 return 0;
221 void
222 gdict_main (int *argc,
223 char ***argv)
225 gchar *loader_path;
227 if (!gdict_create_data_dir ())
228 exit (1);
230 g_type_init ();
231 gtk_init (argc, argv);
233 g_set_application_name (_("Dictionary"));
234 gtk_window_set_default_icon_name ("accessories-dictionary");
236 /* the main application instance */
237 singleton = g_object_new (gdict_app_get_type (), NULL);
239 /* add user's path for fetching dictionary sources */
240 singleton->loader = gdict_source_loader_new ();
241 loader_path = gdict_get_data_dir ();
242 gdict_source_loader_add_search_path (singleton->loader, loader_path);
243 g_free (loader_path);
245 singleton->app = gtk_application_new ("org.gnome.Dictionary", G_APPLICATION_HANDLES_COMMAND_LINE);
246 g_signal_connect (singleton->app, "command-line", G_CALLBACK (gdict_command_line), singleton);
248 g_application_run (G_APPLICATION (singleton->app), *argc, *argv);
251 void
252 gdict_cleanup (void)
254 if (!singleton)
256 g_warning ("You must initialize GdictApp using gdict_init()\n");
257 return;
260 g_object_unref (singleton->app);
261 g_object_unref (singleton);