/soc/2013/ankitkv/gobjectification: 725e37471387: Make actions a...

Ankit Vani a at nevitus.org
Sat Aug 10 14:42:05 EDT 2013


Changeset: 725e37471387bf0183743b64ee7791d45afc52af
Author:	 Ankit Vani <a at nevitus.org>
Date:	 2013-08-10 23:24 +0530
Branch:	 soc.2013.gobjectification.plugins
URL: https://hg.pidgin.im/soc/2013/ankitkv/gobjectification/rev/725e37471387

Description:

Make actions a callback again, so that a plugin/protocol can change them

diffstat:

 libpurple/plugins.c |  87 ++++++++++++++++++++++++++++------------------------
 libpurple/plugins.h |  60 ++++++++++++++++++++++--------------
 libpurple/prpl.c    |  25 +++++++-------
 libpurple/prpl.h    |  24 +++++++++----
 4 files changed, 112 insertions(+), 84 deletions(-)

diffs (truncated from 374 to 300 lines):

diff --git a/libpurple/plugins.c b/libpurple/plugins.c
--- a/libpurple/plugins.c
+++ b/libpurple/plugins.c
@@ -38,10 +38,12 @@ struct _PurplePluginInfoPrivate {
 	guint32 purple_abi;    /**< ABI version of purple required by the plugin */
 	char *category;        /**< The category the plugin belongs to           */
 	char *ui_requirement;  /**< ID of UI that is required to load the plugin */
-	GList *actions;        /**< Actions that the plugin can perform          */
 	gboolean loadable;     /**< Whether the plugin is loadable               */
 	char *error;           /**< Why the plugin is not loadable               */
 
+	/** Callback that returns a list of actions the plugin can perform */
+	PurplePluginGetActionsCallback get_actions;
+
 	/** Callback that returns a preferences frame for a plugin */
 	PurplePluginPrefFrameCallback get_pref_frame;
 
@@ -56,6 +58,7 @@ enum
 	PROP_PURPLE_ABI,
 	PROP_CATEGORY,
 	PROP_UI_REQUIREMENT,
+	PROP_GET_ACTIONS,
 	PROP_PREFERENCES_FRAME,
 	PROP_LAST
 };
@@ -249,43 +252,6 @@ purple_plugin_add_interface(PurplePlugin
 #endif
 }
 
-void
-purple_plugin_add_action(PurplePlugin *plugin, const char* label,
-                         PurplePluginActionCallback callback)
-{
-	PurplePluginInfoPrivate *priv;
-	PurplePluginAction *action;
-
-	g_return_if_fail(plugin != NULL);
-	g_return_if_fail(label != NULL && callback != NULL);
-
-	priv = PURPLE_PLUGIN_INFO_GET_PRIVATE(purple_plugin_get_info(plugin));
-
-	g_return_if_fail(priv != NULL);
-
-	action = g_new0(PurplePluginAction, 1);
-
-	action->label    = g_strdup(label);
-	action->callback = callback;
-	action->plugin   = plugin;
-
-	priv->actions = g_list_append(priv->actions, action);
-}
-
-GList *
-purple_plugin_get_actions(const PurplePlugin *plugin)
-{
-	PurplePluginInfoPrivate *priv;
-
-	g_return_val_if_fail(plugin != NULL, NULL);
-
-	priv = PURPLE_PLUGIN_INFO_GET_PRIVATE(purple_plugin_get_info(plugin));
-
-	g_return_val_if_fail(priv != NULL, NULL);
-
-	return priv->actions;
-}
-
 gboolean
 purple_plugin_is_internal(const PurplePlugin *plugin)
 {
@@ -355,6 +321,7 @@ purple_plugin_get_dependent_plugins(cons
 #define PROP_PURPLE_ABI_S         "purple-abi"
 #define PROP_CATEGORY_S           "category"
 #define PROP_UI_REQUIREMENT_S     "ui-requirement"
+#define PROP_GET_ACTIONS_S        "get-actions"
 #define PROP_PREFERENCES_FRAME_S  "preferences-frame"
 
 /* Set method for GObject properties */
@@ -375,6 +342,9 @@ purple_plugin_info_set_property(GObject 
 		case PROP_UI_REQUIREMENT:
 			priv->ui_requirement = g_strdup(g_value_get_string(value));
 			break;
+		case PROP_GET_ACTIONS:
+			priv->get_actions = g_value_get_pointer(value);
+			break;
 		case PROP_PREFERENCES_FRAME:
 			priv->get_pref_frame = g_value_get_pointer(value);
 			break;
@@ -398,6 +368,10 @@ purple_plugin_info_get_property(GObject 
 		case PROP_CATEGORY:
 			g_value_set_string(value, purple_plugin_info_get_category(info));
 			break;
+		case PROP_GET_ACTIONS:
+			g_value_set_pointer(value,
+					purple_plugin_info_get_actions_callback(info));
+			break;
 		case PROP_PREFERENCES_FRAME:
 			g_value_set_pointer(value,
 					purple_plugin_info_get_pref_frame_callback(info));
@@ -507,6 +481,12 @@ static void purple_plugin_info_class_ini
 		                  "ID of UI that is required by this plugin", NULL,
 		                  G_PARAM_WRITABLE));
 
+	g_object_class_install_property(obj_class, PROP_GET_ACTIONS,
+		g_param_spec_pointer(PROP_GET_ACTIONS_S,
+		                  "Plugin actions",
+		                  "Callback that returns list of the plugin's actions",
+		                  G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
 	g_object_class_install_property(obj_class, PROP_PREFERENCES_FRAME,
 		g_param_spec_pointer(PROP_PREFERENCES_FRAME_S,
 		                  "Preferences frame callback",
@@ -735,6 +715,16 @@ purple_plugin_info_get_dependencies(cons
 #endif
 }
 
+PurplePluginGetActionsCallback
+purple_plugin_info_get_actions_callback(const PurplePluginInfo *info)
+{
+	PurplePluginInfoPrivate *priv = PURPLE_PLUGIN_INFO_GET_PRIVATE(info);
+
+	g_return_val_if_fail(priv != NULL, NULL);
+
+	return priv->get_actions;
+}
+
 PurplePluginPrefFrameCallback
 purple_plugin_info_get_pref_frame_callback(const PurplePluginInfo *info)
 {
@@ -748,7 +738,23 @@ purple_plugin_info_get_pref_frame_callba
 /**************************************************************************
  * PluginAction API
  **************************************************************************/
-static void
+PurplePluginAction *
+purple_plugin_action_new(const char* label, PurplePluginActionCallback callback)
+{
+	PurplePluginAction *action;
+
+	g_return_if_fail(label != NULL && callback != NULL);
+
+	action = g_new0(PurplePluginAction, 1);
+
+	action->label    = g_strdup(label);
+	action->callback = callback;
+	action->plugin   = plugin;
+
+	return action;
+}
+
+void
 purple_plugin_action_free(PurplePluginAction *action)
 {
 	g_return_if_fail(action != NULL);
@@ -765,10 +771,9 @@ purple_plugin_action_copy(PurplePluginAc
 	g_return_val_if_fail(action != NULL, NULL);
 
 	action_copy = g_new(PurplePluginAction, 1);
+	*action_copy = *action;
 
 	action_copy->label    = g_strdup(action->label);
-	action_copy->callback = action->callback;
-	action_copy->plugin   = action->plugin;
 
 	return action_copy;
 }
diff --git a/libpurple/plugins.h b/libpurple/plugins.h
--- a/libpurple/plugins.h
+++ b/libpurple/plugins.h
@@ -97,6 +97,7 @@ typedef struct _PurplePluginAction Purpl
 #include "pluginpref.h"
 
 typedef void (*PurplePluginActionCallback)(PurplePluginAction *);
+typedef GList *(*PurplePluginGetActionsCallback)(PurplePlugin *);
 typedef PurplePluginPrefFrame *(*PurplePluginPrefFrameCallback)(PurplePlugin *);
 
 /**
@@ -140,6 +141,7 @@ struct _PurplePluginAction {
 	char *label;
 	PurplePluginActionCallback callback;
 	PurplePlugin *plugin;
+	gpointer *user_data;
 };
 
 /** Returns an ABI version to set in plugins using major and minor versions */
@@ -291,28 +293,6 @@ void purple_plugin_add_interface(PurpleP
                                  const GInterfaceInfo *interface_info);
 
 /**
- * Adds a new action to a plugin.
- *
- * @param plugin   The plugin to add the action to.
- * @param label    The description of the action to show to the user.
- * @param callback The callback to call when the user selects this action.
- */
-void purple_plugin_add_action(PurplePlugin *plugin, const char* label,
-                              PurplePluginActionCallback callback);
-
-/**
- * Returns a list of actions that a plugin can perform.
- *
- * @param plugin The plugin to get the actions from.
- *
- * @constreturn A list of #PurplePluginAction instances corresponding to the
- *              actions a plugin can perform.
- *
- * @see purple_plugin_add_action()
- */
-GList *purple_plugin_get_actions(const PurplePlugin *plugin);
-
-/**
  * Returns whether a plugin is an internal plugin. Internal plugins provide
  * required additional functionality to the libpurple core. These plugins must
  * not be shown in plugin lists. Examples of such plugins are in-tree protocol
@@ -399,8 +379,11 @@ GType purple_plugin_info_get_type(void);
  *                               SPDX.                                       \n
  * "purple-abi"         (guint32) The purple ABI version required by plugin. \n
  * "dependencies"       (GSList) List of plugin IDs required by the plugin.  \n
+ * "get-actions"        (PurplePluginGetActionsCallback) Callback that
+ *                               returns a list of actions the plugin can
+ *                               perform.                                    \n
  * "preferences-frame"  (PurplePluginPrefFrameCallback) Callback that returns
- *                                        a preferences frame for the plugin.
+ *                               a preferences frame for the plugin.
  *
  * Additionally, you can provide a "flags" property if the plugin is to be
  * distributed with libpurple, the value for which should be a bitwise
@@ -546,6 +529,20 @@ guint32 purple_plugin_info_get_abi_versi
 GSList *purple_plugin_info_get_dependencies(const PurplePluginInfo *info);
 
 /**
+ * Returns the callback that retrieves the list of actions a plugin can perform
+ * at that moment.
+ *
+ * @param info The plugin info to get the callback from.
+ *
+ * @constreturn The callback that returns a list of #PurplePluginAction
+ *              instances corresponding to the actions a plugin can perform.
+ *
+ * @see purple_plugin_add_action()
+ */
+PurplePluginGetActionsCallback
+purple_plugin_info_get_actions_callback(const PurplePluginInfo *info);
+
+/**
  * Returns the callback that retrieves the preferences frame for a plugin.
  *
  * @param info The plugin info to get the callback from.
@@ -567,6 +564,23 @@ purple_plugin_info_get_pref_frame_callba
  */
 GType purple_plugin_action_get_type(void);
 
+/**
+ * Allocates and returns a new PurplePluginAction. Use this to add actions in a
+ * list in the "get-actions" callback for your plugin.
+ *
+ * @param label    The description of the action to show to the user.
+ * @param callback The callback to call when the user selects this action.
+ */
+PurplePluginAction *purple_plugin_action_new(const char* label,
+		PurplePluginActionCallback callback);
+
+/**
+ * Frees a PurplePluginAction
+ *
+ * @param action The PurplePluginAction to free.
+ */
+void purple_plugin_action_free(PurplePluginAction *action);
+
 /*@}*/
 
 /**************************************************************************/
diff --git a/libpurple/prpl.c b/libpurple/prpl.c
--- a/libpurple/prpl.c
+++ b/libpurple/prpl.c
@@ -620,13 +620,12 @@ purple_prpl_got_media_caps(PurpleAccount
 #endif
 }
 
-void
-purple_protocol_add_action(PurplePluginProtocolInfo *prpl_info,
-		const char* label, PurpleProtocolActionCallback callback)
+PurpleProtocolAction *
+purple_protocol_action_new(const char* label,
+		PurpleProtocolActionCallback callback)
 {
 	PurpleProtocolAction *action;
 
-	g_return_if_fail(prpl_info != NULL);
 	g_return_if_fail(label != NULL && callback != NULL);
 
 	action = g_new0(PurpleProtocolAction, 1);
@@ -634,7 +633,16 @@ purple_protocol_add_action(PurplePluginP
 	action->label    = g_strdup(label);
 	action->callback = callback;
 
-	prpl_info->actions = g_list_append(prpl_info->actions, action);
+	return action;
+}
+
+void



More information about the Commits mailing list