evolution-3.6.4/mail/em-filter-context.c

No issues found

  1 /*
  2  * This program is free software; you can redistribute it and/or
  3  * modify it under the terms of the GNU Lesser General Public
  4  * License as published by the Free Software Foundation; either
  5  * version 2 of the License, or (at your option) version 3.
  6  *
  7  * This program is distributed in the hope that it will be useful,
  8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 10  * Lesser General Public License for more details.
 11  *
 12  * You should have received a copy of the GNU Lesser General Public
 13  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 14  *
 15  *
 16  * Authors:
 17  *		Not Zed <notzed@lostzed.mmc.com.au>
 18  *      Jeffrey Stedfast <fejj@ximian.com>
 19  *
 20  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 21  *
 22  */
 23 
 24 #ifdef HAVE_CONFIG_H
 25 #include <config.h>
 26 #endif
 27 
 28 #include <string.h>
 29 
 30 #include "em-filter-context.h"
 31 #include "em-filter-rule.h"
 32 #include "filter/e-filter-option.h"
 33 #include "filter/e-filter-int.h"
 34 #include "em-filter-source-element.h"
 35 
 36 /* For poking into filter-folder guts */
 37 #include "em-filter-editor-folder-element.h"
 38 
 39 #define EM_FILTER_CONTEXT_GET_PRIVATE(obj) \
 40 	(G_TYPE_INSTANCE_GET_PRIVATE \
 41 	((obj), EM_TYPE_FILTER_CONTEXT, EMFilterContextPrivate))
 42 
 43 struct _EMFilterContextPrivate {
 44 	EMailSession *session;
 45 	GList *actions;
 46 };
 47 
 48 enum {
 49 	PROP_0,
 50 	PROP_SESSION
 51 };
 52 
 53 G_DEFINE_TYPE (
 54 	EMFilterContext,
 55 	em_filter_context,
 56 	E_TYPE_RULE_CONTEXT)
 57 
 58 static void
 59 filter_context_set_session (EMFilterContext *context,
 60                             EMailSession *session)
 61 {
 62 	g_return_if_fail (E_IS_MAIL_SESSION (session));
 63 	g_return_if_fail (context->priv->session == NULL);
 64 
 65 	context->priv->session = g_object_ref (session);
 66 }
 67 
 68 static void
 69 filter_context_set_property (GObject *object,
 70                              guint property_id,
 71                              const GValue *value,
 72                              GParamSpec *pspec)
 73 {
 74 	switch (property_id) {
 75 		case PROP_SESSION:
 76 			filter_context_set_session (
 77 				EM_FILTER_CONTEXT (object),
 78 				g_value_get_object (value));
 79 			return;
 80 	}
 81 
 82 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 83 }
 84 
 85 static void
 86 filter_context_get_property (GObject *object,
 87                              guint property_id,
 88                              GValue *value,
 89                              GParamSpec *pspec)
 90 {
 91 	switch (property_id) {
 92 		case PROP_SESSION:
 93 			g_value_set_object (
 94 				value,
 95 				em_filter_context_get_session (
 96 				EM_FILTER_CONTEXT (object)));
 97 			return;
 98 	}
 99 
100 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
101 }
102 
103 static void
104 filter_context_dispose (GObject *object)
105 {
106 	EMFilterContextPrivate *priv;
107 
108 	priv = EM_FILTER_CONTEXT_GET_PRIVATE (object);
109 
110 	if (priv->session != NULL) {
111 		g_object_unref (priv->session);
112 		priv->session = NULL;
113 	}
114 
115 	g_list_foreach (priv->actions, (GFunc) g_object_unref, NULL);
116 	g_list_free (priv->actions);
117 
118 	/* Chain up to parent's dispose() method. */
119 	G_OBJECT_CLASS (em_filter_context_parent_class)->dispose (object);
120 }
121 
122 /* We search for any folders in our actions list that need updating
123  * and update them. */
124 static GList *
125 filter_context_rename_uri (ERuleContext *context,
126                            const gchar *olduri,
127                            const gchar *newuri,
128                            GCompareFunc cmp)
129 {
130 	EFilterRule *rule;
131 	GList *l, *el;
132 	EFilterPart *action;
133 	EFilterElement *element;
134 	gint count = 0;
135 	GList *changed = NULL;
136 
137 	/* For all rules, for all actions, for all elements, rename any
138 	 * folder elements.  XXX Yes we could do this inside each part
139 	 * itself, but not today. */
140 	rule = NULL;
141 	while ((rule = e_rule_context_next_rule (context, rule, NULL))) {
142 		gint rulecount = 0;
143 
144 		l = EM_FILTER_RULE (rule)->actions;
145 		while (l) {
146 			action = l->data;
147 
148 			el = action->elements;
149 			while (el) {
150 				element = el->data;
151 
152 				if (EM_IS_FILTER_FOLDER_ELEMENT (element)
153 				    && cmp (em_filter_folder_element_get_uri (
154 				    EM_FILTER_FOLDER_ELEMENT (element)), olduri)) {
155 					em_filter_folder_element_set_uri (
156 						EM_FILTER_FOLDER_ELEMENT (
157 						element), newuri);
158 					rulecount++;
159 				}
160 				el = el->next;
161 			}
162 			l = l->next;
163 		}
164 
165 		if (rulecount) {
166 			changed = g_list_append (changed, g_strdup (rule->name));
167 			e_filter_rule_emit_changed (rule);
168 		}
169 
170 		count += rulecount;
171 	}
172 
173 	return changed;
174 }
175 
176 static GList *
177 filter_context_delete_uri (ERuleContext *context,
178                            const gchar *uri,
179                            GCompareFunc cmp)
180 {
181 	/* We basically do similar to above, but when we find it,
182 	 * remove the action, and if thats the last action, this
183 	 * might create an empty rule?  Remove the rule? */
184 
185 	EFilterRule *rule;
186 	GList *l, *el;
187 	EFilterPart *action;
188 	EFilterElement *element;
189 	gint count = 0;
190 	GList *deleted = NULL;
191 
192 	/* For all rules, for all actions, for all elements, check
193 	 * deleted folder elements. XXX  Yes we could do this inside
194 	 * each part itself, but not today. */
195 	rule = NULL;
196 	while ((rule = e_rule_context_next_rule (context, rule, NULL))) {
197 		gint recorded = 0;
198 
199 		l = EM_FILTER_RULE (rule)->actions;
200 		while (l) {
201 			action = l->data;
202 
203 			el = action->elements;
204 			while (el) {
205 				element = el->data;
206 
207 				if (EM_IS_FILTER_FOLDER_ELEMENT (element)
208 				    && cmp (em_filter_folder_element_get_uri (
209 				    EM_FILTER_FOLDER_ELEMENT (element)), uri)) {
210 					/* check if last action, if so, remove rule instead? */
211 					l = l->next;
212 					em_filter_rule_remove_action ((EMFilterRule *) rule, action);
213 					g_object_unref (action);
214 					count++;
215 					if (!recorded)
216 						deleted = g_list_append (deleted, g_strdup (rule->name));
217 					goto next_action;
218 				}
219 				el = el->next;
220 			}
221 			l = l->next;
222 		next_action:
223 			;
224 		}
225 	}
226 
227 	return deleted;
228 }
229 
230 static EFilterElement *
231 filter_context_new_element (ERuleContext *context,
232                             const gchar *type)
233 {
234 	EMFilterContextPrivate *priv;
235 
236 	priv = EM_FILTER_CONTEXT_GET_PRIVATE (context);
237 
238 	if (strcmp (type, "folder") == 0)
239 		return em_filter_editor_folder_element_new (priv->session);
240 
241 	if (strcmp (type, "system-flag") == 0)
242 		return e_filter_option_new ();
243 
244 	if (strcmp (type, "score") == 0)
245 		return e_filter_int_new_type ("score", -3, 3);
246 
247 	if (strcmp (type, "source") == 0)
248 		return em_filter_source_element_new (priv->session);
249 
250 	return E_RULE_CONTEXT_CLASS (em_filter_context_parent_class)->
251 		new_element (context, type);
252 }
253 
254 static void
255 em_filter_context_class_init (EMFilterContextClass *class)
256 {
257 	GObjectClass *object_class;
258 	ERuleContextClass *rule_context_class;
259 
260 	g_type_class_add_private (class, sizeof (EMFilterContextPrivate));
261 
262 	object_class = G_OBJECT_CLASS (class);
263 	object_class->set_property = filter_context_set_property;
264 	object_class->get_property = filter_context_get_property;
265 	object_class->dispose = filter_context_dispose;
266 
267 	rule_context_class = E_RULE_CONTEXT_CLASS (class);
268 	rule_context_class->rename_uri = filter_context_rename_uri;
269 	rule_context_class->delete_uri = filter_context_delete_uri;
270 	rule_context_class->new_element = filter_context_new_element;
271 
272 	g_object_class_install_property (
273 		object_class,
274 		PROP_SESSION,
275 		g_param_spec_object (
276 			"session",
277 			NULL,
278 			NULL,
279 			E_TYPE_MAIL_SESSION,
280 			G_PARAM_READWRITE |
281 			G_PARAM_CONSTRUCT_ONLY));
282 }
283 
284 static void
285 em_filter_context_init (EMFilterContext *context)
286 {
287 	context->priv = EM_FILTER_CONTEXT_GET_PRIVATE (context);
288 
289 	e_rule_context_add_part_set (
290 		E_RULE_CONTEXT (context),
291 		"partset", E_TYPE_FILTER_PART,
292 		(ERuleContextPartFunc) e_rule_context_add_part,
293 		(ERuleContextNextPartFunc) e_rule_context_next_part);
294 
295 	e_rule_context_add_part_set (
296 		E_RULE_CONTEXT (context),
297 		"actionset", E_TYPE_FILTER_PART,
298 		(ERuleContextPartFunc) em_filter_context_add_action,
299 		(ERuleContextNextPartFunc) em_filter_context_next_action);
300 
301 	e_rule_context_add_rule_set (
302 		E_RULE_CONTEXT (context),
303 		"ruleset", EM_TYPE_FILTER_RULE,
304 		(ERuleContextRuleFunc) e_rule_context_add_rule,
305 		(ERuleContextNextRuleFunc) e_rule_context_next_rule);
306 }
307 
308 EMFilterContext *
309 em_filter_context_new (EMailSession *session)
310 {
311 	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
312 
313 	return g_object_new (
314 		EM_TYPE_FILTER_CONTEXT, "session", session, NULL);
315 }
316 
317 EMailSession *
318 em_filter_context_get_session (EMFilterContext *context)
319 {
320 	g_return_val_if_fail (EM_IS_FILTER_CONTEXT (context), NULL);
321 
322 	return context->priv->session;
323 }
324 
325 void
326 em_filter_context_add_action (EMFilterContext *context,
327                               EFilterPart *action)
328 {
329 	context->priv->actions =
330 		g_list_append (context->priv->actions, action);
331 }
332 
333 EFilterPart *
334 em_filter_context_find_action (EMFilterContext *context,
335                                const gchar *name)
336 {
337 	return e_filter_part_find_list (context->priv->actions, name);
338 }
339 
340 EFilterPart *
341 em_filter_context_create_action (EMFilterContext *context,
342                                  const gchar *name)
343 {
344 	EFilterPart *part;
345 
346 	if ((part = em_filter_context_find_action (context, name)))
347 		return e_filter_part_clone (part);
348 
349 	return NULL;
350 }
351 
352 EFilterPart *
353 em_filter_context_next_action (EMFilterContext *context,
354                                EFilterPart *last)
355 {
356 	return e_filter_part_next_list (context->priv->actions, last);
357 }