2007-09-16 [colin] 3.0.0cvs17
[claws.git] / src / common / plugin.c
index 83289efca46c2c886ab4bea30ee015d3a0e57f77..20f942e0c12871c2d81499849257e5758c3d7332 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail Team
+ * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #include <stdio.h>
 #include "plugin.h"
 #include "prefs.h"
 #include "claws.h"
+#include "timing.h"
 
+#ifdef HAVE_VALGRIND
+#include "valgrind.h"
+#endif
 struct _Plugin
 {
        gchar   *filename;
@@ -47,11 +51,13 @@ struct _Plugin
        
        GSList *rdeps;
        gchar *error;
+       gboolean unloaded_hidden;
 };
 
 const gchar *plugin_feature_names[] =
        { N_("Nothing"),
          N_("a viewer"),
+         N_("a MIME parser"),
          N_("folders"),
          N_("filtering"),
          N_("a privacy interface"),
@@ -114,11 +120,17 @@ void plugin_save_list(void)
                for (plugin_cur = plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
                        plugin = (Plugin *) plugin_cur->data;
                        
+                       if (plugin->unloaded_hidden)
+                               continue;
+
                        if (!strcmp(plugin->type(), type_cur->data))
                                fprintf(pfile->fp, "%s\n", plugin->filename);
                }
                for (plugin_cur = unloaded_plugins; plugin_cur != NULL; plugin_cur = g_slist_next(plugin_cur)) {
                        plugin = (Plugin *) plugin_cur->data;
+
+                       if (plugin->unloaded_hidden)
+                               continue;
                        
                        if (!strcmp(plugin->type(), type_cur->data))
                                fprintf(pfile->fp, "%s\n", plugin->filename);
@@ -250,6 +262,8 @@ static gchar *plugin_check_features(struct PluginFeature *features) {
        for(; cur; cur = cur->next) {
                Plugin *p = (Plugin *)cur->data;
                struct PluginFeature *cur_features = p->provides();
+               if (p->unloaded_hidden)
+                       continue;
                for (j = 0; cur_features[j].type != PLUGIN_NOTHING; j++) {
                        for (i = 0; features[i].type != PLUGIN_NOTHING; i++) {
                                if (cur_features[j].type == features[i].type &&
@@ -282,16 +296,21 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
        const gchar *(*plugin_type)(void);
        const gchar *(*plugin_licence)(void);
        struct PluginFeature *(*plugin_provides)(void);
-
        gint ok;
-
+       START_TIMING((filename?filename:"NULL plugin"));
        g_return_val_if_fail(filename != NULL, NULL);
        g_return_val_if_fail(error != NULL, NULL);
 
        /* check duplicate plugin path name */
        if (plugin_is_loaded(filename)) {
-               *error = g_strdup(_("Plugin already loaded"));
-               return NULL;            
+               plugin = plugin_get_by_filename(filename);
+               if (plugin->unloaded_hidden) {
+                       /* reshow it */
+                       goto init_plugin;
+               } else {
+                       *error = g_strdup(_("Plugin already loaded"));
+                       return NULL;            
+               }
        }                              
        
        plugin_remove_from_unloaded_list(filename);
@@ -312,6 +331,7 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
                return NULL;
        }
 
+init_plugin:
        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_version", &plugin_version) ||
@@ -320,14 +340,18 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
            !g_module_symbol(plugin->module, "plugin_provides", (gpointer)&plugin_provides) ||
            !g_module_symbol(plugin->module, "plugin_init", (gpointer)&plugin_init)) {
                *error = g_strdup(g_module_error());
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
        }
        
-       if (strcmp(plugin_licence(), "GPL")
-       &&  strncmp(plugin_licence(), "GPL-compatible", strlen("GPL-compatible"))) {
-               *error = g_strdup(_("This module is not licenced under a GPL compatible licence."));
+       if (strcmp(plugin_licence(), "GPL2+") && strncmp(plugin_licence(), "GPL3", strlen("GPL3"))
+       &&  strncmp(plugin_licence(), "GPL2+-compatible", strlen("GPL2+-compatible"))) {
+               *error = g_strdup(_("This module is not licenced under a GPL v2 or later compatible licence."));
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
@@ -335,12 +359,16 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
 
        if (!strcmp(plugin_type(), "GTK")) {
                *error = g_strdup(_("This module is for Claws Mail GTK1."));
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
        }
 
        if ((*error = plugin_check_features(plugin_provides())) != NULL) {
+               if (plugin->unloaded_hidden)
+                       return NULL;
                g_module_close(plugin->module);
                g_free(plugin);
                return NULL;
@@ -353,6 +381,7 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
        plugin->provides = plugin_provides;
        plugin->filename = g_strdup(filename);
        plugin->error = NULL;
+
        if ((ok = plugin_init(error)) < 0) {
                if (*error)
                        plugin->error = g_strdup(*error);
@@ -360,34 +389,48 @@ Plugin *plugin_load(const gchar *filename, gchar **error)
                return NULL;
        }
 
-       plugins = g_slist_append(plugins, plugin);
+       if (!plugin->unloaded_hidden)
+               plugins = g_slist_append(plugins, plugin);
+       plugin->unloaded_hidden = FALSE;
 
        debug_print("Plugin %s (from file %s) loaded\n", plugin->name(), filename);
-
+       END_TIMING();
        return plugin;
 }
 
 void plugin_unload(Plugin *plugin)
 {
-       void (*plugin_done) (void);
+       gboolean (*plugin_done) (void);
+       gboolean can_unload = TRUE;
 
        plugin_unload_rdeps(plugin);
 
+       if (plugin->unloaded_hidden)
+               return;
+
        if (plugin->error) {
                plugin_remove_from_unloaded_list(plugin->filename);
                return;
        }
        if (g_module_symbol(plugin->module, "plugin_done", (gpointer) &plugin_done)) {
-               plugin_done();
+               can_unload = plugin_done();
        }
 
-       /* comment this line when running valgrind's leak-check if
-        * you want to be able to get symbols from plugins */
-       g_module_close(plugin->module);
+       if (can_unload) {
+#ifdef HAVE_VALGRIND
+               if (!RUNNING_ON_VALGRIND) {
+                       g_module_close(plugin->module);
+               }
+#else
+               g_module_close(plugin->module);
+#endif
+               plugins = g_slist_remove(plugins, plugin);
+               g_free(plugin->filename);
+               g_free(plugin);
+       } else {
+               plugin->unloaded_hidden = TRUE;
+       }
 
-       plugins = g_slist_remove(plugins, plugin);
-       g_free(plugin->filename);
-       g_free(plugin);
 }
 
 void plugin_load_all(const gchar *type)
@@ -497,7 +540,15 @@ void plugin_load_standard_plugins (void)
 
 GSList *plugin_get_list(void)
 {
-       return g_slist_copy(plugins);
+       GSList *new = NULL;
+       GSList *cur = plugins;
+       for (; cur; cur = cur->next) {
+               Plugin *p = (Plugin *)cur->data;
+               if (!p->unloaded_hidden)
+                       new = g_slist_prepend(new, p);
+       }
+       new = g_slist_reverse(new);
+       return new;
 }
 
 GSList *plugin_get_unloaded_list(void)