evolution-3.6.4/mail/e-mail-junk-options.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found e-mail-junk-options.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found e-mail-junk-options.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
Failure running clang-analyzer ('no-output-found')
Message
Unable to locate XML output from invoke-clang-analyzer
  1 /*
  2  * e-mail-junk-options.c
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  */
 18 
 19 #include "e-mail-junk-options.h"
 20 
 21 #include <config.h>
 22 #include <glib/gi18n-lib.h>
 23 
 24 #include <libemail-engine/e-mail-junk-filter.h>
 25 
 26 #define E_MAIL_JUNK_OPTIONS_GET_PRIVATE(obj) \
 27 	(G_TYPE_INSTANCE_GET_PRIVATE \
 28 	((obj), E_TYPE_MAIL_JUNK_OPTIONS, EMailJunkOptionsPrivate))
 29 
 30 G_DEFINE_TYPE (
 31 	EMailJunkOptions,
 32 	e_mail_junk_options,
 33 	GTK_TYPE_GRID)
 34 
 35 struct _EMailJunkOptionsPrivate {
 36 	EMailSession *session;
 37 
 38 	GtkWidget *label;		/* not referenced */
 39 	GtkWidget *combo_box;		/* not referenced */
 40 	GtkWidget *option_box;		/* not referenced */
 41 	GPtrArray *widgets;		/* not referenced */
 42 
 43 	GBinding *active_id_binding;
 44 };
 45 
 46 enum {
 47 	PROP_0,
 48 	PROP_SESSION
 49 };
 50 
 51 enum {
 52 	COLUMN_FILTER_NAME,
 53 	COLUMN_DISPLAY_NAME
 54 };
 55 
 56 static void
 57 mail_junk_options_combo_box_changed_cb (GtkComboBox *combo_box,
 58                                         EMailJunkOptions *options)
 59 {
 60 	GPtrArray *array;
 61 	gint active;
 62 	guint ii;
 63 
 64 	array = options->priv->widgets;
 65 	active = gtk_combo_box_get_active (combo_box);
 66 
 67 	for (ii = 0; ii < array->len; ii++) {
 68 		GtkWidget *widget = GTK_WIDGET (array->pdata[ii]);
 69 		gtk_widget_set_visible (widget, ii == active);
 70 	}
 71 }
 72 
 73 static void
 74 mail_junk_options_rebuild (EMailJunkOptions *options)
 75 {
 76 	EMailSession *session;
 77 	GtkComboBox *combo_box;
 78 	GtkTreeModel *model;
 79 	GtkBox *option_box;
 80 	GList *list = NULL;
 81 	GList *link;
 82 	guint n_filters;
 83 
 84 	session = e_mail_junk_options_get_session (options);
 85 	combo_box = GTK_COMBO_BOX (options->priv->combo_box);
 86 	option_box = GTK_BOX (options->priv->option_box);
 87 
 88 	/* Remove the GtkComboBox:active-id binding so it doesn't
 89 	 * affect EMailSession:junk-filter-name when we clear the
 90 	 * combo box's list model. */
 91 	if (options->priv->active_id_binding != NULL) {
 92 		g_object_unref (options->priv->active_id_binding);
 93 		options->priv->active_id_binding = NULL;
 94 	}
 95 
 96 	model = gtk_combo_box_get_model (combo_box);
 97 	gtk_list_store_clear (GTK_LIST_STORE (model));
 98 
 99 	g_ptr_array_foreach (
100 		options->priv->widgets,
101 		(GFunc) gtk_widget_destroy, NULL);
102 	g_ptr_array_set_size (options->priv->widgets, 0);
103 
104 	if (session != NULL)
105 		list = e_mail_session_get_available_junk_filters (session);
106 
107 	for (link = list; link != NULL; link = g_list_next (link)) {
108 		EMailJunkFilter *junk_filter;
109 		EMailJunkFilterClass *class;
110 		GtkWidget *widget;
111 		GtkTreeIter iter;
112 
113 		junk_filter = E_MAIL_JUNK_FILTER (link->data);
114 		class = E_MAIL_JUNK_FILTER_GET_CLASS (junk_filter);
115 
116 		gtk_list_store_append (GTK_LIST_STORE (model), &iter);
117 
118 		gtk_list_store_set (
119 			GTK_LIST_STORE (model), &iter,
120 			COLUMN_FILTER_NAME, class->filter_name,
121 			COLUMN_DISPLAY_NAME, class->display_name,
122 			-1);
123 
124 		/* Create a configuration widget for this junk filter,
125 		 * or else just create an empty placeholder widget. */
126 		widget = e_mail_junk_filter_new_config_widget (junk_filter);
127 		if (widget == NULL)
128 			widget = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
129 
130 		g_ptr_array_add (options->priv->widgets, widget);
131 
132 		/* Set extra padding to 12px, since only one child of
133 		 * 'option_box' is visible at a time, and we still want
134 		 * the extra padding if the first grid row is invisible. */
135 		gtk_box_pack_start (option_box, widget, FALSE, FALSE, 12);
136 	}
137 
138 	/* Synchronize the combo box with the active junk filter. */
139 	if (session != NULL) {
140 		GBinding *binding;
141 
142 		binding = g_object_bind_property (
143 			session, "junk-filter-name",
144 			combo_box, "active-id",
145 			G_BINDING_BIDIRECTIONAL |
146 			G_BINDING_SYNC_CREATE);
147 		options->priv->active_id_binding = binding;
148 	}
149 
150 	/* Select the first combo box item if we need to.  If there's
151 	 * no first item to select, this will silently do nothing. */
152 	if (gtk_combo_box_get_active (combo_box) < 0)
153 		gtk_combo_box_set_active (combo_box, 0);
154 
155 	/* Update visibility of widgets. */
156 	n_filters = g_list_length (list);
157 	gtk_widget_set_visible (GTK_WIDGET (options), n_filters > 0);
158 	gtk_widget_set_visible (options->priv->label, n_filters > 1);
159 	gtk_widget_set_visible (options->priv->combo_box, n_filters > 1);
160 
161 	g_list_free (list);
162 }
163 
164 static void
165 mail_junk_options_set_property (GObject *object,
166                                 guint property_id,
167                                 const GValue *value,
168                                 GParamSpec *pspec)
169 {
170 	switch (property_id) {
171 		case PROP_SESSION:
172 			e_mail_junk_options_set_session (
173 				E_MAIL_JUNK_OPTIONS (object),
174 				g_value_get_object (value));
175 			return;
176 	}
177 
178 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
179 }
180 
181 static void
182 mail_junk_options_get_property (GObject *object,
183                                 guint property_id,
184                                 GValue *value,
185                                 GParamSpec *pspec)
186 {
187 	switch (property_id) {
188 		case PROP_SESSION:
189 			g_value_set_object (
190 				value,
191 				e_mail_junk_options_get_session (
192 				E_MAIL_JUNK_OPTIONS (object)));
193 			return;
194 	}
195 
196 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
197 }
198 
199 static void
200 mail_junk_options_dispose (GObject *object)
201 {
202 	EMailJunkOptionsPrivate *priv;
203 
204 	priv = E_MAIL_JUNK_OPTIONS_GET_PRIVATE (object);
205 
206 	if (priv->session != NULL) {
207 		g_object_unref (priv->session);
208 		priv->session = NULL;
209 	}
210 
211 	/* Chain up to parent's dispose() method. */
212 	G_OBJECT_CLASS (e_mail_junk_options_parent_class)->dispose (object);
213 }
214 
215 static void
216 mail_junk_options_finalize (GObject *object)
217 {
218 	EMailJunkOptionsPrivate *priv;
219 
220 	priv = E_MAIL_JUNK_OPTIONS_GET_PRIVATE (object);
221 
222 	g_ptr_array_free (priv->widgets, TRUE);
223 
224 	/* Chain up to parent's finalize() method. */
225 	G_OBJECT_CLASS (e_mail_junk_options_parent_class)->finalize (object);
226 }
227 
228 static void
229 mail_junk_options_constructed (GObject *object)
230 {
231 	EMailJunkOptionsPrivate *priv;
232 	GtkCellRenderer *cell_renderer;
233 	GtkCellLayout *cell_layout;
234 	GtkListStore *list_store;
235 	GtkWidget *widget;
236 
237 	priv = E_MAIL_JUNK_OPTIONS_GET_PRIVATE (object);
238 
239 	/* XXX The margins we're using here are tailored to its
240 	 *     placement in the Junk tab of Mail Preferences.
241 	 *     EMailJunkOptions is not really reusable as is. */
242 
243 	/* Chain up to parent's constructed() method. */
244 	G_OBJECT_CLASS (e_mail_junk_options_parent_class)->constructed (object);
245 
246 	gtk_grid_set_column_spacing (GTK_GRID (object), 6);
247 
248 	list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
249 
250 	/* Label + combo box has a 12px left margin so it's
251 	 * aligned with the junk mail options above it. */
252 	widget = gtk_label_new (_("Junk filtering software:"));
253 	gtk_misc_set_alignment (GTK_MISC (widget), 0.0, 0.5);
254 	gtk_widget_set_margin_left (widget, 12);
255 	gtk_grid_attach (GTK_GRID (object), widget, 0, 0, 1, 1);
256 	priv->label = widget;  /* not referenced */
257 	gtk_widget_show (widget);
258 
259 	widget = gtk_combo_box_new_with_model (GTK_TREE_MODEL (list_store));
260 	gtk_combo_box_set_id_column (
261 		GTK_COMBO_BOX (widget), COLUMN_FILTER_NAME);
262 	gtk_grid_attach (GTK_GRID (object), widget, 1, 0, 1, 1);
263 	priv->combo_box = widget;  /* not referenced */
264 	gtk_widget_show (widget);
265 
266 	g_signal_connect (
267 		widget, "changed",
268 		G_CALLBACK (mail_junk_options_combo_box_changed_cb), object);
269 
270 	/* The config widgets that come from EMailJunkFilter have no
271 	 * left margin, since they usually include a bold header and
272 	 * interactive widgets with their own left margin. */
273 	widget = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
274 	gtk_grid_attach (GTK_GRID (object), widget, 0, 1, 2, 1);
275 	priv->option_box = widget;  /* not referenced */
276 	gtk_widget_show (widget);
277 
278 	cell_layout = GTK_CELL_LAYOUT (priv->combo_box);
279 	cell_renderer = gtk_cell_renderer_text_new ();
280 	gtk_cell_layout_pack_start (cell_layout, cell_renderer, FALSE);
281 
282 	gtk_cell_layout_add_attribute (
283 		cell_layout, cell_renderer,
284 		"text", COLUMN_DISPLAY_NAME);
285 
286 	g_object_unref (list_store);
287 }
288 
289 static void
290 mail_junk_options_map (GtkWidget *widget)
291 {
292 	/* Chain up to parent's map() method. */
293 	GTK_WIDGET_CLASS (e_mail_junk_options_parent_class)->map (widget);
294 
295 	mail_junk_options_rebuild (E_MAIL_JUNK_OPTIONS (widget));
296 }
297 
298 static void
299 e_mail_junk_options_class_init (EMailJunkOptionsClass *class)
300 {
301 	GObjectClass *object_class;
302 	GtkWidgetClass *widget_class;
303 
304 	g_type_class_add_private (class, sizeof (EMailJunkOptionsPrivate));
305 
306 	object_class = G_OBJECT_CLASS (class);
307 	object_class->set_property = mail_junk_options_set_property;
308 	object_class->get_property = mail_junk_options_get_property;
309 	object_class->dispose = mail_junk_options_dispose;
310 	object_class->finalize = mail_junk_options_finalize;
311 	object_class->constructed = mail_junk_options_constructed;
312 
313 	widget_class = GTK_WIDGET_CLASS (class);
314 	widget_class->map = mail_junk_options_map;
315 
316 	g_object_class_install_property (
317 		object_class,
318 		PROP_SESSION,
319 		g_param_spec_object (
320 			"session",
321 			NULL,
322 			NULL,
323 			E_TYPE_MAIL_SESSION,
324 			G_PARAM_READWRITE |
325 			G_PARAM_STATIC_STRINGS));
326 }
327 
328 static void
329 e_mail_junk_options_init (EMailJunkOptions *options)
330 {
331 	options->priv = E_MAIL_JUNK_OPTIONS_GET_PRIVATE (options);
332 
333 	options->priv->widgets = g_ptr_array_new ();
334 }
335 
336 GtkWidget *
337 e_mail_junk_options_new (EMailSession *session)
338 {
339 	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
340 
341 	return g_object_new (
342 		E_TYPE_MAIL_JUNK_OPTIONS, "session", session, NULL);
343 }
344 
345 EMailSession *
346 e_mail_junk_options_get_session (EMailJunkOptions *options)
347 {
348 	g_return_val_if_fail (E_IS_MAIL_JUNK_OPTIONS (options), NULL);
349 
350 	return options->priv->session;
351 }
352 
353 void
354 e_mail_junk_options_set_session (EMailJunkOptions *options,
355                                  EMailSession *session)
356 {
357 	g_return_if_fail (E_IS_MAIL_JUNK_OPTIONS (options));
358 
359 	if (options->priv->session == session)
360 		return;
361 
362 	if (session != NULL) {
363 		g_return_if_fail (E_IS_MAIL_SESSION (session));
364 		g_object_ref (session);
365 	}
366 
367 	if (options->priv->session != NULL)
368 		g_object_unref (options->priv->session);
369 
370 	options->priv->session = session;
371 
372 	g_object_notify (G_OBJECT (options), "session");
373 
374 	mail_junk_options_rebuild (options);
375 }