soc.2009.transport: c2ad9389: Rewrite of very old AutoConnectLoop clas...

hanzz at soc.pidgin.im hanzz at soc.pidgin.im
Sat May 2 06:20:30 EDT 2009


-----------------------------------------------------------------
Revision: c2ad93896402feb01bd8fa638a6cdc8b4febd8ca
Ancestor: 42ec2bdfa299ecc9ba700c6d790ddad2cd836cc4
Author: hanzz at soc.pidgin.im
Date: 2009-05-02T10:19:29
Branch: im.pidgin.soc.2009.transport
URL: http://d.pidgin.im/viewmtn/revision/info/c2ad93896402feb01bd8fa638a6cdc8b4febd8ca

Modified files:
        autoconnectloop.cpp autoconnectloop.h

ChangeLog: 

Rewrite of very old AutoConnectLoop class (much better performance)

-------------- next part --------------
============================================================
--- autoconnectloop.cpp	75f031272c82b4532c22f428679fb889d00cb7f4
+++ autoconnectloop.cpp	12e5f22934db95c46fa69620a00f8378cb1dc684
@@ -34,45 +34,53 @@ static gboolean iter(gpointer data){
 	return FALSE;
 }
 
+static void account_removed(const PurpleAccount *account, gpointer user_data) {
+	AutoConnectLoop *loop = (AutoConnectLoop*) user_data;
+	loop->purpleAccountRemoved(account);
+}
+
 AutoConnectLoop::AutoConnectLoop(GlooxMessageHandler *m){
 	main = m;
-	g_timeout_add(10000,&iter,this);
+	static int conn_handle;
+	purple_signal_connect(purple_accounts_get_handle(), "account-removed", &conn_handle, PURPLE_CALLBACK(account_removed), this);
+	m_account = purple_accounts_get_all();
+	if (m_account != NULL)
+		g_timeout_add(10000,&iter,this);
+	else
+		delete this;
 }
 
 AutoConnectLoop::~AutoConnectLoop() {
 	Log().Get("connection restorer") << "Restorer deleted";
 }
 
+void AutoConnectLoop::purpleAccountRemoved(const PurpleAccount *account) {
+	if (m_account)
+		if (account == m_account->data)
+			m_account = m_account->next;
+}
+
 bool AutoConnectLoop::restoreNextConnection() {
 	Stanza * stanza;
 	PurpleAccount *account;
 	GList *l;
 	User *user;
 	Log().Get("connection restorer") << "Restoring new connection";
-	l = purple_accounts_get_all();
-	if (l==NULL)
-		return true;
-	for (l; l != NULL; l = l->next)
-	{
-		account = (PurpleAccount *)l->data;
-		std::map<std::string,int>::iterator it = m_probes.find(JID((std::string)purple_account_get_string(account,"lastUsedJid","")).bare()); //See Find in the Algorithms section.
-		if( it == m_probes.end() )
-		{
-			Log().Get("connection restorer") << "Checking next account";
-			m_probes[JID((std::string)purple_account_get_string(account,"lastUsedJid","")).bare()]=1;
-			if (purple_presence_is_online(account->presence))
-			{
-				user = main->userManager()->getUserByAccount(account);
-				if (user==NULL){
-					Log().Get("connection restorer") << "Sending probe presence to "<< JID((std::string)purple_account_get_string(account,"lastUsedJid","")).bare();
-					stanza = new Stanza("presence");
-					stanza->addAttribute( "to", JID((std::string)purple_account_get_string(account,"lastUsedJid","")).bare());
-					stanza->addAttribute( "type", "probe");
-					stanza->addAttribute( "from", main->jid());
-					main->j->send(stanza);
-					return true;
-				}
-			}
+	if (m_account == NULL)
+		return false;
+
+	Log().Get("connection restorer") << "Checking next account";
+	if (purple_presence_is_online(account->presence)) {
+		user = main->userManager()->getUserByAccount(account);
+		if (user == NULL) {
+			Log().Get("connection restorer") << "Sending probe presence to "<< JID((std::string)purple_account_get_string(account,"lastUsedJid","")).bare();
+			stanza = new Stanza("presence");
+			stanza->addAttribute( "to", JID((std::string)purple_account_get_string(account,"lastUsedJid","")).bare());
+			stanza->addAttribute( "type", "probe");
+			stanza->addAttribute( "from", main->jid());
+			main->j->send(stanza);
+			m_account = m_account->next;
+			return true;
 		}
 	}
 	Log().Get("connection restorer") << "There is no other account to be checked => stopping Restorer";
============================================================
--- autoconnectloop.h	314a85e595c49a7738f5dcbc8934a1cff48b24f3
+++ autoconnectloop.h	a1b0001af920e780f0476956f6521d68ada437ed
@@ -23,6 +23,8 @@
 
 #include <string>
 #include <map>
+#include "glib.h"
+#include "account.h"
 
 class GlooxMessageHandler;
 
@@ -31,11 +33,12 @@ class AutoConnectLoop
 	public:
 		AutoConnectLoop(GlooxMessageHandler *m);
 		~AutoConnectLoop();
+		void purpleAccountRemoved(const PurpleAccount *account);
 		bool restoreNextConnection();
 	
 	private:
 		GlooxMessageHandler *main;
-		std::map<std::string,int> m_probes;
+		GList *m_account;
 	
 };
 


More information about the Commits mailing list