/soc/2012/sanket/statscollector-2.x.y: 195032c25d68: Add Plugin ...

sanket sanket at soc.pidgin.im
Fri Aug 3 08:19:17 EDT 2012


Changeset: 195032c25d68673567695c024fd9498e06f88b63
Author:	 sanket <sanket at soc.pidgin.im>
Date:	 2012-08-02 22:28 +0530
Branch:	 soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/195032c25d68

Description:

Add Plugin Preference (Configure) box for Allow/deny collection

A Plugin Configuration box using the prefs API is added so that
a user can change his mind at any point of time. The pref box will
handle (de)registering of signals as and when the user will highlight
his preference through the configure box.

diffstat:

 libpurple/plugins/statscollector.c |  82 +++++++++++++++++++++++++++++++++++--
 1 files changed, 77 insertions(+), 5 deletions(-)

diffs (147 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
--- a/libpurple/plugins/statscollector.c
+++ b/libpurple/plugins/statscollector.c
@@ -26,10 +26,18 @@
 #include<sys/sysctl.h>
 #endif
 
+/* Prefs constants */
+#define PREF_PREFIX     "/plugins/core/statscollector"
+#define PREF_ALLOW      PREF_PREFIX "/allow"
+
 /* Allow/Deny Constants */
-#define ALLOW 0
-#define DENY 1
-#define ASK 2
+enum
+{
+  ALLOW,
+  DENY,
+  ASK
+} AllowSettings;
+
 /* Timeout */
 #define RESEND_SEC (24*3600)
 
@@ -57,7 +65,7 @@
 PurplePlugin *plugin_g;
 xmlnode *root_stats, *cpuinfo_xml, *ui_info;
 GHashTable *stats_acc_ht, *stats_plugins_ht, *stats_uis_ht;
-int save_timer = 0, send_handle = 0;
+int save_timer = 0, send_handle = 0, pref_cb_id = -1;
 
 /* Types of Operating Systems */
 enum OS_TYPES {WINDOWS, APPLE, UNIX};
@@ -66,6 +74,7 @@ enum BIT_32_64 {BIT_32, BIT_64};
 static void schedule_send(void);
 static gboolean send_stats();
 static gboolean plugin_load(PurplePlugin *);
+static gboolean plugin_unload(PurplePlugin *plugin);
 
 static void
 confirm_allow(){
@@ -1091,6 +1100,28 @@ show_confirm_dialog(gpointer data){
 
 }
 
+static void
+pref_allow_cb(const char *name, PurplePrefType type, \
+    gconstpointer val, gpointer data){
+
+  purple_debug_info("STATS PREF", "Pref %s has changed to %d", name, (int)val);
+
+  switch((int)val){
+
+    case ALLOW:
+      plugin_load(plugin_g);
+      break;
+
+    case DENY:
+    case ASK:
+      /* Remove all timeouts and signal handles */
+      plugin_unload(plugin_g);
+      break;
+  }
+
+
+}
+
 static gboolean
 plugin_load(PurplePlugin *plugin) {
 
@@ -1103,6 +1134,13 @@ plugin_load(PurplePlugin *plugin) {
   /* Save a pointer to the plugin for global use */
   plugin_g = plugin;
 
+  /* Register callback to Allow prefs */
+  if(pref_cb_id == -1){
+    pref_cb_id = \
+      purple_prefs_connect_callback(NULL, PREF_ALLOW, pref_allow_cb, NULL);
+  }
+
+
   /* Check if the allow is indeed ASK, in that case we *can* assume
    * that this is the first time the user has used the plugin AND
    * ask him to verify whether he would like to allow collecting stats
@@ -1113,6 +1151,7 @@ plugin_load(PurplePlugin *plugin) {
   if(check_ask == ASK){
     purple_timeout_add_seconds(5, show_confirm_dialog, NULL);
   }
+
   else if(check_ask == ALLOW){
 
     /* Load the stats file into a global variable for any updations */
@@ -1139,11 +1178,44 @@ plugin_unload(PurplePlugin *plugin){
 
   /* Remove any timers which might be executing in a loop! */
   if(send_handle) purple_timeout_remove(send_handle);
+  purple_signals_disconnect_by_handle(plugin);
 
   return TRUE;
 
 }
 
+static PurplePluginPrefFrame *
+get_plugin_pref_frame(PurplePlugin *plugin)
+{
+	PurplePluginPrefFrame *frame;
+	PurplePluginPref *pref;
+
+	frame = purple_plugin_pref_frame_new();
+
+
+	pref = purple_plugin_pref_new_with_name_and_label(PREF_ALLOW, \
+					_("Allow anonymous collection and sending of stats"));
+	purple_plugin_pref_set_type(pref, PURPLE_PLUGIN_PREF_CHOICE);
+	purple_plugin_pref_add_choice(pref, _("Allow"), GINT_TO_POINTER(ALLOW));
+	purple_plugin_pref_add_choice(pref, _("Deny"), GINT_TO_POINTER(DENY));
+	purple_plugin_pref_add_choice(pref, _("Later"), GINT_TO_POINTER(ASK));
+	purple_plugin_pref_frame_add(frame, pref);
+
+	return frame;
+}
+
+static PurplePluginUiInfo prefs_info = {
+	get_plugin_pref_frame,
+	0,
+	NULL,
+
+	/* padding */
+	NULL,
+	NULL,
+	NULL,
+	NULL
+};
+
 static PurplePluginInfo info =
 {
 	PURPLE_PLUGIN_MAGIC,
@@ -1170,7 +1242,7 @@ static PurplePluginInfo info =
 	NULL,                                           /**< destroy */
 	NULL,                                           /**< ui_info */
 	NULL,                                           /**< extra_info */
-	NULL,                                           /**< prefs_info */
+	&prefs_info,                                    /**< prefs_info */
 	NULL,                                           /**< actions */
 
 	/* padding */



More information about the Commits mailing list