changeset 16310:8c89913276b3

Implement the plugin-pref ui using the request api. The preferences for the core plugins can now be modified from Finch. And no new strings.
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sun, 22 Apr 2007 23:55:24 +0000
parents 03532b86c20f
children 2195e8f2518d
files finch/gntplugin.c finch/gntplugin.h finch/gntprefs.c finch/gntrequest.c finch/gntrequest.h
diffstat 5 files changed, 128 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/finch/gntplugin.c	Sun Apr 22 18:20:05 2007 +0000
+++ b/finch/gntplugin.c	Sun Apr 22 23:55:24 2007 +0000
@@ -30,9 +30,11 @@
 #include <gnttree.h>
 
 #include "notify.h"
+#include "request.h"
 
 #include "finch.h"
 #include "gntplugin.h"
+#include "gntrequest.h"
 
 static struct
 {
@@ -44,6 +46,8 @@
 
 static GHashTable *confwins;
 
+static void process_pref_frame(PurplePluginPrefFrame *frame);
+
 static void
 decide_conf_button(PurplePlugin *plugin)
 {
@@ -195,8 +199,7 @@
 	else if (plugin->info->prefs_info &&
 			plugin->info->prefs_info->get_plugin_pref_frame)
 	{
-		purple_notify_info(plugin, _("..."),
-			_("Still need to do something about this."), NULL);
+		process_pref_frame(plugin->info->prefs_info->get_plugin_pref_frame(plugin));
 		return;
 	}
 	else
@@ -277,3 +280,59 @@
 	decide_conf_button(gnt_tree_get_selection_data(GNT_TREE(tree)));
 }
 
+static void
+process_pref_frame(PurplePluginPrefFrame *frame)
+{
+	PurpleRequestField *field;
+	PurpleRequestFields *fields;
+	PurpleRequestFieldGroup *group = NULL;
+	GList *prefs;
+	
+	fields = purple_request_fields_new();
+
+	for (prefs = purple_plugin_pref_frame_get_prefs(frame); prefs; prefs = prefs->next) {
+		PurplePluginPref *pref = prefs->data;
+		const char *name = purple_plugin_pref_get_name(pref);
+		const char *label = purple_plugin_pref_get_label(pref);
+		if(name == NULL) {
+			if(label == NULL)
+				continue;
+
+			if(purple_plugin_pref_get_type(pref) == PURPLE_PLUGIN_PREF_INFO) {
+				field = purple_request_field_label_new("*", purple_plugin_pref_get_label(pref));
+				purple_request_field_group_add_field(group, field);
+			} else {
+				group = purple_request_field_group_new(label);
+				purple_request_fields_add_group(fields, group);
+			}
+			continue;
+		}
+
+		field = NULL;
+		switch(purple_prefs_get_type(name)) {
+			case PURPLE_PREF_BOOLEAN:
+				field = purple_request_field_bool_new(name, label, purple_prefs_get_bool(name));
+				break;
+			case PURPLE_PREF_INT:
+				field = purple_request_field_int_new(name, label, purple_prefs_get_int(name));
+				break;
+			case PURPLE_PREF_STRING:
+				field = purple_request_field_string_new(name, label, purple_prefs_get_string(name),
+						purple_plugin_pref_get_format_type(pref) & PURPLE_STRING_FORMAT_TYPE_MULTILINE);
+				break;
+			default:
+				break;
+		}
+		if (field) {
+			if (group == NULL) {
+				group = purple_request_field_group_new(_("Preferences"));
+				purple_request_fields_add_group(fields, group);
+			}
+			purple_request_field_group_add_field(group, field);
+		}
+	}
+
+	purple_request_fields(NULL, _("Preferences"), NULL, NULL, fields,
+			_("Save"), G_CALLBACK(finch_request_save_in_prefs), _("Cancel"), NULL, NULL);
+}
+
--- a/finch/gntplugin.h	Sun Apr 22 18:20:05 2007 +0000
+++ b/finch/gntplugin.h	Sun Apr 22 23:55:24 2007 +0000
@@ -28,6 +28,7 @@
 #include <gnt.h>
 
 #include <plugin.h>
+#include <pluginpref.h>
 
 #include <string.h>
 
--- a/finch/gntprefs.c	Sun Apr 22 18:20:05 2007 +0000
+++ b/finch/gntprefs.c	Sun Apr 22 23:55:24 2007 +0000
@@ -198,57 +198,7 @@
 static void
 save_cb(void *data, PurpleRequestFields *allfields)
 {
-	GList *list;
-	for (list = purple_request_fields_get_groups(allfields); list; list = list->next)
-	{
-		PurpleRequestFieldGroup *group = list->data;
-		GList *fields = purple_request_field_group_get_fields(group);
-		
-		for (; fields ; fields = fields->next)
-		{
-			PurpleRequestField *field = fields->data;
-			PurpleRequestFieldType type = purple_request_field_get_type(field);
-			PurplePrefType pt;
-			gpointer val = NULL;
-			const char *id = purple_request_field_get_id(field);
-
-			switch (type)
-			{
-				case PURPLE_REQUEST_FIELD_LIST:
-					val = purple_request_field_list_get_selected(field)->data;
-					break;
-				case PURPLE_REQUEST_FIELD_BOOLEAN:
-					val = GINT_TO_POINTER(purple_request_field_bool_get_value(field));
-					break;
-				case PURPLE_REQUEST_FIELD_INTEGER:
-					val = GINT_TO_POINTER(purple_request_field_int_get_value(field));
-					break;
-				case PURPLE_REQUEST_FIELD_STRING:
-					val = (gpointer)purple_request_field_string_get_value(field);
-					break;
-				default:
-					break;
-			}
-
-			pt = purple_prefs_get_type(id);
-			switch (pt)
-			{
-				case PURPLE_PREF_INT:
-					if (type == PURPLE_REQUEST_FIELD_LIST) /* Lists always return string */
-						sscanf(val, "%ld", (long int *)&val);
-					purple_prefs_set_int(id, GPOINTER_TO_INT(val));
-					break;
-				case PURPLE_PREF_BOOLEAN:
-					purple_prefs_set_bool(id, GPOINTER_TO_INT(val));
-					break;
-				case PURPLE_PREF_STRING:
-					purple_prefs_set_string(id, val);
-					break;
-				default:
-					break;
-			}
-		}
-	}
+	finch_request_save_in_prefs(data, allfields);
 	free_strings();
 }
 
--- a/finch/gntrequest.c	Sun Apr 22 18:20:05 2007 +0000
+++ b/finch/gntrequest.c	Sun Apr 22 23:55:24 2007 +0000
@@ -139,6 +139,11 @@
 finch_close_request(PurpleRequestType type, gpointer ui_handle)
 {
 	GntWidget *widget = GNT_WIDGET(ui_handle);
+	if (type == PURPLE_REQUEST_FIELDS) {
+		PurpleRequestFields *fields = g_object_get_data(G_OBJECT(widget), "fields");
+		purple_request_fields_destroy(fields);
+	}
+
 	while (widget->parent)
 		widget = widget->parent;
 	gnt_widget_destroy(widget);
@@ -243,7 +248,7 @@
 	gpointer data = g_object_get_data(G_OBJECT(button), "activate-userdata");
 	GList *list;
 
-	/* Update the data of the fields. GtkPurple does this differently. Instead of
+	/* Update the data of the fields. Pidgin does this differently. Instead of
 	 * updating the fields at the end like here, it updates the appropriate field
 	 * instantly whenever a change is made. That allows it to make sure the
 	 * 'required' fields are entered before the user can hit OK. It's not the case
@@ -532,6 +537,8 @@
 	gnt_box_add_widget(GNT_BOX(window), box);
 
 	gnt_widget_show(window);
+
+	g_object_set_data(G_OBJECT(window), "fields", allfields);
 	
 	return window;
 }
@@ -620,3 +627,54 @@
 {
 }
 
+void finch_request_save_in_prefs(gpointer null, PurpleRequestFields *allfields)
+{
+	GList *list;
+	for (list = purple_request_fields_get_groups(allfields); list; list = list->next) {
+		PurpleRequestFieldGroup *group = list->data;
+		GList *fields = purple_request_field_group_get_fields(group);
+		
+		for (; fields ; fields = fields->next) {
+			PurpleRequestField *field = fields->data;
+			PurpleRequestFieldType type = purple_request_field_get_type(field);
+			PurplePrefType pt;
+			gpointer val = NULL;
+			const char *id = purple_request_field_get_id(field);
+
+			switch (type) {
+				case PURPLE_REQUEST_FIELD_LIST:
+					val = purple_request_field_list_get_selected(field)->data;
+					break;
+				case PURPLE_REQUEST_FIELD_BOOLEAN:
+					val = GINT_TO_POINTER(purple_request_field_bool_get_value(field));
+					break;
+				case PURPLE_REQUEST_FIELD_INTEGER:
+					val = GINT_TO_POINTER(purple_request_field_int_get_value(field));
+					break;
+				case PURPLE_REQUEST_FIELD_STRING:
+					val = (gpointer)purple_request_field_string_get_value(field);
+					break;
+				default:
+					break;
+			}
+
+			pt = purple_prefs_get_type(id);
+			switch (pt) {
+				case PURPLE_PREF_INT:
+					if (type == PURPLE_REQUEST_FIELD_LIST) /* Lists always return string */
+						sscanf(val, "%ld", (long int *)&val);
+					purple_prefs_set_int(id, GPOINTER_TO_INT(val));
+					break;
+				case PURPLE_PREF_BOOLEAN:
+					purple_prefs_set_bool(id, GPOINTER_TO_INT(val));
+					break;
+				case PURPLE_PREF_STRING:
+					purple_prefs_set_string(id, val);
+					break;
+				default:
+					break;
+			}
+		}
+	}
+}
+
--- a/finch/gntrequest.h	Sun Apr 22 18:20:05 2007 +0000
+++ b/finch/gntrequest.h	Sun Apr 22 23:55:24 2007 +0000
@@ -49,6 +49,12 @@
  */
 void finch_request_uninit(void);
 
+/**
+ * Save the request fields in preferences where the id attribute of each field is the
+ * id of a preference.
+ */
+void finch_request_save_in_prefs(gpointer null, PurpleRequestFields *fields);
+
 /*@}*/
 
 #endif