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

« back to all changes in this revision

Viewing changes to libmergeant/mg-qfield.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-qfield.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 <string.h>
22
 
#include "mg-qfield.h"
23
 
#include "mg-query.h"
24
 
#include "mg-field.h"
25
 
#include "mg-qf-all.h"
26
 
#include "mg-qf-field.h"
27
 
#include "mg-qf-value.h"
28
 
#include "mg-qf-func.h"
29
 
#include "mg-xml-storage.h"
30
 
#include "mg-ref-base.h"
31
 
#include "mg-server.h"
32
 
 
33
 
/* 
34
 
 * Main static functions 
35
 
 */
36
 
static void mg_qfield_class_init (MgQfieldClass *class);
37
 
static void mg_qfield_init (MgQfield *qfield);
38
 
static void mg_qfield_dispose (GObject *object);
39
 
static void mg_qfield_finalize (GObject *object);
40
 
 
41
 
static void mg_qfield_set_property    (GObject              *object,
42
 
                                       guint                 param_id,
43
 
                                       const GValue         *value,
44
 
                                       GParamSpec           *pspec);
45
 
static void mg_qfield_get_property    (GObject              *object,
46
 
                                       guint                 param_id,
47
 
                                       GValue               *value,
48
 
                                       GParamSpec           *pspec);
49
 
 
50
 
static void attach_to_query (MgQfield *qfield, MgQuery *query);
51
 
 
52
 
/* get a pointer to the parents to be able to call their destructor */
53
 
static GObjectClass  *parent_class = NULL;
54
 
 
55
 
/* signals */
56
 
enum
57
 
{
58
 
        LAST_SIGNAL
59
 
};
60
 
 
61
 
static gint mg_qfield_signals[LAST_SIGNAL] = { };
62
 
 
63
 
/* properties */
64
 
enum
65
 
{
66
 
        PROP_0,
67
 
        PROP
68
 
};
69
 
 
70
 
 
71
 
struct _MgQfieldPrivate
72
 
{
73
 
        gchar     *alias;
74
 
        gboolean   visible;
75
 
        gboolean   internal;
76
 
};
77
 
 
78
 
/* module error */
79
 
GQuark mg_qfield_error_quark (void)
80
 
{
81
 
        static GQuark quark;
82
 
        if (!quark)
83
 
                quark = g_quark_from_static_string ("mg_qfield_error");
84
 
        return quark;
85
 
}
86
 
 
87
 
 
88
 
guint
89
 
mg_qfield_get_type (void)
90
 
{
91
 
        static GType type = 0;
92
 
 
93
 
        if (!type) {
94
 
                static const GTypeInfo info = {
95
 
                        sizeof (MgQfieldClass),
96
 
                        (GBaseInitFunc) NULL,
97
 
                        (GBaseFinalizeFunc) NULL,
98
 
                        (GClassInitFunc) mg_qfield_class_init,
99
 
                        NULL,
100
 
                        NULL,
101
 
                        sizeof (MgQfield),
102
 
                        0,
103
 
                        (GInstanceInitFunc) mg_qfield_init
104
 
                };
105
 
                
106
 
                type = g_type_register_static (MG_BASE_TYPE, "MgQfield", &info, 0);
107
 
        }
108
 
        return type;
109
 
}
110
 
 
111
 
static void
112
 
mg_qfield_class_init (MgQfieldClass * class)
113
 
{
114
 
        GObjectClass   *object_class = G_OBJECT_CLASS (class);
115
 
 
116
 
        parent_class = g_type_class_peek_parent (class);
117
 
 
118
 
 
119
 
 
120
 
        /* virtual functions */
121
 
        class->copy = NULL;
122
 
        class->is_equal = NULL;
123
 
 
124
 
        object_class->dispose = mg_qfield_dispose;
125
 
        object_class->finalize = mg_qfield_finalize;
126
 
 
127
 
        /* Properties */
128
 
        object_class->set_property = mg_qfield_set_property;
129
 
        object_class->get_property = mg_qfield_get_property;
130
 
        g_object_class_install_property (object_class, PROP,
131
 
                                         g_param_spec_pointer ("prop", NULL, NULL, 
132
 
                                                               (G_PARAM_READABLE | G_PARAM_WRITABLE)));
133
 
}
134
 
 
135
 
static void
136
 
mg_qfield_init (MgQfield *qfield)
137
 
{
138
 
        qfield->priv = g_new0 (MgQfieldPrivate, 1);
139
 
        qfield->priv->alias = NULL;
140
 
        qfield->priv->visible = TRUE;
141
 
        qfield->priv->internal = FALSE;
142
 
}
143
 
 
144
 
/**
145
 
 * mg_qfield_new_from_xml
146
 
 * @query: a #MgQuery object
147
 
 * @node: an XML node corresponding to a MG_QFIELD tag
148
 
 * @error: location to store error, or %NULL
149
 
 *
150
 
 * This is an object factory which does create instances of class inheritants of the #MgDfield class.
151
 
 * Ths #MgQfield object MUST then be attached to @query
152
 
 * 
153
 
 * Returns: the newly created object
154
 
 */
155
 
GObject   *
156
 
mg_qfield_new_from_xml (MgQuery *query, xmlNodePtr node, GError **error)
157
 
{
158
 
        GObject *obj = NULL;
159
 
        gchar *prop;
160
 
 
161
 
        g_return_val_if_fail (query && IS_MG_QUERY (query), NULL);
162
 
        g_return_val_if_fail (node, NULL);
163
 
        g_return_val_if_fail (!strcmp (node->name, "MG_QF"), NULL);
164
 
 
165
 
        prop = xmlGetProp (node, "type");
166
 
        if (prop) {
167
 
                gboolean done = FALSE;
168
 
                if (!strcmp (prop, "ALL")) {
169
 
                        gchar *target;
170
 
 
171
 
                        done = TRUE;
172
 
                        target = xmlGetProp (node, "target");
173
 
                        if (target) {
174
 
                                obj = mg_qf_all_new_with_xml_id (query, target);
175
 
                                g_free (target);
176
 
                        }
177
 
                        else {
178
 
                                g_set_error (error,
179
 
                                             MG_QF_ALL_ERROR,
180
 
                                             MG_QF_ALL_XML_LOAD_ERROR,
181
 
                                             _("Missing 'target' attribute in <MG_QF>"));
182
 
                                return NULL;
183
 
                        }
184
 
                }
185
 
 
186
 
                if (!done && !strcmp (prop, "FIELD")) {
187
 
                        gchar *target, *field;
188
 
 
189
 
                        done = TRUE;
190
 
                        target = xmlGetProp (node, "target");
191
 
                        field = xmlGetProp (node, "object");
192
 
                        if (target && field) 
193
 
                                obj = mg_qf_field_new_with_xml_ids (query, target, field);
194
 
 
195
 
                        if (target)
196
 
                                g_free (target);
197
 
                        if (field)
198
 
                                g_free (field);
199
 
 
200
 
                        if (!obj) {
201
 
                                g_set_error (error,
202
 
                                             MG_QF_ALL_ERROR,
203
 
                                             MG_QF_ALL_XML_LOAD_ERROR,
204
 
                                             _("Missing 'target' attribute in <MG_QF>"));
205
 
                                return NULL;
206
 
                        }
207
 
 
208
 
                }
209
 
 
210
 
                if (!done && !strcmp (prop, "AGG")) {
211
 
                        TO_IMPLEMENT;
212
 
                }
213
 
 
214
 
                if (!done && !strcmp (prop, "FUNC")) {
215
 
                        gchar *func;
216
 
 
217
 
                        func = xmlGetProp (node, "object");
218
 
                        if (func) {
219
 
                                obj = mg_qf_func_new_with_xml_id (query, func);
220
 
                                g_free (func);
221
 
                        }
222
 
                        
223
 
                        if (!obj) {
224
 
                                g_set_error (error,
225
 
                                             MG_QF_ALL_ERROR,
226
 
                                             MG_QF_ALL_XML_LOAD_ERROR,
227
 
                                             _("Missing 'object' attribute in <MG_QF>"));
228
 
                                return NULL;
229
 
                        }
230
 
                }
231
 
 
232
 
                if (!done && !strcmp (prop, "VAL")) {
233
 
                        gchar *srvt;
234
 
 
235
 
                        done = TRUE;
236
 
                        srvt = xmlGetProp (node, "srv_type");
237
 
                        if (srvt) {
238
 
                                MgServerDataType *dt;
239
 
 
240
 
                                dt = mg_server_get_data_type_by_xml_id (mg_conf_get_server (mg_base_get_conf (MG_BASE (query))),
241
 
                                                                        srvt);
242
 
                                if (dt)
243
 
                                        obj = mg_qf_value_new (query, dt);
244
 
                                else {
245
 
                                        g_set_error (error,
246
 
                                                     MG_QF_ALL_ERROR,
247
 
                                                     MG_QF_ALL_XML_LOAD_ERROR,
248
 
                                                     _("Can't find data type %s for query field"), srvt);
249
 
                                        return NULL;
250
 
                                }
251
 
                                g_free (srvt);
252
 
                        }
253
 
                        else {
254
 
                                g_set_error (error,
255
 
                                             MG_QF_ALL_ERROR,
256
 
                                             MG_QF_ALL_XML_LOAD_ERROR,
257
 
                                             _("Missing 'srv_type' attribute for VALUE query field"));
258
 
                                return NULL;
259
 
                        }
260
 
                                        
261
 
                }
262
 
 
263
 
                g_free (prop);
264
 
 
265
 
                if (obj) {
266
 
                        attach_to_query (MG_QFIELD (obj), query);
267
 
                        if (!mg_xml_storage_load_from_xml (MG_XML_STORAGE (obj), node, error))
268
 
                                return NULL;
269
 
                }
270
 
                else 
271
 
                        g_set_error (error,
272
 
                                     MG_QF_ALL_ERROR,
273
 
                                     MG_QF_ALL_XML_LOAD_ERROR,
274
 
                                     _("Missing Implementation in loading <MG_QF>"));
275
 
        }
276
 
        else {
277
 
                g_set_error (error,
278
 
                             MG_QFIELD_ERROR,
279
 
                             MG_QFIELD_XML_LOAD_ERROR,
280
 
                             _("Unknown value for 'type' attribute in <MG_QF>"));
281
 
                return NULL;
282
 
        }
283
 
 
284
 
        return obj;
285
 
}
286
 
 
287
 
 
288
 
/**
289
 
 * mg_qfield_new_copy
290
 
 * @orig: a #MgQfield to copy
291
 
 *
292
 
 * This is a copy constructor
293
 
 *
294
 
 * Returns: the new object
295
 
 */
296
 
GObject *
297
 
mg_qfield_new_copy (MgQfield *orig)
298
 
{
299
 
        MgQfieldClass *class;
300
 
        GObject *obj;
301
 
        MgQuery *query;
302
 
        MgQfield *newfield;
303
 
 
304
 
        g_return_val_if_fail (orig && IS_MG_QFIELD (orig), NULL);
305
 
        g_return_val_if_fail (orig->priv, NULL);
306
 
        g_object_get (G_OBJECT (orig), "query", &query, NULL);
307
 
        g_return_val_if_fail (query, NULL);
308
 
 
309
 
        class = MG_QFIELD_CLASS (G_OBJECT_GET_CLASS (orig));
310
 
        g_return_val_if_fail (class->copy, NULL);
311
 
 
312
 
        obj = (class->copy) (orig);
313
 
        newfield = MG_QFIELD (obj);
314
 
        newfield->priv->visible = orig->priv->visible;
315
 
        newfield->priv->internal = orig->priv->internal;
316
 
 
317
 
        attach_to_query (MG_QFIELD (obj), query);
318
 
 
319
 
        return obj;
320
 
}
321
 
 
322
 
static void
323
 
attach_to_query (MgQfield *qfield, MgQuery *query)
324
 
{
325
 
        g_object_set (G_OBJECT (qfield), "query", query, NULL);
326
 
}
327
 
 
328
 
 
329
 
static void
330
 
mg_qfield_dispose (GObject *object)
331
 
{
332
 
        MgQfield *qfield;
333
 
 
334
 
        g_return_if_fail (object != NULL);
335
 
        g_return_if_fail (IS_MG_QFIELD (object));
336
 
 
337
 
        qfield = MG_QFIELD (object);
338
 
        if (qfield->priv) {
339
 
                if (qfield->priv->alias) {
340
 
                        g_free (qfield->priv->alias);
341
 
                        qfield->priv->alias = NULL;
342
 
                }
343
 
        }
344
 
 
345
 
        /* parent class */
346
 
        parent_class->dispose (object);
347
 
}
348
 
 
349
 
static void
350
 
mg_qfield_finalize (GObject   * object)
351
 
{
352
 
        MgQfield *qfield;
353
 
 
354
 
        g_return_if_fail (object != NULL);
355
 
        g_return_if_fail (IS_MG_QFIELD (object));
356
 
 
357
 
        qfield = MG_QFIELD (object);
358
 
        if (qfield->priv) {
359
 
                g_free (qfield->priv);
360
 
                qfield->priv = NULL;
361
 
        }
362
 
 
363
 
        /* parent class */
364
 
        parent_class->finalize (object);
365
 
}
366
 
 
367
 
 
368
 
static void 
369
 
mg_qfield_set_property (GObject              *object,
370
 
                        guint                 param_id,
371
 
                        const GValue         *value,
372
 
                        GParamSpec           *pspec)
373
 
{
374
 
        MgQfield *qfield;
375
 
 
376
 
        qfield = MG_QFIELD (object);
377
 
        if (qfield->priv) {
378
 
                switch (param_id) {
379
 
                case PROP:
380
 
                        break;
381
 
                }
382
 
        }
383
 
}
384
 
 
385
 
static void
386
 
mg_qfield_get_property (GObject              *object,
387
 
                        guint                 param_id,
388
 
                        GValue               *value,
389
 
                        GParamSpec           *pspec)
390
 
{
391
 
        MgQfield *qfield;
392
 
 
393
 
        qfield = MG_QFIELD (object);
394
 
        if (qfield->priv) {
395
 
                switch (param_id) {
396
 
                case PROP:
397
 
                        break;
398
 
                }
399
 
        }
400
 
}
401
 
 
402
 
 
403
 
/**
404
 
 * mg_qfield_set_alias
405
 
 * @qfield: a #MgQfield object
406
 
 * @alias: the alias to set @qfield to
407
 
 *
408
 
 * Sets @qfield's alias
409
 
 */
410
 
void
411
 
mg_qfield_set_alias (MgQfield *qfield, const gchar *alias)
412
 
{
413
 
        g_return_if_fail (qfield && IS_MG_QFIELD (qfield));
414
 
        g_return_if_fail (qfield->priv);
415
 
 
416
 
        if (qfield->priv->alias) {
417
 
                g_free (qfield->priv->alias);
418
 
                qfield->priv->alias = NULL;
419
 
        }
420
 
        
421
 
        if (alias)
422
 
                qfield->priv->alias = g_strdup (alias);
423
 
}
424
 
 
425
 
/**
426
 
 * mg_qfield_get_alias
427
 
 * @qfield: a #MgQfield object
428
 
 *
429
 
 * Get @qfield's alias
430
 
 *
431
 
 * Returns: the alias
432
 
 */
433
 
const gchar *
434
 
mg_qfield_get_alias (MgQfield *qfield)
435
 
{
436
 
        g_return_val_if_fail (qfield && IS_MG_QFIELD (qfield), NULL);
437
 
        g_return_val_if_fail (qfield->priv, NULL);
438
 
 
439
 
        return qfield->priv->alias;
440
 
}
441
 
 
442
 
 
443
 
/**
444
 
 * mg_qfield_set_visible
445
 
 * @qfield: a #MgQfield object
446
 
 * @visible:
447
 
 *
448
 
 * Sets the visibility of @qfield. A visible field will appear in the query's 
449
 
 * corresponding (virtual) entity, whereas a non visible one will be hidden (and
450
 
 * possibly not taking part in the query).
451
 
 */
452
 
void
453
 
mg_qfield_set_visible (MgQfield *qfield, gboolean visible)
454
 
{
455
 
        MgQuery *query;
456
 
 
457
 
        g_return_if_fail (qfield && IS_MG_QFIELD (qfield));
458
 
        g_return_if_fail (qfield->priv);
459
 
        g_object_get (G_OBJECT (qfield), "query", &query, NULL);
460
 
        g_return_if_fail (query);
461
 
 
462
 
        if (qfield->priv->visible != visible) {
463
 
                qfield->priv->visible = visible;
464
 
                if (visible)
465
 
                        g_signal_emit_by_name (G_OBJECT (query), "field_added", MG_FIELD (qfield));
466
 
                else
467
 
                        g_signal_emit_by_name (G_OBJECT (query), "field_removed", MG_FIELD (qfield));
468
 
        }
469
 
}
470
 
 
471
 
 
472
 
/**
473
 
 * mg_qfield_is_visible
474
 
 * @qfield: a #MgQfield object
475
 
 *
476
 
 * Returns: TRUE if @field is visible
477
 
 */
478
 
gboolean
479
 
mg_qfield_is_visible (MgQfield *qfield)
480
 
{
481
 
        g_return_val_if_fail (qfield && IS_MG_QFIELD (qfield), FALSE);
482
 
        g_return_val_if_fail (qfield->priv, FALSE);
483
 
 
484
 
        return qfield->priv->visible;
485
 
}
486
 
 
487
 
/**
488
 
 * mg_qfield_set_internal
489
 
 * @qfield: a #MgQfield object
490
 
 * @internal:
491
 
 *
492
 
 * Sets weather @qfield is internal or not. Internal fields in a query are fields added
493
 
 * or changed by libmergeant itself, such fields may or may not be visible.
494
 
 */
495
 
void
496
 
mg_qfield_set_internal (MgQfield *qfield, gboolean internal)
497
 
{
498
 
        g_return_if_fail (qfield && IS_MG_QFIELD (qfield));
499
 
        g_return_if_fail (qfield->priv);
500
 
 
501
 
        qfield->priv->internal = internal;
502
 
}
503
 
 
504
 
 
505
 
/**
506
 
 * mg_qfield_is_internal
507
 
 * @qfield: a #MgQfield object
508
 
 *
509
 
 * Returns: TRUE if @field is internal
510
 
 */
511
 
gboolean
512
 
mg_qfield_is_internal (MgQfield *qfield)
513
 
{
514
 
        g_return_val_if_fail (qfield && IS_MG_QFIELD (qfield), FALSE);
515
 
        g_return_val_if_fail (qfield->priv, FALSE);
516
 
 
517
 
        return qfield->priv->internal;
518
 
}
519
 
 
520
 
/**
521
 
 * mg_qfield_get_data_type
522
 
 * @qfield: a #MgQfield object
523
 
 *
524
 
 * Get the #MgServerDataType represented by the @qfield object: for a function it returns
525
 
 * the return type, for a value, it returns its type, etc.
526
 
 *
527
 
 * Returns: the data type, or %NULL if @qfield does not have a data type.
528
 
 */
529
 
MgServerDataType *
530
 
mg_qfield_get_data_type (MgQfield *qfield)
531
 
{
532
 
        g_return_val_if_fail (qfield && IS_MG_QFIELD (qfield), NULL);
533
 
        g_return_val_if_fail (qfield->priv, NULL);
534
 
 
535
 
        /* it is assumed that qfield really implements the MgField interface */
536
 
        return mg_field_get_data_type (MG_FIELD (qfield));
537
 
}
538
 
 
539
 
/**
540
 
 * mg_qfield_get_parameters
541
 
 * @qfield: a #MgQfield object
542
 
 *
543
 
 * Get a list of all the parameters needed to @qfield to be
544
 
 * rendered as a valid statement
545
 
 *
546
 
 * Returns: a new list of parameters for @qfield
547
 
 */
548
 
GSList *
549
 
mg_qfield_get_parameters (MgQfield *qfield)
550
 
{
551
 
        MgQfieldClass *class;
552
 
 
553
 
        g_return_val_if_fail (qfield && IS_MG_QFIELD (qfield), NULL);
554
 
        g_return_val_if_fail (qfield->priv, NULL);
555
 
        class = MG_QFIELD_CLASS (G_OBJECT_GET_CLASS (qfield));
556
 
 
557
 
        if (class->get_params)
558
 
                return (class->get_params) (qfield);
559
 
        else
560
 
                return NULL;
561
 
}
562
 
 
563
 
/**
564
 
 * mg_qfield_is_equal
565
 
 * @qfield1: a #MgQfield object
566
 
 * @qfield2: a #MgQfield object
567
 
 *
568
 
 * Compares the @qfield1 and @qfield2. The name and aliases of the two fields are
569
 
 * not compared, only the contents of the fields are.
570
 
 *
571
 
 * Returns: TRUE if they are equal and FALSE otherwise
572
 
 */
573
 
gboolean
574
 
mg_qfield_is_equal (MgQfield *qfield1, MgQfield *qfield2)
575
 
{
576
 
        MgQfieldClass *class1, *class2;
577
 
        MgQuery *q1, *q2;
578
 
 
579
 
        g_return_val_if_fail (qfield1 && IS_MG_QFIELD (qfield1), FALSE);
580
 
        g_return_val_if_fail (qfield2 && IS_MG_QFIELD (qfield2), FALSE);
581
 
        g_return_val_if_fail (qfield1->priv, FALSE);
582
 
        g_return_val_if_fail (qfield2->priv, FALSE);
583
 
        g_object_get (G_OBJECT (qfield1), "query", &q1, NULL);
584
 
        g_object_get (G_OBJECT (qfield2), "query", &q2, NULL);
585
 
        g_return_val_if_fail (q1, FALSE);
586
 
        g_return_val_if_fail (q2, FALSE);
587
 
 
588
 
        if (q1 != q2)
589
 
                return FALSE;
590
 
 
591
 
        class1 = MG_QFIELD_CLASS (G_OBJECT_GET_CLASS (qfield1));
592
 
        class2 = MG_QFIELD_CLASS (G_OBJECT_GET_CLASS (qfield2));
593
 
        if (class1 != class2)
594
 
                return FALSE;
595
 
 
596
 
        g_return_val_if_fail (class1->is_equal, FALSE);
597
 
 
598
 
        return (class1->is_equal) (qfield1, qfield2);
599
 
}
600
 
 
601
 
 
602
 
/**
603
 
 * mg_qfield_is_list
604
 
 * @qfield: a #MgQfield object
605
 
 *
606
 
 * Tells if @qfield can potentially represent a list of values.
607
 
 *
608
 
 * Returns: TRUE if @field can be a list of values
609
 
 */
610
 
gboolean
611
 
mg_qfield_is_list (MgQfield *qfield)
612
 
{
613
 
        MgQfieldClass *class;
614
 
 
615
 
        g_return_val_if_fail (qfield && IS_MG_QFIELD (qfield), FALSE);
616
 
        g_return_val_if_fail (qfield->priv, FALSE);
617
 
 
618
 
        class = MG_QFIELD_CLASS (G_OBJECT_GET_CLASS (qfield));
619
 
        if (class->is_list)
620
 
                return (class->is_list) (qfield);
621
 
        else
622
 
                return FALSE;
623
 
}
624