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
|
/* 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-context
* @short_description: Interface for dictionary transports
*
* #GdictContext is an interface used to uniformly access dictionary
* transport objects. Each implementation of #GdictContext must provide
* functions for accessing the list of databases available on a dictionary
* source and the available matching strategies; a function for retrieving
* all words matching a given string, inside one (or more) of those databases
* and using one of those strategies; a function for querying one (or more)
* of those databases for a definition of a word.
*
* Implementations of the #GdictContext interface should query their
* dictionary sources asynchronously; methods of the #GdictContext interface
* should return immediately, and each time a new database, strategy, match
* or definition has been found, a signal should be fired by those
* implementations.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <glib/gi18n-lib.h>
#include "gdict-context.h"
#include "gdict-enum-types.h"
#include "gdict-utils.h"
#include "gdict-marshal.h"
#include "gdict-context-private.h"
#include "gdict-private.h"
static void gdict_context_class_init (gpointer g_iface, void *user_data);
GType
gdict_context_get_type (void)
{
static GType context_type = 0;
if (G_UNLIKELY (context_type == 0))
{
static GTypeInfo context_info =
{
sizeof (GdictContextIface),
NULL, /* base_init */
NULL, /* base_finalize */
(GClassInitFunc) gdict_context_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
};
context_type = g_type_register_static (G_TYPE_INTERFACE,
"GdictContext",
&context_info, 0);
g_type_interface_add_prerequisite (context_type, G_TYPE_OBJECT);
}
return context_type;
}
static void
gdict_context_class_init (gpointer g_iface, void *user_data)
{
GType iface_type = G_TYPE_FROM_INTERFACE (g_iface);
/**
* GdictContext::lookup-start
* @context: the object which received the signal
*
* This signal is emitted when a look up operation has been issued using
* a #GdictContext. Since every operation using a context is
* asynchronous, you can use this signal to know if the request has been
* issued or not.
*
* Since: 1.0
*/
g_signal_new ("lookup-start",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, lookup_start),
NULL, NULL,
gdict_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GdictContext::lookup-end
* @context: the object which received the signal
*
* This signal is emitted when a look up operation that has been issued
* using a #GdictContext has been completed. Since every operation using a
* context is asynchronous, you can use this signal to know if the request
* has been completed or not.
*
* Since: 1.0
*/
g_signal_new ("lookup-end",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, lookup_end),
NULL, NULL,
gdict_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GdictContext::error
* @context: the object which received the signal
* @error: a #GError
*
* This signal is emitted when an error happened during an asynchronous
* request.
*
* Since: 1.0
*/
g_signal_new ("error",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, error),
NULL, NULL,
gdict_marshal_VOID__POINTER,
G_TYPE_NONE, 1,
G_TYPE_POINTER);
/**
* GdictContext::database-found
* @context: the object which received the signal
* @database: a #GdictDatabase
*
* This signal is emitted when a database request has found a database.
*
* Since: 1.0
*/
g_signal_new ("database-found",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, database_found),
NULL, NULL,
gdict_marshal_VOID__BOXED,
G_TYPE_NONE, 1,
GDICT_TYPE_DATABASE);
/**
* GdictContext::strategy-found
* @context: the object which received the signal
* @strategy: a #GdictStrategy
*
* This signal is emitted when a strategy request has found a strategy.
*
* Since: 1.0
*/
g_signal_new ("strategy-found",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, strategy_found),
NULL, NULL,
gdict_marshal_VOID__BOXED,
G_TYPE_NONE, 1,
GDICT_TYPE_STRATEGY);
/**
* GdictContext::match-found
* @context: the object which received the signal
* @match: a #GdictMatch
*
* This signal is emitted when a match request has found a match.
*
* Since: 1.0
*/
g_signal_new ("match-found",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, match_found),
NULL, NULL,
gdict_marshal_VOID__BOXED,
G_TYPE_NONE, 1,
GDICT_TYPE_MATCH);
/**
* GdictContext::definition-found
* @context: the object which received the signal
* @definition: a #GdictDefinition
*
* This signal is emitted when a definition request has found a definition.
*
* Since: 1.0
*/
g_signal_new ("definition-found",
iface_type,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GdictContextIface, definition_found),
NULL, NULL,
gdict_marshal_VOID__BOXED,
G_TYPE_NONE, 1,
GDICT_TYPE_DEFINITION);
/**
* GdictContext:local-only
*
* Whether the context uses only local dictionaries or not.
*
* Since: 1.0
*/
g_object_interface_install_property (g_iface,
g_param_spec_boolean ("local-only",
_("Local Only"),
_("Whether the context uses only local dictionaries or not"),
FALSE,
(G_PARAM_READABLE | G_PARAM_WRITABLE)));
}
GQuark
gdict_context_error_quark (void)
{
return g_quark_from_static_string ("gdict-context-error-quark");
}
/**
* gdict_context_set_local_only:
* @context: a #GdictContext
* @local_only: %TRUE if only local resources will be used
*
* Sets whether only local resources will be used when querying for databases,
* strategies, matches or definitions.
*
* Since: 1.0
*/
void
gdict_context_set_local_only (GdictContext *context,
gboolean local_only)
{
g_return_if_fail (GDICT_IS_CONTEXT (context));
g_object_set (context, "local-only", &local_only, NULL);
}
/**
* gdict_context_get_local_only:
* @context: a #GdictContext
*
* Gets whether only local resources will be used when querying.
*
* Return value: %TRUE if only local resources will be used.
*
* Since: 1.0
*/
gboolean
gdict_context_get_local_only (GdictContext *context)
{
gboolean local_only;
g_return_val_if_fail (GDICT_IS_CONTEXT (context), FALSE);
g_object_get (context, "local-only", &local_only, NULL);
return local_only;
}
/**
* gdict_context_lookup_databases:
* @context: a #GdictContext
* @error: return location for a #GError, or %NULL
*
* Query @context for the list of databases available. Each time a
* database is found, the "database-found" signal is fired.
*
* Return value: %TRUE if the query was successfully started.
*
* Since: 1.0
*/
gboolean
gdict_context_lookup_databases (GdictContext *context,
GError **error)
{
g_return_val_if_fail (GDICT_IS_CONTEXT (context), FALSE);
if (!GDICT_CONTEXT_GET_IFACE (context)->get_databases)
{
g_warning ("Object `%s' does not implement the get_databases "
"virtual function.",
g_type_name (G_OBJECT_TYPE (context)));
return FALSE;
}
return GDICT_CONTEXT_GET_IFACE (context)->get_databases (context, error);
}
/**
* gdict_context_lookup_strategies:
* @context: a #GdictContext
* @error: return location for a #GError, or %NULL
*
* Query @context for the list of matching strategies available. Each
* time a new strategy is found, the "strategy-found" signal is fired.
*
* Return value: %TRUE if the query was successfully started.
*
* Since: 1.0
*/
gboolean
gdict_context_lookup_strategies (GdictContext *context,
GError **error)
{
g_return_val_if_fail (GDICT_IS_CONTEXT (context), FALSE);
if (!GDICT_CONTEXT_GET_IFACE (context)->get_strategies)
{
g_warning ("Object `%s' does not implement the get_strategies "
"virtual function.",
g_type_name (G_OBJECT_TYPE (context)));
return FALSE;
}
return GDICT_CONTEXT_GET_IFACE (context)->get_strategies (context, error);
}
/**
* gdict_context_match_word:
* @context: a #GdictContext
* @database: a database name to search into, or %NULL for the
* default database
* @strategy: a strategy name to use for matching, or %NULL for
* the default strategy
* @word: the word to match
* @error: return location for a #GError, or %NULL
*
* Query @context for a list of word matching @word inside @database,
* using @strategy as a matching strategy. Each time a matching word
* is found, the "match-found" signal is fired.
*
* Return value: %TRUE if the query was successfully started.
*
* Since: 1.0
*/
gboolean
gdict_context_match_word (GdictContext *context,
const gchar *database,
const gchar *strategy,
const gchar *word,
GError **error)
{
g_return_val_if_fail (GDICT_IS_CONTEXT (context), FALSE);
g_return_val_if_fail (word != NULL, FALSE);
if (!GDICT_CONTEXT_GET_IFACE (context)->match_word)
{
g_warning ("Object `%s' does not implement the match_word "
"virtual function.",
g_type_name (G_OBJECT_TYPE (context)));
return FALSE;
}
return GDICT_CONTEXT_GET_IFACE (context)->match_word (context,
database,
strategy,
word,
error);
}
/**
* gdict_context_define_word:
* @context: a #GdictContext
* @database: a database name to search into, or %NULL for the
* default database
* @word: the word to search
* @error: return location for a #GError, or %NULL
*
* Query @context for a list of definitions of @word inside @database. Each
* time a new definition is found, the "definition-found" signal is fired.
*
* Return value: %TRUE if the query was successfully sent.
*
* Since: 1.0
*/
gboolean
gdict_context_define_word (GdictContext *context,
const gchar *database,
const gchar *word,
GError **error)
{
g_return_val_if_fail (GDICT_IS_CONTEXT (context), FALSE);
g_return_val_if_fail (word != NULL, FALSE);
if (!GDICT_CONTEXT_GET_IFACE (context)->define_word)
{
g_warning ("Object `%s' does not implement the define_word "
"virtual function.",
g_type_name (G_OBJECT_TYPE (context)));
return FALSE;
}
return GDICT_CONTEXT_GET_IFACE (context)->define_word (context,
database,
word,
error);
}
/*****************
* GdictDatabase *
*****************/
GDICT_DEFINE_BOXED_TYPE (GdictDatabase, gdict_database);
GdictDatabase *
_gdict_database_new (const gchar *name)
{
GdictDatabase *retval;
g_return_val_if_fail (name != NULL, NULL);
retval = g_slice_new (GdictDatabase);
retval->name = g_strdup (name);
retval->full_name = NULL;
retval->ref_count = 1;
return retval;
}
/**
* gdict_database_ref:
* @db: a #GdictDatabase
*
* Increases the reference count of @db by one.
*
* Return value: @db with its reference count increased
*
* Since: 1.0
*/
GdictDatabase *
gdict_database_ref (GdictDatabase *db)
{
g_return_val_if_fail (db != NULL, NULL);
g_assert (db->ref_count != 0);
db->ref_count += 1;
return db;
}
/**
* gdict_database_unref:
* @db: a #GdictDatabase
*
* Decreases the reference count of @db by one. If the reference count reaches
* zero, @db is destroyed.
*
* Since: 1.0
*/
void
gdict_database_unref (GdictDatabase *db)
{
g_return_if_fail (db != NULL);
g_assert (db->ref_count != 0);
db->ref_count -= 1;
if (db->ref_count == 0)
{
g_free (db->name);
g_free (db->full_name);
g_slice_free (GdictDatabase, db);
}
}
/**
* gdict_database_get_name:
* @db: a #GdictDatabase
*
* Gets the short name of the database, to be used with functions like
* gdict_context_match_word() or gdict_context_define_word().
*
* Return value: the short name of the database. The string is owned by
* the #GdictDatabase object, and should never be modified or freed.
*
* Since: 1.0
*/
const gchar *
gdict_database_get_name (GdictDatabase *db)
{
g_return_val_if_fail (db != NULL, NULL);
return db->name;
}
/**
* gdict_database_get_full_name:
* @db: a #GdictDatabase
*
* Gets the full name of the database, suitable for display.
*
* Return value: the full name of the database. The string is owned by
* the #GdictDatabase object, and should never be modified or freed.
*
* Since: 1.0
*/
const gchar *
gdict_database_get_full_name (GdictDatabase *db)
{
g_return_val_if_fail (db != NULL, NULL);
return db->full_name;
}
/*****************
* GdictStrategy *
*****************/
GDICT_DEFINE_BOXED_TYPE (GdictStrategy, gdict_strategy);
GdictStrategy *
_gdict_strategy_new (const gchar *name)
{
GdictStrategy *strat;
g_return_val_if_fail (name != NULL, NULL);
strat = g_slice_new (GdictStrategy);
strat->name = g_strdup (name);
strat->description = NULL;
strat->ref_count = 1;
return strat;
}
/**
* gdict_strategy_ref:
* @strat: a #GdictStrategy
*
* Increases the reference count of @strat by one.
*
* Return value: the #GdictStrategy object with its reference count
* increased
*
* Since: 1.0
*/
GdictStrategy *
gdict_strategy_ref (GdictStrategy *strat)
{
g_return_val_if_fail (strat != NULL, NULL);
g_assert (strat->ref_count != 0);
strat->ref_count += 1;
return strat;
}
/**
* gdict_strategy_unref:
* @strat: a #GdictStrategy
*
* Decreases the reference count of @strat by one. If the reference count
* reaches zero, the #GdictStrategy object is freed.
*
* Since: 1.0
*/
void
gdict_strategy_unref (GdictStrategy *strat)
{
g_return_if_fail (strat != NULL);
g_assert (strat->ref_count != 0);
strat->ref_count -= 1;
if (strat->ref_count == 0)
{
g_free (strat->name);
g_free (strat->description);
g_slice_free (GdictStrategy, strat);
}
}
/**
* gdict_strategy_get_name:
* @strat: a #GdictStrategy
*
* FIXME
*
* Return value: FIXME
*
* Since: 1.0
*/
const gchar *
gdict_strategy_get_name (GdictStrategy *strat)
{
g_return_val_if_fail (strat != NULL, NULL);
return strat->name;
}
/**
* gdict_strategy_get_description:
* @strat: a #GdictStrategy
*
* FIXME
*
* Return value: FIXME
*
* Since: 1.0
*/
const gchar *
gdict_strategy_get_description (GdictStrategy *strat)
{
g_return_val_if_fail (strat != NULL, NULL);
return strat->description;
}
/**************
* GdictMatch *
**************/
GDICT_DEFINE_BOXED_TYPE (GdictMatch, gdict_match);
GdictMatch *
_gdict_match_new (const gchar *word)
{
GdictMatch *match;
g_return_val_if_fail (word != NULL, NULL);
match = g_slice_new (GdictMatch);
match->word = g_strdup (word);
match->database = NULL;
match->ref_count = 1;
return match;
}
/**
* gdict_match_ref:
* @match: a #GdictMatch
*
* FIXME
*
* Return value: FIXME
*
* Since: 1.0
*/
GdictMatch *
gdict_match_ref (GdictMatch *match)
{
g_return_val_if_fail (match != NULL, NULL);
g_assert (match->ref_count != 0);
match->ref_count += 1;
return match;
}
/**
* gdict_match_unref:
* @match: a #GdictMatch
*
* FIXME
*
* Since: 1.0
*/
void
gdict_match_unref (GdictMatch *match)
{
g_return_if_fail (match != NULL);
g_assert (match->ref_count != 0);
match->ref_count -= 1;
if (match->ref_count == 0)
{
g_free (match->word);
g_free (match->database);
g_slice_free (GdictMatch, match);
}
}
/**
* gdict_match_get_word:
* @match: a #GdictMatch
*
* FIXME
*
* Return value: FIXME
*
* Since: 1.0
*/
const gchar *
gdict_match_get_word (GdictMatch *match)
{
g_return_val_if_fail (match != NULL, NULL);
return match->word;
}
/**
* gdict_match_get_database:
* @match: a #GdictMatch
*
* FIXME
*
* Return value: FIXME
*
* Since: 1.0
*/
const gchar *
gdict_match_get_database (GdictMatch *match)
{
g_return_val_if_fail (match != NULL, NULL);
return match->database;
}
/*******************
* GdictDefinition *
*******************/
GDICT_DEFINE_BOXED_TYPE (GdictDefinition, gdict_definition);
/* GdictDefinition constructor */
GdictDefinition *
_gdict_definition_new (gint total)
{
GdictDefinition *def;
def = g_slice_new (GdictDefinition);
def->total = total;
def->word = NULL;
def->database_name = NULL;
def->database_full = NULL;
def->ref_count = 1;
return def;
}
/**
* gdict_definition_ref:
* @def: a #GdictDefinition
*
* Increases the reference count of @def by one.
*
* Return value: the #GdictDefinition object with its reference count
* increased.
*
* Since: 1.0
*/
GdictDefinition *
gdict_definition_ref (GdictDefinition *def)
{
g_return_val_if_fail (def != NULL, NULL);
g_assert (def->ref_count != 0);
def->ref_count += 1;
return def;
}
/**
* gdict_definition_unref:
* @def: a #GdictDefinition
*
* Decreases the reference count of @def by one. If the reference count
* reaches zero, the #GdictDefinition object is freed.
*
* Since: 1.0
*/
void
gdict_definition_unref (GdictDefinition *def)
{
g_return_if_fail (def != NULL);
g_assert (def->ref_count != 0);
def->ref_count -= 1;
if (def->ref_count == 0)
{
g_free (def->word);
g_free (def->database_name);
g_free (def->database_full);
g_slice_free (GdictDefinition, def);
}
}
/**
* gdict_definition_get_total:
* @def: a #GdictDefinition
*
* Retrieves the total number of definitions that were found on a
* dictionary.
*
* Return value: the number of definitions.
*
* Since: 1.0
*/
gint
gdict_definition_get_total (GdictDefinition *def)
{
g_return_val_if_fail (def != NULL, -1);
return def->total;
}
/**
* gdict_definition_get_word:
* @def: a #GdictDefinition
*
* Retrieves the word used by the dictionary database to store
* the definition.
*
* Return value: a word. The returned string is owned by the
* #GdictDefinition object and should not be modified or freed.
*
* Since: 1.0
*/
const gchar *
gdict_definition_get_word (GdictDefinition *def)
{
g_return_val_if_fail (def != NULL, NULL);
return def->word;
}
/**
* gdict_definition_get_database:
* @def: a #GdictDefinition
*
* Retrieves the full name of the dictionary database where the
* definition is stored.
*
* Return value: the full name of a database. The returned string
* is owned by the #GdictDefinition object and should not be
* modified or freed.
*
* Since: 1.0
*/
const gchar *
gdict_definition_get_database (GdictDefinition *def)
{
g_return_val_if_fail (def != NULL, NULL);
return def->database_full;
}
/**
* gdict_definition_get_text:
* @def: a #GdictDefinition
*
* Retrieves the text of the definition.
*
* Return value: the text of the definition. The returned string
* is owned by the #GdictDefinition object, and should not be
* modified or freed.
*
* Since: 1.0
*/
const gchar *
gdict_definition_get_text (GdictDefinition *def)
{
g_return_val_if_fail (def != NULL, NULL);
return def->definition;
}
|