evolution-3.6.4/mail/em-filter-editor.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 <gtk/gtk.h>
 29 #include <glib/gi18n.h>
 30 
 31 #include "e-util/e-util.h"
 32 #include "e-util/e-util-private.h"
 33 
 34 #include "em-filter-editor.h"
 35 #include "em-filter-rule.h"
 36 
 37 G_DEFINE_TYPE (EMFilterEditor, em_filter_editor, E_TYPE_RULE_EDITOR)
 38 
 39 static EFilterRule *
 40 filter_editor_create_rule (ERuleEditor *rule_editor)
 41 {
 42 	EFilterRule *rule;
 43 	EFilterPart *part;
 44 
 45 	/* create a rule with 1 part & 1 action in it */
 46 	rule = (EFilterRule *) em_filter_rule_new ();
 47 	part = e_rule_context_next_part (rule_editor->context, NULL);
 48 	e_filter_rule_add_part (rule, e_filter_part_clone (part));
 49 	part = em_filter_context_next_action (
 50 		(EMFilterContext *) rule_editor->context, NULL);
 51 	em_filter_rule_add_action (
 52 		(EMFilterRule *) rule, e_filter_part_clone (part));
 53 
 54 	return rule;
 55 }
 56 
 57 static void
 58 em_filter_editor_class_init (EMFilterEditorClass *class)
 59 {
 60 	ERuleEditorClass *rule_editor_class;
 61 
 62 	rule_editor_class = E_RULE_EDITOR_CLASS (class);
 63 	rule_editor_class->create_rule = filter_editor_create_rule;
 64 }
 65 
 66 static void
 67 em_filter_editor_init (EMFilterEditor *filter_editor)
 68 {
 69 	gtk_window_set_default_size (GTK_WINDOW (filter_editor), 400, 650);
 70 
 71 	e_restore_window (
 72 		GTK_WINDOW (filter_editor),
 73 		"/org/gnome/evolution/mail/filter-window/",
 74 		E_RESTORE_WINDOW_SIZE);
 75 }
 76 
 77 /**
 78  * em_filter_editor_new:
 79  *
 80  * Create a new EMFilterEditor object.
 81  *
 82  * Return value: A new #EMFilterEditor object.
 83  **/
 84 EMFilterEditor *
 85 em_filter_editor_new (EMFilterContext *fc,
 86                       const EMFilterSource *source_names)
 87 {
 88 	EMFilterEditor *fe;
 89 	GtkBuilder *builder;
 90 
 91 	fe = g_object_new (EM_TYPE_FILTER_EDITOR, NULL);
 92 
 93 	builder = gtk_builder_new ();
 94 	e_load_ui_builder_definition (builder, "filter.ui");
 95 	em_filter_editor_construct (fe, fc, builder, source_names);
 96 	g_object_unref (builder);
 97 
 98 	return fe;
 99 }
100 
101 static void
102 free_sources (gpointer data)
103 {
104 	GSList *sources = data;
105 
106 	g_slist_foreach (sources, (GFunc) g_free, NULL);
107 	g_slist_free (sources);
108 }
109 
110 static void
111 select_source (GtkComboBox *combobox,
112                EMFilterEditor *fe)
113 {
114 	gchar *source;
115 	gint idx;
116 	GSList *sources;
117 
118 	g_return_if_fail (GTK_IS_COMBO_BOX (combobox));
119 
120 	idx = gtk_combo_box_get_active (combobox);
121 	sources = g_object_get_data (G_OBJECT (combobox), "sources");
122 
123 	g_return_if_fail (idx >= 0 && idx < g_slist_length (sources));
124 
125 	source = (gchar *) (g_slist_nth (sources, idx))->data;
126 	g_return_if_fail (source);
127 
128 	e_rule_editor_set_source ((ERuleEditor *) fe, source);
129 }
130 
131 void
132 em_filter_editor_construct (EMFilterEditor *fe,
133                             EMFilterContext *fc,
134                             GtkBuilder *builder,
135                             const EMFilterSource *source_names)
136 {
137 	GtkWidget *combobox;
138 	gint i;
139 	GtkTreeViewColumn *column;
140 	GtkTreeIter iter;
141 	GtkListStore *store;
142 	GSList *sources = NULL;
143 
144 	combobox = e_builder_get_widget (builder, "filter_source_combobox");
145 	store = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (combobox)));
146 	gtk_list_store_clear (store);
147 
148 	for (i = 0; source_names[i].source; i++) {
149 		gtk_list_store_append (store, &iter);
150 		gtk_list_store_set (
151 			store, &iter,
152 			0, source_names[i].name,
153 			-1);
154 		sources = g_slist_append (sources, g_strdup (source_names[i].source));
155 	}
156 
157 	gtk_combo_box_set_active (GTK_COMBO_BOX (combobox), 0);
158 	g_signal_connect (
159 		combobox, "changed",
160 		G_CALLBACK (select_source), fe);
161 	g_object_set_data_full (G_OBJECT (combobox), "sources", sources, free_sources);
162 	gtk_widget_show (combobox);
163 
164 	e_rule_editor_construct (
165 		(ERuleEditor *) fe, (ERuleContext *) fc,
166 		builder, source_names[0].source, _("_Filter Rules"));
167 
168 	/* Show the Enabled column, we support it here */
169 	column = gtk_tree_view_get_column (GTK_TREE_VIEW (E_RULE_EDITOR (fe)->list), 0);
170 	gtk_tree_view_column_set_visible (column, TRUE);
171 }