2020-09-03 09:24:49 -04:00
|
|
|
/*!
|
2020-08-03 13:27:42 -04:00
|
|
|
* Copyright (C) 2020 by Savoir-faire Linux
|
|
|
|
* Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>
|
2020-08-06 15:24:57 -04:00
|
|
|
* Author: Anthony L�onard <anthony.leonard@savoirfairelinux.com>
|
2020-08-03 13:27:42 -04:00
|
|
|
* Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
|
|
|
|
* Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
|
|
|
|
* Author: Isa Nanic <isa.nanic@savoirfairelinux.com>
|
|
|
|
* Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 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 "conversationsadapter.h"
|
|
|
|
|
|
|
|
#include "utils.h"
|
|
|
|
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::ConversationsAdapter(QObject* parent)
|
2020-08-03 13:27:42 -04:00
|
|
|
: QmlAdapterBase(parent)
|
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
2020-09-03 09:24:49 -04:00
|
|
|
ConversationsAdapter::safeInit()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
conversationSmartListModel_ = new SmartListModel(LRCInstance::getCurrAccId(), this);
|
|
|
|
|
2020-09-03 09:24:49 -04:00
|
|
|
emit modelChanged(QVariant::fromValue(conversationSmartListModel_));
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
connect(&LRCInstance::behaviorController(),
|
|
|
|
&BehaviorController::showChatView,
|
2020-09-04 17:53:24 -04:00
|
|
|
[this](const QString& accountId, lrc::api::conversation::Info convInfo) {
|
2020-08-03 13:27:42 -04:00
|
|
|
emit showChatView(accountId, convInfo.uid);
|
|
|
|
});
|
2020-08-27 16:46:22 -04:00
|
|
|
|
2020-09-04 17:53:24 -04:00
|
|
|
connect(&LRCInstance::instance(),
|
|
|
|
&LRCInstance::currentAccountChanged,
|
|
|
|
this,
|
|
|
|
&ConversationsAdapter::onCurrentAccountIdChanged);
|
2020-08-27 16:46:22 -04:00
|
|
|
|
|
|
|
connectConversationModel();
|
2020-08-06 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
void
|
|
|
|
ConversationsAdapter::backToWelcomePage()
|
|
|
|
{
|
|
|
|
deselectConversation();
|
2020-09-03 09:24:49 -04:00
|
|
|
emit navigateToWelcomePageRequested();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::selectConversation(const QString& accountId,
|
|
|
|
const QString& convUid,
|
2020-08-03 13:27:42 -04:00
|
|
|
bool preventSendingSignal)
|
|
|
|
{
|
2020-09-04 17:53:24 -04:00
|
|
|
auto& accInfo = LRCInstance::getAccountInfo(accountId);
|
2020-08-18 17:21:28 +02:00
|
|
|
const auto convInfo = accInfo.conversationModel->getConversationForUID(convUid);
|
|
|
|
|
|
|
|
selectConversation(convInfo, preventSendingSignal);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::selectConversation(const QString& convUid)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-08-18 17:21:28 +02:00
|
|
|
auto* convModel = LRCInstance::getCurrentConversationModel();
|
2020-08-03 13:27:42 -04:00
|
|
|
if (convModel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-04 17:53:24 -04:00
|
|
|
const auto& conversation = convModel->getConversationForUID(convUid);
|
2020-08-18 17:21:28 +02:00
|
|
|
|
|
|
|
if (selectConversation(conversation, false)) {
|
|
|
|
// If it is calling, show callview (can use showChatView signal, since it will be determined on qml).
|
2020-08-03 13:27:42 -04:00
|
|
|
if (!conversation.uid.isEmpty()
|
|
|
|
&& LRCInstance::getCurrentCallModel()->hasCall(conversation.callId)) {
|
|
|
|
emit showChatView(LRCInstance::getCurrAccId(), conversation.uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::selectConversation(const lrc::api::conversation::Info& item,
|
2020-08-03 13:27:42 -04:00
|
|
|
bool preventSendingSignal)
|
|
|
|
{
|
2020-08-18 17:21:28 +02:00
|
|
|
// accInfo.conversationModel->selectConversation(item.uid) only emit ui
|
|
|
|
// behavior control signals, but sometimes we do not want that,
|
|
|
|
// preventSendingSignal boolean can help us to determine.
|
2020-08-03 13:27:42 -04:00
|
|
|
if (LRCInstance::getCurrentConvUid() == item.uid) {
|
|
|
|
return false;
|
|
|
|
} else if (item.participants.size() > 0) {
|
2020-09-04 17:53:24 -04:00
|
|
|
auto& accInfo = LRCInstance::getAccountInfo(item.accountId);
|
2020-08-03 13:27:42 -04:00
|
|
|
LRCInstance::setSelectedConvId(item.uid);
|
|
|
|
if (!preventSendingSignal)
|
|
|
|
accInfo.conversationModel->selectConversation(item.uid);
|
|
|
|
accInfo.conversationModel->clearUnreadInteractions(item.uid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationsAdapter::deselectConversation()
|
|
|
|
{
|
|
|
|
if (LRCInstance::getCurrentConvUid().isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto currentConversationModel = LRCInstance::getCurrentConversationModel();
|
|
|
|
|
|
|
|
if (currentConversationModel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LRCInstance::setSelectedConvId();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::onCurrentAccountIdChanged()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-09-04 17:53:24 -04:00
|
|
|
auto accountId = LRCInstance::getCurrAccId();
|
2020-08-18 17:21:28 +02:00
|
|
|
// Should be called when current account is changed.
|
2020-08-27 16:46:22 -04:00
|
|
|
conversationSmartListModel_->setAccount(accountId);
|
|
|
|
|
|
|
|
auto& accountInfo = LRCInstance::accountModel().getAccountInfo(accountId);
|
2020-08-03 13:27:42 -04:00
|
|
|
currentTypeFilter_ = accountInfo.profileInfo.type;
|
|
|
|
LRCInstance::getCurrentConversationModel()->setFilter(accountInfo.profileInfo.type);
|
2020-08-27 16:46:22 -04:00
|
|
|
updateConversationsFilterWidget();
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2020-09-01 14:41:53 -04:00
|
|
|
disconnectConversationModel();
|
2020-08-03 13:27:42 -04:00
|
|
|
connectConversationModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationsAdapter::updateConversationsFilterWidget()
|
|
|
|
{
|
2020-08-18 17:21:28 +02:00
|
|
|
// Update status of "Conversations" and "Invitations".
|
2020-08-03 13:27:42 -04:00
|
|
|
auto invites = LRCInstance::getCurrentAccountInfo().contactModel->pendingRequestCount();
|
|
|
|
if (invites == 0 && currentTypeFilter_ == lrc::api::profile::Type::PENDING) {
|
|
|
|
currentTypeFilter_ = lrc::api::profile::Type::RING;
|
|
|
|
LRCInstance::getCurrentConversationModel()->setFilter(currentTypeFilter_);
|
|
|
|
}
|
|
|
|
showConversationTabs(invites);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::setConversationFilter(const QString& type)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-08-18 17:21:28 +02:00
|
|
|
// Set conversation filter according to type,
|
|
|
|
// type needs to be recognizable by lrc::api::profile::to_type.
|
2020-08-03 13:27:42 -04:00
|
|
|
if (type.isEmpty()) {
|
|
|
|
if (LRCInstance::getCurrentAccountInfo().profileInfo.type == lrc::api::profile::Type::RING)
|
|
|
|
setConversationFilter(lrc::api::profile::Type::RING);
|
|
|
|
else
|
|
|
|
setConversationFilter(lrc::api::profile::Type::SIP);
|
|
|
|
} else {
|
|
|
|
setConversationFilter(lrc::api::profile::to_type(type));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ConversationsAdapter::setConversationFilter(lrc::api::profile::Type filter)
|
|
|
|
{
|
|
|
|
if (currentTypeFilter_ == filter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentTypeFilter_ = filter;
|
|
|
|
LRCInstance::getCurrentConversationModel()->setFilter(currentTypeFilter_);
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:41:53 -04:00
|
|
|
void
|
|
|
|
ConversationsAdapter::refill()
|
|
|
|
{
|
|
|
|
if (conversationSmartListModel_)
|
|
|
|
conversationSmartListModel_->fillConversationsList();
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
bool
|
2020-09-01 14:41:53 -04:00
|
|
|
ConversationsAdapter::connectConversationModel(bool updateFilter)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-08-18 17:21:28 +02:00
|
|
|
// Signal connections
|
|
|
|
auto currentConversationModel = LRCInstance::getCurrentConversationModel();
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2020-09-04 17:53:24 -04:00
|
|
|
modelSortedConnection_ = QObject::connect(
|
|
|
|
currentConversationModel, &lrc::api::ConversationModel::modelSorted, [this]() {
|
|
|
|
conversationSmartListModel_->fillConversationsList();
|
|
|
|
updateConversationsFilterWidget();
|
|
|
|
emit updateListViewRequested();
|
|
|
|
auto* convModel = LRCInstance::getCurrentConversationModel();
|
|
|
|
const auto conversation = convModel->getConversationForUID(
|
|
|
|
LRCInstance::getCurrentConvUid());
|
|
|
|
|
|
|
|
if (conversation.uid.isEmpty() || conversation.participants.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto contactURI = conversation.participants[0];
|
|
|
|
if (contactURI.isEmpty()
|
|
|
|
|| convModel->owner.contactModel->getContact(contactURI).profileInfo.type
|
|
|
|
== lrc::api::profile::Type::TEMPORARY) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emit modelSorted(QVariant::fromValue(contactURI));
|
|
|
|
});
|
2020-08-18 17:21:28 +02:00
|
|
|
|
|
|
|
modelUpdatedConnection_ = QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::conversationUpdated,
|
2020-09-04 17:53:24 -04:00
|
|
|
[this](const QString& convUid) {
|
|
|
|
conversationSmartListModel_->updateConversation(
|
|
|
|
convUid);
|
|
|
|
updateConversationsFilterWidget();
|
|
|
|
emit updateListViewRequested();
|
|
|
|
});
|
|
|
|
|
|
|
|
filterChangedConnection_
|
|
|
|
= QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::filterChanged,
|
|
|
|
[this]() {
|
|
|
|
conversationSmartListModel_->fillConversationsList();
|
|
|
|
conversationSmartListModel_->setAccount(LRCInstance::getCurrAccId());
|
|
|
|
updateConversationsFilterWidget();
|
|
|
|
emit updateListViewRequested();
|
|
|
|
});
|
|
|
|
|
|
|
|
newConversationConnection_
|
|
|
|
= QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::newConversation,
|
|
|
|
[this](const QString& convUid) {
|
|
|
|
conversationSmartListModel_->fillConversationsList();
|
|
|
|
conversationSmartListModel_->setAccount(LRCInstance::getCurrAccId());
|
|
|
|
updateConversationForNewContact(convUid);
|
|
|
|
});
|
|
|
|
|
|
|
|
conversationRemovedConnection_
|
|
|
|
= QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::conversationRemoved,
|
|
|
|
[this]() {
|
|
|
|
conversationSmartListModel_->fillConversationsList();
|
|
|
|
backToWelcomePage();
|
|
|
|
});
|
|
|
|
|
|
|
|
conversationClearedConnection
|
|
|
|
= QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::conversationCleared,
|
|
|
|
[this](const QString& convUid) {
|
|
|
|
// If currently selected, switch to welcome screen (deselecting
|
|
|
|
// current smartlist item ).
|
|
|
|
if (convUid != LRCInstance::getCurrentConvUid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
backToWelcomePage();
|
|
|
|
});
|
|
|
|
|
|
|
|
searchStatusChangedConnection_
|
|
|
|
= QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::searchStatusChanged,
|
|
|
|
[this](const QString& status) { emit showSearchStatus(status); });
|
|
|
|
|
|
|
|
searchResultUpdatedConnection_
|
|
|
|
= QObject::connect(currentConversationModel,
|
|
|
|
&lrc::api::ConversationModel::searchResultUpdated,
|
|
|
|
[this]() {
|
|
|
|
conversationSmartListModel_->fillConversationsList();
|
|
|
|
conversationSmartListModel_->setAccount(LRCInstance::getCurrAccId());
|
|
|
|
emit updateListViewRequested();
|
|
|
|
});
|
|
|
|
|
|
|
|
if (updateFilter)
|
|
|
|
currentConversationModel->setFilter("");
|
2020-08-03 13:27:42 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-01 14:41:53 -04:00
|
|
|
void
|
|
|
|
ConversationsAdapter::disconnectConversationModel()
|
|
|
|
{
|
|
|
|
QObject::disconnect(modelSortedConnection_);
|
|
|
|
QObject::disconnect(modelUpdatedConnection_);
|
|
|
|
QObject::disconnect(filterChangedConnection_);
|
|
|
|
QObject::disconnect(conversationRemovedConnection_);
|
|
|
|
QObject::disconnect(conversationClearedConnection);
|
|
|
|
QObject::disconnect(interactionRemovedConnection_);
|
|
|
|
QObject::disconnect(searchStatusChangedConnection_);
|
|
|
|
QObject::disconnect(searchResultUpdatedConnection_);
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
void
|
2020-09-04 17:53:24 -04:00
|
|
|
ConversationsAdapter::updateConversationForNewContact(const QString& convUid)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-08-18 17:21:28 +02:00
|
|
|
auto* convModel = LRCInstance::getCurrentConversationModel();
|
2020-08-03 13:27:42 -04:00
|
|
|
if (convModel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2020-08-18 17:21:28 +02:00
|
|
|
const auto selectedUid = LRCInstance::getCurrentConvUid();
|
|
|
|
const auto conversation = convModel->getConversationForUID(convUid);
|
|
|
|
if (!conversation.uid.isEmpty() && !conversation.participants.isEmpty()) {
|
2020-08-03 13:27:42 -04:00
|
|
|
try {
|
2020-09-04 17:53:24 -04:00
|
|
|
const auto contact = convModel->owner.contactModel->getContact(
|
|
|
|
conversation.participants[0]);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (!contact.profileInfo.uri.isEmpty() && contact.profileInfo.uri == selectedUid) {
|
|
|
|
LRCInstance::setSelectedConvId(convUid);
|
|
|
|
convModel->selectConversation(convUid);
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 17:21:28 +02:00
|
|
|
}
|