1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 * camel-provider.c: provider framework
5 * Bertrand Guiheneuf <bertrand@helixcode.com>
6 * Dan Winship <danw@ximian.com>
7 * Jeffrey Stedfast <fejj@ximian.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
26 /* FIXME: Shouldn't we add a version number to providers ? */
35 #include <sys/types.h>
37 #include <glib/gi18n-lib.h>
38 #include <glib/gstdio.h>
41 #include "camel-provider.h"
42 #include "camel-string-utils.h"
43 #include "camel-vee-store.h"
44 #include "camel-win32.h"
46 /* table of CamelProviderModule's */
47 static GHashTable *module_table;
48 /* table of CamelProvider's */
49 static GHashTable *provider_table;
50 static GRecMutex provider_lock;
52 #define LOCK() (g_rec_mutex_lock(&provider_lock))
53 #define UNLOCK() (g_rec_mutex_unlock(&provider_lock))
55 /* The vfolder provider is always available */
56 static CamelProvider vee_provider = {
58 N_("Virtual folder email provider"),
60 N_("For reading mail as a query of another set of folders"),
64 CAMEL_PROVIDER_IS_STORAGE | CAMEL_PROVIDER_IS_LOCAL,
65 CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
67 NULL, /* extra conf */
69 NULL, /* port providers */
74 static GOnce setup_once = G_ONCE_INIT;
77 provider_register_internal (CamelProvider *provider)
79 CamelProviderConfEntry *conf;
80 CamelProviderPortEntry *port;
84 g_return_if_fail (provider != NULL);
85 g_return_if_fail (provider->protocol != NULL);
89 if (g_hash_table_lookup (provider_table, provider->protocol) != NULL) {
91 "Trying to re-register CamelProvider for protocol '%s'",
97 /* Translate all strings here */
98 #define P_(string) dgettext (provider->translation_domain, string)
100 provider->name = P_(provider->name);
101 provider->description = P_(provider->description);
103 conf = provider->extra_conf;
105 for (ii = 0; conf[ii].type != CAMEL_PROVIDER_CONF_END; ii++) {
106 if (conf[ii].text != NULL)
107 conf[ii].text = P_(conf[ii].text);
111 for (link = provider->authtypes; link != NULL; link = link->next) {
112 CamelServiceAuthType *auth = link->data;
114 auth->name = P_(auth->name);
115 auth->description = P_(auth->description);
118 if (provider->port_entries != NULL) {
119 provider->url_flags |= CAMEL_URL_NEED_PORT;
120 port = provider->port_entries;
121 for (ii = 0; port[ii].port != 0; ii++)
122 if (port[ii].desc != NULL)
123 port[ii].desc = P_(port[ii].desc);
125 provider->url_flags &= ~CAMEL_URL_NEED_PORT;
128 g_hash_table_insert (
130 (gpointer) provider->protocol, provider);
136 provider_setup (gpointer param)
138 module_table = g_hash_table_new (
139 (GHashFunc) camel_strcase_hash,
140 (GEqualFunc) camel_strcase_equal);
141 provider_table = g_hash_table_new (
142 (GHashFunc) camel_strcase_hash,
143 (GEqualFunc) camel_strcase_equal);
145 vee_provider.object_types[CAMEL_PROVIDER_STORE] = CAMEL_TYPE_VEE_STORE;
146 vee_provider.url_hash = camel_url_hash;
147 vee_provider.url_equal = camel_url_equal;
148 provider_register_internal (&vee_provider);
154 * camel_provider_init:
156 * Initialize the Camel provider system by reading in the .urls
157 * files in the provider directory and creating a hash table mapping
158 * URLs to module names.
160 * A .urls file has the same initial prefix as the shared library it
161 * correspond to, and consists of a series of lines containing the URL
162 * protocols that that library handles.
164 * TODO: This should be pathed?
165 * TODO: This should be plugin-d?
168 camel_provider_init (void)
172 gchar *p, *name, buf[80];
173 CamelProviderModule *m;
174 static gint loaded = 0;
176 g_once (&setup_once, provider_setup, NULL);
183 dir = g_dir_open (CAMEL_PROVIDERDIR, 0, NULL);
186 "Could not open camel provider directory (%s): %s",
187 CAMEL_PROVIDERDIR, g_strerror (errno));
191 while ((entry = g_dir_read_name (dir))) {
194 p = strrchr (entry, '.');
195 if (!p || strcmp (p, ".urls") != 0)
198 name = g_strdup_printf ("%s/%s", CAMEL_PROVIDERDIR, entry);
199 fp = g_fopen (name, "r");
202 "Could not read provider info file %s: %s",
203 name, g_strerror (errno));
208 p = strrchr (name, '.');
210 strcpy (p, "." G_MODULE_SUFFIX);
212 m = g_malloc0 (sizeof (*m));
215 while ((fgets (buf, sizeof (buf), fp))) {
216 buf[sizeof (buf) - 1] = '\0';
217 p = strchr (buf, '\n');
222 gchar *protocol = g_strdup (buf);
224 m->types = g_slist_prepend (m->types, protocol);
225 g_hash_table_insert (module_table, protocol, m);
236 * camel_provider_load:
237 * @path: the path to a shared library
238 * @error: return location for a #GError, or %NULL
240 * Loads the provider at @path, and calls its initialization function,
241 * passing @session as an argument. The provider should then register
242 * itself with @session.
244 * Returns: %TRUE on success, %FALSE on failure
247 camel_provider_load (const gchar *path,
251 CamelProvider *(*provider_module_init) (void);
253 g_once (&setup_once, provider_setup, NULL);
255 if (!g_module_supported ()) {
257 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
258 _("Could not load %s: Module loading "
259 "not supported on this system."), path);
263 module = g_module_open (path, G_MODULE_BIND_LAZY);
264 if (module == NULL) {
266 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
267 _("Could not load %s: %s"),
268 path, g_module_error ());
272 if (!g_module_symbol (module, "camel_provider_module_init",
273 (gpointer *) &provider_module_init)) {
275 error, CAMEL_ERROR, CAMEL_ERROR_GENERIC,
276 _("Could not load %s: No initialization "
277 "code in module."), path);
278 g_module_close (module);
282 provider_module_init ();
288 * camel_provider_register:
289 * @provider: provider object
291 * Registers a provider.
294 camel_provider_register (CamelProvider *provider)
296 g_once (&setup_once, provider_setup, NULL);
298 provider_register_internal (provider);
302 provider_compare (gconstpointer a,
305 const CamelProvider *cpa = (const CamelProvider *) a;
306 const CamelProvider *cpb = (const CamelProvider *) b;
308 return strcmp (cpa->name, cpb->name);
312 add_to_list (gpointer key,
316 GList **list = user_data;
318 *list = g_list_prepend(*list, value);
322 * camel_session_list_providers:
323 * @session: the session
324 * @load: whether or not to load in providers that are not already loaded
326 * This returns a list of available providers in this session. If @load
327 * is %TRUE, it will first load in all available providers that haven't
330 * Free the returned list with g_list_free(). The #CamelProvider structs
331 * in the list are owned by Camel and should not be modified or freed.
333 * Returns: a #GList of #CamelProvider structs
336 camel_provider_list (gboolean load)
340 /* provider_table can be NULL, so initialize it */
341 if (G_UNLIKELY (provider_table == NULL))
342 camel_provider_init ();
344 g_return_val_if_fail (provider_table != NULL, NULL);
351 g_hash_table_foreach (module_table, add_to_list, &list);
352 for (w = list; w; w = w->next) {
353 CamelProviderModule *m = w->data;
354 GError *error = NULL;
357 camel_provider_load (m->path, &error);
365 g_error_free (error);
372 g_hash_table_foreach (provider_table, add_to_list, &list);
376 list = g_list_sort (list, provider_compare);
382 * camel_provider_get:
383 * @protocol: a #CamelProvider protocol name
384 * @error: return location for a #GError, or %NULL
386 * Returns the registered #CamelProvider for @protocol, loading it
387 * from disk if necessary. If no #CamelProvider can be found for
388 * @protocol, or the provider module fails to load, the function
389 * sets @error and returns %NULL.
391 * The returned #CamelProvider is owned by Camel and should not be
394 * Returns: a #CamelProvider for %protocol, or %NULL
397 camel_provider_get (const gchar *protocol,
400 CamelProvider *provider = NULL;
402 g_return_val_if_fail (protocol != NULL, NULL);
403 g_return_val_if_fail (provider_table != NULL, NULL);
407 provider = g_hash_table_lookup (provider_table, protocol);
408 if (provider == NULL) {
409 CamelProviderModule *module;
411 module = g_hash_table_lookup (module_table, protocol);
412 if (module != NULL && !module->loaded) {
414 if (!camel_provider_load (module->path, error))
417 provider = g_hash_table_lookup (provider_table, protocol);
420 if (provider == NULL)
422 error, CAMEL_SERVICE_ERROR,
423 CAMEL_SERVICE_ERROR_URL_INVALID,
424 _("No provider available for protocol '%s'"),
433 * camel_provider_auto_detect:
434 * @provider: camel provider
436 * @auto_detected: output hash table of auto-detected values
437 * @error: return location for a #GError, or %NULL
439 * After filling in the standard Username/Hostname/Port/Path settings
440 * (which must be set in @url), if the provider supports it, you
441 * may wish to have the provider auto-detect further settings based on
442 * the aformentioned settings.
444 * If the provider does not support auto-detection, @auto_detected
445 * will be set to %NULL. Otherwise the provider will attempt to
446 * auto-detect whatever it can and file them into @auto_detected. If
447 * for some reason it cannot auto-detect anything (not enough
448 * information provided in @url?) then @auto_detected will be
449 * set to %NULL and an exception may be set to explain why it failed.
451 * Returns: 0 on success or -1 on fail.
454 camel_provider_auto_detect (CamelProvider *provider,
456 GHashTable **auto_detected,
459 g_return_val_if_fail (provider != NULL, -1);
461 if (provider->auto_detect) {
462 return provider->auto_detect (url, auto_detected, error);
464 *auto_detected = NULL;