From d54aac14c8f0c8c68f937b1dfb1970f4594947bc Mon Sep 17 00:00:00 2001 From: HanzZ Date: Fri, 25 Jan 2013 17:55:52 +0100 Subject: [PATCH] Cache nodes in libpurple backend --- backends/libpurple/main.cpp | 62 +++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/backends/libpurple/main.cpp b/backends/libpurple/main.cpp index 4695389d..7ceedad6 100644 --- a/backends/libpurple/main.cpp +++ b/backends/libpurple/main.cpp @@ -90,6 +90,14 @@ struct FTData { bool paused; }; +struct NodeCache { + PurpleAccount *account; + std::map nodes; + int timer; +}; + +bool caching = true; + static void *notify_user_info(PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info); static gboolean ft_ui_ready(void *data) { @@ -356,6 +364,12 @@ class SpectrumNetworkPlugin : public NetworkPlugin { void handleLogoutRequest(const std::string &user, const std::string &legacyName) { PurpleAccount *account = m_sessions[user]; if (account) { + if (account->ui_data) { + NodeCache *cache = (NodeCache *) account->ui_data; + purple_timeout_remove_wrapped(cache->timer); + delete cache; + account->ui_data = NULL; + } if (purple_account_get_int_wrapped(account, "version", 0) != 0) { std::string data = stringOf(purple_account_get_int_wrapped(account, "version", 0)); g_file_set_contents ("gfire.cfg", data.c_str(), data.size(), NULL); @@ -805,12 +819,56 @@ static std::vector getGroups(PurpleBuddy *m_buddy) { return groups; } -static void buddyListNewNode(PurpleBlistNode *node) { +void buddyListNewNode(PurpleBlistNode *node); + +static gboolean new_node_cache(void *data) { + NodeCache *cache = (NodeCache *) data; + caching = false; + for (std::map::const_iterator it = cache->nodes.begin(); it != cache->nodes.end(); it++) { + buddyListNewNode(it->first); + } + caching = true; + + cache->account->ui_data = NULL; + delete cache; + + return FALSE; +} + +static void buddyNodeRemoved(PurpleBuddyList *list, PurpleBlistNode *node) { if (!PURPLE_BLIST_NODE_IS_BUDDY_WRAPPED(node)) return; PurpleBuddy *buddy = (PurpleBuddy *) node; PurpleAccount *account = purple_buddy_get_account_wrapped(buddy); + if (!account->ui_data) { + return; + } + + NodeCache *cache = (NodeCache *) account->ui_data; + cache->nodes.erase(node); +} + +void buddyListNewNode(PurpleBlistNode *node) { + if (!PURPLE_BLIST_NODE_IS_BUDDY_WRAPPED(node)) + return; + PurpleBuddy *buddy = (PurpleBuddy *) node; + PurpleAccount *account = purple_buddy_get_account_wrapped(buddy); + + if (caching) { + if (!account->ui_data) { + NodeCache *cache = new NodeCache; + cache->account = account; + cache->timer = purple_timeout_add_wrapped(400, new_node_cache, cache); + account->ui_data = (void *) cache; + } + + NodeCache *cache = (NodeCache *) account->ui_data; + cache->nodes[node] = 1; + return; + } + + std::vector groups = getGroups(buddy); LOG4CXX_INFO(logger, "Buddy updated " << np->m_accounts[account] << " " << purple_buddy_get_name_wrapped(buddy) << " " << getAlias(buddy) << " group (" << groups.size() << ")=" << groups[0]); @@ -886,7 +944,7 @@ static PurpleBlistUiOps blistUiOps = buddyListNewNode, NULL, buddyListUpdate, - NULL, //NodeRemoved, + buddyNodeRemoved, NULL, NULL, NULL, // buddyListAddBuddy,