evolution-3.6.4/filter/e-filter-option.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 <gtk/gtk.h>
 31 #include <glib/gi18n.h>
 32 #include <gmodule.h>
 33 
 34 #include "e-filter-option.h"
 35 #include "e-filter-part.h"
 36 
 37 G_DEFINE_TYPE (
 38 	EFilterOption,
 39 	e_filter_option,
 40 	E_TYPE_FILTER_ELEMENT)
 41 
 42 static void
 43 free_option (struct _filter_option *opt)
 44 {
 45 	g_free (opt->title);
 46 	g_free (opt->value);
 47 	g_free (opt->code);
 48 	g_free (opt->code_gen_func);
 49 	g_free (opt);
 50 }
 51 
 52 static struct _filter_option *
 53 find_option (EFilterOption *option,
 54              const gchar *name)
 55 {
 56 	GList *link;
 57 
 58 	for (link = option->options; link != NULL; link = g_list_next (link)) {
 59 		struct _filter_option *opt = link->data;
 60 
 61 		if (strcmp (name, opt->value) == 0)
 62 			return opt;
 63 	}
 64 
 65 	return NULL;
 66 }
 67 
 68 static void
 69 filter_option_combobox_changed (GtkComboBox *combo_box,
 70                                 EFilterElement *element)
 71 {
 72 	EFilterOption *option = E_FILTER_OPTION (element);
 73 	gint active;
 74 
 75 	active = gtk_combo_box_get_active (combo_box);
 76 	option->current = g_list_nth_data (option->options, active);
 77 }
 78 
 79 static GSList *
 80 filter_option_get_dynamic_options (EFilterOption *option)
 81 {
 82 	GModule *module;
 83 	GSList *(*get_func)(void);
 84 	GSList *res = NULL;
 85 
 86 	if (!option || !option->dynamic_func)
 87 		return res;
 88 
 89 	module = g_module_open (NULL, G_MODULE_BIND_LAZY);
 90 
 91 	if (g_module_symbol (module, option->dynamic_func, (gpointer) &get_func)) {
 92 		res = get_func ();
 93 	} else {
 94 		g_warning (
 95 			"optionlist dynamic fill function '%s' not found",
 96 			option->dynamic_func);
 97 	}
 98 
 99 	g_module_close (module);
100 
101 	return res;
102 }
103 
104 static void
105 filter_option_generate_code (EFilterOption *option,
106                              GString *out,
107                              EFilterPart *part)
108 {
109 	GModule *module;
110 	void (*code_gen_func) (EFilterElement *element, GString *out, EFilterPart *part);
111 
112 	if (!option || !option->current || !option->current->code_gen_func)
113 		return;
114 
115 	module = g_module_open (NULL, G_MODULE_BIND_LAZY);
116 
117 	if (g_module_symbol (module, option->current->code_gen_func, (gpointer) &code_gen_func)) {
118 		code_gen_func (E_FILTER_ELEMENT (option), out, part);
119 	} else {
120 		g_warning (
121 			"optionlist dynamic code function '%s' not found",
122 			option->current->code_gen_func);
123 	}
124 
125 	g_module_close (module);
126 }
127 
128 static void
129 filter_option_finalize (GObject *object)
130 {
131 	EFilterOption *option = E_FILTER_OPTION (object);
132 
133 	g_list_foreach (option->options, (GFunc) free_option, NULL);
134 	g_list_free (option->options);
135 
136 	g_free (option->dynamic_func);
137 
138 	/* Chain up to parent's finalize() method. */
139 	G_OBJECT_CLASS (e_filter_option_parent_class)->finalize (object);
140 }
141 
142 static gint
143 filter_option_eq (EFilterElement *element_a,
144                   EFilterElement *element_b)
145 {
146 	EFilterOption *option_a = E_FILTER_OPTION (element_a);
147 	EFilterOption *option_b = E_FILTER_OPTION (element_b);
148 
149 	/* Chain up to parent's eq() method. */
150 	if (!E_FILTER_ELEMENT_CLASS (e_filter_option_parent_class)->
151 		eq (element_a, element_b))
152 		return FALSE;
153 
154 	if (option_a->current == NULL && option_b->current == NULL)
155 		return TRUE;
156 
157 	if (option_a->current == NULL || option_b->current == NULL)
158 		return FALSE;
159 
160 	return (g_strcmp0 (option_a->current->value, option_b->current->value) == 0);
161 }
162 
163 static void
164 filter_option_xml_create (EFilterElement *element,
165                           xmlNodePtr node)
166 {
167 	EFilterOption *option = E_FILTER_OPTION (element);
168 	xmlNodePtr n, work;
169 
170 	/* Chain up to parent's xml_create() method. */
171 	E_FILTER_ELEMENT_CLASS (e_filter_option_parent_class)->
172 		xml_create (element, node);
173 
174 	n = node->children;
175 	while (n) {
176 		if (!strcmp ((gchar *) n->name, "option")) {
177 			gchar *tmp, *value, *title = NULL, *code = NULL, *code_gen_func = NULL;
178 
179 			value = (gchar *) xmlGetProp (n, (xmlChar *)"value");
180 			work = n->children;
181 			while (work) {
182 				if (!strcmp ((gchar *) work->name, "title") ||
183 					!strcmp ((gchar *) work->name, "_title")) {
184 					if (!title) {
185 						if (!(tmp = (gchar *) xmlNodeGetContent (work)))
186 							tmp = (gchar *) xmlStrdup ((xmlChar *)"");
187 
188 						title = g_strdup (tmp);
189 						xmlFree (tmp);
190 					}
191 				} else if (!strcmp ((gchar *) work->name, "code")) {
192 					if (code || code_gen_func) {
193 						g_warning (
194 							"Element 'code' defined twice in '%s'",
195 							element->name);
196 					} else {
197 						xmlChar *fn;
198 
199 						/* if element 'code' has attribute 'func', then
200 						 * the content of the element is ignored and only
201 						 * the 'func' is used to generate actual rule code;
202 						 * The function prototype is:
203 						 * void code_gen_func (EFilterElement *element, GString *out, EFilterPart *part);
204 						 * where @element is the one on which was called,
205 						 * @out is GString where to add the code, and
206 						 * @part is part which contains @element and other options of it.
207 						*/
208 						fn = xmlGetProp (work, (xmlChar *)"func");
209 						if (fn && *fn) {
210 							code_gen_func = g_strdup ((const gchar *) fn);
211 						} else {
212 							if (!(tmp = (gchar *) xmlNodeGetContent (work)))
213 								tmp = (gchar *) xmlStrdup ((xmlChar *)"");
214 
215 							code = g_strdup (tmp);
216 							xmlFree (tmp);
217 						}
218 
219 						xmlFree (fn);
220 					}
221 				}
222 				work = work->next;
223 			}
224 
225 			e_filter_option_add (option, value, title, code, code_gen_func, FALSE);
226 			xmlFree (value);
227 			g_free (title);
228 			g_free (code);
229 			g_free (code_gen_func);
230 		} else if (g_str_equal ((gchar *) n->name, "dynamic")) {
231 			if (option->dynamic_func) {
232 				g_warning (
233 					"Only one 'dynamic' node is "
234 					"acceptable in the optionlist '%s'",
235 					element->name);
236 			} else {
237 				/* Expecting only one <dynamic func="cb" />
238 				 * in the option list,
239 				 * The 'cb' should be of this prototype:
240 				 * GSList *cb (void);
241 				 * returning GSList of struct _filter_option,
242 				 * all newly allocated, because it'll be
243 				 * freed with g_free and g_slist_free.
244 				 * 'is_dynamic' member is ignored here.
245 				 */
246 				xmlChar *fn;
247 
248 				fn = xmlGetProp (n, (xmlChar *)"func");
249 				if (fn && *fn) {
250 					GSList *items, *i;
251 					struct _filter_option *op;
252 
253 					option->dynamic_func = g_strdup ((const gchar *) fn);
254 
255 					/* Get options now, to have them
256 					 * available when reading saved
257 					 * rules. */
258 					items = filter_option_get_dynamic_options (option);
259 					for (i = items; i; i = i->next) {
260 						op = i->data;
261 
262 						if (op) {
263 							e_filter_option_add (
264 								option,
265 								op->value,
266 								op->title,
267 								op->code,
268 								op->code_gen_func,
269 								TRUE);
270 							free_option (op);
271 						}
272 					}
273 
274 					g_slist_free (items);
275 				} else {
276 					g_warning (
277 						"Missing 'func' attribute within "
278 						"'%s' node in optionlist '%s'",
279 						n->name, element->name);
280 				}
281 
282 				xmlFree (fn);
283 			}
284 		} else if (n->type == XML_ELEMENT_NODE) {
285 			g_warning ("Unknown xml node within optionlist: %s\n", n->name);
286 		}
287 		n = n->next;
288 	}
289 }
290 
291 static xmlNodePtr
292 filter_option_xml_encode (EFilterElement *element)
293 {
294 	EFilterOption *option = E_FILTER_OPTION (element);
295 	xmlNodePtr value;
296 
297 	value = xmlNewNode (NULL, (xmlChar *) "value");
298 	xmlSetProp (value, (xmlChar *) "name", (xmlChar *) element->name);
299 	xmlSetProp (value, (xmlChar *) "type", (xmlChar *) option->type);
300 	if (option->current)
301 		xmlSetProp (value, (xmlChar *) "value", (xmlChar *) option->current->value);
302 
303 	return value;
304 }
305 
306 static gint
307 filter_option_xml_decode (EFilterElement *element,
308                           xmlNodePtr node)
309 {
310 	EFilterOption *option = E_FILTER_OPTION (element);
311 	gchar *value;
312 
313 	xmlFree (element->name);
314 	element->name = (gchar *) xmlGetProp (node, (xmlChar *)"name");
315 
316 	value = (gchar *) xmlGetProp (node, (xmlChar *)"value");
317 	if (value) {
318 		option->current = find_option (option, value);
319 		xmlFree (value);
320 	} else {
321 		option->current = NULL;
322 	}
323 
324 	return 0;
325 }
326 
327 static EFilterElement *
328 filter_option_clone (EFilterElement *element)
329 {
330 	EFilterOption *option = E_FILTER_OPTION (element);
331 	EFilterOption *clone_option;
332 	EFilterElement *clone;
333 	GList *link;
334 
335 	/* Chain up to parent's clone() method. */
336 	clone = E_FILTER_ELEMENT_CLASS (e_filter_option_parent_class)->
337 		clone (element);
338 
339 	clone_option = E_FILTER_OPTION (clone);
340 
341 	for (link = option->options; link != NULL; link = g_list_next (link)) {
342 		struct _filter_option *op = link->data;
343 		struct _filter_option *newop;
344 
345 		newop = e_filter_option_add (
346 			clone_option, op->value,
347 			op->title, op->code, op->code_gen_func, op->is_dynamic);
348 		if (option->current == op)
349 			clone_option->current = newop;
350 	}
351 
352 	clone_option->dynamic_func = g_strdup (option->dynamic_func);
353 
354 	return clone;
355 }
356 
357 static GtkWidget *
358 filter_option_get_widget (EFilterElement *element)
359 {
360 	EFilterOption *option = E_FILTER_OPTION (element);
361 	GtkWidget *combobox;
362 	GList *l;
363 	struct _filter_option *op;
364 	gint index = 0, current = 0;
365 
366 	if (option->dynamic_func) {
367 		/* it is dynamically filled, thus remove all dynamics
368 		 * and put there the fresh ones */
369 		GSList *items, *i;
370 		GList *old_ops;
371 		struct _filter_option *old_cur;
372 
373 		old_ops = option->options;
374 		old_cur = option->current;
375 
376 		/* start with an empty list */
377 		option->current = NULL;
378 		option->options = NULL;
379 
380 		for (l = option->options; l; l = l->next) {
381 			op = l->data;
382 
383 			if (op->is_dynamic) {
384 				break;
385 			} else {
386 				e_filter_option_add (
387 					option, op->value, op->title,
388 					op->code, op->code_gen_func, FALSE);
389 			}
390 		}
391 
392 		items = filter_option_get_dynamic_options (option);
393 		for (i = items; i; i = i->next) {
394 			op = i->data;
395 
396 			if (op) {
397 				e_filter_option_add (
398 					option, op->value, op->title,
399 					op->code, op->code_gen_func, TRUE);
400 				free_option (op);
401 			}
402 		}
403 
404 		g_slist_free (items);
405 
406 		/* maybe some static left after those dynamic, add them too */
407 		for (; l; l = l->next) {
408 			op = l->data;
409 
410 			if (!op->is_dynamic)
411 				e_filter_option_add (
412 					option, op->value, op->title,
413 					op->code, op->code_gen_func, FALSE);
414 		}
415 
416 		if (old_cur)
417 			e_filter_option_set_current (option, old_cur->value);
418 
419 		/* free old list */
420 		g_list_foreach (old_ops, (GFunc) free_option, NULL);
421 		g_list_free (old_ops);
422 	}
423 
424 	combobox = gtk_combo_box_text_new ();
425 	l = option->options;
426 	while (l) {
427 		op = l->data;
428 		gtk_combo_box_text_append_text (
429 			GTK_COMBO_BOX_TEXT (combobox), _(op->title));
430 
431 		if (op == option->current)
432 			current = index;
433 
434 		l = g_list_next (l);
435 		index++;
436 	}
437 
438 	g_signal_connect (
439 		combobox, "changed",
440 		G_CALLBACK (filter_option_combobox_changed), element);
441 
442 	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), current);
443 
444 	return combobox;
445 }
446 
447 static void
448 filter_option_build_code (EFilterElement *element,
449                           GString *out,
450                           EFilterPart *part)
451 {
452 	EFilterOption *option = E_FILTER_OPTION (element);
453 
454 	if (option->current && option->current->code_gen_func) {
455 		filter_option_generate_code (option, out, part);
456 	} else if (option->current && option->current->code) {
457 		e_filter_part_expand_code (part, option->current->code, out);
458 	}
459 }
460 
461 static void
462 filter_option_format_sexp (EFilterElement *element,
463                            GString *out)
464 {
465 	EFilterOption *option = E_FILTER_OPTION (element);
466 
467 	if (option->current)
468 		camel_sexp_encode_string (out, option->current->value);
469 }
470 
471 static void
472 e_filter_option_class_init (EFilterOptionClass *class)
473 {
474 	GObjectClass *object_class;
475 	EFilterElementClass *filter_element_class;
476 
477 	object_class = G_OBJECT_CLASS (class);
478 	object_class->finalize = filter_option_finalize;
479 
480 	filter_element_class = E_FILTER_ELEMENT_CLASS (class);
481 	filter_element_class->eq = filter_option_eq;
482 	filter_element_class->xml_create = filter_option_xml_create;
483 	filter_element_class->xml_encode = filter_option_xml_encode;
484 	filter_element_class->xml_decode = filter_option_xml_decode;
485 	filter_element_class->clone = filter_option_clone;
486 	filter_element_class->get_widget = filter_option_get_widget;
487 	filter_element_class->build_code = filter_option_build_code;
488 	filter_element_class->format_sexp = filter_option_format_sexp;
489 }
490 
491 static void
492 e_filter_option_init (EFilterOption *option)
493 {
494 	option->type = "option";
495 	option->dynamic_func = NULL;
496 }
497 
498 EFilterElement *
499 e_filter_option_new (void)
500 {
501 	return g_object_new (E_TYPE_FILTER_OPTION, NULL);
502 }
503 
504 void
505 e_filter_option_set_current (EFilterOption *option,
506                              const gchar *name)
507 {
508 	g_return_if_fail (E_IS_FILTER_OPTION (option));
509 
510 	option->current = find_option (option, name);
511 }
512 
513 /* used by implementers to add additional options */
514 struct _filter_option *
515 e_filter_option_add (EFilterOption *option,
516                      const gchar *value,
517                      const gchar *title,
518                      const gchar *code,
519                      const gchar *code_gen_func,
520                      gboolean is_dynamic)
521 {
522 	struct _filter_option *op;
523 
524 	g_return_val_if_fail (E_IS_FILTER_OPTION (option), NULL);
525 	g_return_val_if_fail (find_option (option, value) == NULL, NULL);
526 
527 	if (code_gen_func && !*code_gen_func)
528 		code_gen_func = NULL;
529 
530 	op = g_malloc (sizeof (*op));
531 	op->title = g_strdup (title);
532 	op->value = g_strdup (value);
533 	op->code = g_strdup (code);
534 	op->code_gen_func = g_strdup (code_gen_func);
535 	op->is_dynamic = is_dynamic;
536 
537 	option->options = g_list_append (option->options, op);
538 
539 	if (option->current == NULL)
540 		option->current = op;
541 
542 	return op;
543 }
544 
545 const gchar *
546 e_filter_option_get_current (EFilterOption *option)
547 {
548 	g_return_val_if_fail (E_IS_FILTER_OPTION (option), NULL);
549 
550 	if (option->current == NULL)
551 		return NULL;
552 
553 	return option->current->value;
554 }
555 
556 void
557 e_filter_option_remove_all (EFilterOption *option)
558 {
559 	g_return_if_fail (E_IS_FILTER_OPTION (option));
560 
561 	g_list_foreach (option->options, (GFunc) free_option, NULL);
562 	g_list_free (option->options);
563 
564 	option->options = NULL;
565 	option->current = NULL;
566 }