99dd5bd9711447adfe1338ca860a761ff5316967
[profile/ivi/gsignond.git] / src / common / gsignond-plugin-loader.c
1 /* vi: set et sw=4 ts=4 cino=t0,(0: */
2 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /*
4  * This file is part of gsignond
5  *
6  * Copyright (C) 2012 Intel Corporation.
7  *
8  * Contact: Alexander Kanavin <alex.kanavin@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library 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 GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25
26
27 #include <gmodule.h>
28
29 #include "gsignond/gsignond-log.h"
30 #include "gsignond-plugin-loader.h"
31
32 GSignondPlugin*
33 gsignond_load_plugin(
34         GSignondConfig* config,
35         gchar* plugin_type)
36 {
37     gchar* plugin_filename = g_module_build_path (
38             gsignond_config_get_string (config,
39                     GSIGNOND_CONFIG_GENERAL_PLUGINS_DIR), plugin_type);
40     GSignondPlugin *plugin = gsignond_load_plugin_with_filename (plugin_type,
41             plugin_filename);
42     g_free(plugin_filename);
43     return plugin;
44 }
45
46 GSignondPlugin*
47 gsignond_load_plugin_with_filename(
48         gchar *plugin_type,
49         gchar *plugin_filename)
50 {
51     DBG("Loading plugin %s", plugin_filename);
52     GModule* plugin_module = g_module_open (plugin_filename, 
53             G_MODULE_BIND_LOCAL);
54     if (plugin_module == NULL) {
55         DBG("Plugin couldn't be opened: %s", g_module_error());
56         return NULL;
57     }
58
59     gchar* plugin_get_type = g_strdup_printf("gsignond_%s_plugin_get_type",
60         plugin_type);
61     gpointer p;
62
63     DBG("Resolving symbol %s", plugin_get_type);
64     gboolean symfound = g_module_symbol (plugin_module,
65         plugin_get_type, &p);
66     g_free(plugin_get_type);
67     if (!symfound) {
68         DBG("Symbol couldn't be resolved");
69         g_module_close (plugin_module);
70         return NULL;
71     }
72     
73     DBG("Creating plugin object");
74     GType (*plugin_get_type_f)(void) = p;
75     GSignondPlugin* plugin = g_object_new(plugin_get_type_f(), NULL);
76     if (plugin == NULL) {
77         DBG("Plugin couldn't be created");
78         g_module_close (plugin_module);
79         return NULL;
80     }
81     g_module_make_resident (plugin_module);
82     return plugin;
83 }