diff options
-rw-r--r-- | filter/ChangeLog | 4 | ||||
-rw-r--r-- | filter/rule-context.c | 600 | ||||
-rw-r--r-- | filter/rule-context.h | 125 |
3 files changed, 370 insertions, 359 deletions
diff --git a/filter/ChangeLog b/filter/ChangeLog index 0a4c0fdadf..c9d33d7c10 100644 --- a/filter/ChangeLog +++ b/filter/ChangeLog @@ -1,3 +1,7 @@ +2002-11-02 Jeffrey Stedfast <fejj@ximian.com> + + * rule-context.c: Ported to glib2/GObject. + 2002-11-01 Jeffrey Stedfast <fejj@ximian.com> * filter-element.c: Ported to GObject. diff --git a/filter/rule-context.c b/filter/rule-context.c index f27a13df0e..69de387100 100644 --- a/filter/rule-context.c +++ b/filter/rule-context.c @@ -1,8 +1,9 @@ /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2000 Ximian Inc. + * Copyright (C) 2000-2002 Ximian Inc. * * Authors: Not Zed <notzed@lostzed.mmc.com.au> + * Jeffrey Stedfast <fejj@ximian.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -24,10 +25,6 @@ #include <config.h> #endif -#ifdef HAVE_ALLOCA_H -#include <alloca.h> -#endif - #include <string.h> #include <sys/types.h> #include <sys/stat.h> @@ -35,7 +32,6 @@ #include <fcntl.h> #include <errno.h> -#include <gtk/gtksignal.h> #include <libgnomeui/gnome-dialog.h> #include <libgnomeui/gnome-stock.h> @@ -51,9 +47,9 @@ static int revert(RuleContext *f, const char *user); static GList *rename_uri(RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp); static GList *delete_uri(RuleContext *f, const char *uri, GCompareFunc cmp); -static void rule_context_class_init(RuleContextClass * class); -static void rule_context_init(RuleContext * gspaper); -static void rule_context_finalise(GtkObject * obj); +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) @@ -61,7 +57,7 @@ struct _RuleContextPrivate { int frozen; }; -static GtkObjectClass *parent_class; +static GObjectClass *parent_class = NULL; enum { RULE_ADDED, @@ -72,80 +68,82 @@ enum { static guint signals[LAST_SIGNAL] = { 0 }; -guint -rule_context_get_type(void) -{ - static guint type = 0; +GType +rule_context_get_type (void) +{ + static GType type = 0; + if (!type) { - GtkTypeInfo type_info = { - "RuleContext", - sizeof(RuleContext), - sizeof(RuleContextClass), - (GtkClassInitFunc) rule_context_class_init, - (GtkObjectInitFunc) rule_context_init, - (GtkArgSetFunc) NULL, - (GtkArgGetFunc) NULL + 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 = gtk_type_unique(gtk_object_get_type(), &type_info); + + type = g_type_register_static (G_TYPE_OBJECT, "RuleContext", &info, 0); } - + return type; } static void -rule_context_class_init (RuleContextClass * class) +rule_context_class_init (RuleContextClass *klass) { - GtkObjectClass *object_class; + GObjectClass *object_class = G_OBJECT_CLASS (klass); - object_class = (GtkObjectClass *) class; - parent_class = gtk_type_class(gtk_object_get_type()); + parent_class = g_type_class_ref (G_TYPE_OBJECT); object_class->finalize = rule_context_finalise; /* override methods */ - class->load = load; - class->save = save; - class->revert = revert; - class->rename_uri = rename_uri; - class->delete_uri = delete_uri; - + klass->load = load; + klass->save = save; + klass->revert = revert; + klass->rename_uri = rename_uri; + klass->delete_uri = delete_uri; + /* signals */ signals[RULE_ADDED] = - gtk_signal_new("rule_added", - GTK_RUN_LAST, - object_class->type, - GTK_SIGNAL_OFFSET (RuleContextClass, rule_added), - gtk_marshal_NONE__POINTER, - GTK_TYPE_NONE, 1, GTK_TYPE_POINTER); - + g_signal_new ("rule_added", + RULE_TYPE_CONTEXT, + G_SIGNAL_RUN_LAST, + GTK_SIGNAL_OFFSET (RuleContextClass, rule_added), + NULL, + NULL, + gtk_marshal_NONE__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); + signals[RULE_REMOVED] = - gtk_signal_new("rule_removed", - GTK_RUN_LAST, - object_class->type, - GTK_SIGNAL_OFFSET (RuleContextClass, rule_removed), - gtk_marshal_NONE__POINTER, - GTK_TYPE_NONE, 1, GTK_TYPE_POINTER); - - signals[CHANGED] = - gtk_signal_new("changed", - GTK_RUN_LAST, - object_class->type, - GTK_SIGNAL_OFFSET (RuleContextClass, changed), - gtk_marshal_NONE__NONE, - GTK_TYPE_NONE, 0); + g_signal_new ("rule_removed", + RULE_TYPE_CONTEXT, + G_SIGNAL_RUN_LAST, + GTK_SIGNAL_OFFSET (RuleContextClass, rule_removed), + gtk_marshal_NONE__POINTER, + G_TYPE_NONE, 1, G_TYPE_POINTER); - gtk_object_class_add_signals(object_class, signals, LAST_SIGNAL); + signals[CHANGED] = + g_signal_new ("changed", + RULE_TYPE_CONTEXT, + G_SIGNAL_RUN_LAST, + GTK_SIGNAL_OFFSET (RuleContextClass, changed), + gtk_marshal_NONE__NONE, + G_TYPE_NONE, 0); } static void -rule_context_init (RuleContext * o) +rule_context_init (RuleContext *rc) { - o->priv = g_malloc0 (sizeof (*o->priv)); + rc->priv = g_malloc0 (sizeof (*rc->priv)); - o->part_set_map = g_hash_table_new (g_str_hash, g_str_equal); - o->rule_set_map = g_hash_table_new (g_str_hash, g_str_equal); + 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); } static void @@ -163,33 +161,33 @@ free_rule_set (struct _rule_set_map *map, void *data) } static void -rule_context_finalise (GtkObject * obj) +rule_context_finalise (GObject *obj) { - RuleContext *o = (RuleContext *) obj; + RuleContext *rc = (RuleContext *) obj; - g_list_foreach (o->rule_set_list, (GFunc)free_rule_set, NULL); - g_list_free (o->rule_set_list); - g_hash_table_destroy (o->rule_set_map); + 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 (o->part_set_list, (GFunc)free_part_set, NULL); - g_list_free (o->part_set_list); - g_hash_table_destroy (o->part_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 (o->error); + g_free (rc->error); - g_list_foreach (o->parts, (GFunc)gtk_object_unref, NULL); - g_list_free (o->parts); - g_list_foreach (o->rules, (GFunc)gtk_object_unref, NULL); - g_list_free (o->rules); + 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); - if (o->system) - xmlFreeDoc (o->system); - if (o->user) - xmlFreeDoc (o->user); + if (rc->system) + xmlFreeDoc (rc->system); + if (rc->user) + xmlFreeDoc (rc->user); - g_free (o->priv); + g_free (rc->priv); - ((GtkObjectClass *) (parent_class))->finalize (obj); + G_OBJECT_CLASS (parent_class)->finalize (obj); } /** @@ -202,42 +200,40 @@ rule_context_finalise (GtkObject * obj) RuleContext * rule_context_new (void) { - RuleContext *o = (RuleContext *) gtk_type_new(rule_context_get_type()); - - return o; + return (RuleContext *) g_object_new (RULE_TYPE_CONTEXT, NULL, NULL); } void -rule_context_add_part_set (RuleContext * f, const char *setname, int part_type, RCPartFunc append, RCNextPartFunc next) +rule_context_add_part_set (RuleContext *rc, const char *setname, int part_type, RCPartFunc append, RCNextPartFunc next) { struct _part_set_map *map; - g_assert(g_hash_table_lookup(f->part_set_map, setname) == NULL); + g_assert(g_hash_table_lookup (rc->part_set_map, setname) == NULL); - map = g_malloc0 (sizeof(*map)); + map = g_malloc0 (sizeof (*map)); map->type = part_type; map->append = append; map->next = next; map->name = g_strdup (setname); - g_hash_table_insert (f->part_set_map, map->name, map); - f->part_set_list = g_list_append (f->part_set_list, map); + 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 * f, const char *setname, int rule_type, RCRuleFunc append, RCNextRuleFunc next) +rule_context_add_rule_set (RuleContext *rc, const char *setname, int rule_type, RCRuleFunc append, RCNextRuleFunc next) { struct _rule_set_map *map; - - g_assert(g_hash_table_lookup (f->rule_set_map, setname) == NULL); + + g_assert(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 (f->rule_set_map, map->name, map); - f->rule_set_list = g_list_append (f->rule_set_list, map); + 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)); } @@ -249,12 +245,12 @@ rule_context_add_rule_set (RuleContext * f, const char *setname, int rule_type, * Set the text error for the context, or NULL to clear it. **/ static void -rule_context_set_error (RuleContext * f, char *error) +rule_context_set_error (RuleContext *rc, char *error) { - g_assert(f); - - g_free (f->error); - f->error = error; + g_assert(rc); + + g_free (rc->error); + rc->error = error; } /** @@ -268,64 +264,65 @@ rule_context_set_error (RuleContext * f, char *error) * Return value: **/ int -rule_context_load (RuleContext *f, const char *system, const char *user) +rule_context_load (RuleContext *rc, const char *system, const char *user) { int res; - - g_assert(f); - + + g_assert(rc); + d(printf("rule_context: loading %s %s\n", system, user)); - - f->priv->frozen++; - res= ((RuleContextClass *) ((GtkObject *) f)->klass)->load (f, system, user); - f->priv->frozen--; - + + rc->priv->frozen++; + res = RULE_CONTEXT_GET_CLASS (rc)->load (rc, system, user); + rc->priv->frozen--; + return res; } static int -load (RuleContext *f, const char *system, const char *user) +load (RuleContext *rc, const char *system, const char *user) { xmlNodePtr set, rule; struct _part_set_map *part_map; struct _rule_set_map *rule_map; - rule_context_set_error (f, NULL); + rule_context_set_error (rc, NULL); d(printf("loading rules %s %s\n", system, user)); - f->system = xmlParseFile (system); - if (f->system == NULL) { - rule_context_set_error(f, g_strdup_printf ("Unable to load system rules '%s': %s", - system, strerror(errno))); + rc->system = xmlParseFile (system); + if (rc->system == NULL) { + rule_context_set_error (rc, g_strdup_printf ("Unable to load system rules '%s': %s", + system, g_strerror (errno))); return -1; } - if (strcmp (f->system->root->name, "filterdescription")) { - rule_context_set_error (f, g_strdup_printf ("Unable to load system rules '%s': Invalid format", system)); - xmlFreeDoc (f->system); - f->system = NULL; + + if (strcmp (rc->system->root->name, "filterdescription")) { + rule_context_set_error (rc, g_strdup_printf ("Unable to load system rules '%s': Invalid format", system)); + xmlFreeDoc (rc->system); + rc->system = NULL; return -1; } /* doesn't matter if this doens't exist */ - f->user = xmlParseFile (user); + rc->user = xmlParseFile (user); /* now parse structure */ /* get rule parts */ - set = f->system->root->childs; + set = rc->system->root->childs; while (set) { d(printf("set name = %s\n", set->name)); - part_map = g_hash_table_lookup (f->part_set_map, set->name); + part_map = g_hash_table_lookup (rc->part_set_map, set->name); if (part_map) { d(printf("loading parts ...\n")); rule = set->childs; while (rule) { if (!strcmp (rule->name, "part")) { - FilterPart *part = FILTER_PART (gtk_type_new (part_map->type)); + FilterPart *part = FILTER_PART (g_object_new (part_map->type, NULL, NULL)); if (filter_part_xml_create (part, rule) == 0) { - part_map->append (f, part); + part_map->append (rc, part); } else { - gtk_object_unref (GTK_OBJECT (part)); + g_object_unref (part); g_warning ("Cannot load filter part"); } } @@ -336,23 +333,23 @@ load (RuleContext *f, const char *system, const char *user) } /* now load actual rules */ - if (f->user) { - set = f->user->root->childs; + if (rc->user) { + set = rc->user->root->childs; while (set) { d(printf("set name = %s\n", set->name)); - rule_map = g_hash_table_lookup (f->rule_set_map, set->name); + rule_map = g_hash_table_lookup (rc->rule_set_map, set->name); if (rule_map) { d(printf("loading rules ...\n")); rule = set->childs; while (rule) { d(printf("checking node: %s\n", rule->name)); if (!strcmp (rule->name, "rule")) { - FilterRule *part = FILTER_RULE(gtk_type_new (rule_map->type)); + FilterRule *part = FILTER_RULE (g_object_new (rule_map->type, NULL, NULL)); - if (filter_rule_xml_decode (part, rule, f) == 0) { - rule_map->append (f, part); + if (filter_rule_xml_decode (part, rule, rc) == 0) { + rule_map->append (rc, part); } else { - gtk_object_unref (GTK_OBJECT (part)); + gtk_object_unref (part); g_warning ("Cannot load filter part"); } } @@ -362,6 +359,7 @@ load (RuleContext *f, const char *system, const char *user) set = set->next; } } + return 0; } @@ -375,16 +373,16 @@ load (RuleContext *f, const char *system, const char *user) * Return value: **/ int -rule_context_save (RuleContext *f, const char *user) +rule_context_save (RuleContext *rc, const char *user) { - g_assert(f); + g_assert(rc); g_assert(user); - - return ((RuleContextClass *) ((GtkObject *) f)->klass)->save(f, user); + + return RULE_CONTEXT_CLASS (rc)->save (rc, user); } static int -save (RuleContext *f, const char *user) +save (RuleContext *rc, const char *user) { xmlDocPtr doc; xmlNodePtr root, rules, work; @@ -396,13 +394,13 @@ save (RuleContext *f, const char *user) doc = xmlNewDoc ("1.0"); root = xmlNewDocNode (doc, NULL, "filteroptions", NULL); xmlDocSetRootElement (doc, root); - l = f->rule_set_list; + l = rc->rule_set_list; while (l) { map = l->data; rules = xmlNewDocNode (doc, NULL, map->name, NULL); xmlAddChild (root, rules); rule = NULL; - while ((rule = map->next (f, rule, NULL))) { + while ((rule = map->next (rc, rule, NULL))) { d(printf("processing rule %s\n", rule->name)); work = filter_rule_xml_encode (rule); xmlAddChild (rules, work); @@ -428,13 +426,13 @@ save (RuleContext *f, const char *user) * Return value: **/ int -rule_context_revert(RuleContext *f, const char *user) +rule_context_revert(RuleContext *rc, const char *user) { - g_assert(f); - + g_assert(rc); + d(printf("rule_context: restoring %s %s\n", user)); - - return ((RuleContextClass *) ((GtkObject *) f)->klass)->revert(f, user); + + return RULE_CONTEXT_CLASS (rc)->revert (rc, user); } struct _revert_data { @@ -443,35 +441,37 @@ struct _revert_data { }; static void -revert_rule_remove(void *key, FilterRule *frule, RuleContext *f) +revert_rule_remove (void *key, FilterRule *frule, RuleContext *rc) { - rule_context_remove_rule(f, frule); - gtk_object_unref((GtkObject *)frule); + rule_context_remove_rule (rc, frule); + g_object_unref (frule); } static void -revert_source_remove(void *key, struct _revert_data *rest_data, RuleContext *f) +revert_source_remove(void *key, struct _revert_data *rest_data, RuleContext *rc) { - g_hash_table_foreach(rest_data->rules, (GHFunc)revert_rule_remove, f); - g_hash_table_destroy(rest_data->rules); - g_free(rest_data); + 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 char *a) +static guint +source_hashf (const char *a) { if (a) - return g_str_hash(a); + return g_str_hash (a); return 0; } -static int source_eqf(const char *a, const char *b) +static int +source_eqf (const char *a, const char *b) { - return (a && b && strcmp(a, b) == 0) + return (a && b && strcmp (a, b) == 0) || (a == NULL && b == NULL); } static int -revert(RuleContext *f, const char *user) +revert (RuleContext *rc, const char *user) { xmlNodePtr set, rule; /*struct _part_set_map *part_map;*/ @@ -480,67 +480,67 @@ revert(RuleContext *f, const char *user) GHashTable *source_hash; xmlDocPtr userdoc; FilterRule *frule; - - rule_context_set_error (f, NULL); + + rule_context_set_error (rc, NULL); d(printf("restoring rules %s %s\n", user)); - + userdoc = xmlParseFile (user); if (userdoc == NULL) /* clear out anythign we have? */ return 0; - - source_hash = g_hash_table_new((GHashFunc)source_hashf, (GCompareFunc)source_eqf); - + + 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(f, frule, NULL))) { - rest_data = g_hash_table_lookup(source_hash, frule->source); + 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); + 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); + g_hash_table_insert (rest_data->rules, frule->name, frule); } - + /* make what we have, match what we load */ set = userdoc->root->childs; while (set) { d(printf("set name = %s\n", set->name)); - rule_map = g_hash_table_lookup (f->rule_set_map, set->name); + rule_map = g_hash_table_lookup (rc->rule_set_map, set->name); if (rule_map) { d(printf("loading rules ...\n")); rule = set->childs; while (rule) { d(printf("checking node: %s\n", rule->name)); if (!strcmp (rule->name, "rule")) { - FilterRule *part = FILTER_RULE(gtk_type_new (rule_map->type)); + FilterRule *part = FILTER_RULE (g_object_new (rule_map->type, NULL, NULL)); - if (filter_rule_xml_decode (part, rule, f) == 0) { + 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); + 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); + 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); + frule = g_hash_table_lookup (rest_data->rules, part->name); if (frule) { - if (f->priv->frozen == 0 && !filter_rule_eq(frule, part)) - filter_rule_copy(frule, part); - gtk_object_unref (GTK_OBJECT (part)); - rule_context_rank_rule(f, frule, rest_data->rank); - g_hash_table_remove(rest_data->rules, frule->name); + if (f->priv->frozen == 0 && !filter_rule_eq (frule, part)) + filter_rule_copy (frule, part); + g_object_unref (part); + rule_context_rank_rule (rc, frule, rest_data->rank); + g_hash_table_remove (rest_data->rules, frule->name); } else { - rule_context_add_rule(f, part); - rule_context_rank_rule(f, part, rest_data->rank); + rule_context_add_rule (rc, part); + rule_context_rank_rule (rc, part, rest_data->rank); } rest_data->rank++; } else { - gtk_object_unref (GTK_OBJECT (part)); + g_object_unref (part); g_warning ("Cannot load filter part"); } } @@ -549,87 +549,87 @@ revert(RuleContext *f, const char *user) } set = set->next; } - - xmlFreeDoc(userdoc); - + + xmlFreeDoc (userdoc); + /* remove any we still have that weren't in the file */ - g_hash_table_foreach(source_hash, (GHFunc)revert_source_remove, f); - g_hash_table_destroy(source_hash); - + 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 *f, const char *name) +rule_context_find_part (RuleContext *rc, const char *name) { - g_assert(f); + g_assert(rc); g_assert(name); - + d(printf("find part : ")); - return filter_part_find_list (f->parts, name); + return filter_part_find_list (rc->parts, name); } FilterPart * -rule_context_create_part (RuleContext *f, const char *name) +rule_context_create_part (RuleContext *rc, const char *name) { FilterPart *part; - - g_assert(f); + + g_assert(rc); g_assert(name); - - part = rule_context_find_part (f, name); - if (part) - part = filter_part_clone (part); - return part; + + if ((part = rule_context_find_part (rc, name))) + return filter_part_clone (part); + + return NULL; } FilterPart * -rule_context_next_part (RuleContext *f, FilterPart *last) +rule_context_next_part (RuleContext *rc, FilterPart *last) { - g_assert(f); - - return filter_part_next_list (f->parts, last); + g_assert(rc); + + return filter_part_next_list (rc->parts, last); } FilterRule * -rule_context_next_rule (RuleContext *f, FilterRule *last, const char *source) +rule_context_next_rule (RuleContext *rc, FilterRule *last, const char *source) { - g_assert(f); - - return filter_rule_next_list (f->rules, last, source); + g_assert(rc); + + return filter_rule_next_list (rc->rules, last, source); } FilterRule * -rule_context_find_rule (RuleContext *f, const char *name, const char *source) +rule_context_find_rule (RuleContext *rc, const char *name, const char *source) { g_assert(name); - g_assert(f); - - return filter_rule_find_list (f->rules, name, source); + g_assert(rc); + + return filter_rule_find_list (rc->rules, name, source); } void -rule_context_add_part (RuleContext *f, FilterPart *part) +rule_context_add_part (RuleContext *rc, FilterPart *part) { - g_assert(f); + g_assert(rc); g_assert(part); - - f->parts = g_list_append (f->parts, part); + + rc->parts = g_list_append (rc->parts, part); } void -rule_context_add_rule (RuleContext *f, FilterRule *new) +rule_context_add_rule (RuleContext *rc, FilterRule *new) { - g_assert(f); + g_assert(rc); g_assert(new); - + d(printf("add rule '%s'\n", new->name)); - - f->rules = g_list_append (f->rules, new); - - if (f->priv->frozen == 0) { - gtk_signal_emit((GtkObject *)f, signals[RULE_ADDED], new); - gtk_signal_emit((GtkObject *)f, signals[CHANGED]); + + rc->rules = g_list_append (rc->rules, new); + + if (rc->priv->frozen == 0) { + g_signal_emit (rc, signals[RULE_ADDED], new); + g_signal_emit (rc, signals[CHANGED]); } } @@ -637,125 +637,127 @@ static void new_rule_clicked (GtkWidget *dialog, int button, RuleContext *context) { if (button == 0) { - FilterRule *rule = gtk_object_get_data (GTK_OBJECT (dialog), "rule"); - char *user = gtk_object_get_data (GTK_OBJECT (dialog), "path"); + FilterRule *rule = g_object_get_data (dialog, "rule"); + char *user = g_object_get_data (dialog, "path"); if (!filter_rule_validate (rule)) { /* no need to popup a dialog because the validate code does that. */ return; } - gtk_object_ref (GTK_OBJECT (rule)); + g_object_ref (rule); rule_context_add_rule (context, rule); - if (user) { - rule_context_save ((RuleContext *) context, user); - } + if (user) + rule_context_save (context, user); } - if (button != -1) { + if (button != -1) gnome_dialog_close (GNOME_DIALOG (dialog)); - } } /* add a rule, with a gui, asking for confirmation first ... optionally save to path */ void -rule_context_add_rule_gui (RuleContext *f, FilterRule *rule, const char *title, const char *path) +rule_context_add_rule_gui (RuleContext *rc, FilterRule *rule, const char *title, const char *path) { GtkWidget *dialog, *w; - + d(printf("add rule gui '%s'\n", rule->name)); - - g_assert(f); + + g_assert(rc); g_assert(rule); - w = filter_rule_get_widget (rule, f); + w = filter_rule_get_widget (rule, rc); dialog = gnome_dialog_new (title, GNOME_STOCK_BUTTON_OK, GNOME_STOCK_BUTTON_CANCEL, NULL); gtk_window_set_policy (GTK_WINDOW (dialog), FALSE, TRUE, FALSE); gtk_box_pack_start (GTK_BOX (GNOME_DIALOG (dialog)->vbox), w, TRUE, TRUE, 0); gtk_window_set_default_size (GTK_WINDOW (dialog), 600, 400); gtk_widget_show (w); - gtk_object_set_data_full (GTK_OBJECT (dialog), "rule", rule, (GtkDestroyNotify) gtk_object_unref); + g_object_set_data_full (dialog, "rule", rule, (GtkDestroyNotify) g_object_unref); if (path) - gtk_object_set_data_full (GTK_OBJECT (dialog), "path", g_strdup (path), (GtkDestroyNotify) g_free); - gtk_signal_connect (GTK_OBJECT (dialog), "clicked", new_rule_clicked, f); - gtk_object_ref (GTK_OBJECT (f)); - gtk_object_set_data_full (GTK_OBJECT (dialog), "context", f, (GtkDestroyNotify) gtk_object_unref); + g_object_set_data_full (dialog, "path", g_strdup (path), (GtkDestroyNotify) g_free); + + g_signal_connect (dialog, "clicked", new_rule_clicked, rc); + + g_object_ref (rc); + + g_object_set_data_full (dialog, "context", rc, (GtkDestroyNotify) g_object_unref); + gtk_widget_show (dialog); } void -rule_context_remove_rule (RuleContext *f, FilterRule *rule) +rule_context_remove_rule (RuleContext *rc, FilterRule *rule) { - g_assert(f); + g_assert(rc); g_assert(rule); - + d(printf("remove rule '%s'\n", rule->name)); - - f->rules = g_list_remove (f->rules, rule); - - if (f->priv->frozen == 0) { - gtk_signal_emit((GtkObject *)f, signals[RULE_REMOVED], rule); - gtk_signal_emit((GtkObject *)f, signals[CHANGED]); + + rc->rules = g_list_remove (rc->rules, rule); + + if (rc->priv->frozen == 0) { + g_signal_emit (rc, signals[RULE_REMOVED], rule); + g_signal_emit (rc, signals[CHANGED]); } } void -rule_context_rank_rule (RuleContext *f, FilterRule *rule, int rank) +rule_context_rank_rule (RuleContext *rc, FilterRule *rule, int rank) { GList *node; int i = 0, index = 0; - - g_assert(f); + + g_assert(rc); g_assert(rule); - - if (rule_context_get_rank_rule(f, rule, rule->source) == rank) + + if (rule_context_get_rank_rule (rc, rule, rule->source) == rank) return; - - f->rules = g_list_remove(f->rules, rule); - node = f->rules; + + rc->rules = g_list_remove (rc->rules, rule); + node = rc->rules; while (node) { FilterRule *r = node->data; - + if (i == rank) { - f->rules = g_list_insert(f->rules, rule, index); + rc->rules = g_list_insert (f->rules, rule, index); if (f->priv->frozen == 0) - gtk_signal_emit((GtkObject *)f, signals[CHANGED]); + g_signal_emit (rc, signals[CHANGED]); return; } - + index++; if (rule->source == NULL || (r->source && strcmp (r->source, rule->source) == 0)) i++; - + node = node->next; } - - f->rules = g_list_append(f->rules, rule); - if (f->priv->frozen == 0) - gtk_signal_emit((GtkObject *)f, signals[CHANGED]); + + rc->rules = g_list_append (rc->rules, rule); + if (rc->priv->frozen == 0) + g_signal_emit (rc, signals[CHANGED]); } int -rule_context_get_rank_rule (RuleContext *f, FilterRule *rule, const char *source) +rule_context_get_rank_rule (RuleContext *rc, FilterRule *rule, const char *source) { GList *node; int i = 0; - - g_assert(f); + + g_assert(rc); g_assert(rule); - + d(printf("getting rank of rule '%s'\n", rule->name)); - - node = f->rules; + + 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++; @@ -766,22 +768,22 @@ rule_context_get_rank_rule (RuleContext *f, FilterRule *rule, const char *source } FilterRule * -rule_context_find_rank_rule (RuleContext *f, int rank, const char *source) +rule_context_find_rank_rule (RuleContext *rc, int rank, const char *source) { GList *node; int i = 0; - - g_assert(f); - + + g_assert(rc); + d(printf("getting rule at rank %d source '%s'\n", rank, source?source:"<any>")); - - node = f->rules; + + 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 (source == NULL || (r->source && strcmp (r->source, source) == 0)) { if (rank == i) return r; i++; @@ -794,42 +796,40 @@ rule_context_find_rank_rule (RuleContext *f, int rank, const char *source) } static GList * -delete_uri(RuleContext *f, const char *uri, GCompareFunc cmp) +delete_uri (RuleContext *rc, const char *uri, GCompareFunc cmp) { return NULL; } GList * -rule_context_delete_uri(RuleContext *f, const char *uri, GCompareFunc cmp) +rule_context_delete_uri (RuleContext *rc, const char *uri, GCompareFunc cmp) { - return ((RuleContextClass *) ((GtkObject *) f)->klass)->delete_uri(f, uri, cmp); + return RULE_CONTEXT_GET_CLASS (rc)->delete_uri (rc, uri, cmp); } static GList * -rename_uri(RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp) +rename_uri (RuleContext *rc, const char *olduri, const char *newuri, GCompareFunc cmp) { return NULL; } GList * -rule_context_rename_uri(RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp) +rule_context_rename_uri (RuleContext *rc, const char *olduri, const char *newuri, GCompareFunc cmp) { - return ((RuleContextClass *) ((GtkObject *) f)->klass)->rename_uri (f, olduri, newuri, cmp); + return RULE_CONTEXT_GET_CLASS (rc)->rename_uri (rc, olduri, newuri, cmp); } void -rule_context_free_uri_list(RuleContext *f, GList *uris) +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); + g_free (l->data); + g_list_free_1 (l); l = n; } } - - diff --git a/filter/rule-context.h b/filter/rule-context.h index 60d0d6f932..62e6f4ac19 100644 --- a/filter/rule-context.h +++ b/filter/rule-context.h @@ -1,7 +1,9 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ /* - * Copyright (C) 2000 Ximian Inc. + * Copyright (C) 2000-2002 Ximian Inc. * * Authors: Not Zed <notzed@lostzed.mmc.com.au> + * Jeffrey Stedfast <fejj@ximian.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -21,60 +23,64 @@ #ifndef _RULE_CONTEXT_H #define _RULE_CONTEXT_H -#include <gtk/gtkobject.h> +#include <glib.h> +#include <glib-object.h> #include <gnome-xml/parser.h> #include "filter-part.h" #include "filter-rule.h" -#define RULE_CONTEXT(obj) GTK_CHECK_CAST (obj, rule_context_get_type (), RuleContext) -#define RULE_CONTEXT_CLASS(klass) GTK_CHECK_CLASS_CAST (klass, rule_context_get_type (), RuleContextClass) -#define IS_RULE_CONTEXT(obj) GTK_CHECK_TYPE (obj, rule_context_get_type ()) +#define RULE_TYPE_CONTEXT (rule_context_get_type ()) +#define RULE_CONTEXT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RULE_TYPE_CONTEXT, RuleContext)) +#define RULE_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RULE_TYPE_CONTEXT, RuleContextClass)) +#define IS_RULE_CONTEXT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RULE_TYPE_CONTEXT)) +#define IS_RULE_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RULE_TYPE_CONTEXT)) +#define RULE_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RULE_TYPE_CONTEXT, RuleContextClass)) -typedef struct _RuleContext RuleContext; -typedef struct _RuleContextClass RuleContextClass; +typedef struct _RuleContext RuleContext; +typedef struct _RuleContextClass RuleContextClass; struct _RuleContext { - GtkObject parent; + GObject parent_object; struct _RuleContextPrivate *priv; - - char *error; /* string version of error */ - - xmlDocPtr system; /* system rules source */ - xmlDocPtr user; /* user defined rules source */ - + + char *error; /* string version of error */ + + xmlDocPtr system; /* system rules source */ + xmlDocPtr user; /* user defined rules source */ + GList *parts; GList *rules; - - GHashTable *part_set_map;/* map set types to part types */ + + GHashTable *part_set_map; /* map set types to part types */ GList *part_set_list; - GHashTable *rule_set_map;/* map set types to rule types */ + GHashTable *rule_set_map; /* map set types to rule types */ GList *rule_set_list; }; -typedef void (*RCRegisterFunc)(RuleContext *f, FilterRule *rule, gpointer data); +typedef void (*RCRegisterFunc) (RuleContext *f, FilterRule *rule, gpointer user_data); struct _RuleContextClass { - GtkObjectClass parent_class; - + GObjectClass parent_class; + /* virtual methods */ - int (*load)(RuleContext *f, const char *system, const char *user); - int (*save)(RuleContext *f, const char *user); - int (*revert)(RuleContext *f, const char *user); - - GList *(*delete_uri)(RuleContext *f, const char *uri, GCompareFunc cmp); - GList *(*rename_uri)(RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp); - + int (*load) (RuleContext *f, const char *system, const char *user); + int (*save) (RuleContext *f, const char *user); + int (*revert) (RuleContext *f, const char *user); + + GList *(*delete_uri) (RuleContext *f, const char *uri, GCompareFunc cmp); + GList *(*rename_uri) (RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp); + /* signals */ - void (*rule_added)(RuleContext *f, FilterRule *rule); - void (*rule_removed)(RuleContext *f, FilterRule *rule); - void (*changed)(RuleContext *f); + void (*rule_added) (RuleContext *f, FilterRule *rule); + void (*rule_removed) (RuleContext *f, FilterRule *rule); + void (*changed) (RuleContext *f); }; -typedef void (*RCPartFunc)(RuleContext *f, FilterPart *part); -typedef void (*RCRuleFunc)(RuleContext *f, FilterRule *part); -typedef FilterPart * (*RCNextPartFunc)(RuleContext *f, FilterPart *part); -typedef FilterRule * (*RCNextRuleFunc)(RuleContext *f, FilterRule *rule, const char *source); +typedef void (*RCPartFunc) (RuleContext *f, FilterPart *part); +typedef void (*RCRuleFunc) (RuleContext *f, FilterRule *part); +typedef FilterPart * (*RCNextPartFunc) (RuleContext *f, FilterPart *part); +typedef FilterRule * (*RCNextRuleFunc) (RuleContext *f, FilterRule *rule, const char *source); struct _part_set_map { char *name; @@ -90,39 +96,40 @@ struct _rule_set_map { RCNextRuleFunc next; }; -guint rule_context_get_type (void); -RuleContext *rule_context_new (void); +GType rule_context_get_type (void); +RuleContext *rule_context_new (void); /* methods */ -int rule_context_load(RuleContext *f, const char *system, const char *user); -int rule_context_save(RuleContext *f, const char *user); -int rule_context_revert(RuleContext *f, const char *user); - -void rule_context_add_part(RuleContext *f, FilterPart *new); -FilterPart *rule_context_find_part(RuleContext *f, const char *name); -FilterPart *rule_context_create_part(RuleContext *f, const char *name); -FilterPart *rule_context_next_part(RuleContext *f, FilterPart *last); - -FilterRule *rule_context_next_rule(RuleContext *f, FilterRule *last, const char *source); -FilterRule *rule_context_find_rule(RuleContext *f, const char *name, const char *source); -FilterRule *rule_context_find_rank_rule(RuleContext *f, int rank, const char *source); -void rule_context_add_rule(RuleContext *f, FilterRule *new); -void rule_context_add_rule_gui(RuleContext *f, FilterRule *rule, const char *title, const char *path); -void rule_context_remove_rule(RuleContext *f, FilterRule *rule); +int rule_context_load (RuleContext *f, const char *system, const char *user); +int rule_context_save (RuleContext *f, const char *user); +int rule_context_revert (RuleContext *f, const char *user); + +void rule_context_add_part (RuleContext *f, FilterPart *new); +FilterPart *rule_context_find_part (RuleContext *f, const char *name); +FilterPart *rule_context_create_part (RuleContext *f, const char *name); +FilterPart *rule_context_next_part (RuleContext *f, FilterPart *last); + +FilterRule *rule_context_next_rule (RuleContext *f, FilterRule *last, const char *source); +FilterRule *rule_context_find_rule (RuleContext *f, const char *name, const char *source); +FilterRule *rule_context_find_rank_rule (RuleContext *f, int rank, const char *source); +void rule_context_add_rule (RuleContext *f, FilterRule *new); +void rule_context_add_rule_gui (RuleContext *f, FilterRule *rule, const char *title, const char *path); +void rule_context_remove_rule (RuleContext *f, FilterRule *rule); /* get/set the rank (position) of a rule */ -void rule_context_rank_rule(RuleContext *f, FilterRule *rule, int rank); -int rule_context_get_rank_rule(RuleContext *f, FilterRule *rule, const char *source); +void rule_context_rank_rule (RuleContext *f, FilterRule *rule, int rank); +int rule_context_get_rank_rule (RuleContext *f, FilterRule *rule, const char *source); /* setup type for set parts */ -void rule_context_add_part_set(RuleContext *f, const char *setname, int part_type, RCPartFunc append, RCNextPartFunc next); -void rule_context_add_rule_set(RuleContext *f, const char *setname, int rule_type, RCRuleFunc append, RCNextRuleFunc next); +void rule_context_add_part_set (RuleContext *f, const char *setname, int part_type, + RCPartFunc append, RCNextPartFunc next); +void rule_context_add_rule_set (RuleContext *f, const char *setname, int rule_type, + RCRuleFunc append, RCNextRuleFunc next); /* uri's disappear/renamed externally */ -GList *rule_context_delete_uri(RuleContext *f, const char *uri, GCompareFunc cmp); -GList *rule_context_rename_uri(RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp); +GList *rule_context_delete_uri (RuleContext *f, const char *uri, GCompareFunc cmp); +GList *rule_context_rename_uri (RuleContext *f, const char *olduri, const char *newuri, GCompareFunc cmp); -void rule_context_free_uri_list(RuleContext *f, GList *uris); +void rule_context_free_uri_list (RuleContext *f, GList *uris); #endif /* ! _RULE_CONTEXT_H */ - |