Commit 76fceef7 authored by Ming Rui Zhang's avatar Ming Rui Zhang Committed by Sébastien Blin

peerdiscovery: add peerdiscoverymodel

-used to reveive peer discovery map change signal
-get peer discovery map directly by knowing accountId

Change-Id: I35faf9360200d86a68b64d17969797bfcaa3695e
parent d7aa76b0
......@@ -343,6 +343,7 @@ SET( libringclient_LIB_SRCS
src/authority/databasehelper.cpp
src/lrc.cpp
src/newaccountmodel.cpp
src/peerdiscoverymodel.cpp
src/callbackshandler.cpp
src/behaviorcontroller.cpp
src/datatransfermodel.cpp
......@@ -483,6 +484,7 @@ SET(libringclient_api_LIB_HDRS
src/api/lrc.h
src/api/avmodel.h
src/api/newaccountmodel.h
src/api/peerdiscoverymodel.h
src/api/newcallmodel.h
src/api/newcodecmodel.h
src/api/newdevicemodel.h
......
......@@ -40,6 +40,7 @@ class NewCallModel;
class NewAccountModel;
class NewDeviceModel;
class NewCodecModel;
class PeerDiscoveryModel;
namespace account
{
......@@ -225,6 +226,7 @@ struct Info
std::unique_ptr<lrc::api::NewCallModel> callModel;
std::unique_ptr<lrc::api::NewDeviceModel> deviceModel;
std::unique_ptr<lrc::api::NewCodecModel> codecModel;
std::unique_ptr<lrc::api::PeerDiscoveryModel> peerDiscoveryModel;
NewAccountModel* accountModel {nullptr};
// daemon config
......
/****************************************************************************
* Copyright (C) 2019 Savoir-faire Linux Inc. *
* Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#pragma once
// std
#include <vector>
#include <map>
#include <memory>
#include <string>
// Qt
#include <qobject.h>
// Lrc
#include "typedefs.h"
namespace lrc
{
class CallbacksHandler;
class PeerDiscoveryModelPimpl;
namespace api
{
struct PeerContact
{
std::string uri;
std::string displayName;
};
enum class PeerModelChanged
{
INSERT,
REMOVE
};
/**
* @brief Class that manages local peer discovery info
*/
class LIB_EXPORT PeerDiscoveryModel : public QObject {
Q_OBJECT
public:
PeerDiscoveryModel(const CallbacksHandler& callbackHandler, const std::string& accountID);
~PeerDiscoveryModel();
/**
* get a map of discovered peers account
* @return a std::vector<PeerContact>
*/
std::vector<PeerContact> getNearbyPeers() const;
Q_SIGNALS:
/**
* Connect this signal to know when the status of local peer discovery map changed.
*/
void modelChanged(const std::string& contactUri, PeerModelChanged state, const std::string& displayname);
private:
std::unique_ptr<PeerDiscoveryModelPimpl> pimpl_;
};
} // namespace api
} // namespace lrc
......@@ -64,6 +64,12 @@ CallbacksHandler::CallbacksHandler(const Lrc& parent)
&CallbacksHandler::slotNewBuddySubscription,
Qt::QueuedConnection);
connect(&PresenceManager::instance(),
&PresenceManagerInterface::nearbyPeerNotification,
this,
&CallbacksHandler::slotNearbyPeerSubscription,
Qt::QueuedConnection);
connect(&ConfigurationManager::instance(),
&ConfigurationManagerInterface::contactAdded,
this,
......@@ -248,6 +254,15 @@ CallbacksHandler::slotNewBuddySubscription(const QString& accountId,
emit newBuddySubscription(uri.toStdString(), status);
}
void
CallbacksHandler::slotNearbyPeerSubscription(const QString& accountId,
const QString& contactUri,
int state,
const QString& displayname)
{
emit newPeerSubscription(accountId.toStdString(), contactUri.toStdString(), state, displayname.toStdString());
}
void
CallbacksHandler::slotContactAdded(const QString& accountId,
const QString& contactUri,
......
......@@ -68,6 +68,12 @@ Q_SIGNALS:
* @param present if the peer is online.
*/
void newBuddySubscription(const std::string& contactUri, bool present);
/**
* Connect this signal to get information when peer discovery changes.
* @param contactUri the peer.
* @param state is 0 if the peer is added.
*/
void newPeerSubscription(const std::string& accountId, const std::string& contactUri, int state, const std::string& displayname);
/**
* Connect this signal to know when a contact is removed by the daemon.
* @param accountId the one who lost a contact.
......@@ -467,6 +473,18 @@ private Q_SLOTS:
*/
void slotAudioMeterReceived(const QString& id, float level);
/**
* Emit newPeerSubscription
* @param accountId
* @param contactUri
* @param status if the peer is added or removed
* @param displayname is the account display name
*/
void slotNearbyPeerSubscription(const QString& accountId,
const QString& contactUri,
int state,
const QString& displayname);
private:
const api::Lrc& parent;
};
......
......@@ -31,6 +31,7 @@
#include "api/lrc.h"
#include "api/contactmodel.h"
#include "api/conversationmodel.h"
#include "api/peerdiscoverymodel.h"
#include "api/newcallmodel.h"
#include "api/newcodecmodel.h"
#include "api/newdevicemodel.h"
......@@ -582,6 +583,7 @@ NewAccountModelPimpl::addToAccounts(const std::string& accountId)
newAcc.callModel = std::make_unique<NewCallModel>(newAcc, callbacksHandler);
newAcc.contactModel = std::make_unique<ContactModel>(newAcc, database, callbacksHandler, behaviorController);
newAcc.conversationModel = std::make_unique<ConversationModel>(newAcc, lrc, database, callbacksHandler, behaviorController);
newAcc.peerDiscoveryModel = std::make_unique<PeerDiscoveryModel>(callbacksHandler, accountId);
newAcc.deviceModel = std::make_unique<NewDeviceModel>(newAcc, callbacksHandler);
newAcc.codecModel = std::make_unique<NewCodecModel>(newAcc, callbacksHandler);
newAcc.accountModel = &linked;
......
/****************************************************************************
* Copyright (C) 2019 Savoir-faire Linux Inc. *
* Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include "api/peerdiscoverymodel.h"
// new LRC
#include "callbackshandler.h"
// Dbus
#include "dbus/configurationmanager.h"
namespace lrc
{
using namespace api;
class PeerDiscoveryModelPimpl: public QObject
{
Q_OBJECT
public:
PeerDiscoveryModelPimpl(PeerDiscoveryModel& linked,
const CallbacksHandler& callbackHandler,
const std::string& accountID);
~PeerDiscoveryModelPimpl();
PeerDiscoveryModel& linked_;
const CallbacksHandler& callbacksHandler_;
const std::string accountID_;
public Q_SLOTS:
/**
* Emit peerMapStatusChanged.
* @param accountId
* @param status
*/
void slotPeerMapStatusChanged(const std::string& accountID, const std::string& contactUri, int state, const std::string& displayname);
};
PeerDiscoveryModel::PeerDiscoveryModel(const CallbacksHandler& callbacksHandler, const std::string& accountID)
: QObject()
, pimpl_(std::make_unique<PeerDiscoveryModelPimpl>(*this, callbacksHandler, accountID))
{
}
PeerDiscoveryModel::~PeerDiscoveryModel()
{
}
PeerDiscoveryModelPimpl::PeerDiscoveryModelPimpl(PeerDiscoveryModel& linked,
const CallbacksHandler& callbacksHandler,
const std::string& accountID)
: linked_(linked)
, callbacksHandler_(callbacksHandler)
, accountID_(accountID)
{
connect(&callbacksHandler_, &CallbacksHandler::newPeerSubscription, this, &PeerDiscoveryModelPimpl::slotPeerMapStatusChanged);
}
PeerDiscoveryModelPimpl::~PeerDiscoveryModelPimpl()
{
disconnect(&callbacksHandler_, &CallbacksHandler::newPeerSubscription, this, &PeerDiscoveryModelPimpl::slotPeerMapStatusChanged);
}
void
PeerDiscoveryModelPimpl::slotPeerMapStatusChanged(const std::string& accountID, const std::string& contactUri, int state, const std::string& displayname)
{
if(accountID != accountID_){
return;
}
emit linked_.modelChanged(contactUri,state == 0 ? PeerModelChanged::INSERT : PeerModelChanged::REMOVE,displayname);
}
std::vector<PeerContact>
PeerDiscoveryModel::getNearbyPeers() const
{
std::vector<PeerContact> result;
const MapStringString nearbyPeers = ConfigurationManager::instance().getNearbyPeers(QString::fromStdString(pimpl_->accountID_));
result.reserve(nearbyPeers.size());
QMap<QString, QString>::const_iterator i = nearbyPeers.constBegin();
while (i != nearbyPeers.constEnd()) {
result.emplace_back(PeerContact{i.key().toStdString(), i.value().toStdString()});
++i;
}
return result;
}
} // namespace lrc
#include "api/moc_peerdiscoverymodel.cpp"
#include "peerdiscoverymodel.moc"
......@@ -621,6 +621,10 @@ public Q_SLOTS: // METHODS
return DRing::getMessageStatus(id);
}
MapStringString getNearbyPeers(const QString &accountID){
return convertMap(DRing::getNearbyPeers(accountID.toStdString()));
}
void connectivityChanged() {
DRing::connectivityChanged();
}
......
......@@ -63,6 +63,10 @@ public:
exportable_callback<PresenceSignal::SubscriptionStateChanged>(
[this] (const std::string &accountID, const std::string &buddyUri, bool state) {
Q_EMIT this->subscriptionStateChanged(QString(accountID.c_str()), QString(buddyUri.c_str()), state);
}),
exportable_callback<PresenceSignal::NearbyPeerNotification>(
[this] (const std::string &accountID, const std::string &buddyUri, int status, const std::string &displayname) {
Q_EMIT this->nearbyPeerNotification(QString(accountID.c_str()), QString(buddyUri.c_str()), status, QString(displayname.c_str()));
})
};
}
......@@ -100,6 +104,7 @@ public Q_SLOTS: // METHODS
}
Q_SIGNALS: // SIGNALS
void nearbyPeerNotification(const QString &accountID, const QString &buddyUri, int status, const QString &displayname);
void newServerSubscriptionRequest(const QString &buddyUri);
void serverError(const QString &accountID, const QString &error, const QString &msg);
void newBuddyNotification(const QString &accountID, const QString &buddyUri, bool status, const QString &lineStatus);
......
......@@ -52,6 +52,10 @@ public:
{
emit newBuddyNotification(accountID, buddyUri, status, lineStatus);
}
void emitNearbyPeerNotification(const QString &accountID, const QString &buddyUri, int status, const QString &displayname)
{
emit nearbyPeerNotification(accountID, buddyUri, status, displayname);
}
public Q_SLOTS: // METHODS
void answerServerRequest(const QString &uri, bool flag)
......@@ -91,6 +95,7 @@ Q_SIGNALS: // SIGNALS
void newServerSubscriptionRequest(const QString &buddyUri);
void serverError(const QString &accountID, const QString &error, const QString &msg);
void newBuddyNotification(const QString &accountID, const QString &buddyUri, bool status, const QString &lineStatus);
void nearbyPeerNotification(const QString &accountID, const QString &buddyUri, int status, const QString &displayname);
void subscriptionStateChanged(const QString &accountID, const QString &buddyUri, bool state);
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment