diff --git a/shared/message.cpp b/shared/message.cpp index e6b47b2..f3f6b45 100644 --- a/shared/message.cpp +++ b/shared/message.cpp @@ -406,7 +406,7 @@ bool Shared::Message::change(const QMap& data) if (!edited || lastModified < correctionDate) { originalMessage = body; lastModified = correctionDate; - setBody(body); + setBody(b); setEdited(true); } } diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index 2d5f99f..588fb1d 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -549,6 +549,8 @@ void Models::Roster::changeMessage(const QString& account, const QString& jid, c Element* el = getElement({account, jid}); if (el != NULL) { el->changeMessage(id, data); + } else { + qDebug() << "A request to change a message of the contact " << jid << " in the account " << account << " but it wasn't found"; } } diff --git a/ui/widgets/conversation.cpp b/ui/widgets/conversation.cpp index 07c599e..608faf3 100644 --- a/ui/widgets/conversation.cpp +++ b/ui/widgets/conversation.cpp @@ -110,7 +110,7 @@ Conversation::Conversation(bool muc, Models::Account* acc, Models::Element* el, initializeOverlay(); - m_ui->currentActionBadge->setVisible(false);; + m_ui->currentActionBadge->setVisible(false); // m_ui->currentActionBadge->setText(tr("Editing message...")); } @@ -476,10 +476,10 @@ void Conversation::onFeedContext(const QPoint& pos) Shared::Message* item = static_cast(index.internalPointer()); contextMenu->clear(); + QString id = item->getId(); bool showMenu = false; if (item->getState() == Shared::Message::State::error) { showMenu = true; - QString id = item->getId(); QAction* resend = contextMenu->addAction(Shared::icon("view-refresh"), tr("Try sending again")); connect(resend, &QAction::triggered, [this, id]() { element->feed->registerUpload(id); @@ -500,6 +500,12 @@ void Conversation::onFeedContext(const QPoint& pos) Shared::Global::highlightInFileManager(path); }); } + + if (item->getOutgoing()) { + showMenu = true; + QAction* edit = contextMenu->addAction(Shared::icon("edit-rename"), tr("Edit")); + connect(edit, &QAction::triggered, this, std::bind(&Conversation::onMessageEditRequested, this, id)); + } if (showMenu) { contextMenu->popup(feed->viewport()->mapToGlobal(pos)); @@ -517,3 +523,23 @@ void Conversation::onMessageEditorContext(const QPoint& pos) editorMenu->exec(this->m_ui->messageEditor->mapToGlobal(pos)); } + +void Conversation::onMessageEditRequested(const QString& id) +{ + if (currentAction == CurrentAction::edit) { + //todo; + } + + try { + Shared::Message msg = element->feed->getMessage(id); + + m_ui->currentActionBadge->setVisible(true); + m_ui->currentActionBadge->setText(tr("Editing message...")); + currentAction = CurrentAction::edit; + m_ui->messageEditor->setText(msg.getBody()); + + } catch (const Models::MessageFeed::NotFound& e) { + qDebug() << "The message requested to be edited was not found" << e.getMessage().c_str(); + qDebug() << "Ignoring"; + } +} diff --git a/ui/widgets/conversation.h b/ui/widgets/conversation.h index c43d7a0..4bccdfc 100644 --- a/ui/widgets/conversation.h +++ b/ui/widgets/conversation.h @@ -116,6 +116,7 @@ protected slots: void positionShadow(); void onFeedContext(const QPoint &pos); void onMessageEditorContext(const QPoint &pos); + void onMessageEditRequested(const QString& id); public: const bool isMuc; diff --git a/ui/widgets/messageline/messagefeed.cpp b/ui/widgets/messageline/messagefeed.cpp index 4803dce..33fbdd4 100644 --- a/ui/widgets/messageline/messagefeed.cpp +++ b/ui/widgets/messageline/messagefeed.cpp @@ -224,8 +224,20 @@ std::set Models::MessageFeed::detectChanges(c void Models::MessageFeed::removeMessage(const QString& id) { + //todo; } +Shared::Message Models::MessageFeed::getMessage(const QString& id) +{ + StorageById::iterator itr = indexById.find(id); + if (itr == indexById.end()) { + throw NotFound(id.toStdString(), rosterItem->getJid().toStdString(), rosterItem->getAccountName().toStdString()); + } + + return **itr; +} + + QVariant Models::MessageFeed::data(const QModelIndex& index, int role) const { int i = index.row(); diff --git a/ui/widgets/messageline/messagefeed.h b/ui/widgets/messageline/messagefeed.h index 2273b15..c9701ae 100644 --- a/ui/widgets/messageline/messagefeed.h +++ b/ui/widgets/messageline/messagefeed.h @@ -32,6 +32,7 @@ #include #include +#include namespace Models { @@ -55,6 +56,7 @@ public: void addMessage(const Shared::Message& msg); void changeMessage(const QString& id, const QMap& data); void removeMessage(const QString& id); + Shared::Message getMessage(const QString& id); QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex& parent = QModelIndex()) const override; @@ -103,6 +105,21 @@ public: Error, Bulk }; + + class NotFound: + public Utils::Exception + { + public: + NotFound(const std::string& k, const std::string& j, const std::string& acc):Exception(), key(k), jid(j), account(acc){} + + std::string getMessage() const { + return "Message with id " + key + " wasn't found in messageFeed " + account + " of the chat with " + jid; + } + private: + std::string key; + std::string jid; + std::string account; + }; protected: bool sentByMe(const Shared::Message& msg) const;