Fix compiler warnings in camel.
[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
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30
31 #include <ctype.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <glib.h>
38 #include <glib/gi18n-lib.h>
39
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"
45
46 #define d(x)
47 #define w(x)
48
49 static CamelObjectClass *parent_class = NULL;
50
51 /* Returns the class for a CamelService */
52 #define CSERV_CLASS(so) CAMEL_SERVICE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
53
54 static void construct (CamelService *service, CamelSession *session,
55                        CamelProvider *provider, CamelURL *url,
56                        CamelException *ex);
57 static gboolean service_connect(CamelService *service, CamelException *ex);
58 static gboolean service_disconnect(CamelService *service, gboolean clean,
59                                    CamelException *ex);
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);
64
65 static gint service_setv (CamelObject *object, CamelException *ex, CamelArgV *args);
66 static gint service_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args);
67
68
69 static void
70 camel_service_class_init (CamelServiceClass *camel_service_class)
71 {
72         CamelObjectClass *object_class = CAMEL_OBJECT_CLASS (camel_service_class);
73
74         parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
75
76         /* virtual method overloading */
77         object_class->setv = service_setv;
78         object_class->getv = service_getv;
79
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;
88 }
89
90 static void
91 camel_service_init (gpointer o, gpointer k)
92 {
93         CamelService *service = o;
94
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);
98 }
99
100 static void
101 camel_service_finalize (CamelObject *object)
102 {
103         CamelService *service = CAMEL_SERVICE (object);
104
105         if (service->status == CAMEL_SERVICE_CONNECTED) {
106                 CamelException ex;
107
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)));
113                 }
114                 camel_exception_clear (&ex);
115         }
116
117         if (service->url)
118                 camel_url_free (service->url);
119         if (service->session)
120                 camel_object_unref (service->session);
121
122         g_static_rec_mutex_free (&service->priv->connect_lock);
123         g_static_mutex_free (&service->priv->connect_op_lock);
124
125         g_free (service->priv);
126 }
127
128
129
130 CamelType
131 camel_service_get_type (void)
132 {
133         static CamelType type = CAMEL_INVALID_TYPE;
134
135         if (type == CAMEL_INVALID_TYPE) {
136                 type =
137                         camel_type_register (CAMEL_OBJECT_TYPE,
138                                              "CamelService",
139                                              sizeof (CamelService),
140                                              sizeof (CamelServiceClass),
141                                              (CamelObjectClassInitFunc) camel_service_class_init,
142                                              NULL,
143                                              (CamelObjectInitFunc) camel_service_init,
144                                              camel_service_finalize );
145         }
146
147         return type;
148 }
149
150
151 static gint
152 service_setv (CamelObject *object, CamelException *ex, CamelArgV *args)
153 {
154         CamelService *service = (CamelService *) object;
155         CamelURL *url = service->url;
156         gboolean reconnect = FALSE;
157         guint32 tag;
158         gint i;
159
160         for (i = 0; i < args->argc; i++) {
161                 tag = args->argv[i].tag;
162
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)
166                         continue;
167
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);
172                                 reconnect = TRUE;
173                         }
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);
178                                 reconnect = TRUE;
179                         }
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);
184                                 reconnect = TRUE;
185                         }
186                 } else if (tag == CAMEL_SERVICE_PORT) {
187                         /* set the port */
188                         if (url->port != args->argv[i].ca_int) {
189                                 camel_url_set_port (url, args->argv[i].ca_int);
190                                 reconnect = TRUE;
191                         }
192                 } else if (tag == CAMEL_SERVICE_PATH) {
193                         /* set the path */
194                         if (strcmp (url->path, args->argv[i].ca_str) != 0) {
195                                 camel_url_set_path (url, args->argv[i].ca_str);
196                                 reconnect = TRUE;
197                         }
198                 } else {
199                         /* error? */
200                         continue;
201                 }
202
203                 /* let our parent know that we've handled this arg */
204                 camel_argv_ignore (args, i);
205         }
206
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);
212         }
213
214         return CAMEL_OBJECT_CLASS (parent_class)->setv (object, ex, args);
215 }
216
217 static gint
218 service_getv (CamelObject *object, CamelException *ex, CamelArgGetV *args)
219 {
220         CamelService *service = (CamelService *) object;
221         CamelURL *url = service->url;
222         guint32 tag;
223         gint i;
224
225         for (i = 0; i < args->argc; i++) {
226                 tag = args->argv[i].tag;
227
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)
231                         continue;
232
233                 switch (tag) {
234                 case CAMEL_SERVICE_USERNAME:
235                         /* get the username */
236                         *args->argv[i].ca_str = url->user;
237                         break;
238                 case CAMEL_SERVICE_AUTH:
239                         /* get the auth mechanism */
240                         *args->argv[i].ca_str = url->authmech;
241                         break;
242                 case CAMEL_SERVICE_HOSTNAME:
243                         /* get the hostname */
244                         *args->argv[i].ca_str = url->host;
245                         break;
246                 case CAMEL_SERVICE_PORT:
247                         /* get the port */
248                         *args->argv[i].ca_int = url->port;
249                         break;
250                 case CAMEL_SERVICE_PATH:
251                         /* get the path */
252                         *args->argv[i].ca_str = url->path;
253                         break;
254                 default:
255                         /* error? */
256                         break;
257                 }
258         }
259
260         return CAMEL_OBJECT_CLASS (parent_class)->getv (object, ex, args);
261 }
262
263 static void
264 construct (CamelService *service, CamelSession *session, CamelProvider *provider, CamelURL *url, CamelException *ex)
265 {
266         gchar *err, *url_string;
267
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");
271                 goto fail;
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");
275                 goto fail;
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");
279                 goto fail;
280         }
281
282         service->provider = provider;
283         service->url = camel_url_copy(url);
284         service->session = session;
285         camel_object_ref (session);
286
287         service->status = CAMEL_SERVICE_DISCONNECTED;
288
289         return;
290
291 fail:
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);
294         g_free(url_string);
295 }
296
297 /**
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
304  *
305  * Constructs a #CamelService initialized with the given parameters.
306  **/
307 void
308 camel_service_construct (CamelService *service, CamelSession *session,
309                          CamelProvider *provider, CamelURL *url,
310                          CamelException *ex)
311 {
312         g_return_if_fail (CAMEL_IS_SERVICE (service));
313         g_return_if_fail (CAMEL_IS_SESSION (session));
314
315         CSERV_CLASS (service)->construct (service, session, provider, url, ex);
316 }
317
318
319 static gboolean
320 service_connect (CamelService *service, CamelException *ex)
321 {
322         /* Things like the CamelMboxStore can validly
323          * not define a connect function.
324          */
325          return TRUE;
326 }
327
328
329 /**
330  * camel_service_connect:
331  * @service: a #CamelService object
332  * @ex: a #CamelException
333  *
334  * Connect to the service using the parameters it was initialized
335  * with.
336  *
337  * Returns: %TRUE if the connection is made or %FALSE otherwise
338  **/
339 gboolean
340 camel_service_connect (CamelService *service, CamelException *ex)
341 {
342         gboolean ret = FALSE;
343         gboolean unreg = FALSE;
344         CamelOperation *connect_op;
345
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);
349
350         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
351
352         if (service->status == CAMEL_SERVICE_CONNECTED) {
353                 CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
354                 return TRUE;
355         }
356
357         /* Register a separate operation for connecting, so that
358          * the offline code can cancel it.
359          */
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);
365                 unreg = TRUE;
366         }
367         connect_op = service->connect_op;
368         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
369
370         service->status = CAMEL_SERVICE_CONNECTING;
371         ret = CSERV_CLASS (service)->connect (service, ex);
372         service->status = ret ? CAMEL_SERVICE_CONNECTED : CAMEL_SERVICE_DISCONNECTED;
373
374         CAMEL_SERVICE_LOCK (service, connect_op_lock);
375         if (connect_op) {
376                 if (unreg && service->connect_op)
377                         camel_operation_unregister (connect_op);
378
379                 camel_operation_unref (connect_op);
380                 service->connect_op = NULL;
381         }
382         CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
383
384         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
385
386         return ret;
387 }
388
389 static gboolean
390 service_disconnect (CamelService *service, gboolean clean, CamelException *ex)
391 {
392         /*service->connect_level--;*/
393
394         /* We let people get away with not having a disconnect
395          * function -- CamelMboxStore, for example.
396          */
397
398         return TRUE;
399 }
400
401
402 /**
403  * camel_service_disconnect:
404  * @service: a #CamelService object
405  * @clean: whether or not to try to disconnect cleanly
406  * @ex: a #CamelException
407  *
408  * Disconnect from the service. If @clean is %FALSE, it should not
409  * try to do any synchronizing or other cleanup of the connection.
410  *
411  * Returns: %TRUE if the disconnect was successful or %FALSE otherwise
412  **/
413 gboolean
414 camel_service_disconnect (CamelService *service, gboolean clean,
415                           CamelException *ex)
416 {
417         gboolean res = TRUE;
418         gint unreg = FALSE;
419
420         CAMEL_SERVICE_REC_LOCK (service, connect_lock);
421
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);
429                         unreg = TRUE;
430                 }
431                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
432
433                 service->status = CAMEL_SERVICE_DISCONNECTING;
434                 res = CSERV_CLASS (service)->disconnect (service, clean, ex);
435                 service->status = CAMEL_SERVICE_DISCONNECTED;
436
437                 CAMEL_SERVICE_LOCK (service, connect_op_lock);
438                 if (unreg)
439                         camel_operation_unregister (service->connect_op);
440
441                 camel_operation_unref (service->connect_op);
442                 service->connect_op = NULL;
443                 CAMEL_SERVICE_UNLOCK (service, connect_op_lock);
444         }
445
446         CAMEL_SERVICE_REC_UNLOCK (service, connect_lock);
447
448                 service->status = CAMEL_SERVICE_DISCONNECTED;
449         return res;
450 }
451
452 static void
453 cancel_connect (CamelService *service)
454 {
455         camel_operation_cancel (service->connect_op);
456 }
457
458 /**
459  * camel_service_cancel_connect:
460  * @service: a #CamelService object
461  *
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
464  * no-op.
465  **/
466 void
467 camel_service_cancel_connect (CamelService *service)
468 {
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);
473 }
474
475 /**
476  * camel_service_get_url:
477  * @service: a #CamelService object
478  *
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.
482  *
483  * Returns: the URL representing @service
484  **/
485 gchar *
486 camel_service_get_url (CamelService *service)
487 {
488         return camel_url_to_string (service->url, CAMEL_URL_HIDE_PASSWORD);
489 }
490
491
492 static gchar *
493 get_name (CamelService *service, gboolean brief)
494 {
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 ("???");
498 }
499
500
501 /**
502  * camel_service_get_name:
503  * @service: a #CamelService object
504  * @brief: whether or not to use a briefer form
505  *
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.
510  *
511  * Returns: a description of the service which the caller must free
512  **/
513 gchar *
514 camel_service_get_name (CamelService *service, gboolean brief)
515 {
516         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
517         g_return_val_if_fail (service->url, NULL);
518
519         return CSERV_CLASS (service)->get_name (service, brief);
520 }
521
522
523 static gchar *
524 get_path (CamelService *service)
525 {
526         CamelProvider *prov = service->provider;
527         CamelURL *url = service->url;
528         GString *gpath;
529         gchar *path;
530
531         /* A sort of ad-hoc default implementation that works for our
532          * current set of services.
533          */
534
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 : "");
541
542                         if (url->port)
543                                 g_string_append_printf (gpath, ":%d", url->port);
544                 } else {
545                         g_string_append_printf (gpath, "/%s%s", url->user ? url->user : "",
546                                                 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
547                 }
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 : "");
552
553                 if (url->port)
554                         g_string_append_printf (gpath, ":%d", url->port);
555         }
556
557         if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH))
558                 g_string_append_printf (gpath, "%s%s", *url->path == '/' ? "" : "/", url->path);
559
560         path = gpath->str;
561         g_string_free (gpath, FALSE);
562
563         return path;
564 }
565
566 /**
567  * camel_service_get_path:
568  * @service: a #CamelService object
569  *
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
574  * provider.
575  *
576  * Returns: the path, which the caller must free
577  **/
578 gchar *
579 camel_service_get_path (CamelService *service)
580 {
581         g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
582         g_return_val_if_fail (service->url, NULL);
583
584         return CSERV_CLASS (service)->get_path (service);
585 }
586
587
588 /**
589  * camel_service_get_session:
590  * @service: a #CamelService object
591  *
592  * Gets the #CamelSession associated with the service.
593  *
594  * Returns: the session
595  **/
596 CamelSession *
597 camel_service_get_session (CamelService *service)
598 {
599         return service->session;
600 }
601
602
603 /**
604  * camel_service_get_provider:
605  * @service: a #CamelService object
606  *
607  * Gets the #CamelProvider associated with the service.
608  *
609  * Returns: the provider
610  **/
611 CamelProvider *
612 camel_service_get_provider (CamelService *service)
613 {
614         return service->provider;
615 }
616
617 static GList *
618 query_auth_types (CamelService *service, CamelException *ex)
619 {
620         return NULL;
621 }
622
623
624 /**
625  * camel_service_query_auth_types:
626  * @service: a #CamelService object
627  * @ex: a #CamelException
628  *
629  * This is used by the mail source wizard to get the list of
630  * authentication types supported by the protocol, and information
631  * about them.
632  *
633  * Returns: a list of #CamelServiceAuthType records. The caller
634  * must free the list with #g_list_free when it is done with it.
635  **/
636 GList *
637 camel_service_query_auth_types (CamelService *service, CamelException *ex)
638 {
639         GList *ret;
640
641         g_return_val_if_fail (service != NULL, NULL);
642
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);
648
649         return ret;
650 }