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 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include "camel-service.h"
28 #include "camel-session.h"
29 #include "camel-exception.h"
34 #include "camel-private.h"
36 static CamelObjectClass *parent_class = NULL;
38 /* Returns the class for a CamelService */
39 #define CSERV_CLASS(so) CAMEL_SERVICE_CLASS (CAMEL_OBJECT_GET_CLASS(so))
41 static void construct (CamelService *service, CamelSession *session,
42 CamelProvider *provider, CamelURL *url,
44 static gboolean service_connect(CamelService *service, CamelException *ex);
45 static gboolean service_disconnect(CamelService *service, gboolean clean,
47 /*static gboolean is_connected (CamelService *service);*/
48 static GList * query_auth_types (CamelService *service, CamelException *ex);
49 static char * get_name (CamelService *service, gboolean brief);
50 static char * get_path (CamelService *service);
54 camel_service_class_init (CamelServiceClass *camel_service_class)
56 parent_class = camel_type_get_global_classfuncs (CAMEL_OBJECT_TYPE);
58 /* virtual method definition */
59 camel_service_class->construct = construct;
60 camel_service_class->connect = service_connect;
61 camel_service_class->disconnect = service_disconnect;
62 camel_service_class->query_auth_types = query_auth_types;
63 camel_service_class->get_name = get_name;
64 camel_service_class->get_path = get_path;
68 camel_service_init (void *o, void *k)
70 CamelService *service = o;
72 service->priv = g_malloc0(sizeof(*service->priv));
74 service->priv->connect_lock = e_mutex_new(E_MUTEX_REC);
79 camel_service_finalize (CamelObject *object)
81 CamelService *camel_service = CAMEL_SERVICE (object);
83 if (camel_service->connected) {
86 /*g_warning ("camel_service_finalize: finalizing while still connected!");*/
87 camel_exception_init (&ex);
88 CSERV_CLASS (camel_service)->disconnect (camel_service, FALSE, &ex);
89 if (camel_exception_is_set (&ex)) {
90 g_warning ("camel_service_finalize: silent disconnect failure: %s",
91 camel_exception_get_description(&ex));
93 camel_exception_clear (&ex);
96 if (camel_service->url)
97 camel_url_free (camel_service->url);
98 if (camel_service->session)
99 camel_object_unref (CAMEL_OBJECT (camel_service->session));
101 #ifdef ENABLE_THREADS
102 e_mutex_destroy(camel_service->priv->connect_lock);
104 g_free(camel_service->priv);
110 camel_service_get_type (void)
112 static CamelType camel_service_type = CAMEL_INVALID_TYPE;
114 if (camel_service_type == CAMEL_INVALID_TYPE) {
116 camel_type_register (CAMEL_OBJECT_TYPE, "CamelService",
117 sizeof (CamelService),
118 sizeof (CamelServiceClass),
119 (CamelObjectClassInitFunc) camel_service_class_init,
121 (CamelObjectInitFunc) camel_service_init,
122 camel_service_finalize );
125 return camel_service_type;
130 construct (CamelService *service, CamelSession *session,
131 CamelProvider *provider, CamelURL *url, CamelException *ex)
135 if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_USER) &&
136 (url->user == NULL || url->user[0] == '\0')) {
137 url_string = camel_url_to_string (url, FALSE);
138 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
139 _("URL '%s' needs a username component"),
143 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_HOST) &&
144 (url->host == NULL || url->host[0] == '\0')) {
145 url_string = camel_url_to_string (url, FALSE);
146 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
147 _("URL '%s' needs a host component"),
151 } else if (CAMEL_PROVIDER_NEEDS (provider, CAMEL_URL_PART_PATH) &&
152 (url->path == NULL || url->path[0] == '\0')) {
153 url_string = camel_url_to_string (url, FALSE);
154 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
155 _("URL '%s' needs a path component"),
161 service->provider = provider;
163 service->session = session;
164 camel_object_ref (CAMEL_OBJECT (session));
166 service->connected = FALSE;
170 * camel_service_construct:
171 * @service: the CamelService
172 * @session: the session for the service
173 * @provider: the service's provider
174 * @url: the default URL for the service (may be NULL)
175 * @ex: a CamelException
177 * Constructs a CamelService initialized with the given parameters.
180 camel_service_construct (CamelService *service, CamelSession *session,
181 CamelProvider *provider, CamelURL *url,
184 g_return_if_fail (CAMEL_IS_SERVICE (service));
185 g_return_if_fail (CAMEL_IS_SESSION (session));
187 CSERV_CLASS (service)->construct (service, session, provider, url, ex);
192 service_connect (CamelService *service, CamelException *ex)
194 /* Things like the CamelMboxStore can validly
195 * not define a connect function.
201 * camel_service_connect:
202 * @service: CamelService object
203 * @ex: a CamelException
205 * Connect to the service using the parameters it was initialized
208 * Return value: whether or not the connection succeeded
212 camel_service_connect (CamelService *service, CamelException *ex)
214 gboolean ret = FALSE;
216 g_return_val_if_fail (CAMEL_IS_SERVICE (service), FALSE);
217 g_return_val_if_fail (service->session != NULL, FALSE);
218 g_return_val_if_fail (service->url != NULL, FALSE);
220 CAMEL_SERVICE_LOCK(service, connect_lock);
222 if (service->connected) {
223 /* But we're still connected, so no exception
226 g_warning ("camel_service_connect: trying to connect to an already connected service");
228 } else if (CSERV_CLASS (service)->connect (service, ex)) {
229 service->connected = TRUE;
233 CAMEL_SERVICE_UNLOCK(service, connect_lock);
239 service_disconnect (CamelService *service, gboolean clean, CamelException *ex)
241 /*service->connect_level--;*/
243 /* We let people get away with not having a disconnect
244 * function -- CamelMboxStore, for example.
251 * camel_service_disconnect:
252 * @service: CamelService object
253 * @clean: whether or not to try to disconnect cleanly.
254 * @ex: a CamelException
256 * Disconnect from the service. If @clean is %FALSE, it should not
257 * try to do any synchronizing or other cleanup of the connection.
259 * Return value: whether or not the disconnection succeeded without
260 * errors. (Consult @ex if %FALSE.)
263 camel_service_disconnect (CamelService *service, gboolean clean,
268 CAMEL_SERVICE_LOCK(service, connect_lock);
270 if (service->connected) {
271 res = CSERV_CLASS (service)->disconnect (service, clean, ex);
272 service->connected = FALSE;
275 CAMEL_SERVICE_UNLOCK(service, connect_lock);
281 * camel_service_get_url:
282 * @service: a service
284 * Returns the URL representing a service. The returned URL must be
285 * freed when it is no longer needed. For security reasons, this
286 * routine does not return the password.
288 * Return value: the url name
291 camel_service_get_url (CamelService *service)
293 return camel_url_to_string(service->url, FALSE);
298 get_name (CamelService *service, gboolean brief)
300 g_warning ("CamelService::get_name not implemented for `%s'",
301 camel_type_to_name (CAMEL_OBJECT_GET_TYPE (service)));
302 return g_strdup ("???");
306 * camel_service_get_name:
307 * @service: the service
308 * @brief: whether or not to use a briefer form
310 * This gets the name of the service in a "friendly" (suitable for
311 * humans) form. If @brief is %TRUE, this should be a brief description
312 * such as for use in the folder tree. If @brief is %FALSE, it should
313 * be a more complete and mostly unambiguous description.
315 * Return value: the description, which the caller must free.
318 camel_service_get_name (CamelService *service, gboolean brief)
320 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
321 g_return_val_if_fail (service->url, NULL);
323 return CSERV_CLASS (service)->get_name (service, brief);
328 get_path (CamelService *service)
332 CamelURL *url = service->url;
333 CamelProvider *prov = service->provider;
335 /* A sort of ad-hoc default implementation that works for our
336 * current set of services.
339 gpath = g_string_new (service->provider->protocol);
340 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_USER)) {
341 if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
342 g_string_sprintfa (gpath, "/%s@%s",
343 url->user ? url->user : "",
344 url->host ? url->host : "");
346 g_string_sprintfa (gpath, "/%s%s",
347 url->user ? url->user : "",
348 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_USER) ? "" : "@");
350 } else if (CAMEL_PROVIDER_ALLOWS (prov, CAMEL_URL_PART_HOST)) {
351 g_string_sprintfa (gpath, "/%s%s",
352 CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_HOST) ? "" : "@",
353 url->host ? url->host : "");
355 if (CAMEL_PROVIDER_NEEDS (prov, CAMEL_URL_PART_PATH)) {
356 g_string_sprintfa (gpath, "%s%s",
357 *url->path == '/' ? "" : "/",
362 g_string_free (gpath, FALSE);
367 * camel_service_get_path:
368 * @service: the service
370 * This gets a valid UNIX relative path describing the service, which
371 * is guaranteed to be different from the path returned for any
372 * different service. This path MUST start with the name of the
373 * provider, followed by a "/", but after that, it is up to the
376 * Return value: the path, which the caller must free.
379 camel_service_get_path (CamelService *service)
381 g_return_val_if_fail (CAMEL_IS_SERVICE (service), NULL);
382 g_return_val_if_fail (service->url, NULL);
384 return CSERV_CLASS (service)->get_path (service);
389 * camel_service_get_session:
390 * @service: a service
392 * Returns the CamelSession associated with the service.
394 * Return value: the session
397 camel_service_get_session (CamelService *service)
399 return service->session;
403 * camel_service_get_provider:
404 * @service: a service
406 * Returns the CamelProvider associated with the service.
408 * Return value: the provider
411 camel_service_get_provider (CamelService *service)
413 return service->provider;
417 query_auth_types (CamelService *service, CamelException *ex)
423 * camel_service_query_auth_types:
424 * @service: a CamelService
425 * @ex: a CamelException
427 * This is used by the mail source wizard to get the list of
428 * authentication types supported by the protocol, and information
431 * Return value: a list of CamelServiceAuthType records. The caller
432 * must free the list with g_list_free() when it is done with it.
435 camel_service_query_auth_types (CamelService *service, CamelException *ex)
439 /* note that we get the connect lock here, which means the callee
440 must not call the connect functions itself */
441 CAMEL_SERVICE_LOCK(service, connect_lock);
442 ret = CSERV_CLASS (service)->query_auth_types (service, ex);
443 CAMEL_SERVICE_UNLOCK(service, connect_lock);
448 /* URL utility routines */
451 * camel_service_gethost:
452 * @service: a CamelService
453 * @ex: a CamelException
455 * This is a convenience function to do a gethostbyname on the host
456 * for the service's URL.
458 * Return value: a (statically-allocated) hostent.
461 camel_service_gethost (CamelService *service, CamelException *ex)
466 #warning "This needs to use gethostbyname_r()"
468 if (service->url->host)
469 hostname = service->url->host;
471 hostname = "localhost";
472 h = gethostbyname (hostname);
476 if (h_errno == HOST_NOT_FOUND || h_errno == NO_DATA) {
477 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
478 _("No such host %s."), hostname);
480 camel_exception_setv (ex, CAMEL_EXCEPTION_SERVICE_UNAVAILABLE,
481 _("Temporarily unable to look "
482 "up hostname %s."), hostname);