From db3894bcb5e25db00c05374b48de4282c4c2f7c4 Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Wed, 24 Oct 2018 16:46:26 -0400
Subject: [PATCH] call: use new lrc models for in-call chat

Change-Id: I1980b6bb8a226d7170c26f9eff55d4541532835f
Reviewed-by: Isa Nanic <isa.nanic@savoirfairelinux.com>
---
 callwidget.cpp             |  32 +++++-------
 callwidget.h               |   1 -
 instantmessagingwidget.cpp | 104 +++++++++++++++++++------------------
 instantmessagingwidget.h   |  12 +++--
 lrcinstance.h              |   1 +
 5 files changed, 76 insertions(+), 74 deletions(-)

diff --git a/callwidget.cpp b/callwidget.cpp
index 0a5aa21..8fe1e3f 100644
--- a/callwidget.cpp
+++ b/callwidget.cpp
@@ -586,8 +586,8 @@ void CallWidget::slotShowIncomingCallView(const std::string& accountId,
     }
 
     ui->videoWidget->pushRenderer(convInfo.callId);
-    // TODO:(new lrc) in call chat
-    //ui->instantMessagingWidget->setMediaText(actualCall_);
+
+    ui->instantMessagingWidget->setupCallMessaging(convInfo.callId, messageModel_.get());
 
     disconnect(selectedCallChanged_);
     selectedCallChanged_ = connect(
@@ -733,7 +733,9 @@ void
 CallWidget::on_sendIMButton_clicked()
 {
     auto msg = ui->imMessageEdit->text();
-    if (msg.trimmed().isEmpty()) return;
+    if (msg.trimmed().isEmpty()) {
+        return;
+    }
     ui->imMessageEdit->clear();
     try {
         LRCInstance::getCurrentConversationModel()->sendMessage(selectedConvUid(), msg.toStdString());
@@ -843,37 +845,32 @@ CallWidget::connectConversationModel()
             updateConversationsFilterWidget();
             selectSmartlistItem(selectedConvUid());
             ui->smartList->update();
-        }
-    );
+        });
     modelUpdatedConnection_ = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::conversationUpdated,
         [this](const std::string& convUid) {
             Q_UNUSED(convUid);
             ui->smartList->update();
-        }
-    );
+        });
     filterChangedConnection_ = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::filterChanged,
         [this]() {
             updateSmartList();
             updateConversationsFilterWidget();
             ui->smartList->update();
-        }
-    );
+        });
     newConversationConnection_ = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::newConversation,
         [this](const std::string& convUid) {
             updateSmartList();
             updateConversationForNewContact(convUid);
             ui->conversationsFilterWidget->update();
-        }
-    );
+        });
     conversationRemovedConnection_ = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::conversationRemoved,
         [this]() {
             backToWelcomePage();
-        }
-    );
+        });
     conversationClearedConnection = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::conversationCleared,
         [this](const std::string& convUid) {
@@ -884,8 +881,7 @@ CallWidget::connectConversationModel()
                 return;
             }
             backToWelcomePage();
-        }
-    );
+        });
     interactionStatusUpdatedConnection_ = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::interactionStatusUpdated,
         [this](const std::string& convUid) {
@@ -893,14 +889,12 @@ CallWidget::connectConversationModel()
                 return;
             }
             updateConversationView(convUid);
-        }
-    );
+        });
     newInteractionConnection_ = QObject::connect(
         currentConversationModel, &lrc::api::ConversationModel::newInteraction,
         [this](const std::string& convUid, uint64_t interactionId, const lrc::api::interaction::Info& interaction) {
             onIncomingMessage(convUid, interactionId, interaction);
-        }
-    );
+        });
     currentConversationModel->setFilter("");
     // clear search field
     ui->ringContactLineEdit->setText("");
diff --git a/callwidget.h b/callwidget.h
index 7a40389..2395b10 100644
--- a/callwidget.h
+++ b/callwidget.h
@@ -112,7 +112,6 @@ private:
     void setupSmartListContextMenu(const QPoint &pos);
     void setupQRCode(QString ringID);
     void backToWelcomePage();
-    void triggerDeleteContactDialog(ContactMethod *cm, Account *ac);
 
     // lrc
     void selectConversation(const QModelIndex& index);
diff --git a/instantmessagingwidget.cpp b/instantmessagingwidget.cpp
index 9473950..add65b8 100644
--- a/instantmessagingwidget.cpp
+++ b/instantmessagingwidget.cpp
@@ -1,6 +1,7 @@
 /***************************************************************************
- * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
+ * Copyright (C) 2015-2018 by Savoir-faire Linux                           *
  * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
+ * Author: Andreas Traczyk <andreas.traczyk@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    *
@@ -29,6 +30,8 @@
 #include "imdelegate.h"
 #include "globalsystemtray.h"
 #include "settingskey.h"
+#include "lrcinstance.h"
+#include "utils.h"
 
 InstantMessagingWidget::InstantMessagingWidget(QWidget *parent) :
     QWidget(parent),
@@ -55,57 +58,41 @@ InstantMessagingWidget::~InstantMessagingWidget()
 }
 
 void
-InstantMessagingWidget::setMediaText(Call *call)
+InstantMessagingWidget::setupCallMessaging(const std::string& callId,
+                                           MessageModel *messageModel)
 {
     ui->listMessageView->disconnect();
     ui->messageEdit->disconnect();
-    if (call != nullptr) {
-        connect(call, SIGNAL(mediaAdded(media::Media*)),
-                this, SLOT(mediaAdd(media::Media*)));
-        media::Text *textMedia = nullptr;
-        if (call->hasMedia(media::Media::Type::TEXT, media::Media::Direction::OUT)) {
-            textMedia = call->firstMedia<media::Text>(media::Media::Direction::OUT);
-        } else {
-            textMedia = call->addOutgoingMedia<media::Text>();
-        }
-        if (textMedia) {
-            ui->listMessageView->setModel(textMedia->recording()->instantMessagingModel());
-            ui->listMessageView->scrollToBottom();
-            connect(ui->messageEdit, &QLineEdit::returnPressed, [=]() {
-                if (not ui->messageEdit->text().trimmed().isEmpty()) {
-                    QMap<QString, QString> messages;
-                    messages["text/plain"] = ui->messageEdit->text();
-                    textMedia->send(messages);
-                    ui->messageEdit->clear();
-                    ui->listMessageView->scrollToBottom();
-                }
-            });
-        }
+    if (messageModel == nullptr) {
+        return;
     }
-}
 
-void
-InstantMessagingWidget::mediaAdd(media::Media *media)
-{
-    switch(media->type()) {
-    case media::Media::Type::AUDIO:
-        break;
-    case media::Media::Type::VIDEO:
-        break;
-    case media::Media::Type::TEXT:
-        if (media->direction() == media::Text::Direction::IN) {
-            connect(static_cast<media::Text*>(media),
-                    SIGNAL(messageReceived(QMap<QString,QString>)),
-                    this,
-                    SLOT(onMsgReceived(QMap<QString,QString>)));
-            this->show();
-        }
-        break;
-    case media::Media::Type::FILE:
-        break;
-    default:
-        break;
-    }
+    using namespace lrc::api;
+
+    ui->listMessageView->setModel(messageModel);
+    ui->listMessageView->scrollToBottom();
+    connect(ui->messageEdit, &QLineEdit::returnPressed,
+        [=]() {
+            auto msg = ui->messageEdit->text();
+            if (msg.trimmed().isEmpty()) {
+                return;
+            }
+            ui->messageEdit->clear();
+            try {
+                auto selectedConvUid = LRCInstance::getSelectedConvUid();
+                LRCInstance::getCurrentConversationModel()->sendMessage(selectedConvUid, msg.toStdString());
+            }
+            catch (...) {
+                qDebug() << "exception when sending message";
+            }
+            ui->listMessageView->scrollToBottom();
+        });
+
+    QObject::disconnect(newInteractionConnection_);
+    newInteractionConnection_ = QObject::connect(LRCInstance::getCurrentConversationModel(), &ConversationModel::newInteraction,
+        [this](const std::string& convUid, uint64_t interactionId, const lrc::api::interaction::Info& interaction) {
+            onIncomingMessage(convUid, interactionId, interaction);
+        });
 }
 
 void
@@ -132,6 +119,22 @@ InstantMessagingWidget::copyToClipboard()
 
         QApplication::clipboard()->setText(text.value<QString>());
     }
+}
+
+void
+InstantMessagingWidget::updateConversationView(const std::string & convUid)
+{
+    auto& currentAccountInfo = LRCInstance::getCurrentAccountInfo();
+    auto currentConversationModel = currentAccountInfo.conversationModel.get();
+    currentConversationModel->clearUnreadInteractions(convUid);
+    auto currentConversation = Utils::getConversationFromUid(convUid, *currentConversationModel);
+    if (currentConversation == currentConversationModel->allFilteredConversations().end()) {
+        return;
+    }
+    messageModel_.reset(new MessageModel(*currentConversation, currentAccountInfo, this->parent()));
+    ui->listMessageView->setModel(messageModel_.get());
+    ui->listMessageView->scrollToBottom();
+    this->show();
 }
 
 void
@@ -141,12 +144,11 @@ InstantMessagingWidget::on_sendButton_clicked()
 }
 
 void
-InstantMessagingWidget::onMsgReceived(const QMap<QString,QString>& message)
+InstantMessagingWidget::onIncomingMessage(const std::string& convUid, uint64_t interactionId, const lrc::api::interaction::Info& interaction)
 {
     if (!QApplication::activeWindow() && settings_.value(SettingsKey::enableNotifications).toBool()) {
-        GlobalSystemTray::instance().showMessage("Ring: Message Received", message["text/plain"]);
+        GlobalSystemTray::instance().showMessage("Ring: Message Received", QString::fromStdString(interaction.body));
         QApplication::alert(this, 5000);
     }
-    ui->listMessageView->scrollToBottom();
-    this->show();
+    updateConversationView(convUid);
 }
diff --git a/instantmessagingwidget.h b/instantmessagingwidget.h
index e67f7d7..ed4a79a 100644
--- a/instantmessagingwidget.h
+++ b/instantmessagingwidget.h
@@ -26,6 +26,7 @@
 #include "media/media.h"
 
 #include "imdelegate.h"
+#include "messagemodel.h"
 
 namespace Ui {
 class InstantMessagingWidget;
@@ -38,7 +39,8 @@ class InstantMessagingWidget final : public QWidget
 public:
     explicit InstantMessagingWidget(QWidget *parent = 0);
     ~InstantMessagingWidget();
-    void setMediaText(Call* call);
+    void setupCallMessaging(const std::string& callId,
+                            MessageModel *messageModel);
 
 protected:
     virtual void keyPressEvent(QKeyEvent *event) override;
@@ -49,13 +51,17 @@ private slots:
     void on_sendButton_clicked();
 
 private slots:
-    void mediaAdd(media::Media *media);
-    void onMsgReceived(const QMap<QString, QString>& message);
+    void onIncomingMessage(const std::string& convUid, uint64_t interactionId, const lrc::api::interaction::Info& interaction);
 
 private:
     Ui::InstantMessagingWidget *ui;
     ImDelegate* imDelegate_;
+    std::unique_ptr<MessageModel> messageModel_;
     QSettings settings_;
+    QMetaObject::Connection newInteractionConnection_;
+
     void copyToClipboard();
+    void updateConversationView(const std::string& convUid);
+
 };
 
diff --git a/lrcinstance.h b/lrcinstance.h
index e92dd34..517eaae 100644
--- a/lrcinstance.h
+++ b/lrcinstance.h
@@ -32,6 +32,7 @@
 #include "api/contactmodel.h"
 #include "api/contact.h"
 #include "api/datatransfermodel.h"
+#include "api/conversationmodel.h"
 
 #include <settingskey.h>
 
-- 
GitLab