Merge branch 'master' of git://repo.or.cz/tele
[tele.git] / src / plugin.c
blob25d18d834cb194af6804445f247038389bd539c1
1 /**
3 * Teleport plugin management
5 * plugin.c
7 * Copyright : (C) 2007 by Diogo Ferreira <diogo@underdev.org>
8 * (C) 2007 by João Oliveirinha <joliveirinha@floodbit.org>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 **/
23 #include "teleport-private.h"
24 #include "teleport.h"
26 GList *pluginList = NULL;
28 TeleportPlugin *
29 teleport_load_plugin (const gchar* name)
31 TeleportPlugin *plug;
32 GModule *dlm;
33 gchar* pluginPath;
35 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: Attempting to load", name);
37 if (!g_module_supported())
39 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: This platform doesn't support modules", name);
40 return NULL;
43 plug = g_new (TeleportPlugin, 1);
45 pluginPath = g_module_build_path ("plugins",name);
47 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: Loading %s", name, pluginPath);
49 dlm = g_module_open (pluginPath, G_MODULE_BIND_LAZY);
50 if (dlm)
52 GetVTableProc getVTable;
53 int (*getAbi)();
55 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: Loaded", name);
57 if (!g_module_symbol(dlm, "teleport_get_abi", (gpointer)(&getAbi))
58 || (*getAbi)() != TELEPORT_ABI)
60 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: ABI mismatch, recompile the plugin", name);
61 g_module_close (dlm);
62 g_free (pluginPath);
63 g_free (plug);
65 return NULL;
69 if (!g_module_symbol(dlm, "teleport_get_vtable", (gpointer)(&getVTable)))
71 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: Unable to find teleportGetVTable", name);
72 g_module_close (dlm);
73 g_free (pluginPath);
74 g_free (plug);
76 return NULL;
80 plug->priority = 0;
81 plug->modulePtr = dlm;
82 plug->vTable = (*getVTable) ();
84 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: VTable loaded", name);
86 if (!(*plug->vTable->initPlugin) (plug))
88 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: unable to init plugin",name);
89 g_module_close (dlm);
90 g_free (pluginPath);
91 g_free (plug);
92 return NULL;
95 pluginList = g_list_append (pluginList, plug);
97 else {
98 g_log (NULL, G_LOG_LEVEL_DEBUG, "%s: Unable to load %s", name,pluginPath);
99 g_free (plug);
102 g_free (pluginPath);
104 return plug;
107 void
108 teleport_unload_plugin (TeleportPlugin *plug)
110 (*plug->vTable->finiPlugin) (plug);
112 pluginList = g_list_remove (pluginList, (gpointer)plug);
114 g_module_close (plug->modulePtr);
116 free (plug);
119 void
120 teleport_traverse_plugins ()
122 GList *iterator;
123 for (iterator = pluginList; iterator; iterator = iterator->next)
125 TeleportPlugin *plug = (TeleportPlugin*)iterator->data;
126 g_print ("Priority: %d\n",plug->priority);
127 g_print ("Loaded: %s\n",plug->vTable->name);
131 void
132 teleport_perform_query (const gchar *s)
134 GList *iterator;
135 GSList *queryResult;
136 for (iterator = pluginList; iterator; iterator = iterator->next)
138 TeleportPlugin *plug = (TeleportPlugin*)iterator->data;
140 if (plug->vTable->cancelQuery)
141 (*plug->vTable->cancelQuery)();
143 queryResult = (*plug->vTable->query) (plug, s);
144 if (queryResult) /* Possible sync returns */
146 teleport_trigger_query_update (queryResult);
151 void
152 teleport_do_action (TeleportAction *action)
154 (*(action->backend->vTable->doAction))(action);