Merge miscellaneous cleanups 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 = session;
222         camel_object_ref (session);
223
224         service->status = CAMEL_SERVICE_DISCONNECTED;
225
226         return;
227
228 fail:
229         url_string = camel_url_to_string(url, CAMEL_URL_HIDE_PASSWORD);
230         camel_exception_setv (
231                 ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
232                 err, url_string);
233         g_free(url_string);
234 }
235
236 static gboolean
237 service_connect (CamelService *service,
238                  CamelException *ex)
239 {
240         /* Things like the CamelMboxStore can validly
241          * not define a connect function. */
242          return TRUE;
243 }
244
245 static gboolean
246 service_disconnect (CamelService *service,
247                     gboolean clean,
248                     CamelException *ex)
249 {
250         /* We let people get away with not having a disconnect
251          * function -- CamelMboxStore, for example. */
252         return TRUE;
253 }
254
255 static void
256 service_cancel_connect (CamelService *service)
257 {
258         camel_operation_cancel (service->connect_op);
259 }
260
261 static GList *
262 service_query_auth_types (CamelService *service,
263                           CamelException *ex)
264 {
265         return NULL;
266 }
267
268 static gchar *
269 service_get_name (CamelService *service,
270                   gboolean brief)
271 {
272         g_warning (
273                 "%s does not implement CamelServiceClass::get_name()",
274                 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
275
276         return g_strdup (camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
277 }
278
279 static gchar *
280 service_get_path (CamelService *service)
281 {
282         CamelProvider *prov = service->provider;
283         CamelURL *url = service->url;
284         GString *gpath;
285         gchar *path;
286
287         /* A sort of ad-hoc default implementation that works for our
288          * current set of services.
289          */
290
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 : "");
297
298                         if (url->port)
299                                 g_string_append_printf (gpath, ":%d", url->port);
300                 } else {
301                         g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
302                                                 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
303                 }
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 : "");
308
309                 if (url->port)
310                         g_string_append_printf (gpath, ":%d", url->port);
311         }
312
313         if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
314                 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
315
316         path = gpath->str;
317         g_string_free (gpath, FALSE);
318
319         return path;
320 }
321
322 static void
323 camel_service_class_init (CamelServiceClass *class)
324 {
325         CamelObjectClass *camel_object_class;
326
327         camel_service_parent_class = camel_type_get_global_classfuncs (CAMEL_TYPE_OBJECT);
328
329         camel_object_class = CAMEL_OBJECT_CLASS (class);
330         camel_object_class->setv = service_setv;
331         camel_object_class->getv = service_getv;
332
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;
340 }
341
342 static void
343 camel_service_init (CamelService *service)
344 {
345         service->priv = g_malloc0(sizeof(*service->priv));
346
347         g_static_rec_mutex_init (&service->priv->connect_lock);
348         g_static_mutex_init (&service->priv->connect_op_lock);
349 }
350
351 CamelType
352 camel_service_get_type (void)
353 {
354         static CamelType type = CAMEL_INVALID_TYPE;
355
356         if (type == CAMEL_INVALID_TYPE) {
357                 type =
358                         camel_type_register (CAMEL_TYPE_OBJECT,
359                                              "CamelService",
360                                              sizeof (CamelService),
361                                              sizeof (CamelServiceClass),
362                                              (CamelObjectClassInitFunc) camel_service_class_init,
363                                              NULL,
364                                              (CamelObjectInitFunc) camel_service_init,
365                                              camel_service_finalize );
366         }
367
368         return type;
369 }
370
371 /**
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
378  *
379  * Constructs a #CamelService initialized with the given parameters.
380  **/
381 void
382 camel_service_construct (CamelService *service,
383                          CamelSession *session,
384                          CamelProvider *provider,
385                          CamelURL *url,
386                          CamelException *ex)
387 {
388         CamelServiceClass *class;
389
390         g_return_if_fail (CAMEL_IS_SERVICE (service));
391         g_return_if_fail (CAMEL_IS_SESSION (session));
392
393         class = CAMEL_SERVICE_GET_CLASS (service);
394         g_return_if_fail (class->construct != NULL);
395
396         class->construct (service, session, provider, url, ex);
397 }
398
399 /**
400  * camel_service_connect:
401  * @service: a #CamelService object
402  * @ex: a #CamelException
403  *
404  * Connect to the service using the parameters it was initialized
405  * with.
406  *
407  * Returns: %TRUE if the connection is made or %FALSE otherwise
408  **/
409 gboolean
410 camel_service_connect (CamelService *service,
411                        CamelException *ex)
412 {
413         CamelServiceClass *class;
414         gboolean ret = FALSE;
415         gboolean unreg = FALSE;
416         CamelOperation *connect_op;
417
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);
421
422         class = CAMEL_SERVICE_GET_CLASS (service);
423         g_return_val_if_fail (class->connect != NULL, FALSE);
424
425         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
426
427         if (service->status == CAMEL_SERVICE_CONNECTED) {
428                 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
429                 return TRUE;
430         }
431
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);
439                 unreg = TRUE;
440         }
441         connect_op = service->connect_op;
442         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
443
444         service->status = CAMEL_SERVICE_CONNECTING;
445         ret = class->connect (service, ex);
446         service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
447
448         CAMEL_SERVICE_LOCK (service, connect_op_lock);
449         if (connect_op) {
450                 if (unreg && service->connect_op)
451                         camel_operation_unregister (connect_op);
452
453                 camel_operation_unref (connect_op);
454                 service->connect_op = NULL;
455         }
456         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
457
458         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
459
460         return ret;
461 }
462
463 /**
464  * camel_service_disconnect:
465  * @service: a #CamelService object
466  * @clean: whether or not to try to disconnect cleanly
467  * @ex: a #CamelException
468  *
469  * Disconnect from the service. If @clean is %FALSE, it should not
470  * try to do any synchronizing or other cleanup of the connection.
471  *
472  * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
473  **/
474 gboolean
475 camel_service_disconnect (CamelService *service,
476                           gboolean clean,
477                           CamelException *ex)
478 {
479         CamelServiceClass *class;
480         gboolean res = TRUE;
481         gint unreg = FALSE;
482
483         g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
484
485         class = CAMEL_SERVICE_GET_CLASS (service);
486         g_return_val_if_fail (class->disconnect != NULL, FALSE);
487
488         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
489
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);
497                         unreg = TRUE;
498                 }
499                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
500
501                 service->status = CAMEL_SERVICE_DISCONNECTING;
502                 res = class->disconnect (service, clean, ex);
503                 service->status = CAMEL_SERVICE_DISCONNECTED;
504
505                 CAMEL_SERVICE_LOCK (service, connect_op_lock);
506                 if (unreg)
507                         camel_operation_unregister (service->connect_op);
508
509                 camel_operation_unref (service->connect_op);
510                 service->connect_op = NULL;
511                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
512         }
513
514         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
515
516                 service->status = CAMEL_SERVICE_DISCONNECTED;
517         return res;
518 }
519
520 /**
521  * camel_service_cancel_connect:
522  * @service: a #CamelService object
523  *
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
526  * no-op.
527  **/
528 void
529 camel_service_cancel_connect (CamelService *service)
530 {
531         CamelServiceClass *class;
532
533         g_return_if_fail (CAMEL_IS_SERVICE (service));
534
535         class = CAMEL_SERVICE_GET_CLASS (service);
536         g_return_if_fail (class->cancel_connect != NULL);
537
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);
542 }
543
544 /**
545  * camel_service_get_url:
546  * @service: a #CamelService object
547  *
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.
551  *
552  * Returns: the URL representing @service
553  **/
554 gchar *
555 camel_service_get_url (CamelService *service)
556 {
557         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
558
559         return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
560 }
561
562 /**
563  * camel_service_get_name:
564  * @service: a #CamelService object
565  * @brief: whether or not to use a briefer form
566  *
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.
571  *
572  * Returns: a description of the service which the caller must free
573  **/
574 gchar *
575 camel_service_get_name (CamelService *service,
576                         gboolean brief)
577 {
578         CamelServiceClass *class;
579
580         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
581         g_return_val_if_fail (service->url, NULL);
582
583         class = CAMEL_SERVICE_GET_CLASS (service);
584         g_return_val_if_fail (class->get_name != NULL, NULL);
585
586         return class->get_name (service, brief);
587 }
588
589 /**
590  * camel_service_get_path:
591  * @service: a #CamelService object
592  *
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
597  * provider.
598  *
599  * Returns: the path, which the caller must free
600  **/
601 gchar *
602 camel_service_get_path (CamelService *service)
603 {
604         CamelServiceClass *class;
605
606         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
607         g_return_val_if_fail (service->url, NULL);
608
609         class = CAMEL_SERVICE_GET_CLASS (service);
610         g_return_val_if_fail (class->get_path != NULL, NULL);
611
612         return class->get_path (service);
613 }
614
615 /**
616  * camel_service_get_session:
617  * @service: a #CamelService object
618  *
619  * Gets the #CamelSession associated with the service.
620  *
621  * Returns: the session
622  **/
623 CamelSession *
624 camel_service_get_session (CamelService *service)
625 {
626         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
627
628         return service->session;
629 }
630
631 /**
632  * camel_service_get_provider:
633  * @service: a #CamelService object
634  *
635  * Gets the #CamelProvider associated with the service.
636  *
637  * Returns: the provider
638  **/
639 CamelProvider *
640 camel_service_get_provider (CamelService *service)
641 {
642         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
643
644         return service->provider;
645 }
646
647 /**
648  * camel_service_query_auth_types:
649  * @service: a #CamelService object
650  * @ex: a #CamelException
651  *
652  * This is used by the mail source wizard to get the list of
653  * authentication types supported by the protocol, and information
654  * about them.
655  *
656  * Returns: a list of #CamelServiceAuthType records. The caller
657  * must free the list with #g_list_free when it is done with it.
658  **/
659 GList *
660 camel_service_query_auth_types (CamelService *service,
661                                 CamelException *ex)
662 {
663         CamelServiceClass *class;
664         GList *ret;
665
666         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
667
668         class = CAMEL_SERVICE_GET_CLASS (service);
669         g_return_val_if_fail (class->query_auth_types != NULL, NULL);
670
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);
676
677         return ret;
678 }