aboutsummaryrefslogtreecommitdiffstats
path: root/filter/filter-part.c
blob: 2e74f9c926a83329a7010d0badbd69274bd71703 (plain) (blame)
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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * This program 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>
 *
 *
 * Authors:
 *      Not Zed <notzed@lostzed.mmc.com.au>
 *      Jeffrey Stedfast <fejj@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <string.h>

#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "filter-part.h"
#include "rule-context.h"
#include "filter-file.h"

#define d(x)

static void filter_part_class_init (FilterPartClass *klass);
static void filter_part_init (FilterPart *fp);
static void filter_part_finalise (GObject *obj);

static GObjectClass *parent_class = NULL;

GType
filter_part_get_type (void)
{
    static GType type = 0;

    if (!type) {
        static const GTypeInfo info = {
            sizeof (FilterPartClass),
            NULL, /* base_class_init */
            NULL, /* base_class_finalize */
            (GClassInitFunc) filter_part_class_init,
            NULL, /* class_finalize */
            NULL, /* class_data */
            sizeof (FilterPart),
            0,    /* n_preallocs */
            (GInstanceInitFunc) filter_part_init,
        };

        type = g_type_register_static (G_TYPE_OBJECT, "FilterPart", &info, 0);
    }

    return type;
}

static void
filter_part_class_init (FilterPartClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_ref (G_TYPE_OBJECT);

    object_class->finalize = filter_part_finalise;
}

static void
filter_part_init (FilterPart *fp)
{
    ;
}

static void
filter_part_finalise (GObject *obj)
{
    FilterPart *fp = (FilterPart *) obj;
    GList *l;

    l = fp->elements;
    while (l) {
        g_object_unref (l->data);
        l = l->next;
    }

    g_list_free (fp->elements);
    g_free (fp->name);
    g_free (fp->title);
    g_free (fp->code);

        G_OBJECT_CLASS (parent_class)->finalize (obj);
}

/**
 * filter_part_new:
 *
 * Create a new FilterPart object.
 *
 * Return value: A new #FilterPart object.
 **/
FilterPart *
filter_part_new (void)
{
    return (FilterPart *) g_object_new (FILTER_TYPE_PART, NULL, NULL);
}

gboolean
filter_part_validate (FilterPart *fp)
{
    gboolean correct = TRUE;
    GList *l;

    l = fp->elements;
    while (l && correct) {
        FilterElement *fe = l->data;

        correct = filter_element_validate (fe);

        l = l->next;
    }

    return correct;
}

gint
filter_part_eq (FilterPart *fp, FilterPart *fc)
{
    gint truth;
    GList *al, *bl;

    truth = ((fp->name && fc->name && strcmp(fp->name, fc->name) == 0)
         || (fp->name == NULL && fc->name == NULL))
        && ((fp->title && fc->title && strcmp(fp->title, fc->title) == 0)
            || (fp->title == NULL && fc->title == NULL))
        && ((fp->code && fc->code && strcmp(fp->code, fc->code) == 0)
            || (fp->code == NULL && fc->code == NULL));

    al = fp->elements;
    bl = fc->elements;
    while (truth && al && bl) {
        FilterElement *a = al->data, *b = bl->data;

        truth = filter_element_eq(a, b);

        al = al->next;
        bl = bl->next;
    }

    return truth && al == NULL && bl == NULL;
}

gint
filter_part_xml_create (FilterPart *ff, xmlNodePtr node, RuleContext *rc)
{
    xmlNodePtr n;
    gchar *type, *str;
    FilterElement *el;

    str = (gchar *)xmlGetProp (node, (const guchar *)"name");
    ff->name = g_strdup (str);
    if (str)
        xmlFree (str);

    n = node->children;
    while (n) {
        if (!strcmp ((gchar *)n->name, "input")) {
            type = (gchar *)xmlGetProp (n, (const guchar *)"type");
            d(printf ("creating new element type input '%s'\n", type));
            if (type != NULL
                && (el = rule_context_new_element(rc, type)) != NULL) {
                filter_element_xml_create (el, n);
                xmlFree (type);
                d(printf ("adding element part %p %s\n", ff, el, el->name));
                ff->elements = g_list_append (ff->elements, el);
            } else {
                g_warning ("Invalid xml format, missing/unknown input type");
            }
        } else if (!strcmp ((gchar *)n->name, "title") || !strcmp ((gchar *)n->name, "_title")) {
            if (!ff->title) {
                str = (gchar *)xmlNodeGetContent (n);
                ff->title = g_strdup (str);
                if (str)
                    xmlFree (str);
            }
        } else if (!strcmp ((gchar *)n->name, "code")) {
            if (!ff->code) {
                str = (gchar *)xmlNodeGetContent (n);
                ff->code = g_strdup (str);
                if (str)
                    xmlFree (str);
            }
        } else if (n->type == XML_ELEMENT_NODE) {
            g_warning ("Unknown part element in xml: %s\n", n->name);
        }
        n = n->next;
    }

    return 0;
}

xmlNodePtr
filter_part_xml_encode (FilterPart *fp)
{
    GList *l;
    FilterElement *fe;
    xmlNodePtr part, value;

    g_return_val_if_fail (fp != NULL, NULL);

    part = xmlNewNode (NULL, (const guchar *)"part");
    xmlSetProp (part, (const guchar *)"name", (guchar *)fp->name);
    l = fp->elements;
    while (l) {
        fe = l->data;
        value = filter_element_xml_encode (fe);
        xmlAddChild (part, value);
        l = g_list_next (l);
    }

    return part;
}

gint
filter_part_xml_decode (FilterPart *fp, xmlNodePtr node)
{
    FilterElement *fe;
    xmlNodePtr n;
    gchar *name;

    g_return_val_if_fail (fp != NULL, -1);
    g_return_val_if_fail (node != NULL, -1);

    n = node->children;
    while (n) {
        if (!strcmp ((gchar *)n->name, "value")) {
            name = (gchar *)xmlGetProp (n, (const guchar *)"name");
            d(printf ("finding element part %p %s = %p\n", name, name, fe));
            fe = filter_part_find_element (fp, name);
            d(printf ("finding element part %p %s = %p\n", name, name, fe));
            xmlFree (name);
            if (fe)
                filter_element_xml_decode (fe, n);
        }
        n = n->next;
    }

    return 0;
}

FilterPart *
filter_part_clone (FilterPart *fp)
{
    FilterPart *new;
    GList *l;
    FilterElement *fe, *ne;

    new = (FilterPart *) g_object_new (G_OBJECT_TYPE (fp), NULL, NULL);
    new->name = g_strdup (fp->name);
    new->title = g_strdup (fp->title);
    new->code = g_strdup (fp->code);
    l = fp->elements;
    while (l) {
        fe = l->data;
        ne = filter_element_clone (fe);
        new->elements = g_list_append (new->elements, ne);
        l = g_list_next (l);
    }

    return new;
}

/* only copies values of matching parts in the right order */
void
filter_part_copy_values (FilterPart *dst, FilterPart *src)
{
    GList *dstl, *srcl, *dstt;
    FilterElement *de, *se;

    /* NOTE: we go backwards, it just works better that way */

    /* for each source type, search the dest type for
       a matching type in the same order */
    srcl = g_list_last (src->elements);
    dstl = g_list_last (dst->elements);
    while (srcl && dstl) {
        se = srcl->data;
        dstt = dstl;
        while (dstt) {
            de = dstt->data;
            if (FILTER_PART_GET_CLASS (de) == FILTER_PART_GET_CLASS (se)) {
                filter_element_copy_value (de, se);
                dstl = dstt->prev;
                break;
            }
            dstt = dstt->prev;
        }

        srcl = srcl->prev;
    }
}

FilterElement *
filter_part_find_element (FilterPart *ff, const gchar *name)
{
    GList *l = ff->elements;
    FilterElement *fe;

    if (name == NULL)
        return NULL;

    while (l) {
        fe = l->data;
        if (fe->name && !strcmp (fe->name, name))
            return fe;
        l = g_list_next (l);
    }

    return NULL;
}

GtkWidget *
filter_part_get_widget (FilterPart *ff)
{
    GtkWidget *hbox;
    GList *l = ff->elements;
    FilterElement *fe;
    GtkWidget *w;

    hbox = gtk_hbox_new (FALSE, 3);

    while (l) {
        fe = l->data;
        w = filter_element_get_widget (fe);
        if (w)
            gtk_box_pack_start (GTK_BOX (hbox), w, IS_FILTER_FILE (fe), IS_FILTER_FILE (fe), 3);

        l = g_list_next (l);
    }

    gtk_widget_show_all (hbox);

    return hbox;
}

/**
 * filter_part_build_code:
 * @ff:
 * @out:
 *
 * Outputs the code of a part.
 **/
void
filter_part_build_code (FilterPart *ff, GString *out)
{
    GList *l = ff->elements;
    FilterElement *fe;

    if (ff->code)
        filter_part_expand_code (ff, ff->code, out);

    while (l) {
        fe = l->data;
        filter_element_build_code (fe, out, ff);
        l = l->next;
    }
}

/**
 * filter_part_build_code_list:
 * @l:
 * @out:
 *
 * Construct a list of the filter parts code into
 * a single string.
 **/
void
filter_part_build_code_list (GList *l, GString *out)
{
    FilterPart *fp;

    while (l) {
        fp = l->data;
        filter_part_build_code (fp, out);
        g_string_append (out, "\n  ");
        l = l->next;
    }
}

/**
 * filter_part_find_list:
 * @l:
 * @name:
 *
 * Find a filter part stored in a list.
 *
 * Return value:
 **/
FilterPart *
filter_part_find_list (GList *l, const gchar *name)
{
    FilterPart *part;

    d(printf ("Find part named %s\n", name));

    while (l) {
        part = l->data;
        if (!strcmp (part->name, name)) {
            d(printf ("Found!\n"));
            return part;
        }
        l = l->next;
    }

    return NULL;
}

/**
 * filter_part_next_list:
 * @l:
 * @last: The last item retrieved, or NULL to start
 * from the beginning of the list.
 *
 * Iterate through a filter part list.
 *
 * Return value: The next value in the list, or NULL if the
 * list is expired.
 **/
FilterPart *
filter_part_next_list (GList *l, FilterPart *last)
{
    GList *node = l;

    if (last != NULL) {
        node = g_list_find (node, last);
        if (node == NULL)
            node = l;
        else
            node = node->next;
    }

    if (node)
        return node->data;

    return NULL;
}

/**
 * filter_part_expand_code:
 * @ff:
 * @str:
 * @out:
 *
 * Expands the variables in string @str based on the values of the part.
 **/
void
filter_part_expand_code (FilterPart *ff, const gchar *source, GString *out)
{
    const gchar *newstart, *start, *end;
    gchar *name = alloca (32);
    gint len, namelen = 32;
    FilterElement *fe;

    start = source;
    while (start && (newstart = strstr (start, "${"))
        && (end = strstr (newstart+2, "}")) ) {
        len = end - newstart - 2;
        if (len + 1 > namelen) {
            namelen = (len + 1) * 2;
            name = g_alloca (namelen);
        }
        memcpy (name, newstart+2, len);
        name[len] = 0;
        fe = filter_part_find_element (ff, name);
        d(printf("expand code: looking up variab le '%s' = %p\n", ff, name, fe));
        if (fe) {
            g_string_append_printf (out, "%.*s", (gint)(newstart-start), start);
            filter_element_format_sexp (fe, out);
#if 0
        } else if ((val = g_hash_table_lookup (ff->globals, name))) {
            g_string_append_printf (out, "%.*s", newstart-start, start);
            e_sexp_encode_string (out, val);
#endif
        } else {
            g_string_append_printf (out, "%.*s", (gint)(end-start+1), start);
        }
        start = end + 1;
    }
    g_string_append (out, start);
}

#if 0
gint main(gint argc, gchar **argv)
{
    GtkWidget *dialog, *w;
    xmlDocPtr system;
    xmlNodePtr node;
    FilterPart *ff;
    GString *code;

    gnome_init ("test", "0.0", argc, argv);

    system = xmlParseFile ("form.xml");
    if (system == NULL) {
        printf("i/o error\n");
        return 1;
    }

    ff = filter_part_new ();
    filter_part_xml_create (ff, system->root);

    w = filter_part_get_widget (ff);

    dialog = gtk_dialog_new ();
    gtk_dialog_add_buttons (GTK_DIALOG (dialog), GTK_BUTTONS_OK, NULL);
    gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
    gtk_window_set_title (GTK_WINDOW (dialog), _("Test"));
    gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
    gtk_box_pack_start (GTK_BOX (dialog->vbox), w, TRUE, TRUE, 0);

    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);

    code = g_string_new ("");
    filter_part_build_code (ff, code);
    printf ("code is:\n%s\n", code->str);

    return 0;
}
#endif