evolution-3.6.4/libemail-utils/em-filter-folder-element.c

No issues found

  1 /*
  2  *
  3  * This program is free software; you can redistribute it and/or
  4  * modify it under the terms of the GNU Lesser General Public
  5  * License as published by the Free Software Foundation; either
  6  * version 2 of the License, or (at your option) version 3.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 11  * Lesser General Public License for more details.
 12  *
 13  * You should have received a copy of the GNU Lesser General Public
 14  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 15  *
 16  *
 17  * Authors:
 18  *		Not Zed <notzed@lostzed.mmc.com.au>
 19  *      Jeffrey Stedfast <fejj@ximian.com>
 20  *
 21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 22  *
 23  */
 24 
 25 #ifdef HAVE_CONFIG_H
 26 #include <config.h>
 27 #endif
 28 
 29 #include <string.h>
 30 
 31 #include <gtk/gtk.h>
 32 #include <glib/gi18n.h>
 33 
 34 #include "em-filter-folder-element.h"
 35 #include "filter/e-filter-part.h"
 36 #include "libevolution-utils/e-alert.h"
 37 
 38 #define EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE(obj) \
 39 	(G_TYPE_INSTANCE_GET_PRIVATE \
 40 	((obj), EM_TYPE_FILTER_FOLDER_ELEMENT, EMFilterFolderElementPrivate))
 41 
 42 struct _EMFilterFolderElementPrivate {
 43 	gchar *uri;
 44 };
 45 
 46 G_DEFINE_TYPE (
 47 	EMFilterFolderElement,
 48 	em_filter_folder_element,
 49 	E_TYPE_FILTER_ELEMENT)
 50 
 51 static void
 52 filter_folder_element_finalize (GObject *object)
 53 {
 54 	EMFilterFolderElementPrivate *priv;
 55 
 56 	priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (object);
 57 
 58 	g_free (priv->uri);
 59 
 60 	/* Chain up to parent's finalize() method. */
 61 	G_OBJECT_CLASS (em_filter_folder_element_parent_class)->finalize (object);
 62 }
 63 
 64 static gboolean
 65 filter_folder_element_validate (EFilterElement *fe,
 66                                 EAlert **alert)
 67 {
 68 	EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;
 69 
 70 	g_warn_if_fail (alert == NULL || *alert == NULL);
 71 
 72 	if (ff->priv->uri != NULL && *ff->priv->uri != '\0')
 73 		return TRUE;
 74 
 75 	if (alert)
 76 		*alert = e_alert_new ("mail:no-folder", NULL);
 77 
 78 	return FALSE;
 79 }
 80 
 81 static gint
 82 filter_folder_element_eq (EFilterElement *fe,
 83                           EFilterElement *cm)
 84 {
 85 	return E_FILTER_ELEMENT_CLASS (
 86 		em_filter_folder_element_parent_class)->eq (fe, cm) &&
 87 		strcmp (((EMFilterFolderElement *) fe)->priv->uri,
 88 		((EMFilterFolderElement *) cm)->priv->uri)== 0;
 89 }
 90 
 91 static xmlNodePtr
 92 filter_folder_element_xml_encode (EFilterElement *fe)
 93 {
 94 	xmlNodePtr value, work;
 95 	EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;
 96 
 97 	value = xmlNewNode (NULL, (xmlChar *) "value");
 98 	xmlSetProp (value, (xmlChar *) "name", (xmlChar *) fe->name);
 99 	xmlSetProp (value, (xmlChar *) "type", (xmlChar *) "folder");
100 
101 	work = xmlNewChild (value, NULL, (xmlChar *) "folder", NULL);
102 	xmlSetProp (work, (xmlChar *) "uri", (xmlChar *) ff->priv->uri);
103 
104 	return value;
105 }
106 
107 static gint
108 filter_folder_element_xml_decode (EFilterElement *fe,
109                                   xmlNodePtr node)
110 {
111 	EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;
112 	xmlNodePtr n;
113 
114 	xmlFree (fe->name);
115 	fe->name = (gchar *) xmlGetProp (node, (xmlChar *) "name");
116 
117 	n = node->children;
118 	while (n) {
119 		if (!strcmp ((gchar *) n->name, "folder")) {
120 			gchar *uri;
121 
122 			uri = (gchar *) xmlGetProp (n, (xmlChar *) "uri");
123 			g_free (ff->priv->uri);
124 			ff->priv->uri = g_strdup (uri);
125 			xmlFree (uri);
126 			break;
127 		}
128 		n = n->next;
129 	}
130 
131 	return 0;
132 }
133 
134 static GtkWidget *
135 filter_folder_element_get_widget (EFilterElement *fe)
136 {
137 	GtkWidget *widget;
138 
139 	widget = E_FILTER_ELEMENT_CLASS (em_filter_folder_element_parent_class)->
140 		get_widget (fe);
141 
142 	return widget;
143 }
144 
145 static void
146 filter_folder_element_build_code (EFilterElement *fe,
147                                   GString *out,
148                                   EFilterPart *ff)
149 {
150 	/* We are doing nothing on purpose. */
151 }
152 
153 static void
154 filter_folder_element_format_sexp (EFilterElement *fe,
155                                    GString *out)
156 {
157 	EMFilterFolderElement *ff = (EMFilterFolderElement *) fe;
158 
159 	camel_sexp_encode_string (out, ff->priv->uri);
160 }
161 
162 static void
163 filter_folder_element_copy_value (EFilterElement *de,
164                                   EFilterElement *se)
165 {
166 	if (EM_IS_FILTER_FOLDER_ELEMENT (se)) {
167 		em_filter_folder_element_set_uri (
168 			EM_FILTER_FOLDER_ELEMENT (de),
169 			EM_FILTER_FOLDER_ELEMENT (se)->priv->uri);
170 	} else {
171 		E_FILTER_ELEMENT_CLASS (
172 		em_filter_folder_element_parent_class)->copy_value (de, se);
173 	}
174 }
175 static void
176 em_filter_folder_element_class_init (EMFilterFolderElementClass *class)
177 {
178 	GObjectClass *object_class;
179 	EFilterElementClass *filter_element_class;
180 
181 	g_type_class_add_private (class, sizeof (EMFilterFolderElementPrivate));
182 
183 	object_class = G_OBJECT_CLASS (class);
184 	object_class->finalize = filter_folder_element_finalize;
185 
186 	filter_element_class = E_FILTER_ELEMENT_CLASS (class);
187 	filter_element_class->validate = filter_folder_element_validate;
188 	filter_element_class->eq = filter_folder_element_eq;
189 	filter_element_class->xml_encode = filter_folder_element_xml_encode;
190 	filter_element_class->xml_decode = filter_folder_element_xml_decode;
191 	filter_element_class->get_widget = filter_folder_element_get_widget;
192 	filter_element_class->build_code = filter_folder_element_build_code;
193 	filter_element_class->format_sexp = filter_folder_element_format_sexp;
194 	filter_element_class->copy_value = filter_folder_element_copy_value;
195 }
196 
197 static void
198 em_filter_folder_element_init (EMFilterFolderElement *element)
199 {
200 	element->priv = EM_FILTER_FOLDER_ELEMENT_GET_PRIVATE (element);
201 }
202 
203 EFilterElement *
204 em_filter_folder_element_new ()
205 {
206 	return g_object_new (
207 		EM_TYPE_FILTER_FOLDER_ELEMENT,
208 		NULL);
209 }
210 
211 const gchar *
212 em_filter_folder_element_get_uri (EMFilterFolderElement *element)
213 {
214 	g_return_val_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element), NULL);
215 
216 	return element->priv->uri;
217 }
218 
219 void
220 em_filter_folder_element_set_uri (EMFilterFolderElement *element,
221                                   const gchar *uri)
222 {
223 	g_return_if_fail (EM_IS_FILTER_FOLDER_ELEMENT (element));
224 
225 	g_free (element->priv->uri);
226 	element->priv->uri = g_strdup (uri);
227 }