evolution-3.6.4/modules/mail/em-account-prefs.c

No issues found

  1 /*
  2  * em-account-prefs.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 /* XXX EMailAccountManager handles all the user interface stuff.
 23  *     This subclass applies policies using mailer resources that
 24  *     EMailAccountManager does not have access to.  The desire is
 25  *     to someday move account management completely out of the mailer,
 26  *     perhaps to evolution-data-server. */
 27 
 28 #ifdef HAVE_CONFIG_H
 29 #include <config.h>
 30 #endif
 31 
 32 #include "em-account-prefs.h"
 33 #include "e-mail-shell-backend.h"
 34 
 35 #include <glib/gi18n.h>
 36 
 37 #include <libevolution-utils/e-alert-dialog.h>
 38 
 39 #include <shell/e-shell.h>
 40 
 41 #include <mail/e-mail-backend.h>
 42 #include <mail/e-mail-ui-session.h>
 43 #include <mail/em-config.h>
 44 #include <mail/em-utils.h>
 45 #include <mail/mail-vfolder-ui.h>
 46 
 47 #define EM_ACCOUNT_PREFS_GET_PRIVATE(obj) \
 48 	(G_TYPE_INSTANCE_GET_PRIVATE \
 49 	((obj), EM_TYPE_ACCOUNT_PREFS, EMAccountPrefsPrivate))
 50 
 51 #define EM_ACCOUNT_PREFS_GET_PRIVATE(obj) \
 52 	(G_TYPE_INSTANCE_GET_PRIVATE \
 53 	((obj), EM_TYPE_ACCOUNT_PREFS, EMAccountPrefsPrivate))
 54 
 55 struct _EMAccountPrefsPrivate {
 56 	EMailBackend *backend;
 57 };
 58 
 59 enum {
 60 	PROP_0,
 61 	PROP_BACKEND
 62 };
 63 
 64 G_DEFINE_DYNAMIC_TYPE (
 65 	EMAccountPrefs,
 66 	em_account_prefs,
 67 	E_TYPE_MAIL_ACCOUNT_MANAGER)
 68 
 69 static void
 70 account_prefs_service_enabled_cb (EMailAccountStore *store,
 71                                   CamelService *service,
 72                                   EMAccountPrefs *prefs)
 73 {
 74 	EMailBackend *backend;
 75 	const gchar *uid;
 76 	EMailSession *session;
 77 
 78 	uid = camel_service_get_uid (service);
 79 	backend = em_account_prefs_get_backend (prefs);
 80 	session = e_mail_backend_get_session (backend);
 81 
 82 	/* FIXME Kind of a gross hack.  EMailSession doesn't have
 83 	 *       access to EMailBackend so it can't do this itself. */
 84 	if (g_strcmp0 (uid, E_MAIL_SESSION_VFOLDER_UID) == 0)
 85 		vfolder_load_storage (session);
 86 }
 87 
 88 static void
 89 account_prefs_set_backend (EMAccountPrefs *prefs,
 90                            EMailBackend *backend)
 91 {
 92 	g_return_if_fail (E_IS_MAIL_BACKEND (backend));
 93 	g_return_if_fail (prefs->priv->backend == NULL);
 94 
 95 	prefs->priv->backend = g_object_ref (backend);
 96 }
 97 
 98 static void
 99 account_prefs_set_property (GObject *object,
100                             guint property_id,
101                             const GValue *value,
102                             GParamSpec *pspec)
103 {
104 	switch (property_id) {
105 		case PROP_BACKEND:
106 			account_prefs_set_backend (
107 				EM_ACCOUNT_PREFS (object),
108 				g_value_get_object (value));
109 			return;
110 	}
111 
112 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
113 }
114 
115 static void
116 account_prefs_get_property (GObject *object,
117                             guint property_id,
118                             GValue *value,
119                             GParamSpec *pspec)
120 {
121 	switch (property_id) {
122 		case PROP_BACKEND:
123 			g_value_set_object (
124 				value,
125 				em_account_prefs_get_backend (
126 				EM_ACCOUNT_PREFS (object)));
127 			return;
128 	}
129 
130 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
131 }
132 
133 static void
134 account_prefs_dispose (GObject *object)
135 {
136 	EMAccountPrefsPrivate *priv;
137 
138 	priv = EM_ACCOUNT_PREFS_GET_PRIVATE (object);
139 
140 	if (priv->backend != NULL) {
141 		g_object_unref (priv->backend);
142 		priv->backend = NULL;
143 	}
144 
145 	/* Chain up to parent's dispose() method. */
146 	G_OBJECT_CLASS (em_account_prefs_parent_class)->dispose (object);
147 }
148 
149 static void
150 account_prefs_constructed (GObject *object)
151 {
152 	EMailAccountManager *manager;
153 	EMailAccountStore *store;
154 
155 	/* Chain up to parent's constructed() method. */
156 	G_OBJECT_CLASS (em_account_prefs_parent_class)->constructed (object);
157 
158 	manager = E_MAIL_ACCOUNT_MANAGER (object);
159 	store = e_mail_account_manager_get_store (manager);
160 
161 	g_signal_connect (
162 		store, "service-enabled",
163 		G_CALLBACK (account_prefs_service_enabled_cb), manager);
164 }
165 
166 static void
167 account_prefs_add_account (EMailAccountManager *manager)
168 {
169 	EMAccountPrefsPrivate *priv;
170 	gpointer parent;
171 
172 	priv = EM_ACCOUNT_PREFS_GET_PRIVATE (manager);
173 
174 	parent = gtk_widget_get_toplevel (GTK_WIDGET (manager));
175 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
176 
177 	e_mail_shell_backend_new_account (
178 		E_MAIL_SHELL_BACKEND (priv->backend), parent);
179 }
180 
181 static void
182 account_prefs_edit_account (EMailAccountManager *manager,
183                             ESource *source)
184 {
185 	EMAccountPrefsPrivate *priv;
186 	gpointer parent;
187 
188 	priv = EM_ACCOUNT_PREFS_GET_PRIVATE (manager);
189 
190 	parent = gtk_widget_get_toplevel (GTK_WIDGET (manager));
191 	parent = gtk_widget_is_toplevel (parent) ? parent : NULL;
192 
193 	e_mail_shell_backend_edit_account (
194 		E_MAIL_SHELL_BACKEND (priv->backend), parent, source);
195 }
196 
197 static void
198 em_account_prefs_class_init (EMAccountPrefsClass *class)
199 {
200 	GObjectClass *object_class;
201 	EMailAccountManagerClass *account_manager_class;
202 
203 	g_type_class_add_private (class, sizeof (EMAccountPrefsPrivate));
204 
205 	object_class = G_OBJECT_CLASS (class);
206 	object_class->set_property = account_prefs_set_property;
207 	object_class->get_property = account_prefs_get_property;
208 	object_class->dispose = account_prefs_dispose;
209 	object_class->constructed = account_prefs_constructed;
210 
211 	account_manager_class = E_MAIL_ACCOUNT_MANAGER_CLASS (class);
212 	account_manager_class->add_account = account_prefs_add_account;
213 	account_manager_class->edit_account = account_prefs_edit_account;
214 
215 	g_object_class_install_property (
216 		object_class,
217 		PROP_BACKEND,
218 		g_param_spec_object (
219 			"backend",
220 			NULL,
221 			NULL,
222 			E_TYPE_MAIL_BACKEND,
223 			G_PARAM_READWRITE |
224 			G_PARAM_CONSTRUCT_ONLY));
225 }
226 
227 static void
228 em_account_prefs_class_finalize (EMAccountPrefsClass *class)
229 {
230 }
231 
232 static void
233 em_account_prefs_init (EMAccountPrefs *prefs)
234 {
235 	prefs->priv = EM_ACCOUNT_PREFS_GET_PRIVATE (prefs);
236 }
237 
238 void
239 em_account_prefs_type_register (GTypeModule *type_module)
240 {
241 	/* XXX G_DEFINE_DYNAMIC_TYPE declares a static type registration
242 	 *     function, so we have to wrap it with a public function in
243 	 *     order to register types from a separate compilation unit. */
244 	em_account_prefs_register_type (type_module);
245 }
246 
247 GtkWidget *
248 em_account_prefs_new (EPreferencesWindow *window)
249 {
250 	EShell *shell;
251 	EShellBackend *shell_backend;
252 	EMailAccountStore *account_store;
253 	EMailBackend *backend;
254 	EMailSession *session;
255 
256 	/* XXX Figure out a better way to get the mail backend. */
257 	shell = e_preferences_window_get_shell (window);
258 	shell_backend = e_shell_get_backend_by_name (shell, "mail");
259 
260 	backend = E_MAIL_BACKEND (shell_backend);
261 	session = e_mail_backend_get_session (backend);
262 	account_store = e_mail_ui_session_get_account_store (
263 		E_MAIL_UI_SESSION (session));
264 
265 	return g_object_new (
266 		EM_TYPE_ACCOUNT_PREFS,
267 		"store", account_store,
268 		"backend", backend, NULL);
269 }
270 
271 EMailBackend *
272 em_account_prefs_get_backend (EMAccountPrefs *prefs)
273 {
274 	g_return_val_if_fail (EM_IS_ACCOUNT_PREFS (prefs), NULL);
275 
276 	return prefs->priv->backend;
277 }