Merge some cleanup bits from camel-gobject.
[platform/upstream/evolution-data-server.git] / camel / camel-service.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-service.c : Abstract class for an email service */
3
4 /*
5  *
6  * Author :
7  *  Bertrand Guiheneuf <bertrand@helixcode.com>
8  *
9  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
10  *
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.
14  *
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.
19  *
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
23  * USA
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <ctype.h>
31 #include <errno.h>
32 #include <pthread.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include <glib/gi18n-lib.h>
37
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"
43
44 #define d(x)
45 #define w(x)
46
47 static CamelObjectClass *camel_service_parent_class = NULL;
48
49 static void
50 camel_service_finalize (CamelObject *object)
51 {
52         CamelService *service = CAMEL_SERVICE (object);
53
54         if (service->status == CAMEL_SERVICE_CONNECTED) {
55                 CamelException ex;
56
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)));
62                 }
63                 camel_exception_clear (&ex);
64         }
65
66         if (service->url)
67                 camel_url_free (service->url);
68
69         if (service->session)
70                 camel_object_unref (service->session);
71
72         g_static_rec_mutex_free (&service->priv->connect_lock);
73         g_static_mutex_free (&service->priv->connect_op_lock);
74
75         g_free (service->priv);
76 }
77
78 static gint
79 service_setv (CamelObject *object,
80               CamelException *ex,
81               CamelArgV *args)
82 {
83         CamelService *service = (CamelService *) object;
84         CamelURL *url = service->url;
85         gboolean reconnect = FALSE;
86         guint32 tag;
87         gint i;
88
89         for (i = 0; i < args->argc; i++) {
90                 tag = args->argv[i].tag;
91
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)
95                         continue;
96
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);
101                                 reconnect = TRUE;
102                         }
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);
107                                 reconnect = TRUE;
108                         }
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);
113                                 reconnect = TRUE;
114                         }
115                 } else if (tag == CAMEL_SERVICE_PORT) {
116                         /* set the port */
117                         if (url->port != args->argv[i].ca_int) {
118                                 camel_url_set_port (url, args->argv[i].ca_int);
119                                 reconnect = TRUE;
120                         }
121                 } else if (tag == CAMEL_SERVICE_PATH) {
122                         /* set the path */
123                         if (strcmp (url->path, args->argv[i].ca_str) != 0) {
124                                 camel_url_set_path (url, args->argv[i].ca_str);
125                                 reconnect = TRUE;
126                         }
127                 } else {
128                         /* error? */
129                         continue;
130                 }
131
132                 /* let our parent know that we've handled this arg */
133                 camel_argv_ignore (args, i);
134         }
135
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);
141         }
142
143         /* Chain up to parent's setv() method. */
144         return CAMEL_OBJECT_CLASS (camel_service_parent_class)->setv (object, ex, args);
145 }
146
147 static gint
148 service_getv (CamelObject *object,
149               CamelException *ex,
150               CamelArgGetV *args)
151 {
152         CamelService *service = (CamelService *) object;
153         CamelURL *url = service->url;
154         guint32 tag;
155         gint i;
156
157         for (i = 0; i < args->argc; i++) {
158                 tag = args->argv[i].tag;
159
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)
163                         continue;
164
165                 switch (tag) {
166                 case CAMEL_SERVICE_USERNAME:
167                         /* get the username */
168                         *args->argv[i].ca_str = url->user;
169                         break;
170                 case CAMEL_SERVICE_AUTH:
171                         /* get the auth mechanism */
172                         *args->argv[i].ca_str = url->authmech;
173                         break;
174                 case CAMEL_SERVICE_HOSTNAME:
175                         /* get the hostname */
176                         *args->argv[i].ca_str = url->host;
177                         break;
178                 case CAMEL_SERVICE_PORT:
179                         /* get the port */
180                         *args->argv[i].ca_int = url->port;
181                         break;
182                 case CAMEL_SERVICE_PATH:
183                         /* get the path */
184                         *args->argv[i].ca_str = url->path;
185                         break;
186                 default:
187                         /* error? */
188                         break;
189                 }
190         }
191
192         /* Chain up to parent's getv() method. */
193         return CAMEL_OBJECT_CLASS (camel_service_parent_class)->getv (object, ex, args);
194 }
195
196 static void
197 service_construct (CamelService *service,
198                    CamelSession *session,
199                    CamelProvider *provider,
200                    CamelURL *url,
201                    CamelException *ex)
202 {
203         gchar *err, *url_string;
204
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");
208                 goto fail;
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");
212                 goto fail;
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");
216                 goto fail;
217         }
218
219         service->provider = provider;
220         service->url = camel_url_copy(url);
221         service->session = camel_object_ref (session);
222
223         service->status = CAMEL_SERVICE_DISCONNECTED;
224
225         return;
226
227 fail:
228         url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
229         camel_exception_setv (
230                 ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
231                 err, url_string);
232         g_free(url_string);
233 }
234
235 static gboolean
236 service_connect (CamelService *service,
237                  CamelException *ex)
238 {
239         /* Things like the CamelMboxStore can validly
240          * not define a connect function. */
241          return TRUE;
242 }
243
244 static gboolean
245 service_disconnect (CamelService *service,
246                     gboolean clean,
247                     CamelException *ex)
248 {
249         /* We let people get away with not having a disconnect
250          * function -- CamelMboxStore, for example. */
251         return TRUE;
252 }
253
254 static void
255 service_cancel_connect (CamelService *service)
256 {
257         camel_operation_cancel (service->connect_op);
258 }
259
260 static GList *
261 service_query_auth_types (CamelService *service,
262                           CamelException *ex)
263 {
264         return NULL;
265 }
266
267 static gchar *
268 service_get_name (CamelService *service,
269                   gboolean brief)
270 {
271         g_warning (
272                 "%s does not implement CamelServiceClass::get_name()",
273                 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
274
275         return g_strdup (camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
276 }
277
278 static gchar *
279 service_get_path (CamelService *service)
280 {
281         CamelProvider *prov = service->provider;
282         CamelURL *url = service->url;
283         GString *gpath;
284         gchar *path;
285
286         /* A sort of ad-hoc default implementation that works for our
287          * current set of services.
288          */
289
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 : "");
296
297                         if (url->port)
298                                 g_string_append_printf (gpath, ":%d", url->port);
299                 } else {
300                         g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
301                                                 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
302                 }
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 : "");
307
308                 if (url->port)
309                         g_string_append_printf (gpath, ":%d", url->port);
310         }
311
312         if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
313                 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
314
315         path = gpath->str;
316         g_string_free (gpath, FALSE);
317
318         return path;
319 }
320
321 static void
322 camel_service_class_init (CamelServiceClass *class)
323 {
324         CamelObjectClass *camel_object_class;
325
326         camel_service_parent_class = camel_type_get_global_classfuncs (CAMEL_TYPE_OBJECT);
327
328         camel_object_class = CAMEL_OBJECT_CLASS (class);
329         camel_object_class->setv = service_setv;
330         camel_object_class->getv = service_getv;
331
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;
339 }
340
341 static void
342 camel_service_init (CamelService *service)
343 {
344         service->priv = g_malloc0(sizeof(*service->priv));
345
346         g_static_rec_mutex_init (&service->priv->connect_lock);
347         g_static_mutex_init (&service->priv->connect_op_lock);
348 }
349
350 CamelType
351 camel_service_get_type (void)
352 {
353         static CamelType type = CAMEL_INVALID_TYPE;
354
355         if (type == CAMEL_INVALID_TYPE) {
356                 type =
357                         camel_type_register (CAMEL_TYPE_OBJECT,
358                                              "CamelService",
359                                              sizeof (CamelService),
360                                              sizeof (CamelServiceClass),
361                                              (CamelObjectClassInitFunc) camel_service_class_init,
362                                              NULL,
363                                              (CamelObjectInitFunc) camel_service_init,
364                                              camel_service_finalize );
365         }
366
367         return type;
368 }
369
370 /**
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
377  *
378  * Constructs a #CamelService initialized with the given parameters.
379  **/
380 void
381 camel_service_construct (CamelService *service,
382                          CamelSession *session,
383                          CamelProvider *provider,
384                          CamelURL *url,
385                          CamelException *ex)
386 {
387         CamelServiceClass *class;
388
389         g_return_if_fail (CAMEL_IS_SERVICE (service));
390         g_return_if_fail (CAMEL_IS_SESSION (session));
391
392         class = CAMEL_SERVICE_GET_CLASS (service);
393         g_return_if_fail (class->construct != NULL);
394
395         class->construct (service, session, provider, url, ex);
396 }
397
398 /**
399  * camel_service_connect:
400  * @service: a #CamelService object
401  * @ex: a #CamelException
402  *
403  * Connect to the service using the parameters it was initialized
404  * with.
405  *
406  * Returns: %TRUE if the connection is made or %FALSE otherwise
407  **/
408 gboolean
409 camel_service_connect (CamelService *service,
410                        CamelException *ex)
411 {
412         CamelServiceClass *class;
413         gboolean ret = FALSE;
414         gboolean unreg = FALSE;
415         CamelOperation *connect_op;
416
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);
420
421         class = CAMEL_SERVICE_GET_CLASS (service);
422         g_return_val_if_fail (class->connect != NULL, FALSE);
423
424         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
425
426         if (service->status == CAMEL_SERVICE_CONNECTED) {
427                 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
428                 return TRUE;
429         }
430
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);
438                 unreg = TRUE;
439         }
440         connect_op = service->connect_op;
441         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
442
443         service->status = CAMEL_SERVICE_CONNECTING;
444         ret = class->connect (service, ex);
445         service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
446
447         CAMEL_SERVICE_LOCK (service, connect_op_lock);
448         if (connect_op) {
449                 if (unreg && service->connect_op)
450                         camel_operation_unregister (connect_op);
451
452                 camel_operation_unref (connect_op);
453                 service->connect_op = NULL;
454         }
455         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
456
457         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
458
459         return ret;
460 }
461
462 /**
463  * camel_service_disconnect:
464  * @service: a #CamelService object
465  * @clean: whether or not to try to disconnect cleanly
466  * @ex: a #CamelException
467  *
468  * Disconnect from the service. If @clean is %FALSE, it should not
469  * try to do any synchronizing or other cleanup of the connection.
470  *
471  * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
472  **/
473 gboolean
474 camel_service_disconnect (CamelService *service,
475                           gboolean clean,
476                           CamelException *ex)
477 {
478         CamelServiceClass *class;
479         gboolean res = TRUE;
480         gint unreg = FALSE;
481
482         g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
483
484         class = CAMEL_SERVICE_GET_CLASS (service);
485         g_return_val_if_fail (class->disconnect != NULL, FALSE);
486
487         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
488
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);
496                         unreg = TRUE;
497                 }
498                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
499
500                 service->status = CAMEL_SERVICE_DISCONNECTING;
501                 res = class->disconnect (service, clean, ex);
502                 service->status = CAMEL_SERVICE_DISCONNECTED;
503
504                 CAMEL_SERVICE_LOCK (service, connect_op_lock);
505                 if (unreg)
506                         camel_operation_unregister (service->connect_op);
507
508                 camel_operation_unref (service->connect_op);
509                 service->connect_op = NULL;
510                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
511         }
512
513         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
514
515                 service->status = CAMEL_SERVICE_DISCONNECTED;
516         return res;
517 }
518
519 /**
520  * camel_service_cancel_connect:
521  * @service: a #CamelService object
522  *
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
525  * no-op.
526  **/
527 void
528 camel_service_cancel_connect (CamelService *service)
529 {
530         CamelServiceClass *class;
531
532         g_return_if_fail (CAMEL_IS_SERVICE (service));
533
534         class = CAMEL_SERVICE_GET_CLASS (service);
535         g_return_if_fail (class->cancel_connect != NULL);
536
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);
541 }
542
543 /**
544  * camel_service_get_url:
545  * @service: a #CamelService object
546  *
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.
550  *
551  * Returns: the URL representing @service
552  **/
553 gchar *
554 camel_service_get_url (CamelService *service)
555 {
556         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
557
558         return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
559 }
560
561 /**
562  * camel_service_get_name:
563  * @service: a #CamelService object
564  * @brief: whether or not to use a briefer form
565  *
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.
570  *
571  * Returns: a description of the service which the caller must free
572  **/
573 gchar *
574 camel_service_get_name (CamelService *service,
575                         gboolean brief)
576 {
577         CamelServiceClass *class;
578
579         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
580         g_return_val_if_fail (service->url, NULL);
581
582         class = CAMEL_SERVICE_GET_CLASS (service);
583         g_return_val_if_fail (class->get_name != NULL, NULL);
584
585         return class->get_name (service, brief);
586 }
587
588 /**
589  * camel_service_get_path:
590  * @service: a #CamelService object
591  *
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
596  * provider.
597  *
598  * Returns: the path, which the caller must free
599  **/
600 gchar *
601 camel_service_get_path (CamelService *service)
602 {
603         CamelServiceClass *class;
604
605         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
606         g_return_val_if_fail (service->url, NULL);
607
608         class = CAMEL_SERVICE_GET_CLASS (service);
609         g_return_val_if_fail (class->get_path != NULL, NULL);
610
611         return class->get_path (service);
612 }
613
614 /**
615  * camel_service_get_session:
616  * @service: a #CamelService object
617  *
618  * Gets the #CamelSession associated with the service.
619  *
620  * Returns: the session
621  **/
622 CamelSession *
623 camel_service_get_session (CamelService *service)
624 {
625         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
626
627         return service->session;
628 }
629
630 /**
631  * camel_service_get_provider:
632  * @service: a #CamelService object
633  *
634  * Gets the #CamelProvider associated with the service.
635  *
636  * Returns: the provider
637  **/
638 CamelProvider *
639 camel_service_get_provider (CamelService *service)
640 {
641         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
642
643         return service->provider;
644 }
645
646 /**
647  * camel_service_query_auth_types:
648  * @service: a #CamelService object
649  * @ex: a #CamelException
650  *
651  * This is used by the mail source wizard to get the list of
652  * authentication types supported by the protocol, and information
653  * about them.
654  *
655  * Returns: a list of #CamelServiceAuthType records. The caller
656  * must free the list with #g_list_free when it is done with it.
657  **/
658 GList *
659 camel_service_query_auth_types (CamelService *service,
660                                 CamelException *ex)
661 {
662         CamelServiceClass *class;
663         GList *ret;
664
665         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
666
667         class = CAMEL_SERVICE_GET_CLASS (service);
668         g_return_val_if_fail (class->query_auth_types != NULL, NULL);
669
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);
675
676         return ret;
677 }