changeset 20716:227e397d8e80

Add a 'Install plugin...' button in the plugins dialog.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sat, 29 Sep 2007 08:53:53 +0000
parents df58938e9f61
children 7281d72e316a
files finch/gntplugin.c
diffstat 1 files changed, 92 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/finch/gntplugin.c	Sat Sep 29 08:52:41 2007 +0000
+++ b/finch/gntplugin.c	Sat Sep 29 08:53:53 2007 +0000
@@ -32,6 +32,7 @@
 
 #include "finch.h"
 
+#include "debug.h"
 #include "notify.h"
 #include "request.h"
 
@@ -237,6 +238,93 @@
 	}
 }
 
+static void
+install_selected_file_cb(gpointer handle, const char *filename)
+{
+	/* Try to init the selected file.
+	 * If it succeeds, try to make a copy of the file in $USERDIR/plugins/.
+	 * If the copy succeeds, unload and destroy the plugin in the original
+	 *  location and init+load the new one.
+	 * Select the plugin in the plugin list.
+	 */
+	char *path;
+	PurplePlugin *plugin;
+
+	g_return_if_fail(plugins.window);
+	
+	plugin = purple_plugin_probe(filename);
+	if (!plugin) {
+		purple_notify_error(handle, _("Error loading plugin"),
+				_("The selected file is not a valid plugin."),
+				_("Please open the debug window and try again to see the exact error message."));
+		return;
+	}
+	if (g_list_find(gnt_tree_get_rows(GNT_TREE(plugins.tree)), plugin)) {
+		purple_plugin_load(plugin);
+		gnt_tree_set_choice(GNT_TREE(plugins.tree), plugin, purple_plugin_is_loaded(plugin));
+		gnt_tree_set_selected(GNT_TREE(plugins.tree), plugin);
+		return;
+	}
+
+	path = g_build_filename(purple_user_dir(), "plugins", NULL);
+	if (purple_build_dir(path, S_IRUSR | S_IWUSR | S_IXUSR) == 0) {
+		char *content = NULL;
+		gsize length = 0;
+
+		if (g_file_get_contents(filename, &content, &length, NULL)) {
+			char *file = g_path_get_basename(filename);
+			g_free(path);
+			path = g_build_filename(purple_user_dir(), "plugins", file, NULL);
+			if (purple_util_write_data_to_file_absolute(path, content, length)) {
+				purple_plugin_destroy(plugin);
+				plugin = purple_plugin_probe(path);
+				if (!plugin) {
+					purple_debug_warning("gntplugin", "This is really strange. %s can be loaded, but %s can't!\n",
+							filename, path);
+					g_unlink(path);
+					plugin = purple_plugin_probe(filename);
+				}
+			} else {
+			}
+		}
+		g_free(content);
+	}
+	g_free(path);
+
+	purple_plugin_load(plugin);
+
+	if (plugin->info->type == PURPLE_PLUGIN_LOADER) {
+		GList *cur;
+		for (cur = PURPLE_PLUGIN_LOADER_INFO(plugin)->exts; cur != NULL;
+				cur = cur->next)
+			purple_plugins_probe(cur->data);
+		return;
+	}
+
+	if (plugin->info->type != PURPLE_PLUGIN_STANDARD ||
+			(plugin->info->flags & PURPLE_PLUGIN_FLAG_INVISIBLE) ||
+			plugin->error)
+		return;
+
+	gnt_tree_add_choice(GNT_TREE(plugins.tree), plugin,
+			gnt_tree_create_row(GNT_TREE(plugins.tree), plugin->info->name), NULL, NULL);
+	gnt_tree_set_choice(GNT_TREE(plugins.tree), plugin, purple_plugin_is_loaded(plugin));
+	gnt_tree_set_row_flags(GNT_TREE(plugins.tree), plugin, GNT_TEXT_FLAG_BOLD);
+	gnt_tree_set_selected(GNT_TREE(plugins.tree), plugin);
+}
+
+static void
+install_plugin_cb(GntWidget *w, gpointer null)
+{
+	static int handle;
+
+	purple_request_close_with_handle(&handle);
+	purple_request_file(&handle, _("Select plugin to install"), NULL,
+			FALSE, G_CALLBACK(install_selected_file_cb), NULL,
+			NULL, NULL, NULL, &handle);
+	g_signal_connect_swapped(G_OBJECT(w), "destroy", G_CALLBACK(purple_request_close_with_handle), &handle);
+}
+
 void finch_plugins_show_all()
 {
 	GntWidget *window, *tree, *box, *aboot, *button;
@@ -308,6 +396,10 @@
 	box = gnt_hbox_new(FALSE);
 	gnt_box_add_widget(GNT_BOX(window), box);
 
+	button = gnt_button_new(_("Install Plugin..."));
+	gnt_box_add_widget(GNT_BOX(box), button);
+	g_signal_connect(G_OBJECT(button), "activate", G_CALLBACK(install_plugin_cb), NULL);
+
 	button = gnt_button_new(_("Close"));
 	gnt_box_add_widget(GNT_BOX(box), button);
 	g_signal_connect_swapped(G_OBJECT(button), "activate",