Initial start of PidginProtocolStore

Wed, 09 Oct 2019 05:42:34 -0500

author
Gary Kramlich <grim@reaperworld.com>
date
Wed, 09 Oct 2019 05:42:34 -0500
changeset 40296
c9900fc6e689
parent 40285
8177902f9562
child 40297
082c07053a79

Initial start of PidginProtocolStore

pidgin/meson.build file | annotate | diff | comparison | revisions
pidgin/pidginprotocolstore.c file | annotate | diff | comparison | revisions
pidgin/pidginprotocolstore.h file | annotate | diff | comparison | revisions
pidgin/plugins/meson.build file | annotate | diff | comparison | revisions
pidgin/plugins/widget-test.c file | annotate | diff | comparison | revisions
--- a/pidgin/meson.build	Fri Feb 14 07:46:31 2020 +0000
+++ b/pidgin/meson.build	Wed Oct 09 05:42:34 2019 -0500
@@ -44,6 +44,7 @@
 	'pidginmessage.c',
 	'pidginplugininfo.c',
 	'pidginpluginsdialog.c',
+	'pidginprotocolstore.c',
 	'pidgintalkatu.c',
 	'pidgintooltip.c',
 ]
@@ -96,6 +97,7 @@
 	'pidginmessage.h',
 	'pidginplugininfo.h',
 	'pidginpluginsdialog.h',
+	'pidginprotocolstore.h',
 	'pidgintalkatu.h',
 	'pidgintooltip.h',
 	'pidgin.h',
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pidgin/pidginprotocolstore.c	Wed Oct 09 05:42:34 2019 -0500
@@ -0,0 +1,161 @@
+/* pidgin
+ *
+ * Pidgin is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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  02111-1301 USA
+ */
+
+#include "pidgin/pidginprotocolstore.h"
+
+struct _PidginProtocolStore {
+	GtkListStore parent;
+};
+
+/******************************************************************************
+ * Helpers
+ *****************************************************************************/
+static void
+pidgin_protocol_store_add_protocol(PidginProtocolStore *store,
+                                   PurpleProtocol *protocol)
+{
+	GtkTreeIter iter;
+
+	gtk_list_store_append(GTK_LIST_STORE(store), &iter);
+	gtk_list_store_set(
+		GTK_LIST_STORE(store),
+		&iter,
+		PIDGIN_PROTOCOL_STORE_COLUMN_PROTOCOL, protocol,
+		PIDGIN_PROTOCOL_STORE_COLUMN_NAME, purple_protocol_get_name(protocol),
+		-1
+	);
+}
+
+static void
+pidgin_protocol_store_add_protocols(PidginProtocolStore *store) {
+	GList *protocols = purple_protocols_get_all(), *l;
+
+	for(l = protocols; l != NULL; l = l->data) {
+		if(PURPLE_IS_PROTOCOL(l->data)) {
+			pidgin_protocol_store_add_protocol(store, PURPLE_PROTOCOL(l->data));
+		}
+	}
+}
+
+static void
+pidgin_protocol_store_remove_protocol(PidginProtocolStore *store,
+                                      PurpleProtocol *protocol)
+{
+	GtkTreeIter iter;
+
+	if(gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter) == FALSE) {
+		purple_debug_warning("protocol-store",
+		                     "protocol %s was removed but not in the store",
+		                     purple_protocol_get_name(protocol));
+		return;
+	}
+
+	/* walk through the store and look for the protocol to remove */
+	do {
+		PurpleProtocol *prpl = NULL;
+
+		gtk_tree_model_get(
+			GTK_TREE_MODEL(store),
+			&iter,
+			PIDGIN_PROTOCOL_STORE_COLUMN_PROTOCOL, &prpl,
+			-1
+		);
+
+		if(prpl == protocol) {
+			g_object_unref(G_OBJECT(prpl));
+
+			gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
+
+			return;
+		}
+	} while(gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
+}
+
+/******************************************************************************
+ * Callbacks
+ *****************************************************************************/
+static void
+pidgin_protocol_store_protocol_added_cb(PurpleProtocol *protocol,
+                                        gpointer data)
+{
+	pidgin_protocol_store_add_protocol(PIDGIN_PROTOCOL_STORE(data), protocol);
+}
+
+static void
+pidgin_protocol_store_protocol_removed_cb(PurpleProtocol *protocol,
+                                          gpointer data)
+{
+	pidgin_protocol_store_remove_protocol(PIDGIN_PROTOCOL_STORE(data), protocol);
+}
+
+/******************************************************************************
+ * GObject Implementation
+ *****************************************************************************/
+G_DEFINE_TYPE(PidginProtocolStore, pidgin_protocol_store, GTK_TYPE_LIST_STORE)
+
+static void
+pidgin_protocol_store_init(PidginProtocolStore *store) {
+	gpointer protocols_handle = NULL;
+	GType types[PIDGIN_PROTOCOL_STORE_N_COLUMNS] = {
+		PURPLE_TYPE_PROTOCOL,
+		G_TYPE_STRING,
+	};
+
+	gtk_list_store_set_column_types(
+		GTK_LIST_STORE(store),
+		PIDGIN_PROTOCOL_STORE_N_COLUMNS,
+		types
+	);
+
+	/* add the known protocols */
+	pidgin_protocol_store_add_protocols(store);
+
+	/* add the signal handlers to dynamically add/remove protocols */
+	protocols_handle = purple_protocols_get_handle();
+	purple_signal_connect(protocols_handle, "protocol-added", store,
+	                      G_CALLBACK(pidgin_protocol_store_protocol_added_cb),
+	                      store);
+	purple_signal_connect(protocols_handle, "protocol-removed", store,
+	                      G_CALLBACK(pidgin_protocol_store_protocol_removed_cb),
+	                      store);
+}
+
+static void
+pidgin_protocol_store_finalize(GObject *obj) {
+	purple_signals_disconnect_by_handle(obj);
+
+	G_OBJECT_CLASS(pidgin_protocol_store_parent_class)->finalize(obj);
+}
+
+static void
+pidgin_protocol_store_class_init(PidginProtocolStoreClass *klass) {
+	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
+
+	obj_class->finalize = pidgin_protocol_store_finalize;
+}
+
+/******************************************************************************
+ * API
+ *****************************************************************************/
+GtkListStore *
+pidgin_protocol_store_new(void) {
+	return GTK_LIST_STORE(g_object_new(PIDGIN_TYPE_PROTOCOL_STORE, NULL));
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pidgin/pidginprotocolstore.h	Wed Oct 09 05:42:34 2019 -0500
@@ -0,0 +1,67 @@
+/* pidgin
+ *
+ * Pidgin is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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  02111-1301  USA
+ */
+
+#ifndef PIDGIN_PROTOCOL_STORE_H
+#define PIDGIN_PROTOCOL_STORE_H
+
+/**
+ * SECTION:pidginprotocolstore
+ * @section_id: pidgin-protocol-store
+ * @short_description: A GtkListStore that keeps track of protocols
+ * @title: Protocol Store
+ *
+ * #PidginProtocolStore is a #GtkListStore that automatically keeps track of
+ * what protocols are currently available in libpurple.  It's intended to be
+ * used any time that you need to present a protocol selection to the user.
+ */
+
+#include <gtk/gtk.h>
+
+#include <purple.h>
+
+typedef enum {
+	PIDGIN_PROTOCOL_STORE_COLUMN_PROTOCOL,
+	PIDGIN_PROTOCOL_STORE_COLUMN_NAME,
+	PIDGIN_PROTOCOL_STORE_N_COLUMNS,
+} PidginProtocolStoreColumn;
+
+G_BEGIN_DECLS
+
+#define PIDGIN_TYPE_PROTOCOL_STORE pidgin_protocol_store_get_type()
+
+G_DECLARE_FINAL_TYPE(PidginProtocolStore, pidgin_protocol_store, PIDGIN,
+                     PROTOCOL_STORE, GtkListStore)
+
+/**
+ * pidgin_protocol_store_new:
+ *
+ * Creates a new #PidginProtocolStore that can be used anywhere a #GtkListStore
+ * can be used.
+ *
+ * Returns: (transfer full): The new #PidginProtocolStore instance.
+ *
+ * Since: 3.0.0
+ */
+GtkListStore *pidgin_protocol_store_new(void);
+
+G_END_DECLS
+
+#endif /* PIDGIN_PROTOCOL_STORE_H */
--- a/pidgin/plugins/meson.build	Fri Feb 14 07:46:31 2020 +0000
+++ b/pidgin/plugins/meson.build	Wed Oct 09 05:42:34 2019 -0500
@@ -85,4 +85,11 @@
 		    name_prefix : '',
 		    install : true, install_dir : PIDGIN_PLUGINDIR)
 	endif
+
+	widget_test = library('widget-test', 'widget-test.c',
+		dependencies : [libpurple_dep, libpidgin_dep, glib],
+		name_prefix : '',
+		build_by_default: false,
+		install : false, install_dir : PIDGIN_PLUGINDIR)
+
 endif # PLUGINS
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pidgin/plugins/widget-test.c	Wed Oct 09 05:42:34 2019 -0500
@@ -0,0 +1,94 @@
+/* pidgin
+ *
+ * Pidgin is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * 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
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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  02111-1301  USA
+ */
+#include <glib.h>
+#include <gmodule.h>
+#include <gplugin.h>
+
+#include <purple.h>
+
+#include "gtkplugin.h"
+#include "pidginprotocolstore.h"
+
+/* make the compiler happy... */
+G_MODULE_EXPORT GPluginPluginInfo *gplugin_query(GError **error);
+G_MODULE_EXPORT gboolean gplugin_load(GPluginPlugin *plugin, GError **error);
+G_MODULE_EXPORT gboolean gplugin_unload(GPluginPlugin *plugin, GError **error);
+
+/******************************************************************************
+ * Config Frame
+ *****************************************************************************/
+static GtkWidget *
+pidgin_widget_test_config_frame(G_GNUC_UNUSED PurplePlugin *plugin) {
+	GtkWidget *vbox = NULL, *hbox = NULL;
+	GtkWidget *label = NULL, *combo = NULL;
+	GtkListStore *store = NULL;
+
+	vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
+
+	hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
+	gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+
+	label = gtk_label_new("Protocols:");
+	gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+
+	store = pidgin_protocol_store_new();
+	combo = gtk_combo_box_new_with_model(GTK_TREE_MODEL(store));
+	gtk_box_pack_start(GTK_BOX(hbox), combo, FALSE, FALSE, 0);
+
+	gtk_widget_show_all(vbox);
+
+	return vbox;
+}
+
+/******************************************************************************
+ * Plugin Exports
+ *****************************************************************************/
+G_MODULE_EXPORT GPluginPluginInfo *
+gplugin_query(GError **error) {
+	PidginPluginInfo *info = NULL;
+
+	const gchar * const authors[] = {
+		"Gary Kramlich <grim@reaperworld.com>",
+		NULL
+	};
+
+	info = pidgin_plugin_info_new(
+		"id", "pidgin/widget-test",
+		"abi-version", PURPLE_ABI_VERSION,
+		"name", "Pidgin Widget Test",
+		"version", "0.0.1",
+		"authors", authors,
+		"gtk-config-frame-cb", pidgin_widget_test_config_frame,
+		NULL
+	);
+
+	return GPLUGIN_PLUGIN_INFO(info);
+}
+
+G_MODULE_EXPORT gboolean
+gplugin_load(GPluginPlugin *plugin, GError **error) {
+	return TRUE;
+}
+
+G_MODULE_EXPORT gboolean
+gplugin_unload(GPluginPlugin *plugin, GError **error) {
+	return TRUE;
+}

mercurial