X-Git-Url: http://git.claws-mail.org/?p=claws.git;a=blobdiff_plain;f=src%2Fcommon%2Fplugin.c;h=0006d1a6ed26e4357398e49bd4a076a36949724a;hp=0071715c759585df9dcd79868b1e36ff5a7b104f;hb=456c372a5785f69908f501f9eb82e688124c2ec3;hpb=68fc213265b95b5d724755d9af31b51c5ae6b77d diff --git a/src/common/plugin.c b/src/common/plugin.c index 0071715c7..0006d1a6e 100644 --- a/src/common/plugin.c +++ b/src/common/plugin.c @@ -19,11 +19,11 @@ #include +#include "defs.h" #include #include #include "intl.h" -#include "defs.h" #include "utils.h" #include "plugin.h" #include "prefs.h" @@ -31,40 +31,54 @@ struct _Plugin { gchar *filename; - gchar *name; GModule *module; - gchar *desc; + gchar *(*name) (void); + gchar *(*desc) (void); + gchar *(*type) (void); }; /** * List of all loaded plugins */ GSList *plugins = NULL; +GSList *plugin_types = NULL; -void plugin_save_list() +static gint list_find_by_string(gconstpointer data, gconstpointer str) { - gchar *rcpath; + return strcmp((gchar *)data, (gchar *)str) ? TRUE : FALSE; +} + +void plugin_save_list(void) +{ + gchar *rcpath, *block; PrefFile *pfile; - GSList *cur; + GSList *type_cur, *plugin_cur; Plugin *plugin; - rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "pluginsrc", NULL); - if ((pfile = prefs_write_open(rcpath)) == NULL) { - g_warning("failed to write plugin list\n"); - g_free(rcpath); - return; - } + for (type_cur = plugin_types; type_cur != NULL; type_cur = g_slist_next(type_cur)) { + rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); + block = g_strconcat("Plugins_", type_cur->data, NULL); + if ((pfile = prefs_write_open(rcpath)) == NULL || + (prefs_set_block_label(pfile, block) < 0)) { + g_warning("failed to write plugin list\n"); + g_free(rcpath); + return; + } + g_free(block); - for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) { - plugin = (Plugin *)cur->data; + for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) { + plugin = (Plugin *) plugin_cur->data; - fprintf(pfile->fp, "%s\n", plugin->filename); - } + if (!strcmp(plugin->type(), type_cur->data)) + fprintf(pfile->fp, "%s\n", plugin->filename); + } + fprintf(pfile->fp, "\n"); - if (prefs_write_close(pfile) < 0) - g_warning("failed to write plugin list\n"); + if (prefs_file_close(pfile) < 0) + g_warning("failed to write plugin list\n"); - g_free(rcpath); + g_free(rcpath); + } } /** @@ -78,6 +92,7 @@ gint plugin_load(const gchar *filename, gchar **error) { Plugin *plugin; gint (*plugin_init) (gchar **error); + gpointer plugin_name, plugin_desc, plugin_type; gint ok; g_return_val_if_fail(filename != NULL, -1); @@ -96,9 +111,10 @@ gint plugin_load(const gchar *filename, gchar **error) return -1; } - if (!g_module_symbol(plugin->module, "plugin_name", (gpointer *)&plugin->name) || - !g_module_symbol(plugin->module, "plugin_desc", (gpointer *)&plugin->desc) || - !g_module_symbol(plugin->module, "plugin_init", (gpointer *)&plugin_init)) { + if (!g_module_symbol(plugin->module, "plugin_name", &plugin_name) || + !g_module_symbol(plugin->module, "plugin_desc", &plugin_desc) || + !g_module_symbol(plugin->module, "plugin_type", &plugin_type) || + !g_module_symbol(plugin->module, "plugin_init", (gpointer *) &plugin_init)) { *error = g_strdup(g_module_error()); g_module_close(plugin->module); g_free(plugin); @@ -111,72 +127,98 @@ gint plugin_load(const gchar *filename, gchar **error) return ok; } + plugin->name = plugin_name; + plugin->desc = plugin_desc; + plugin->type = plugin_type; + plugin->filename = g_strdup(filename); + plugins = g_slist_append(plugins, plugin); - debug_print("Plugin %s (from file %s) loaded\n", plugin->name, plugin->filename); + debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename); return 0; } void plugin_unload(Plugin *plugin) { - void (*plugin_done) (); + void (*plugin_done) (void); if (g_module_symbol(plugin->module, "plugin_done", (gpointer *)&plugin_done)) { plugin_done(); } g_module_close(plugin->module); + plugins = g_slist_remove(plugins, plugin); + g_free(plugin->filename); g_free(plugin); } -void plugin_load_all() +void plugin_load_all(const gchar *type) { gchar *rcpath; gchar buf[BUFFSIZE]; - FILE *fp; + PrefFile *pfile; + gchar *error, *block; - rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "pluginsrc", NULL); - if ((fp = fopen(rcpath, "rb")) != NULL) { - gchar *error; + plugin_types = g_slist_append(plugin_types, g_strdup(type)); - while(fgets(buf, sizeof(buf), fp) != NULL) { - g_strstrip(buf); - - if (plugin_load(buf, &error) < 0) { - debug_print("plugin loading error: %s\n", error); - g_free(error); - } - } + rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); + block = g_strconcat("Plugins_", type, NULL); + if ((pfile = prefs_read_open(rcpath)) == NULL || + (prefs_set_block_label(pfile, block) < 0)) { + g_free(rcpath); + return; + } + g_free(block); + + while (fgets(buf, sizeof(buf), pfile->fp) != NULL) { + if (buf[0] == '[') + break; - fclose(fp); + g_strstrip(buf); + if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) { + g_warning("plugin loading error: %s\n", error); + g_free(error); + } } + prefs_file_close(pfile); g_free(rcpath); } -void plugin_unload_all() +void plugin_unload_all(const gchar *type) { - GSList *cur; + GSList *list, *cur; + + list = g_slist_copy(plugins); + list = g_slist_reverse(list); + + for(cur = list; cur != NULL; cur = g_slist_next(cur)) { + Plugin *plugin = (Plugin *) cur->data; + + if (!strcmp(type, plugin->type())) + plugin_unload(plugin); + } + g_slist_free(list); - for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) { - plugin_unload((Plugin *)cur->data); + cur = g_slist_find_custom(plugin_types, type, list_find_by_string); + if (cur) { + g_free(cur->data); + g_slist_remove(plugin_types, cur); } - g_slist_free(plugins); - plugins = NULL; } -GSList *plugin_get_list() +GSList *plugin_get_list(void) { return g_slist_copy(plugins); } const gchar *plugin_get_name(Plugin *plugin) { - return plugin->name; + return plugin->name(); } const gchar *plugin_get_desc(Plugin *plugin) { - return plugin->desc; + return plugin->desc(); }