1 /*
2 * Copyright (C) 2008 Ignacio Casal Quinteiro <nacho.resa@gmail.com>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "gtr-translation-memory.h"
19
G_DEFINE_INTERFACE(GtrTranslationMemory,gtr_translation_memory,G_TYPE_OBJECT)20 G_DEFINE_INTERFACE (GtrTranslationMemory, gtr_translation_memory, G_TYPE_OBJECT)
21
22 /**
23 * gtr_translation_memory_store:
24 * @obj: a #GtrTranslationMemory
25 * @msg: message
26 *
27 * Stores the @msg in the database.
28 */
29 gboolean
30 gtr_translation_memory_store (GtrTranslationMemory * obj, GtrMsg * msg)
31 {
32 g_return_val_if_fail (GTR_IS_TRANSLATION_MEMORY (obj), FALSE);
33 return GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->store (obj, msg);
34 }
35
36 /* Default implementation */
37 static gboolean
gtr_translation_memory_store_default(GtrTranslationMemory * obj,GtrMsg * msg)38 gtr_translation_memory_store_default (GtrTranslationMemory * obj, GtrMsg * msg)
39 {
40 g_return_val_if_reached (FALSE);
41 }
42
43 /**
44 * gtr_translation_memory_store_list:
45 * @obj: a #GtrTranslationMemory
46 * @msgs: list of messages (#GtrMsg)
47 *
48 * Stores the messages from @msgs in the database.
49 */
50 gboolean
gtr_translation_memory_store_list(GtrTranslationMemory * obj,GList * msgs)51 gtr_translation_memory_store_list (GtrTranslationMemory * obj, GList * msgs)
52 {
53 g_return_val_if_fail (GTR_IS_TRANSLATION_MEMORY (obj), FALSE);
54 return GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->store_list (obj, msgs);
55 }
56
57 /* Default implementation */
58 static gboolean
gtr_translation_memory_store_list_default(GtrTranslationMemory * obj,GList * msgs)59 gtr_translation_memory_store_list_default (GtrTranslationMemory * obj,
60 GList * msgs)
61 {
62 GList * l;
63
64 for (l = msgs; l; l = g_list_next (l))
65 {
66 GtrMsg *msg = GTR_MSG (l->data);
67 gboolean result;
68
69 if (!gtr_msg_is_translated (msg))
70 continue;
71
72 result = gtr_translation_memory_store (obj, msg);
73 if (!result)
74 return FALSE;
75 }
76
77 return TRUE;
78 }
79
80 /**
81 * gtr_translation_memory_remove:
82 * @obj: a #GtrTranslationMemory
83 * @original: the original message.
84 * @translation: the translation to remove.
85 *
86 * Removes @translation of @original from translation memory.
87 */
88 void
gtr_translation_memory_remove(GtrTranslationMemory * obj,gint translation_id)89 gtr_translation_memory_remove (GtrTranslationMemory * obj,
90 gint translation_id)
91 {
92 g_return_if_fail (GTR_IS_TRANSLATION_MEMORY (obj));
93 return GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->remove (obj,
94 translation_id);
95 }
96
97 /* Default implementation */
98 static void
gtr_translation_memory_remove_default(GtrTranslationMemory * obj,gint translation_id)99 gtr_translation_memory_remove_default (GtrTranslationMemory * obj,
100 gint translation_id)
101 {
102 }
103
104 /**
105 * gtr_translation_memory_lookup:
106 * @obj: a #GtrTranslationMemory
107 * @phrase: the unstranslated text to search for translations.
108 *
109 * Looks for the @phrase in the database and gets a list of the #GtrTranslationMemoryMatch.
110 *
111 * Returns: a list of #GtrTranslationMemoryMatch.
112 */
113 GList *
gtr_translation_memory_lookup(GtrTranslationMemory * obj,const gchar * phrase)114 gtr_translation_memory_lookup (GtrTranslationMemory * obj,
115 const gchar * phrase)
116 {
117 g_return_val_if_fail (GTR_IS_TRANSLATION_MEMORY (obj), 0);
118 return GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->lookup (obj, phrase);
119 }
120
121 /* Default implementation */
122 static GList *
gtr_translation_memory_lookup_default(GtrTranslationMemory * obj,const gchar * phrase)123 gtr_translation_memory_lookup_default (GtrTranslationMemory *obj,
124 const gchar * phrase)
125 {
126 g_return_val_if_reached (0);
127 }
128
129 /**
130 * gtr_translation_memory_set_max_omits:
131 * @omits: the number of omits
132 *
133 * Sets the number of omits used in the search.
134 */
135 void
gtr_translation_memory_set_max_omits(GtrTranslationMemory * obj,gsize omits)136 gtr_translation_memory_set_max_omits (GtrTranslationMemory * obj, gsize omits)
137 {
138 g_return_if_fail (GTR_IS_TRANSLATION_MEMORY (obj));
139 GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->set_max_omits (obj, omits);
140 }
141
142 /* Default implementation */
143 static void
gtr_translation_memory_set_max_omits_default(GtrTranslationMemory * obj,gsize omits)144 gtr_translation_memory_set_max_omits_default (GtrTranslationMemory * obj,
145 gsize omits)
146 {
147 g_return_if_reached ();
148 }
149
150 /**
151 * gtr_translation_memory_set_max_delta:
152 * @delta: the difference in the length of strings
153 *
154 * Sets the difference in the length of string for searching in the database.
155 */
156 void
gtr_translation_memory_set_max_delta(GtrTranslationMemory * obj,gsize delta)157 gtr_translation_memory_set_max_delta (GtrTranslationMemory * obj, gsize delta)
158 {
159 g_return_if_fail (GTR_IS_TRANSLATION_MEMORY (obj));
160 GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->set_max_delta (obj, delta);
161 }
162
163 /* Default implementation */
164 static void
gtr_translation_memory_set_max_delta_default(GtrTranslationMemory * obj,gsize omits)165 gtr_translation_memory_set_max_delta_default
166 (GtrTranslationMemory * obj, gsize omits)
167 {
168 g_return_if_reached ();
169 }
170
171 /**
172 * gtr_translation_memory_set_max_items:
173 * @items: the max item to return in lookup
174 *
175 * Sets the number of item to return in gtr_translation_memory_lookup().
176 */
177 void
gtr_translation_memory_set_max_items(GtrTranslationMemory * obj,gint items)178 gtr_translation_memory_set_max_items (GtrTranslationMemory * obj, gint items)
179 {
180 g_return_if_fail (GTR_IS_TRANSLATION_MEMORY (obj));
181 GTR_TRANSLATION_MEMORY_GET_IFACE (obj)->set_max_items (obj, items);
182 }
183
184 /* Default implementation */
185 static void
gtr_translation_memory_set_max_items_default(GtrTranslationMemory * obj,gint items)186 gtr_translation_memory_set_max_items_default (GtrTranslationMemory * obj, gint items)
187 {
188 g_return_if_reached ();
189 }
190
191 static void
gtr_translation_memory_default_init(GtrTranslationMemoryInterface * iface)192 gtr_translation_memory_default_init (GtrTranslationMemoryInterface *iface)
193 {
194 static gboolean initialized = FALSE;
195
196 iface->store = gtr_translation_memory_store_default;
197 iface->store_list = gtr_translation_memory_store_list_default;
198 iface->remove = gtr_translation_memory_remove_default;
199 iface->lookup = gtr_translation_memory_lookup_default;
200 iface->set_max_omits = gtr_translation_memory_set_max_omits_default;
201 iface->set_max_delta = gtr_translation_memory_set_max_delta_default;
202 iface->set_max_items = gtr_translation_memory_set_max_items_default;
203
204 if (!initialized)
205 initialized = TRUE;
206 }
207
208
209