* src/common/plugin.[ch]
[claws.git] / src / common / plugin.c
index c704aca6fe81e9cb83178c6ce3e5e6534a4c5003..887726cb13b2dda5fc116c374c142485e89c0267 100644 (file)
 
 #include <stdio.h>
 
+#include "defs.h"
 #include <glib.h>
 #include <gmodule.h>
 
 #include "intl.h"
-#include "defs.h"
 #include "utils.h"
 #include "plugin.h"
 #include "prefs.h"
@@ -32,40 +32,53 @@ struct _Plugin
 {
        gchar   *filename;
        GModule *module;
-       gchar   *(*name) ();
-       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, COMMON_RC, NULL);
-       if ((pfile = prefs_write_open(rcpath)) == NULL ||
-           (prefs_set_block_label(pfile, "Plugins") < 0)) {
-               g_warning("failed to write plugin list\n");
-               g_free(rcpath);
-               return;
-       }
-
-       for (cur = plugins; cur != NULL; cur = g_slist_next(cur)) {
-               plugin = (Plugin *)cur->data;
+       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 (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_file_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); 
+       }
 }
 
 /**
@@ -79,7 +92,7 @@ gint plugin_load(const gchar *filename, gchar **error)
 {
        Plugin *plugin;
        gint (*plugin_init) (gchar **error);
-       gchar *plugin_name, *plugin_desc;
+       gpointer plugin_name, plugin_desc, plugin_type;
        gint ok;
 
        g_return_val_if_fail(filename != NULL, -1);
@@ -98,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);
@@ -115,6 +129,7 @@ gint plugin_load(const gchar *filename, gchar **error)
 
        plugin->name = plugin_name;
        plugin->desc = plugin_desc;
+       plugin->type = plugin_type;
        plugin->filename = g_strdup(filename);
 
        plugins = g_slist_append(plugins, plugin);
@@ -133,30 +148,36 @@ void plugin_unload(Plugin *plugin)
        }
 
        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];
        PrefFile *pfile;
-       gchar *error;
+       gchar *error, *block;
+
+       plugin_types = g_slist_append(plugin_types, g_strdup(type));
 
        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, "Plugins") < 0)) {
+           (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;
-                       
+
                g_strstrip(buf);
-               if (plugin_load(buf, &error) < 0) {
-                       debug_print("plugin loading error: %s\n", error);
+               if ((buf[0] != '\0') && (plugin_load(buf, &error) < 0)) {
+                       g_warning("plugin loading error: %s\n", error);
                        g_free(error);
                }                                                       
        }
@@ -165,18 +186,29 @@ void plugin_load_all()
        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);
 }