soc.2009.transport: 05aeefbe: Using PurpleValue for transport settings...

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Mon Jun 15 01:25:28 EDT 2009


-----------------------------------------------------------------
Revision: 05aeefbe4c4402480cc704133599a11c23ad028a
Ancestor: 51e1e912f22b657146a10d05c7ef6473ac6f72cf
Author: hanzz at soc.pidgin.im
Date: 2009-06-15T05:20:19
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/05aeefbe4c4402480cc704133599a11c23ad028a

Modified files:
        adhocsettings.cpp sql.cpp sql.h user.cpp user.h

ChangeLog: 

Using PurpleValue for transport settings.

-------------- next part --------------
============================================================
--- adhocsettings.cpp	1995f5a816b804443e7c2da53c91a55851e0ef10
+++ adhocsettings.cpp	b9fb134394d4b7261f43d37a3081657a8c561f68
@@ -26,7 +26,7 @@ AdhocSettings::AdhocSettings(GlooxMessag
 AdhocSettings::AdhocSettings(GlooxMessageHandler *m, User *user, const std::string &from, const std::string &id) {
 	main = m;
 	m_user = user;
-	std::string setting;
+	PurpleValue *value;
 	
 	IQ _response(IQ::Result, from, id);
 	Tag *response = _response.tag();
@@ -53,11 +53,12 @@ AdhocSettings::AdhocSettings(GlooxMessag
 	field->addAttribute("type","boolean");
 	field->addAttribute("label","Enable transport");
 	field->addAttribute("var","enable_transport");
-	setting = m_user->getSetting("enable_transport");
-	if (setting.empty() || setting == "0")
+	value = m_user->getSetting("enable_transport");
+	if (purple_value_get_boolean(value))
+		field->addChild(new Tag("value","1"));
+	else
 		field->addChild(new Tag("value","0"));
-	else
-		field->addChild(new Tag("value","1"));
+		
 	xdata->addChild(field);
 
 	c->addChild(xdata);
@@ -94,16 +95,22 @@ bool AdhocSettings::handleIq(const IQ &s
 			std::string key = (*it)->findAttribute("var");
 			if (key.empty()) continue;
 			
-			std::string savedValue = m_user->getSetting(key.c_str());
-			if (savedValue.empty()) continue;
+			PurpleValue * savedValue = m_user->getSetting(key.c_str());
+			if (!savedValue) continue;
 			
 			Tag *v =(*it)->findChild("value");
 			if (!v) continue;
 			
-			std::string value = v->cdata();
-			if (savedValue == value) continue;
-			
-			m_user->updateSetting(key, value);
+			PurpleValue *value;
+			if (purple_value_get_type(savedValue) == PURPLE_TYPE_BOOLEAN) {
+				value = purple_value_new(PURPLE_TYPE_BOOLEAN);
+				purple_value_set_boolean(value, atoi(v->cdata().c_str()));
+				if (purple_value_get_boolean(savedValue) == purple_value_get_boolean(value)) {
+					purple_value_destroy(value);
+					continue;
+				}
+				m_user->updateSetting(key, value);
+			}
 		}
 
 		IQ _s(IQ::Result, stanza.from().full(), stanza.id());
============================================================
--- sql.cpp	d617e860739f28d13f23a3e0a47af3863493cac6
+++ sql.cpp	e0af9ef554ab071998c2b79ec4275474e33cd13b
@@ -330,7 +330,7 @@ void SQLClass::getRandomStatus(std::stri
 
 // settings
 
-void SQLClass::addSetting(const std::string &jid, const std::string &key, const std::string &value, SettingType type) {
+void SQLClass::addSetting(const std::string &jid, const std::string &key, const std::string &value, PurpleType type) {
 	mysqlpp::Query query = sql->query();
 	query << "INSERT INTO "<< p->configuration().sqlPrefix <<"settings " << "(jid, var, type, value) VALUES (\"" << jid << "\",\"" << key << "\", \"" << type << "\", \"" << value << "\")";
 	query.execute();
@@ -347,7 +347,9 @@ GHashTable * SQLClass::getSettings(const
 }
 
 GHashTable * SQLClass::getSettings(const std::string &jid) {
-	GHashTable *settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
+	GHashTable *settings = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify) purple_value_destroy);
+	PurpleType type;
+	PurpleValue *value;
     mysqlpp::Query query = sql->query();
 #if MYSQLPP_HEADER_VERSION < 0x030000
 	mysqlpp::Result res;
@@ -359,21 +361,27 @@ GHashTable * SQLClass::getSettings(const
     query << "SELECT * FROM "<< p->configuration().sqlPrefix <<"settings WHERE jid=\"" << jid << "\";";
 
     res = query.store();
-#if MYSQLPP_HEADER_VERSION < 0x030000
 	if (res) {
+#if MYSQLPP_HEADER_VERSION < 0x030000
 		mysqlpp::Row row;
-		while(row = res.fetch_row()){
-			g_hash_table_replace(settings, g_strdup(row["var"]), g_strdup(row["value"]));
-		}
-	}
+		while(row = res.fetch_row()) {
 #else
-	mysqlpp::StoreQueryResult::size_type i;
-	mysqlpp::Row row;
-	for (i = 0; i < res.num_rows(); ++i) {
-		row = res[i];
-		g_hash_table_replace(settings, g_strdup(row["var"]), g_strdup(row["value"]));
+		mysqlpp::StoreQueryResult::size_type i;
+		mysqlpp::Row row;
+		for (i = 0; i < res.num_rows(); ++i) {
+			row = res[i];
+#endif
+			type = (PurpleType) atoi(row["type"]);
+			if (type == PURPLE_TYPE_BOOLEAN) {
+				value = purple_value_new(PURPLE_TYPE_BOOLEAN);
+				purple_value_set_boolean(value, atoi(row["value"]));
+			}
+			g_hash_table_replace(settings, g_strdup(row["var"]), value);
+		}
 	}
-#endif
+
+
+
 	return settings;
 }
 			
============================================================
--- sql.h	7cdc7138b7e6fd5ad70f6f2ddd026e6d4dff16b0
+++ sql.h	759e69268eb6a044c1fc2309c639e64bc060d350
@@ -28,9 +28,6 @@ using namespace gloox;
 #include "main.h"
 using namespace gloox;
 
-typedef enum { 	SETTING_BOOLEAN = 2,
-				} SettingType;
-
 struct UserRow {
 	long id;
 	std::string jid;
@@ -72,7 +69,7 @@ public:
     void getRandomStatus(std::string & status);
 	
 	// settings
-	void addSetting(const std::string &jid, const std::string &key, const std::string &value, SettingType type);
+	void addSetting(const std::string &jid, const std::string &key, const std::string &value, PurpleType type);
 	void updateSetting(const std::string &jid, const std::string &key, const std::string &value);
 	void getSetting(const std::string &jid, const std::string &key);
 	GHashTable * getSettings(const std::string &jid);
============================================================
--- user.cpp	ccf973635cf814780dd166295e89a459934295d7
+++ user.cpp	42d98746f8b1d19fb31a3d9283f6de8cc1bdeec8
@@ -56,13 +56,14 @@ User::User(GlooxMessageHandler *parent, 
 	m_lang = NULL;
 	this->features = 6; // TODO: I can't be hardcoded
 	m_mucs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
-	std::string setting;
+	PurpleValue *value;
 	
 	// check default settings
-	setting = getSetting("enable_transport");
-	if (setting.empty()) {
-		p->sql()->addSetting(m_jid, "enable_transport", "1", SETTING_BOOLEAN);
-		g_hash_table_replace(m_settings, g_strdup("enable_transport"), g_strdup("1"));
+	if ( (value = getSetting("enable_transport")) == NULL ) {
+		p->sql()->addSetting(m_jid, "enable_transport", "1", PURPLE_TYPE_BOOLEAN);
+		value = purple_value_new(PURPLE_TYPE_BOOLEAN);
+		purple_value_set_boolean(value, true);
+		g_hash_table_replace(m_settings, g_strdup("enable_transport"), value);
 	}
 	
 }
@@ -582,16 +583,19 @@ void User::purpleChatRemoveUsers(PurpleC
 	}
 }
 
-std::string User::getSetting(const char *key) {
-	char *value = (char *) g_hash_table_lookup(m_settings, key);
-	if (!value)
-		return std::string("");
-	return std::string(value);
+PurpleValue * User::getSetting(const char *key) {
+	PurpleValue *value = (PurpleValue *) g_hash_table_lookup(m_settings, key);
+	return value;
 }
 
-void User::updateSetting(const std::string &key, const std::string &value) {
-	p->sql()->updateSetting(m_jid, key, value);
-	g_hash_table_replace(m_settings, g_strdup(key.c_str()), g_strdup(value.c_str()));
+void User::updateSetting(const std::string &key, PurpleValue *value) {
+	if (purple_value_get_type(value) == PURPLE_TYPE_BOOLEAN) {
+		if (purple_value_get_boolean(value))
+			p->sql()->updateSetting(m_jid, key, "1");
+		else
+			p->sql()->updateSetting(m_jid, key, "0");
+	}
+	g_hash_table_replace(m_settings, g_strdup(key.c_str()), value);
 }
 
 /*
============================================================
--- user.h	ce64b18c320edb82385a258add824caf2e45597c
+++ user.h	e9afda5b48ca12b766cb6430a8d3de27f086b1b6
@@ -110,8 +110,8 @@ class User {
 		bool isConnected() { return m_connected; }
 
 		// Settings
-		std::string getSetting(const char *key);
-		void updateSetting(const std::string &key, const std::string &value);
+		PurpleValue *getSetting(const char *key);
+		void updateSetting(const std::string &key, PurpleValue *value);
 
 		// Entity Capabilities
 		std::string capsVersion() { return m_capsVersion; }


More information about the Commits mailing list