soc.2009.transport: 0b6d8db0: VCard cache

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Sun Jun 28 15:15:31 EDT 2009


-----------------------------------------------------------------
Revision: 0b6d8db0d8973dcb84dbc0951c4183d0e40c3c8d
Ancestor: 02abe3f525bdb920eaeea5ca001c03471aa7f0c2
Author: hanzz at soc.pidgin.im
Date: 2009-06-28T19:13:00
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/0b6d8db0d8973dcb84dbc0951c4183d0e40c3c8d

Modified files:
        sql.cpp sql.h vcardhandler.cpp

ChangeLog: 

VCard cache

-------------- next part --------------
============================================================
--- sql.cpp	e0af9ef554ab071998c2b79ec4275474e33cd13b
+++ sql.cpp	6a364fbe82c7a8bc37fbf2d72cf59f6afe87a641
@@ -19,6 +19,7 @@
  */
 
 #include "sql.h"
+#include "parser.h"
 
 SQLClass::SQLClass(GlooxMessageHandler *parent){
 	p=parent;
@@ -384,7 +385,44 @@ GHashTable * SQLClass::getSettings(const
 
 	return settings;
 }
-			
+
+bool SQLClass::getVCard(const std::string &name, void (*handleTagCallback)(Tag *tag, Tag *user_data), Tag *user_data) {
+    mysqlpp::Query query = sql->query();
+#if MYSQLPP_HEADER_VERSION < 0x030000
+	mysqlpp::Result res;
+#else
+	mysqlpp::StoreQueryResult res;
+#endif
+    mysqlpp::Row row;
+
+    query << "SELECT vcard FROM "<< p->configuration().sqlPrefix <<"vcards WHERE username=\"" +name+ "\" AND DATE_ADD(timestamp, INTERVAL 1 DAY);";
+
+    res = query.store();
+	if (res) {
+#if MYSQLPP_HEADER_VERSION < 0x030000
+		mysqlpp::Row row;
+		row = res.fetch_row();
+		if (row) {
+#else
+		mysqlpp::StoreQueryResult::size_type i;
+		mysqlpp::Row row;
+		if (res.num_rows()!=0) {
+			row = res[0];
+#endif
+			std::string vcardTag = (std::string) row["vcard"];
+			p->parser()->getTag(vcardTag, handleTagCallback, user_data);
+			return true;
+		}
+	}
+	return false;
+}
+
+void SQLClass::updateVCard(const std::string &name, const std::string &vcard) {
+	mysqlpp::Query query = sql->query();
+	query << "INSERT INTO "<< p->configuration().sqlPrefix <<"vcards (username, vcard) VALUES (\"" << name <<"\",\"" << vcard <<"\") ON DUPLICATE KEY UPDATE vcard=\""+ vcard +"\";";
+	query.execute();
+}
+
 // SQLClass::~SQLClass(){
 //  	delete(sql);
 //  	sql=NULL;
============================================================
--- sql.h	759e69268eb6a044c1fc2309c639e64bc060d350
+++ sql.h	6ad295beaaf825c3a81e69f29a97cf1faef23097
@@ -48,6 +48,10 @@ struct RosterRow {
 	std::string lastPresence;
 };
 
+struct GlooxVCard {
+	Tag *vcard;
+	time_t created;
+};
 
 class SQLClass
 {
@@ -73,7 +77,11 @@ public:
 	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);
-	
+
+	// vcards
+	bool getVCard(const std::string &name, void (*handleTagCallback)(Tag *tag, Tag *user_data), Tag *user_data);
+	void updateVCard(const std::string &name, const std::string &vcard);
+
 	UserRow getUserByJid(const std::string &jid);
 	std::map<std::string,RosterRow> getRosterByJid(const std::string &jid);
 	std::map<std::string,RosterRow> getRosterByJidAsk(const std::string &jid);
============================================================
--- vcardhandler.cpp	5968722b388b7c0fb2a588a84b425dc76d83b53d
+++ vcardhandler.cpp	39b2519e99ab84e84a7e7e810e2217a78670bcc8
@@ -84,6 +84,16 @@ void base64encode(const unsigned char * 
     }
 }   
 
+static void sendVCardTag(Tag *tag, Tag *stanzaTag) {
+	std::string id = stanzaTag->findAttribute("id");
+	std::string from = stanzaTag->findAttribute("from");
+	Tag *vcard = tag->clone();
+	vcard->addAttribute("id",id);
+	vcard->addAttribute("to",from);
+	GlooxMessageHandler::instance()->j->send(vcard);
+	delete stanzaTag;
+}
+
 GlooxVCardHandler::GlooxVCardHandler(GlooxMessageHandler *parent) : IqHandler(){
 	p=parent;
 	p->j->registerStanzaExtension( new VCard() );
@@ -105,12 +115,16 @@ bool GlooxVCardHandler::handleIq (const 
 	}
 
 	if(stanza.subtype() == IQ::Get) {
-		std::list<std::string> temp;
-		temp.push_back((std::string)stanza.id());
-		temp.push_back((std::string)stanza.from().full());
-		vcardRequests[(std::string)stanza.to().username()]=temp;
+		Tag *stanzaTag = stanza.tag();
 		
-		serv_get_info(purple_account_get_connection(user->account()), stanza.to().username().c_str());
+		if (!p->sql()->getVCard(stanza.to().username(), sendVCardTag, stanzaTag)) {
+			std::list<std::string> temp;
+			temp.push_back((std::string)stanza.id());
+			temp.push_back((std::string)stanza.from().full());
+			vcardRequests[(std::string)stanza.to().username()]=temp;
+			delete stanzaTag;
+			serv_get_info(purple_account_get_connection(user->account()), stanza.to().username().c_str());
+		}
 	}
 
 	return true;
@@ -176,7 +190,7 @@ void GlooxVCardHandler::userInfoArrived(
 		}
 
 		reply->addChild(vcard);
-		std::cout << reply->xml() << "\n";
+		p->sql()->updateVCard(who, reply->xml());
 		p->j->send(reply);
 		vcardRequests.erase(who);
 	}


More information about the Commits mailing list