summaryrefslogtreecommitdiff
path: root/mate-dictionary/libgdict/gdict-source.c
blob: 9fe0fe56a568a837d6d3ff4418adbb6eea1f0011 (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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
/* 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
 * @short_description: A dictionary source definition
 *
 * #GdictSource is the representation of a #GdictContext. Each dictionary
 * source provides a list of available dictionaries (databases) and a list
 * of available matching strategies. Using a #GdictContext you can query
 * the dictionary source for matching words and for definitions.
 *
 * By using a #GdictSource object you can retrieve the appropriate
 * #GdictContext, already set up with the right parameters.
 */

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <glib/gi18n-lib.h>

#include "gdict-source.h"
#include "gdict-client-context.h"
#include "gdict-utils.h"
#include "gdict-enum-types.h"
#include "gdict-private.h"

/* Main group */
#define SOURCE_GROUP		"Dictionary Source"

/* Common keys */
#define SOURCE_KEY_NAME		"Name"
#define SOURCE_KEY_DESCRIPTION	"Description"
#define SOURCE_KEY_TRANSPORT	"Transport"
#define SOURCE_KEY_DATABASE 	"Database"
#define SOURCE_KEY_STRATEGY 	"Strategy"

/* dictd transport keys */
#define SOURCE_KEY_HOSTNAME	"Hostname"
#define SOURCE_KEY_PORT		"Port"

struct _GdictSourcePrivate
{
  gchar *filename;
  GKeyFile *keyfile;

  gchar *name;
  gchar *description;

  gchar *database;
  gchar *strategy;

  GdictSourceTransport transport;

  GdictContext *context;
};

enum
{
  PROP_0,

  PROP_FILENAME,
  PROP_NAME,
  PROP_DESCRIPTION,
  PROP_DATABASE,
  PROP_STRATEGY,
  PROP_TRANSPORT,
  PROP_CONTEXT
};

/* keep in sync with GdictSourceTransport */
static const gchar *valid_transports[] =
{
  "dictd",	/* GDICT_SOURCE_TRANSPORT_DICTD */

  NULL		/* GDICT_SOURCE_TRANSPORT_INVALID */
};

#define IS_VALID_TRANSPORT(t)	(((t) >= GDICT_SOURCE_TRANSPORT_DICTD) && \
				 ((t) < GDICT_SOURCE_TRANSPORT_INVALID))

GQuark
gdict_source_error_quark (void)
{
  static GQuark quark = 0;

  if (G_UNLIKELY (quark == 0))
    quark = g_quark_from_static_string ("gdict-source-error-quark");

  return quark;
}


G_DEFINE_TYPE_WITH_PRIVATE (GdictSource, gdict_source, G_TYPE_OBJECT);



static void
gdict_source_set_property (GObject      *object,
			   guint         prop_id,
			   const GValue *value,
			   GParamSpec   *pspec)
{
  GdictSource *source = GDICT_SOURCE (object);

  switch (prop_id)
    {
    case PROP_NAME:
      gdict_source_set_name (source, g_value_get_string (value));
      break;
    case PROP_DESCRIPTION:
      gdict_source_set_description (source, g_value_get_string (value));
      break;
    case PROP_TRANSPORT:
      gdict_source_set_transport (source, g_value_get_enum (value), NULL);
      break;
    case PROP_DATABASE:
      gdict_source_set_database (source, g_value_get_string (value));
      break;
    case PROP_STRATEGY:
      gdict_source_set_strategy (source, g_value_get_string (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdict_source_get_property (GObject    *object,
			   guint       prop_id,
			   GValue     *value,
			   GParamSpec *pspec)
{
  GdictSource *source = GDICT_SOURCE (object);
  GdictSourcePrivate *priv = source->priv;

  switch (prop_id)
    {
    case PROP_FILENAME:
      g_value_set_string (value, priv->filename);
      break;
    case PROP_NAME:
      g_value_set_string (value, priv->name);
      break;
    case PROP_DESCRIPTION:
      g_value_set_string (value, priv->description);
      break;
    case PROP_DATABASE:
      g_value_set_string (value, priv->database);
      break;
    case PROP_STRATEGY:
      g_value_set_string (value, priv->strategy);
      break;
    case PROP_TRANSPORT:
      g_value_set_enum (value, priv->transport);
      break;
    case PROP_CONTEXT:
      g_value_set_object (value, gdict_source_peek_context (source));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gdict_source_finalize (GObject *object)
{
  GdictSourcePrivate *priv = gdict_source_get_instance_private (GDICT_SOURCE (object));

  g_free (priv->filename);

  if (priv->keyfile)
    g_key_file_free (priv->keyfile);

  g_free (priv->name);
  g_free (priv->description);

  g_free (priv->database);
  g_free (priv->strategy);

  if (priv->context)
    g_object_unref (priv->context);

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

static void
gdict_source_class_init (GdictSourceClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->set_property = gdict_source_set_property;
  gobject_class->get_property = gdict_source_get_property;
  gobject_class->finalize = gdict_source_finalize;

  /**
   * GdictSource:filename
   *
   * The filename used by this dictionary source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_FILENAME,
  				   g_param_spec_string ("filename",
  				   			_("Filename"),
  				   			_("The filename used by this dictionary source"),
  				   			NULL,
  				   			G_PARAM_READABLE));
  /**
   * GdictSource:name
   *
   * The display name of this dictionary source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_NAME,
  				   g_param_spec_string ("name",
  				   			_("Name"),
  				   			_("The display name of this dictionary source"),
  				   			NULL,
  				   			(G_PARAM_READABLE | G_PARAM_WRITABLE)));
  /**
   * GdictSource:description
   *
   * The description of this dictionary source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_DESCRIPTION,
  				   g_param_spec_string ("description",
  				   			_("Description"),
  				   			_("The description of this dictionary source"),
  				   			NULL,
  				   			(G_PARAM_READABLE | G_PARAM_WRITABLE)));
  /**
   * GdictSource:database
   *
   * The default database of this dictionary source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_DATABASE,
  				   g_param_spec_string ("database",
  				   			_("Database"),
  				   			_("The default database of this dictionary source"),
  				   			NULL,
  				   			(G_PARAM_READABLE | G_PARAM_WRITABLE)));
  /**
   * GdictSource:strategy
   *
   * The default strategy of this dictionary source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_STRATEGY,
  				   g_param_spec_string ("strategy",
  				   			_("Strategy"),
  				   			_("The default strategy of this dictionary source"),
  				   			NULL,
  				   			(G_PARAM_READABLE | G_PARAM_WRITABLE)));
  /**
   * GdictSource:transport
   *
   * The transport mechanism used by this source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_TRANSPORT,
  				   g_param_spec_enum ("transport",
  				   		      _("Transport"),
  				   		      _("The transport mechanism used by this dictionary source"),
  				   		      GDICT_TYPE_SOURCE_TRANSPORT,
  				   		      GDICT_SOURCE_TRANSPORT_INVALID,
  				   		      (G_PARAM_READABLE | G_PARAM_WRITABLE)));
  /**
   * GdictSource:context
   *
   * The #GdictContext bound to this source.
   *
   * Since: 1.0
   */
  g_object_class_install_property (gobject_class,
  				   PROP_CONTEXT,
  				   g_param_spec_object ("context",
  				   			_("Context"),
  				   			_("The GdictContext bound to this source"),
  				   			GDICT_TYPE_CONTEXT,
  				   			G_PARAM_READABLE));
}

static void
gdict_source_init (GdictSource *source)
{
  GdictSourcePrivate *priv;

  priv = gdict_source_get_instance_private (source);
  source->priv = priv;

  priv->filename = NULL;
  priv->keyfile = g_key_file_new ();

  priv->name = NULL;
  priv->description = NULL;
  priv->database = NULL;
  priv->strategy = NULL;
  priv->transport = GDICT_SOURCE_TRANSPORT_INVALID;

  priv->context = NULL;
}

/**
 * gdict_source_new:
 *
 * Creates an empty #GdictSource object.  Use gdict_load_from_file() to
 * read an existing dictionary source definition file.
 *
 * Return value: an empty #GdictSource
 */
GdictSource *
gdict_source_new (void)
{
  return g_object_new (GDICT_TYPE_SOURCE, NULL);
}

static GdictSourceTransport
gdict_source_resolve_transport (const gchar *transport)
{
  if (!transport)
    return GDICT_SOURCE_TRANSPORT_INVALID;

  if (strcmp (transport, "dictd") == 0)
    return GDICT_SOURCE_TRANSPORT_DICTD;
  else
    return GDICT_SOURCE_TRANSPORT_INVALID;

  g_assert_not_reached ();
}

static GdictContext *
gdict_source_create_context (GdictSource           *source,
			     GdictSourceTransport   transport,
			     GError               **error)
{
  GdictSourcePrivate *priv;
  GdictContext *context;

  g_assert (GDICT_IS_SOURCE (source));

  priv = source->priv;

  switch (transport)
    {
    case GDICT_SOURCE_TRANSPORT_DICTD:
      {
      gchar *hostname;
      gint port;

      hostname = g_key_file_get_string (priv->keyfile,
      					SOURCE_GROUP,
      					SOURCE_KEY_HOSTNAME,
      					NULL);

      port = g_key_file_get_integer (priv->keyfile,
      				     SOURCE_GROUP,
      				     SOURCE_KEY_PORT,
      				     NULL);
      if (!port)
        port = -1;

      context = gdict_client_context_new (hostname, port);

      if (hostname)
        g_free (hostname);
      }
      break;
    default:
      g_set_error (error, GDICT_SOURCE_ERROR,
                   GDICT_SOURCE_ERROR_PARSE,
                   _("Invalid transport type '%d'"),
                   transport);
      return NULL;
    }

  g_assert (context != NULL);

  if (priv->transport != transport)
    priv->transport = transport;

  return context;
}

static gboolean
gdict_source_parse (GdictSource  *source,
		    GError      **error)
{
  GdictSourcePrivate *priv;
  GError *parse_error;
  gchar *transport;
  GdictSourceTransport t;

  priv = source->priv;

  if (!g_key_file_has_group (priv->keyfile, SOURCE_GROUP))
    {
      g_set_error (error, GDICT_SOURCE_ERROR,
                   GDICT_SOURCE_ERROR_PARSE,
                   _("No '%s' group found inside the dictionary source definition"),
                   SOURCE_GROUP);

      return FALSE;
    }

  /* fetch the name for the dictionary source definition */
  parse_error = NULL;
  priv->name = g_key_file_get_string (priv->keyfile,
		  		      SOURCE_GROUP,
				      SOURCE_KEY_NAME,
				      &parse_error);
  if (parse_error)
    {
      g_set_error (error, GDICT_SOURCE_ERROR,
                   GDICT_SOURCE_ERROR_PARSE,
                   _("Unable to get the '%s' key inside the dictionary "
                     "source definition: %s"),
                   SOURCE_KEY_NAME,
                   parse_error->message);
      g_error_free (parse_error);

      g_key_file_free (priv->keyfile);
      priv->keyfile = NULL;

      return FALSE;
    }

  /* if present, fetch the localized description */
  if (g_key_file_has_key (priv->keyfile, SOURCE_GROUP, SOURCE_KEY_DESCRIPTION, NULL))
    {
      priv->description = g_key_file_get_locale_string (priv->keyfile,
                                                        SOURCE_GROUP,
                                                        SOURCE_KEY_DESCRIPTION,
                                                        NULL,
                                                        &parse_error);
      if (parse_error)
        {
          g_set_error (error, GDICT_SOURCE_ERROR,
                       GDICT_SOURCE_ERROR_PARSE,
                       _("Unable to get the '%s' key inside the dictionary "
                         "source definition: %s"),
                       SOURCE_KEY_DESCRIPTION,
                       parse_error->message);

          g_error_free (parse_error);
          g_key_file_free (priv->keyfile);
          priv->keyfile = NULL;
          g_free (priv->name);

          return FALSE;
        }
    }

  if (g_key_file_has_key (priv->keyfile, SOURCE_GROUP, SOURCE_KEY_DATABASE, NULL))
    {
      priv->database = g_key_file_get_string (priv->keyfile,
                                              SOURCE_GROUP,
                                              SOURCE_KEY_DATABASE,
                                              &parse_error);
      if (parse_error)
        {
          g_set_error (error, GDICT_SOURCE_ERROR,
                       GDICT_SOURCE_ERROR_PARSE,
                       _("Unable to get the '%s' key inside the dictionary "
                         "source definition: %s"),
                       SOURCE_KEY_DATABASE,
                       parse_error->message);

          g_error_free (parse_error);
          g_key_file_free (priv->keyfile);
          priv->keyfile = NULL;
          g_free (priv->name);
          g_free (priv->description);

          return FALSE;
        }
    }

  if (g_key_file_has_key (priv->keyfile, SOURCE_GROUP, SOURCE_KEY_STRATEGY, NULL))
    {
      priv->strategy = g_key_file_get_string (priv->keyfile,
                                              SOURCE_GROUP,
                                              SOURCE_KEY_STRATEGY,
                                              &parse_error);
      if (parse_error)
        {
          g_set_error (error, GDICT_SOURCE_ERROR,
                       GDICT_SOURCE_ERROR_PARSE,
                       _("Unable to get the '%s' key inside the dictionary "
                         "source definition: %s"),
                       SOURCE_KEY_STRATEGY,
                       parse_error->message);

          g_error_free (parse_error);
          g_key_file_free (priv->keyfile);
          priv->keyfile = NULL;

          g_free (priv->name);
          g_free (priv->description);
          g_free (priv->database);

          return FALSE;
        }
    }

  transport = g_key_file_get_string (priv->keyfile,
  				     SOURCE_GROUP,
  				     SOURCE_KEY_TRANSPORT,
  				     &parse_error);
  if (parse_error)
    {
      g_set_error (error, GDICT_SOURCE_ERROR,
      		   GDICT_SOURCE_ERROR_PARSE,
      		   _("Unable to get the '%s' key inside the dictionary "
      		     "source definition file: %s"),
      		   SOURCE_KEY_TRANSPORT,
      		   parse_error->message);

      g_error_free (parse_error);
      g_key_file_free (priv->keyfile);
      priv->keyfile = NULL;
      g_free (priv->name);
      g_free (priv->description);
      g_free (priv->database);
      g_free (priv->strategy);

      return FALSE;
    }

  t = gdict_source_resolve_transport (transport);
  g_free (transport);

  priv->context = gdict_source_create_context (source, t, &parse_error);
  if (parse_error)
    {
      g_propagate_error (error, parse_error);

      g_key_file_free (priv->keyfile);
      priv->keyfile = NULL;

      g_free (priv->name);
      g_free (priv->description);
      g_free (priv->database);
      g_free (priv->strategy);

      return FALSE;
    }

  return TRUE;
}

/**
 * gdict_source_load_from_file:
 * @source: an empty #GdictSource
 * @filename: path to a dictionary source file
 * @error: return location for a #GError or %NULL
 *
 * Loads a dictionary source definition file into an empty #GdictSource
 * object.
 *
 * Return value: %TRUE if @filename was loaded successfully.
 *
 * Since: 1.0
 */
gboolean
gdict_source_load_from_file (GdictSource  *source,
			     const gchar  *filename,
			     GError      **error)
{
  GdictSourcePrivate *priv;
  GError *read_error;
  GError *parse_error;

  g_return_val_if_fail (GDICT_IS_SOURCE (source), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);

  priv = source->priv;

  if (!priv->keyfile)
    priv->keyfile = g_key_file_new ();

  read_error = NULL;
  g_key_file_load_from_file (priv->keyfile,
                             filename,
                             G_KEY_FILE_KEEP_TRANSLATIONS,
                             &read_error);
  if (read_error)
    {
      g_propagate_error (error, read_error);

      return FALSE;
    }

  parse_error = NULL;
  gdict_source_parse (source, &parse_error);
  if (parse_error)
    {
      g_propagate_error (error, parse_error);

      return FALSE;
    }

  g_assert (priv->context != NULL);

  priv->filename = g_strdup (filename);

  return TRUE;
}

/**
 * gdict_source_load_from_data:
 * @source: a #GdictSource
 * @data: string containing a dictionary source
 * @length: length of @data
 * @error: return location for a #GError or %NULL
 *
 * Loads a dictionary source definition from @data inside an empty
 * #GdictSource object.
 *
 * Return value: %TRUE if @filename was loaded successfully.
 *
 * Since: 1.0
 */
gboolean
gdict_source_load_from_data (GdictSource  *source,
			     const gchar  *data,
			     gsize         length,
			     GError      **error)
{
  GdictSourcePrivate *priv;
  GError *read_error;
  GError *parse_error;

  g_return_val_if_fail (GDICT_IS_SOURCE (source), FALSE);
  g_return_val_if_fail (data != NULL, FALSE);

  priv = source->priv;

  if (!priv->keyfile)
    priv->keyfile = g_key_file_new ();

  read_error = NULL;
  g_key_file_load_from_data (priv->keyfile,
                             data,
                             length,
                             G_KEY_FILE_KEEP_TRANSLATIONS,
                             &read_error);
  if (read_error)
    {
      g_propagate_error (error, read_error);

      return FALSE;
    }

  parse_error = NULL;
  gdict_source_parse (source, &parse_error);
  if (parse_error)
    {
      g_propagate_error (error, parse_error);

      return FALSE;
    }

  g_assert (priv->context != NULL);

  g_free (priv->filename);
  priv->filename = NULL;

  return TRUE;
}

/**
 * gdict_source_to_data:
 * @source: a #GdictSource
 * @length: return loaction for the length of the string, or %NULL
 * @error: return location for a #GError or %NULL
 *
 * Outputs a dictionary source as a string.
 *
 * Return value: a newly allocated string holding the contents of @source.
 *
 * Since: 1.0
 */
gchar *
gdict_source_to_data (GdictSource  *source,
		      gsize        *length,
		      GError      **error)
{
  GdictSourcePrivate *priv;
  gchar *retval = NULL;

  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  priv = source->priv;

  if (!priv->name)
    {
      g_set_error (error, GDICT_SOURCE_ERROR,
                   GDICT_SOURCE_ERROR_INVALID_NAME,
                   _("Dictionary source does not have name"));

      return NULL;
    }

  if (!IS_VALID_TRANSPORT (priv->transport))
    {
      g_set_error (error, GDICT_SOURCE_ERROR,
                   GDICT_SOURCE_ERROR_INVALID_TRANSPORT,
                   _("Dictionary source '%s' has invalid transport '%s'"),
                   priv->name,
                   valid_transports[priv->transport]);

      return NULL;
    }

  if (priv->keyfile)
    {
      GError *write_error = NULL;

      retval = g_key_file_to_data (priv->keyfile,
      				   length,
      				   &write_error);
      if (write_error)
        g_propagate_error (error, write_error);
    }

  return retval;
}

/**
 * gdict_source_set_name:
 * @source: a #GdictSource
 * @name: the UTF8-encoded name of the dictionary source
 *
 * Sets @name as the displayable name of the dictionary source.
 *
 * Since: 1.0
 */
void
gdict_source_set_name (GdictSource *source,
		       const gchar *name)
{
  g_return_if_fail (GDICT_IS_SOURCE (source));
  g_return_if_fail (name != NULL);

  g_free (source->priv->name);
  source->priv->name = g_strdup (name);

  if (!source->priv->keyfile)
    source->priv->keyfile = g_key_file_new ();

  g_key_file_set_string (source->priv->keyfile,
    			 SOURCE_GROUP,
    			 SOURCE_KEY_NAME,
    			 name);
}

/**
 * gdict_source_get_name:
 * @source: a #GdictSource
 *
 * Retrieves the name of @source.
 *
 * Return value: the name of a #GdictSource.  The returned string is owned
 *   by the #GdictSource object, and should not be modified or freed.
 *
 * Since: 1.0
 */
const gchar *
gdict_source_get_name (GdictSource *source)
{
  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  return source->priv->name;
}

/**
 * gdict_source_set_description:
 * @source: a #GdictSource
 * @description: a UTF-8 encoded description or %NULL
 *
 * Sets the description of @source.  If @description is %NULL, unsets the
 * currently set description.
 *
 * Since: 1.0
 */
void
gdict_source_set_description (GdictSource *source,
			      const gchar *description)
{
  g_return_if_fail (GDICT_IS_SOURCE (source));

  g_free (source->priv->description);

  if (!source->priv->keyfile)
    source->priv->keyfile = g_key_file_new ();

  if (description && description[0] != '\0')
    {
      source->priv->description = g_strdup (description);

      g_key_file_set_string (source->priv->keyfile,
  			     SOURCE_GROUP,
  			     SOURCE_KEY_DESCRIPTION,
  			     description);
    }
  else
    {
      if (g_key_file_has_key (source->priv->keyfile,
      			      SOURCE_GROUP,
      			      SOURCE_KEY_DESCRIPTION,
      			      NULL))
        g_key_file_remove_key (source->priv->keyfile,
                               SOURCE_GROUP,
                               SOURCE_KEY_DESCRIPTION,
                               NULL);
    }
}

/**
 * gdict_source_get_description:
 * @source: a #GdictSource
 *
 * Retrieves the description of @source.
 *
 * Return value: the description of a #GdictSource.  The returned string is
 *   owned by the #GdictSource object, and should not be modified or freed.
 *
 * Since: 1.0
 */
const gchar *
gdict_source_get_description (GdictSource *source)
{
  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  return source->priv->description;
}

/**
 * gdict_source_set_database:
 * @source: a #GdictSource
 * @database: a UTF-8 encoded database name or %NULL
 *
 * Sets the default database of @source.  If @database is %NULL, unsets the
 * currently set database.
 *
 * Since: 1.0
 */
void
gdict_source_set_database (GdictSource *source,
			   const gchar *database)
{
  g_return_if_fail (GDICT_IS_SOURCE (source));

  g_free (source->priv->database);

  if (!source->priv->keyfile)
    source->priv->keyfile = g_key_file_new ();

  if (database && database[0] != '\0')
    {
      source->priv->database = g_strdup (database);

      g_key_file_set_string (source->priv->keyfile,
  			     SOURCE_GROUP,
  			     SOURCE_KEY_DATABASE,
  			     database);
    }
  else
    {
      if (g_key_file_has_key (source->priv->keyfile,
      			      SOURCE_GROUP,
      			      SOURCE_KEY_DATABASE,
      			      NULL))
        g_key_file_remove_key (source->priv->keyfile,
                               SOURCE_GROUP,
                               SOURCE_KEY_DATABASE,
                               NULL);
    }
}

/**
 * gdict_source_get_database:
 * @source: a #GdictSource
 *
 * Retrieves the default database of @source.
 *
 * Return value: the default strategy of a #GdictSource.  The returned string
 *   is owned by the #GdictSource object, and should not be modified or freed.
 *
 * Since: 1.0
 */
const gchar *
gdict_source_get_database (GdictSource *source)
{
  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  return source->priv->database;
}

/**
 * gdict_source_set_strategy:
 * @source: a #GdictSource
 * @strategy: a UTF-8 encoded strategy or %NULL
 *
 * Sets the description of @source.  If @strategy is %NULL, unsets the
 * currently set strategy.
 *
 * Since: 1.0
 */
void
gdict_source_set_strategy (GdictSource *source,
			   const gchar *strategy)
{
  g_return_if_fail (GDICT_IS_SOURCE (source));

  g_free (source->priv->strategy);

  if (!source->priv->keyfile)
    source->priv->keyfile = g_key_file_new ();

  if (strategy && strategy[0] != '\0')
    {
      source->priv->strategy = g_strdup (strategy);

      g_key_file_set_string (source->priv->keyfile,
  			     SOURCE_GROUP,
  			     SOURCE_KEY_STRATEGY,
  			     strategy);
    }
  else
    {
      if (g_key_file_has_key (source->priv->keyfile,
      			      SOURCE_GROUP,
      			      SOURCE_KEY_STRATEGY,
      			      NULL))
        g_key_file_remove_key (source->priv->keyfile,
                               SOURCE_GROUP,
                               SOURCE_KEY_STRATEGY,
                               NULL);
    }
}

/**
 * gdict_source_get_strategy:
 * @source: a #GdictSource
 *
 * Retrieves the default strategy of @source.
 *
 * Return value: the default strategy of a #GdictSource.  The returned string
 *   is owned by the #GdictSource object, and should not be modified or freed.
 *
 * Since: 1.0
 */
const gchar *
gdict_source_get_strategy (GdictSource *source)
{
  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  return source->priv->strategy;
}

/**
 * gdict_source_set_transportv
 * @source: a #GdictSource
 * @transport: a #GdictSourceTransport
 * @first_transport_property: FIXME
 * @var_args: FIXME
 *
 * FIXME
 *
 * Since: 1.0
 */
void
gdict_source_set_transportv (GdictSource          *source,
			     GdictSourceTransport  transport,
			     const gchar          *first_transport_property,
			     va_list               var_args)
{
  GdictSourcePrivate *priv;

  g_return_if_fail (GDICT_IS_SOURCE (source));
  g_return_if_fail (IS_VALID_TRANSPORT (transport));

  priv = source->priv;

  priv->transport = transport;

  if (priv->context)
    g_object_unref (priv->context);

  switch (priv->transport)
    {
    case GDICT_SOURCE_TRANSPORT_DICTD:
      priv->context = gdict_client_context_new (NULL, -1);
      g_assert (GDICT_IS_CLIENT_CONTEXT (priv->context));

      g_object_set_valist (G_OBJECT (priv->context),
                           first_transport_property,
                           var_args);

      break;
    case GDICT_SOURCE_TRANSPORT_INVALID:
    default:
      g_assert_not_reached ();
      break;
    }

  /* update the keyfile */
  if (!priv->keyfile)
    priv->keyfile = g_key_file_new ();

  g_key_file_set_string (priv->keyfile,
  			 SOURCE_GROUP,
  			 SOURCE_KEY_TRANSPORT,
  			 valid_transports[transport]);

  switch (priv->transport)
    {
    case GDICT_SOURCE_TRANSPORT_DICTD:
      g_key_file_set_string (priv->keyfile,
      			     SOURCE_GROUP,
      			     SOURCE_KEY_HOSTNAME,
      			     gdict_client_context_get_hostname (GDICT_CLIENT_CONTEXT (priv->context)));
      g_key_file_set_integer (priv->keyfile,
      			      SOURCE_GROUP,
      			      SOURCE_KEY_PORT,
      			      gdict_client_context_get_port (GDICT_CLIENT_CONTEXT (priv->context)));
      break;
    case GDICT_SOURCE_TRANSPORT_INVALID:
    default:
      g_assert_not_reached ();
      break;
    }
}

/**
 * gdict_source_set_transport:
 * @source: a #GdictSource
 * @transport: a valid transport
 * @first_transport_property: property for the context bound to
 *   the transport, or %NULL
 * @Varargs: property value for first property name, then additionary
 *   properties, ending with %NULL
 *
 * Sets @transport as the choosen transport for @source.  The @transport
 * argument is a method of retrieving dictionary data from a source; it is
 * used to create the right #GdictContext for this #GdictSource.  After
 * @transport, property name/value pairs should be listed, with a %NULL
 * pointer ending the list.  Properties are the same passed to a #GdictContext
 * implementation instance using g_object_set().
 *
 * Here's a simple example:
 *
 * <informalexample><programlisting>
 * #include &lt;gdict/gdict.h&gt;
 *  GdictSource *source = gdict_source_new ();
 *
 *  gdict_source_set_name (source, "My Source");
 *  gdict_source_set_transport (source, GDICT_SOURCE_TRANSPORT_DICTD,
 *                              "hostname", "dictionary-server.org",
 *                              "port", 2628,
 *                              NULL);
 * </programlisting></informalexample>
 *
 * Since: 1.0
 */
void
gdict_source_set_transport (GdictSource          *source,
			    GdictSourceTransport  transport,
			    const gchar          *first_transport_property,
			    ...)
{
  va_list args;

  g_return_if_fail (GDICT_IS_SOURCE (source));
  g_return_if_fail (IS_VALID_TRANSPORT (transport));

  va_start (args, first_transport_property);

  gdict_source_set_transportv (source, transport,
                               first_transport_property,
                               args);

  va_end (args);
}

/**
 * gdict_source_get_transport:
 * @source: a #GdictSource
 *
 * FIXME
 *
 * Return value: FIXME
 *
 * Since: 1.0
 */
GdictSourceTransport
gdict_source_get_transport (GdictSource *source)
{
  g_return_val_if_fail (GDICT_IS_SOURCE (source), GDICT_SOURCE_TRANSPORT_INVALID);

  return source->priv->transport;
}

/**
 * gdict_source_get_context:
 * @source: a #GdictSource
 *
 * Gets the #GdictContext bound to @source.
 *
 * Return value: a #GdictContext for @source.  Use g_object_unref()
 *   when you don't need it anymore.
 *
 * Since: 1.0
 */
GdictContext *
gdict_source_get_context (GdictSource *source)
{
  GdictContext *retval;

  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  retval = gdict_source_create_context (source,
		  			source->priv->transport,
					NULL);

  return retval;
}

/**
 * gdict_source_peek_context:
 * @source: a #GdictSource
 *
 * Gets the #GdictContext bound to @source.  The returned object is a
 * referenced copy of the context held by @source; if you want a different
 * instance, use gdict_source_get_context().
 *
 * Return value: a referenced #GdictContext.  Use g_object_unref() when
 *   finished using it.
 *
 * Since: 1.0
 */
GdictContext *
gdict_source_peek_context (GdictSource *source)
{
  g_return_val_if_fail (GDICT_IS_SOURCE (source), NULL);

  if (!source->priv->context)
    source->priv->context = gdict_source_create_context (source,
    							 source->priv->transport,
    							 NULL);
  return g_object_ref (source->priv->context);
}