1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-service.c : Abstract class for an email service */
7 * Bertrand Guiheneuf <bertrand@helixcode.com>
9 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU Lesser General Public
13 * License as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
36 #include <glib/gi18n-lib.h>
38 #include "camel-exception.h"
39 #include "camel-operation.h"
40 #include "camel-private.h"
41 #include "camel-service.h"
42 #include "camel-session.h"
47 static CamelObjectClass *camel_service_parent_class = NULL;
50 camel_service_finalize (CamelObject *object)
52 CamelService *service = CAMEL_SERVICE (object);
54 if (service->status == CAMEL_SERVICE_CONNECTED) {
57 camel_exception_init (&ex);
58 CAMEL_SERVICE_GET_CLASS (service)->disconnect (service, TRUE, &ex);
59 if (camel_exception_is_set (&ex)) {
60 w(g_warning ("camel_service_finalize: silent disconnect failure: %s",
61 camel_exception_get_description (&ex)));
63 camel_exception_clear (&ex);
67 camel_url_free (service->url);
70 camel_object_unref (service->session);
72 g_static_rec_mutex_free (&service->priv->connect_lock);
73 g_static_mutex_free (&service->priv->connect_op_lock);
75 g_free (service->priv);
79 service_setv (CamelObject *object,
83 CamelService *service = (CamelService *) object;
84 CamelURL *url = service->url;
85 gboolean reconnect = FALSE;
89 for (i = 0; i < args->argc; i++) {
90 tag = args->argv[i].tag;
92 /* make sure this is an arg we're supposed to handle */
93 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
94 (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
97 if (tag == CAMEL_SERVICE_USERNAME) {
98 /* set the username */
99 if (strcmp (url->user, args->argv[i].ca_str) != 0) {
100 camel_url_set_user (url, args->argv[i].ca_str);
103 } else if (tag == CAMEL_SERVICE_AUTH) {
104 /* set the auth mechanism */
105 if (strcmp (url->authmech, args->argv[i].ca_str) != 0) {
106 camel_url_set_authmech (url, args->argv[i].ca_str);
109 } else if (tag == CAMEL_SERVICE_HOSTNAME) {
110 /* set the hostname */
111 if (strcmp (url->host, args->argv[i].ca_str) != 0) {
112 camel_url_set_host (url, args->argv[i].ca_str);
115 } else if (tag == CAMEL_SERVICE_PORT) {
117 if (url->port != args->argv[i].ca_int) {
118 camel_url_set_port (url, args->argv[i].ca_int);
121 } else if (tag == CAMEL_SERVICE_PATH) {
123 if (strcmp (url->path, args->argv[i].ca_str) != 0) {
124 camel_url_set_path (url, args->argv[i].ca_str);
132 /* let our parent know that we've handled this arg */
133 camel_argv_ignore (args, i);
136 /* FIXME: what if we are in the process of connecting? */
137 if (reconnect && service->status == CAMEL_SERVICE_CONNECTED) {
138 /* reconnect the service using the new URL */
139 if (camel_service_disconnect (service, TRUE, ex))
140 camel_service_connect (service, ex);
143 /* Chain up to parent's setv() method. */
144 return CAMEL_OBJECT_CLASS (camel_service_parent_class)->setv (object, ex, args);
148 service_getv (CamelObject *object,
152 CamelService *service = (CamelService *) object;
153 CamelURL *url = service->url;
157 for (i = 0; i < args->argc; i++) {
158 tag = args->argv[i].tag;
160 /* make sure this is an arg we're supposed to handle */
161 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
162 (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
166 case CAMEL_SERVICE_USERNAME:
167 /* get the username */
168 *args->argv[i].ca_str = url->user;
170 case CAMEL_SERVICE_AUTH:
171 /* get the auth mechanism */
172 *args->argv[i].ca_str = url->authmech;
174 case CAMEL_SERVICE_HOSTNAME:
175 /* get the hostname */
176 *args->argv[i].ca_str = url->host;
178 case CAMEL_SERVICE_PORT:
180 *args->argv[i].ca_int = url->port;
182 case CAMEL_SERVICE_PATH:
184 *args->argv[i].ca_str = url->path;
192 /* Chain up to parent's getv() method. */
193 return CAMEL_OBJECT_CLASS (camel_service_parent_class)->getv (object, ex, args);
197 service_construct (CamelService *service,
198 CamelSession *session,
199 CamelProvider *provider,
203 gchar *err, *url_string;
205 if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_USER) &&
206 (url->user == NULL || url->user[0] == '\0')) {
207 err = _("URL '%s' needs a username component");
209 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_HOST) &&
210 (url->host == NULL || url->host[0] == '\0')) {
211 err = _("URL '%s' needs a host component");
213 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_PATH) &&
214 (url->path == NULL || url->path[0] == '\0')) {
215 err = _("URL '%s' needs a path component");
219 service->provider = provider;
220 service->url = camel_url_copy(url);
221 service->session = camel_object_ref (session);
223 service->status = CAMEL_SERVICE_DISCONNECTED;
228 url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
229 camel_exception_setv (
230 ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
236 service_connect (CamelService *service,
239 /* Things like the CamelMboxStore can validly
240 * not define a connect function. */
245 service_disconnect (CamelService *service,
249 /* We let people get away with not having a disconnect
250 * function -- CamelMboxStore, for example. */
255 service_cancel_connect (CamelService *service)
257 camel_operation_cancel (service->connect_op);
261 service_query_auth_types (CamelService *service,
268 service_get_name (CamelService *service,
272 "%s does not implement CamelServiceClass::get_name()",
273 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
275 return g_strdup (camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
279 service_get_path (CamelService *service)
281 CamelProvider *prov = service->provider;
282 CamelURL *url = service->url;
286 /* A sort of ad-hoc default implementation that works for our
287 * current set of services.
290 gpath = g_string_new (service->provider->protocol);
291 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
292 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
293 g_string_append_printf (gpath, "/%s@%s",
294 url->user ? url->user : "",
295 url->host ? url->host : "");
298 g_string_append_printf (gpath, ":%d", url->port);
300 g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
301 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
303 } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
304 g_string_append_printf (gpath, "/%s%s",
305 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
306 url->host ? url->host : "");
309 g_string_append_printf (gpath, ":%d", url->port);
312 if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
313 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
316 g_string_free (gpath, FALSE);
322 camel_service_class_init (CamelServiceClass *class)
324 CamelObjectClass *camel_object_class;
326 camel_service_parent_class = camel_type_get_global_classfuncs (CAMEL_TYPE_OBJECT);
328 camel_object_class = CAMEL_OBJECT_CLASS (class);
329 camel_object_class->setv = service_setv;
330 camel_object_class->getv = service_getv;
332 class->construct = service_construct;
333 class->connect = service_connect;
334 class->disconnect = service_disconnect;
335 class->cancel_connect = service_cancel_connect;
336 class->query_auth_types = service_query_auth_types;
337 class->get_name = service_get_name;
338 class->get_path = service_get_path;
342 camel_service_init (CamelService *service)
344 service->priv = g_malloc0(sizeof(*service->priv));
346 g_static_rec_mutex_init (&service->priv->connect_lock);
347 g_static_mutex_init (&service->priv->connect_op_lock);
351 camel_service_get_type (void)
353 static CamelType type = CAMEL_INVALID_TYPE;
355 if (type == CAMEL_INVALID_TYPE) {
357 camel_type_register (CAMEL_TYPE_OBJECT,
359 sizeof (CamelService),
360 sizeof (CamelServiceClass),
361 (CamelObjectClassInitFunc) camel_service_class_init,
363 (CamelObjectInitFunc) camel_service_init,
364 camel_service_finalize );
371 * camel_service_construct:
372 * @service: a #CamelService object
373 * @session: the #CamelSession for @service
374 * @provider: the #CamelProvider associated with @service
375 * @url: the default URL for the service (may be %NULL)
376 * @ex: a #CamelException
378 * Constructs a #CamelService initialized with the given parameters.
381 camel_service_construct (CamelService *service,
382 CamelSession *session,
383 CamelProvider *provider,
387 CamelServiceClass *class;
389 g_return_if_fail (CAMEL_IS_SERVICE (service));
390 g_return_if_fail (CAMEL_IS_SESSION (session));
392 class = CAMEL_SERVICE_GET_CLASS (service);
393 g_return_if_fail (class->construct != NULL);
395 class->construct (service, session, provider, url, ex);
399 * camel_service_connect:
400 * @service: a #CamelService object
401 * @ex: a #CamelException
403 * Connect to the service using the parameters it was initialized
406 * Returns: %TRUE if the connection is made or %FALSE otherwise
409 camel_service_connect (CamelService *service,
412 CamelServiceClass *class;
413 gboolean ret = FALSE;
414 gboolean unreg = FALSE;
415 CamelOperation *connect_op;
417 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
418 g_return_val_if_fail (service->session != NULL, FALSE);
419 g_return_val_if_fail (service->url != NULL, FALSE);
421 class = CAMEL_SERVICE_GET_CLASS (service);
422 g_return_val_if_fail (class->connect != NULL, FALSE);
424 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
426 if (service->status == CAMEL_SERVICE_CONNECTED) {
427 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
431 /* Register a separate operation for connecting, so that
432 * the offline code can cancel it. */
433 CAMEL_SERVICE_LOCK (service, connect_op_lock);
434 service->connect_op = camel_operation_registered ();
435 if (!service->connect_op) {
436 service->connect_op = camel_operation_new (NULL, NULL);
437 camel_operation_register (service->connect_op);
440 connect_op = service->connect_op;
441 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
443 service->status = CAMEL_SERVICE_CONNECTING;
444 ret = class->connect (service, ex);
445 service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
447 CAMEL_SERVICE_LOCK (service, connect_op_lock);
449 if (unreg && service->connect_op)
450 camel_operation_unregister (connect_op);
452 camel_operation_unref (connect_op);
453 service->connect_op = NULL;
455 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
457 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
463 * camel_service_disconnect:
464 * @service: a #CamelService object
465 * @clean: whether or not to try to disconnect cleanly
466 * @ex: a #CamelException
468 * Disconnect from the service. If @clean is %FALSE, it should not
469 * try to do any synchronizing or other cleanup of the connection.
471 * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
474 camel_service_disconnect (CamelService *service,
478 CamelServiceClass *class;
482 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
484 class = CAMEL_SERVICE_GET_CLASS (service);
485 g_return_val_if_fail (class->disconnect != NULL, FALSE);
487 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
489 if (service->status != CAMEL_SERVICE_DISCONNECTED
490 && service->status != CAMEL_SERVICE_DISCONNECTING) {
491 CAMEL_SERVICE_LOCK (service, connect_op_lock);
492 service->connect_op = camel_operation_registered ();
493 if (!service->connect_op) {
494 service->connect_op = camel_operation_new (NULL, NULL);
495 camel_operation_register (service->connect_op);
498 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
500 service->status = CAMEL_SERVICE_DISCONNECTING;
501 res = class->disconnect (service, clean, ex);
502 service->status = CAMEL_SERVICE_DISCONNECTED;
504 CAMEL_SERVICE_LOCK (service, connect_op_lock);
506 camel_operation_unregister (service->connect_op);
508 camel_operation_unref (service->connect_op);
509 service->connect_op = NULL;
510 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
513 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
515 service->status = CAMEL_SERVICE_DISCONNECTED;
520 * camel_service_cancel_connect:
521 * @service: a #CamelService object
523 * If @service is currently attempting to connect to or disconnect
524 * from a server, this causes it to stop and fail. Otherwise it is a
528 camel_service_cancel_connect (CamelService *service)
530 CamelServiceClass *class;
532 g_return_if_fail (CAMEL_IS_SERVICE (service));
534 class = CAMEL_SERVICE_GET_CLASS (service);
535 g_return_if_fail (class->cancel_connect != NULL);
537 CAMEL_SERVICE_LOCK (service, connect_op_lock);
538 if (service->connect_op)
539 class->cancel_connect (service);
540 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
544 * camel_service_get_url:
545 * @service: a #CamelService object
547 * Gets the URL representing @service. The returned URL must be
548 * freed when it is no longer needed. For security reasons, this
549 * routine does not return the password.
551 * Returns: the URL representing @service
554 camel_service_get_url (CamelService *service)
556 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
558 return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
562 * camel_service_get_name:
563 * @service: a #CamelService object
564 * @brief: whether or not to use a briefer form
566 * This gets the name of the service in a "friendly" (suitable for
567 * humans) form. If @brief is %TRUE, this should be a brief description
568 * such as for use in the folder tree. If @brief is %FALSE, it should
569 * be a more complete and mostly unambiguous description.
571 * Returns: a description of the service which the caller must free
574 camel_service_get_name (CamelService *service,
577 CamelServiceClass *class;
579 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
580 g_return_val_if_fail (service->url, NULL);
582 class = CAMEL_SERVICE_GET_CLASS (service);
583 g_return_val_if_fail (class->get_name != NULL, NULL);
585 return class->get_name (service, brief);
589 * camel_service_get_path:
590 * @service: a #CamelService object
592 * This gets a valid UNIX relative path describing @service, which
593 * is guaranteed to be different from the path returned for any
594 * different service. This path MUST start with the name of the
595 * provider, followed by a "/", but after that, it is up to the
598 * Returns: the path, which the caller must free
601 camel_service_get_path (CamelService *service)
603 CamelServiceClass *class;
605 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
606 g_return_val_if_fail (service->url, NULL);
608 class = CAMEL_SERVICE_GET_CLASS (service);
609 g_return_val_if_fail (class->get_path != NULL, NULL);
611 return class->get_path (service);
615 * camel_service_get_session:
616 * @service: a #CamelService object
618 * Gets the #CamelSession associated with the service.
620 * Returns: the session
623 camel_service_get_session (CamelService *service)
625 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
627 return service->session;
631 * camel_service_get_provider:
632 * @service: a #CamelService object
634 * Gets the #CamelProvider associated with the service.
636 * Returns: the provider
639 camel_service_get_provider (CamelService *service)
641 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
643 return service->provider;
647 * camel_service_query_auth_types:
648 * @service: a #CamelService object
649 * @ex: a #CamelException
651 * This is used by the mail source wizard to get the list of
652 * authentication types supported by the protocol, and information
655 * Returns: a list of #CamelServiceAuthType records. The caller
656 * must free the list with #g_list_free when it is done with it.
659 camel_service_query_auth_types (CamelService *service,
662 CamelServiceClass *class;
665 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
667 class = CAMEL_SERVICE_GET_CLASS (service);
668 g_return_val_if_fail (class->query_auth_types != NULL, NULL);
670 /* Note that we get the connect lock here, which means the
671 * callee must not call the connect functions itself. */
672 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
673 ret = class->query_auth_types (service, ex);
674 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);