* src/common/plugin.[ch]
[claws.git] / src / common / plugin.c
index b378794d8602ee33ee3487a12b5f2e898c500904..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"
 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,8 +127,15 @@ 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(), filename);
+
        return 0;
 }
 
@@ -125,56 +148,77 @@ 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];
-       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();
 }