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 = session;
222 camel_object_ref (session);
224 service->status = CAMEL_SERVICE_DISCONNECTED;
229 url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
230 camel_exception_setv (
231 ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
237 service_connect (CamelService *service,
240 /* Things like the CamelMboxStore can validly
241 * not define a connect function. */
246 service_disconnect (CamelService *service,
250 /* We let people get away with not having a disconnect
251 * function -- CamelMboxStore, for example. */
256 service_cancel_connect (CamelService *service)
258 camel_operation_cancel (service->connect_op);
262 service_query_auth_types (CamelService *service,
269 service_get_name (CamelService *service,
273 "%s does not implement CamelServiceClass::get_name()",
274 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
276 return g_strdup (camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
280 service_get_path (CamelService *service)
282 CamelProvider *prov = service->provider;
283 CamelURL *url = service->url;
287 /* A sort of ad-hoc default implementation that works for our
288 * current set of services.
291 gpath = g_string_new (service->provider->protocol);
292 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
293 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
294 g_string_append_printf (gpath, "/%s@%s",
295 url->user ? url->user : "",
296 url->host ? url->host : "");
299 g_string_append_printf (gpath, ":%d", url->port);
301 g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
302 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
304 } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
305 g_string_append_printf (gpath, "/%s%s",
306 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
307 url->host ? url->host : "");
310 g_string_append_printf (gpath, ":%d", url->port);
313 if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
314 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
317 g_string_free (gpath, FALSE);
323 camel_service_class_init (CamelServiceClass *class)
325 CamelObjectClass *camel_object_class;
327 camel_service_parent_class = camel_type_get_global_classfuncs (CAMEL_TYPE_OBJECT);
329 camel_object_class = CAMEL_OBJECT_CLASS (class);
330 camel_object_class->setv = service_setv;
331 camel_object_class->getv = service_getv;
333 class->construct = service_construct;
334 class->connect = service_connect;
335 class->disconnect = service_disconnect;
336 class->cancel_connect = service_cancel_connect;
337 class->query_auth_types = service_query_auth_types;
338 class->get_name = service_get_name;
339 class->get_path = service_get_path;
343 camel_service_init (CamelService *service)
345 service->priv = g_malloc0(sizeof(*service->priv));
347 g_static_rec_mutex_init (&service->priv->connect_lock);
348 g_static_mutex_init (&service->priv->connect_op_lock);
352 camel_service_get_type (void)
354 static CamelType type = CAMEL_INVALID_TYPE;
356 if (type == CAMEL_INVALID_TYPE) {
358 camel_type_register (CAMEL_TYPE_OBJECT,
360 sizeof (CamelService),
361 sizeof (CamelServiceClass),
362 (CamelObjectClassInitFunc) camel_service_class_init,
364 (CamelObjectInitFunc) camel_service_init,
365 camel_service_finalize );
372 * camel_service_construct:
373 * @service: a #CamelService object
374 * @session: the #CamelSession for @service
375 * @provider: the #CamelProvider associated with @service
376 * @url: the default URL for the service (may be %NULL)
377 * @ex: a #CamelException
379 * Constructs a #CamelService initialized with the given parameters.
382 camel_service_construct (CamelService *service,
383 CamelSession *session,
384 CamelProvider *provider,
388 CamelServiceClass *class;
390 g_return_if_fail (CAMEL_IS_SERVICE (service));
391 g_return_if_fail (CAMEL_IS_SESSION (session));
393 class = CAMEL_SERVICE_GET_CLASS (service);
394 g_return_if_fail (class->construct != NULL);
396 class->construct (service, session, provider, url, ex);
400 * camel_service_connect:
401 * @service: a #CamelService object
402 * @ex: a #CamelException
404 * Connect to the service using the parameters it was initialized
407 * Returns: %TRUE if the connection is made or %FALSE otherwise
410 camel_service_connect (CamelService *service,
413 CamelServiceClass *class;
414 gboolean ret = FALSE;
415 gboolean unreg = FALSE;
416 CamelOperation *connect_op;
418 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
419 g_return_val_if_fail (service->session != NULL, FALSE);
420 g_return_val_if_fail (service->url != NULL, FALSE);
422 class = CAMEL_SERVICE_GET_CLASS (service);
423 g_return_val_if_fail (class->connect != NULL, FALSE);
425 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
427 if (service->status == CAMEL_SERVICE_CONNECTED) {
428 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
432 /* Register a separate operation for connecting, so that
433 * the offline code can cancel it. */
434 CAMEL_SERVICE_LOCK (service, connect_op_lock);
435 service->connect_op = camel_operation_registered ();
436 if (!service->connect_op) {
437 service->connect_op = camel_operation_new (NULL, NULL);
438 camel_operation_register (service->connect_op);
441 connect_op = service->connect_op;
442 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
444 service->status = CAMEL_SERVICE_CONNECTING;
445 ret = class->connect (service, ex);
446 service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
448 CAMEL_SERVICE_LOCK (service, connect_op_lock);
450 if (unreg && service->connect_op)
451 camel_operation_unregister (connect_op);
453 camel_operation_unref (connect_op);
454 service->connect_op = NULL;
456 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
458 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
464 * camel_service_disconnect:
465 * @service: a #CamelService object
466 * @clean: whether or not to try to disconnect cleanly
467 * @ex: a #CamelException
469 * Disconnect from the service. If @clean is %FALSE, it should not
470 * try to do any synchronizing or other cleanup of the connection.
472 * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
475 camel_service_disconnect (CamelService *service,
479 CamelServiceClass *class;
483 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
485 class = CAMEL_SERVICE_GET_CLASS (service);
486 g_return_val_if_fail (class->disconnect != NULL, FALSE);
488 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
490 if (service->status != CAMEL_SERVICE_DISCONNECTED
491 && service->status != CAMEL_SERVICE_DISCONNECTING) {
492 CAMEL_SERVICE_LOCK (service, connect_op_lock);
493 service->connect_op = camel_operation_registered ();
494 if (!service->connect_op) {
495 service->connect_op = camel_operation_new (NULL, NULL);
496 camel_operation_register (service->connect_op);
499 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
501 service->status = CAMEL_SERVICE_DISCONNECTING;
502 res = class->disconnect (service, clean, ex);
503 service->status = CAMEL_SERVICE_DISCONNECTED;
505 CAMEL_SERVICE_LOCK (service, connect_op_lock);
507 camel_operation_unregister (service->connect_op);
509 camel_operation_unref (service->connect_op);
510 service->connect_op = NULL;
511 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
514 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
516 service->status = CAMEL_SERVICE_DISCONNECTED;
521 * camel_service_cancel_connect:
522 * @service: a #CamelService object
524 * If @service is currently attempting to connect to or disconnect
525 * from a server, this causes it to stop and fail. Otherwise it is a
529 camel_service_cancel_connect (CamelService *service)
531 CamelServiceClass *class;
533 g_return_if_fail (CAMEL_IS_SERVICE (service));
535 class = CAMEL_SERVICE_GET_CLASS (service);
536 g_return_if_fail (class->cancel_connect != NULL);
538 CAMEL_SERVICE_LOCK (service, connect_op_lock);
539 if (service->connect_op)
540 class->cancel_connect (service);
541 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
545 * camel_service_get_url:
546 * @service: a #CamelService object
548 * Gets the URL representing @service. The returned URL must be
549 * freed when it is no longer needed. For security reasons, this
550 * routine does not return the password.
552 * Returns: the URL representing @service
555 camel_service_get_url (CamelService *service)
557 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
559 return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
563 * camel_service_get_name:
564 * @service: a #CamelService object
565 * @brief: whether or not to use a briefer form
567 * This gets the name of the service in a "friendly" (suitable for
568 * humans) form. If @brief is %TRUE, this should be a brief description
569 * such as for use in the folder tree. If @brief is %FALSE, it should
570 * be a more complete and mostly unambiguous description.
572 * Returns: a description of the service which the caller must free
575 camel_service_get_name (CamelService *service,
578 CamelServiceClass *class;
580 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
581 g_return_val_if_fail (service->url, NULL);
583 class = CAMEL_SERVICE_GET_CLASS (service);
584 g_return_val_if_fail (class->get_name != NULL, NULL);
586 return class->get_name (service, brief);
590 * camel_service_get_path:
591 * @service: a #CamelService object
593 * This gets a valid UNIX relative path describing @service, which
594 * is guaranteed to be different from the path returned for any
595 * different service. This path MUST start with the name of the
596 * provider, followed by a "/", but after that, it is up to the
599 * Returns: the path, which the caller must free
602 camel_service_get_path (CamelService *service)
604 CamelServiceClass *class;
606 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
607 g_return_val_if_fail (service->url, NULL);
609 class = CAMEL_SERVICE_GET_CLASS (service);
610 g_return_val_if_fail (class->get_path != NULL, NULL);
612 return class->get_path (service);
616 * camel_service_get_session:
617 * @service: a #CamelService object
619 * Gets the #CamelSession associated with the service.
621 * Returns: the session
624 camel_service_get_session (CamelService *service)
626 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
628 return service->session;
632 * camel_service_get_provider:
633 * @service: a #CamelService object
635 * Gets the #CamelProvider associated with the service.
637 * Returns: the provider
640 camel_service_get_provider (CamelService *service)
642 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
644 return service->provider;
648 * camel_service_query_auth_types:
649 * @service: a #CamelService object
650 * @ex: a #CamelException
652 * This is used by the mail source wizard to get the list of
653 * authentication types supported by the protocol, and information
656 * Returns: a list of #CamelServiceAuthType records. The caller
657 * must free the list with #g_list_free when it is done with it.
660 camel_service_query_auth_types (CamelService *service,
663 CamelServiceClass *class;
666 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
668 class = CAMEL_SERVICE_GET_CLASS (service);
669 g_return_val_if_fail (class->query_auth_types != NULL, NULL);
671 /* Note that we get the connect lock here, which means the
672 * callee must not call the connect functions itself. */
673 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
674 ret = class->query_auth_types (service, ex);
675 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);