summaryrefslogtreecommitdiff
path: root/mate-dictionary/src/gdict-app.c
blob: baf3ded87c60ebe96d935355b72d1182acd6892c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* gdict-app.c - main application class
 *
 * This file is part of MATE Dictionary
 *
 * Copyright (C) 2005 Emmanuele Bassi
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>

#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n.h>

#include "gdict-common.h"
#include "gdict-pref-dialog.h"
#include "gdict-app.h"

static GdictApp *singleton = NULL;


struct _GdictAppClass
{
  GObjectClass parent_class;
};



G_DEFINE_TYPE (GdictApp, gdict_app, G_TYPE_OBJECT);


static void
gdict_app_finalize (GObject *object)
{
  GdictApp *app = GDICT_APP (object);
  
  if (app->loader)
    g_object_unref (app->loader);
  
  app->current_window = NULL;
  
  g_slist_foreach (app->windows,
  		   (GFunc) gtk_widget_destroy,
  		   NULL);
  g_slist_free (app->windows);

  g_slist_foreach (app->lookup_words, (GFunc) g_free, NULL);
  g_slist_free (app->lookup_words);

  g_slist_foreach (app->match_words, (GFunc) g_free, NULL);
  g_slist_free (app->match_words);

  g_free (app->database);
  g_free (app->source_name);

  G_OBJECT_CLASS (gdict_app_parent_class)->finalize (object);
}

static void
gdict_app_class_init (GdictAppClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  
  gobject_class->finalize = gdict_app_finalize;
}

static void
gdict_app_init (GdictApp *app)
{
  app->windows = NULL;
  app->current_window = NULL;
}

static void
gdict_window_destroy_cb (GtkWidget *widget,
                         gpointer   user_data)
{
  GdictWindow *window = GDICT_WINDOW (widget);
  GdictApp *app = GDICT_APP (user_data);
  
  g_assert (GDICT_IS_APP (app));

  app->windows = g_slist_remove (app->windows, window);
  
  if (window == app->current_window)
    app->current_window = app->windows ? app->windows->data : NULL;
  
  if (app->windows == NULL)
    gtk_main_quit ();  
}

static void
gdict_window_created_cb (GdictWindow *parent,
                         GdictWindow *new_window,
                         gpointer     user_data)
{
  GdictApp *app = GDICT_APP (user_data);
  
  /* this might seem convoluted - but it's necessary, since I don't want
   * GdictWindow to know about the GdictApp singleton.  every time a new
   * window is created by a GdictWindow, it will register its "child window"
   * here; the lifetime handlers will check every child window created and
   * destroyed, and will add/remove it to the windows list accordingly
   */
  g_signal_connect (new_window, "created",
  		    G_CALLBACK (gdict_window_created_cb), app);
  g_signal_connect (new_window, "destroy",
  		    G_CALLBACK (gdict_window_destroy_cb), app);

  if (gtk_window_get_group (GTK_WINDOW (parent)))
    gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (parent)),
		    		 GTK_WINDOW (new_window));
  
  app->windows = g_slist_prepend (app->windows, new_window);
  app->current_window = new_window;
}

static void
gdict_create_window (GdictApp *app)
{
  GSList *l;

  if (!singleton->lookup_words && !singleton->match_words)
    {
      GtkWidget *window;

      window = gdict_window_new (GDICT_WINDOW_ACTION_CLEAR,
      				 singleton->loader,
				 singleton->source_name,
				 NULL);
      g_signal_connect (window, "created",
		        G_CALLBACK (gdict_window_created_cb), app);
      g_signal_connect (window, "destroy",
		        G_CALLBACK (gdict_window_destroy_cb), app);
  
      app->windows = g_slist_prepend (app->windows, window);
      app->current_window = GDICT_WINDOW (window);
  
      gtk_widget_show (window);

      return;
    }
      
  for (l = singleton->lookup_words; l != NULL; l = l->next)
    {
      gchar *word = l->data;
      GtkWidget *window;

      window = gdict_window_new (GDICT_WINDOW_ACTION_LOOKUP,
      				 singleton->loader,
		      		 singleton->source_name,
				 word);
      
      g_signal_connect (window, "created",
		        G_CALLBACK (gdict_window_created_cb), app);
      g_signal_connect (window, "destroy",
		        G_CALLBACK (gdict_window_destroy_cb), app);
  
      app->windows = g_slist_prepend (app->windows, window);
      app->current_window = GDICT_WINDOW (window);
  
      gtk_widget_show (window);
    }

  for (l = singleton->match_words; l != NULL; l = l->next)
    {
      gchar *word = l->data;
      GtkWidget *window;

      window = gdict_window_new (GDICT_WINDOW_ACTION_MATCH,
      				 singleton->loader,
				 singleton->source_name,
				 word);
      
      g_signal_connect (window, "created",
      			G_CALLBACK (gdict_window_created_cb), app);
      g_signal_connect (window, "destroy",
      			G_CALLBACK (gdict_window_destroy_cb), app);
      
      app->windows = g_slist_prepend (app->windows, window);
      app->current_window = GDICT_WINDOW (window);

      gtk_widget_show (window);
    }
}

static void
definition_found_cb (GdictContext    *context,
                     GdictDefinition *definition,
                     gpointer         user_data)
{
  /* Translators: the first is the word found, the second is the
   * database name and the last is the definition's text; please
   * keep the new lines. */
  g_print (_("Definition for '%s'\n"
	     "  From '%s':\n"
	     "\n"
	     "%s\n"),
           gdict_definition_get_word (definition),
	   gdict_definition_get_database (definition),
	   gdict_definition_get_text (definition));
}

static void
error_cb (GdictContext *context,
          const GError *error,
          gpointer      user_data)
{
  g_print (_("Error: %s\n"), error->message);

  gtk_main_quit ();
}

static void
lookup_end_cb (GdictContext *context,
               gpointer      user_data)
{
  GdictApp *app = GDICT_APP (user_data);

  app->remaining_words -= 1;

  if (app->remaining_words == 0)
    gtk_main_quit ();
}

static void
gdict_look_up_word_and_quit (GdictApp *app)
{
  GdictSource *source;
  GdictContext *context;
  GSList *l;
  
  if (!app->lookup_words)
    {
      g_print (_("See mate-dictionary --help for usage\n"));

      gdict_cleanup ();
      exit (1);
    }

  if (app->source_name)
    source = gdict_source_loader_get_source (app->loader, app->source_name);
  else
    source = gdict_source_loader_get_source (app->loader, GDICT_DEFAULT_SOURCE_NAME);

  if (!source)
    {
      g_warning (_("Unable to find a suitable dictionary source"));

      gdict_cleanup ();
      exit (1);
    }

  /* we'll just use this one context, so we can destroy it along with
   * the source that contains it
   */
  context = gdict_source_peek_context (source);
  g_assert (GDICT_IS_CONTEXT (context));

  g_signal_connect (context, "definition-found",
		    G_CALLBACK (definition_found_cb), app);
  g_signal_connect (context, "error",
		    G_CALLBACK (error_cb), app);
  g_signal_connect (context, "lookup-end",
		    G_CALLBACK (lookup_end_cb), app);

  app->remaining_words = 0;
  for (l = app->lookup_words; l != NULL; l = l->next)
    {
      gchar *word = l->data;
      GError *err = NULL;

      app->remaining_words += 1;

      gdict_context_define_word (context,
                                 app->database,
                                 word,
                                 &err);

      if (err)
        {
          g_warning (_("Error while looking up the definition of \"%s\":\n%s"),
                     word, err->message);
          g_error_free (err);
        }
    }

  gtk_main ();

  g_object_unref (source);

  gdict_cleanup ();
  exit (0);
}

void
gdict_init (int *argc, char ***argv)
{
  GError *err = NULL;
  GOptionContext *context;
  gchar *loader_path;
  gchar **lookup_words = NULL;
  gchar **match_words = NULL;
  gchar *database = NULL;
  gchar *strategy = NULL;
  gchar *source_name = NULL;
  gboolean no_window = FALSE;
  gboolean list_sources = FALSE;

  const GOptionEntry gdict_app_goptions[] =
  {
    { "look-up", 0, 0, G_OPTION_ARG_STRING_ARRAY, &lookup_words,
       N_("Words to look up"), N_("word") },
    { "match", 0, 0, G_OPTION_ARG_STRING_ARRAY, &match_words,
       N_("Words to match"), N_("word") },
    { "source", 's', 0, G_OPTION_ARG_STRING, &source_name,
       N_("Dictionary source to use"), N_("source") },
    { "list-sources", 'l', 0, G_OPTION_ARG_NONE, &list_sources,
       N_("Show available dictionary sources"), NULL },
    { "no-window", 'n', 0, G_OPTION_ARG_NONE, &no_window,
       N_("Print result to the console"), NULL },
    { "database", 'D', 0, G_OPTION_ARG_STRING, &database,
       N_("Database to use"), N_("db") },
    { "strategy", 'S', 0, G_OPTION_ARG_STRING, &strategy,
       N_("Strategy to use"), N_("strat") },
    { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &lookup_words,
       N_("Words to look up"), N_("word") },
    { NULL },
  };
  
  g_assert (singleton == NULL);  
  
  singleton = GDICT_APP (g_object_new (GDICT_TYPE_APP, NULL));
  g_assert (GDICT_IS_APP (singleton));
  
  /* create the new option context */
  context = g_option_context_new (_(" - Look up words in dictionaries"));
  
  g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
  g_option_context_add_main_entries (context, gdict_app_goptions, GETTEXT_PACKAGE);
  g_option_context_add_group (context, gdict_get_option_group ());
  g_option_context_add_group (context, gtk_get_option_group (TRUE));

  g_option_context_parse (context, argc, argv, &err);
  if (err)
    {
      g_critical ("Failed to parse argument: %s", err->message);
      g_error_free (err);
      g_option_context_free (context);
      gdict_cleanup ();

      exit (1);
    }
  
  g_set_application_name (_("Dictionary"));
  gtk_window_set_default_icon_name ("accessories-dictionary");
  
  if (!gdict_create_data_dir ())
    {
      gdict_cleanup ();

      exit (1);
    }

  singleton->settings = g_settings_new ("org.mate.dictionary");

  /* add user's path for fetching dictionary sources */  
  singleton->loader = gdict_source_loader_new ();
  loader_path = gdict_get_data_dir (); 
  gdict_source_loader_add_search_path (singleton->loader, loader_path);
  g_free (loader_path);

  if (lookup_words)
    {
      gsize i;
      gsize length = g_strv_length (lookup_words);

      for (i = 0; i < length; i++)
        singleton->lookup_words = g_slist_prepend (singleton->lookup_words,
                                                   g_strdup (lookup_words[i]));
    }

  if (match_words)
    {
      gsize i;
      gsize length = g_strv_length (match_words);

      for (i = 0; i < length; i++)
        singleton->match_words = g_slist_prepend (singleton->match_words,
                                                  g_strdup (match_words[i]));
    }

  if (database)
    singleton->database = g_strdup (database);
  
  if (source_name)
    singleton->source_name = g_strdup (source_name);

  if (no_window)
    singleton->no_window = TRUE;

  if (list_sources)
    singleton->list_sources = TRUE;
}

void
gdict_main (void)
{
  if (!singleton)
    {
      g_warning ("You must initialize GdictApp using gdict_init()\n");
      return;
    }
  
  if (!singleton->no_window)
    gdict_create_window (singleton);
  else
    gdict_look_up_word_and_quit (singleton);
  
  gtk_main ();
}

void
gdict_cleanup (void)
{
  if (!singleton)
    {
      g_warning ("You must initialize GdictApp using gdict_init()\n");
      return;
    }

  g_object_unref (singleton);
}