diff options
author | Srinivasa Ragavan <sragavan@gnome.org> | 2012-03-02 01:04:32 +0800 |
---|---|---|
committer | Matthew Barnes <mbarnes@redhat.com> | 2012-03-03 22:02:34 +0800 |
commit | e758de5b1d7f0e257ff034caa9d73db64607a800 (patch) | |
tree | a599c6a8898897852a4be59b8a9b34b8203d80de /libemail-utils | |
parent | 147687c60f59d6b8a9d484750119102329b6be04 (diff) | |
download | gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.tar gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.tar.gz gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.tar.bz2 gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.tar.lz gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.tar.xz gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.tar.zst gsoc2013-evolution-e758de5b1d7f0e257ff034caa9d73db64607a800.zip |
Redo Filters/Search folder editors to use a shared non ui based library for
making search folders running remotely.
Diffstat (limited to 'libemail-utils')
-rw-r--r-- | libemail-utils/Makefile.am | 8 | ||||
-rw-r--r-- | libemail-utils/em-filter-folder-element.c | 241 | ||||
-rw-r--r-- | libemail-utils/em-filter-folder-element.h | 74 | ||||
-rw-r--r-- | libemail-utils/em-vfolder-context.c | 131 | ||||
-rw-r--r-- | libemail-utils/em-vfolder-context.h | 70 | ||||
-rw-r--r-- | libemail-utils/em-vfolder-rule.c | 380 | ||||
-rw-r--r-- | libemail-utils/em-vfolder-rule.h | 89 |
7 files changed, 993 insertions, 0 deletions
diff --git a/libemail-utils/Makefile.am b/libemail-utils/Makefile.am index 70e6a33b57..9b574310b2 100644 --- a/libemail-utils/Makefile.am +++ b/libemail-utils/Makefile.am @@ -17,6 +17,9 @@ libmailutilsinclude_HEADERS = \ e-signature-list.h \ e-signature-utils.h \ e-signature.h \ + em-filter-folder-element.h \ + em-vfolder-context.h \ + em-vfolder-rule.h \ mail-mt.h \ $(NULL) @@ -26,12 +29,17 @@ libemail_utils_la_SOURCES = \ e-signature-list.c \ e-signature-utils.c \ e-signature.c \ + em-filter-folder-element.c \ + em-vfolder-context.c \ + em-vfolder-rule.c \ mail-mt.c \ $(NULL) libemail_utils_la_LDFLAGS = -avoid-version $(NO_UNDEFINED) libemail_utils_la_LIBADD = \ + $(top_builddir)/filter/libfilter.la \ + $(top_builddir)/libevolution-utils/libevolution-utils.la \ $(EVOLUTION_DATA_SERVER_LIBS) \ $(GNOME_PLATFORM_LIBS) \ $(NULL) diff --git a/libemail-utils/em-filter-folder-element.c b/libemail-utils/em-filter-folder-element.c new file mode 100644 index 0000000000..1e286e3e43 --- /dev/null +++ b/libemail-utils/em-filter-folder-element.c @@ -0,0 +1,241 @@ +/* + * + * 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 <string.h> + +#include <gtk/gtk.h> +#include <glib/gi18n.h> + +#include "em-filter-folder-element.h" +#include "filter/e-filter-part.h" +#include "libevolution-utils/e-alert.h" + +#define EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementPrivate)) + +struct _EMFilterFolderElementPrivate { + gchar *uri; +}; + +G_DEFINE_TYPE ( + EMFilterFolderElement, + em_filter_folder_element, + E_TYPE_FILTER_ELEMENT) + + +static void +filter_folder_element_dispose (GObject *object) +{ + EMFilterFolderElementPrivate *priv; + + priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object); + + /* Chain up to parent's dispose() method. */ + G_OBJECT_CLASS (em_filter_folder_element_parent_class)->dispose (object); +} + +static void +filter_folder_element_finalize (GObject *object) +{ + EMFilterFolderElementPrivate *priv; + + priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object); + + g_free (priv->uri); + + /* Chain up to parent's finalize() method. */ + G_OBJECT_CLASS (em_filter_folder_element_parent_class)->finalize (object); +} + +static gboolean +filter_folder_element_validate (EFilterElement *fe, + EAlert **alert) +{ + EMFilterFolderElement *ff = (EMFilterFolderElement *) fe; + + g_warn_if_fail (alert == NULL || *alert == NULL); + + if (ff->priv->uri != NULL && *ff->priv->uri != '\0') + return TRUE; + + if (alert) + *alert = e_alert_new ("mail:no-folder", NULL); + + return FALSE; +} + +static gint +filter_folder_element_eq (EFilterElement *fe, + EFilterElement *cm) +{ + return E_FILTER_ELEMENT_CLASS ( + em_filter_folder_element_parent_class)->eq (fe, cm) && + strcmp (((EMFilterFolderElement *) fe)->priv->uri, + ((EMFilterFolderElement *) cm)->priv->uri)== 0; +} + +static xmlNodePtr +filter_folder_element_xml_encode (EFilterElement *fe) +{ + xmlNodePtr value, work; + EMFilterFolderElement *ff = (EMFilterFolderElement *) fe; + + value = xmlNewNode (NULL, (xmlChar *) "value"); + xmlSetProp (value, (xmlChar *) "name", (xmlChar *) fe->name); + xmlSetProp (value, (xmlChar *) "type", (xmlChar *) "folder"); + + work = xmlNewChild (value, NULL, (xmlChar *) "folder", NULL); + xmlSetProp (work, (xmlChar *) "uri", (xmlChar *) ff->priv->uri); + + return value; +} + +static gint +filter_folder_element_xml_decode (EFilterElement *fe, + xmlNodePtr node) +{ + EMFilterFolderElement *ff = (EMFilterFolderElement *) fe; + xmlNodePtr n; + + xmlFree (fe->name); + fe->name = (gchar *) xmlGetProp(node, (xmlChar *) "name"); + + n = node->children; + while (n) { + if (!strcmp((gchar *) n->name, "folder")) { + gchar *uri; + + uri = (gchar *) xmlGetProp(n, (xmlChar *) "uri"); + g_free (ff->priv->uri); + ff->priv->uri = g_strdup (uri); + xmlFree (uri); + break; + } + n = n->next; + } + + return 0; +} + +static GtkWidget * +filter_folder_element_get_widget (EFilterElement *fe) +{ + GtkWidget *widget; + + widget = E_FILTER_ELEMENT_CLASS (em_filter_folder_element_parent_class)-> + get_widget (fe); + + return widget; +} + +static void +filter_folder_element_build_code (EFilterElement *fe, + GString *out, + EFilterPart *ff) +{ + /* We are doing nothing on purpose. */ +} + +static void +filter_folder_element_format_sexp (EFilterElement *fe, + GString *out) +{ + EMFilterFolderElement *ff = (EMFilterFolderElement *) fe; + + camel_sexp_encode_string (out, ff->priv->uri); +} + +static void +filter_folder_element_copy_value (EFilterElement *de, + EFilterElement *se) +{ + if (EM_IS_FILTER_FOLDER_ELEMENT (se)) { + em_filter_folder_element_set_uri ( + EM_FILTER_FOLDER_ELEMENT (de), + EM_FILTER_FOLDER_ELEMENT (se)->priv->uri); + } else { + E_FILTER_ELEMENT_CLASS ( + em_filter_folder_element_parent_class)->copy_value (de, se); + } +} +static void +em_filter_folder_element_class_init (EMFilterFolderElementClass *class) +{ + GObjectClass *object_class; + EFilterElementClass *filter_element_class; + + g_type_class_add_private (class, sizeof (EMFilterFolderElementPrivate)); + + object_class = G_OBJECT_CLASS (class); + object_class->dispose = filter_folder_element_dispose; + object_class->finalize = filter_folder_element_finalize; + + filter_element_class = E_FILTER_ELEMENT_CLASS (class); + filter_element_class->validate = filter_folder_element_validate; + filter_element_class->eq = filter_folder_element_eq; + filter_element_class->xml_encode = filter_folder_element_xml_encode; + filter_element_class->xml_decode = filter_folder_element_xml_decode; + filter_element_class->get_widget = filter_folder_element_get_widget; + filter_element_class->build_code = filter_folder_element_build_code; + filter_element_class->format_sexp = filter_folder_element_format_sexp; + filter_element_class->copy_value = filter_folder_element_copy_value; +} + +static void +em_filter_folder_element_init (EMFilterFolderElement *element) +{ + element->priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (element); +} + +EFilterElement * +em_filter_folder_element_new () +{ + return g_object_new ( + EM_TYPE_FILTER_FOLDER_ELEMENT, + NULL); +} + +const gchar * +em_filter_folder_element_get_uri (EMFilterFolderElement *element) +{ + g_return_val_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element), NULL); + + return element->priv->uri; +} + +void +em_filter_folder_element_set_uri (EMFilterFolderElement *element, + const gchar *uri) +{ + g_return_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element)); + + g_free (element->priv->uri); + element->priv->uri = g_strdup (uri); +} + diff --git a/libemail-utils/em-filter-folder-element.h b/libemail-utils/em-filter-folder-element.h new file mode 100644 index 0000000000..24ed6aa01f --- /dev/null +++ b/libemail-utils/em-filter-folder-element.h @@ -0,0 +1,74 @@ +/* + * + * 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> + * Jeelementrey Stedfast <fejj@ximian.com> + * + * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com) + * + */ + +#ifndef EM_FILTER_FOLDER_ELEMENT_H +#define EM_FILTER_FOLDER_ELEMENT_H + +#include <filter/e-filter-element.h> + +/* Standard GObject macros */ +#define EM_TYPE_FILTER_FOLDER_ELEMENT \ + (em_filter_folder_element_get_type ()) +#define EM_FILTER_FOLDER_ELEMENT(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElement)) +#define EM_FILTER_FOLDER_ELEMENT_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_CAST \ + ((cls), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementClass)) +#define EM_IS_FILTER_FOLDER_ELEMENT(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE \ + ((obj), EM_TYPE_FILTER_FOLDER_ELEMENT)) +#define EM_IS_FILTER_FOLDER_ELEMENT_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_TYPE \ + ((cls), EM_TYPE_FILTER_FOLDER_ELEMENT)) +#define EM_FILTER_FOLDER_ELEMENT_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS \ + ((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementClass)) + +G_BEGIN_DECLS + +typedef struct _EMFilterFolderElement EMFilterFolderElement; +typedef struct _EMFilterFolderElementClass EMFilterFolderElementClass; +typedef struct _EMFilterFolderElementPrivate EMFilterFolderElementPrivate; + +struct _EMFilterFolderElement { + EFilterElement parent; + EMFilterFolderElementPrivate *priv; +}; + +struct _EMFilterFolderElementClass { + EFilterElementClass parent_class; +}; + +GType em_filter_folder_element_get_type (void); +EFilterElement *em_filter_folder_element_new (void); +const gchar * em_filter_folder_element_get_uri + (EMFilterFolderElement *element); +void em_filter_folder_element_set_uri + (EMFilterFolderElement *element, + const gchar *uri); + +G_END_DECLS + +#endif /* EM_FILTER_FOLDER_ELEMENT_H */ diff --git a/libemail-utils/em-vfolder-context.c b/libemail-utils/em-vfolder-context.c new file mode 100644 index 0000000000..647ded888c --- /dev/null +++ b/libemail-utils/em-vfolder-context.c @@ -0,0 +1,131 @@ +/* + * + * 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 <string.h> + +#include "em-vfolder-context.h" +#include "em-vfolder-rule.h" +#include "filter/e-filter-option.h" +#include "filter/e-filter-int.h" + +#include "em-filter-folder-element.h" + +#define EM_VFOLDER_CONTEXT_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContextPrivate)) + +struct _EMVFolderContextPrivate { + int foo; +}; + +enum { + PROP_0, + PROP_SESSION +}; + +G_DEFINE_TYPE ( + EMVFolderContext, + em_vfolder_context, + E_TYPE_RULE_CONTEXT) + +static void +vfolder_context_dispose (GObject *object) +{ + EMVFolderContextPrivate *priv; + + priv = EM_VFOLDER_CONTEXT_GET_PRIVATE (object); + + /* Chain up to parent's dispose() method. */ + G_OBJECT_CLASS (em_vfolder_context_parent_class)->dispose (object); +} + +static EFilterElement * +vfolder_context_new_element (ERuleContext *context, + const gchar *type) +{ + EMVFolderContextPrivate *priv; + + priv = EM_VFOLDER_CONTEXT_GET_PRIVATE (context); + + if (strcmp (type, "system-flag") == 0) + return e_filter_option_new (); + + if (strcmp (type, "score") == 0) + return e_filter_int_new_type("score", -3, 3); + + if (strcmp (type, "folder") == 0) + return em_filter_folder_element_new (); + + /* XXX Legacy type name. Same as "folder" now. */ + if (strcmp (type, "folder-curi") == 0) + return em_filter_folder_element_new (); + + return E_RULE_CONTEXT_CLASS (em_vfolder_context_parent_class)-> + new_element (context, type); +} + +static void +em_vfolder_context_class_init (EMVFolderContextClass *class) +{ + GObjectClass *object_class; + ERuleContextClass *rule_context_class; + + g_type_class_add_private (class, sizeof (EMVFolderContextPrivate)); + + object_class = G_OBJECT_CLASS (class); + object_class->dispose = vfolder_context_dispose; + + rule_context_class = E_RULE_CONTEXT_CLASS (class); + rule_context_class->new_element = vfolder_context_new_element; +} + +static void +em_vfolder_context_init (EMVFolderContext *context) +{ + context->priv = EM_VFOLDER_CONTEXT_GET_PRIVATE (context); + + e_rule_context_add_part_set ( + E_RULE_CONTEXT (context), "partset", E_TYPE_FILTER_PART, + (ERuleContextPartFunc) e_rule_context_add_part, + (ERuleContextNextPartFunc) e_rule_context_next_part); + + e_rule_context_add_rule_set ( + E_RULE_CONTEXT (context), "ruleset", EM_TYPE_VFOLDER_RULE, + (ERuleContextRuleFunc) e_rule_context_add_rule, + (ERuleContextNextRuleFunc) e_rule_context_next_rule); + + E_RULE_CONTEXT (context)->flags = + E_RULE_CONTEXT_THREADING | E_RULE_CONTEXT_GROUPING; +} + +EMVFolderContext * +em_vfolder_context_new () +{ + return g_object_new ( + EM_TYPE_VFOLDER_CONTEXT, NULL); +} diff --git a/libemail-utils/em-vfolder-context.h b/libemail-utils/em-vfolder-context.h new file mode 100644 index 0000000000..9d46f92ed1 --- /dev/null +++ b/libemail-utils/em-vfolder-context.h @@ -0,0 +1,70 @@ +/* + * + * 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) + * + */ + +#ifndef EM_VFOLDER_CONTEXT_H +#define EM_VFOLDER_CONTEXT_H + +#include <filter/e-rule-context.h> + +/* Standard GObject macros */ +#define EM_TYPE_VFOLDER_CONTEXT \ + (em_vfolder_context_get_type ()) +#define EM_VFOLDER_CONTEXT(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContext)) +#define EM_VFOLDER_CONTEXT_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_CAST \ + ((cls), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContextClass)) +#define EM_IS_VFOLDER_CONTEXT(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE \ + ((obj), EM_TYPE_VFOLDER_CONTEXT)) +#define EM_IS_VFOLDER_CONTEXT_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_TYPE \ + ((cls), EM_TYPE_VFOLDER_CONTEXT)) +#define EM_VFOLDER_CONTEXT_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS \ + ((obj), EM_TYPE_VFOLDER_CONTEXT, EMVFolderContextClass)) + +G_BEGIN_DECLS + +typedef struct _EMVFolderContext EMVFolderContext; +typedef struct _EMVFolderContextClass EMVFolderContextClass; +typedef struct _EMVFolderContextPrivate EMVFolderContextPrivate; + +struct _EMVFolderContext { + ERuleContext parent; + EMVFolderContextPrivate *priv; +}; + +struct _EMVFolderContextClass { + ERuleContextClass parent_class; +}; + +GType em_vfolder_context_get_type (void); +EMVFolderContext * + em_vfolder_context_new (void); + +G_END_DECLS + +#endif /* EM_VFOLDER_CONTEXT_H */ diff --git a/libemail-utils/em-vfolder-rule.c b/libemail-utils/em-vfolder-rule.c new file mode 100644 index 0000000000..9cf69544f1 --- /dev/null +++ b/libemail-utils/em-vfolder-rule.c @@ -0,0 +1,380 @@ +/* + * 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 <string.h> + +#include <gtk/gtk.h> +#include <glib/gi18n.h> + +#include <libevolution-utils/e-alert.h> + +#include <libemail-engine/e-mail-folder-utils.h> + +#include "em-vfolder-context.h" +#include "em-vfolder-rule.h" + +#define EM_VFOLDER_RULE_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRulePrivate)) + +#define EM_VFOLDER_RULE_GET_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE \ + ((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRulePrivate)) + +struct _EMVFolderRulePrivate { + int foo; +}; + +static gint validate (EFilterRule *, EAlert **alert); +static gint vfolder_eq (EFilterRule *fr, EFilterRule *cm); +static xmlNodePtr xml_encode (EFilterRule *); +static gint xml_decode (EFilterRule *, xmlNodePtr, ERuleContext *f); +static void rule_copy (EFilterRule *dest, EFilterRule *src); +static GtkWidget *get_widget (EFilterRule *fr, ERuleContext *f); + +/* DO NOT internationalise these strings */ +static const gchar *with_names[] = { + "specific", + "local_remote_active", + "remote_active", + "local" +}; + +G_DEFINE_TYPE ( + EMVFolderRule, + em_vfolder_rule, + E_TYPE_FILTER_RULE) + + +static void +vfolder_rule_dispose (GObject *object) +{ + EMVFolderRulePrivate *priv; + + priv = EM_VFOLDER_RULE_GET_PRIVATE (object); + + /* Chain up to parent's dispose() method. */ + G_OBJECT_CLASS (em_vfolder_rule_parent_class)->dispose (object); +} + +static void +vfolder_rule_finalize (GObject *object) +{ + EMVFolderRule *rule = EM_VFOLDER_RULE (object); + gchar *uri; + + while ((uri = g_queue_pop_head (&rule->sources)) != NULL) + g_free (uri); + + /* Chain up to parent's finalize() method. */ + G_OBJECT_CLASS (em_vfolder_rule_parent_class)->finalize (object); +} + +static void +em_vfolder_rule_class_init (EMVFolderRuleClass *class) +{ + GObjectClass *object_class; + EFilterRuleClass *filter_rule_class; + + g_type_class_add_private (class, sizeof (EMVFolderRulePrivate)); + + object_class = G_OBJECT_CLASS (class); + object_class->dispose = vfolder_rule_dispose; + object_class->finalize = vfolder_rule_finalize; + + filter_rule_class = E_FILTER_RULE_CLASS (class); + filter_rule_class->validate = validate; + filter_rule_class->eq = vfolder_eq; + filter_rule_class->xml_encode = xml_encode; + filter_rule_class->xml_decode = xml_decode; + filter_rule_class->copy = rule_copy; + filter_rule_class->get_widget = get_widget; +} + +static void +em_vfolder_rule_init (EMVFolderRule *rule) +{ + rule->priv = EM_VFOLDER_RULE_GET_PRIVATE (rule); + rule->with = EM_VFOLDER_RULE_WITH_SPECIFIC; + rule->rule.source = g_strdup ("incoming"); +} + +EFilterRule * +em_vfolder_rule_new () +{ + return g_object_new ( + EM_TYPE_VFOLDER_RULE, NULL); +} + +void +em_vfolder_rule_add_source (EMVFolderRule *rule, + const gchar *uri) +{ + g_return_if_fail (EM_IS_VFOLDER_RULE (rule)); + g_return_if_fail (uri); + + g_queue_push_tail (&rule->sources, g_strdup (uri)); + + e_filter_rule_emit_changed (E_FILTER_RULE (rule)); +} + +const gchar * +em_vfolder_rule_find_source (EMVFolderRule *rule, + const gchar *uri) +{ + GList *link; + + g_return_val_if_fail (EM_IS_VFOLDER_RULE (rule), NULL); + + /* only does a simple string or address comparison, should + * probably do a decoded url comparison */ + link = g_queue_find_custom ( + &rule->sources, uri, (GCompareFunc) strcmp); + + return (link != NULL) ? link->data : NULL; +} + +void +em_vfolder_rule_remove_source (EMVFolderRule *rule, + const gchar *uri) +{ + gchar *found; + + g_return_if_fail (EM_IS_VFOLDER_RULE (rule)); + + found =(gchar *) em_vfolder_rule_find_source (rule, uri); + if (found != NULL) { + g_queue_remove (&rule->sources, found); + g_free (found); + e_filter_rule_emit_changed (E_FILTER_RULE (rule)); + } +} + +const gchar * +em_vfolder_rule_next_source (EMVFolderRule *rule, + const gchar *last) +{ + GList *link; + + if (last == NULL) { + link = g_queue_peek_head_link (&rule->sources); + } else { + link = g_queue_find (&rule->sources, last); + if (link == NULL) + link = g_queue_peek_head_link (&rule->sources); + else + link = g_list_next (link); + } + + return (link != NULL) ? link->data : NULL; +} + +static gint +validate (EFilterRule *fr, + EAlert **alert) +{ + g_return_val_if_fail (fr != NULL, 0); + g_warn_if_fail (alert == NULL || *alert == NULL); + + if (!fr->name || !*fr->name) { + if (alert) + *alert = e_alert_new ("mail:no-name-vfolder", NULL); + return 0; + } + + /* We have to have at least one source set in the "specific" case. + * Do not translate this string! */ + if (((EMVFolderRule *) fr)->with == EM_VFOLDER_RULE_WITH_SPECIFIC && + g_queue_is_empty (&((EMVFolderRule *) fr)->sources)) { + if (alert) + *alert = e_alert_new ("mail:vfolder-no-source", NULL); + return 0; + } + + return E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->validate (fr, alert); +} + +static gint +queue_eq (GQueue *queue_a, + GQueue *queue_b) +{ + GList *link_a; + GList *link_b; + gint truth = TRUE; + + link_a = g_queue_peek_head_link (queue_a); + link_b = g_queue_peek_head_link (queue_b); + + while (truth && link_a != NULL && link_b != NULL) { + gchar *uri_a = link_a->data; + gchar *uri_b = link_b->data; + + truth = (strcmp (uri_a, uri_b)== 0); + + link_a = g_list_next (link_a); + link_b = g_list_next (link_b); + } + + return truth && link_a == NULL && link_b == NULL; +} + +static gint +vfolder_eq (EFilterRule *fr, + EFilterRule *cm) +{ + return E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->eq (fr, cm) + && queue_eq ( + &((EMVFolderRule *) fr)->sources, + &((EMVFolderRule *) cm)->sources); +} + +static xmlNodePtr +xml_encode (EFilterRule *fr) +{ + EMVFolderRule *vr =(EMVFolderRule *) fr; + xmlNodePtr node, set, work; + GList *head, *link; + + node = E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->xml_encode (fr); + g_return_val_if_fail (node != NULL, NULL); + g_return_val_if_fail (vr->with < G_N_ELEMENTS (with_names), NULL); + + set = xmlNewNode(NULL, (const guchar *)"sources"); + xmlAddChild (node, set); + xmlSetProp(set, (const guchar *)"with", (guchar *)with_names[vr->with]); + + head = g_queue_peek_head_link (&vr->sources); + for (link = head; link != NULL; link = g_list_next (link)) { + const gchar *uri = link->data; + + work = xmlNewNode (NULL, (const guchar *) "folder"); + xmlSetProp (work, (const guchar *) "uri", (guchar *) uri); + xmlAddChild (set, work); + } + + return node; +} + +static void +set_with (EMVFolderRule *vr, + const gchar *name) +{ + gint i; + + for (i = 0; i < G_N_ELEMENTS (with_names); i++) { + if (!strcmp (name, with_names[i])) { + vr->with = i; + return; + } + } + + vr->with = 0; +} + +static gint +xml_decode (EFilterRule *fr, + xmlNodePtr node, + ERuleContext *f) +{ + xmlNodePtr set, work; + gint result; + EMVFolderRule *vr =(EMVFolderRule *) fr; + gchar *tmp; + + result = E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)-> + xml_decode (fr, node, f); + if (result != 0) + return result; + + /* handle old format file, vfolder source is in filterrule */ + if (strcmp(fr->source, "incoming")!= 0) { + set_with (vr, fr->source); + g_free (fr->source); + fr->source = g_strdup("incoming"); + } + + set = node->children; + while (set) { + if (!strcmp((gchar *)set->name, "sources")) { + tmp = (gchar *)xmlGetProp(set, (const guchar *)"with"); + if (tmp) { + set_with (vr, tmp); + xmlFree (tmp); + } + work = set->children; + while (work) { + if (!strcmp((gchar *)work->name, "folder")) { + tmp = (gchar *)xmlGetProp(work, (const guchar *)"uri"); + if (tmp) { + g_queue_push_tail (&vr->sources, g_strdup (tmp)); + xmlFree (tmp); + } + } + work = work->next; + } + } + set = set->next; + } + return 0; +} + +static void +rule_copy (EFilterRule *dest, + EFilterRule *src) +{ + EMVFolderRule *vdest, *vsrc; + GList *head, *link; + gchar *uri; + + vdest =(EMVFolderRule *) dest; + vsrc =(EMVFolderRule *) src; + + while ((uri = g_queue_pop_head (&vdest->sources)) != NULL) + g_free (uri); + + head = g_queue_peek_head_link (&vsrc->sources); + for (link = head; link != NULL; link = g_list_next (link)) { + const gchar *uri = link->data; + g_queue_push_tail (&vdest->sources, g_strdup (uri)); + } + + vdest->with = vsrc->with; + + E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)->copy (dest, src); +} + +static GtkWidget * +get_widget (EFilterRule *fr, + ERuleContext *rc) +{ + GtkWidget *widget; + + widget = E_FILTER_RULE_CLASS (em_vfolder_rule_parent_class)-> + get_widget (fr, rc); + + return widget; +} diff --git a/libemail-utils/em-vfolder-rule.h b/libemail-utils/em-vfolder-rule.h new file mode 100644 index 0000000000..892aded2bc --- /dev/null +++ b/libemail-utils/em-vfolder-rule.h @@ -0,0 +1,89 @@ +/* + * 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: + * NotZed <notzed@ximian.com> + * Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com) + * + */ + +#ifndef EM_VFOLDER_RULE_H +#define EM_VFOLDER_RULE_H + +#include <filter/e-filter-rule.h> + +/* Standard GObject macros */ +#define EM_TYPE_VFOLDER_RULE \ + (em_vfolder_rule_get_type ()) +#define EM_VFOLDER_RULE(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST \ + ((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRule)) +#define EM_VFOLDER_RULE_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_CAST \ + ((cls), EM_TYPE_VFOLDER_RULE, EMVFolderRuleClass)) +#define EM_IS_VFOLDER_RULE(obj) \ + (G_TYPE_CHECK_INSTANCE_TYPE \ + ((obj), EM_TYPE_VFOLDER_RULE)) +#define EM_IS_VFOLDER_RULE_CLASS(cls) \ + (G_TYPE_CHECK_CLASS_TYPE \ + ((cls), EM_TYPE_VFOLDER_RULE)) +#define EM_VFOLDER_RULE_GET_CLASS(obj) \ + (G_TYPE_INSTANCE_GET_CLASS \ + ((obj), EM_TYPE_VFOLDER_RULE, EMVFolderRuleClass)) + +G_BEGIN_DECLS + +/* perhaps should be bits? */ +enum _em_vfolder_rule_with_t { + EM_VFOLDER_RULE_WITH_SPECIFIC, + EM_VFOLDER_RULE_WITH_LOCAL_REMOTE_ACTIVE, + EM_VFOLDER_RULE_WITH_REMOTE_ACTIVE, + EM_VFOLDER_RULE_WITH_LOCAL +}; + +typedef struct _EMVFolderRule EMVFolderRule; +typedef struct _EMVFolderRuleClass EMVFolderRuleClass; +typedef struct _EMVFolderRulePrivate EMVFolderRulePrivate; + +typedef enum _em_vfolder_rule_with_t em_vfolder_rule_with_t; + +struct _EMVFolderRule { + EFilterRule rule; + EMVFolderRulePrivate *priv; + + em_vfolder_rule_with_t with; + GQueue sources; /* uri's of the source folders */ +}; + +struct _EMVFolderRuleClass { + EFilterRuleClass parent_class; +}; + +GType em_vfolder_rule_get_type (void); +EFilterRule * em_vfolder_rule_new (); +void em_vfolder_rule_add_source (EMVFolderRule *rule, + const gchar *uri); +void em_vfolder_rule_remove_source (EMVFolderRule *rule, + const gchar *uri); +const gchar * em_vfolder_rule_find_source (EMVFolderRule *rule, + const gchar *uri); +const gchar * em_vfolder_rule_next_source (EMVFolderRule *rule, + const gchar *last); + +G_END_DECLS + +#endif /* EM_VFOLDER_RULE_H */ |