evolution-3.6.4/mail/e-mail-sidebar.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found e-mail-sidebar.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found e-mail-sidebar.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-sidebar.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  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 19  *
 20  */
 21 
 22 #ifdef HAVE_CONFIG_H
 23 #include <config.h>
 24 #endif
 25 
 26 #include "e-mail-sidebar.h"
 27 
 28 #include <string.h>
 29 #include <camel/camel.h>
 30 
 31 #include <libemail-engine/e-mail-folder-utils.h>
 32 
 33 #include "mail/em-utils.h"
 34 
 35 #define E_MAIL_SIDEBAR_GET_PRIVATE(obj) \
 36 	(G_TYPE_INSTANCE_GET_PRIVATE \
 37 	((obj), E_TYPE_MAIL_SIDEBAR, EMailSidebarPrivate))
 38 
 39 struct _EMailSidebarPrivate {
 40 	GKeyFile *key_file;  /* not owned */
 41 	GtkTreeModel *model;
 42 	GtkTreeSelection *selection;
 43 };
 44 
 45 enum {
 46 	PROP_0,
 47 	PROP_KEY_FILE
 48 };
 49 
 50 enum {
 51 	KEY_FILE_CHANGED,
 52 	LAST_SIGNAL
 53 };
 54 
 55 static guint signals[LAST_SIGNAL];
 56 
 57 G_DEFINE_TYPE (
 58 	EMailSidebar,
 59 	e_mail_sidebar,
 60 	EM_TYPE_FOLDER_TREE)
 61 
 62 static void
 63 mail_sidebar_restore_state (EMailSidebar *sidebar)
 64 {
 65 	EMFolderTree *folder_tree;
 66 	GKeyFile *key_file;
 67 	gchar *selected;
 68 
 69 	key_file = e_mail_sidebar_get_key_file (sidebar);
 70 
 71 	/* Make sure we have a key file to restore state from. */
 72 	if (key_file == NULL)
 73 		return;
 74 
 75 	folder_tree = EM_FOLDER_TREE (sidebar);
 76 
 77 	/* Restore selected folder. */
 78 
 79 	selected = g_key_file_get_string (
 80 		key_file, "Folder Tree", "Selected", NULL);
 81 	if (selected != NULL) {
 82 		em_folder_tree_set_selected (folder_tree, selected, FALSE);
 83 		g_free (selected);
 84 	}
 85 
 86 	em_folder_tree_restore_state (folder_tree, key_file);
 87 }
 88 
 89 static void
 90 mail_sidebar_model_loaded_row_cb (GtkTreeModel *model,
 91                                   GtkTreePath *path,
 92                                   GtkTreeIter *iter,
 93                                   EMailSidebar *sidebar)
 94 {
 95 	GtkTreeView *tree_view;
 96 	CamelStore *store;
 97 	GKeyFile *key_file;
 98 	gboolean expanded;
 99 	gboolean is_folder;
100 	gboolean is_store;
101 	gchar *folder_name;
102 	gchar *group_name;
103 	const gchar *key;
104 
105 	tree_view = GTK_TREE_VIEW (sidebar);
106 	key_file = e_mail_sidebar_get_key_file (sidebar);
107 
108 	/* Make sure we have a key file to record state changes. */
109 	if (key_file == NULL)
110 		return;
111 
112 	gtk_tree_model_get (
113 		model, iter,
114 		COL_POINTER_CAMEL_STORE, &store,
115 		COL_STRING_FULL_NAME, &folder_name,
116 		COL_BOOL_IS_STORE, &is_store,
117 		COL_BOOL_IS_FOLDER, &is_folder, -1);
118 
119 	g_return_if_fail (is_store || is_folder);
120 
121 	key = STATE_KEY_EXPANDED;
122 	if (is_store) {
123 		const gchar *uid;
124 
125 		uid = camel_service_get_uid (CAMEL_SERVICE (store));
126 		group_name = g_strdup_printf ("Store %s", uid);
127 		expanded = TRUE;
128 	} else {
129 		gchar *uri;
130 
131 		uri = e_mail_folder_uri_build (store, folder_name);
132 		group_name = g_strdup_printf ("Folder %s", uri);
133 		g_free (uri);
134 		expanded = FALSE;
135 	}
136 
137 	if (g_key_file_has_key (key_file, group_name, key, NULL))
138 		expanded = g_key_file_get_boolean (
139 			key_file, group_name, key, NULL);
140 
141 	if (expanded)
142 		gtk_tree_view_expand_row (tree_view, path, FALSE);
143 
144 	g_free (group_name);
145 	g_free (folder_name);
146 }
147 
148 static void
149 mail_sidebar_selection_changed_cb (GtkTreeSelection *selection,
150                                    EMailSidebar *sidebar)
151 {
152 	GtkTreeModel *model;
153 	GtkTreeIter iter;
154 	GKeyFile *key_file;
155 	gchar *uri = NULL;
156 
157 	key_file = e_mail_sidebar_get_key_file (sidebar);
158 
159 	/* Make sure we have a key file to record state changes. */
160 	if (key_file == NULL)
161 		return;
162 
163 	if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
164 		CamelStore *store;
165 		gchar *folder_name;
166 
167 		gtk_tree_model_get (
168 			model, &iter,
169 			COL_POINTER_CAMEL_STORE, &store,
170 			COL_STRING_FULL_NAME, &folder_name, -1);
171 
172 		if (CAMEL_IS_STORE (store) && folder_name != NULL)
173 			uri = e_mail_folder_uri_build (store, folder_name);
174 
175 		g_free (folder_name);
176 	}
177 
178 	if (uri != NULL)
179 		g_key_file_set_string (
180 			key_file, "Folder Tree", "Selected", uri);
181 	else
182 		g_key_file_remove_key (
183 			key_file, "Folder Tree", "Selected", NULL);
184 
185 	e_mail_sidebar_key_file_changed (sidebar);
186 
187 	g_free (uri);
188 }
189 
190 static void
191 mail_sidebar_set_property (GObject *object,
192                            guint property_id,
193                            const GValue *value,
194                            GParamSpec *pspec)
195 {
196 	switch (property_id) {
197 		case PROP_KEY_FILE:
198 			e_mail_sidebar_set_key_file (
199 				E_MAIL_SIDEBAR (object),
200 				g_value_get_pointer (value));
201 			return;
202 	}
203 
204 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
205 }
206 
207 static void
208 mail_sidebar_get_property (GObject *object,
209                            guint property_id,
210                            GValue *value,
211                            GParamSpec *pspec)
212 {
213 	switch (property_id) {
214 		case PROP_KEY_FILE:
215 			g_value_set_pointer (
216 				value, e_mail_sidebar_get_key_file (
217 				E_MAIL_SIDEBAR (object)));
218 			return;
219 	}
220 
221 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
222 }
223 
224 static void
225 mail_sidebar_constructed (GObject *object)
226 {
227 	EMailSidebarPrivate *priv;
228 	GtkTreeSelection *selection;
229 	GtkTreeView *tree_view;
230 	GtkTreeModel *model;
231 
232 	priv = E_MAIL_SIDEBAR_GET_PRIVATE (object);
233 
234 	/* Chain up to parent's constructed() property. */
235 	G_OBJECT_CLASS (e_mail_sidebar_parent_class)->constructed (object);
236 
237 	tree_view = GTK_TREE_VIEW (object);
238 	model = gtk_tree_view_get_model (tree_view);
239 	selection = gtk_tree_view_get_selection (tree_view);
240 
241 	em_folder_tree_model_set_selection (
242 		EM_FOLDER_TREE_MODEL (model), selection);
243 
244 	/* Keep an internal reference to these since we're connecting
245 	 * signal handlers to them.  Retrieving them during dispose()
246 	 * does not guarantee we get the same instances back. */
247 	priv->model = g_object_ref (model);
248 	priv->selection = g_object_ref (selection);
249 
250 	g_signal_connect (
251 		model, "loaded-row",
252 		G_CALLBACK (mail_sidebar_model_loaded_row_cb), object);
253 
254 	g_signal_connect (
255 		selection, "changed",
256 		G_CALLBACK (mail_sidebar_selection_changed_cb), object);
257 }
258 
259 static void
260 mail_sidebar_dispose (GObject *object)
261 {
262 	EMailSidebarPrivate *priv;
263 
264 	priv = E_MAIL_SIDEBAR_GET_PRIVATE (object);
265 
266 	if (priv->model != NULL) {
267 		g_signal_handlers_disconnect_by_func (
268 			priv->model,
269 			mail_sidebar_model_loaded_row_cb, object);
270 		g_object_unref (priv->model);
271 		priv->model = NULL;
272 	}
273 
274 	if (priv->selection != NULL) {
275 		g_signal_handlers_disconnect_by_func (
276 			priv->selection,
277 			mail_sidebar_selection_changed_cb, object);
278 		g_object_unref (priv->selection);
279 		priv->selection = NULL;
280 	}
281 
282 	/* Chain up to parent's dispose() method. */
283 	G_OBJECT_CLASS (e_mail_sidebar_parent_class)->dispose (object);
284 }
285 
286 static void
287 mail_sidebar_row_expanded (GtkTreeView *tree_view,
288                            GtkTreeIter *unused,
289                            GtkTreePath *path)
290 {
291 	GtkTreeViewClass *tree_view_class;
292 	EMailSidebar *sidebar;
293 	GtkTreeModel *model;
294 	GKeyFile *key_file;
295 	const gchar *key;
296 	gboolean is_folder;
297 	gboolean is_store;
298 	gchar *group_name;
299 
300 	/* Chain up to parent's row_expanded() method.  Do this first
301 	 * because we stomp on the path argument a few lines down. */
302 	tree_view_class = GTK_TREE_VIEW_CLASS (e_mail_sidebar_parent_class);
303 	tree_view_class->row_expanded (tree_view, unused, path);
304 
305 	sidebar = E_MAIL_SIDEBAR (tree_view);
306 	key_file = e_mail_sidebar_get_key_file (sidebar);
307 
308 	/* Make sure we have a key file to record state changes. */
309 	if (key_file == NULL)
310 		return;
311 
312 	path = gtk_tree_path_copy (path);
313 	model = gtk_tree_view_get_model (tree_view);
314 
315 	/* Expand the node and all ancestors. */
316 	while (gtk_tree_path_get_depth (path) > 0) {
317 		CamelStore *store;
318 		GtkTreeIter iter;
319 		gchar *folder_name;
320 
321 		gtk_tree_model_get_iter (model, &iter, path);
322 
323 		gtk_tree_model_get (
324 			model, &iter,
325 			COL_POINTER_CAMEL_STORE, &store,
326 			COL_STRING_FULL_NAME, &folder_name,
327 			COL_BOOL_IS_STORE, &is_store,
328 			COL_BOOL_IS_FOLDER, &is_folder, -1);
329 
330 		g_return_if_fail (is_store || is_folder);
331 
332 		key = STATE_KEY_EXPANDED;
333 		if (is_store) {
334 			const gchar *uid;
335 
336 			uid = camel_service_get_uid (CAMEL_SERVICE (store));
337 			group_name = g_strdup_printf ("Store %s", uid);
338 		} else {
339 			gchar *uri;
340 
341 			uri = e_mail_folder_uri_build (store, folder_name);
342 			group_name = g_strdup_printf ("Folder %s", uri);
343 			g_free (uri);
344 		}
345 
346 		g_key_file_set_boolean (key_file, group_name, key, TRUE);
347 		e_mail_sidebar_key_file_changed (sidebar);
348 
349 		g_free (group_name);
350 		g_free (folder_name);
351 
352 		gtk_tree_path_up (path);
353 	}
354 
355 	gtk_tree_path_free (path);
356 }
357 
358 static void
359 mail_sidebar_row_collapsed (GtkTreeView *tree_view,
360                             GtkTreeIter *iter,
361                             GtkTreePath *path)
362 {
363 	EMailSidebar *sidebar;
364 	GtkTreeModel *model;
365 	GKeyFile *key_file;
366 	CamelStore *store;
367 	const gchar *key;
368 	gboolean is_folder;
369 	gboolean is_store;
370 	gchar *folder_name;
371 	gchar *group_name;
372 
373 	sidebar = E_MAIL_SIDEBAR (tree_view);
374 	key_file = e_mail_sidebar_get_key_file (sidebar);
375 
376 	/* Make sure we have a key file to record state changes. */
377 	if (key_file == NULL)
378 		return;
379 
380 	model = gtk_tree_view_get_model (tree_view);
381 
382 	gtk_tree_model_get (
383 		model, iter,
384 		COL_POINTER_CAMEL_STORE, &store,
385 		COL_STRING_FULL_NAME, &folder_name,
386 		COL_BOOL_IS_STORE, &is_store,
387 		COL_BOOL_IS_FOLDER, &is_folder, -1);
388 
389 	g_return_if_fail (is_store || is_folder);
390 
391 	key = STATE_KEY_EXPANDED;
392 	if (is_store) {
393 		const gchar *uid;
394 
395 		uid = camel_service_get_uid (CAMEL_SERVICE (store));
396 		group_name = g_strdup_printf ("Store %s", uid);
397 	} else {
398 		gchar *uri;
399 
400 		uri = e_mail_folder_uri_build (store, folder_name);
401 		group_name = g_strdup_printf ("Folder %s", uri);
402 		g_free (uri);
403 	}
404 
405 	g_key_file_set_boolean (key_file, group_name, key, FALSE);
406 	e_mail_sidebar_key_file_changed (sidebar);
407 
408 	g_free (group_name);
409 	g_free (folder_name);
410 }
411 
412 static guint32
413 mail_sidebar_check_state (EMailSidebar *sidebar)
414 {
415 	GtkTreeSelection *selection;
416 	GtkTreeView *tree_view;
417 	GtkTreeModel *model;
418 	GtkTreeIter iter;
419 	CamelStore *store;
420 	gchar *full_name;
421 	const gchar *uid;
422 	gboolean store_is_local;
423 	gboolean store_is_vfolder;
424 	gboolean allows_children = TRUE;
425 	gboolean can_delete = TRUE;
426 	gboolean can_disable = TRUE;
427 	gboolean is_junk = FALSE;
428 	gboolean is_outbox = FALSE;
429 	gboolean is_store;
430 	gboolean is_trash = FALSE;
431 	gboolean is_virtual = FALSE;
432 	guint32 folder_flags = 0;
433 	guint32 state = 0;
434 
435 	tree_view = GTK_TREE_VIEW (sidebar);
436 	selection = gtk_tree_view_get_selection (tree_view);
437 
438 	if (!gtk_tree_selection_get_selected (selection, &model, &iter))
439 		return 0;
440 
441 	gtk_tree_model_get (
442 		model, &iter,
443 		COL_POINTER_CAMEL_STORE, &store,
444 		COL_STRING_FULL_NAME, &full_name,
445 		COL_BOOL_IS_STORE, &is_store,
446 		COL_UINT_FLAGS, &folder_flags, -1);
447 
448 	uid = camel_service_get_uid (CAMEL_SERVICE (store));
449 	store_is_local = (g_strcmp0 (uid, E_MAIL_SESSION_LOCAL_UID) == 0);
450 	store_is_vfolder = (g_strcmp0 (uid, E_MAIL_SESSION_VFOLDER_UID) == 0);
451 
452 	/* Bit of a hack to indicate "Search Folders" is selected. */
453 	if (is_store && store_is_vfolder)
454 		is_virtual = TRUE;
455 
456 	if (!is_store && full_name != NULL) {
457 		guint32 folder_type;
458 
459 		/* Is this a virtual junk or trash folder? */
460 		is_junk = (strcmp (full_name, CAMEL_VJUNK_NAME) == 0);
461 		is_trash = (strcmp (full_name, CAMEL_VTRASH_NAME) == 0);
462 
463 		/* Is this a real trash folder?
464 		 * Used by Exchange and GroupWise accounts. */
465 		folder_type = (folder_flags & CAMEL_FOLDER_TYPE_MASK);
466 		is_trash |= (folder_type == CAMEL_FOLDER_TYPE_TRASH);
467 
468 		/* Is this a virtual folder (junk/trash/search)? */
469 		is_virtual |= (folder_flags & CAMEL_FOLDER_VIRTUAL);
470 
471 		allows_children = !(is_junk || is_trash);
472 
473 		/* Don't allow deletion of special local folders. */
474 		if (store_is_local) {
475 			can_delete =
476 				(strcmp (full_name, "Drafts") != 0) &&
477 				(strcmp (full_name, "Inbox") != 0) &&
478 				(strcmp (full_name, "Outbox") != 0) &&
479 				(strcmp (full_name, "Sent") != 0) &&
480 				(strcmp (full_name, "Templates") != 0);
481 			is_outbox =
482 				(strcmp (full_name, "Outbox") == 0);
483 		}
484 
485 		can_delete &= !(folder_flags & CAMEL_FOLDER_SYSTEM);
486 	}
487 
488 	/* GOA-based accounts cannot be disabled from Evolution. */
489 	if (is_store && !store_is_local && !store_is_vfolder) {
490 		EMFolderTree *folder_tree;
491 		EMailSession *session;
492 		ESourceRegistry *registry;
493 		ESource *source;
494 		ESource *ancestor;
495 
496 		folder_tree = EM_FOLDER_TREE (sidebar);
497 		session = em_folder_tree_get_session (folder_tree);
498 		registry = e_mail_session_get_registry (session);
499 		source = e_source_registry_ref_source (registry, uid);
500 
501 		ancestor = e_source_registry_find_extension (
502 			registry, source, E_SOURCE_EXTENSION_GOA);
503 		if (ancestor != NULL) {
504 			can_disable = FALSE;
505 			g_object_unref (ancestor);
506 		}
507 
508 		g_object_unref (source);
509 	}
510 
511 	if (allows_children)
512 		state |= E_MAIL_SIDEBAR_FOLDER_ALLOWS_CHILDREN;
513 	if (can_delete)
514 		state |= E_MAIL_SIDEBAR_FOLDER_CAN_DELETE;
515 	if (is_junk)
516 		state |= E_MAIL_SIDEBAR_FOLDER_IS_JUNK;
517 	if (is_outbox)
518 		state |= E_MAIL_SIDEBAR_FOLDER_IS_OUTBOX;
519 	if (is_store)
520 		state |= E_MAIL_SIDEBAR_FOLDER_IS_STORE;
521 	if (is_trash)
522 		state |= E_MAIL_SIDEBAR_FOLDER_IS_TRASH;
523 	if (is_virtual)
524 		state |= E_MAIL_SIDEBAR_FOLDER_IS_VIRTUAL;
525 	if (store_is_local || store_is_vfolder)
526 		state |= E_MAIL_SIDEBAR_STORE_IS_BUILTIN;
527 	if (CAMEL_IS_SUBSCRIBABLE (store))
528 		state |= E_MAIL_SIDEBAR_STORE_IS_SUBSCRIBABLE;
529 	if (can_disable)
530 		state |= E_MAIL_SIDEBAR_STORE_CAN_BE_DISABLED;
531 
532 	g_free (full_name);
533 
534 	return state;
535 }
536 
537 static void
538 e_mail_sidebar_class_init (EMailSidebarClass *class)
539 {
540 	GObjectClass *object_class;
541 	GtkTreeViewClass *tree_view_class;
542 
543 	g_type_class_add_private (class, sizeof (EMailSidebarPrivate));
544 
545 	object_class = G_OBJECT_CLASS (class);
546 	object_class->set_property = mail_sidebar_set_property;
547 	object_class->get_property = mail_sidebar_get_property;
548 	object_class->constructed = mail_sidebar_constructed;
549 	object_class->dispose = mail_sidebar_dispose;
550 
551 	tree_view_class = GTK_TREE_VIEW_CLASS (class);
552 	tree_view_class->row_expanded = mail_sidebar_row_expanded;
553 	tree_view_class->row_collapsed = mail_sidebar_row_collapsed;
554 
555 	class->check_state = mail_sidebar_check_state;
556 
557 	g_object_class_install_property (
558 		object_class,
559 		PROP_KEY_FILE,
560 		g_param_spec_pointer (
561 			"key-file",
562 			"Key File",
563 			NULL,
564 			G_PARAM_READWRITE));
565 
566 	signals[KEY_FILE_CHANGED] = g_signal_new (
567 		"key-file-changed",
568 		G_OBJECT_CLASS_TYPE (object_class),
569 		G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
570 		G_STRUCT_OFFSET (EMailSidebarClass, key_file_changed),
571 		NULL, NULL,
572 		g_cclosure_marshal_VOID__VOID,
573 		G_TYPE_NONE, 0);
574 }
575 
576 static void
577 e_mail_sidebar_init (EMailSidebar *sidebar)
578 {
579 	EMFolderTree *folder_tree;
580 
581 	sidebar->priv = E_MAIL_SIDEBAR_GET_PRIVATE (sidebar);
582 
583 	folder_tree = EM_FOLDER_TREE (sidebar);
584 	em_folder_tree_set_excluded (folder_tree, 0);
585 	em_folder_tree_enable_drag_and_drop (folder_tree);
586 }
587 
588 GtkWidget *
589 e_mail_sidebar_new (EMailSession *session,
590                     EAlertSink *alert_sink)
591 {
592 	EMFolderTreeModel *model;
593 
594 	g_return_val_if_fail (E_IS_MAIL_SESSION (session), NULL);
595 	g_return_val_if_fail (E_IS_ALERT_SINK (alert_sink), NULL);
596 
597 	model = em_folder_tree_model_get_default ();
598 
599 	return g_object_new (
600 		E_TYPE_MAIL_SIDEBAR,
601 		"alert-sink", alert_sink,
602 		"session", session,
603 		"model", model, NULL);
604 }
605 
606 GKeyFile *
607 e_mail_sidebar_get_key_file (EMailSidebar *sidebar)
608 {
609 	g_return_val_if_fail (E_IS_MAIL_SIDEBAR (sidebar), NULL);
610 
611 	return sidebar->priv->key_file;
612 }
613 
614 void
615 e_mail_sidebar_set_key_file (EMailSidebar *sidebar,
616                              GKeyFile *key_file)
617 {
618 	g_return_if_fail (E_IS_MAIL_SIDEBAR (sidebar));
619 
620 	/* XXX GKeyFile has no reference count, so all we can do is
621 	 *     replace the old pointer and hope the key file is not
622 	 *     freed on us.  Most other GLib data structures have
623 	 *     grown reference counts so maybe this should too. */
624 	sidebar->priv->key_file = key_file;
625 
626 	mail_sidebar_restore_state (sidebar);
627 
628 	g_object_notify (G_OBJECT (sidebar), "key-file");
629 }
630 
631 guint32
632 e_mail_sidebar_check_state (EMailSidebar *sidebar)
633 {
634 	EMailSidebarClass *class;
635 
636 	g_return_val_if_fail (E_IS_MAIL_SIDEBAR (sidebar), 0);
637 
638 	class = E_MAIL_SIDEBAR_GET_CLASS (sidebar);
639 	g_return_val_if_fail (class->check_state != NULL, 0);
640 
641 	return class->check_state (sidebar);
642 }
643 
644 void
645 e_mail_sidebar_key_file_changed (EMailSidebar *sidebar)
646 {
647 	g_return_if_fail (E_IS_MAIL_SIDEBAR (sidebar));
648 
649 	g_signal_emit (sidebar, signals[KEY_FILE_CHANGED], 0);
650 }