aboutsummaryrefslogtreecommitdiffstats
path: root/camel/camel-provider.c
blob: 52cfea60a1179a5fc5cf9f1722be06d0ca5df98a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* camel-provider.c :  provider framework   */

/* 
 *
 * Author : 
 *  Bertrand Guiheneuf <bertrand@helixcode.com>
 *
 * Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
 *
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


/* 
   A provider can be added "by hand" or by loading a module. 


   Adding providers with modules. 
   ------------------------------

   The modules are shared libraries which must contain the 
   function

   CamelProvider *camel_provider_module_init ();
  
   returning the provider object defined in the module 

   
*/

/* FIXME: Shouldn't we add a version number to providers ? */

#include "config.h"
#include "camel-provider.h"

#include <dirent.h>
#include <string.h>

static GList *_provider_list = NULL;

static gint        
_provider_name_cmp (gconstpointer a, gconstpointer b)
{
    CamelProvider *provider_a = CAMEL_PROVIDER (a);
    CamelProvider *provider_b = CAMEL_PROVIDER (b);

    return strcmp ( provider_a->name,  provider_b->name);
}

void
camel_provider_register (CamelProvider *provider)
{
    GList *old_provider_node = NULL;

    g_assert (provider);

    if (_provider_list)
        old_provider_node = g_list_find_custom (_provider_list, provider, _provider_name_cmp);

    if (old_provider_node != NULL) {
        /* camel_provider_unref (CAMEL_PROVIDER (old_provider_node->data)); */
        old_provider_node->data = provider;
    } else {
        /* be careful, we use prepend here, so that last registered
           providers come first */
        _provider_list = g_list_prepend (_provider_list, provider);
    }
    /* camel_provider_ref (provider); */
}


const CamelProvider *
camel_provider_register_as_module (const gchar *module_path)
{
    
    CamelProvider *new_provider = NULL;
    GModule *new_module = NULL;
    CamelProvider * (*camel_provider_module_init) ();
    gboolean has_module_init;
    
    g_return_val_if_fail (module_path, NULL);

    if (!g_module_supported ()) {
        g_warning ("CamelProvider::register_as_module: module "
               "loading not supported on this system\n");
        return NULL;
    }
    

    new_module = g_module_open (module_path, 0);
    if (!new_module) {
        printf ("g_module_open reports: %s\n", g_module_error ());
        g_warning ("CamelProvider::register_as_module: Unable to "
               "load module %s\n", module_path);
        return NULL;
    }
        
    has_module_init = g_module_symbol (new_module, "camel_provider_module_init", (gpointer *)&camel_provider_module_init);
    if (!has_module_init){
        g_warning ("CamelProvider::register_as_module loading "
               "of module %s failed,\n\tSymbol "
               "camel_provider_module_init not defined in it\n",
               module_path);
        return NULL;
    }

    new_provider = camel_provider_module_init();
    new_provider->gmodule = new_module;
    camel_provider_register (new_provider);

    return new_provider;
} 

/**
 * camel_provider_scan: Scan for available providers and return a list.
 *
 * Note that this function will cause all providers in the providerdir
 * to be loaded into memory.
 *
 * Return value: the list of CamelProviders. The caller must free this
 * list when it is done with it.
 **/
GList *
camel_provider_scan (void)
{
    DIR *dir;
    struct dirent *dent;
    char *p, *name;

    dir = opendir (CAMEL_PROVIDERDIR);
    if (!dir)
        return NULL;
    while ((dent = readdir (dir))) {
        p = strstr (dent->d_name, ".so");
        if (!p || strcmp (p, ".so") != 0)
            continue;

        name = g_module_build_path (CAMEL_PROVIDERDIR, dent->d_name);
        camel_provider_register_as_module (name);
        g_free (name);
    }
    closedir (dir);

    return g_list_copy (_provider_list);
}
    

/**
 * camel_provider_get_for_protocol: get a registered provider for a protocol
 * @protocol: protocol name (case insensitive)
 * @type: provider type (transport, store, ...)
 * 
 * Look into the list of registered provider if 
 * one correspond both to the protocol name 
 * and to the protocol type. When several providers
 * exist for a same protocol, the last registered
 * is returned.
 * 
 * Return value: Matching provider or NULL if none exists. 
 **/
const CamelProvider *
camel_provider_get_for_protocol (const gchar *protocol, ProviderType type)
{
    CamelProvider *current_provider = NULL;
    GList *current_provider_node;
    gboolean protocol_is_found;
    gboolean provider_is_found;

    g_assert (protocol);
    g_return_val_if_fail (_provider_list, NULL);

    current_provider_node = _provider_list;
    provider_is_found = FALSE;

    while ((!provider_is_found) && current_provider_node) {
        current_provider = (CamelProvider *)current_provider_node->data;
        
        protocol_is_found = (g_strcasecmp (protocol, current_provider->protocol) == 0);
        if (protocol_is_found) 
            provider_is_found = (current_provider->provider_type == type);
        
        current_provider_node = current_provider_node->next;
        }

    if (provider_is_found) return current_provider;
    else return NULL;
}