evolution-3.6.4/mail/em-filter-rule.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@ximian.com>
 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 
 33 #include "em-filter-rule.h"
 34 #include "em-filter-context.h"
 35 
 36 #define d(x)
 37 
 38 static gint validate (EFilterRule *fr, EAlert **alert);
 39 static gint filter_eq (EFilterRule *fr, EFilterRule *cm);
 40 static xmlNodePtr xml_encode (EFilterRule *fr);
 41 static gint xml_decode (EFilterRule *fr, xmlNodePtr, ERuleContext *rc);
 42 static void rule_copy (EFilterRule *dest, EFilterRule *src);
 43 static GtkWidget *get_widget (EFilterRule *fr, ERuleContext *rc);
 44 
 45 G_DEFINE_TYPE (EMFilterRule, em_filter_rule, E_TYPE_FILTER_RULE)
 46 
 47 static void
 48 em_filter_rule_finalize (GObject *object)
 49 {
 50 	EMFilterRule *ff =(EMFilterRule *) object;
 51 
 52 	g_list_free_full (ff->actions, (GDestroyNotify) g_object_unref);
 53 
 54 	/* Chain up to parent's finalize() method. */
 55 	G_OBJECT_CLASS (em_filter_rule_parent_class)->finalize (object);
 56 }
 57 
 58 static void
 59 em_filter_rule_class_init (EMFilterRuleClass *class)
 60 {
 61 	GObjectClass *object_class;
 62 	EFilterRuleClass *filter_rule_class;
 63 
 64 	object_class = G_OBJECT_CLASS (class);
 65 	object_class->finalize = em_filter_rule_finalize;
 66 
 67 	filter_rule_class = E_FILTER_RULE_CLASS (class);
 68 	filter_rule_class->validate = validate;
 69 	filter_rule_class->eq = filter_eq;
 70 	filter_rule_class->xml_encode = xml_encode;
 71 	filter_rule_class->xml_decode = xml_decode;
 72 	filter_rule_class->copy = rule_copy;
 73 	filter_rule_class->get_widget = get_widget;
 74 }
 75 
 76 static void
 77 em_filter_rule_init (EMFilterRule *ff)
 78 {
 79 }
 80 
 81 /**
 82  * em_filter_rule_new:
 83  *
 84  * Create a new EMFilterRule object.
 85  *
 86  * Return value: A new #EMFilterRule object.
 87  **/
 88 EFilterRule *
 89 em_filter_rule_new (void)
 90 {
 91 	return g_object_new (em_filter_rule_get_type (), NULL, NULL);
 92 }
 93 
 94 void
 95 em_filter_rule_add_action (EMFilterRule *fr,
 96                            EFilterPart *fp)
 97 {
 98 	fr->actions = g_list_append (fr->actions, fp);
 99 
100 	e_filter_rule_emit_changed ((EFilterRule *) fr);
101 }
102 
103 void
104 em_filter_rule_remove_action (EMFilterRule *fr,
105                               EFilterPart *fp)
106 {
107 	fr->actions = g_list_remove (fr->actions, fp);
108 
109 	e_filter_rule_emit_changed ((EFilterRule *) fr);
110 }
111 
112 void
113 em_filter_rule_replace_action (EMFilterRule *fr,
114                                EFilterPart *fp,
115                                EFilterPart *new)
116 {
117 	GList *l;
118 
119 	l = g_list_find (fr->actions, fp);
120 	if (l) {
121 		l->data = new;
122 	} else {
123 		fr->actions = g_list_append (fr->actions, new);
124 	}
125 
126 	e_filter_rule_emit_changed ((EFilterRule *) fr);
127 }
128 
129 void
130 em_filter_rule_build_action (EMFilterRule *fr,
131                              GString *out)
132 {
133 	g_string_append (out, "(begin\n");
134 	e_filter_part_build_code_list (fr->actions, out);
135 	g_string_append (out, ")\n");
136 }
137 
138 static gint
139 validate (EFilterRule *fr,
140           EAlert **alert)
141 {
142 	EMFilterRule *ff =(EMFilterRule *) fr;
143 	GList *parts;
144 	gint valid;
145 
146 	valid = E_FILTER_RULE_CLASS (em_filter_rule_parent_class)->
147 		validate (fr, alert);
148 
149 	/* validate rule actions */
150 	parts = ff->actions;
151 	while (parts && valid) {
152 		valid = e_filter_part_validate ((EFilterPart *) parts->data, alert);
153 		parts = parts->next;
154 	}
155 
156 	return valid;
157 }
158 
159 static gint
160 list_eq (GList *al,
161          GList *bl)
162 {
163 	gint truth = TRUE;
164 
165 	while (truth && al && bl) {
166 		EFilterPart *a = al->data, *b = bl->data;
167 
168 		truth = e_filter_part_eq (a, b);
169 		al = al->next;
170 		bl = bl->next;
171 	}
172 
173 	return truth && al == NULL && bl == NULL;
174 }
175 
176 static gint
177 filter_eq (EFilterRule *fr,
178            EFilterRule *cm)
179 {
180 	return E_FILTER_RULE_CLASS (em_filter_rule_parent_class)->eq (fr, cm)
181 		&& list_eq (
182 			((EMFilterRule *) fr)->actions,
183 			((EMFilterRule *) cm)->actions);
184 }
185 
186 static xmlNodePtr
187 xml_encode (EFilterRule *fr)
188 {
189 	EMFilterRule *ff =(EMFilterRule *) fr;
190 	xmlNodePtr node, set, work;
191 	GList *l;
192 
193 	node = E_FILTER_RULE_CLASS (em_filter_rule_parent_class)->
194 		xml_encode (fr);
195 	g_return_val_if_fail (node != NULL, NULL);
196 	set = xmlNewNode (NULL, (const guchar *)"actionset");
197 	xmlAddChild (node, set);
198 	l = ff->actions;
199 	while (l) {
200 		work = e_filter_part_xml_encode ((EFilterPart *) l->data);
201 		xmlAddChild (set, work);
202 		l = l->next;
203 	}
204 
205 	return node;
206 
207 }
208 
209 static void
210 load_set (xmlNodePtr node,
211           EMFilterRule *ff,
212           ERuleContext *rc)
213 {
214 	xmlNodePtr work;
215 	gchar *rulename;
216 	EFilterPart *part;
217 
218 	work = node->children;
219 	while (work) {
220 		if (!strcmp ((gchar *) work->name, "part")) {
221 			rulename = (gchar *) xmlGetProp (work, (const guchar *)"name");
222 			part = em_filter_context_find_action ((EMFilterContext *) rc, rulename);
223 			if (part) {
224 				part = e_filter_part_clone (part);
225 				e_filter_part_xml_decode (part, work);
226 				em_filter_rule_add_action (ff, part);
227 			} else {
228 				g_warning ("cannot find rule part '%s'\n", rulename);
229 			}
230 			xmlFree (rulename);
231 		} else if (work->type == XML_ELEMENT_NODE) {
232 			g_warning ("Unknown xml node in part: %s", work->name);
233 		}
234 		work = work->next;
235 	}
236 }
237 
238 static gint
239 xml_decode (EFilterRule *fr,
240             xmlNodePtr node,
241             ERuleContext *rc)
242 {
243 	EMFilterRule *ff =(EMFilterRule *) fr;
244 	xmlNodePtr work;
245 	gint result;
246 
247 	result = E_FILTER_RULE_CLASS (em_filter_rule_parent_class)->
248 		xml_decode (fr, node, rc);
249 	if (result != 0)
250 		return result;
251 
252 	work = node->children;
253 	while (work) {
254 		if (!strcmp ((gchar *) work->name, "actionset")) {
255 			load_set (work, ff, rc);
256 		}
257 		work = work->next;
258 	}
259 
260 	return 0;
261 }
262 
263 static void
264 rule_copy (EFilterRule *dest,
265            EFilterRule *src)
266 {
267 	EMFilterRule *fdest, *fsrc;
268 	GList *node;
269 
270 	fdest =(EMFilterRule *) dest;
271 	fsrc =(EMFilterRule *) src;
272 
273 	if (fdest->actions) {
274 		g_list_foreach (fdest->actions, (GFunc) g_object_unref, NULL);
275 		g_list_free (fdest->actions);
276 		fdest->actions = NULL;
277 	}
278 
279 	node = fsrc->actions;
280 	while (node) {
281 		EFilterPart *part = node->data;
282 
283 		g_object_ref (part);
284 		fdest->actions = g_list_append (fdest->actions, part);
285 		node = node->next;
286 	}
287 
288 	E_FILTER_RULE_CLASS (em_filter_rule_parent_class)->copy (dest, src);
289 }
290 
291 struct _part_data {
292 	EFilterRule *fr;
293 	EMFilterContext *f;
294 	EFilterPart *part;
295 	GtkWidget *partwidget, *container;
296 };
297 
298 static void
299 part_combobox_changed (GtkComboBox *combobox,
300                        struct _part_data *data)
301 {
302 	EFilterPart *part = NULL;
303 	EFilterPart *newpart;
304 	gint index, i;
305 
306 	index = gtk_combo_box_get_active (combobox);
307 	for (i = 0, part = em_filter_context_next_action (data->f, part);
308 		part && i < index;
309 		i++, part = em_filter_context_next_action (data->f, part)) {
310 		/* traverse until reached index */
311 	}
312 
313 	g_return_if_fail (part != NULL);
314 	g_return_if_fail (i == index);
315 
316 	/* dont update if we haven't changed */
317 	if (!strcmp (part->title, data->part->title))
318 		return;
319 
320 	/* here we do a widget shuffle, throw away the old widget/rulepart,
321 	 * and create another */
322 	if (data->partwidget)
323 		gtk_container_remove (GTK_CONTAINER (data->container), data->partwidget);
324 
325 	newpart = e_filter_part_clone (part);
326 	e_filter_part_copy_values (newpart, data->part);
327 	em_filter_rule_replace_action ((EMFilterRule *) data->fr, data->part, newpart);
328 	g_object_unref (data->part);
329 	data->part = newpart;
330 	data->partwidget = e_filter_part_get_widget (newpart);
331 	if (data->partwidget)
332 		gtk_box_pack_start (
333 			GTK_BOX (data->container),
334 			data->partwidget, TRUE, TRUE, 0);
335 }
336 
337 static GtkWidget *
338 get_rule_part_widget (EMFilterContext *f,
339                       EFilterPart *newpart,
340                       EFilterRule *fr)
341 {
342 	EFilterPart *part = NULL;
343 	GtkWidget *combobox;
344 	GtkWidget *hbox;
345 	GtkWidget *p;
346 	gint index = 0, current = 0;
347 	struct _part_data *data;
348 
349 	data = g_malloc0 (sizeof (*data));
350 	data->fr = fr;
351 	data->f = f;
352 	data->part = newpart;
353 
354 	hbox = gtk_hbox_new (FALSE, 0);
355 	/* only set to automatically clean up the memory and for less_parts */
356 	g_object_set_data_full ((GObject *) hbox, "data", data, g_free);
357 
358 	p = e_filter_part_get_widget (newpart);
359 
360 	data->partwidget = p;
361 	data->container = hbox;
362 
363 	combobox = gtk_combo_box_text_new ();
364 	while ((part = em_filter_context_next_action (f, part))) {
365 		gtk_combo_box_text_append_text (
366 			GTK_COMBO_BOX_TEXT (combobox), _(part->title));
367 
368 		if (!strcmp (newpart->title, part->title))
369 			current = index;
370 
371 		index++;
372 	}
373 
374 	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), current);
375 	g_signal_connect (
376 		combobox, "changed",
377 		G_CALLBACK (part_combobox_changed), data);
378 	gtk_widget_show (combobox);
379 
380 	gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, 0);
381 	if (p)
382 		gtk_box_pack_start (GTK_BOX (hbox), p, TRUE, TRUE, 0);
383 
384 	gtk_widget_show_all (hbox);
385 
386 	return hbox;
387 }
388 
389 struct _rule_data {
390 	EFilterRule *fr;
391 	EMFilterContext *f;
392 	GtkWidget *parts;
393 };
394 
395 static void
396 less_parts (GtkWidget *button,
397             struct _rule_data *data)
398 {
399 	EFilterPart *part;
400 	GtkWidget *rule;
401 	struct _part_data *part_data;
402 	GList *l;
403 
404 	l =((EMFilterRule *) data->fr)->actions;
405 	if (g_list_length (l) < 2)
406 		return;
407 
408 	rule = g_object_get_data ((GObject *) button, "rule");
409 	part_data = g_object_get_data ((GObject *) rule, "data");
410 
411 	g_return_if_fail (part_data != NULL);
412 
413 	part = part_data->part;
414 
415 	/* remove the part from the list */
416 	em_filter_rule_remove_action ((EMFilterRule *) data->fr, part);
417 	g_object_unref (part);
418 
419 	/* and from the display */
420 	gtk_container_remove (GTK_CONTAINER (data->parts), rule);
421 	gtk_container_remove (GTK_CONTAINER (data->parts), button);
422 }
423 
424 static void
425 attach_rule (GtkWidget *rule,
426              struct _rule_data *data,
427              EFilterPart *part,
428              gint row)
429 {
430 	GtkWidget *remove;
431 
432 	gtk_table_attach (
433 		GTK_TABLE (data->parts), rule, 0, 1, row, row + 1,
434 		GTK_EXPAND | GTK_FILL, 0, 0, 0);
435 
436 	remove = gtk_button_new_from_stock (GTK_STOCK_REMOVE);
437 	g_object_set_data ((GObject *) remove, "rule", rule);
438 	/*gtk_button_set_relief(GTK_BUTTON(remove), GTK_RELIEF_NONE);*/
439 	g_signal_connect (
440 		remove, "clicked",
441 		G_CALLBACK (less_parts), data);
442 	gtk_table_attach (
443 		GTK_TABLE (data->parts), remove, 1, 2, row, row + 1,
444 		0, 0, 0, 0);
445 	gtk_widget_show (remove);
446 }
447 
448 static void
449 do_grab_focus_cb (GtkWidget *widget,
450                   gpointer data)
451 {
452 	gboolean *done = (gboolean *) data;
453 
454 	if (*done || !widget)
455 		return;
456 
457 	if (gtk_widget_get_can_focus (widget) || GTK_IS_COMBO_BOX (widget)) {
458 		*done = TRUE;
459 		gtk_widget_grab_focus (widget);
460 	} else if (GTK_IS_CONTAINER (widget)) {
461 		gtk_container_foreach (GTK_CONTAINER (widget), do_grab_focus_cb, done);
462 	}
463 }
464 
465 static void
466 more_parts (GtkWidget *button,
467             struct _rule_data *data)
468 {
469 	EFilterPart *new;
470 
471 	/* create a new rule entry, use the first type of rule */
472 	new = em_filter_context_next_action ((EMFilterContext *) data->f, NULL);
473 	if (new) {
474 		GtkWidget *w;
475 		guint rows;
476 
477 		new = e_filter_part_clone (new);
478 		em_filter_rule_add_action ((EMFilterRule *) data->fr, new);
479 		w = get_rule_part_widget (data->f, new, data->fr);
480 
481 		g_object_get (data->parts, "n-rows", &rows, NULL);
482 		gtk_table_resize (GTK_TABLE (data->parts), rows + 1, 2);
483 		attach_rule (w, data, new, rows);
484 
485 		if (GTK_IS_CONTAINER (w)) {
486 			gboolean done = FALSE;
487 
488 			gtk_container_foreach (GTK_CONTAINER (w), do_grab_focus_cb, &done);
489 		} else
490 			gtk_widget_grab_focus (w);
491 
492 		/* also scroll down to see new part */
493 		w = (GtkWidget *) g_object_get_data (G_OBJECT (button), "scrolled-window");
494 		if (w) {
495 			GtkAdjustment *adjustment;
496 
497 			adjustment = gtk_scrolled_window_get_vadjustment (
498 				GTK_SCROLLED_WINDOW (w));
499 
500 			if (adjustment) {
501 				gdouble upper;
502 
503 				upper = gtk_adjustment_get_upper (adjustment);
504 				gtk_adjustment_set_value (adjustment, upper);
505 			}
506 		}
507 	}
508 }
509 
510 static void
511 ensure_scrolled_height_cb (GtkAdjustment *adj,
512                            GParamSpec *param_spec,
513                            GtkScrolledWindow *scrolled_window)
514 {
515 	GtkWidget *toplevel;
516 	GdkScreen *screen;
517 	gint toplevel_height, scw_height, require_scw_height = 0, max_height;
518 
519 	toplevel = gtk_widget_get_toplevel (GTK_WIDGET (scrolled_window));
520 	if (!toplevel || !gtk_widget_is_toplevel (toplevel))
521 		return;
522 
523 	scw_height = gtk_widget_get_allocated_height (GTK_WIDGET (scrolled_window));
524 
525 	gtk_widget_get_preferred_height_for_width (gtk_bin_get_child (GTK_BIN (scrolled_window)),
526 		gtk_widget_get_allocated_width (GTK_WIDGET (scrolled_window)),
527 		&require_scw_height, NULL);
528 
529 	if (scw_height >= require_scw_height) {
530 		if (require_scw_height > 0)
531 			gtk_scrolled_window_set_min_content_height (scrolled_window, require_scw_height);
532 		return;
533 	}
534 
535 	if (!GTK_IS_WINDOW (toplevel) ||
536 	    !gtk_widget_get_window (toplevel))
537 		return;
538 
539 	screen = gtk_window_get_screen (GTK_WINDOW (toplevel));
540 	if (screen) {
541 		gint monitor;
542 		GdkRectangle workarea;
543 
544 		monitor = gdk_screen_get_monitor_at_window (screen, gtk_widget_get_window (toplevel));
545 		if (monitor < 0)
546 			monitor = 0;
547 
548 		gdk_screen_get_monitor_workarea (screen, monitor, &workarea);
549 
550 		/* can enlarge up to 4 / 5 monitor's work area height */
551 		max_height = workarea.height * 4 / 5;
552 	} else {
553 		return;
554 	}
555 
556 	toplevel_height = gtk_widget_get_allocated_height (toplevel);
557 	if (toplevel_height + require_scw_height - scw_height > max_height)
558 		return;
559 
560 	gtk_scrolled_window_set_min_content_height (scrolled_window, require_scw_height);
561 }
562 
563 static GtkWidget *
564 get_widget (EFilterRule *fr,
565             ERuleContext *rc)
566 {
567 	GtkWidget *widget, *add, *label;
568 	GtkWidget *parts, *inframe, *w;
569 	GtkWidget *scrolledwindow;
570 	GtkGrid *hgrid;
571 	GtkAdjustment *hadj, *vadj;
572 	GList *l;
573 	EFilterPart *part;
574 	struct _rule_data *data;
575 	EMFilterRule *ff =(EMFilterRule *) fr;
576 	gint rows, i = 0;
577 	gchar *msg;
578 
579 	widget = E_FILTER_RULE_CLASS (em_filter_rule_parent_class)->
580 		get_widget (fr, rc);
581 
582 	/* and now for the action area */
583 	msg = g_strdup_printf ("<b>%s</b>", _("Then"));
584 	label = gtk_label_new (msg);
585 	gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
586 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
587 	gtk_container_add (GTK_CONTAINER (widget), label);
588 	g_free (msg);
589 
590 	hgrid = GTK_GRID (gtk_grid_new ());
591 	gtk_grid_set_column_spacing (hgrid, 3);
592 	gtk_widget_set_hexpand (GTK_WIDGET (hgrid), TRUE);
593 	gtk_widget_set_halign (GTK_WIDGET (hgrid), GTK_ALIGN_FILL);
594 	gtk_container_add (GTK_CONTAINER (widget), GTK_WIDGET (hgrid));
595 
596 	label = gtk_label_new ("");
597 	gtk_grid_attach (hgrid, label, 0, 0, 1, 1);
598 
599 	inframe = gtk_grid_new ();
600 	gtk_grid_set_row_spacing (GTK_GRID (inframe), 6);
601 	gtk_orientable_set_orientation (GTK_ORIENTABLE (inframe), GTK_ORIENTATION_VERTICAL);
602 	gtk_widget_set_hexpand (inframe, TRUE);
603 	gtk_widget_set_halign (inframe, GTK_ALIGN_FILL);
604 	gtk_widget_set_vexpand (inframe, TRUE);
605 	gtk_widget_set_valign (inframe, GTK_ALIGN_FILL);
606 
607 	gtk_grid_attach_next_to (hgrid, inframe, label, GTK_POS_RIGHT, 1, 1);
608 
609 	rows = g_list_length (ff->actions);
610 	parts = gtk_table_new (rows, 2, FALSE);
611 	data = g_malloc0 (sizeof (*data));
612 	data->f =(EMFilterContext *) rc;
613 	data->fr = fr;
614 	data->parts = parts;
615 
616 	/* only set to automatically clean up the memory */
617 	g_object_set_data_full ((GObject *) hgrid, "data", data, g_free);
618 
619 	l = ff->actions;
620 	while (l) {
621 		part = l->data;
622 		d (printf ("adding action %s\n", part->title));
623 		w = get_rule_part_widget ((EMFilterContext *) rc, part, fr);
624 		attach_rule (w, data, part, i++);
625 		l = l->next;
626 	}
627 
628 	hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 1.0, 1.0 ,1.0, 1.0));
629 	vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 1.0, 1.0 ,1.0, 1.0));
630 	scrolledwindow = gtk_scrolled_window_new (hadj, vadj);
631 
632 	gtk_scrolled_window_set_policy (
633 		GTK_SCROLLED_WINDOW (scrolledwindow),
634 		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
635 	gtk_scrolled_window_add_with_viewport (
636 		GTK_SCROLLED_WINDOW (scrolledwindow), parts);
637 
638 	gtk_widget_set_hexpand (scrolledwindow, TRUE);
639 	gtk_widget_set_halign (scrolledwindow, GTK_ALIGN_FILL);
640 	gtk_widget_set_vexpand (scrolledwindow, TRUE);
641 	gtk_widget_set_valign (scrolledwindow, GTK_ALIGN_FILL);
642 
643 	gtk_container_add (GTK_CONTAINER (inframe), scrolledwindow);
644 
645 	hgrid = GTK_GRID (gtk_grid_new ());
646 	gtk_grid_set_column_spacing (hgrid, 3);
647 
648 	add = gtk_button_new_with_mnemonic (_("Add Ac_tion"));
649 	gtk_button_set_image (
650 		GTK_BUTTON (add), gtk_image_new_from_stock (
651 		GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON));
652 	g_signal_connect (
653 		add, "clicked",
654 		G_CALLBACK (more_parts), data);
655 	gtk_grid_attach (hgrid, add, 0, 0, 1, 1);
656 
657 	gtk_container_add (GTK_CONTAINER (inframe), GTK_WIDGET (hgrid));
658 
659 	g_object_set_data (G_OBJECT (add), "scrolled-window", scrolledwindow);
660 
661 	g_signal_connect (
662 		vadj, "notify::upper",
663 		G_CALLBACK (ensure_scrolled_height_cb), scrolledwindow);
664 
665 	gtk_widget_show_all (widget);
666 
667 	return widget;
668 }