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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
/*
* 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)
*
*/
#include <config.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <libedataserver/e-xml-utils.h>
#include "e-util/e-error.h"
#include "e-util/e-xml-utils.h"
#include "filter-code.h"
#include "filter-colour.h"
#include "filter-datespec.h"
#include "filter-file.h"
#include "filter-input.h"
#include "filter-int.h"
#include "filter-option.h"
#include "filter-rule.h"
#include "rule-context.h"
#define d(x)
static gint load(RuleContext *rc, const gchar *system, const gchar *user);
static gint save(RuleContext *rc, const gchar *user);
static gint revert(RuleContext *rc, const gchar *user);
static GList *rename_uri(RuleContext *rc, const gchar *olduri, const gchar *newuri, GCompareFunc cmp);
static GList *delete_uri(RuleContext *rc, const gchar *uri, GCompareFunc cmp);
static FilterElement *new_element(RuleContext *rc, const gchar *name);
static void rule_context_class_init(RuleContextClass *klass);
static void rule_context_init(RuleContext *rc);
static void rule_context_finalise(GObject *obj);
#define _PRIVATE(x)(((RuleContext *)(x))->priv)
struct _RuleContextPrivate {
gint frozen;
};
static GObjectClass *parent_class = NULL;
enum {
RULE_ADDED,
RULE_REMOVED,
CHANGED,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
GType
rule_context_get_type(void)
{
static GType type = 0;
if (!type) {
static const GTypeInfo info = {
sizeof(RuleContextClass),
NULL, /* base_class_init */
NULL, /* base_class_finalize */
(GClassInitFunc) rule_context_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof(RuleContext),
0, /* n_preallocs */
(GInstanceInitFunc) rule_context_init,
};
type = g_type_register_static(G_TYPE_OBJECT, "RuleContext", &info, 0);
}
return type;
}
static void
rule_context_class_init(RuleContextClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
parent_class = g_type_class_ref(G_TYPE_OBJECT);
object_class->finalize = rule_context_finalise;
/* override methods */
klass->load = load;
klass->save = save;
klass->revert = revert;
klass->rename_uri = rename_uri;
klass->delete_uri = delete_uri;
klass->new_element = new_element;
/* signals */
signals[RULE_ADDED] =
g_signal_new("rule_added",
RULE_TYPE_CONTEXT,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(RuleContextClass, rule_added),
NULL,
NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
signals[RULE_REMOVED] =
g_signal_new("rule_removed",
RULE_TYPE_CONTEXT,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(RuleContextClass, rule_removed),
NULL,
NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
signals[CHANGED] =
g_signal_new("changed",
RULE_TYPE_CONTEXT,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET(RuleContextClass, changed),
NULL,
NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
}
static void
rule_context_init(RuleContext *rc)
{
rc->priv = g_malloc0(sizeof(*rc->priv));
rc->part_set_map = g_hash_table_new(g_str_hash, g_str_equal);
rc->rule_set_map = g_hash_table_new(g_str_hash, g_str_equal);
rc->flags = RULE_CONTEXT_GROUPING;
}
static void
free_part_set(struct _part_set_map *map, gpointer data)
{
g_free(map->name);
g_free(map);
}
static void
free_rule_set(struct _rule_set_map *map, gpointer data)
{
g_free(map->name);
g_free(map);
}
static void
rule_context_finalise(GObject *obj)
{
RuleContext *rc =(RuleContext *) obj;
g_list_foreach(rc->rule_set_list, (GFunc)free_rule_set, NULL);
g_list_free(rc->rule_set_list);
g_hash_table_destroy(rc->rule_set_map);
g_list_foreach(rc->part_set_list, (GFunc)free_part_set, NULL);
g_list_free(rc->part_set_list);
g_hash_table_destroy(rc->part_set_map);
g_free(rc->error);
g_list_foreach(rc->parts, (GFunc)g_object_unref, NULL);
g_list_free(rc->parts);
g_list_foreach(rc->rules, (GFunc)g_object_unref, NULL);
g_list_free(rc->rules);
g_free(rc->priv);
G_OBJECT_CLASS(parent_class)->finalize(obj);
}
/**
* rule_context_new:
*
* Create a new RuleContext object.
*
* Return value: A new #RuleContext object.
**/
RuleContext *
rule_context_new(void)
{
return(RuleContext *) g_object_new(RULE_TYPE_CONTEXT, NULL, NULL);
}
void
rule_context_add_part_set(RuleContext *rc, const gchar *setname, GType part_type, RCPartFunc append, RCNextPartFunc next)
{
struct _part_set_map *map;
g_return_if_fail (g_hash_table_lookup(rc->part_set_map, setname) == NULL);
map = g_malloc0(sizeof(*map));
map->type = part_type;
map->append = append;
map->next = next;
map->name = g_strdup(setname);
g_hash_table_insert(rc->part_set_map, map->name, map);
rc->part_set_list = g_list_append(rc->part_set_list, map);
d(printf("adding part set '%s'\n", setname));
}
void
rule_context_add_rule_set(RuleContext *rc, const gchar *setname, GType rule_type, RCRuleFunc append, RCNextRuleFunc next)
{
struct _rule_set_map *map;
g_return_if_fail (g_hash_table_lookup(rc->rule_set_map, setname) == NULL);
map = g_malloc0(sizeof(*map));
map->type = rule_type;
map->append = append;
map->next = next;
map->name = g_strdup(setname);
g_hash_table_insert(rc->rule_set_map, map->name, map);
rc->rule_set_list = g_list_append(rc->rule_set_list, map);
d(printf("adding rule set '%s'\n", setname));
}
/**
* rule_context_set_error:
* @f:
* @error:
*
* Set the text error for the context, or NULL to clear it.
**/
static void
rule_context_set_error(RuleContext *rc, gchar *error)
{
g_return_if_fail (rc);
g_free(rc->error);
rc->error = error;
}
/**
* rule_context_load:
* @f:
* @system:
* @user:
*
* Load a rule context from a system and user description file.
*
* Return value:
**/
gint
rule_context_load(RuleContext *rc, const gchar *system, const gchar *user)
{
gint res;
g_return_val_if_fail (rc, -1);
d(printf("rule_context: loading %s %s\n", system, user));
rc->priv->frozen++;
res = RULE_CONTEXT_GET_CLASS(rc)->load(rc, system, user);
rc->priv->frozen--;
return res;
}
static gint
load(RuleContext *rc, const gchar *system, const gchar *user)
{
xmlNodePtr set, rule, root;
xmlDocPtr systemdoc, userdoc;
struct _part_set_map *part_map;
struct _rule_set_map *rule_map;
rule_context_set_error(rc, NULL);
d(printf("loading rules %s %s\n", system, user));
systemdoc = e_xml_parse_file (system);
if (systemdoc == NULL) {
rule_context_set_error(rc, g_strdup_printf("Unable to load system rules '%s': %s",
system, g_strerror(errno)));
return -1;
}
root = xmlDocGetRootElement(systemdoc);
if (root == NULL || strcmp((gchar *)root->name, "filterdescription")) {
rule_context_set_error(rc, g_strdup_printf("Unable to load system rules '%s': Invalid format", system));
xmlFreeDoc(systemdoc);
return -1;
}
/* doesn't matter if this doens't exist */
userdoc = NULL;
if (g_file_test(user, G_FILE_TEST_IS_REGULAR))
userdoc = e_xml_parse_file (user);
/* now parse structure */
/* get rule parts */
set = root->children;
while (set) {
d(printf("set name = %s\n", set->name));
part_map = g_hash_table_lookup(rc->part_set_map, set->name);
if (part_map) {
d(printf("loading parts ...\n"));
rule = set->children;
while (rule) {
if (!strcmp((gchar *)rule->name, "part")) {
FilterPart *part = FILTER_PART(g_object_new(part_map->type, NULL, NULL));
if (filter_part_xml_create(part, rule, rc) == 0) {
part_map->append(rc, part);
} else {
g_object_unref(part);
g_warning("Cannot load filter part");
}
}
rule = rule->next;
}
} else if ((rule_map = g_hash_table_lookup(rc->rule_set_map, set->name))) {
d(printf("loading system rules ...\n"));
rule = set->children;
while (rule) {
d(printf("checking node: %s\n", rule->name));
if (!strcmp((gchar *)rule->name, "rule")) {
FilterRule *part = FILTER_RULE(g_object_new(rule_map->type, NULL, NULL));
if (filter_rule_xml_decode(part, rule, rc) == 0) {
part->system = TRUE;
rule_map->append(rc, part);
} else {
g_object_unref(part);
g_warning("Cannot load filter part");
}
}
rule = rule->next;
}
}
set = set->next;
}
/* now load actual rules */
if (userdoc) {
root = xmlDocGetRootElement(userdoc);
set = root?root->children:NULL;
while (set) {
d(printf("set name = %s\n", set->name));
rule_map = g_hash_table_lookup(rc->rule_set_map, set->name);
if (rule_map) {
d(printf("loading rules ...\n"));
rule = set->children;
while (rule) {
d(printf("checking node: %s\n", rule->name));
if (!strcmp((gchar *)rule->name, "rule")) {
FilterRule *part = FILTER_RULE(g_object_new(rule_map->type, NULL, NULL));
if (filter_rule_xml_decode(part, rule, rc) == 0) {
rule_map->append(rc, part);
} else {
g_object_unref(part);
g_warning("Cannot load filter part");
}
}
rule = rule->next;
}
}
set = set->next;
}
}
xmlFreeDoc(userdoc);
xmlFreeDoc(systemdoc);
return 0;
}
/**
* rule_context_save:
* @f:
* @user:
*
* Save a rule context to disk.
*
* Return value:
**/
gint
rule_context_save(RuleContext *rc, const gchar *user)
{
g_return_val_if_fail (rc, -1);
g_return_val_if_fail (user, -1);
return RULE_CONTEXT_GET_CLASS(rc)->save(rc, user);
}
static gint
save(RuleContext *rc, const gchar *user)
{
xmlDocPtr doc;
xmlNodePtr root, rules, work;
GList *l;
FilterRule *rule;
struct _rule_set_map *map;
gint ret;
doc = xmlNewDoc((const guchar *)"1.0");
/* FIXME: set character encoding to UTF-8? */
root = xmlNewDocNode(doc, NULL, (const guchar *)"filteroptions", NULL);
xmlDocSetRootElement(doc, root);
l = rc->rule_set_list;
while (l) {
map = l->data;
rules = xmlNewDocNode(doc, NULL, (guchar *)map->name, NULL);
xmlAddChild(root, rules);
rule = NULL;
while ((rule = map->next(rc, rule, NULL))) {
if (!rule->system) {
d(printf("processing rule %s\n", rule->name));
work = filter_rule_xml_encode(rule);
xmlAddChild(rules, work);
}
}
l = g_list_next(l);
}
ret = e_xml_save_file(user, doc);
xmlFreeDoc(doc);
return ret;
}
/**
* rule_context_revert:
* @f:
* @user:
*
* Reverts a rule context from a user description file. Assumes the
* system description file is unchanged from when it was loaded.
*
* Return value:
**/
gint
rule_context_revert(RuleContext *rc, const gchar *user)
{
g_return_val_if_fail (rc, 0);
d(printf("rule_context: restoring %s\n", user));
return RULE_CONTEXT_GET_CLASS(rc)->revert(rc, user);
}
struct _revert_data {
GHashTable *rules;
gint rank;
};
static void
revert_rule_remove(gpointer key, FilterRule *frule, RuleContext *rc)
{
rule_context_remove_rule(rc, frule);
g_object_unref(frule);
}
static void
revert_source_remove(gpointer key, struct _revert_data *rest_data, RuleContext *rc)
{
g_hash_table_foreach(rest_data->rules, (GHFunc)revert_rule_remove, rc);
g_hash_table_destroy(rest_data->rules);
g_free(rest_data);
}
static guint
source_hashf(const gchar *a)
{
if (a)
return g_str_hash(a);
return 0;
}
static gint
source_eqf(const gchar *a, const gchar *b)
{
return((a && b && strcmp(a, b) == 0))
|| (a == NULL && b == NULL);
}
static gint
revert(RuleContext *rc, const gchar *user)
{
xmlNodePtr set, rule;
/*struct _part_set_map *part_map;*/
struct _rule_set_map *rule_map;
struct _revert_data *rest_data;
GHashTable *source_hash;
xmlDocPtr userdoc;
FilterRule *frule;
rule_context_set_error(rc, NULL);
d(printf("restoring rules %s\n", user));
userdoc = e_xml_parse_file (user);
if (userdoc == NULL)
/* clear out anythign we have? */
return 0;
source_hash = g_hash_table_new((GHashFunc)source_hashf, (GCompareFunc)source_eqf);
/* setup stuff we have now */
/* Note that we assume there is only 1 set of rules in a given rule context,
although other parts of the code dont assume this */
frule = NULL;
while ((frule = rule_context_next_rule(rc, frule, NULL))) {
rest_data = g_hash_table_lookup(source_hash, frule->source);
if (rest_data == NULL) {
rest_data = g_malloc0(sizeof(*rest_data));
rest_data->rules = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(source_hash, frule->source, rest_data);
}
g_hash_table_insert(rest_data->rules, frule->name, frule);
}
/* make what we have, match what we load */
set = xmlDocGetRootElement(userdoc);
set = set?set->children:NULL;
while (set) {
d(printf("set name = %s\n", set->name));
rule_map = g_hash_table_lookup(rc->rule_set_map, set->name);
if (rule_map) {
d(printf("loading rules ...\n"));
rule = set->children;
while (rule) {
d(printf("checking node: %s\n", rule->name));
if (!strcmp((gchar *)rule->name, "rule")) {
FilterRule *part = FILTER_RULE(g_object_new(rule_map->type, NULL, NULL));
if (filter_rule_xml_decode(part, rule, rc) == 0) {
/* use the revert data to keep track of the right rank of this rule part */
rest_data = g_hash_table_lookup(source_hash, part->source);
if (rest_data == NULL) {
rest_data = g_malloc0(sizeof(*rest_data));
rest_data->rules = g_hash_table_new(g_str_hash, g_str_equal);
g_hash_table_insert(source_hash, part->source, rest_data);
}
frule = g_hash_table_lookup(rest_data->rules, part->name);
if (frule) {
if (rc->priv->frozen == 0 && !filter_rule_eq(frule, part))
filter_rule_copy(frule, part);
g_object_unref(part);
rule_context_rank_rule(rc, frule, frule->source, rest_data->rank);
g_hash_table_remove(rest_data->rules, frule->name);
} else {
rule_context_add_rule(rc, part);
rule_context_rank_rule(rc, part, part->source, rest_data->rank);
}
rest_data->rank++;
} else {
g_object_unref(part);
g_warning("Cannot load filter part");
}
}
rule = rule->next;
}
}
set = set->next;
}
xmlFreeDoc(userdoc);
/* remove any we still have that weren't in the file */
g_hash_table_foreach(source_hash, (GHFunc)revert_source_remove, rc);
g_hash_table_destroy(source_hash);
return 0;
}
FilterPart *
rule_context_find_part(RuleContext *rc, const gchar *name)
{
g_return_val_if_fail (rc, NULL);
g_return_val_if_fail (name, NULL);
d(printf("find part : "));
return filter_part_find_list(rc->parts, name);
}
FilterPart *
rule_context_create_part(RuleContext *rc, const gchar *name)
{
FilterPart *part;
g_return_val_if_fail (rc, NULL);
g_return_val_if_fail (name, NULL);
if ((part = rule_context_find_part(rc, name)))
return filter_part_clone(part);
return NULL;
}
FilterPart *
rule_context_next_part(RuleContext *rc, FilterPart *last)
{
g_return_val_if_fail (rc, NULL);
return filter_part_next_list(rc->parts, last);
}
FilterRule *
rule_context_next_rule(RuleContext *rc, FilterRule *last, const gchar *source)
{
g_return_val_if_fail (rc, NULL);
return filter_rule_next_list(rc->rules, last, source);
}
FilterRule *
rule_context_find_rule(RuleContext *rc, const gchar *name, const gchar *source)
{
g_return_val_if_fail (name, NULL);
g_return_val_if_fail (rc, NULL);
return filter_rule_find_list(rc->rules, name, source);
}
void
rule_context_add_part(RuleContext *rc, FilterPart *part)
{
g_return_if_fail (rc);
g_return_if_fail (part);
rc->parts = g_list_append(rc->parts, part);
}
void
rule_context_add_rule(RuleContext *rc, FilterRule *new)
{
g_return_if_fail (rc);
g_return_if_fail (new);
d(printf("add rule '%s'\n", new->name));
rc->rules = g_list_append(rc->rules, new);
if (rc->priv->frozen == 0) {
g_signal_emit(rc, signals[RULE_ADDED], 0, new);
g_signal_emit(rc, signals[CHANGED], 0);
}
}
static void
new_rule_response(GtkWidget *dialog, gint button, RuleContext *context)
{
if (button == GTK_RESPONSE_OK) {
FilterRule *rule = g_object_get_data((GObject *) dialog, "rule");
gchar *user = g_object_get_data((GObject *) dialog, "path");
if (!filter_rule_validate(rule)) {
/* no need to popup a dialog because the validate code does that. */
return;
}
if (rule_context_find_rule (context, rule->name, rule->source)) {
e_error_run((GtkWindow *)dialog, "filter:bad-name-notunique", rule->name, NULL);
return;
}
g_object_ref(rule);
rule_context_add_rule(context, rule);
if (user)
rule_context_save(context, user);
}
gtk_widget_destroy(dialog);
}
/* add a rule, with a gui, asking for confirmation first ... optionally save to path */
void
rule_context_add_rule_gui(RuleContext *rc, FilterRule *rule, const gchar *title, const gchar *path)
{
GtkDialog *dialog;
GtkWidget *widget;
d(printf("add rule gui '%s'\n", rule->name));
g_return_if_fail (rc);
g_return_if_fail (rule);
widget = filter_rule_get_widget(rule, rc);
gtk_widget_show(widget);
dialog =(GtkDialog *) gtk_dialog_new();
gtk_dialog_add_buttons(dialog,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_has_separator (dialog, FALSE);
gtk_window_set_title((GtkWindow *) dialog, title);
gtk_window_set_default_size((GtkWindow *) dialog, 600, 400);
gtk_window_set_resizable((GtkWindow *) dialog, TRUE);
gtk_box_pack_start((GtkBox *) dialog->vbox, widget, TRUE, TRUE, 0);
g_object_set_data_full((GObject *) dialog, "rule", rule, g_object_unref);
if (path)
g_object_set_data_full((GObject *) dialog, "path", g_strdup(path), g_free);
g_signal_connect(dialog, "response", G_CALLBACK(new_rule_response), rc);
g_object_ref(rc);
g_object_set_data_full((GObject *) dialog, "context", rc, g_object_unref);
gtk_widget_show((GtkWidget *) dialog);
}
void
rule_context_remove_rule(RuleContext *rc, FilterRule *rule)
{
g_return_if_fail (rc);
g_return_if_fail (rule);
d(printf("remove rule '%s'\n", rule->name));
rc->rules = g_list_remove(rc->rules, rule);
if (rc->priv->frozen == 0) {
g_signal_emit(rc, signals[RULE_REMOVED], 0, rule);
g_signal_emit(rc, signals[CHANGED], 0);
}
}
void
rule_context_rank_rule(RuleContext *rc, FilterRule *rule, const gchar *source, gint rank)
{
GList *node;
gint i = 0, index = 0;
g_return_if_fail (rc);
g_return_if_fail (rule);
if (rule_context_get_rank_rule (rc, rule, source) == rank)
return;
rc->rules = g_list_remove(rc->rules, rule);
node = rc->rules;
while (node) {
FilterRule *r = node->data;
if (i == rank) {
rc->rules = g_list_insert(rc->rules, rule, index);
if (rc->priv->frozen == 0)
g_signal_emit(rc, signals[CHANGED], 0);
return;
}
index++;
if (source == NULL || (r->source && strcmp(r->source, source) == 0))
i++;
node = node->next;
}
rc->rules = g_list_append(rc->rules, rule);
if (rc->priv->frozen == 0)
g_signal_emit(rc, signals[CHANGED], 0);
}
gint
rule_context_get_rank_rule(RuleContext *rc, FilterRule *rule, const gchar *source)
{
GList *node;
gint i = 0;
g_return_val_if_fail (rc, -1);
g_return_val_if_fail (rule, -1);
d(printf("getting rank of rule '%s'\n", rule->name));
node = rc->rules;
while (node) {
FilterRule *r = node->data;
d(printf(" checking against rule '%s' rank '%d'\n", r->name, i));
if (r == rule)
return i;
if (source == NULL || (r->source && strcmp(r->source, source) == 0))
i++;
node = node->next;
}
return -1;
}
FilterRule *
rule_context_find_rank_rule(RuleContext *rc, gint rank, const gchar *source)
{
GList *node;
gint i = 0;
g_return_val_if_fail (rc, NULL);
d(printf("getting rule at rank %d source '%s'\n", rank, source?source:"<any>"));
node = rc->rules;
while (node) {
FilterRule *r = node->data;
d(printf(" checking against rule '%s' rank '%d'\n", r->name, i));
if (source == NULL || (r->source && strcmp(r->source, source) == 0)) {
if (rank == i)
return r;
i++;
}
node = node->next;
}
return NULL;
}
static GList *
delete_uri(RuleContext *rc, const gchar *uri, GCompareFunc cmp)
{
return NULL;
}
GList *
rule_context_delete_uri(RuleContext *rc, const gchar *uri, GCompareFunc cmp)
{
return RULE_CONTEXT_GET_CLASS(rc)->delete_uri(rc, uri, cmp);
}
static GList *
rename_uri(RuleContext *rc, const gchar *olduri, const gchar *newuri, GCompareFunc cmp)
{
return NULL;
}
GList *
rule_context_rename_uri(RuleContext *rc, const gchar *olduri, const gchar *newuri, GCompareFunc cmp)
{
return RULE_CONTEXT_GET_CLASS(rc)->rename_uri(rc, olduri, newuri, cmp);
}
void
rule_context_free_uri_list(RuleContext *rc, GList *uris)
{
GList *l = uris, *n;
/* TODO: should be virtual */
while (l) {
n = l->next;
g_free(l->data);
g_list_free_1(l);
l = n;
}
}
static FilterElement *
new_element(RuleContext *rc, const gchar *type)
{
if (!strcmp (type, "string")) {
return (FilterElement *) filter_input_new ();
} else if (!strcmp (type, "address")) {
/* FIXME: temporary ... need real address type */
return (FilterElement *) filter_input_new_type_name (type);
} else if (!strcmp (type, "code")) {
return (FilterElement *) filter_code_new (FALSE);
} else if (!strcmp (type, "rawcode")) {
return (FilterElement *) filter_code_new (TRUE);
} else if (!strcmp (type, "colour")) {
return (FilterElement *) filter_colour_new ();
} else if (!strcmp (type, "optionlist")) {
return (FilterElement *) filter_option_new ();
} else if (!strcmp (type, "datespec")) {
return (FilterElement *) filter_datespec_new ();
} else if (!strcmp (type, "command")) {
return (FilterElement *) filter_file_new_type_name (type);
} else if (!strcmp (type, "file")) {
return (FilterElement *) filter_file_new_type_name (type);
} else if (!strcmp (type, "integer")) {
return (FilterElement *) filter_int_new ();
} else if (!strcmp (type, "regex")) {
return (FilterElement *) filter_input_new_type_name (type);
}else if (!strcmp(type, "completedpercent")) {
return (FilterElement *) filter_int_new_type("completedpercent", 0,100);
} else {
g_warning("Unknown filter type '%s'", type);
return NULL;
}
}
/**
* rule_context_new_element:
* @rc:
* @name:
*
* create a new filter element based on name.
*
* Return value:
**/
FilterElement *
rule_context_new_element(RuleContext *rc, const gchar *name)
{
if (name == NULL)
return NULL;
return RULE_CONTEXT_GET_CLASS(rc)->new_element(rc, name);
}
|