soc.2009.transport: 439c2999: Better PURPLE_REQUEST_FIELDS support

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Sat Jun 27 01:20:29 EDT 2009


-----------------------------------------------------------------
Revision: 439c2999d42e9695d8108ece3deb877c7b002e26
Ancestor: e3600bb3c2c0e5e5247c50396e677a2a17494f1f
Author: hanzz at soc.pidgin.im
Date: 2009-06-27T05:15:27
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/439c2999d42e9695d8108ece3deb877c7b002e26

Modified files:
        adhocrepeater.cpp adhocrepeater.h dataforms.cpp

ChangeLog: 

Better PURPLE_REQUEST_FIELDS support

-------------- next part --------------
============================================================
--- adhocrepeater.cpp	cf719fe044496bb6e77064a6b7f86d88cc330cbc
+++ adhocrepeater.cpp	1e09f7b61c354ee0178bce0954e221dce561fe0d
@@ -58,6 +58,9 @@ AdhocRepeater::AdhocRepeater(GlooxMessag
 	c->addChild(actions);
 
 	c->addChild( xdataFromRequestInput(title, primaryString, value, multiline) );
+	
+	m_defaultString = value;
+	
 	response->addChild(c);
 	main->j->send(response);
 
@@ -111,6 +114,9 @@ AdhocRepeater::AdhocRepeater(GlooxMessag
 	setType(PURPLE_REQUEST_FIELDS);
 	main = m;
 	m_user = user;
+	m_ok_cb = ok_cb;
+	m_cancel_cb = cancel_cb;
+	m_fields = fields;
 	m_requestData = user_data;
 	AdhocData data = user->adhocData();
 	m_from = data.from;
@@ -155,6 +161,13 @@ bool AdhocRepeater::handleIq(const IQ &s
 		response->addChild(c);
 		main->j->send(response);
 
+		if (m_type == PURPLE_REQUEST_FIELDS) {
+			((PurpleRequestFieldsCb) m_cancel_cb) (m_requestData, m_fields);
+		}
+		else if (m_type == PURPLE_REQUEST_INPUT) {
+			((PurpleRequestInputCb) m_cancel_cb)(m_requestData, m_defaultString.c_str());
+		}
+
 		g_timeout_add(0,&removeRepeater,this);
 		delete stanzaTag;
 		return false;
@@ -163,7 +176,43 @@ bool AdhocRepeater::handleIq(const IQ &s
 	Tag *x = tag->findChildWithAttrib("xmlns","jabber:x:data");
 	if (x) {
 		if (m_type == PURPLE_REQUEST_FIELDS) {
-			// TODO :) too tired to do it now... it's just typing... nothing to think about
+			for(std::list<Tag*>::const_iterator it = x->children().begin(); it != x->children().end(); ++it){
+				std::string key = (*it)->findAttribute("var");
+				if (key.empty()) continue;
+				
+				PurpleRequestField * fld = purple_request_fields_get_field(m_fields, key.c_str());
+				if (!fld) continue;
+				PurpleRequestFieldType type;
+				type = purple_request_field_get_type(fld);
+				
+				Tag *v =(*it)->findChild("value");
+				if (!v) continue;
+				
+				std::string val = v->cdata();
+				if (val.empty() && type != PURPLE_REQUEST_FIELD_STRING)
+					continue;
+				
+				if (type == PURPLE_REQUEST_FIELD_STRING) {
+					purple_request_field_string_set_value(fld, (val.empty() ? NULL : val.c_str()));
+				}
+				else if (type == PURPLE_REQUEST_FIELD_INTEGER) {
+					purple_request_field_int_set_value(fld, atoi(val.c_str()));
+				}
+				else if (type == PURPLE_REQUEST_FIELD_BOOLEAN) {
+					purple_request_field_bool_set_value(fld, atoi(val.c_str()));
+				}
+				else if (type == PURPLE_REQUEST_FIELD_CHOICE) {
+					purple_request_field_choice_set_value(fld, atoi(val.c_str()));
+				}
+				else if (type == PURPLE_REQUEST_FIELD_LIST) {
+					for(std::list<Tag*>::const_iterator it2 = (*it)->children().begin(); it2 != (*it)->children().end(); ++it2) {
+						val = (*it2)->cdata();
+						if (!val.empty())
+							purple_request_field_list_add_selected(fld, val.c_str());
+					}
+				}
+			}
+			((PurpleRequestFieldsCb) m_ok_cb) (m_requestData, m_fields);
 		}
 		else {
 			std::string result("");
============================================================
--- adhocrepeater.h	7449f26026af0d2704293f723da940167b908b31
+++ adhocrepeater.h	40b068f03ab1792cd7fd1455490a55bba621949f
@@ -55,6 +55,8 @@ class AdhocRepeater : public AdhocComman
 		PurpleRequestType m_type;
 		std::string m_from;
 		std::map<int, GCallback> m_actions;
+		PurpleRequestFields *m_fields;
+		std::string m_defaultString;
 };
 
 #endif
============================================================
--- dataforms.cpp	fb97044ccb82a26669c9d2216a01648f504bfbef
+++ dataforms.cpp	98948695c73052911a683ccb62ab2d3eaa4bb8ab
@@ -122,8 +122,8 @@ Tag * xdataFromRequestFields(const std::
 					field->addAttribute("type","text-single");
 				else
 					field->addAttribute("type","text-multi");
-				field->addAttribute("label",(std::string) field_label);
-				field->addAttribute("var",(std::string) field_label);
+				field->addAttribute("label", field_label ? (std::string) field_label : (std::string) purple_request_field_get_id(fld));
+				field->addAttribute("var",(std::string) purple_request_field_get_id(fld));
 				if (v)
 					field->addChild(new Tag("value", (std::string) v));
 				xdata->addChild(field);
@@ -132,8 +132,8 @@ Tag * xdataFromRequestFields(const std::
 				int v = purple_request_field_int_get_default_value(fld);
 				field = new Tag("field");
 				field->addAttribute("type","text-single");
-				field->addAttribute("label",(std::string) field_label);
-				field->addAttribute("var",(std::string) field_label);
+				field->addAttribute("label", field_label ? (std::string) field_label : (std::string) purple_request_field_get_id(fld));
+				field->addAttribute("var",(std::string) purple_request_field_get_id(fld));
 				if (v!=0) {
 					std::ostringstream os;
 					os << v;
@@ -144,8 +144,8 @@ Tag * xdataFromRequestFields(const std::
 			else if (type == PURPLE_REQUEST_FIELD_BOOLEAN) {
 				field = new Tag("field");
 				field->addAttribute("type","boolean");
-				field->addAttribute("label",(std::string) field_label);
-				field->addAttribute("var",(std::string) field_label);
+				field->addAttribute("label", field_label ? (std::string) field_label : (std::string) purple_request_field_get_id(fld));
+				field->addAttribute("var",(std::string) purple_request_field_get_id(fld));
 				if (purple_request_field_bool_get_default_value(fld))
 					field->addChild(new Tag("value", "1"));
 				else
@@ -158,8 +158,8 @@ Tag * xdataFromRequestFields(const std::
 				GList *l;
 				field = new Tag("field");
 				field->addAttribute("type","list-single");
-				field->addAttribute("label",(std::string) field_label);
-				field->addAttribute("var",(std::string) field_label);
+				field->addAttribute("label", field_label ? (std::string) field_label : (std::string) purple_request_field_get_id(fld));
+				field->addAttribute("var",(std::string) purple_request_field_get_id(fld));
 
 				for (l = labels; l != NULL; l = l->next, i++) {
 					Tag *option;
@@ -184,19 +184,17 @@ Tag * xdataFromRequestFields(const std::
 					field->addAttribute("type","list-multi");
 				else
 					field->addAttribute("type","list-single");
-				field->addAttribute("label",(std::string) field_label);
-				field->addAttribute("var",(std::string) field_label);
+				field->addAttribute("label", field_label ? (std::string) field_label : (std::string) purple_request_field_get_id(fld));
+				field->addAttribute("var",(std::string) purple_request_field_get_id(fld));
 				
 				for (l = purple_request_field_list_get_items(fld); l != NULL; l = l->next, i++) {
 					Tag *option;
-					std::ostringstream os;
-					os << i;
 					std::string name((char *) l->data);
 					if (i == 0)
-						field->addChild(new Tag("value",os.str()));
+						field->addChild(new Tag("value",name));
 					option = new Tag("option");
 					option->addAttribute("label",name);
-					option->addChild( new Tag("value",os.str()) );
+					option->addChild( new Tag("value",name) );
 					field->addChild(option);
 				}
 				field->addAttribute("var","result");


More information about the Commits mailing list