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

« back to all changes in this revision

Viewing changes to libmergeant/mg-qf-all.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-qf-all.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-qf-all.h"
22
 
#include "mg-xml-storage.h"
23
 
#include "mg-field.h"
24
 
#include "mg-entity.h"
25
 
#include "mg-renderer.h"
26
 
#include "mg-referer.h"
27
 
#include "mg-ref-base.h"
28
 
#include "marshal.h"
29
 
#include "mg-query.h"
30
 
#include "mg-target.h"
31
 
#include <string.h>
32
 
 
33
 
/* 
34
 
 * Main static functions 
35
 
 */
36
 
static void mg_qf_all_class_init (MgQfAllClass * class);
37
 
static void mg_qf_all_init (MgQfAll *qf);
38
 
static void mg_qf_all_dispose (GObject *object);
39
 
static void mg_qf_all_finalize (GObject *object);
40
 
 
41
 
static void mg_qf_all_set_property (GObject              *object,
42
 
                                    guint                 param_id,
43
 
                                    const GValue         *value,
44
 
                                    GParamSpec           *pspec);
45
 
static void mg_qf_all_get_property (GObject              *object,
46
 
                                    guint                 param_id,
47
 
                                    GValue               *value,
48
 
                                    GParamSpec           *pspec);
49
 
 
50
 
/* XML storage interface */
51
 
static void        mg_qf_all_xml_storage_init (MgXmlStorageIface *iface);
52
 
static gchar      *mg_qf_all_get_xml_id (MgXmlStorage *iface);
53
 
static xmlNodePtr  mg_qf_all_save_to_xml (MgXmlStorage *iface, GError **error);
54
 
static gboolean    mg_qf_all_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error);
55
 
 
56
 
/* Field interface */
57
 
static void              mg_qf_all_field_init      (MgFieldIface *iface);
58
 
static MgEntity         *mg_qf_all_get_entity      (MgField *iface);
59
 
static MgServerDataType *mg_qf_all_get_data_type   (MgField *iface);
60
 
 
61
 
/* Renderer interface */
62
 
static void            mg_qf_all_renderer_init      (MgRendererIface *iface);
63
 
static GdaXqlItem     *mg_qf_all_render_as_xql   (MgRenderer *iface, MgContext *context, GError **error);
64
 
static gchar          *mg_qf_all_render_as_sql   (MgRenderer *iface, MgContext *context, guint options, GError **error);
65
 
static gchar          *mg_qf_all_render_as_str   (MgRenderer *iface, MgContext *context);
66
 
 
67
 
/* Referer interface */
68
 
static void        mg_qf_all_referer_init        (MgRefererIface *iface);
69
 
static gboolean    mg_qf_all_activate            (MgReferer *iface);
70
 
static void        mg_qf_all_deactivate          (MgReferer *iface);
71
 
static gboolean    mg_qf_all_is_active           (MgReferer *iface);
72
 
static GSList     *mg_qf_all_get_ref_objects     (MgReferer *iface);
73
 
static void        mg_qf_all_replace_refs        (MgReferer *iface, GHashTable *replacements);
74
 
 
75
 
/* virtual functions */
76
 
static GObject    *mg_qf_all_copy           (MgQfield *orig);
77
 
static gboolean    mg_qf_all_is_equal       (MgQfield *qfield1, MgQfield *qfield2);
78
 
 
79
 
 
80
 
/* When the MgQuery or MgTarget is nullified */
81
 
static void nullified_object_cb (GObject *obj, MgQfAll *field);
82
 
 
83
 
#ifdef debug
84
 
static void mg_qf_all_dump (MgQfAll *field, guint offset);
85
 
#endif
86
 
 
87
 
/* get a pointer to the parents to be able to call their destructor */
88
 
static GObjectClass  *parent_class = NULL;
89
 
 
90
 
/* signals */
91
 
enum
92
 
{
93
 
        TEMPL_SIGNAL,
94
 
        LAST_SIGNAL
95
 
};
96
 
 
97
 
static gint mg_qf_all_signals[LAST_SIGNAL] = { 0 };
98
 
 
99
 
/* properties */
100
 
enum
101
 
{
102
 
        PROP_0,
103
 
        PROP_QUERY
104
 
};
105
 
 
106
 
 
107
 
/* private structure */
108
 
struct _MgQfAllPrivate
109
 
{
110
 
        MgQuery    *query;
111
 
        MgRefBase  *target_ref;
112
 
};
113
 
 
114
 
 
115
 
/* module error */
116
 
GQuark mg_qf_all_error_quark (void)
117
 
{
118
 
        static GQuark quark;
119
 
        if (!quark)
120
 
                quark = g_quark_from_static_string ("mg_qf_all_error");
121
 
        return quark;
122
 
}
123
 
 
124
 
 
125
 
guint
126
 
mg_qf_all_get_type (void)
127
 
{
128
 
        static GType type = 0;
129
 
 
130
 
        if (!type) {
131
 
                static const GTypeInfo info = {
132
 
                        sizeof (MgQfAllClass),
133
 
                        (GBaseInitFunc) NULL,
134
 
                        (GBaseFinalizeFunc) NULL,
135
 
                        (GClassInitFunc) mg_qf_all_class_init,
136
 
                        NULL,
137
 
                        NULL,
138
 
                        sizeof (MgQfAll),
139
 
                        0,
140
 
                        (GInstanceInitFunc) mg_qf_all_init
141
 
                };
142
 
 
143
 
                static const GInterfaceInfo xml_storage_info = {
144
 
                        (GInterfaceInitFunc) mg_qf_all_xml_storage_init,
145
 
                        NULL,
146
 
                        NULL
147
 
                };
148
 
 
149
 
                static const GInterfaceInfo field_info = {
150
 
                        (GInterfaceInitFunc) mg_qf_all_field_init,
151
 
                        NULL,
152
 
                        NULL
153
 
                };
154
 
 
155
 
                static const GInterfaceInfo renderer_info = {
156
 
                        (GInterfaceInitFunc) mg_qf_all_renderer_init,
157
 
                        NULL,
158
 
                        NULL
159
 
                };
160
 
 
161
 
                static const GInterfaceInfo referer_info = {
162
 
                        (GInterfaceInitFunc) mg_qf_all_referer_init,
163
 
                        NULL,
164
 
                        NULL
165
 
                };
166
 
                
167
 
                
168
 
                type = g_type_register_static (MG_QFIELD_TYPE, "MgQfAll", &info, 0);
169
 
                g_type_add_interface_static (type, MG_XML_STORAGE_TYPE, &xml_storage_info);
170
 
                g_type_add_interface_static (type, MG_FIELD_TYPE, &field_info);
171
 
                g_type_add_interface_static (type, MG_RENDERER_TYPE, &renderer_info);
172
 
                g_type_add_interface_static (type, MG_REFERER_TYPE, &referer_info);
173
 
        }
174
 
        return type;
175
 
}
176
 
 
177
 
static void 
178
 
mg_qf_all_xml_storage_init (MgXmlStorageIface *iface)
179
 
{
180
 
        iface->get_xml_id = mg_qf_all_get_xml_id;
181
 
        iface->save_to_xml = mg_qf_all_save_to_xml;
182
 
        iface->load_from_xml = mg_qf_all_load_from_xml;
183
 
}
184
 
 
185
 
static void
186
 
mg_qf_all_field_init (MgFieldIface *iface)
187
 
{
188
 
        iface->get_entity = mg_qf_all_get_entity;
189
 
        iface->get_data_type = mg_qf_all_get_data_type;
190
 
}
191
 
 
192
 
static void
193
 
mg_qf_all_renderer_init (MgRendererIface *iface)
194
 
{
195
 
        iface->render_as_xql = mg_qf_all_render_as_xql;
196
 
        iface->render_as_sql = mg_qf_all_render_as_sql;
197
 
        iface->render_as_str = mg_qf_all_render_as_str;
198
 
        iface->is_valid = NULL;
199
 
}
200
 
 
201
 
static void
202
 
mg_qf_all_referer_init (MgRefererIface *iface)
203
 
{
204
 
        iface->activate = mg_qf_all_activate;
205
 
        iface->deactivate = mg_qf_all_deactivate;
206
 
        iface->is_active = mg_qf_all_is_active;
207
 
        iface->get_ref_objects = mg_qf_all_get_ref_objects;
208
 
        iface->replace_refs = mg_qf_all_replace_refs;
209
 
}
210
 
 
211
 
static void
212
 
mg_qf_all_class_init (MgQfAllClass * class)
213
 
{
214
 
        GObjectClass   *object_class = G_OBJECT_CLASS (class);
215
 
 
216
 
        parent_class = g_type_class_peek_parent (class);
217
 
 
218
 
        mg_qf_all_signals[TEMPL_SIGNAL] =
219
 
                g_signal_new ("templ_signal",
220
 
                              G_TYPE_FROM_CLASS (object_class),
221
 
                              G_SIGNAL_RUN_FIRST,
222
 
                              G_STRUCT_OFFSET (MgQfAllClass, templ_signal),
223
 
                              NULL, NULL,
224
 
                              marshal_VOID__VOID, G_TYPE_NONE,
225
 
                              0);
226
 
        class->templ_signal = NULL;
227
 
 
228
 
        object_class->dispose = mg_qf_all_dispose;
229
 
        object_class->finalize = mg_qf_all_finalize;
230
 
 
231
 
        /* Properties */
232
 
        object_class->set_property = mg_qf_all_set_property;
233
 
        object_class->get_property = mg_qf_all_get_property;
234
 
        g_object_class_install_property (object_class, PROP_QUERY,
235
 
                                         g_param_spec_pointer ("query", NULL, NULL, 
236
 
                                                               (G_PARAM_READABLE | G_PARAM_WRITABLE)));
237
 
 
238
 
        /* virtual functions */
239
 
#ifdef debug
240
 
        MG_BASE_CLASS (class)->dump = (void (*)(MgBase *, guint)) mg_qf_all_dump;
241
 
#endif
242
 
        MG_QFIELD_CLASS (class)->copy = mg_qf_all_copy;
243
 
        MG_QFIELD_CLASS (class)->is_equal = mg_qf_all_is_equal;
244
 
        MG_QFIELD_CLASS (class)->is_list = NULL;
245
 
        MG_QFIELD_CLASS (class)->get_params = NULL;
246
 
}
247
 
 
248
 
static void
249
 
mg_qf_all_init (MgQfAll *mg_qf_all)
250
 
{
251
 
        mg_qf_all->priv = g_new0 (MgQfAllPrivate, 1);
252
 
        mg_qf_all->priv->query = NULL;
253
 
        mg_qf_all->priv->target_ref = NULL;
254
 
}
255
 
 
256
 
 
257
 
/**
258
 
 * mg_qf_all_new_with_target
259
 
 * @query: a #MgQuery in which the new object will be
260
 
 * @target: a #MgTarget object
261
 
 *
262
 
 * Creates a new MgQfAll object which represents all the fields of the entity represented
263
 
 * by @target. For example if @target represents my_table, then the created object would
264
 
 * represent 'my_table.*' in SQL notation
265
 
 *
266
 
 * Returns: the new object
267
 
 */
268
 
GObject*
269
 
mg_qf_all_new_with_target (MgQuery *query, MgTarget *target)
270
 
{
271
 
        GObject   *obj;
272
 
        MgQfAll *mg_qf_all;
273
 
        MgConf *conf;
274
 
        guint id;
275
 
 
276
 
        g_return_val_if_fail (query && IS_MG_QUERY (query), NULL);
277
 
        g_return_val_if_fail (target && IS_MG_TARGET (target), NULL);
278
 
        g_return_val_if_fail (mg_target_get_query (target) == query, NULL);
279
 
        conf = mg_base_get_conf (MG_BASE (query));
280
 
 
281
 
        obj = g_object_new (MG_QF_ALL_TYPE, "conf", conf, NULL);
282
 
        mg_qf_all = MG_QF_ALL (obj);
283
 
        g_object_get (G_OBJECT (query), "field_serial", &id, NULL);
284
 
        mg_base_set_id (MG_BASE (mg_qf_all), id);
285
 
 
286
 
        mg_qf_all->priv->query = query;
287
 
        g_signal_connect (G_OBJECT (query), "nullified",
288
 
                          G_CALLBACK (nullified_object_cb), mg_qf_all);
289
 
 
290
 
        mg_qf_all->priv->target_ref = MG_REF_BASE (mg_ref_base_new (conf));
291
 
        mg_ref_base_set_ref_object (mg_qf_all->priv->target_ref, MG_BASE (target));
292
 
        
293
 
        return obj;
294
 
}
295
 
 
296
 
/**
297
 
 * mg_qf_all_new_with_xml_id
298
 
 * @query: a #MgQuery in which the new object will be
299
 
 * @target_xml_id: the XML Id of a #MgTarget object
300
 
 *
301
 
 * Creates a new MgQfAll object which represents all the fields of the entity represented
302
 
 * by the target identified by @target_xml_id. 
303
 
 *
304
 
 * Returns: the new object
305
 
 */
306
 
GObject*
307
 
mg_qf_all_new_with_xml_id (MgQuery *query, const gchar *target_xml_id)
308
 
{
309
 
        GObject   *obj;
310
 
        MgQfAll *mg_qf_all;
311
 
        MgConf *conf;
312
 
        gchar *str, *ptr, *tok, *qid;
313
 
        guint id;
314
 
 
315
 
        g_return_val_if_fail (query && IS_MG_QUERY (query), NULL);
316
 
        g_return_val_if_fail (target_xml_id && *target_xml_id, NULL);
317
 
        qid = mg_xml_storage_get_xml_id (MG_XML_STORAGE (query));
318
 
        str = g_strdup (target_xml_id);
319
 
        ptr = strtok_r (str, ":", &tok);
320
 
        g_return_val_if_fail (!strcmp (ptr, qid), NULL);
321
 
        g_free (qid);
322
 
        g_free (str);
323
 
 
324
 
        conf = mg_base_get_conf (MG_BASE (query));
325
 
        obj = g_object_new (MG_QF_ALL_TYPE, "conf", conf, NULL);
326
 
        mg_qf_all = MG_QF_ALL (obj);
327
 
        g_object_get (G_OBJECT (query), "field_serial", &id, NULL);
328
 
        mg_base_set_id (MG_BASE (mg_qf_all), id);
329
 
 
330
 
        mg_qf_all->priv->query = query;
331
 
        g_signal_connect (G_OBJECT (query), "nullified",
332
 
                          G_CALLBACK (nullified_object_cb), mg_qf_all);
333
 
 
334
 
        mg_qf_all->priv->target_ref = MG_REF_BASE (mg_ref_base_new (conf));
335
 
        mg_ref_base_set_ref_name (mg_qf_all->priv->target_ref, MG_TARGET_TYPE, REFERENCE_BY_XML_ID, target_xml_id);
336
 
        
337
 
        return obj;
338
 
}
339
 
 
340
 
static void 
341
 
nullified_object_cb (GObject *obj, MgQfAll *field)
342
 
{
343
 
        mg_base_nullify (MG_BASE (field));
344
 
}
345
 
 
346
 
static void
347
 
mg_qf_all_dispose (GObject *object)
348
 
{
349
 
        MgQfAll *mg_qf_all;
350
 
 
351
 
        g_return_if_fail (object != NULL);
352
 
        g_return_if_fail (IS_MG_QF_ALL (object));
353
 
 
354
 
        mg_qf_all = MG_QF_ALL (object);
355
 
        if (mg_qf_all->priv) {
356
 
                mg_base_nullify_check (MG_BASE (object));
357
 
 
358
 
                if (mg_qf_all->priv->query) {
359
 
                        g_signal_handlers_disconnect_by_func (G_OBJECT (mg_qf_all->priv->query),
360
 
                                                              G_CALLBACK (nullified_object_cb), mg_qf_all);
361
 
                        mg_qf_all->priv->query = NULL;
362
 
                }
363
 
                if (mg_qf_all->priv->target_ref) {
364
 
                        g_object_unref (G_OBJECT (mg_qf_all->priv->target_ref));
365
 
                        mg_qf_all->priv->target_ref = NULL;
366
 
                }
367
 
        }
368
 
 
369
 
        /* parent class */
370
 
        parent_class->dispose (object);
371
 
}
372
 
 
373
 
static void
374
 
mg_qf_all_finalize (GObject   * object)
375
 
{
376
 
        MgQfAll *mg_qf_all;
377
 
 
378
 
        g_return_if_fail (object != NULL);
379
 
        g_return_if_fail (IS_MG_QF_ALL (object));
380
 
 
381
 
        mg_qf_all = MG_QF_ALL (object);
382
 
        if (mg_qf_all->priv) {
383
 
                g_free (mg_qf_all->priv);
384
 
                mg_qf_all->priv = NULL;
385
 
        }
386
 
 
387
 
        /* parent class */
388
 
        parent_class->finalize (object);
389
 
}
390
 
 
391
 
 
392
 
static void 
393
 
mg_qf_all_set_property (GObject              *object,
394
 
                        guint                 param_id,
395
 
                        const GValue         *value,
396
 
                        GParamSpec           *pspec)
397
 
{
398
 
        MgQfAll *mg_qf_all;
399
 
        gpointer ptr;
400
 
 
401
 
        mg_qf_all = MG_QF_ALL (object);
402
 
        if (mg_qf_all->priv) {
403
 
                switch (param_id) {
404
 
                case PROP_QUERY:
405
 
                        ptr = g_value_get_pointer (value);
406
 
                        g_return_if_fail (ptr && IS_MG_QUERY (ptr));
407
 
 
408
 
                        if (mg_qf_all->priv->query) {
409
 
                                if (mg_qf_all->priv->query == MG_QUERY (ptr))
410
 
                                        return;
411
 
 
412
 
                                g_signal_handlers_disconnect_by_func (G_OBJECT (mg_qf_all->priv->query),
413
 
                                                                      G_CALLBACK (nullified_object_cb), mg_qf_all);
414
 
                        }
415
 
 
416
 
                        mg_qf_all->priv->query = MG_QUERY (ptr);
417
 
                        g_signal_connect (G_OBJECT (ptr), "nullified",
418
 
                                          G_CALLBACK (nullified_object_cb), mg_qf_all);
419
 
                        break;
420
 
                }
421
 
        }
422
 
}
423
 
 
424
 
static void
425
 
mg_qf_all_get_property (GObject              *object,
426
 
                        guint                 param_id,
427
 
                        GValue               *value,
428
 
                        GParamSpec           *pspec)
429
 
{
430
 
        MgQfAll *mg_qf_all;
431
 
        mg_qf_all = MG_QF_ALL (object);
432
 
        
433
 
        if (mg_qf_all->priv) {
434
 
                switch (param_id) {
435
 
                case PROP_QUERY:
436
 
                        g_value_set_pointer (value, mg_qf_all->priv->query);
437
 
                        break;
438
 
                }       
439
 
        }
440
 
}
441
 
 
442
 
static GObject *
443
 
mg_qf_all_copy (MgQfield *orig)
444
 
{
445
 
        MgQfAll *qf;
446
 
        GObject *obj;
447
 
        g_assert (IS_MG_QF_ALL (orig));
448
 
        qf = MG_QF_ALL (orig);
449
 
 
450
 
        obj = mg_qf_all_new_with_xml_id (qf->priv->query, 
451
 
                                         mg_ref_base_get_ref_name (qf->priv->target_ref, NULL, NULL));
452
 
        if (mg_base_get_name (MG_BASE (orig)))
453
 
                mg_base_set_name (MG_BASE (obj), mg_base_get_name (MG_BASE (orig)));
454
 
 
455
 
        if (mg_base_get_description (MG_BASE (orig)))
456
 
                mg_base_set_description (MG_BASE (obj), mg_base_get_description (MG_BASE (orig)));
457
 
 
458
 
        return obj;
459
 
}
460
 
 
461
 
static gboolean
462
 
mg_qf_all_is_equal (MgQfield *qfield1, MgQfield *qfield2)
463
 
{
464
 
        const gchar *ref1, *ref2;
465
 
        g_assert (IS_MG_QF_ALL (qfield1));
466
 
        g_assert (IS_MG_QF_ALL (qfield2));
467
 
        
468
 
        /* it is here assumed that qfield1 and qfield2 are of the same type and refer to the same
469
 
           query */
470
 
        ref1 = mg_ref_base_get_ref_name (MG_QF_ALL (qfield1)->priv->target_ref, NULL, NULL);
471
 
        ref2 = mg_ref_base_get_ref_name (MG_QF_ALL (qfield2)->priv->target_ref, NULL, NULL);
472
 
 
473
 
        return !strcmp (ref1, ref2) ? TRUE : FALSE;
474
 
}
475
 
 
476
 
/**
477
 
 * mg_qf_all_get_target
478
 
 * @field: a #MgQfAll object
479
 
 *
480
 
 * Get the #MgTarget object @field 'belongs' to
481
 
 *
482
 
 * Returns: the #MgTarget object
483
 
 */
484
 
MgTarget *
485
 
mg_qf_all_get_target (MgQfAll *field)
486
 
{
487
 
        MgBase *base;
488
 
        g_return_val_if_fail (field && IS_MG_QF_ALL (field), NULL);
489
 
        g_return_val_if_fail (field->priv, NULL);
490
 
 
491
 
        base = mg_ref_base_get_ref_object (field->priv->target_ref);
492
 
        if (base)
493
 
                return MG_TARGET (base);
494
 
        else
495
 
                return NULL;
496
 
}
497
 
 
498
 
#ifdef debug
499
 
static void
500
 
mg_qf_all_dump (MgQfAll *field, guint offset)
501
 
{
502
 
        gchar *str;
503
 
        gint i;
504
 
 
505
 
        g_return_if_fail (field && IS_MG_QF_ALL (field));
506
 
        
507
 
        /* string for the offset */
508
 
        str = g_new0 (gchar, offset+1);
509
 
        for (i=0; i<offset; i++)
510
 
                str[i] = ' ';
511
 
        str[offset] = 0;
512
 
 
513
 
        /* dump */
514
 
        if (field->priv) {
515
 
                g_print ("%s" D_COL_H1 "MgQfAll" D_COL_NOR " \"%s\" (%p, id=%d) ",
516
 
                         str, mg_base_get_name (MG_BASE (field)), field, mg_base_get_id (MG_BASE (field)));
517
 
                if (mg_qf_all_is_active (MG_REFERER (field)))
518
 
                        g_print ("Active, ");
519
 
                else
520
 
                        g_print (D_COL_ERR "Inactive" D_COL_NOR ", ");
521
 
                g_print ("references %p (%s)\n", 
522
 
                         mg_ref_base_get_ref_object (field->priv->target_ref),
523
 
                         mg_ref_base_get_ref_name (field->priv->target_ref, NULL, NULL));
524
 
        }
525
 
        else
526
 
                g_print ("%s" D_COL_ERR "Using finalized object %p" D_COL_NOR, str, field);
527
 
}
528
 
#endif
529
 
 
530
 
 
531
 
/* 
532
 
 * MgField interface implementation
533
 
 */
534
 
static MgEntity *
535
 
mg_qf_all_get_entity (MgField *iface)
536
 
{
537
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
538
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
539
 
 
540
 
        return MG_ENTITY (MG_QF_ALL (iface)->priv->query);
541
 
}
542
 
 
543
 
static MgServerDataType *
544
 
mg_qf_all_get_data_type (MgField *iface)
545
 
{
546
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
547
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
548
 
 
549
 
        return NULL;
550
 
}
551
 
 
552
 
/* 
553
 
 * MgXmlStorage interface implementation
554
 
 */
555
 
static gchar *
556
 
mg_qf_all_get_xml_id (MgXmlStorage *iface)
557
 
{
558
 
        gchar *q_xml_id, *xml_id;
559
 
 
560
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
561
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
562
 
 
563
 
        q_xml_id = mg_xml_storage_get_xml_id (MG_XML_STORAGE (MG_QF_ALL (iface)->priv->query));
564
 
        xml_id = g_strdup_printf ("%s:QF%d", q_xml_id, mg_base_get_id (MG_BASE (iface)));
565
 
        g_free (q_xml_id);
566
 
        
567
 
        return xml_id;
568
 
}
569
 
 
570
 
static xmlNodePtr
571
 
mg_qf_all_save_to_xml (MgXmlStorage *iface, GError **error)
572
 
{
573
 
        xmlNodePtr node = NULL;
574
 
        MgQfAll *field;
575
 
        gchar *str;
576
 
 
577
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
578
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
579
 
 
580
 
        field = MG_QF_ALL (iface);
581
 
 
582
 
        node = xmlNewNode (NULL, "MG_QF");
583
 
        
584
 
        str = mg_qf_all_get_xml_id (iface);
585
 
        xmlSetProp (node, "id", str);
586
 
        g_free (str);
587
 
 
588
 
        xmlSetProp (node, "type", "ALL");
589
 
        xmlSetProp (node, "name", mg_base_get_name (MG_BASE (field)));
590
 
        xmlSetProp (node, "target", mg_ref_base_get_ref_name (field->priv->target_ref, NULL, NULL));
591
 
        if (! mg_qfield_is_visible (MG_QFIELD (field)))
592
 
                xmlSetProp (node, "is_visible",  "f");
593
 
        if (mg_qfield_is_internal (MG_QFIELD (field)))
594
 
                xmlSetProp (node, "is_internal", "t");
595
 
 
596
 
        return node;
597
 
}
598
 
 
599
 
static gboolean
600
 
mg_qf_all_load_from_xml (MgXmlStorage *iface, xmlNodePtr node, GError **error)
601
 
{
602
 
        MgQfAll *field;
603
 
        gchar *prop;
604
 
        gboolean target = FALSE;
605
 
 
606
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), FALSE);
607
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, FALSE);
608
 
        g_return_val_if_fail (node, FALSE);
609
 
 
610
 
        field = MG_QF_ALL (iface);
611
 
        if (strcmp (node->name, "MG_QF")) {
612
 
                g_set_error (error,
613
 
                             MG_QF_ALL_ERROR,
614
 
                             MG_QF_ALL_XML_LOAD_ERROR,
615
 
                             _("XML Tag is not <MG_QF>"));
616
 
                return FALSE;
617
 
        }
618
 
 
619
 
        prop = xmlGetProp (node, "type");
620
 
        if (prop) {
621
 
                if (strcmp (prop, "ALL")) {
622
 
                        g_set_error (error,
623
 
                                     MG_QF_ALL_ERROR,
624
 
                                     MG_QF_ALL_XML_LOAD_ERROR,
625
 
                                     _("Wrong type of field in <MG_QF>"));
626
 
                        return FALSE;
627
 
                }
628
 
                g_free (prop);
629
 
        }
630
 
 
631
 
        prop = xmlGetProp (node, "id");
632
 
        if (prop) {
633
 
                gchar *ptr, *tok;
634
 
                ptr = strtok_r (prop, ":", &tok);
635
 
                ptr = strtok_r (NULL, ":", &tok);
636
 
                if (strlen (ptr) < 3) {
637
 
                        g_set_error (error,
638
 
                                     MG_QF_ALL_ERROR,
639
 
                                     MG_QF_ALL_XML_LOAD_ERROR,
640
 
                                     _("Wrong 'id' attribute in <MG_QF>"));
641
 
                        return FALSE;
642
 
                }
643
 
                mg_base_set_id (MG_BASE (field), atoi (ptr+2));
644
 
                g_free (prop);
645
 
        }
646
 
 
647
 
        prop = xmlGetProp (node, "name");
648
 
        if (prop) {
649
 
                mg_base_set_name (MG_BASE (field), prop);
650
 
                g_free (prop);
651
 
        }
652
 
 
653
 
        prop = xmlGetProp (node, "target");
654
 
        if (prop) {
655
 
                target = TRUE;
656
 
                mg_ref_base_set_ref_name (field->priv->target_ref, MG_TARGET_TYPE, REFERENCE_BY_XML_ID, prop);
657
 
                g_free (prop);
658
 
        }
659
 
 
660
 
        prop = xmlGetProp (node, "is_visible");
661
 
        if (prop) {
662
 
                mg_qfield_set_visible (MG_QFIELD (field), (*prop == 't') ? TRUE : FALSE);
663
 
                g_free (prop);
664
 
        }
665
 
 
666
 
        prop = xmlGetProp (node, "is_internal");
667
 
        if (prop) {
668
 
                mg_qfield_set_internal (MG_QFIELD (field), (*prop == 't') ? TRUE : FALSE);
669
 
                g_free (prop);
670
 
        }
671
 
 
672
 
        if (target)
673
 
                return TRUE;
674
 
        else {
675
 
                g_set_error (error,
676
 
                             MG_QF_ALL_ERROR,
677
 
                             MG_QF_ALL_XML_LOAD_ERROR,
678
 
                             _("Missing required attributes for <MG_QF>"));
679
 
                return FALSE;
680
 
        }
681
 
}
682
 
 
683
 
 
684
 
/*
685
 
 * MgRenderer interface implementation
686
 
 */
687
 
static GdaXqlItem *
688
 
mg_qf_all_render_as_xql (MgRenderer *iface, MgContext *context, GError **error)
689
 
{
690
 
        GdaXqlItem *node = NULL;
691
 
 
692
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
693
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
694
 
        
695
 
        TO_IMPLEMENT;
696
 
        return node;
697
 
}
698
 
 
699
 
static gchar *
700
 
mg_qf_all_render_as_sql (MgRenderer *iface, MgContext *context, guint options, GError **error)
701
 
{
702
 
        gchar *str = NULL;
703
 
        MgBase *base;
704
 
        MgQfAll *field;
705
 
 
706
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
707
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
708
 
        field = MG_QF_ALL (iface);
709
 
 
710
 
        base = mg_ref_base_get_ref_object (field->priv->target_ref);
711
 
        if (base) 
712
 
                str = g_strdup_printf ("%s.*", mg_target_get_alias (MG_TARGET (base)));
713
 
        else
714
 
                g_set_error (error,
715
 
                             MG_QF_ALL_ERROR,
716
 
                             MG_QF_ALL_RENDER_ERROR,
717
 
                             _("Can't find target '%s'"), mg_ref_base_get_ref_name (field->priv->target_ref,
718
 
                                                                                    NULL, NULL));
719
 
        
720
 
        return str;
721
 
}
722
 
 
723
 
static gchar *
724
 
mg_qf_all_render_as_str (MgRenderer *iface, MgContext *context)
725
 
{
726
 
        gchar *str = NULL;
727
 
        MgBase *base;
728
 
        MgQfAll *field;
729
 
 
730
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
731
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
732
 
        field = MG_QF_ALL (iface);
733
 
 
734
 
        base = mg_ref_base_get_ref_object (field->priv->target_ref);
735
 
        if (base) {
736
 
                MgEntity *ent = mg_target_get_represented_entity (MG_TARGET (base));
737
 
                str = g_strdup_printf ("%s(%s).*", mg_base_get_name (MG_BASE (ent)), 
738
 
                                       mg_target_get_alias (MG_TARGET (base)));
739
 
        }
740
 
        else
741
 
                str = g_strdup (_("Non activated field"));
742
 
        return str;
743
 
}
744
 
 
745
 
 
746
 
/*
747
 
 * MgReferer interface implementation
748
 
 */
749
 
static gboolean
750
 
mg_qf_all_activate (MgReferer *iface)
751
 
{
752
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), FALSE);
753
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, FALSE);
754
 
 
755
 
        return mg_ref_base_activate (MG_QF_ALL (iface)->priv->target_ref);
756
 
}
757
 
 
758
 
static void
759
 
mg_qf_all_deactivate (MgReferer *iface)
760
 
{
761
 
        g_return_if_fail (iface && IS_MG_QF_ALL (iface));
762
 
        g_return_if_fail (MG_QF_ALL (iface)->priv);
763
 
 
764
 
        mg_ref_base_deactivate (MG_QF_ALL (iface)->priv->target_ref);
765
 
}
766
 
 
767
 
static gboolean
768
 
mg_qf_all_is_active (MgReferer *iface)
769
 
{
770
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), FALSE);
771
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, FALSE);
772
 
 
773
 
        return mg_ref_base_is_active (MG_QF_ALL (iface)->priv->target_ref);
774
 
}
775
 
 
776
 
static GSList *
777
 
mg_qf_all_get_ref_objects (MgReferer *iface)
778
 
{
779
 
        GSList *list = NULL;
780
 
        MgBase *base;
781
 
 
782
 
        g_return_val_if_fail (iface && IS_MG_QF_ALL (iface), NULL);
783
 
        g_return_val_if_fail (MG_QF_ALL (iface)->priv, NULL);
784
 
 
785
 
        base = mg_ref_base_get_ref_object (MG_QF_ALL (iface)->priv->target_ref);
786
 
        if (base)
787
 
                list = g_slist_append (list, base);
788
 
 
789
 
        return list;
790
 
}
791
 
 
792
 
static void
793
 
mg_qf_all_replace_refs (MgReferer *iface, GHashTable *replacements)
794
 
{
795
 
        MgQfAll *field;
796
 
 
797
 
        g_return_if_fail (iface && IS_MG_QF_ALL (iface));
798
 
        g_return_if_fail (MG_QF_ALL (iface)->priv);
799
 
 
800
 
        field = MG_QF_ALL (iface);
801
 
        if (field->priv->query) {
802
 
                MgQuery *query = g_hash_table_lookup (replacements, field->priv->query);
803
 
                if (query) {
804
 
                        g_signal_handlers_disconnect_by_func (G_OBJECT (field->priv->query),
805
 
                                                              G_CALLBACK (nullified_object_cb), field);
806
 
                        field->priv->query = query;
807
 
                        g_signal_connect (G_OBJECT (query), "nullified",
808
 
                                          G_CALLBACK (nullified_object_cb), field);
809
 
                }
810
 
        }
811
 
 
812
 
        mg_ref_base_replace_ref_object (field->priv->target_ref, replacements);
813
 
}