From ed960fceef8cb70173d6b283bf1760c9820ad582 Mon Sep 17 00:00:00 2001
From: aviau <alexandre@alexandreviau.net>
Date: Mon, 5 Dec 2016 16:14:18 -0500
Subject: [PATCH] presence: load tracked buddies

Load track buddies when creating an account. We do this so that the
state of contactmethods is as up to date as possible.

Otherwise, restarting the client would show the contacts as offline
because we wait for callbacks of the daemon to set the presence status.

This loads the status of all currently tracked buddies.

Tuleap: #1379
Change-Id: I047900bcbec9196f66d0d425e573f64090e3eba7
---
 src/account.cpp     | 10 ++++++++++
 src/contactmethod.h |  4 +++-
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/account.cpp b/src/account.cpp
index 309feddb..3cbaa0b7 100644
--- a/src/account.cpp
+++ b/src/account.cpp
@@ -35,6 +35,7 @@
 #include "dbus/configurationmanager.h"
 #include "dbus/callmanager.h"
 #include "dbus/videomanager.h"
+#include "dbus/presencemanager.h"
 #include "globalinstances.h"
 #include "interfaces/accountlistcolorizeri.h"
 #include "certificate.h"
@@ -162,6 +163,15 @@ Account* Account::buildExistingAccountFromId(const QByteArray& _accountId)
       }
    }
 
+   //Load the tracked buddies
+   const VectorMapStringString subscriptions = PresenceManager::instance().getSubscriptions(a->id());
+   foreach(auto subscription, subscriptions){
+       ContactMethod* tracked_buddy = PhoneDirectoryModel::instance().getNumber(subscription["Buddy"], a);
+       bool tracked_buddy_present = subscription["Status"].compare("Online") == 0;
+       tracked_buddy->setTracked(true);
+       tracked_buddy->setPresent(tracked_buddy_present);
+   }
+
    return a;
 } //buildExistingAccountFromId
 
diff --git a/src/contactmethod.h b/src/contactmethod.h
index 7b3f73e1..f9e725e6 100644
--- a/src/contactmethod.h
+++ b/src/contactmethod.h
@@ -31,6 +31,7 @@
 #include "itemdataroles.h"
 #include "uri.h"
 class Account;
+class AccountPrivate;
 class Person;
 class Call;
 class ContactMethodPrivate;
@@ -54,6 +55,7 @@ public:
    friend class PhoneDirectoryModelPrivate;
    friend class LocalTextRecordingCollection;
    friend class CallPrivate;
+   friend class AccountPrivate;
 
    enum class Role {
       Uri          = static_cast<int>(Ring::Role::UserRole) + 1000,
@@ -149,6 +151,7 @@ public:
    void             setUid           (const QString&      uri           );
    bool             setType          (ContactMethod::Type t             );
    void             setLastUsed      (time_t              t             );
+   void             setPresent       (bool                present       );
 
    //Mutator
    Q_INVOKABLE void addCall(Call* call);
@@ -172,7 +175,6 @@ protected:
    virtual ~ContactMethod();
 
    //Private setters
-   void setPresent(bool present);
    void setPresenceMessage(const QString& message);
 
    //PhoneDirectoryModel mutator
-- 
GitLab