evolution-3.6.4/modules/addressbook/e-book-shell-sidebar.c

No issues found

  1 /*
  2  * e-book-shell-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-book-shell-sidebar.h"
 27 
 28 #include <string.h>
 29 #include <glib/gi18n.h>
 30 
 31 #include <e-util/e-util.h>
 32 
 33 #include "e-book-shell-view.h"
 34 #include "e-book-shell-backend.h"
 35 #include "e-addressbook-selector.h"
 36 
 37 #define E_BOOK_SHELL_SIDEBAR_GET_PRIVATE(obj) \
 38 	(G_TYPE_INSTANCE_GET_PRIVATE \
 39 	((obj), E_TYPE_BOOK_SHELL_SIDEBAR, EBookShellSidebarPrivate))
 40 
 41 struct _EBookShellSidebarPrivate {
 42 	GtkWidget *selector;
 43 };
 44 
 45 enum {
 46 	PROP_0,
 47 	PROP_SELECTOR
 48 };
 49 
 50 G_DEFINE_DYNAMIC_TYPE (
 51 	EBookShellSidebar,
 52 	e_book_shell_sidebar,
 53 	E_TYPE_SHELL_SIDEBAR)
 54 
 55 static void
 56 book_shell_sidebar_get_property (GObject *object,
 57                                  guint property_id,
 58                                  GValue *value,
 59                                  GParamSpec *pspec)
 60 {
 61 	switch (property_id) {
 62 		case PROP_SELECTOR:
 63 			g_value_set_object (
 64 				value, e_book_shell_sidebar_get_selector (
 65 				E_BOOK_SHELL_SIDEBAR (object)));
 66 			return;
 67 	}
 68 
 69 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 70 }
 71 
 72 static void
 73 book_shell_sidebar_dispose (GObject *object)
 74 {
 75 	EBookShellSidebarPrivate *priv;
 76 
 77 	priv = E_BOOK_SHELL_SIDEBAR_GET_PRIVATE (object);
 78 
 79 	if (priv->selector != NULL) {
 80 		g_object_unref (priv->selector);
 81 		priv->selector = NULL;
 82 	}
 83 
 84 	/* Chain up to parent's dispose() method. */
 85 	G_OBJECT_CLASS (e_book_shell_sidebar_parent_class)->dispose (object);
 86 }
 87 
 88 static void
 89 book_shell_sidebar_constructed (GObject *object)
 90 {
 91 	EBookShellSidebarPrivate *priv;
 92 	EShell *shell;
 93 	EShellView *shell_view;
 94 	EShellBackend *shell_backend;
 95 	EShellSidebar *shell_sidebar;
 96 	EShellSettings *shell_settings;
 97 	ESourceRegistry *registry;
 98 	GtkContainer *container;
 99 	GtkWidget *widget;
100 
101 	priv = E_BOOK_SHELL_SIDEBAR_GET_PRIVATE (object);
102 
103 	/* Chain up to parent's constructed() method. */
104 	G_OBJECT_CLASS (e_book_shell_sidebar_parent_class)->constructed (object);
105 
106 	shell_sidebar = E_SHELL_SIDEBAR (object);
107 	shell_view = e_shell_sidebar_get_shell_view (shell_sidebar);
108 	shell_backend = e_shell_view_get_shell_backend (shell_view);
109 
110 	shell = e_shell_backend_get_shell (shell_backend);
111 	shell_settings = e_shell_get_shell_settings (shell);
112 
113 	container = GTK_CONTAINER (shell_sidebar);
114 
115 	widget = gtk_scrolled_window_new (NULL, NULL);
116 	gtk_scrolled_window_set_policy (
117 		GTK_SCROLLED_WINDOW (widget),
118 		GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
119 	gtk_scrolled_window_set_shadow_type (
120 		GTK_SCROLLED_WINDOW (widget), GTK_SHADOW_IN);
121 	gtk_container_add (container, widget);
122 	gtk_widget_show (widget);
123 
124 	container = GTK_CONTAINER (widget);
125 
126 	registry = e_shell_get_registry (shell);
127 	widget = e_addressbook_selector_new (registry);
128 	gtk_container_add (GTK_CONTAINER (container), widget);
129 	priv->selector = g_object_ref (widget);
130 	gtk_widget_show (widget);
131 
132 	g_object_bind_property_full (
133 		shell_settings, "book-primary-selection",
134 		widget, "primary-selection",
135 		G_BINDING_BIDIRECTIONAL |
136 		G_BINDING_SYNC_CREATE,
137 		(GBindingTransformFunc) e_binding_transform_uid_to_source,
138 		(GBindingTransformFunc) e_binding_transform_source_to_uid,
139 		g_object_ref (registry),
140 		(GDestroyNotify) g_object_unref);
141 }
142 
143 static guint32
144 book_shell_sidebar_check_state (EShellSidebar *shell_sidebar)
145 {
146 	EBookShellSidebar *book_shell_sidebar;
147 	ESourceSelector *selector;
148 	ESourceRegistry *registry;
149 	ESource *source;
150 	gboolean is_writable = FALSE;
151 	gboolean is_removable = FALSE;
152 	gboolean is_remote_creatable = FALSE;
153 	gboolean is_remote_deletable = FALSE;
154 	gboolean in_collection = FALSE;
155 	gboolean has_primary_source = FALSE;
156 	guint32 state = 0;
157 
158 	book_shell_sidebar = E_BOOK_SHELL_SIDEBAR (shell_sidebar);
159 	selector = e_book_shell_sidebar_get_selector (book_shell_sidebar);
160 	source = e_source_selector_ref_primary_selection (selector);
161 	registry = e_source_selector_get_registry (selector);
162 
163 	if (source != NULL) {
164 		ESource *collection;
165 
166 		has_primary_source = TRUE;
167 		is_writable = e_source_get_writable (source);
168 		is_removable = e_source_get_removable (source);
169 		is_remote_creatable = e_source_get_remote_creatable (source);
170 		is_remote_deletable = e_source_get_remote_deletable (source);
171 
172 		collection = e_source_registry_find_extension (
173 			registry, source, E_SOURCE_EXTENSION_COLLECTION);
174 		if (collection != NULL) {
175 			in_collection = TRUE;
176 			g_object_unref (collection);
177 		}
178 
179 		g_object_unref (source);
180 	}
181 
182 	if (has_primary_source)
183 		state |= E_BOOK_SHELL_SIDEBAR_HAS_PRIMARY_SOURCE;
184 	if (is_writable)
185 		state |= E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_WRITABLE;
186 	if (is_removable)
187 		state |= E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOVABLE;
188 	if (is_remote_creatable)
189 		state |= E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOTE_CREATABLE;
190 	if (is_remote_deletable)
191 		state |= E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IS_REMOTE_DELETABLE;
192 	if (in_collection)
193 		state |= E_BOOK_SHELL_SIDEBAR_PRIMARY_SOURCE_IN_COLLECTION;
194 
195 	return state;
196 }
197 
198 static void
199 e_book_shell_sidebar_class_init (EBookShellSidebarClass *class)
200 {
201 	GObjectClass *object_class;
202 	EShellSidebarClass *shell_sidebar_class;
203 
204 	g_type_class_add_private (class, sizeof (EBookShellSidebarPrivate));
205 
206 	object_class = G_OBJECT_CLASS (class);
207 	object_class->get_property = book_shell_sidebar_get_property;
208 	object_class->dispose = book_shell_sidebar_dispose;
209 	object_class->constructed = book_shell_sidebar_constructed;
210 
211 	shell_sidebar_class = E_SHELL_SIDEBAR_CLASS (class);
212 	shell_sidebar_class->check_state = book_shell_sidebar_check_state;
213 
214 	g_object_class_install_property (
215 		object_class,
216 		PROP_SELECTOR,
217 		g_param_spec_object (
218 			"selector",
219 			"Source Selector Widget",
220 			"This widget displays groups of address books",
221 			E_TYPE_SOURCE_SELECTOR,
222 			G_PARAM_READABLE));
223 }
224 
225 static void
226 e_book_shell_sidebar_class_finalize (EBookShellSidebarClass *class)
227 {
228 }
229 
230 static void
231 e_book_shell_sidebar_init (EBookShellSidebar *book_shell_sidebar)
232 {
233 	book_shell_sidebar->priv =
234 		E_BOOK_SHELL_SIDEBAR_GET_PRIVATE (book_shell_sidebar);
235 
236 	/* Postpone widget construction until we have a shell view. */
237 }
238 
239 void
240 e_book_shell_sidebar_type_register (GTypeModule *type_module)
241 {
242 	/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
243 	 *     function, so we have to wrap it with a public function in
244 	 *     order to register types from a separate compilation unit. */
245 	e_book_shell_sidebar_register_type (type_module);
246 }
247 
248 GtkWidget *
249 e_book_shell_sidebar_new (EShellView *shell_view)
250 {
251 	g_return_val_if_fail (E_IS_SHELL_VIEW (shell_view), NULL);
252 
253 	return g_object_new (
254 		E_TYPE_BOOK_SHELL_SIDEBAR,
255 		"shell-view", shell_view, NULL);
256 }
257 
258 ESourceSelector *
259 e_book_shell_sidebar_get_selector (EBookShellSidebar *book_shell_sidebar)
260 {
261 	g_return_val_if_fail (
262 		E_IS_BOOK_SHELL_SIDEBAR (book_shell_sidebar), NULL);
263 
264 	return E_SOURCE_SELECTOR (book_shell_sidebar->priv->selector);
265 }