Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 by Savoir-faire Linux |
| 3 | * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com> |
| 4 | * Author: Anthony L�onard <anthony.leonard@savoirfairelinux.com> |
| 5 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> |
| 6 | * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> |
| 7 | * Author: Isa Nanic <isa.nanic@savoirfairelinux.com> |
| 8 | * Author: Mingrui Zhang <mingrui.zhang@savoirfairelinux.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
| 23 | |
| 24 | #include "messagesadapter.h" |
| 25 | #include "webchathelpers.h" |
| 26 | |
| 27 | #include "utils.h" |
| 28 | |
| 29 | #include <QDesktopServices> |
| 30 | #include <QFileInfo> |
| 31 | #include <QImageReader> |
| 32 | #include <QList> |
| 33 | #include <QUrl> |
| 34 | |
| 35 | MessagesAdapter::MessagesAdapter(QObject *parent) |
| 36 | : QmlAdapterBase(parent) |
| 37 | {} |
| 38 | |
| 39 | MessagesAdapter::~MessagesAdapter() {} |
| 40 | |
| 41 | void |
ababi | 69f5dfc | 2020-08-25 15:07:57 +0200 | [diff] [blame] | 42 | MessagesAdapter::initQmlObject() {} |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 43 | |
| 44 | void |
| 45 | MessagesAdapter::setupChatView(const QString &uid) |
| 46 | { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 47 | |
| 48 | auto* convModel = LRCInstance::getCurrentConversationModel(); |
| 49 | if (convModel == nullptr) { |
| 50 | return; |
| 51 | } |
| 52 | const auto &convInfo = convModel->getConversationForUID(uid); |
| 53 | if (convInfo.uid.isEmpty() || convInfo.participants.isEmpty()) { |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
| 57 | QString contactURI = convInfo.participants.at(0); |
| 58 | |
| 59 | bool isContact = false; |
| 60 | auto selectedAccountId = LRCInstance::getCurrAccId(); |
| 61 | auto &accountInfo = LRCInstance::accountModel().getAccountInfo(selectedAccountId); |
| 62 | |
| 63 | lrc::api::profile::Type contactType; |
| 64 | try { |
| 65 | auto contactInfo = accountInfo.contactModel->getContact(contactURI); |
| 66 | if (contactInfo.isTrusted) { |
| 67 | isContact = true; |
| 68 | } |
| 69 | contactType = contactInfo.profileInfo.type; |
| 70 | } catch (...) { |
| 71 | } |
| 72 | |
| 73 | bool shouldShowSendContactRequestBtn = !isContact |
| 74 | && contactType != lrc::api::profile::Type::SIP; |
| 75 | |
| 76 | QMetaObject::invokeMethod(qmlObj_, |
| 77 | "setSendContactRequestButtonVisible", |
| 78 | Q_ARG(QVariant, shouldShowSendContactRequestBtn)); |
| 79 | |
| 80 | setMessagesVisibility(false); |
| 81 | |
| 82 | /* |
| 83 | * Type Indicator (contact). |
| 84 | */ |
| 85 | contactIsComposing(convInfo.uid, "", false); |
| 86 | connect(LRCInstance::getCurrentConversationModel(), |
| 87 | &ConversationModel::composingStatusChanged, |
| 88 | [this](const QString &uid, const QString &contactUri, bool isComposing) { |
| 89 | contactIsComposing(uid, contactUri, isComposing); |
| 90 | }); |
| 91 | |
| 92 | /* |
| 93 | * Draft and message content set up. |
| 94 | */ |
| 95 | Utils::oneShotConnect(qmlObj_, |
| 96 | SIGNAL(sendMessageContentSaved(const QString &)), |
| 97 | this, |
| 98 | SLOT(slotSendMessageContentSaved(const QString &))); |
| 99 | |
| 100 | requestSendMessageContent(); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | MessagesAdapter::connectConversationModel() |
| 105 | { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 106 | auto currentConversationModel = LRCInstance::getCurrentConversationModel(); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 107 | |
| 108 | QObject::disconnect(newInteractionConnection_); |
| 109 | QObject::disconnect(interactionRemovedConnection_); |
| 110 | QObject::disconnect(interactionStatusUpdatedConnection_); |
| 111 | |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 112 | newInteractionConnection_ = QObject::connect(currentConversationModel, |
| 113 | &lrc::api::ConversationModel::newInteraction, |
| 114 | [this](const QString &convUid, uint64_t interactionId, |
| 115 | const lrc::api::interaction::Info &interaction) { |
| 116 | auto accountId = LRCInstance::getCurrAccId(); |
| 117 | newInteraction(accountId, convUid, interactionId, interaction); |
| 118 | }); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 119 | |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 120 | interactionStatusUpdatedConnection_ = QObject::connect(currentConversationModel, |
| 121 | &lrc::api::ConversationModel::interactionStatusUpdated, |
| 122 | [this](const QString &convUid, uint64_t interactionId, |
| 123 | const lrc::api::interaction::Info &interaction) { |
| 124 | auto currentConversationModel = LRCInstance::getCurrentConversationModel(); |
| 125 | currentConversationModel->clearUnreadInteractions(convUid); |
| 126 | updateInteraction(*currentConversationModel, interactionId, interaction); |
| 127 | }); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 128 | |
| 129 | interactionRemovedConnection_ |
| 130 | = QObject::connect(currentConversationModel, |
| 131 | &lrc::api::ConversationModel::interactionRemoved, |
| 132 | [this](const QString &convUid, uint64_t interactionId) { |
| 133 | Q_UNUSED(convUid); |
| 134 | removeInteraction(interactionId); |
| 135 | }); |
| 136 | |
| 137 | currentConversationModel->setFilter(""); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | MessagesAdapter::sendContactRequest() |
| 142 | { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 143 | const auto convUid = LRCInstance::getCurrentConvUid(); |
| 144 | if (!convUid.isEmpty()) { |
| 145 | LRCInstance::getCurrentConversationModel()->makePermanent(convUid); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | void |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 150 | MessagesAdapter::updateConversationForAddedContact() |
| 151 | { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 152 | auto* convModel = LRCInstance::getCurrentConversationModel(); |
| 153 | const auto conversation = convModel->getConversationForUID(LRCInstance::getCurrentConvUid()); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 154 | |
| 155 | clear(); |
| 156 | setConversationProfileData(conversation); |
| 157 | printHistory(*convModel, conversation.interactions); |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | MessagesAdapter::slotSendMessageContentSaved(const QString &content) |
| 162 | { |
| 163 | if (!LastConvUid_.isEmpty()) { |
| 164 | LRCInstance::setContentDraft(LastConvUid_, LRCInstance::getCurrAccId(), content); |
| 165 | } |
| 166 | LastConvUid_ = LRCInstance::getCurrentConvUid(); |
| 167 | |
| 168 | Utils::oneShotConnect(qmlObj_, SIGNAL(messagesCleared()), this, SLOT(slotMessagesCleared())); |
| 169 | |
| 170 | setInvitation(false); |
| 171 | clear(); |
| 172 | auto restoredContent = LRCInstance::getContentDraft(LRCInstance::getCurrentConvUid(), |
| 173 | LRCInstance::getCurrAccId()); |
| 174 | setSendMessageContent(restoredContent); |
| 175 | emit needToUpdateSmartList(); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | MessagesAdapter::slotUpdateDraft(const QString &content) |
| 180 | { |
| 181 | if (!LastConvUid_.isEmpty()) { |
| 182 | LRCInstance::setContentDraft(LastConvUid_, LRCInstance::getCurrAccId(), content); |
| 183 | } |
| 184 | emit needToUpdateSmartList(); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | MessagesAdapter::slotMessagesCleared() |
| 189 | { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 190 | auto* convModel = LRCInstance::getCurrentConversationModel(); |
| 191 | const auto convInfo = convModel->getConversationForUID(LRCInstance::getCurrentConvUid()); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 192 | |
| 193 | printHistory(*convModel, convInfo.interactions); |
| 194 | |
| 195 | Utils::oneShotConnect(qmlObj_, SIGNAL(messagesLoaded()), this, SLOT(slotMessagesLoaded())); |
| 196 | |
| 197 | setConversationProfileData(convInfo); |
| 198 | } |
| 199 | |
| 200 | void |
| 201 | MessagesAdapter::slotMessagesLoaded() |
| 202 | { |
| 203 | setMessagesVisibility(true); |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | MessagesAdapter::sendMessage(const QString &message) |
| 208 | { |
| 209 | try { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 210 | const auto convUid = LRCInstance::getCurrentConvUid(); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 211 | LRCInstance::getCurrentConversationModel()->sendMessage(convUid, message); |
| 212 | } catch (...) { |
| 213 | qDebug() << "Exception during sendMessage:" << message; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | void |
| 218 | MessagesAdapter::sendImage(const QString &message) |
| 219 | { |
| 220 | if (message.startsWith("data:image/png;base64,")) { |
| 221 | /* |
| 222 | * Img tag contains base64 data, trim "data:image/png;base64," from data. |
| 223 | */ |
| 224 | QByteArray data = QByteArray::fromStdString(message.toStdString().substr(22)); |
| 225 | auto img_name_hash = QString::fromStdString( |
| 226 | QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex().toStdString()); |
| 227 | QString fileName = "\\img_" + img_name_hash + ".png"; |
| 228 | |
| 229 | QPixmap image_to_save; |
| 230 | if (!image_to_save.loadFromData(QByteArray::fromBase64(data))) { |
| 231 | qDebug().noquote() << "Errors during loadFromData" |
| 232 | << "\n"; |
| 233 | } |
| 234 | |
| 235 | QString path = QString(Utils::WinGetEnv("TEMP")) + fileName; |
| 236 | if (!image_to_save.save(path, "PNG")) { |
| 237 | qDebug().noquote() << "Errors during QPixmap save" |
| 238 | << "\n"; |
| 239 | } |
| 240 | |
| 241 | try { |
| 242 | auto convUid = LRCInstance::getCurrentConvUid(); |
| 243 | LRCInstance::getCurrentConversationModel()->sendFile(convUid, path, fileName); |
| 244 | } catch (...) { |
| 245 | qDebug().noquote() << "Exception during sendFile - base64 img" |
| 246 | << "\n"; |
| 247 | } |
| 248 | |
| 249 | } else { |
| 250 | /* |
| 251 | * Img tag contains file paths. |
| 252 | */ |
| 253 | |
| 254 | QString msg(message); |
| 255 | #ifdef Q_OS_WIN |
| 256 | msg = msg.replace("file:///", ""); |
| 257 | #else |
| 258 | msg = msg.replace("file:///", "/"); |
| 259 | #endif |
| 260 | QFileInfo fi(msg); |
| 261 | QString fileName = fi.fileName(); |
| 262 | |
| 263 | try { |
| 264 | auto convUid = LRCInstance::getCurrentConvUid(); |
| 265 | LRCInstance::getCurrentConversationModel()->sendFile(convUid, msg, fileName); |
| 266 | } catch (...) { |
| 267 | qDebug().noquote() << "Exception during sendFile - image from path" |
| 268 | << "\n"; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void |
| 274 | MessagesAdapter::sendFile(const QString &message) |
| 275 | { |
| 276 | QFileInfo fi(message); |
| 277 | QString fileName = fi.fileName(); |
| 278 | try { |
| 279 | auto convUid = LRCInstance::getCurrentConvUid(); |
| 280 | LRCInstance::getCurrentConversationModel()->sendFile(convUid, message, fileName); |
| 281 | } catch (...) { |
| 282 | qDebug() << "Exception during sendFile"; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void |
| 287 | MessagesAdapter::retryInteraction(const QString &arg) |
| 288 | { |
| 289 | bool ok; |
| 290 | uint64_t interactionUid = arg.toULongLong(&ok); |
| 291 | if (ok) { |
| 292 | LRCInstance::getCurrentConversationModel() |
| 293 | ->retryInteraction(LRCInstance::getCurrentConvUid(), interactionUid); |
| 294 | } else { |
| 295 | qDebug() << "retryInteraction - invalid arg" << arg; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | void |
| 300 | MessagesAdapter::setNewMessagesContent(const QString &path) |
| 301 | { |
| 302 | if (path.length() == 0) |
| 303 | return; |
| 304 | QByteArray imageFormat = QImageReader::imageFormat(path); |
| 305 | |
| 306 | if (!imageFormat.isEmpty()) { |
| 307 | setMessagesImageContent(path); |
| 308 | } else { |
| 309 | setMessagesFileContent(path); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void |
| 314 | MessagesAdapter::deleteInteraction(const QString &arg) |
| 315 | { |
| 316 | bool ok; |
| 317 | uint64_t interactionUid = arg.toULongLong(&ok); |
| 318 | if (ok) { |
| 319 | LRCInstance::getCurrentConversationModel() |
| 320 | ->clearInteractionFromConversation(LRCInstance::getCurrentConvUid(), interactionUid); |
| 321 | } else { |
| 322 | qDebug() << "DeleteInteraction - invalid arg" << arg; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | void |
| 327 | MessagesAdapter::openFile(const QString &arg) |
| 328 | { |
| 329 | QUrl fileUrl("file:///" + arg); |
| 330 | if (!QDesktopServices::openUrl(fileUrl)) { |
| 331 | qDebug() << "Couldn't open file: " << fileUrl; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | MessagesAdapter::acceptFile(const QString &arg) |
| 337 | { |
| 338 | try { |
| 339 | auto interactionUid = arg.toLongLong(); |
| 340 | auto convUid = LRCInstance::getCurrentConvUid(); |
| 341 | LRCInstance::getCurrentConversationModel()->acceptTransfer(convUid, interactionUid); |
| 342 | } catch (...) { |
| 343 | qDebug() << "JS bridging - exception during acceptFile: " << arg; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | void |
| 348 | MessagesAdapter::refuseFile(const QString &arg) |
| 349 | { |
| 350 | try { |
| 351 | auto interactionUid = arg.toLongLong(); |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 352 | const auto convUid = LRCInstance::getCurrentConvUid(); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 353 | LRCInstance::getCurrentConversationModel()->cancelTransfer(convUid, interactionUid); |
| 354 | } catch (...) { |
| 355 | qDebug() << "JS bridging - exception during refuseFile:" << arg; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | void |
| 360 | MessagesAdapter::pasteKeyDetected() |
| 361 | { |
| 362 | const QMimeData *mimeData = QApplication::clipboard()->mimeData(); |
| 363 | |
| 364 | if (mimeData->hasImage()) { |
| 365 | /* |
| 366 | * Save temp data into base64 format. |
| 367 | */ |
| 368 | QPixmap pixmap = qvariant_cast<QPixmap>(mimeData->imageData()); |
| 369 | QByteArray ba; |
| 370 | QBuffer bu(&ba); |
| 371 | bu.open(QIODevice::WriteOnly); |
| 372 | pixmap.save(&bu, "PNG"); |
| 373 | auto str = QString::fromLocal8Bit(ba.toBase64()); |
| 374 | |
| 375 | setMessagesImageContent(str, true); |
| 376 | } else if (mimeData->hasUrls()) { |
| 377 | QList<QUrl> urlList = mimeData->urls(); |
| 378 | /* |
| 379 | * Extract the local paths of the files. |
| 380 | */ |
| 381 | for (int i = 0; i < urlList.size(); ++i) { |
| 382 | /* |
| 383 | * Trim file:/// from url. |
| 384 | */ |
| 385 | QString filePath = urlList.at(i).toString().remove(0, 8); |
| 386 | QByteArray imageFormat = QImageReader::imageFormat(filePath); |
| 387 | |
| 388 | /* |
| 389 | * Check if file is qt supported image file type. |
| 390 | */ |
| 391 | if (!imageFormat.isEmpty()) { |
| 392 | setMessagesImageContent(filePath); |
| 393 | } else { |
| 394 | setMessagesFileContent(filePath); |
| 395 | } |
| 396 | } |
| 397 | } else { |
| 398 | QMetaObject::invokeMethod(qmlObj_, |
| 399 | "webViewRunJavaScript", |
| 400 | Q_ARG(QVariant, |
| 401 | QStringLiteral("replaceText(`%1`)").arg(mimeData->text()))); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | void |
| 406 | MessagesAdapter::onComposing(bool isComposing) |
| 407 | { |
| 408 | LRCInstance::getCurrentConversationModel()->setIsComposing(LRCInstance::getCurrentConvUid(), |
| 409 | isComposing); |
| 410 | } |
| 411 | |
| 412 | void |
| 413 | MessagesAdapter::setConversationProfileData(const lrc::api::conversation::Info &convInfo) |
| 414 | { |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 415 | auto* convModel = LRCInstance::getCurrentConversationModel(); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 416 | auto accInfo = &LRCInstance::getCurrentAccountInfo(); |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 417 | const auto conv = convModel->getConversationForUID(convInfo.uid); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 418 | |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 419 | if (conv.participants.isEmpty()) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | auto contactUri = conv.participants.front(); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 424 | if (contactUri.isEmpty()) { |
| 425 | return; |
| 426 | } |
| 427 | try { |
| 428 | auto &contact = accInfo->contactModel->getContact(contactUri); |
| 429 | auto bestName = Utils::bestNameForConversation(convInfo, *convModel); |
| 430 | setInvitation(contact.profileInfo.type == lrc::api::profile::Type::PENDING, |
| 431 | bestName, |
| 432 | contactUri); |
| 433 | |
| 434 | if (!contact.profileInfo.avatar.isEmpty()) { |
| 435 | setSenderImage(contactUri, contact.profileInfo.avatar); |
| 436 | } else { |
| 437 | auto avatar = Utils::conversationPhoto(convInfo.uid, *accInfo, true); |
| 438 | QByteArray ba; |
| 439 | QBuffer bu(&ba); |
| 440 | avatar.save(&bu, "PNG"); |
| 441 | setSenderImage(contactUri, QString::fromLocal8Bit(ba.toBase64())); |
| 442 | } |
| 443 | } catch (...) { |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | void |
| 448 | MessagesAdapter::newInteraction(const QString &accountId, |
| 449 | const QString &convUid, |
| 450 | uint64_t interactionId, |
| 451 | const interaction::Info &interaction) |
| 452 | { |
| 453 | Q_UNUSED(interactionId); |
| 454 | try { |
| 455 | auto &accountInfo = LRCInstance::getAccountInfo(accountId); |
| 456 | auto &convModel = accountInfo.conversationModel; |
ababi | 0b68664 | 2020-08-18 17:21:28 +0200 | [diff] [blame] | 457 | const auto conversation = convModel->getConversationForUID(convUid); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 458 | |
| 459 | if (conversation.uid.isEmpty()) { |
| 460 | return; |
| 461 | } |
| 462 | if (!interaction.authorUri.isEmpty() |
| 463 | && (!QApplication::focusWindow() || LRCInstance::getCurrAccId() != accountId)) { |
| 464 | /* |
| 465 | * TODO: Notification from other accounts. |
| 466 | */ |
| 467 | } |
| 468 | if (convUid != LRCInstance::getCurrentConvUid()) { |
| 469 | return; |
| 470 | } |
| 471 | convModel->clearUnreadInteractions(convUid); |
| 472 | printNewInteraction(*convModel, interactionId, interaction); |
| 473 | } catch (...) { |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | void |
| 478 | MessagesAdapter::updateDraft() |
| 479 | { |
| 480 | Utils::oneShotConnect(qmlObj_, |
| 481 | SIGNAL(sendMessageContentSaved(const QString &)), |
| 482 | this, |
| 483 | SLOT(slotUpdateDraft(const QString &))); |
| 484 | |
| 485 | requestSendMessageContent(); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * JS invoke. |
| 490 | */ |
| 491 | void |
| 492 | MessagesAdapter::setMessagesVisibility(bool visible) |
| 493 | { |
| 494 | QString s = QString::fromLatin1(visible ? "showMessagesDiv();" : "hideMessagesDiv();"); |
| 495 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 496 | } |
| 497 | |
| 498 | void |
| 499 | MessagesAdapter::requestSendMessageContent() |
| 500 | { |
| 501 | QString s = QString::fromLatin1("requestSendMessageContent();"); |
| 502 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 503 | } |
| 504 | |
| 505 | void |
| 506 | MessagesAdapter::setInvitation(bool show, const QString &contactUri, const QString &contactId) |
| 507 | { |
| 508 | QString s |
| 509 | = show |
| 510 | ? QString::fromLatin1("showInvitation(\"%1\", \"%2\")").arg(contactUri).arg(contactId) |
| 511 | : QString::fromLatin1("showInvitation()"); |
| 512 | |
| 513 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 514 | } |
| 515 | |
| 516 | void |
| 517 | MessagesAdapter::clear() |
| 518 | { |
| 519 | QString s = QString::fromLatin1("clearMessages();"); |
| 520 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 521 | } |
| 522 | |
| 523 | void |
| 524 | MessagesAdapter::printHistory(lrc::api::ConversationModel &conversationModel, |
| 525 | const std::map<uint64_t, lrc::api::interaction::Info> interactions) |
| 526 | { |
| 527 | auto interactionsStr = interactionsToJsonArrayObject(conversationModel, interactions).toUtf8(); |
| 528 | QString s = QString::fromLatin1("printHistory(%1);").arg(interactionsStr.constData()); |
| 529 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 530 | } |
| 531 | |
| 532 | void |
| 533 | MessagesAdapter::setSenderImage(const QString &sender, const QString &senderImage) |
| 534 | { |
| 535 | QJsonObject setSenderImageObject = QJsonObject(); |
| 536 | setSenderImageObject.insert("sender_contact_method", QJsonValue(sender)); |
| 537 | setSenderImageObject.insert("sender_image", QJsonValue(senderImage)); |
| 538 | |
| 539 | auto setSenderImageObjectString = QString( |
| 540 | QJsonDocument(setSenderImageObject).toJson(QJsonDocument::Compact)); |
| 541 | QString s = QString::fromLatin1("setSenderImage(%1);") |
| 542 | .arg(setSenderImageObjectString.toUtf8().constData()); |
| 543 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 544 | } |
| 545 | |
| 546 | void |
| 547 | MessagesAdapter::printNewInteraction(lrc::api::ConversationModel &conversationModel, |
| 548 | uint64_t msgId, |
| 549 | const lrc::api::interaction::Info &interaction) |
| 550 | { |
| 551 | auto interactionObject |
| 552 | = interactionToJsonInteractionObject(conversationModel, msgId, interaction).toUtf8(); |
| 553 | if (interactionObject.isEmpty()) { |
| 554 | return; |
| 555 | } |
| 556 | QString s = QString::fromLatin1("addMessage(%1);").arg(interactionObject.constData()); |
| 557 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 558 | } |
| 559 | |
| 560 | void |
| 561 | MessagesAdapter::updateInteraction(lrc::api::ConversationModel &conversationModel, |
| 562 | uint64_t msgId, |
| 563 | const lrc::api::interaction::Info &interaction) |
| 564 | { |
| 565 | auto interactionObject |
| 566 | = interactionToJsonInteractionObject(conversationModel, msgId, interaction).toUtf8(); |
| 567 | if (interactionObject.isEmpty()) { |
| 568 | return; |
| 569 | } |
| 570 | QString s = QString::fromLatin1("updateMessage(%1);").arg(interactionObject.constData()); |
| 571 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 572 | } |
| 573 | |
| 574 | void |
| 575 | MessagesAdapter::setMessagesImageContent(const QString &path, bool isBased64) |
| 576 | { |
| 577 | if (isBased64) { |
| 578 | QString param = QString("addImage_base64('file://%1')").arg(path); |
| 579 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, param)); |
| 580 | } else { |
| 581 | QString param = QString("addImage_path('file://%1')").arg(path); |
| 582 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, param)); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void |
| 587 | MessagesAdapter::setMessagesFileContent(const QString &path) |
| 588 | { |
| 589 | qint64 fileSize = QFileInfo(path).size(); |
| 590 | QString fileName = QFileInfo(path).fileName(); |
| 591 | /* |
| 592 | * If file name is too large, trim it. |
| 593 | */ |
| 594 | if (fileName.length() > 15) { |
| 595 | fileName = fileName.remove(12, fileName.length() - 12) + "..."; |
| 596 | } |
| 597 | QString param = QString("addFile_path('%1','%2','%3')") |
| 598 | .arg(path, fileName, Utils::humanFileSize(fileSize)); |
| 599 | |
| 600 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, param)); |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | MessagesAdapter::removeInteraction(uint64_t interactionId) |
| 605 | { |
| 606 | QString s = QString::fromLatin1("removeInteraction(%1);").arg(QString::number(interactionId)); |
| 607 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 608 | } |
| 609 | |
| 610 | void |
| 611 | MessagesAdapter::setSendMessageContent(const QString &content) |
| 612 | { |
| 613 | QString s = QString::fromLatin1("setSendMessageContent(`%1`);").arg(content); |
| 614 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 615 | } |
| 616 | |
| 617 | void |
| 618 | MessagesAdapter::contactIsComposing(const QString &uid, const QString &contactUri, bool isComposing) |
| 619 | { |
| 620 | if (LRCInstance::getCurrentConvUid() == uid) { |
| 621 | QString s |
| 622 | = QString::fromLatin1("showTypingIndicator(`%1`, %2);").arg(contactUri).arg(isComposing); |
| 623 | QMetaObject::invokeMethod(qmlObj_, "webViewRunJavaScript", Q_ARG(QVariant, s)); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void |
Ming Rui Zhang | 9680887 | 2020-08-27 12:59:09 -0400 | [diff] [blame] | 628 | MessagesAdapter::acceptInvitation(const QString &convUid) |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 629 | { |
Ming Rui Zhang | 9680887 | 2020-08-27 12:59:09 -0400 | [diff] [blame] | 630 | const auto currentConvUid = convUid.isEmpty() ? LRCInstance::getCurrentConvUid() : convUid; |
| 631 | LRCInstance::getCurrentConversationModel()->makePermanent(currentConvUid); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void |
Ming Rui Zhang | 9680887 | 2020-08-27 12:59:09 -0400 | [diff] [blame] | 635 | MessagesAdapter::refuseInvitation(const QString &convUid) |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 636 | { |
Ming Rui Zhang | 9680887 | 2020-08-27 12:59:09 -0400 | [diff] [blame] | 637 | const auto currentConvUid = convUid.isEmpty() ? LRCInstance::getCurrentConvUid() : convUid; |
| 638 | LRCInstance::getCurrentConversationModel()->removeConversation(currentConvUid, false); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 639 | setInvitation(false); |
| 640 | } |
| 641 | |
| 642 | void |
Ming Rui Zhang | 9680887 | 2020-08-27 12:59:09 -0400 | [diff] [blame] | 643 | MessagesAdapter::blockConversation(const QString &convUid) |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 644 | { |
Ming Rui Zhang | 9680887 | 2020-08-27 12:59:09 -0400 | [diff] [blame] | 645 | const auto currentConvUid = convUid.isEmpty() ? LRCInstance::getCurrentConvUid() : convUid; |
| 646 | LRCInstance::getCurrentConversationModel()->removeConversation(currentConvUid, true); |
Sébastien Blin | 1f91576 | 2020-08-03 13:27:42 -0400 | [diff] [blame] | 647 | setInvitation(false); |
| 648 | } |