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 1999, 2000 Ximian, Inc. (www.ximian.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>
38 #include <glib/gi18n-lib.h>
39 #include <glib/gstdio.h>
42 #include <libedataserver/e-msgport.h>
44 #include "camel-exception.h"
45 #include "camel-private.h"
46 #include "camel-provider.h"
47 #include "camel-string-utils.h"
48 #include "camel-vee-store.h"
50 /* table of CamelProviderModule's */
51 static GHashTable *module_table;
52 /* table of CamelProvider's */
53 static GHashTable *provider_table;
54 static GStaticRecMutex provider_lock = G_STATIC_REC_MUTEX_INIT;
56 #define LOCK() (g_static_rec_mutex_lock(&provider_lock))
57 #define UNLOCK() (g_static_rec_mutex_unlock(&provider_lock))
59 /* The vfolder provider is always available */
60 static CamelProvider vee_provider = {
62 N_("Virtual folder email provider"),
64 N_("For reading mail as a query of another set of folders"),
68 CAMEL_PROVIDER_IS_STORAGE,
69 CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
74 static pthread_once_t setup_once = PTHREAD_ONCE_INIT;
79 module_table = g_hash_table_new(camel_strcase_hash, camel_strcase_equal);
80 provider_table = g_hash_table_new(camel_strcase_hash, camel_strcase_equal);
82 vee_provider.object_types[CAMEL_PROVIDER_STORE] = camel_vee_store_get_type ();
83 vee_provider.url_hash = camel_url_hash;
84 vee_provider.url_equal = camel_url_equal;
85 camel_provider_register(&vee_provider);
89 * camel_provider_init:
91 * Initialize the Camel provider system by reading in the .urls
92 * files in the provider directory and creating a hash table mapping
93 * URLs to module names.
95 * A .urls file has the same initial prefix as the shared library it
96 * correspond to, and consists of a series of lines containing the URL
97 * protocols that that library handles.
99 * TODO: This should be pathed?
100 * TODO: This should be plugin-d?
103 camel_provider_init (void)
107 char *p, *name, buf[80];
108 CamelProviderModule *m;
109 static int loaded = 0;
111 pthread_once(&setup_once, provider_setup);
118 dir = g_dir_open (CAMEL_PROVIDERDIR, 0, NULL);
120 g_warning("Could not open camel provider directory (%s): %s",
121 CAMEL_PROVIDERDIR, g_strerror (errno));
125 while ((entry = g_dir_read_name (dir))) {
128 p = strrchr (entry, '.');
129 if (!p || strcmp (p, ".urls") != 0)
132 name = g_strdup_printf ("%s/%s", CAMEL_PROVIDERDIR, entry);
133 fp = g_fopen (name, "r");
135 g_warning ("Could not read provider info file %s: %s",
136 name, g_strerror (errno));
141 p = strrchr (name, '.');
142 strcpy (p, "." G_MODULE_SUFFIX);
144 m = g_malloc0(sizeof(*m));
147 while ((fgets (buf, sizeof (buf), fp))) {
148 buf[sizeof (buf) - 1] = '\0';
149 p = strchr (buf, '\n');
154 char *protocol = g_strdup(buf);
156 m->types = g_slist_prepend(m->types, protocol);
157 g_hash_table_insert(module_table, protocol, m);
168 * camel_provider_load:
169 * @session: the current session
170 * @path: the path to a shared library
171 * @ex: a CamelException
173 * Loads the provider at @path, and calls its initialization function,
174 * passing @session as an argument. The provider should then register
175 * itself with @session.
178 camel_provider_load(const char *path, CamelException *ex)
181 CamelProvider *(*camel_provider_module_init) (void);
183 pthread_once(&setup_once, provider_setup);
185 if (!g_module_supported ()) {
186 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
187 _("Could not load %s: Module loading "
188 "not supported on this system."),
193 module = g_module_open (path, G_MODULE_BIND_LAZY);
195 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
196 _("Could not load %s: %s"),
197 path, g_module_error ());
201 if (!g_module_symbol (module, "camel_provider_module_init",
202 (gpointer *)&camel_provider_module_init)) {
203 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
204 _("Could not load %s: No initialization "
205 "code in module."), path);
206 g_module_close (module);
210 camel_provider_module_init ();
214 * camel_provider_register:
215 * @provider: provider object
217 * Registers a provider.
220 camel_provider_register(CamelProvider *provider)
223 CamelProviderConfEntry *conf;
226 g_return_if_fail (provider != NULL);
228 g_assert(provider_table);
232 if (g_hash_table_lookup(provider_table, provider->protocol) != NULL) {
233 g_warning("Trying to re-register camel provider for protocol '%s'", provider->protocol);
238 for (i = 0; i < CAMEL_NUM_PROVIDER_TYPES; i++) {
239 if (provider->object_types[i])
240 provider->service_cache[i] = camel_object_bag_new (provider->url_hash, provider->url_equal,
241 (CamelCopyFunc)camel_url_copy, (GFreeFunc)camel_url_free);
244 /* Translate all strings here */
245 #define P_(string) dgettext (provider->translation_domain, string)
247 provider->name = P_(provider->name);
248 provider->description = P_(provider->description);
249 conf = provider->extra_conf;
251 for (i=0;conf[i].type != CAMEL_PROVIDER_CONF_END;i++) {
253 conf[i].text = P_(conf[i].text);
257 l = provider->authtypes;
259 CamelServiceAuthType *auth = l->data;
261 auth->name = P_(auth->name);
262 auth->description = P_(auth->description);
266 g_hash_table_insert(provider_table, provider->protocol, provider);
272 provider_compare (gconstpointer a, gconstpointer b)
274 const CamelProvider *cpa = (const CamelProvider *)a;
275 const CamelProvider *cpb = (const CamelProvider *)b;
277 return strcmp (cpa->name, cpb->name);
281 add_to_list (gpointer key, gpointer value, gpointer user_data)
283 GList **list = user_data;
285 *list = g_list_prepend(*list, value);
289 * camel_session_list_providers:
290 * @session: the session
291 * @load: whether or not to load in providers that are not already loaded
293 * This returns a list of available providers in this session. If @load
294 * is %TRUE, it will first load in all available providers that haven't
297 * Return value: a GList of providers, which the caller must free.
300 camel_provider_list(gboolean load)
304 g_assert(provider_table);
311 g_hash_table_foreach(module_table, add_to_list, &list);
312 for (w = list;w;w = w->next) {
313 CamelProviderModule *m = w->data;
316 camel_provider_load(m->path, NULL);
324 g_hash_table_foreach(provider_table, add_to_list, &list);
328 list = g_list_sort(list, provider_compare);
334 * camel_provider_get:
335 * @url_string: the URL for the service whose provider you want
336 * @ex: a CamelException
338 * This returns the CamelProvider that would be used to handle
339 * @url_string, loading it in from disk if necessary.
341 * Return value: the provider, or %NULL, in which case @ex will be set.
344 camel_provider_get(const char *url_string, CamelException *ex)
346 CamelProvider *provider = NULL;
350 g_return_val_if_fail(url_string != NULL, NULL);
351 g_assert(provider_table);
353 len = strcspn(url_string, ":");
354 protocol = g_alloca(len+1);
355 memcpy(protocol, url_string, len);
360 provider = g_hash_table_lookup(provider_table, protocol);
362 CamelProviderModule *m;
364 m = g_hash_table_lookup(module_table, protocol);
365 if (m && !m->loaded) {
367 camel_provider_load(m->path, ex);
368 if (camel_exception_is_set (ex))
371 provider = g_hash_table_lookup(provider_table, protocol);
374 if (provider == NULL)
375 camel_exception_setv(ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
376 _("No provider available for protocol `%s'"),
386 * camel_provider_auto_detect:
387 * @provider: camel provider
388 * @settings: currently set settings
389 * @auto_detected: output hash table of auto-detected values
392 * After filling in the standard Username/Hostname/Port/Path settings
393 * (which must be set in @settings), if the provider supports it, you
394 * may wish to have the provider auto-detect further settings based on
395 * the aformentioned settings.
397 * If the provider does not support auto-detection, @auto_detected
398 * will be set to %NULL. Otherwise the provider will attempt to
399 * auto-detect whatever it can and file them into @auto_detected. If
400 * for some reason it cannot auto-detect anything (not enough
401 * information provided in @settings?) then @auto_deetected will be
402 * set to %NULL and an exception may be set to explain why it failed.
404 * Returns 0 on success or -1 on fail.
407 camel_provider_auto_detect (CamelProvider *provider, CamelURL *url,
408 GHashTable **auto_detected, CamelException *ex)
410 g_return_val_if_fail (provider != NULL, -1);
412 if (provider->auto_detect) {
413 return provider->auto_detect (url, auto_detected, ex);
415 *auto_detected = NULL;