~ubuntu-branches/ubuntu/karmic/mergeant/karmic

« back to all changes in this revision

Viewing changes to libmergeant/handlers/plugins/mg-handler-text.c

  • Committer: Bazaar Package Importer
  • Author(s): Gustavo R. Montesino
  • Date: 2007-11-29 08:44:48 UTC
  • mfrom: (2.1.4 hardy)
  • Revision ID: james.westby@ubuntu.com-20071129084448-6aon73d22bv6hzfw
Tags: 0.67-3
* Re-enable installation of the mime files in mergeant.install
* mergeant.dirs: create usr/share/mime/packages to make dh_installmime add
  the update-mime-database code snippets

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* mg-handler-text.c
2
 
 *
3
 
 * Copyright (C) 2003 Vivien Malerba
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License as
7
 
 * published by the Free Software Foundation; either version 2 of the
8
 
 * License, or (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
 
 * USA
19
 
 */
20
 
 
21
 
#include "mg-handler-text.h"
22
 
/*#include "mg-entry-text.h"*/
23
 
#include <libmergeant/handlers/mg-entry-string.h>
24
 
#include <libmergeant/mg-server.h>
25
 
#include <gmodule.h>
26
 
 
27
 
static void mg_handler_text_class_init (MgHandlerTextClass * class);
28
 
static void mg_handler_text_init (MgHandlerText * wid);
29
 
static void mg_handler_text_dispose (GObject   * object);
30
 
 
31
 
/* plugin initialisation */
32
 
MgDataHandler* plugin_init (MgServer *srv, GModule *module);
33
 
 
34
 
/* MgDataHandler interface */
35
 
static void         mg_handler_text_data_handler_init      (MgDataHandlerIface *iface);
36
 
static MgDataEntry *mg_handler_text_get_entry_from_value   (MgDataHandler *dh, const GdaValue *value, 
37
 
                                                              GdaValueType type);
38
 
static gchar       *mg_handler_text_get_sql_from_value     (MgDataHandler *dh, const GdaValue *value);
39
 
static gchar       *mg_handler_text_get_str_from_value     (MgDataHandler *dh, const GdaValue *value);
40
 
static GdaValue    *mg_handler_text_get_value_from_sql     (MgDataHandler *dh, const gchar *sql, 
41
 
                                                              GdaValueType type);
42
 
static GdaValue    *mg_handler_text_get_sane_init_value    (MgDataHandler * dh, GdaValueType type);
43
 
 
44
 
static guint        mg_handler_text_get_nb_gda_types       (MgDataHandler *dh);
45
 
static GdaValueType mg_handler_text_get_gda_type_index     (MgDataHandler *dh, guint index);
46
 
static gboolean     mg_handler_text_accepts_gda_type       (MgDataHandler * dh, GdaValueType type);
47
 
 
48
 
static const gchar *mg_handler_text_get_descr              (MgDataHandler *dh);
49
 
static const gchar *mg_handler_text_get_descr_detail       (MgDataHandler *dh);
50
 
static const gchar *mg_handler_text_get_version            (MgDataHandler *dh);
51
 
static gboolean     mg_handler_text_is_plugin              (MgDataHandler *dh);
52
 
static const gchar *mg_handler_text_get_plugin_name        (MgDataHandler *dh);
53
 
static const gchar *mg_handler_text_get_plugin_file        (MgDataHandler *dh);
54
 
static gchar       *mg_handler_text_get_key                (MgDataHandler *dh);
55
 
 
56
 
 
57
 
/* signals */
58
 
enum
59
 
{
60
 
        LAST_SIGNAL
61
 
};
62
 
 
63
 
static gint mg_handler_text_signals[LAST_SIGNAL] = { };
64
 
 
65
 
struct  _MgHandlerTextPriv {
66
 
        gchar          *detailled_descr;
67
 
        guint           nb_gda_types;
68
 
        GdaValueType   *valid_gda_types;
69
 
        MgServer       *srv;
70
 
        MgDataHandler  *string_handler;
71
 
 
72
 
        GModule        *module;
73
 
};
74
 
 
75
 
/* get a pointer to the parents to be able to call their destructor */
76
 
static GObjectClass *parent_class = NULL;
77
 
 
78
 
guint
79
 
mg_handler_text_get_type (void)
80
 
{
81
 
        static GType type = 0;
82
 
 
83
 
        if (!type) {
84
 
                static const GTypeInfo info = {
85
 
                        sizeof (MgHandlerTextClass),
86
 
                        (GBaseInitFunc) NULL,
87
 
                        (GBaseFinalizeFunc) NULL,
88
 
                        (GClassInitFunc) mg_handler_text_class_init,
89
 
                        NULL,
90
 
                        NULL,
91
 
                        sizeof (MgHandlerText),
92
 
                        0,
93
 
                        (GInstanceInitFunc) mg_handler_text_init
94
 
                };              
95
 
 
96
 
                static const GInterfaceInfo data_entry_info = {
97
 
                        (GInterfaceInitFunc) mg_handler_text_data_handler_init,
98
 
                        NULL,
99
 
                        NULL
100
 
                };
101
 
 
102
 
                type = g_type_register_static (MG_BASE_TYPE, "MgHandlerText", &info, 0);
103
 
                g_type_add_interface_static (type, MG_DATA_HANDLER_TYPE, &data_entry_info);
104
 
        }
105
 
        return type;
106
 
}
107
 
 
108
 
static void
109
 
mg_handler_text_data_handler_init (MgDataHandlerIface *iface)
110
 
{
111
 
        iface->get_entry_from_value = mg_handler_text_get_entry_from_value;
112
 
        iface->get_sql_from_value = mg_handler_text_get_sql_from_value;
113
 
        iface->get_str_from_value = mg_handler_text_get_str_from_value;
114
 
        iface->get_value_from_sql = mg_handler_text_get_value_from_sql;
115
 
        iface->get_value_from_str = NULL;
116
 
        iface->get_sane_init_value = mg_handler_text_get_sane_init_value;
117
 
        iface->get_nb_gda_types = mg_handler_text_get_nb_gda_types;
118
 
        iface->accepts_gda_type = mg_handler_text_accepts_gda_type;
119
 
        iface->get_gda_type_index = mg_handler_text_get_gda_type_index;
120
 
        iface->get_descr = mg_handler_text_get_descr;
121
 
        iface->get_descr_detail = mg_handler_text_get_descr_detail;
122
 
        iface->get_version = mg_handler_text_get_version;
123
 
        iface->is_plugin = mg_handler_text_is_plugin;
124
 
        iface->get_plugin_name = mg_handler_text_get_plugin_name;
125
 
        iface->get_plugin_file = mg_handler_text_get_plugin_file;
126
 
        iface->get_key = mg_handler_text_get_key;
127
 
}
128
 
 
129
 
 
130
 
static void
131
 
mg_handler_text_class_init (MgHandlerTextClass * class)
132
 
{
133
 
        GObjectClass   *object_class = G_OBJECT_CLASS (class);
134
 
        
135
 
        parent_class = g_type_class_peek_parent (class);
136
 
 
137
 
        object_class->dispose = mg_handler_text_dispose;
138
 
}
139
 
 
140
 
static void
141
 
mg_handler_text_init (MgHandlerText * hdl)
142
 
{
143
 
        /* Private structure */
144
 
        hdl->priv = g_new0 (MgHandlerTextPriv, 1);
145
 
        hdl->priv->detailled_descr = _("Data handler for string data types, presented as a text area");
146
 
        hdl->priv->nb_gda_types = 1;
147
 
        hdl->priv->valid_gda_types = g_new0 (GdaValueType, 1);
148
 
        hdl->priv->valid_gda_types[0] = GDA_VALUE_TYPE_STRING;
149
 
        hdl->priv->srv = NULL;
150
 
        hdl->priv->string_handler = NULL;
151
 
        hdl->priv->module = NULL;
152
 
 
153
 
        mg_base_set_name (MG_BASE (hdl), mg_handler_text_get_plugin_name (MG_DATA_HANDLER (hdl)));
154
 
        mg_base_set_description (MG_BASE (hdl), _("Text area representation"));
155
 
}
156
 
 
157
 
static void
158
 
mg_handler_text_dispose (GObject *object)
159
 
{
160
 
        MgHandlerText *hdl;
161
 
 
162
 
        g_return_if_fail (object != NULL);
163
 
        g_return_if_fail (IS_MG_HANDLER_TEXT (object));
164
 
 
165
 
        hdl = MG_HANDLER_TEXT (object);
166
 
 
167
 
        if (hdl->priv) {
168
 
                mg_base_nullify_check (MG_BASE (object));
169
 
                
170
 
                g_free (hdl->priv->valid_gda_types);
171
 
                hdl->priv->valid_gda_types = NULL;
172
 
                
173
 
                if (hdl->priv->srv)
174
 
                        g_object_remove_weak_pointer (G_OBJECT (hdl->priv->srv),
175
 
                                                      (gpointer *) & (hdl->priv->srv));
176
 
 
177
 
                g_free (hdl->priv);
178
 
                hdl->priv = NULL;
179
 
        }
180
 
 
181
 
        /* for the parent class */
182
 
        parent_class->dispose (object);
183
 
}
184
 
 
185
 
/*
186
 
 * Plugin initialization
187
 
 */
188
 
MgDataHandler*
189
 
plugin_init (MgServer *srv, GModule *module)
190
 
{
191
 
        MgHandlerText *text;
192
 
 
193
 
        text = MG_HANDLER_TEXT (mg_handler_text_new (srv));
194
 
        text->priv->module = module;
195
 
        
196
 
        return MG_DATA_HANDLER (text);
197
 
}
198
 
 
199
 
 
200
 
/**
201
 
 * mg_handler_text_new
202
 
 * @srv: a #MgServer object
203
 
 *
204
 
 * Creates a data handler for texts
205
 
 *
206
 
 * Returns: the new object
207
 
 */
208
 
GObject *
209
 
mg_handler_text_new (MgServer *srv)
210
 
{
211
 
        GObject *obj;
212
 
        MgHandlerText *hdl;
213
 
 
214
 
        g_return_val_if_fail (srv && IS_MG_SERVER (srv), NULL);
215
 
        obj = g_object_new (MG_HANDLER_TEXT_TYPE, NULL);
216
 
        hdl = MG_HANDLER_TEXT (obj);
217
 
 
218
 
        g_object_add_weak_pointer (G_OBJECT (srv), (gpointer *) &(hdl->priv->srv));
219
 
        hdl->priv->srv = srv;
220
 
 
221
 
        /* get a pointer to the String Handler which we will use */
222
 
        hdl->priv->string_handler = mg_server_get_handler_by_gda (srv, GDA_VALUE_TYPE_STRING);
223
 
        g_assert (hdl->priv->string_handler);
224
 
 
225
 
        return obj;
226
 
}
227
 
 
228
 
 
229
 
/* Interface implementation */
230
 
static MgDataEntry *
231
 
mg_handler_text_get_entry_from_value (MgDataHandler *iface, const GdaValue *value, GdaValueType type)
232
 
{
233
 
        MgHandlerText *hdl;
234
 
        MgDataEntry *de;
235
 
        GdaValueType real_type;
236
 
 
237
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
238
 
        hdl = MG_HANDLER_TEXT (iface);
239
 
        g_return_val_if_fail (hdl->priv, NULL);
240
 
 
241
 
        if (value && (gda_value_get_type (value) != GDA_VALUE_TYPE_NULL)) {
242
 
                real_type = gda_value_get_type (value);
243
 
                g_return_val_if_fail (mg_data_handler_accepts_gda_type (iface, type), NULL);
244
 
        }
245
 
        else
246
 
                real_type = type;
247
 
 
248
 
        de = MG_DATA_ENTRY (mg_entry_string_new (iface, real_type));
249
 
        g_object_set (G_OBJECT (de), "multiline", TRUE, NULL);
250
 
        if (value && (gda_value_get_type (value) != GDA_VALUE_TYPE_NULL))
251
 
                mg_data_entry_set_value (de, value);
252
 
        else 
253
 
                mg_data_entry_set_value (de, NULL);
254
 
 
255
 
        return de;
256
 
}
257
 
 
258
 
static gchar *
259
 
mg_handler_text_get_sql_from_value (MgDataHandler *iface, const GdaValue *value)
260
 
{
261
 
        MgHandlerText *hdl;
262
 
 
263
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
264
 
        hdl = MG_HANDLER_TEXT (iface);
265
 
        g_return_val_if_fail (hdl->priv, NULL);
266
 
 
267
 
        return mg_data_handler_get_sql_from_value (hdl->priv->string_handler, value);
268
 
}
269
 
 
270
 
static gchar *
271
 
mg_handler_text_get_str_from_value (MgDataHandler *iface, const GdaValue *value)
272
 
{
273
 
        MgHandlerText *hdl;
274
 
 
275
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
276
 
        hdl = MG_HANDLER_TEXT (iface);
277
 
        g_return_val_if_fail (hdl->priv, NULL);
278
 
 
279
 
        return mg_data_handler_get_str_from_value (hdl->priv->string_handler, value);
280
 
}
281
 
 
282
 
static GdaValue *
283
 
mg_handler_text_get_value_from_sql (MgDataHandler *iface, const gchar *sql, GdaValueType type)
284
 
{
285
 
        MgHandlerText *hdl;
286
 
 
287
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
288
 
        hdl = MG_HANDLER_TEXT (iface);
289
 
        g_return_val_if_fail (hdl->priv, NULL);
290
 
 
291
 
        return mg_data_handler_get_value_from_sql (hdl->priv->string_handler, sql, type);
292
 
}
293
 
 
294
 
static GdaValue *
295
 
mg_handler_text_get_sane_init_value (MgDataHandler *iface, GdaValueType type)
296
 
{
297
 
        MgHandlerText *hdl;
298
 
 
299
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
300
 
        hdl = MG_HANDLER_TEXT (iface);
301
 
        g_return_val_if_fail (hdl->priv, NULL);
302
 
 
303
 
        return mg_data_handler_get_sane_init_value (hdl->priv->string_handler, type);
304
 
}
305
 
 
306
 
static guint
307
 
mg_handler_text_get_nb_gda_types (MgDataHandler *iface)
308
 
{
309
 
        MgHandlerText *hdl;
310
 
 
311
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), 0);
312
 
        hdl = MG_HANDLER_TEXT (iface);
313
 
        g_return_val_if_fail (hdl->priv, 0);
314
 
 
315
 
        return hdl->priv->nb_gda_types;
316
 
}
317
 
 
318
 
 
319
 
static gboolean
320
 
mg_handler_text_accepts_gda_type (MgDataHandler *iface, GdaValueType type)
321
 
{
322
 
        MgHandlerText *hdl;
323
 
        guint i = 0;
324
 
        gboolean found = FALSE;
325
 
 
326
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), FALSE);
327
 
        g_return_val_if_fail (type != GDA_VALUE_TYPE_UNKNOWN, FALSE);
328
 
        hdl = MG_HANDLER_TEXT (iface);
329
 
        g_return_val_if_fail (hdl->priv, 0);
330
 
 
331
 
        while (!found && (i < hdl->priv->nb_gda_types)) {
332
 
                if (hdl->priv->valid_gda_types [i] == type)
333
 
                        found = TRUE;
334
 
                i++;
335
 
        }
336
 
 
337
 
        return found;
338
 
}
339
 
 
340
 
static GdaValueType
341
 
mg_handler_text_get_gda_type_index (MgDataHandler *iface, guint index)
342
 
{
343
 
        MgHandlerText *hdl;
344
 
 
345
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), GDA_VALUE_TYPE_UNKNOWN);
346
 
        hdl = MG_HANDLER_TEXT (iface);
347
 
        g_return_val_if_fail (hdl->priv, GDA_VALUE_TYPE_UNKNOWN);
348
 
        g_return_val_if_fail (index < hdl->priv->nb_gda_types, GDA_VALUE_TYPE_UNKNOWN);
349
 
 
350
 
        return hdl->priv->valid_gda_types[index];
351
 
}
352
 
 
353
 
static const gchar *
354
 
mg_handler_text_get_descr (MgDataHandler *iface)
355
 
{
356
 
        MgHandlerText *hdl;
357
 
 
358
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
359
 
        hdl = MG_HANDLER_TEXT (iface);
360
 
        g_return_val_if_fail (hdl->priv, NULL);
361
 
 
362
 
        return mg_base_get_description (MG_BASE (hdl));
363
 
}
364
 
 
365
 
static const gchar *
366
 
mg_handler_text_get_descr_detail (MgDataHandler *iface)
367
 
{
368
 
        MgHandlerText *hdl;
369
 
 
370
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
371
 
        hdl = MG_HANDLER_TEXT (iface);
372
 
        g_return_val_if_fail (hdl->priv, NULL);
373
 
 
374
 
        return hdl->priv->detailled_descr;
375
 
}
376
 
 
377
 
static const gchar *
378
 
mg_handler_text_get_version (MgDataHandler *iface)
379
 
{
380
 
        MgHandlerText *hdl;
381
 
 
382
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
383
 
        hdl = MG_HANDLER_TEXT (iface);
384
 
        g_return_val_if_fail (hdl->priv, NULL);
385
 
        
386
 
        return g_strdup ("V1R0");
387
 
}
388
 
 
389
 
static gboolean
390
 
mg_handler_text_is_plugin (MgDataHandler *iface)
391
 
{
392
 
        MgHandlerText *hdl;
393
 
 
394
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), FALSE);
395
 
        hdl = MG_HANDLER_TEXT (iface);
396
 
        g_return_val_if_fail (hdl->priv, FALSE);
397
 
 
398
 
        return TRUE;
399
 
}
400
 
 
401
 
static const gchar *
402
 
mg_handler_text_get_plugin_name (MgDataHandler *iface)
403
 
{
404
 
        MgHandlerText *hdl;
405
 
 
406
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
407
 
        hdl = MG_HANDLER_TEXT (iface);
408
 
        g_return_val_if_fail (hdl->priv, NULL);
409
 
        
410
 
        return "Text Plugin";
411
 
}
412
 
 
413
 
static const gchar *
414
 
mg_handler_text_get_plugin_file (MgDataHandler *iface)
415
 
{
416
 
        MgHandlerText *hdl;
417
 
 
418
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
419
 
        hdl = MG_HANDLER_TEXT (iface);
420
 
        g_return_val_if_fail (hdl->priv, NULL);
421
 
        
422
 
        if (hdl->priv->module)
423
 
                return g_module_name (hdl->priv->module);
424
 
        else
425
 
                return NULL;
426
 
}
427
 
 
428
 
static gchar *
429
 
mg_handler_text_get_key (MgDataHandler *iface)
430
 
{
431
 
        MgHandlerText *hdl;
432
 
 
433
 
        g_return_val_if_fail (iface && IS_MG_HANDLER_TEXT (iface), NULL);
434
 
        hdl = MG_HANDLER_TEXT (iface);
435
 
        g_return_val_if_fail (hdl->priv, NULL);
436
 
        
437
 
        return g_strdup (mg_base_get_name (MG_BASE (hdl)));
438
 
}
439