gplugin/gplugin

Update GPluginGtkView to load and unload plugins when the toggle button is toggled
feature/gplugin-gtk-view-cleanups
2019-11-07, Gary Kramlich
6d8d5f52235a
Parents 758a4592f5f8
Children 321a2d04a07b
Update GPluginGtkView to load and unload plugins when the toggle button is toggled
--- a/gplugin-gtk/gplugin-gtk-view.c Thu Nov 07 20:39:14 2019 -0600
+++ b/gplugin-gtk/gplugin-gtk-view.c Thu Nov 07 21:20:25 2019 -0600
@@ -18,6 +18,8 @@
#include <gplugin-gtk/gplugin-gtk-view.h>
#include <gplugin-gtk/gplugin-gtk-store.h>
+#include <gplugin/gplugin.h>
+
/**
* SECTION:gplugin-gtk-view
* @Title: Gtk+ View for gplugins
@@ -59,7 +61,76 @@
static GParamSpec *properties[N_PROPERTIES] = {NULL,};
/******************************************************************************
- * GObject Stuff
+ * Callbacks
+ *****************************************************************************/
+static void
+gplugin_gtk_view_plugin_toggled_cb(GtkCellRendererToggle *rend,
+ gchar *path,
+ gpointer data)
+{
+ GPluginGtkView *view = GPLUGIN_GTK_VIEW(data);
+ GPluginPlugin *plugin = NULL;
+ GPluginPluginState state;
+ GtkTreeModel *model = NULL;
+ GtkTreeIter iter;
+ GtkTreePath *tree_path = NULL;
+ gboolean loaded = FALSE;
+
+ tree_path = gtk_tree_path_new_from_string(path);
+
+ model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
+ gtk_tree_model_get_iter(model, &iter, tree_path);
+ gtk_tree_path_free(tree_path);
+
+ gtk_tree_model_get(
+ model,
+ &iter,
+ GPLUGIN_GTK_STORE_PLUGIN_COLUMN, &plugin,
+ -1
+ );
+
+ if(!GPLUGIN_IS_PLUGIN(plugin)) {
+ return;
+ }
+
+ state = gplugin_plugin_get_state(plugin);
+ if(state == GPLUGIN_PLUGIN_STATE_LOADED) {
+ GError *error = NULL;
+
+ gboolean unloaded = gplugin_manager_unload_plugin(plugin, &error);
+
+ if(error != NULL) {
+ g_warning("Failed to unload plugin: %s", error->message);
+
+ g_error_free(error);
+ }
+
+ /* if unload failed, unloaded is set to false, which mean the plugin is
+ * still loaded. So we set loaded to the negated value of unloaded.
+ */
+ loaded = !unloaded;
+ } else {
+ GError *error = NULL;
+
+ loaded = gplugin_manager_load_plugin(plugin, &error);
+
+ if(error != NULL) {
+ g_warning("Failed to load plugin: %s", error->message);
+
+ g_error_free(error);
+ }
+ }
+
+ gtk_list_store_set(
+ GTK_LIST_STORE(model),
+ &iter,
+ GPLUGIN_GTK_STORE_LOADED_COLUMN, loaded,
+ -1
+ );
+}
+
+/******************************************************************************
+ * GObject Implementation
*****************************************************************************/
G_DEFINE_TYPE(GPluginGtkView, gplugin_gtk_view, GTK_TYPE_TREE_VIEW);
@@ -119,6 +190,8 @@
rend = gtk_cell_renderer_toggle_new();
gtk_tree_view_column_pack_start(col, rend, FALSE);
+ g_signal_connect(G_OBJECT(rend), "toggled",
+ G_CALLBACK(gplugin_gtk_view_plugin_toggled_cb), view);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
gtk_tree_view_column_add_attribute(col, rend, "active",