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-09-03 09:24:49 -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 "messagesadapter.h"
|
|
|
|
|
2021-03-30 15:15:36 -04:00
|
|
|
#include "appsettingsmanager.h"
|
2020-09-04 14:51:39 -04:00
|
|
|
#include "qtutils.h"
|
2020-09-14 13:04:57 -04:00
|
|
|
#include "utils.h"
|
2020-09-03 09:24:49 -04:00
|
|
|
#include "webchathelpers.h"
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-06-28 09:14:31 -04:00
|
|
|
#include <api/datatransfermodel.h>
|
|
|
|
|
2020-09-04 14:51:39 -04:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QClipboard>
|
2020-08-03 13:27:42 -04:00
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QImageReader>
|
|
|
|
#include <QList>
|
|
|
|
#include <QUrl>
|
2021-04-08 16:03:25 -04:00
|
|
|
#include <QMimeData>
|
|
|
|
#include <QBuffer>
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-03-30 15:15:36 -04:00
|
|
|
MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager,
|
|
|
|
LRCInstance* instance,
|
|
|
|
QObject* parent)
|
2021-03-30 14:22:39 -04:00
|
|
|
: QmlAdapterBase(instance, parent)
|
2021-03-30 15:15:36 -04:00
|
|
|
, settingsManager_(settingsManager)
|
2020-08-03 13:27:42 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::safeInit()
|
|
|
|
{
|
2021-05-13 17:47:23 -04:00
|
|
|
connect(lrcInstance_, &LRCInstance::currentAccountIdChanged, [this]() {
|
2020-11-03 13:22:42 -05:00
|
|
|
currentConvUid_.clear();
|
2020-09-10 17:40:51 -04:00
|
|
|
connectConversationModel();
|
|
|
|
});
|
2020-09-01 16:54:06 -04:00
|
|
|
connectConversationModel();
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
void
|
2020-12-15 11:55:38 -05:00
|
|
|
MessagesAdapter::setupChatView(const QString& convUid)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-03-11 13:30:45 -05:00
|
|
|
auto* convModel = lrcInstance_->getCurrentConversationModel();
|
2020-08-18 17:21:28 +02:00
|
|
|
if (convModel == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2020-09-24 13:50:04 +02:00
|
|
|
|
2020-12-15 11:55:38 -05:00
|
|
|
if (currentConvUid_ == convUid)
|
2020-09-24 13:50:04 +02:00
|
|
|
return;
|
|
|
|
|
2021-03-11 13:30:45 -05:00
|
|
|
const auto& convInfo = lrcInstance_->getConversationFromConvUid(convUid);
|
2020-08-18 17:21:28 +02:00
|
|
|
if (convInfo.uid.isEmpty() || convInfo.participants.isEmpty()) {
|
2020-08-03 13:27:42 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString contactURI = convInfo.participants.at(0);
|
|
|
|
|
2021-05-13 17:47:23 -04:00
|
|
|
auto selectedAccountId = lrcInstance_->getCurrentAccountId();
|
2021-03-11 13:30:45 -05:00
|
|
|
auto& accountInfo = lrcInstance_->accountModel().getAccountInfo(selectedAccountId);
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-02-05 17:18:57 -05:00
|
|
|
lrc::api::contact::Info contactInfo;
|
2021-05-14 17:01:58 -04:00
|
|
|
QString bestName;
|
2020-08-03 13:27:42 -04:00
|
|
|
try {
|
2021-02-05 17:18:57 -05:00
|
|
|
contactInfo = accountInfo.contactModel->getContact(contactURI);
|
2021-05-14 17:01:58 -04:00
|
|
|
bestName = accountInfo.contactModel->bestNameForContact(contactURI);
|
2020-08-03 13:27:42 -04:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
2021-01-18 15:44:36 +01:00
|
|
|
bool isPending = contactInfo.profileInfo.type == profile::Type::TEMPORARY;
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
QMetaObject::invokeMethod(qmlObj_,
|
|
|
|
"setSendContactRequestButtonVisible",
|
2021-05-17 14:45:32 -04:00
|
|
|
Q_ARG(QVariant, convInfo.isNotASwarm() && isPending));
|
2021-05-14 17:01:58 -04:00
|
|
|
QMetaObject::invokeMethod(qmlObj_,
|
|
|
|
"setMessagingHeaderButtonsVisible",
|
|
|
|
Q_ARG(QVariant,
|
2021-05-17 14:45:32 -04:00
|
|
|
!(convInfo.isNotASwarm()
|
2021-05-14 17:01:58 -04:00
|
|
|
&& (convInfo.isRequest || convInfo.needsSyncing))));
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
setMessagesVisibility(false);
|
2021-05-17 14:45:32 -04:00
|
|
|
setIsSwarm(!convInfo.isNotASwarm());
|
2021-05-14 17:01:58 -04:00
|
|
|
setInvitation(convInfo.isRequest or convInfo.needsSyncing,
|
|
|
|
bestName,
|
|
|
|
contactURI,
|
2021-05-17 14:45:32 -04:00
|
|
|
!convInfo.isNotASwarm(),
|
2021-05-14 17:01:58 -04:00
|
|
|
convInfo.needsSyncing);
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-01-18 15:44:36 +01:00
|
|
|
// Draft and message content set up.
|
2020-08-03 13:27:42 -04:00
|
|
|
Utils::oneShotConnect(qmlObj_,
|
2020-09-10 17:40:51 -04:00
|
|
|
SIGNAL(sendMessageContentSaved(const QString&)),
|
2020-08-03 13:27:42 -04:00
|
|
|
this,
|
2020-09-10 17:40:51 -04:00
|
|
|
SLOT(slotSendMessageContentSaved(const QString&)));
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
requestSendMessageContent();
|
2020-09-24 13:50:04 +02:00
|
|
|
|
2020-12-15 11:55:38 -05:00
|
|
|
currentConvUid_ = convUid;
|
2021-02-05 17:18:57 -05:00
|
|
|
|
|
|
|
QString s = QString::fromLatin1("reset_message_bar_input(`%1`);")
|
|
|
|
.arg(accountInfo.contactModel->bestNameForContact(contactURI));
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-25 14:37:55 -04:00
|
|
|
MessagesAdapter::onNewInteraction(const QString& convUid,
|
|
|
|
const QString& interactionId,
|
|
|
|
const lrc::api::interaction::Info& interaction)
|
|
|
|
{
|
|
|
|
auto accountId = lrcInstance_->getCurrentAccountId();
|
|
|
|
newInteraction(accountId, convUid, interactionId, interaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::onInteractionStatusUpdated(const QString& convUid,
|
|
|
|
const QString& interactionId,
|
|
|
|
const lrc::api::interaction::Info& interaction)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-03-11 13:30:45 -05:00
|
|
|
auto currentConversationModel = lrcInstance_->getCurrentConversationModel();
|
2021-06-25 14:37:55 -04:00
|
|
|
currentConversationModel->clearUnreadInteractions(convUid);
|
|
|
|
updateInteraction(*currentConversationModel, interactionId, interaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::onInteractionRemoved(const QString& convUid, const QString& interactionId)
|
|
|
|
{
|
|
|
|
Q_UNUSED(convUid);
|
|
|
|
removeInteraction(interactionId);
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-06-25 14:37:55 -04:00
|
|
|
void
|
|
|
|
MessagesAdapter::onNewMessagesAvailable(const QString& accountId, const QString& conversationId)
|
|
|
|
{
|
|
|
|
auto* convModel = lrcInstance_->accountModel().getAccountInfo(accountId).conversationModel.get();
|
|
|
|
auto optConv = convModel->getConversationForUid(conversationId);
|
|
|
|
if (!optConv)
|
|
|
|
return;
|
|
|
|
updateHistory(*convModel, optConv->get().interactions, optConv->get().allMessagesLoaded);
|
|
|
|
Utils::oneShotConnect(qmlObj_, SIGNAL(messagesLoaded()), this, SLOT(slotMessagesLoaded()));
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-06-25 14:37:55 -04:00
|
|
|
void
|
|
|
|
MessagesAdapter::updateConversation(const QString& conversationId)
|
|
|
|
{
|
|
|
|
if (conversationId != lrcInstance_->get_selectedConvUid())
|
|
|
|
return;
|
|
|
|
auto* convModel = lrcInstance_->getCurrentConversationModel();
|
|
|
|
if (auto optConv = convModel->getConversationForUid(conversationId))
|
|
|
|
setConversationProfileData(optConv->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::onComposingStatusChanged(const QString& convId,
|
|
|
|
const QString& contactUri,
|
|
|
|
bool isComposing)
|
|
|
|
{
|
|
|
|
if (convId != lrcInstance_->get_selectedConvUid())
|
|
|
|
return;
|
|
|
|
if (!settingsManager_->getValue(Settings::Key::EnableTypingIndicator).toBool()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
contactIsComposing(contactUri, isComposing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::connectConversationModel()
|
|
|
|
{
|
|
|
|
auto currentConversationModel = lrcInstance_->getCurrentConversationModel();
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::newInteraction,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::onNewInteraction,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::interactionStatusUpdated,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::onInteractionStatusUpdated,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::interactionRemoved,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::onInteractionRemoved,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::newMessagesAvailable,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::onNewMessagesAvailable,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::conversationReady,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::updateConversation,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::needsSyncingSet,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::updateConversation,
|
|
|
|
Qt::UniqueConnection);
|
|
|
|
|
|
|
|
QObject::connect(currentConversationModel,
|
|
|
|
&ConversationModel::composingStatusChanged,
|
|
|
|
this,
|
|
|
|
&MessagesAdapter::onComposingStatusChanged,
|
|
|
|
Qt::UniqueConnection);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-22 14:31:42 -04:00
|
|
|
MessagesAdapter::sendConversationRequest()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-06-22 14:31:42 -04:00
|
|
|
lrcInstance_->makeConversationPermanent();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::slotSendMessageContentSaved(const QString& content)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
if (!LastConvUid_.isEmpty()) {
|
2021-05-13 17:47:23 -04:00
|
|
|
lrcInstance_->setContentDraft(LastConvUid_, lrcInstance_->getCurrentAccountId(), content);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
2021-04-08 16:53:12 -04:00
|
|
|
LastConvUid_ = lrcInstance_->get_selectedConvUid();
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
Utils::oneShotConnect(qmlObj_, SIGNAL(messagesCleared()), this, SLOT(slotMessagesCleared()));
|
|
|
|
|
|
|
|
setInvitation(false);
|
2021-06-22 14:31:42 -04:00
|
|
|
clearChatView();
|
2021-04-08 16:53:12 -04:00
|
|
|
auto restoredContent = lrcInstance_->getContentDraft(lrcInstance_->get_selectedConvUid(),
|
2021-05-13 17:47:23 -04:00
|
|
|
lrcInstance_->getCurrentAccountId());
|
2020-08-03 13:27:42 -04:00
|
|
|
setSendMessageContent(restoredContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::slotUpdateDraft(const QString& content)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
if (!LastConvUid_.isEmpty()) {
|
2021-05-13 17:47:23 -04:00
|
|
|
lrcInstance_->setContentDraft(LastConvUid_, lrcInstance_->getCurrentAccountId(), content);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::slotMessagesCleared()
|
|
|
|
{
|
2021-03-11 13:30:45 -05:00
|
|
|
auto* convModel = lrcInstance_->getCurrentConversationModel();
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2021-05-12 16:55:28 -04:00
|
|
|
auto convOpt = convModel->getConversationForUid(lrcInstance_->get_selectedConvUid());
|
|
|
|
if (!convOpt)
|
|
|
|
return;
|
2021-05-17 14:45:32 -04:00
|
|
|
if (!convOpt->get().isNotASwarm() && !convOpt->get().allMessagesLoaded) {
|
2020-12-23 18:48:06 +01:00
|
|
|
convModel->loadConversationMessages(convOpt->get().uid, 20);
|
2021-05-12 16:55:28 -04:00
|
|
|
} else {
|
|
|
|
printHistory(*convModel, convOpt->get().interactions);
|
|
|
|
Utils::oneShotConnect(qmlObj_, SIGNAL(messagesLoaded()), this, SLOT(slotMessagesLoaded()));
|
|
|
|
}
|
|
|
|
setConversationProfileData(convOpt->get());
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::slotMessagesLoaded()
|
|
|
|
{
|
|
|
|
setMessagesVisibility(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::sendMessage(const QString& message)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
try {
|
2021-04-08 16:53:12 -04:00
|
|
|
const auto convUid = lrcInstance_->get_selectedConvUid();
|
2021-03-11 13:30:45 -05:00
|
|
|
lrcInstance_->getCurrentConversationModel()->sendMessage(convUid, message);
|
2020-08-03 13:27:42 -04:00
|
|
|
} catch (...) {
|
|
|
|
qDebug() << "Exception during sendMessage:" << message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::sendImage(const QString& message)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
if (message.startsWith("data:image/png;base64,")) {
|
|
|
|
/*
|
|
|
|
* Img tag contains base64 data, trim "data:image/png;base64," from data.
|
|
|
|
*/
|
|
|
|
QByteArray data = QByteArray::fromStdString(message.toStdString().substr(22));
|
|
|
|
auto img_name_hash = QString::fromStdString(
|
|
|
|
QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex().toStdString());
|
|
|
|
QString fileName = "\\img_" + img_name_hash + ".png";
|
|
|
|
|
|
|
|
QPixmap image_to_save;
|
|
|
|
if (!image_to_save.loadFromData(QByteArray::fromBase64(data))) {
|
|
|
|
qDebug().noquote() << "Errors during loadFromData"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
QString path = QString(Utils::WinGetEnv("TEMP")) + fileName;
|
|
|
|
if (!image_to_save.save(path, "PNG")) {
|
|
|
|
qDebug().noquote() << "Errors during QPixmap save"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-04-08 16:53:12 -04:00
|
|
|
auto convUid = lrcInstance_->get_selectedConvUid();
|
2021-03-11 13:30:45 -05:00
|
|
|
lrcInstance_->getCurrentConversationModel()->sendFile(convUid, path, fileName);
|
2020-08-03 13:27:42 -04:00
|
|
|
} catch (...) {
|
|
|
|
qDebug().noquote() << "Exception during sendFile - base64 img"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Img tag contains file paths.
|
|
|
|
*/
|
|
|
|
|
2020-09-30 13:39:21 -04:00
|
|
|
// TODO: put all QRegExp strings together
|
2020-08-03 13:27:42 -04:00
|
|
|
QString msg(message);
|
|
|
|
#ifdef Q_OS_WIN
|
2020-09-30 13:39:21 -04:00
|
|
|
msg = msg.replace(QRegExp("^file:\\/{2,3}"), "");
|
2020-08-03 13:27:42 -04:00
|
|
|
#else
|
2020-09-30 13:39:21 -04:00
|
|
|
msg = msg.replace(QRegExp("^file:\\/{2,3}"), "/");
|
2020-08-03 13:27:42 -04:00
|
|
|
#endif
|
|
|
|
QFileInfo fi(msg);
|
|
|
|
QString fileName = fi.fileName();
|
|
|
|
|
|
|
|
try {
|
2021-04-08 16:53:12 -04:00
|
|
|
auto convUid = lrcInstance_->get_selectedConvUid();
|
2021-03-11 13:30:45 -05:00
|
|
|
lrcInstance_->getCurrentConversationModel()->sendFile(convUid, msg, fileName);
|
2020-08-03 13:27:42 -04:00
|
|
|
} catch (...) {
|
|
|
|
qDebug().noquote() << "Exception during sendFile - image from path"
|
|
|
|
<< "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::sendFile(const QString& message)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
QFileInfo fi(message);
|
|
|
|
QString fileName = fi.fileName();
|
|
|
|
try {
|
2021-04-08 16:53:12 -04:00
|
|
|
auto convUid = lrcInstance_->get_selectedConvUid();
|
2021-03-11 13:30:45 -05:00
|
|
|
lrcInstance_->getCurrentConversationModel()->sendFile(convUid, message, fileName);
|
2020-08-03 13:27:42 -04:00
|
|
|
} catch (...) {
|
|
|
|
qDebug() << "Exception during sendFile";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-12 16:55:28 -04:00
|
|
|
MessagesAdapter::retryInteraction(const QString& interactionId)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-12 16:55:28 -04:00
|
|
|
lrcInstance_->getCurrentConversationModel()
|
|
|
|
->retryInteraction(lrcInstance_->get_selectedConvUid(), interactionId);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:14:31 -04:00
|
|
|
void
|
|
|
|
MessagesAdapter::copyToDownloads(const QString& interactionId, const QString& displayName)
|
|
|
|
{
|
|
|
|
auto downloadDir = lrcInstance_->accountModel().downloadDirectory;
|
|
|
|
if (auto accInfo = &lrcInstance_->getCurrentAccountInfo())
|
|
|
|
accInfo->dataTransferModel->copyTo(lrcInstance_->getCurrentAccountId(),
|
|
|
|
lrcInstance_->get_selectedConvUid(),
|
|
|
|
interactionId,
|
|
|
|
downloadDir,
|
|
|
|
displayName);
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::setNewMessagesContent(const QString& path)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
if (path.length() == 0)
|
|
|
|
return;
|
|
|
|
|
2021-06-11 11:16:10 -04:00
|
|
|
// QImageReader will treat .gz file (Jami archive) as svgz image format
|
|
|
|
// so decideFormatFromContent is needed
|
|
|
|
QImageReader reader;
|
|
|
|
reader.setDecideFormatFromContent(true);
|
|
|
|
reader.setFileName(path);
|
|
|
|
|
|
|
|
if (!reader.read().isNull()) {
|
2020-08-03 13:27:42 -04:00
|
|
|
setMessagesImageContent(path);
|
|
|
|
} else {
|
|
|
|
setMessagesFileContent(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-12 16:55:28 -04:00
|
|
|
MessagesAdapter::deleteInteraction(const QString& interactionId)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-12 16:55:28 -04:00
|
|
|
lrcInstance_->getCurrentConversationModel()
|
|
|
|
->clearInteractionFromConversation(lrcInstance_->get_selectedConvUid(), interactionId);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::openFile(const QString& arg)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2020-09-30 13:39:21 -04:00
|
|
|
QUrl fileUrl("file:///" + arg);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (!QDesktopServices::openUrl(fileUrl)) {
|
|
|
|
qDebug() << "Couldn't open file: " << fileUrl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-29 13:45:07 -04:00
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::openUrl(const QString& url)
|
2020-08-29 13:45:07 -04:00
|
|
|
{
|
|
|
|
if (!QDesktopServices::openUrl(url)) {
|
|
|
|
qDebug() << "Couldn't open url: " << url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
void
|
2021-05-12 16:55:28 -04:00
|
|
|
MessagesAdapter::acceptFile(const QString& interactionId)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-12 16:55:28 -04:00
|
|
|
auto convUid = lrcInstance_->get_selectedConvUid();
|
|
|
|
lrcInstance_->getCurrentConversationModel()->acceptTransfer(convUid, interactionId);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-12 16:55:28 -04:00
|
|
|
MessagesAdapter::refuseFile(const QString& interactionId)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-12 16:55:28 -04:00
|
|
|
const auto convUid = lrcInstance_->get_selectedConvUid();
|
|
|
|
lrcInstance_->getCurrentConversationModel()->cancelTransfer(convUid, interactionId);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::pasteKeyDetected()
|
|
|
|
{
|
2020-09-10 17:40:51 -04:00
|
|
|
const QMimeData* mimeData = QApplication::clipboard()->mimeData();
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
if (mimeData->hasImage()) {
|
|
|
|
/*
|
|
|
|
* Save temp data into base64 format.
|
|
|
|
*/
|
|
|
|
QPixmap pixmap = qvariant_cast<QPixmap>(mimeData->imageData());
|
|
|
|
QByteArray ba;
|
|
|
|
QBuffer bu(&ba);
|
|
|
|
bu.open(QIODevice::WriteOnly);
|
|
|
|
pixmap.save(&bu, "PNG");
|
2020-12-03 13:25:34 -05:00
|
|
|
auto str = Utils::byteArrayToBase64String(ba);
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
setMessagesImageContent(str, true);
|
|
|
|
} else if (mimeData->hasUrls()) {
|
|
|
|
QList<QUrl> urlList = mimeData->urls();
|
|
|
|
/*
|
2020-09-10 17:40:51 -04:00
|
|
|
* Extract the local paths of the files.
|
|
|
|
*/
|
2020-08-03 13:27:42 -04:00
|
|
|
for (int i = 0; i < urlList.size(); ++i) {
|
|
|
|
/*
|
2020-09-30 13:39:21 -04:00
|
|
|
* Trim file:// or file:/// from url.
|
2020-08-03 13:27:42 -04:00
|
|
|
*/
|
2020-09-30 13:39:21 -04:00
|
|
|
QString filePath = urlList.at(i).toString().remove(QRegExp("^file:\\/{2,3}"));
|
2020-08-03 13:27:42 -04:00
|
|
|
QByteArray imageFormat = QImageReader::imageFormat(filePath);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if file is qt supported image file type.
|
|
|
|
*/
|
|
|
|
if (!imageFormat.isEmpty()) {
|
|
|
|
setMessagesImageContent(filePath);
|
|
|
|
} else {
|
|
|
|
setMessagesFileContent(filePath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-04 15:39:10 -05:00
|
|
|
// Treat as text content, make chatview.js handle in order to
|
|
|
|
// avoid string escape problems
|
2020-08-03 13:27:42 -04:00
|
|
|
QMetaObject::invokeMethod(qmlObj_,
|
|
|
|
"webViewRunJavaScript",
|
2021-01-04 15:39:10 -05:00
|
|
|
Q_ARG(QVariant, QStringLiteral("replaceText()")));
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::onComposing(bool isComposing)
|
|
|
|
{
|
2021-03-30 15:15:36 -04:00
|
|
|
if (!settingsManager_->getValue(Settings::Key::EnableTypingIndicator).toBool()) {
|
2020-11-05 14:41:25 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-04-08 16:53:12 -04:00
|
|
|
lrcInstance_->getCurrentConversationModel()->setIsComposing(lrcInstance_->get_selectedConvUid(),
|
2021-03-11 13:30:45 -05:00
|
|
|
isComposing);
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::setConversationProfileData(const lrc::api::conversation::Info& convInfo)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-03-11 13:30:45 -05:00
|
|
|
auto accInfo = &lrcInstance_->getCurrentAccountInfo();
|
2020-08-18 17:21:28 +02:00
|
|
|
|
2020-12-15 11:55:38 -05:00
|
|
|
if (convInfo.participants.isEmpty()) {
|
2020-08-18 17:21:28 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-03 13:27:42 -04:00
|
|
|
|
2020-12-15 11:55:38 -05:00
|
|
|
auto contactUri = convInfo.participants.front();
|
2020-08-03 13:27:42 -04:00
|
|
|
if (contactUri.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2020-09-10 17:40:51 -04:00
|
|
|
auto& contact = accInfo->contactModel->getContact(contactUri);
|
2020-10-20 12:11:09 -04:00
|
|
|
auto bestName = accInfo->contactModel->bestNameForContact(contactUri);
|
2021-05-17 14:45:32 -04:00
|
|
|
bool isPending = contact.profileInfo.type == profile::Type::TEMPORARY;
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(qmlObj_,
|
|
|
|
"setSendContactRequestButtonVisible",
|
|
|
|
Q_ARG(QVariant, convInfo.isNotASwarm() && isPending));
|
|
|
|
QMetaObject::invokeMethod(qmlObj_,
|
|
|
|
"setMessagingHeaderButtonsVisible",
|
|
|
|
Q_ARG(QVariant,
|
|
|
|
!(!convInfo.isNotASwarm()
|
|
|
|
&& (convInfo.isRequest || convInfo.needsSyncing))));
|
|
|
|
|
2021-05-14 17:01:58 -04:00
|
|
|
setInvitation(convInfo.isRequest or convInfo.needsSyncing,
|
2020-08-03 13:27:42 -04:00
|
|
|
bestName,
|
2021-05-14 17:01:58 -04:00
|
|
|
contactUri,
|
2021-05-17 14:45:32 -04:00
|
|
|
!convInfo.isNotASwarm(),
|
2021-05-14 17:01:58 -04:00
|
|
|
convInfo.needsSyncing);
|
2020-08-03 13:27:42 -04:00
|
|
|
if (!contact.profileInfo.avatar.isEmpty()) {
|
|
|
|
setSenderImage(contactUri, contact.profileInfo.avatar);
|
|
|
|
} else {
|
2021-03-11 13:30:45 -05:00
|
|
|
auto avatar = Utils::contactPhoto(lrcInstance_, convInfo.participants[0]);
|
2020-08-03 13:27:42 -04:00
|
|
|
QByteArray ba;
|
|
|
|
QBuffer bu(&ba);
|
|
|
|
avatar.save(&bu, "PNG");
|
|
|
|
setSenderImage(contactUri, QString::fromLocal8Bit(ba.toBase64()));
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::newInteraction(const QString& accountId,
|
|
|
|
const QString& convUid,
|
2021-05-12 16:55:28 -04:00
|
|
|
const QString& interactionId,
|
2020-09-10 17:40:51 -04:00
|
|
|
const interaction::Info& interaction)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
Q_UNUSED(interactionId);
|
|
|
|
try {
|
2021-04-08 16:53:12 -04:00
|
|
|
if (convUid.isEmpty() || convUid != lrcInstance_->get_selectedConvUid()) {
|
2020-08-03 13:27:42 -04:00
|
|
|
return;
|
|
|
|
}
|
2021-03-11 13:30:45 -05:00
|
|
|
auto& accountInfo = lrcInstance_->getAccountInfo(accountId);
|
2020-09-14 13:04:57 -04:00
|
|
|
auto& convModel = accountInfo.conversationModel;
|
2020-08-03 13:27:42 -04:00
|
|
|
convModel->clearUnreadInteractions(convUid);
|
|
|
|
printNewInteraction(*convModel, interactionId, interaction);
|
2021-05-20 16:56:33 -04:00
|
|
|
Q_EMIT newInteraction(static_cast<int>(interaction.type));
|
2020-08-03 13:27:42 -04:00
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::updateDraft()
|
|
|
|
{
|
2020-11-04 14:50:47 -05:00
|
|
|
currentConvUid_.clear();
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
Utils::oneShotConnect(qmlObj_,
|
2020-09-10 17:40:51 -04:00
|
|
|
SIGNAL(sendMessageContentSaved(const QString&)),
|
2020-08-03 13:27:42 -04:00
|
|
|
this,
|
2020-09-10 17:40:51 -04:00
|
|
|
SLOT(slotUpdateDraft(const QString&)));
|
2020-08-03 13:27:42 -04:00
|
|
|
|
|
|
|
requestSendMessageContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* JS invoke.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
MessagesAdapter::setMessagesVisibility(bool visible)
|
|
|
|
{
|
|
|
|
QString s = QString::fromLatin1(visible ? "showMessagesDiv();" : "hideMessagesDiv();");
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::requestSendMessageContent()
|
|
|
|
{
|
|
|
|
QString s = QString::fromLatin1("requestSendMessageContent();");
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-14 17:01:58 -04:00
|
|
|
MessagesAdapter::setInvitation(
|
|
|
|
bool show, const QString& contactUri, const QString& contactId, bool isSwarm, bool needsSyncing)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-14 17:01:58 -04:00
|
|
|
QString s = show ? QString::fromLatin1("showInvitation(\"%1\", \"%2\", %3, %4)")
|
|
|
|
.arg(contactUri)
|
|
|
|
.arg(contactId)
|
|
|
|
.arg(isSwarm)
|
|
|
|
.arg(needsSyncing)
|
|
|
|
: QString::fromLatin1("showInvitation()");
|
2020-08-03 13:27:42 -04:00
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
2021-05-17 10:06:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::setIsSwarm(bool isSwarm)
|
|
|
|
{
|
|
|
|
QString s = QString::fromLatin1("set_is_swarm(%1)").arg(isSwarm);
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-22 14:31:42 -04:00
|
|
|
MessagesAdapter::clearChatView()
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
QString s = QString::fromLatin1("clearMessages();");
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
2020-11-05 14:41:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::setDisplayLinks()
|
|
|
|
{
|
2021-06-25 14:37:55 -04:00
|
|
|
QString s
|
|
|
|
= QString::fromLatin1("setDisplayLinks(%1);")
|
|
|
|
.arg(settingsManager_->getValue(Settings::Key::DisplayHyperlinkPreviews).toBool());
|
2020-11-05 14:41:25 -05:00
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::printHistory(lrc::api::ConversationModel& conversationModel,
|
2021-05-12 16:55:28 -04:00
|
|
|
MessagesList interactions)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-17 14:45:32 -04:00
|
|
|
auto interactionsStr = interactionsToJsonArrayObject(conversationModel,
|
|
|
|
lrcInstance_->get_selectedConvUid(),
|
|
|
|
interactions)
|
|
|
|
.toUtf8();
|
2020-08-03 13:27:42 -04:00
|
|
|
QString s = QString::fromLatin1("printHistory(%1);").arg(interactionsStr.constData());
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
2020-12-23 18:48:06 +01:00
|
|
|
void
|
|
|
|
MessagesAdapter::updateHistory(lrc::api::ConversationModel& conversationModel,
|
|
|
|
MessagesList interactions,
|
|
|
|
bool allLoaded)
|
|
|
|
{
|
2021-05-17 14:45:32 -04:00
|
|
|
auto interactionsStr = interactionsToJsonArrayObject(conversationModel,
|
|
|
|
lrcInstance_->get_selectedConvUid(),
|
|
|
|
interactions)
|
|
|
|
.toUtf8();
|
2020-12-23 18:48:06 +01:00
|
|
|
QString s = QString::fromLatin1("updateHistory(%1, %2);")
|
|
|
|
.arg(interactionsStr.constData())
|
|
|
|
.arg(allLoaded);
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::setSenderImage(const QString& sender, const QString& senderImage)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
QJsonObject setSenderImageObject = QJsonObject();
|
|
|
|
setSenderImageObject.insert("sender_contact_method", QJsonValue(sender));
|
|
|
|
setSenderImageObject.insert("sender_image", QJsonValue(senderImage));
|
|
|
|
|
|
|
|
auto setSenderImageObjectString = QString(
|
|
|
|
QJsonDocument(setSenderImageObject).toJson(QJsonDocument::Compact));
|
|
|
|
QString s = QString::fromLatin1("setSenderImage(%1);")
|
|
|
|
.arg(setSenderImageObjectString.toUtf8().constData());
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::printNewInteraction(lrc::api::ConversationModel& conversationModel,
|
2021-05-12 16:55:28 -04:00
|
|
|
const QString& msgId,
|
2020-09-10 17:40:51 -04:00
|
|
|
const lrc::api::interaction::Info& interaction)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-17 14:45:32 -04:00
|
|
|
auto interactionObject = interactionToJsonInteractionObject(conversationModel,
|
|
|
|
lrcInstance_->get_selectedConvUid(),
|
|
|
|
msgId,
|
|
|
|
interaction)
|
|
|
|
.toUtf8();
|
2020-08-03 13:27:42 -04:00
|
|
|
if (interactionObject.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString s = QString::fromLatin1("addMessage(%1);").arg(interactionObject.constData());
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::updateInteraction(lrc::api::ConversationModel& conversationModel,
|
2021-05-12 16:55:28 -04:00
|
|
|
const QString& msgId,
|
2020-09-10 17:40:51 -04:00
|
|
|
const lrc::api::interaction::Info& interaction)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-17 14:45:32 -04:00
|
|
|
auto interactionObject = interactionToJsonInteractionObject(conversationModel,
|
|
|
|
lrcInstance_->get_selectedConvUid(),
|
|
|
|
msgId,
|
|
|
|
interaction)
|
|
|
|
.toUtf8();
|
2020-08-03 13:27:42 -04:00
|
|
|
if (interactionObject.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString s = QString::fromLatin1("updateMessage(%1);").arg(interactionObject.constData());
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::setMessagesImageContent(const QString& path, bool isBased64)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
if (isBased64) {
|
2020-11-18 11:58:09 -05:00
|
|
|
QString param = QString("addImage_base64('%1')").arg(path);
|
2020-08-03 13:27:42 -04:00
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, param));
|
|
|
|
} else {
|
|
|
|
QString param = QString("addImage_path('file://%1')").arg(path);
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, param));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::setMessagesFileContent(const QString& path)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
|
|
|
qint64 fileSize = QFileInfo(path).size();
|
|
|
|
QString fileName = QFileInfo(path).fileName();
|
2021-06-11 11:02:39 -04:00
|
|
|
|
2020-08-03 13:27:42 -04:00
|
|
|
QString param = QString("addFile_path('%1','%2','%3')")
|
|
|
|
.arg(path, fileName, Utils::humanFileSize(fileSize));
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, param));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-05-12 16:55:28 -04:00
|
|
|
MessagesAdapter::removeInteraction(const QString& interactionId)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-12 16:55:28 -04:00
|
|
|
QString s = QString::fromLatin1("removeInteraction(%1);").arg(interactionId);
|
2020-08-03 13:27:42 -04:00
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::setSendMessageContent(const QString& content)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-01-04 15:39:10 -05:00
|
|
|
QMetaObject::invokeMethod(qmlObj_, "setSendMessageContent", Q_ARG(QVariant, content));
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-25 13:28:27 -04:00
|
|
|
MessagesAdapter::contactIsComposing(const QString& contactUri, bool isComposing)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-05-12 16:55:28 -04:00
|
|
|
auto* convModel = lrcInstance_->getCurrentConversationModel();
|
|
|
|
auto convInfo = convModel->getConversationForUid(lrcInstance_->get_selectedConvUid());
|
|
|
|
if (!convInfo)
|
|
|
|
return;
|
|
|
|
auto& conv = convInfo->get();
|
2021-06-25 13:28:27 -04:00
|
|
|
bool showIsComposing = conv.participants.first() == contactUri;
|
2021-05-12 16:55:28 -04:00
|
|
|
if (showIsComposing) {
|
2020-08-03 13:27:42 -04:00
|
|
|
QString s
|
|
|
|
= QString::fromLatin1("showTypingIndicator(`%1`, %2);").arg(contactUri).arg(isComposing);
|
|
|
|
QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-25 14:33:56 -04:00
|
|
|
MessagesAdapter::acceptInvitation(const QString& convId)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-06-25 14:33:56 -04:00
|
|
|
auto conversationId = convId.isEmpty() ? lrcInstance_->get_selectedConvUid() : convId;
|
|
|
|
auto* convModel = lrcInstance_->getCurrentConversationModel();
|
|
|
|
convModel->acceptConversationRequest(conversationId);
|
|
|
|
if (conversationId == currentConvUid_)
|
2021-02-11 12:50:31 +01:00
|
|
|
currentConvUid_.clear();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::refuseInvitation(const QString& convUid)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-04-08 16:53:12 -04:00
|
|
|
const auto currentConvUid = convUid.isEmpty() ? lrcInstance_->get_selectedConvUid() : convUid;
|
2021-03-11 13:30:45 -05:00
|
|
|
lrcInstance_->getCurrentConversationModel()->removeConversation(currentConvUid, false);
|
2020-08-03 13:27:42 -04:00
|
|
|
setInvitation(false);
|
2020-12-15 13:49:19 -05:00
|
|
|
if (convUid == currentConvUid_)
|
|
|
|
currentConvUid_.clear();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-09-10 17:40:51 -04:00
|
|
|
MessagesAdapter::blockConversation(const QString& convUid)
|
2020-08-03 13:27:42 -04:00
|
|
|
{
|
2021-04-08 16:53:12 -04:00
|
|
|
const auto currentConvUid = convUid.isEmpty() ? lrcInstance_->get_selectedConvUid() : convUid;
|
2021-03-11 13:30:45 -05:00
|
|
|
lrcInstance_->getCurrentConversationModel()->removeConversation(currentConvUid, true);
|
2020-08-03 13:27:42 -04:00
|
|
|
setInvitation(false);
|
2020-12-15 13:49:19 -05:00
|
|
|
if (convUid == currentConvUid_)
|
|
|
|
currentConvUid_.clear();
|
2021-03-29 16:51:53 -04:00
|
|
|
Q_EMIT contactBanned();
|
2020-08-03 13:27:42 -04:00
|
|
|
}
|
2020-12-15 13:49:19 -05:00
|
|
|
|
|
|
|
void
|
2021-02-26 09:42:33 +01:00
|
|
|
MessagesAdapter::clearConversationHistory(const QString& accountId, const QString& convUid)
|
2020-12-15 13:49:19 -05:00
|
|
|
{
|
2021-02-26 09:42:33 +01:00
|
|
|
lrcInstance_->getAccountInfo(accountId).conversationModel->clearHistory(convUid);
|
|
|
|
if (convUid == currentConvUid_)
|
2020-12-15 13:49:19 -05:00
|
|
|
currentConvUid_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-26 09:42:33 +01:00
|
|
|
MessagesAdapter::removeConversation(const QString& accountId,
|
|
|
|
const QString& convUid,
|
|
|
|
bool banContact)
|
2020-12-15 13:49:19 -05:00
|
|
|
{
|
2021-02-26 09:42:33 +01:00
|
|
|
QStringList list = lrcInstance_->accountModel().getDefaultModerators(accountId);
|
|
|
|
const auto& convInfo = lrcInstance_->getConversationFromConvUid(convUid, accountId);
|
|
|
|
const auto contactURI = convInfo.participants.front();
|
|
|
|
|
|
|
|
if (!contactURI.isEmpty() && list.contains(contactURI)) {
|
|
|
|
lrcInstance_->accountModel().setDefaultModerator(accountId, contactURI, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
lrcInstance_->getAccountInfo(accountId).conversationModel->removeConversation(convUid,
|
2021-03-30 14:22:39 -04:00
|
|
|
banContact);
|
2021-02-26 09:42:33 +01:00
|
|
|
if (convUid == currentConvUid_)
|
2020-12-15 13:49:19 -05:00
|
|
|
currentConvUid_.clear();
|
|
|
|
}
|
2020-12-23 18:48:06 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
MessagesAdapter::loadMessages(int n)
|
|
|
|
{
|
|
|
|
auto* convModel = lrcInstance_->getCurrentConversationModel();
|
|
|
|
auto convOpt = convModel->getConversationForUid(currentConvUid_);
|
|
|
|
if (!convOpt)
|
|
|
|
return;
|
2021-05-17 14:45:32 -04:00
|
|
|
if (!convOpt->get().isNotASwarm() && !convOpt->get().allMessagesLoaded)
|
2020-12-23 18:48:06 +01:00
|
|
|
convModel->loadConversationMessages(convOpt->get().uid, n);
|
|
|
|
}
|