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
35 #include <glib/gi18n-lib.h>
37 #include "camel-exception.h"
38 #include "camel-operation.h"
39 #include "camel-private.h"
40 #include "camel-service.h"
41 #include "camel-session.h"
46 static CamelObjectClass *camel_service_parent_class = NULL;
49 camel_service_finalize (CamelObject *object)
51 CamelService *service = CAMEL_SERVICE (object);
53 if (service->status == CAMEL_SERVICE_CONNECTED) {
56 camel_exception_init (&ex);
57 CAMEL_SERVICE_GET_CLASS (service)->disconnect (service, TRUE, &ex);
58 if (camel_exception_is_set (&ex)) {
59 w(g_warning ("camel_service_finalize: silent disconnect failure: %s",
60 camel_exception_get_description (&ex)));
62 camel_exception_clear (&ex);
66 camel_url_free (service->url);
69 camel_object_unref (service->session);
71 g_static_rec_mutex_free (&service->priv->connect_lock);
72 g_static_mutex_free (&service->priv->connect_op_lock);
74 g_free (service->priv);
78 service_setv (CamelObject *object,
82 CamelService *service = (CamelService *) object;
83 CamelURL *url = service->url;
84 gboolean reconnect = FALSE;
88 for (i = 0; i < args->argc; i++) {
89 tag = args->argv[i].tag;
91 /* make sure this is an arg we're supposed to handle */
92 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
93 (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
96 if (tag == CAMEL_SERVICE_USERNAME) {
97 /* set the username */
98 if (strcmp (url->user, args->argv[i].ca_str) != 0) {
99 camel_url_set_user (url, args->argv[i].ca_str);
102 } else if (tag == CAMEL_SERVICE_AUTH) {
103 /* set the auth mechanism */
104 if (strcmp (url->authmech, args->argv[i].ca_str) != 0) {
105 camel_url_set_authmech (url, args->argv[i].ca_str);
108 } else if (tag == CAMEL_SERVICE_HOSTNAME) {
109 /* set the hostname */
110 if (strcmp (url->host, args->argv[i].ca_str) != 0) {
111 camel_url_set_host (url, args->argv[i].ca_str);
114 } else if (tag == CAMEL_SERVICE_PORT) {
116 if (url->port != args->argv[i].ca_int) {
117 camel_url_set_port (url, args->argv[i].ca_int);
120 } else if (tag == CAMEL_SERVICE_PATH) {
122 if (strcmp (url->path, args->argv[i].ca_str) != 0) {
123 camel_url_set_path (url, args->argv[i].ca_str);
131 /* let our parent know that we've handled this arg */
132 camel_argv_ignore (args, i);
135 /* FIXME: what if we are in the process of connecting? */
136 if (reconnect && service->status == CAMEL_SERVICE_CONNECTED) {
137 /* reconnect the service using the new URL */
138 if (camel_service_disconnect (service, TRUE, ex))
139 camel_service_connect (service, ex);
142 /* Chain up to parent's setv() method. */
143 return CAMEL_OBJECT_CLASS (camel_service_parent_class)->setv (object, ex, args);
147 service_getv (CamelObject *object,
151 CamelService *service = (CamelService *) object;
152 CamelURL *url = service->url;
156 for (i = 0; i < args->argc; i++) {
157 tag = args->argv[i].tag;
159 /* make sure this is an arg we're supposed to handle */
160 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
161 (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
165 case CAMEL_SERVICE_USERNAME:
166 /* get the username */
167 *args->argv[i].ca_str = url->user;
169 case CAMEL_SERVICE_AUTH:
170 /* get the auth mechanism */
171 *args->argv[i].ca_str = url->authmech;
173 case CAMEL_SERVICE_HOSTNAME:
174 /* get the hostname */
175 *args->argv[i].ca_str = url->host;
177 case CAMEL_SERVICE_PORT:
179 *args->argv[i].ca_int = url->port;
181 case CAMEL_SERVICE_PATH:
183 *args->argv[i].ca_str = url->path;
191 /* Chain up to parent's getv() method. */
192 return CAMEL_OBJECT_CLASS (camel_service_parent_class)->getv (object, ex, args);
196 service_construct (CamelService *service,
197 CamelSession *session,
198 CamelProvider *provider,
202 gchar *err, *url_string;
204 if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_USER) &&
205 (url->user == NULL || url->user[0] == '\0')) {
206 err = _("URL '%s' needs a username component");
208 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_HOST) &&
209 (url->host == NULL || url->host[0] == '\0')) {
210 err = _("URL '%s' needs a host component");
212 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_PATH) &&
213 (url->path == NULL || url->path[0] == '\0')) {
214 err = _("URL '%s' needs a path component");
218 service->provider = provider;
219 service->url = camel_url_copy(url);
220 service->session = camel_object_ref (session);
222 service->status = CAMEL_SERVICE_DISCONNECTED;
227 url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
228 camel_exception_setv (
229 ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
235 service_connect (CamelService *service,
238 /* Things like the CamelMboxStore can validly
239 * not define a connect function. */
244 service_disconnect (CamelService *service,
248 /* We let people get away with not having a disconnect
249 * function -- CamelMboxStore, for example. */
254 service_cancel_connect (CamelService *service)
256 camel_operation_cancel (service->connect_op);
260 service_query_auth_types (CamelService *service,
267 service_get_name (CamelService *service,
271 "%s does not implement CamelServiceClass::get_name()",
272 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
274 return g_strdup (camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
278 service_get_path (CamelService *service)
280 CamelProvider *prov = service->provider;
281 CamelURL *url = service->url;
285 /* A sort of ad-hoc default implementation that works for our
286 * current set of services.
289 gpath = g_string_new (service->provider->protocol);
290 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
291 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
292 g_string_append_printf (gpath, "/%s@%s",
293 url->user ? url->user : "",
294 url->host ? url->host : "");
297 g_string_append_printf (gpath, ":%d", url->port);
299 g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
300 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
302 } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
303 g_string_append_printf (gpath, "/%s%s",
304 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
305 url->host ? url->host : "");
308 g_string_append_printf (gpath, ":%d", url->port);
311 if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
312 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
315 g_string_free (gpath, FALSE);
321 camel_service_class_init (CamelServiceClass *class)
323 CamelObjectClass *camel_object_class;
325 camel_service_parent_class = camel_type_get_global_classfuncs (CAMEL_TYPE_OBJECT);
327 camel_object_class = CAMEL_OBJECT_CLASS (class);
328 camel_object_class->setv = service_setv;
329 camel_object_class->getv = service_getv;
331 class->construct = service_construct;
332 class->connect = service_connect;
333 class->disconnect = service_disconnect;
334 class->cancel_connect = service_cancel_connect;
335 class->query_auth_types = service_query_auth_types;
336 class->get_name = service_get_name;
337 class->get_path = service_get_path;
341 camel_service_init (CamelService *service)
343 service->priv = g_malloc0(sizeof(*service->priv));
345 g_static_rec_mutex_init (&service->priv->connect_lock);
346 g_static_mutex_init (&service->priv->connect_op_lock);
350 camel_service_get_type (void)
352 static CamelType type = CAMEL_INVALID_TYPE;
354 if (type == CAMEL_INVALID_TYPE) {
356 camel_type_register (CAMEL_TYPE_OBJECT,
358 sizeof (CamelService),
359 sizeof (CamelServiceClass),
360 (CamelObjectClassInitFunc) camel_service_class_init,
362 (CamelObjectInitFunc) camel_service_init,
363 camel_service_finalize );
370 * camel_service_construct:
371 * @service: a #CamelService object
372 * @session: the #CamelSession for @service
373 * @provider: the #CamelProvider associated with @service
374 * @url: the default URL for the service (may be %NULL)
375 * @ex: a #CamelException
377 * Constructs a #CamelService initialized with the given parameters.
380 camel_service_construct (CamelService *service,
381 CamelSession *session,
382 CamelProvider *provider,
386 CamelServiceClass *class;
388 g_return_if_fail (CAMEL_IS_SERVICE (service));
389 g_return_if_fail (CAMEL_IS_SESSION (session));
391 class = CAMEL_SERVICE_GET_CLASS (service);
392 g_return_if_fail (class->construct != NULL);
394 class->construct (service, session, provider, url, ex);
398 * camel_service_connect:
399 * @service: a #CamelService object
400 * @ex: a #CamelException
402 * Connect to the service using the parameters it was initialized
405 * Returns: %TRUE if the connection is made or %FALSE otherwise
408 camel_service_connect (CamelService *service,
411 CamelServiceClass *class;
412 gboolean ret = FALSE;
413 gboolean unreg = FALSE;
414 CamelOperation *connect_op;
416 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
417 g_return_val_if_fail (service->session != NULL, FALSE);
418 g_return_val_if_fail (service->url != NULL, FALSE);
420 class = CAMEL_SERVICE_GET_CLASS (service);
421 g_return_val_if_fail (class->connect != NULL, FALSE);
423 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
425 if (service->status == CAMEL_SERVICE_CONNECTED) {
426 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
430 /* Register a separate operation for connecting, so that
431 * the offline code can cancel it. */
432 CAMEL_SERVICE_LOCK (service, connect_op_lock);
433 service->connect_op = camel_operation_registered ();
434 if (!service->connect_op) {
435 service->connect_op = camel_operation_new (NULL, NULL);
436 camel_operation_register (service->connect_op);
439 connect_op = service->connect_op;
440 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
442 service->status = CAMEL_SERVICE_CONNECTING;
443 ret = class->connect (service, ex);
444 service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
446 CAMEL_SERVICE_LOCK (service, connect_op_lock);
448 if (unreg && service->connect_op)
449 camel_operation_unregister (connect_op);
451 camel_operation_unref (connect_op);
452 service->connect_op = NULL;
454 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
456 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
462 * camel_service_disconnect:
463 * @service: a #CamelService object
464 * @clean: whether or not to try to disconnect cleanly
465 * @ex: a #CamelException
467 * Disconnect from the service. If @clean is %FALSE, it should not
468 * try to do any synchronizing or other cleanup of the connection.
470 * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
473 camel_service_disconnect (CamelService *service,
477 CamelServiceClass *class;
481 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
483 class = CAMEL_SERVICE_GET_CLASS (service);
484 g_return_val_if_fail (class->disconnect != NULL, FALSE);
486 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
488 if (service->status != CAMEL_SERVICE_DISCONNECTED
489 && service->status != CAMEL_SERVICE_DISCONNECTING) {
490 CAMEL_SERVICE_LOCK (service, connect_op_lock);
491 service->connect_op = camel_operation_registered ();
492 if (!service->connect_op) {
493 service->connect_op = camel_operation_new (NULL, NULL);
494 camel_operation_register (service->connect_op);
497 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
499 service->status = CAMEL_SERVICE_DISCONNECTING;
500 res = class->disconnect (service, clean, ex);
501 service->status = CAMEL_SERVICE_DISCONNECTED;
503 CAMEL_SERVICE_LOCK (service, connect_op_lock);
505 camel_operation_unregister (service->connect_op);
507 camel_operation_unref (service->connect_op);
508 service->connect_op = NULL;
509 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
512 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
514 service->status = CAMEL_SERVICE_DISCONNECTED;
519 * camel_service_cancel_connect:
520 * @service: a #CamelService object
522 * If @service is currently attempting to connect to or disconnect
523 * from a server, this causes it to stop and fail. Otherwise it is a
527 camel_service_cancel_connect (CamelService *service)
529 CamelServiceClass *class;
531 g_return_if_fail (CAMEL_IS_SERVICE (service));
533 class = CAMEL_SERVICE_GET_CLASS (service);
534 g_return_if_fail (class->cancel_connect != NULL);
536 CAMEL_SERVICE_LOCK (service, connect_op_lock);
537 if (service->connect_op)
538 class->cancel_connect (service);
539 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
543 * camel_service_get_url:
544 * @service: a #CamelService object
546 * Gets the URL representing @service. The returned URL must be
547 * freed when it is no longer needed. For security reasons, this
548 * routine does not return the password.
550 * Returns: the URL representing @service
553 camel_service_get_url (CamelService *service)
555 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
557 return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
561 * camel_service_get_name:
562 * @service: a #CamelService object
563 * @brief: whether or not to use a briefer form
565 * This gets the name of the service in a "friendly" (suitable for
566 * humans) form. If @brief is %TRUE, this should be a brief description
567 * such as for use in the folder tree. If @brief is %FALSE, it should
568 * be a more complete and mostly unambiguous description.
570 * Returns: a description of the service which the caller must free
573 camel_service_get_name (CamelService *service,
576 CamelServiceClass *class;
578 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
579 g_return_val_if_fail (service->url, NULL);
581 class = CAMEL_SERVICE_GET_CLASS (service);
582 g_return_val_if_fail (class->get_name != NULL, NULL);
584 return class->get_name (service, brief);
588 * camel_service_get_path:
589 * @service: a #CamelService object
591 * This gets a valid UNIX relative path describing @service, which
592 * is guaranteed to be different from the path returned for any
593 * different service. This path MUST start with the name of the
594 * provider, followed by a "/", but after that, it is up to the
597 * Returns: the path, which the caller must free
600 camel_service_get_path (CamelService *service)
602 CamelServiceClass *class;
604 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
605 g_return_val_if_fail (service->url, NULL);
607 class = CAMEL_SERVICE_GET_CLASS (service);
608 g_return_val_if_fail (class->get_path != NULL, NULL);
610 return class->get_path (service);
614 * camel_service_get_session:
615 * @service: a #CamelService object
617 * Gets the #CamelSession associated with the service.
619 * Returns: the session
622 camel_service_get_session (CamelService *service)
624 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
626 return service->session;
630 * camel_service_get_provider:
631 * @service: a #CamelService object
633 * Gets the #CamelProvider associated with the service.
635 * Returns: the provider
638 camel_service_get_provider (CamelService *service)
640 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
642 return service->provider;
646 * camel_service_query_auth_types:
647 * @service: a #CamelService object
648 * @ex: a #CamelException
650 * This is used by the mail source wizard to get the list of
651 * authentication types supported by the protocol, and information
654 * Returns: a list of #CamelServiceAuthType records. The caller
655 * must free the list with #g_list_free when it is done with it.
658 camel_service_query_auth_types (CamelService *service,
661 CamelServiceClass *class;
664 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
666 class = CAMEL_SERVICE_GET_CLASS (service);
667 g_return_val_if_fail (class->query_auth_types != NULL, NULL);
669 /* Note that we get the connect lock here, which means the
670 * callee must not call the connect functions itself. */
671 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
672 ret = class->query_auth_types (service, ex);
673 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);