evolution-3.6.4/mail/e-mail-config-auth-check.c

No issues found

Incomplete coverage

Tool Failure ID Location Function Message Data
clang-analyzer no-output-found e-mail-config-auth-check.c Message(text='Unable to locate XML output from invoke-clang-analyzer') None
clang-analyzer no-output-found e-mail-config-auth-check.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-config-auth-check.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 <config.h>
 20 #include <glib/gi18n-lib.h>
 21 
 22 #include <libevolution-utils/e-alert.h>
 23 #include <e-util/e-mktemp.h>
 24 #include <misc/e-auth-combo-box.h>
 25 #include <mail/e-mail-config-service-page.h>
 26 #include "e-mail-ui-session.h"
 27 
 28 #include "e-mail-config-auth-check.h"
 29 
 30 #define E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE(obj) \
 31 	(G_TYPE_INSTANCE_GET_PRIVATE \
 32 	((obj), E_TYPE_MAIL_CONFIG_AUTH_CHECK, EMailConfigAuthCheckPrivate))
 33 
 34 typedef struct _AsyncContext AsyncContext;
 35 
 36 struct _EMailConfigAuthCheckPrivate {
 37 	EMailConfigServiceBackend *backend;
 38 	gchar *active_mechanism;
 39 
 40 	GtkWidget *combo_box;  /* not referenced */
 41 };
 42 
 43 struct _AsyncContext {
 44 	EMailConfigAuthCheck *auth_check;
 45 	CamelSession *temporary_session;
 46 	EActivity *activity;
 47 };
 48 
 49 enum {
 50 	PROP_0,
 51 	PROP_ACTIVE_MECHANISM,
 52 	PROP_BACKEND
 53 };
 54 
 55 G_DEFINE_TYPE (
 56 	EMailConfigAuthCheck,
 57 	e_mail_config_auth_check,
 58 	GTK_TYPE_BOX)
 59 
 60 static void
 61 async_context_free (AsyncContext *async_context)
 62 {
 63 	if (async_context->auth_check != NULL)
 64 		g_object_unref (async_context->auth_check);
 65 
 66 	if (async_context->temporary_session != NULL)
 67 		g_object_unref (async_context->temporary_session);
 68 
 69 	if (async_context->activity != NULL)
 70 		g_object_unref (async_context->activity);
 71 
 72 	g_slice_free (AsyncContext, async_context);
 73 }
 74 
 75 static void
 76 mail_config_auth_check_update_done_cb (GObject *source_object,
 77                                        GAsyncResult *result,
 78                                        gpointer user_data)
 79 {
 80 	AsyncContext *async_context = user_data;
 81 	EMailConfigAuthCheck *auth_check;
 82 	EAlertSink *alert_sink;
 83 	GList *available_authtypes;
 84 	GError *error = NULL;
 85 
 86 	auth_check = async_context->auth_check;
 87 	alert_sink = e_activity_get_alert_sink (async_context->activity);
 88 
 89 	available_authtypes = camel_service_query_auth_types_finish (
 90 		CAMEL_SERVICE (source_object), result, &error);
 91 
 92 	if (e_activity_handle_cancellation (async_context->activity, error)) {
 93 		g_warn_if_fail (available_authtypes == NULL);
 94 		g_error_free (error);
 95 
 96 	} else if (error != NULL) {
 97 		g_warn_if_fail (available_authtypes == NULL);
 98 		e_alert_submit (
 99 			alert_sink,
100 			"mail:checking-service-error",
101 			error->message, NULL);
102 		g_error_free (error);
103 
104 	} else {
105 		e_auth_combo_box_update_available (
106 			E_AUTH_COMBO_BOX (auth_check->priv->combo_box),
107 			available_authtypes);
108 		g_list_free (available_authtypes);
109 	}
110 
111 	gtk_widget_set_sensitive (GTK_WIDGET (auth_check), TRUE);
112 
113 	async_context_free (async_context);
114 }
115 
116 static void
117 mail_config_auth_check_update (EMailConfigAuthCheck *auth_check)
118 {
119 	EActivity *activity;
120 	EMailConfigServicePage *page;
121 	EMailConfigServiceBackend *backend;
122 	EMailConfigServicePageClass *page_class;
123 	EMailConfigServiceBackendClass *backend_class;
124 	CamelService *service;
125 	CamelSession *session;
126 	CamelSettings *settings;
127 	GCancellable *cancellable;
128 	AsyncContext *async_context;
129 	gchar *temp_dir;
130 	GError *error = NULL;
131 
132 	backend = e_mail_config_auth_check_get_backend (auth_check);
133 	page = e_mail_config_service_backend_get_page (backend);
134 	settings = e_mail_config_service_backend_get_settings (backend);
135 
136 	page_class = E_MAIL_CONFIG_SERVICE_PAGE_GET_CLASS (page);
137 	backend_class = E_MAIL_CONFIG_SERVICE_BACKEND_GET_CLASS (backend);
138 
139 	temp_dir = e_mkdtemp ("evolution-auth-check-XXXXXX");
140 
141 	/* Create a temporary session for our temporary service.
142 	 * Use the same temporary directory for "user-data-dir" and
143 	 * "user-cache-dir".  For our purposes it shouldn't matter. */
144 	session = g_object_new (
145 		CAMEL_TYPE_SESSION,
146 		"user-data-dir", temp_dir,
147 		"user-cache-dir", temp_dir,
148 		NULL);
149 
150 	/* to be able to answer for invalid/self-signed server certificates */
151 	CAMEL_SESSION_GET_CLASS (session)->alert_user = e_mail_ui_session_alert_user;
152 
153 	service = camel_session_add_service (
154 		session, "fake-uid",
155 		backend_class->backend_name,
156 		page_class->provider_type, &error);
157 
158 	g_free (temp_dir);
159 
160 	if (error != NULL) {
161 		g_warn_if_fail (service == NULL);
162 		e_alert_submit (
163 			E_ALERT_SINK (page),
164 			"mail:checking-service-error",
165 			error->message, NULL);
166 		g_error_free (error);
167 		return;
168 	}
169 
170 	g_return_if_fail (CAMEL_IS_SERVICE (service));
171 
172 	camel_service_set_settings (service, settings);
173 
174 	activity = e_mail_config_activity_page_new_activity (
175 		E_MAIL_CONFIG_ACTIVITY_PAGE (page));
176 	cancellable = e_activity_get_cancellable (activity);
177 
178 	gtk_widget_set_sensitive (GTK_WIDGET (auth_check), FALSE);
179 
180 	async_context = g_slice_new (AsyncContext);
181 	async_context->auth_check = g_object_ref (auth_check);
182 	async_context->temporary_session = session;  /* takes ownership */
183 	async_context->activity = activity;          /* takes ownership */
184 
185 	camel_service_query_auth_types (
186 		service, G_PRIORITY_DEFAULT, cancellable,
187 		mail_config_auth_check_update_done_cb, async_context);
188 
189 	g_object_unref (service);
190 }
191 
192 static void
193 mail_config_auth_check_clicked_cb (GtkButton *button,
194                                    EMailConfigAuthCheck *auth_check)
195 {
196 	mail_config_auth_check_update (auth_check);
197 }
198 
199 static void
200 mail_config_auth_check_init_mechanism (EMailConfigAuthCheck *auth_check)
201 {
202 	EMailConfigServiceBackend *backend;
203 	CamelProvider *provider;
204 	CamelSettings *settings;
205 	const gchar *auth_mechanism = NULL;
206 
207 	/* Pick an initial active mechanism name by examining both
208 	 * the corresponding CamelNetworkSettings and CamelProvider. */
209 
210 	backend = e_mail_config_auth_check_get_backend (auth_check);
211 	provider = e_mail_config_service_backend_get_provider (backend);
212 	settings = e_mail_config_service_backend_get_settings (backend);
213 	g_return_if_fail (CAMEL_IS_NETWORK_SETTINGS (settings));
214 
215 	auth_mechanism =
216 		camel_network_settings_get_auth_mechanism (
217 		CAMEL_NETWORK_SETTINGS (settings));
218 
219 	/* If CamelNetworkSettings does not have a mechanism name set,
220 	 * choose from the CamelProvider's list of supported mechanisms. */
221 	if (auth_mechanism == NULL && provider != NULL) {
222 		if (provider->authtypes != NULL) {
223 			CamelServiceAuthType *auth_type;
224 			auth_type = provider->authtypes->data;
225 			auth_mechanism = auth_type->authproto;
226 		}
227 	}
228 
229 	if (auth_mechanism != NULL)
230 		e_mail_config_auth_check_set_active_mechanism (
231 			auth_check, auth_mechanism);
232 }
233 
234 static void
235 mail_config_auth_check_set_backend (EMailConfigAuthCheck *auth_check,
236                                     EMailConfigServiceBackend *backend)
237 {
238 	g_return_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend));
239 	g_return_if_fail (auth_check->priv->backend == NULL);
240 
241 	auth_check->priv->backend = g_object_ref (backend);
242 }
243 
244 static void
245 mail_config_auth_check_set_property (GObject *object,
246                                      guint property_id,
247                                      const GValue *value,
248                                      GParamSpec *pspec)
249 {
250 	switch (property_id) {
251 		case PROP_ACTIVE_MECHANISM:
252 			e_mail_config_auth_check_set_active_mechanism (
253 				E_MAIL_CONFIG_AUTH_CHECK (object),
254 				g_value_get_string (value));
255 			return;
256 
257 		case PROP_BACKEND:
258 			mail_config_auth_check_set_backend (
259 				E_MAIL_CONFIG_AUTH_CHECK (object),
260 				g_value_get_object (value));
261 			return;
262 	}
263 
264 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
265 }
266 
267 static void
268 mail_config_auth_check_get_property (GObject *object,
269                                      guint property_id,
270                                      GValue *value,
271                                      GParamSpec *pspec)
272 {
273 	switch (property_id) {
274 		case PROP_ACTIVE_MECHANISM:
275 			g_value_set_string (
276 				value,
277 				e_mail_config_auth_check_get_active_mechanism (
278 				E_MAIL_CONFIG_AUTH_CHECK (object)));
279 			return;
280 
281 		case PROP_BACKEND:
282 			g_value_set_object (
283 				value,
284 				e_mail_config_auth_check_get_backend (
285 				E_MAIL_CONFIG_AUTH_CHECK (object)));
286 			return;
287 	}
288 
289 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
290 }
291 
292 static void
293 mail_config_auth_check_dispose (GObject *object)
294 {
295 	EMailConfigAuthCheckPrivate *priv;
296 
297 	priv = E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE (object);
298 
299 	if (priv->backend != NULL) {
300 		g_object_unref (priv->backend);
301 		priv->backend = NULL;
302 	}
303 
304 	/* Chain up to parent's dispose() method. */
305 	G_OBJECT_CLASS (e_mail_config_auth_check_parent_class)->
306 		dispose (object);
307 }
308 
309 static void
310 mail_config_auth_check_finalize (GObject *object)
311 {
312 	EMailConfigAuthCheckPrivate *priv;
313 
314 	priv = E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE (object);
315 
316 	g_free (priv->active_mechanism);
317 
318 	/* Chain up to parent's finalize() method. */
319 	G_OBJECT_CLASS (e_mail_config_auth_check_parent_class)->
320 		finalize (object);
321 }
322 
323 static void
324 mail_config_auth_check_constructed (GObject *object)
325 {
326 	EMailConfigAuthCheck *auth_check;
327 	EMailConfigServiceBackend *backend;
328 	CamelProvider *provider;
329 	GtkWidget *widget;
330 	const gchar *text;
331 
332 	/* Chain up to parent's constructed() method. */
333 	G_OBJECT_CLASS (e_mail_config_auth_check_parent_class)->
334 		constructed (object);
335 
336 	auth_check = E_MAIL_CONFIG_AUTH_CHECK (object);
337 	backend = e_mail_config_auth_check_get_backend (auth_check);
338 	provider = e_mail_config_service_backend_get_provider (backend);
339 
340 	widget = e_auth_combo_box_new ();
341 	e_auth_combo_box_set_provider (E_AUTH_COMBO_BOX (widget), provider);
342 	gtk_box_pack_start (GTK_BOX (object), widget, FALSE, FALSE, 0);
343 	auth_check->priv->combo_box = widget;  /* do not reference */
344 	gtk_widget_show (widget);
345 
346 	g_object_bind_property (
347 		widget, "active-id",
348 		auth_check, "active-mechanism",
349 		G_BINDING_BIDIRECTIONAL |
350 		G_BINDING_SYNC_CREATE);
351 
352 	text = _("Check for Supported Types");
353 	widget = gtk_button_new_with_label (text);
354 	gtk_box_pack_start (GTK_BOX (object), widget, FALSE, FALSE, 0);
355 	gtk_widget_show (widget);
356 
357 	g_signal_connect (
358 		widget, "clicked",
359 		G_CALLBACK (mail_config_auth_check_clicked_cb),
360 		auth_check);
361 
362 	mail_config_auth_check_init_mechanism (auth_check);
363 }
364 
365 static void
366 e_mail_config_auth_check_class_init (EMailConfigAuthCheckClass *class)
367 {
368 	GObjectClass *object_class;
369 
370 	g_type_class_add_private (class, sizeof (EMailConfigAuthCheckPrivate));
371 
372 	object_class = G_OBJECT_CLASS (class);
373 	object_class->set_property = mail_config_auth_check_set_property;
374 	object_class->get_property = mail_config_auth_check_get_property;
375 	object_class->dispose = mail_config_auth_check_dispose;
376 	object_class->finalize = mail_config_auth_check_finalize;
377 	object_class->constructed = mail_config_auth_check_constructed;
378 
379 	g_object_class_install_property (
380 		object_class,
381 		PROP_ACTIVE_MECHANISM,
382 		g_param_spec_string (
383 			"active-mechanism",
384 			"Active Mechanism",
385 			"Active authentication mechanism",
386 			NULL,
387 			G_PARAM_READWRITE |
388 			G_PARAM_STATIC_STRINGS));
389 
390 	g_object_class_install_property (
391 		object_class,
392 		PROP_BACKEND,
393 		g_param_spec_object (
394 			"backend",
395 			"Backend",
396 			"Mail configuration backend",
397 			E_TYPE_MAIL_CONFIG_SERVICE_BACKEND,
398 			G_PARAM_READWRITE |
399 			G_PARAM_CONSTRUCT_ONLY |
400 			G_PARAM_STATIC_STRINGS));
401 }
402 
403 static void
404 e_mail_config_auth_check_init (EMailConfigAuthCheck *auth_check)
405 {
406 	auth_check->priv = E_MAIL_CONFIG_AUTH_CHECK_GET_PRIVATE (auth_check);
407 
408 	gtk_orientable_set_orientation (
409 		GTK_ORIENTABLE (auth_check),
410 		GTK_ORIENTATION_HORIZONTAL);
411 
412 	gtk_box_set_spacing (GTK_BOX (auth_check), 6);
413 }
414 
415 GtkWidget *
416 e_mail_config_auth_check_new (EMailConfigServiceBackend *backend)
417 {
418 	g_return_val_if_fail (E_IS_MAIL_CONFIG_SERVICE_BACKEND (backend), NULL);
419 
420 	return g_object_new (
421 		E_TYPE_MAIL_CONFIG_AUTH_CHECK,
422 		"backend", backend, NULL);
423 }
424 
425 EMailConfigServiceBackend *
426 e_mail_config_auth_check_get_backend (EMailConfigAuthCheck *auth_check)
427 {
428 	g_return_val_if_fail (E_IS_MAIL_CONFIG_AUTH_CHECK (auth_check), NULL);
429 
430 	return auth_check->priv->backend;
431 }
432 
433 const gchar *
434 e_mail_config_auth_check_get_active_mechanism (EMailConfigAuthCheck *auth_check)
435 {
436 	g_return_val_if_fail (E_IS_MAIL_CONFIG_AUTH_CHECK (auth_check), NULL);
437 
438 	return auth_check->priv->active_mechanism;
439 }
440 
441 void
442 e_mail_config_auth_check_set_active_mechanism (EMailConfigAuthCheck *auth_check,
443                                                const gchar *active_mechanism)
444 {
445 	g_return_if_fail (E_IS_MAIL_CONFIG_AUTH_CHECK (auth_check));
446 
447 	if (g_strcmp0 (auth_check->priv->active_mechanism, active_mechanism) == 0)
448 		return;
449 
450 	g_free (auth_check->priv->active_mechanism);
451 	auth_check->priv->active_mechanism = g_strdup (active_mechanism);
452 
453 	g_object_notify (G_OBJECT (auth_check), "active-mechanism");
454 }