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
38 #include <glib/gi18n-lib.h>
40 #include "camel-exception.h"
41 #include "camel-operation.h"
42 #include "camel-private.h"
43 #include "camel-service.h"
44 #include "camel-session.h"
49 static CamelObjectClass *parent_class = NULL;
51 /* Returns the class for a CamelService */
52 #define CSERV_CLASS(so) CAMEL_SERVICE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
54 static void construct (CamelService *service, CamelSession *session,
55 CamelProvider *provider, CamelURL *url,
57 static gboolean service_connect(CamelService *service, CamelException *ex);
58 static gboolean service_disconnect(CamelService *service, gboolean clean,
60 static void cancel_connect (CamelService *service);
61 static GList *query_auth_types (CamelService *service, CamelException *ex);
62 static gchar *get_name (CamelService *service, gboolean brief);
63 static gchar *get_path (CamelService *service);
65 static gint service_setv (CamelObject *object, CamelException *ex, CamelArgV *args);
66 static gint service_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args);
70 camel_service_class_init (CamelServiceClass *camel_service_class)
72 CamelObjectClass *object_class = CAMEL_OBJECT_CLASS (camel_service_class);
74 parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
76 /* virtual method overloading */
77 object_class->setv = service_setv;
78 object_class->getv = service_getv;
80 /* virtual method definition */
81 camel_service_class->construct = construct;
82 camel_service_class->connect = service_connect;
83 camel_service_class->disconnect = service_disconnect;
84 camel_service_class->cancel_connect = cancel_connect;
85 camel_service_class->query_auth_types = query_auth_types;
86 camel_service_class->get_name = get_name;
87 camel_service_class->get_path = get_path;
91 camel_service_init (gpointer o, gpointer k)
93 CamelService *service = o;
95 service->priv = g_malloc0(sizeof(*service->priv));
96 g_static_rec_mutex_init(&service->priv->connect_lock);
97 g_static_mutex_init(&service->priv->connect_op_lock);
101 camel_service_finalize (CamelObject *object)
103 CamelService *service = CAMEL_SERVICE (object);
105 if (service->status == CAMEL_SERVICE_CONNECTED) {
108 camel_exception_init (&ex);
109 CSERV_CLASS (service)->disconnect (service, TRUE, &ex);
110 if (camel_exception_is_set (&ex)) {
111 w(g_warning ("camel_service_finalize: silent disconnect failure: %s",
112 camel_exception_get_description (&ex)));
114 camel_exception_clear (&ex);
118 camel_url_free (service->url);
119 if (service->session)
120 camel_object_unref (service->session);
122 g_static_rec_mutex_free (&service->priv->connect_lock);
123 g_static_mutex_free (&service->priv->connect_op_lock);
125 g_free (service->priv);
131 camel_service_get_type (void)
133 static CamelType type = CAMEL_INVALID_TYPE;
135 if (type == CAMEL_INVALID_TYPE) {
137 camel_type_register (CAMEL_OBJECT_TYPE,
139 sizeof (CamelService),
140 sizeof (CamelServiceClass),
141 (CamelObjectClassInitFunc) camel_service_class_init,
143 (CamelObjectInitFunc) camel_service_init,
144 camel_service_finalize );
152 service_setv (CamelObject *object, CamelException *ex, CamelArgV *args)
154 CamelService *service = (CamelService *) object;
155 CamelURL *url = service->url;
156 gboolean reconnect = FALSE;
160 for (i = 0; i < args->argc; i++) {
161 tag = args->argv[i].tag;
163 /* make sure this is an arg we're supposed to handle */
164 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
165 (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
168 if (tag == CAMEL_SERVICE_USERNAME) {
169 /* set the username */
170 if (strcmp (url->user, args->argv[i].ca_str) != 0) {
171 camel_url_set_user (url, args->argv[i].ca_str);
174 } else if (tag == CAMEL_SERVICE_AUTH) {
175 /* set the auth mechanism */
176 if (strcmp (url->authmech, args->argv[i].ca_str) != 0) {
177 camel_url_set_authmech (url, args->argv[i].ca_str);
180 } else if (tag == CAMEL_SERVICE_HOSTNAME) {
181 /* set the hostname */
182 if (strcmp (url->host, args->argv[i].ca_str) != 0) {
183 camel_url_set_host (url, args->argv[i].ca_str);
186 } else if (tag == CAMEL_SERVICE_PORT) {
188 if (url->port != args->argv[i].ca_int) {
189 camel_url_set_port (url, args->argv[i].ca_int);
192 } else if (tag == CAMEL_SERVICE_PATH) {
194 if (strcmp (url->path, args->argv[i].ca_str) != 0) {
195 camel_url_set_path (url, args->argv[i].ca_str);
203 /* let our parent know that we've handled this arg */
204 camel_argv_ignore (args, i);
207 /* FIXME: what if we are in the process of connecting? */
208 if (reconnect && service->status == CAMEL_SERVICE_CONNECTED) {
209 /* reconnect the service using the new URL */
210 if (camel_service_disconnect (service, TRUE, ex))
211 camel_service_connect (service, ex);
214 return CAMEL_OBJECT_CLASS (parent_class)->setv (object, ex, args);
218 service_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args)
220 CamelService *service = (CamelService *) object;
221 CamelURL *url = service->url;
225 for (i = 0; i < args->argc; i++) {
226 tag = args->argv[i].tag;
228 /* make sure this is an arg we're supposed to handle */
229 if ((tag & CAMEL_ARG_TAG) <= CAMEL_SERVICE_ARG_FIRST ||
230 (tag & CAMEL_ARG_TAG) >= CAMEL_SERVICE_ARG_FIRST + 100)
234 case CAMEL_SERVICE_USERNAME:
235 /* get the username */
236 *args->argv[i].ca_str = url->user;
238 case CAMEL_SERVICE_AUTH:
239 /* get the auth mechanism */
240 *args->argv[i].ca_str = url->authmech;
242 case CAMEL_SERVICE_HOSTNAME:
243 /* get the hostname */
244 *args->argv[i].ca_str = url->host;
246 case CAMEL_SERVICE_PORT:
248 *args->argv[i].ca_int = url->port;
250 case CAMEL_SERVICE_PATH:
252 *args->argv[i].ca_str = url->path;
260 return CAMEL_OBJECT_CLASS (parent_class)->getv (object, ex, args);
264 construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
266 gchar *err, *url_string;
268 if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_USER) &&
269 (url->user == NULL || url->user[0] == '\0')) {
270 err = _("URL '%s' needs a username component");
272 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_HOST) &&
273 (url->host == NULL || url->host[0] == '\0')) {
274 err = _("URL '%s' needs a host component");
276 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_PATH) &&
277 (url->path == NULL || url->path[0] == '\0')) {
278 err = _("URL '%s' needs a path component");
282 service->provider = provider;
283 service->url = camel_url_copy(url);
284 service->session = session;
285 camel_object_ref (session);
287 service->status = CAMEL_SERVICE_DISCONNECTED;
292 url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
293 camel_exception_setv(ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID, err, url_string);
298 * camel_service_construct:
299 * @service: a #CamelService object
300 * @session: the #CamelSession for @service
301 * @provider: the #CamelProvider associated with @service
302 * @url: the default URL for the service (may be %NULL)
303 * @ex: a #CamelException
305 * Constructs a #CamelService initialized with the given parameters.
308 camel_service_construct (CamelService *service, CamelSession *session,
309 CamelProvider *provider, CamelURL *url,
312 g_return_if_fail (CAMEL_IS_SERVICE (service));
313 g_return_if_fail (CAMEL_IS_SESSION (session));
315 CSERV_CLASS (service)->construct (service, session, provider, url, ex);
320 service_connect (CamelService *service, CamelException *ex)
322 /* Things like the CamelMboxStore can validly
323 * not define a connect function.
330 * camel_service_connect:
331 * @service: a #CamelService object
332 * @ex: a #CamelException
334 * Connect to the service using the parameters it was initialized
337 * Returns: %TRUE if the connection is made or %FALSE otherwise
340 camel_service_connect (CamelService *service, CamelException *ex)
342 gboolean ret = FALSE;
343 gboolean unreg = FALSE;
344 CamelOperation *connect_op;
346 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
347 g_return_val_if_fail (service->session != NULL, FALSE);
348 g_return_val_if_fail (service->url != NULL, FALSE);
350 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
352 if (service->status == CAMEL_SERVICE_CONNECTED) {
353 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
357 /* Register a separate operation for connecting, so that
358 * the offline code can cancel it.
360 CAMEL_SERVICE_LOCK (service, connect_op_lock);
361 service->connect_op = camel_operation_registered ();
362 if (!service->connect_op) {
363 service->connect_op = camel_operation_new (NULL, NULL);
364 camel_operation_register (service->connect_op);
367 connect_op = service->connect_op;
368 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
370 service->status = CAMEL_SERVICE_CONNECTING;
371 ret = CSERV_CLASS (service)->connect (service, ex);
372 service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
374 CAMEL_SERVICE_LOCK (service, connect_op_lock);
376 if (unreg && service->connect_op)
377 camel_operation_unregister (connect_op);
379 camel_operation_unref (connect_op);
380 service->connect_op = NULL;
382 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
384 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
390 service_disconnect (CamelService *service, gboolean clean, CamelException *ex)
392 /*service->connect_level--;*/
394 /* We let people get away with not having a disconnect
395 * function -- CamelMboxStore, for example.
403 * camel_service_disconnect:
404 * @service: a #CamelService object
405 * @clean: whether or not to try to disconnect cleanly
406 * @ex: a #CamelException
408 * Disconnect from the service. If @clean is %FALSE, it should not
409 * try to do any synchronizing or other cleanup of the connection.
411 * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
414 camel_service_disconnect (CamelService *service, gboolean clean,
420 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
422 if (service->status != CAMEL_SERVICE_DISCONNECTED
423 && service->status != CAMEL_SERVICE_DISCONNECTING) {
424 CAMEL_SERVICE_LOCK (service, connect_op_lock);
425 service->connect_op = camel_operation_registered ();
426 if (!service->connect_op) {
427 service->connect_op = camel_operation_new (NULL, NULL);
428 camel_operation_register (service->connect_op);
431 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
433 service->status = CAMEL_SERVICE_DISCONNECTING;
434 res = CSERV_CLASS (service)->disconnect (service, clean, ex);
435 service->status = CAMEL_SERVICE_DISCONNECTED;
437 CAMEL_SERVICE_LOCK (service, connect_op_lock);
439 camel_operation_unregister (service->connect_op);
441 camel_operation_unref (service->connect_op);
442 service->connect_op = NULL;
443 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
446 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
448 service->status = CAMEL_SERVICE_DISCONNECTED;
453 cancel_connect (CamelService *service)
455 camel_operation_cancel (service->connect_op);
459 * camel_service_cancel_connect:
460 * @service: a #CamelService object
462 * If @service is currently attempting to connect to or disconnect
463 * from a server, this causes it to stop and fail. Otherwise it is a
467 camel_service_cancel_connect (CamelService *service)
469 CAMEL_SERVICE_LOCK (service, connect_op_lock);
470 if (service->connect_op)
471 CSERV_CLASS (service)->cancel_connect (service);
472 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
476 * camel_service_get_url:
477 * @service: a #CamelService object
479 * Gets the URL representing @service. The returned URL must be
480 * freed when it is no longer needed. For security reasons, this
481 * routine does not return the password.
483 * Returns: the URL representing @service
486 camel_service_get_url (CamelService *service)
488 return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
493 get_name (CamelService *service, gboolean brief)
495 w(g_warning ("CamelService::get_name not implemented for '%s'",
496 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service))));
497 return g_strdup ("???");
502 * camel_service_get_name:
503 * @service: a #CamelService object
504 * @brief: whether or not to use a briefer form
506 * This gets the name of the service in a "friendly" (suitable for
507 * humans) form. If @brief is %TRUE, this should be a brief description
508 * such as for use in the folder tree. If @brief is %FALSE, it should
509 * be a more complete and mostly unambiguous description.
511 * Returns: a description of the service which the caller must free
514 camel_service_get_name (CamelService *service, gboolean brief)
516 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
517 g_return_val_if_fail (service->url, NULL);
519 return CSERV_CLASS (service)->get_name (service, brief);
524 get_path (CamelService *service)
526 CamelProvider *prov = service->provider;
527 CamelURL *url = service->url;
531 /* A sort of ad-hoc default implementation that works for our
532 * current set of services.
535 gpath = g_string_new (service->provider->protocol);
536 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
537 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
538 g_string_append_printf (gpath, "/%s@%s",
539 url->user ? url->user : "",
540 url->host ? url->host : "");
543 g_string_append_printf (gpath, ":%d", url->port);
545 g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
546 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
548 } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
549 g_string_append_printf (gpath, "/%s%s",
550 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
551 url->host ? url->host : "");
554 g_string_append_printf (gpath, ":%d", url->port);
557 if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
558 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
561 g_string_free (gpath, FALSE);
567 * camel_service_get_path:
568 * @service: a #CamelService object
570 * This gets a valid UNIX relative path describing @service, which
571 * is guaranteed to be different from the path returned for any
572 * different service. This path MUST start with the name of the
573 * provider, followed by a "/", but after that, it is up to the
576 * Returns: the path, which the caller must free
579 camel_service_get_path (CamelService *service)
581 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
582 g_return_val_if_fail (service->url, NULL);
584 return CSERV_CLASS (service)->get_path (service);
589 * camel_service_get_session:
590 * @service: a #CamelService object
592 * Gets the #CamelSession associated with the service.
594 * Returns: the session
597 camel_service_get_session (CamelService *service)
599 return service->session;
604 * camel_service_get_provider:
605 * @service: a #CamelService object
607 * Gets the #CamelProvider associated with the service.
609 * Returns: the provider
612 camel_service_get_provider (CamelService *service)
614 return service->provider;
618 query_auth_types (CamelService *service, CamelException *ex)
625 * camel_service_query_auth_types:
626 * @service: a #CamelService object
627 * @ex: a #CamelException
629 * This is used by the mail source wizard to get the list of
630 * authentication types supported by the protocol, and information
633 * Returns: a list of #CamelServiceAuthType records. The caller
634 * must free the list with #g_list_free when it is done with it.
637 camel_service_query_auth_types (CamelService *service, CamelException *ex)
641 g_return_val_if_fail (service != NULL, NULL);
643 /* note that we get the connect lock here, which means the callee
644 must not call the connect functions itself */
645 CAMEL_SERVICE_REC_LOCK (service, connect_lock);
646 ret = CSERV_CLASS (service)->query_auth_types (service, ex);
647 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);