Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / camel / camel-provider.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2  * camel-provider.c: provider framework
3  *
4  * Authors:
5  *  Bertrand Guiheneuf <bertrand@helixcode.com>
6  *  Dan Winship <danw@ximian.com>
7  *  Jeffrey Stedfast <fejj@ximian.com>
8  *
9  * Copyright 1999, 2000 Ximian, Inc. (www.ximian.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 /* FIXME: Shouldn't we add a version number to providers ? */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include <errno.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/types.h>
36
37 #include <glib.h>
38 #include <glib/gi18n-lib.h>
39 #include <glib/gstdio.h>
40 #include <gmodule.h>
41
42 #include <libedataserver/e-msgport.h>
43
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"
49
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;
55
56 #define LOCK()          (g_static_rec_mutex_lock(&provider_lock))
57 #define UNLOCK()        (g_static_rec_mutex_unlock(&provider_lock))
58
59 /* The vfolder provider is always available */
60 static CamelProvider vee_provider = {
61         "vfolder",
62         N_("Virtual folder email provider"),
63         
64         N_("For reading mail as a query of another set of folders"),
65         
66         "vfolder",
67         
68         CAMEL_PROVIDER_IS_STORAGE,
69         CAMEL_URL_NEED_PATH | CAMEL_URL_PATH_IS_ABSOLUTE | CAMEL_URL_FRAGMENT_IS_PATH,
70         
71         /* ... */
72 };
73
74 static pthread_once_t setup_once = PTHREAD_ONCE_INIT;
75
76 static void
77 provider_setup(void)
78 {
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);
81
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);
86 }
87
88 /**
89  * camel_provider_init:
90  *
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.
94  *
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.
98  *
99  * TODO: This should be pathed?
100  * TODO: This should be plugin-d?
101  **/
102 void
103 camel_provider_init (void)
104 {
105         GDir *dir;
106         const char *entry;
107         char *p, *name, buf[80];
108         CamelProviderModule *m;
109         static int loaded = 0;
110
111         pthread_once(&setup_once, provider_setup);
112
113         if (loaded)
114                 return;
115
116         loaded = 1;
117
118         dir = g_dir_open (CAMEL_PROVIDERDIR, 0, NULL);
119         if (!dir) {
120                 g_warning("Could not open camel provider directory (%s): %s",
121                           CAMEL_PROVIDERDIR, g_strerror (errno));
122                 return;
123         }
124         
125         while ((entry = g_dir_read_name (dir))) {
126                 FILE *fp;
127                 
128                 p = strrchr (entry, '.');
129                 if (!p || strcmp (p, ".urls") != 0)
130                         continue;
131                 
132                 name = g_strdup_printf ("%s/%s", CAMEL_PROVIDERDIR, entry);
133                 fp = g_fopen (name, "r");
134                 if (!fp) {
135                         g_warning ("Could not read provider info file %s: %s",
136                                    name, g_strerror (errno));
137                         g_free (name);
138                         continue;
139                 }
140                 
141                 p = strrchr (name, '.');
142                 strcpy (p, "." G_MODULE_SUFFIX);
143
144                 m = g_malloc0(sizeof(*m));
145                 m->path = name;
146
147                 while ((fgets (buf, sizeof (buf), fp))) {
148                         buf[sizeof (buf) - 1] = '\0';
149                         p = strchr (buf, '\n');
150                         if (p)
151                                 *p = '\0';
152                         
153                         if (*buf) {
154                                 char *protocol = g_strdup(buf);
155
156                                 m->types = g_slist_prepend(m->types, protocol);
157                                 g_hash_table_insert(module_table, protocol, m);
158                         }
159                 }
160
161                 fclose (fp);
162         }
163
164         g_dir_close (dir);
165 }
166
167 /**
168  * camel_provider_load:
169  * @session: the current session
170  * @path: the path to a shared library
171  * @ex: a CamelException
172  *
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.
176  **/ 
177 void
178 camel_provider_load(const char *path, CamelException *ex)
179 {
180         GModule *module;
181         CamelProvider *(*camel_provider_module_init) (void);
182
183         pthread_once(&setup_once, provider_setup);
184
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."),
189                                       path);
190                 return;
191         }
192
193         module = g_module_open (path, G_MODULE_BIND_LAZY);
194         if (!module) {
195                 camel_exception_setv (ex, CAMEL_EXCEPTION_SYSTEM,
196                                       _("Could not load %s: %s"),
197                                       path, g_module_error ());
198                 return;
199         }
200
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);
207                 return;
208         }
209
210         camel_provider_module_init ();
211 }
212
213 /**
214  * camel_provider_register:
215  * @provider: provider object
216  *
217  * Registers a provider.
218  **/
219 void
220 camel_provider_register(CamelProvider *provider)
221 {
222         int i;
223         CamelProviderConfEntry *conf;
224         GList *l;
225
226         g_return_if_fail (provider != NULL);
227
228         g_assert(provider_table);
229
230         LOCK();
231
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);
234                 UNLOCK();
235                 return;
236         }
237         
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);
242         }
243
244         /* Translate all strings here */
245 #define P_(string) dgettext (provider->translation_domain, string)
246
247         provider->name = P_(provider->name);
248         provider->description = P_(provider->description);
249         conf = provider->extra_conf;
250         if (conf) {
251                 for (i=0;conf[i].type != CAMEL_PROVIDER_CONF_END;i++) {
252                         if (conf[i].text)
253                                 conf[i].text = P_(conf[i].text);
254                 }
255         }
256
257         l = provider->authtypes;
258         while (l) {
259                 CamelServiceAuthType *auth = l->data;
260
261                 auth->name = P_(auth->name);
262                 auth->description = P_(auth->description);
263                 l = l->next;
264         }
265
266         g_hash_table_insert(provider_table, provider->protocol, provider);
267
268         UNLOCK();
269 }
270
271 static gint
272 provider_compare (gconstpointer a, gconstpointer b)
273 {
274         const CamelProvider *cpa = (const CamelProvider *)a;
275         const CamelProvider *cpb = (const CamelProvider *)b;
276
277         return strcmp (cpa->name, cpb->name);
278 }
279
280 static void
281 add_to_list (gpointer key, gpointer value, gpointer user_data)
282 {
283         GList **list = user_data;
284
285         *list = g_list_prepend(*list, value);
286 }
287
288 /**
289  * camel_session_list_providers:
290  * @session: the session
291  * @load: whether or not to load in providers that are not already loaded
292  *
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
295  * yet been loaded.
296  *
297  * Return value: a GList of providers, which the caller must free.
298  **/
299 GList *
300 camel_provider_list(gboolean load)
301 {
302         GList *list = NULL;
303
304         g_assert(provider_table);
305
306         LOCK();
307
308         if (load) {
309                 GList *w;
310
311                 g_hash_table_foreach(module_table, add_to_list, &list);
312                 for (w = list;w;w = w->next) {
313                         CamelProviderModule *m = w->data;
314
315                         if (!m->loaded) {
316                                 camel_provider_load(m->path, NULL);
317                                 m->loaded = 1;
318                         }
319                 }
320                 g_list_free(list);
321                 list = NULL;
322         }
323
324         g_hash_table_foreach(provider_table, add_to_list, &list);
325
326         UNLOCK();
327
328         list = g_list_sort(list, provider_compare);
329
330         return list;
331 }
332
333 /**
334  * camel_provider_get:
335  * @url_string: the URL for the service whose provider you want
336  * @ex: a CamelException
337  *
338  * This returns the CamelProvider that would be used to handle
339  * @url_string, loading it in from disk if necessary.
340  *
341  * Return value: the provider, or %NULL, in which case @ex will be set.
342  **/
343 CamelProvider *
344 camel_provider_get(const char *url_string, CamelException *ex)
345 {
346         CamelProvider *provider = NULL;
347         char *protocol;
348         size_t len;
349
350         g_return_val_if_fail(url_string != NULL, NULL);
351         g_assert(provider_table);
352
353         len = strcspn(url_string, ":");
354         protocol = g_alloca(len+1);
355         memcpy(protocol, url_string, len);
356         protocol[len] = 0;
357
358         LOCK();
359
360         provider = g_hash_table_lookup(provider_table, protocol);
361         if (!provider) {
362                 CamelProviderModule *m;
363
364                 m = g_hash_table_lookup(module_table, protocol);
365                 if (m && !m->loaded) {
366                         m->loaded = 1;
367                         camel_provider_load(m->path, ex);
368                         if (camel_exception_is_set (ex))
369                                 goto fail;
370                 }
371                 provider = g_hash_table_lookup(provider_table, protocol);
372         }
373
374         if (provider == NULL)
375                 camel_exception_setv(ex, CAMEL_EXCEPTION_SERVICE_URL_INVALID,
376                                      _("No provider available for protocol `%s'"),
377                                      protocol);
378 fail:
379         UNLOCK();
380
381         return provider;
382 }
383
384
385 /**
386  * camel_provider_auto_detect:
387  * @provider: camel provider
388  * @settings: currently set settings
389  * @auto_detected: output hash table of auto-detected values
390  * @ex: exception
391  *
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.
396  *
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.
403  *
404  * Returns 0 on success or -1 on fail.
405  **/
406 int
407 camel_provider_auto_detect (CamelProvider *provider, CamelURL *url,
408                             GHashTable **auto_detected, CamelException *ex)
409 {
410         g_return_val_if_fail (provider != NULL, -1);
411
412         if (provider->auto_detect) {
413                 return provider->auto_detect (url, auto_detected, ex);
414         } else {
415                 *auto_detected = NULL;
416                 return 0;
417         }
418 }