summaryrefslogtreecommitdiff
path: root/mate-dictionary/libgdict/gdict-source-loader.c
blob: af9097d3a661aa55399b0e2157b1059ddbb134ae (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/* Copyright (C) 2005 Emmanuele Bassi <ebassi@gmail.com>
 *
 * This file is part of MATE Utils.
 *
 * MATE Utils 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.
 *
 * MATE Utils 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 MATE Utils.  If not, see <https://www.gnu.org/licenses/>.
 */

/**
 * SECTION:gdict-source-loader
 * @short_description: Loader object for a set of dictionary sources
 *
 * #GdictSourceLoader allows searching for dictionary source definition
 * files inside a set of paths and return a #GdictSource using its name.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>

#include "gdict-source-loader.h"
#include "gdict-utils.h"
#include "gdict-enum-types.h"
#include "gdict-marshal.h"
#include "gdict-private.h"

#define GDICT_SOURCE_FILE_SUFFIX	 	".desktop"

struct _GdictSourceLoaderPrivate
{
  GSList *paths;

  GSList *sources;
  GHashTable *sources_by_name;

  guint paths_dirty : 1;
};

enum
{
  PROP_0,

  PROP_PATHS,
  PROP_SOURCES
};

enum
{
  SOURCE_LOADED,

  LAST_SIGNAL
};

static guint loader_signals[LAST_SIGNAL] = { 0 };



G_DEFINE_TYPE_WITH_PRIVATE (GdictSourceLoader, gdict_source_loader, G_TYPE_OBJECT);


static void
gdict_source_loader_finalize (GObject *object)
{
  GdictSourceLoaderPrivate *priv = gdict_source_loader_get_instance_private (GDICT_SOURCE_LOADER (object));

  if (priv->paths)
    {
      g_slist_free_full (priv->paths,
                         g_free);
      priv->paths = NULL;
    }

  if (priv->sources_by_name)
    g_hash_table_destroy (priv->sources_by_name);

  if (priv->sources)
    {
      g_slist_free_full (priv->sources,
                         g_object_unref);
      priv->sources = NULL;
    }

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

static void
gdict_source_loader_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
  switch (prop_id)
    {
    case PROP_PATHS:
      break;
    case PROP_SOURCES:
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdict_source_loader_get_property (GObject    *object,
				  guint       prop_id,
				  GValue     *value,
				  GParamSpec *pspec)
{
  switch (prop_id)
    {
    case PROP_PATHS:
      break;
    case PROP_SOURCES:
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdict_source_loader_class_init (GdictSourceLoaderClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gdict_source_loader_set_property;
  gobject_class->get_property = gdict_source_loader_get_property;
  gobject_class->finalize = gdict_source_loader_finalize;

  /**
   * GdictSourceLoader:paths
   *
   * The search paths used by this object
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_PATHS,
  				   g_param_spec_pointer ("paths",
  				   			 _("Paths"),
  				   			 _("Search paths used by this object"),
  				   			 G_PARAM_READABLE));
  /**
   * GdictSourceLoader:sources
   *
   * The #GdictSource objects found by this object
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_SOURCES,
  				   g_param_spec_pointer ("sources",
  				   			 _("Sources"),
  				   			 _("Dictionary sources found"),
  				   			 G_PARAM_READABLE));

  /**
   * GdictSourceLoader::source-loaded
   * @loader: the object which received the signal
   * @source: the new #GdictSource object found
   *
   * This signal is emitted when a new dictionary source has been added
   * to the list.
   *
   * Since: 1.0
   */
  loader_signals[SOURCE_LOADED] =
    g_signal_new ("source-loaded",
    		  G_OBJECT_CLASS_TYPE (gobject_class),
    		  G_SIGNAL_RUN_LAST,
    		  G_STRUCT_OFFSET (GdictSourceLoaderClass, source_loaded),
    		  NULL, NULL,
    		  gdict_marshal_VOID__OBJECT,
    		  G_TYPE_NONE, 1,
    		  GDICT_TYPE_SOURCE);
}

static void
gdict_source_loader_init (GdictSourceLoader *loader)
{
  GdictSourceLoaderPrivate *priv;

  priv = gdict_source_loader_get_instance_private (loader);
  loader->priv = priv;

  priv->paths = NULL;
  /* add the default, system-wide path */
  priv->paths = g_slist_prepend (priv->paths, g_strdup (GDICTSOURCESDIR));

  priv->sources = NULL;
  priv->sources_by_name = g_hash_table_new_full (g_str_hash, g_str_equal,
		  				 g_free,
						 NULL);

  /* ensure that the sources list will be updated */
  priv->paths_dirty = TRUE;
}

/**
 * gdict_source_loader_new:
 *
 * Creates a new #GdictSourceLoader object.  This object is used to search
 * into a list of paths for dictionary source files.  See #GdictSource for
 * more informations about the format of dictionary source files.
 *
 * Return value: a new #GdictSourceLoader object
 *
 * Since: 1.0
 */
GdictSourceLoader *
gdict_source_loader_new (void)
{
  return g_object_new (GDICT_TYPE_SOURCE_LOADER, NULL);
}

/**
 * gdict_source_loader_update:
 * @loader: a #GdictSourceLoader
 *
 * Queue an update of the sources inside @loader.
 *
 * Since: 1.0
 */
void
gdict_source_loader_update (GdictSourceLoader *loader)
{
  g_return_if_fail (GDICT_IS_SOURCE_LOADER (loader));

  loader->priv->paths_dirty = TRUE;
}

/**
 * gdict_source_loader_add_search_path:
 * @loader: a #GdictSourceLoader
 * @path: a path to be added to the search path list
 *
 * Adds @path to the search paths list of @loader.
 *
 * Since: 1.0
 */
void
gdict_source_loader_add_search_path (GdictSourceLoader *loader,
				     const gchar       *path)
{
  GSList *l;

  g_return_if_fail (GDICT_IS_SOURCE_LOADER (loader));
  g_return_if_fail (path != NULL);

  /* avoid duplications */
  for (l = loader->priv->paths; l != NULL; l = l->next)
    if (strcmp (path, (gchar *) l->data) == 0)
      return;

  loader->priv->paths = g_slist_append (loader->priv->paths, g_strdup (path));
  loader->priv->paths_dirty = TRUE;
}

/**
 * gdict_source_loader_get_paths:
 * @loader: a #GdictSourceLoader
 *
 * Gets the list of paths used by @loader to search for dictionary source
 * files.
 *
 * Return value: a list containing the paths.  The returned list is owned
 *   by the #GdictSourceLoader object and should never be free or modified.
 *
 * Since: 1.0
 */
const GSList *
gdict_source_loader_get_paths (GdictSourceLoader *loader)
{
  g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL);

  return loader->priv->paths;
}

/* create the list of dictionary source files, by scanning the search path
 * directories for .desktop files; we disavow symlinks and sub-directories
 * for the time being.
 */
static GSList *
build_source_filenames (GdictSourceLoader *loader)
{
  GSList *retval, *d;

  g_assert (GDICT_IS_SOURCE_LOADER (loader));

  if (!loader->priv->paths)
    return NULL;

  retval = NULL;
  for (d = loader->priv->paths; d != NULL; d = d->next)
    {
      gchar *path = (gchar *) d->data;
      const gchar *filename;
      GDir *dir;

      dir = g_dir_open (path, 0, NULL);
      if (!dir)
        continue;

      do
        {
          filename = g_dir_read_name (dir);
          if (filename)
            {
              gchar *full_path;

              if (!g_str_has_suffix (filename, GDICT_SOURCE_FILE_SUFFIX))
                break;

              full_path = g_build_filename (path, filename, NULL);
              if (g_file_test (full_path, G_FILE_TEST_IS_REGULAR))
                {
		  retval = g_slist_prepend (retval, full_path);
		}
            }
        }
      while (filename != NULL);

      g_dir_close (dir);
    }

  return g_slist_reverse (retval);
}

static void
gdict_source_loader_update_sources (GdictSourceLoader *loader)
{
  GSList *filenames, *f;

  g_assert (GDICT_IS_SOURCE_LOADER (loader));

  g_slist_free_full (loader->priv->sources,
                     g_object_unref);
  loader->priv->sources = NULL;

  filenames = build_source_filenames (loader);
  for (f = filenames; f != NULL; f = f->next)
    {
      GdictSource *source;
      GError *load_err;
      gchar *path = (gchar *) f->data;

      g_assert (path != NULL);

      source = gdict_source_new ();

      load_err = NULL;
      gdict_source_load_from_file (source, path, &load_err);
      if (load_err)
        {
           g_warning ("Unable to load dictionary source at '%s': %s\n",
                      path,
                      load_err->message);
           g_error_free (load_err);

           continue;
        }

      loader->priv->sources = g_slist_append (loader->priv->sources,
                                              source);
      g_hash_table_replace (loader->priv->sources_by_name,
                            g_strdup (gdict_source_get_name (source)),
                            source);

      g_signal_emit (loader, loader_signals[SOURCE_LOADED], 0, source);
    }

  g_slist_free_full (filenames, g_free);

  loader->priv->paths_dirty = FALSE;
}

/**
 * gdict_source_loader_get_names:
 * @loader: a #GdictSourceLoader
 * @length: return location for the number of source names, or %NULL
 *
 * Retrieves the list of dictionary source names available into the
 * search paths of @loader.
 *
 * Return value: a newly allocated, %NULL terminated array of strings.  You
 *   should free the returned string array with g_strfreev()
 *
 * Since: 1.0
 */
gchar **
gdict_source_loader_get_names (GdictSourceLoader *loader,
			       gsize             *length)
{
  GSList *l;
  gchar **names;
  gsize i;

  g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL);

  if (loader->priv->paths_dirty)
    gdict_source_loader_update_sources (loader);

  names = g_new0 (gchar *, g_slist_length (loader->priv->sources) + 1);

  i = 0;
  for (l = loader->priv->sources; l != NULL; l = l->next)
    {
      GdictSource *s = GDICT_SOURCE (l->data);

      g_assert (s != NULL);

      names[i++] = g_strdup (gdict_source_get_name (s));
    }
  names[i] = NULL;

  if (length)
    *length = i;

  return names;
}

/**
 * gdict_source_loader_get_sources:
 * @loader: a #GdictSourceLoader
 *
 * Retrieves the list of dictionary sources available into the search
 * paths of @loader, in form of #GdictSource objects.
 *
 * Return value: a list of #GdictSource objects.  The returned list
 *   is owned by the #GdictSourceLoader object, and should never be
 *   freed or modified.
 *
 * Since: 1.0
 */
const GSList *
gdict_source_loader_get_sources (GdictSourceLoader *loader)
{
  g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL);

  if (loader->priv->paths_dirty)
    gdict_source_loader_update_sources (loader);

  return loader->priv->sources;
}

/**
 * gdict_source_loader_get_source:
 * @loader: a #GdictSourceLoader
 * @name: a name of a dictionary source
 *
 * Retrieves a dictionary source using @name.  You can use the returned
 * #GdictSource object to create the right #GdictContext for that
 * dictionary source.
 *
 * Return value: a referenced #GdictSource object.  You should de-reference
 *   it using g_object_unref() when you finished using it.
 *
 * Since: 1.0
 */
GdictSource *
gdict_source_loader_get_source (GdictSourceLoader *loader,
				const gchar       *name)
{
  GdictSource *retval;

  g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  if (loader->priv->paths_dirty)
    gdict_source_loader_update_sources (loader);

  retval = g_hash_table_lookup (loader->priv->sources_by_name, name);
  if (retval)
    return g_object_ref (retval);

  return NULL;
}

/**
 * gdict_source_loader_remove_source:
 * @loader: a #GdictSourceLoader
 * @name: name of a dictionary source
 *
 * Removes the dictionary source @name from @loader.  This function will
 * also remove the dictionary source definition file bound to it.
 *
 * Return value: %TRUE if the dictionary source was successfully removed
 *
 * Since: 1.0
 */
gboolean
gdict_source_loader_remove_source (GdictSourceLoader *loader,
				   const gchar       *name)
{
  GdictSourceLoaderPrivate *priv;
  GSList *l;

  g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), FALSE);
  g_return_val_if_fail (name != NULL, FALSE);

  priv = loader->priv;

  if (priv->paths_dirty)
    gdict_source_loader_update_sources (loader);

  for (l = priv->sources; l != NULL; l = l->next)
    {
      GdictSource *s = GDICT_SOURCE (l->data);

      if (strcmp (gdict_source_get_name (s), name) == 0)
        {
          gchar *filename;

          g_object_get (G_OBJECT (s), "filename", &filename, NULL);

          if (g_unlink (filename) == -1)
            {
              g_warning ("Unable to remove filename '%s' for the "
                         "dictionary source '%s'\n",
                         filename,
                         name);

              return FALSE;
            }

          g_hash_table_remove (priv->sources_by_name, name);

          priv->sources = g_slist_remove_link (priv->sources, l);

          g_object_unref (s);
          g_slist_free (l);

          return TRUE;
        }
    }

  return FALSE;
}

/**
 * gdict_source_loader_has_source:
 * @loader: a #GdictSourceLoader
 * @source_name: the name of a dictionary source
 *
 * Checks whether @loader has a dictionary source with name @source_name.
 *
 * Return value: %TRUE if the dictionary source is known
 *
 * Since: 0.12
 */
gboolean
gdict_source_loader_has_source (GdictSourceLoader *loader,
                                const gchar       *source_name)
{
  g_return_val_if_fail (GDICT_IS_SOURCE_LOADER (loader), FALSE);
  g_return_val_if_fail (source_name != NULL, FALSE);

  if (loader->priv->paths_dirty)
    gdict_source_loader_update_sources (loader);

  return (g_hash_table_lookup (loader->priv->sources_by_name, source_name) != NULL);
}