Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Add context menu option to show the raw message
Browse files Browse the repository at this point in the history
fixes #437
  • Loading branch information
mujx committed Sep 12, 2018
1 parent bc38fce commit e9ee299
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ qt5_wrap_cpp(MOC_HEADERS
src/dialogs/LeaveRoom.h
src/dialogs/Logout.h
src/dialogs/UserProfile.h
src/dialogs/RawMessage.h
src/dialogs/ReadReceipts.h
src/dialogs/ReCaptcha.h
src/dialogs/RoomSettings.h
Expand Down
1 change: 1 addition & 0 deletions src/MatrixClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ init()
qRegisterMetaType<mtx::responses::JoinedGroups>();
qRegisterMetaType<mtx::responses::GroupProfile>();
qRegisterMetaType<std::string>();
qRegisterMetaType<nlohmann::json>();
qRegisterMetaType<std::vector<std::string>>();
qRegisterMetaType<std::vector<QString>>();
qRegisterMetaType<std::map<QString, bool>>("std::map<QString, bool>");
Expand Down
2 changes: 2 additions & 0 deletions src/MatrixClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <QObject>
#include <QString>

#include "json.hpp"
#include <mtx/responses.hpp>
#include <mtxclient/http/client.hpp>

Expand All @@ -15,6 +16,7 @@ Q_DECLARE_METATYPE(mtx::responses::Sync)
Q_DECLARE_METATYPE(mtx::responses::JoinedGroups)
Q_DECLARE_METATYPE(mtx::responses::GroupProfile)
Q_DECLARE_METATYPE(std::string)
Q_DECLARE_METATYPE(nlohmann::json)
Q_DECLARE_METATYPE(std::vector<std::string>)
Q_DECLARE_METATYPE(std::vector<QString>)

Expand Down
56 changes: 56 additions & 0 deletions src/dialogs/RawMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once

#include <QFont>
#include <QFontDatabase>
#include <QTextBrowser>
#include <QVBoxLayout>
#include <QWidget>

#include "json.hpp"

#include "Logging.h"
#include "MainWindow.h"
#include "ui/FlatButton.h"

namespace dialogs {

class RawMessage : public QWidget
{
Q_OBJECT
public:
RawMessage(QString msg, QWidget *parent = nullptr)
: QWidget{parent}
{
QFont monospaceFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);

auto layout = new QVBoxLayout{this};
auto viewer = new QTextBrowser{this};
viewer->setFont(monospaceFont);
viewer->setText(msg);

layout->setSpacing(0);
layout->setMargin(0);
layout->addWidget(viewer);

setAutoFillBackground(true);
setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint);
setAttribute(Qt::WA_DeleteOnClose, true);

QSize winsize;
QPoint center;

auto window = MainWindow::instance();
if (window) {
winsize = window->frameGeometry().size();
center = window->frameGeometry().center();

move(center.x() - (width() * 0.5), center.y() - (height() * 0.5));
} else {
nhlog::ui()->warn("unable to retrieve MainWindow's size");
}

raise();
show();
}
};
} // namespace dialogs
5 changes: 2 additions & 3 deletions src/dialogs/RoomSettings.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <QApplication>
#include <QComboBox>
#include <QFileDialog>
#include <QFontDatabase>
#include <QImageReader>
#include <QLabel>
#include <QMessageBox>
Expand Down Expand Up @@ -190,9 +191,7 @@ RoomSettings::RoomSettings(const QString &room_id, QWidget *parent)
auto infoLabel = new QLabel(tr("Info").toUpper(), this);
infoLabel->setFont(font);

QFont monospaceFont;
monospaceFont.setFamily("Courier New");
monospaceFont.setStyleHint(QFont::Courier);
QFont monospaceFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);

auto roomIdLabel = new QLabel(room_id, this);
roomIdLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
Expand Down
41 changes: 40 additions & 1 deletion src/timeline/TimelineItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "timeline/widgets/ImageItem.h"
#include "timeline/widgets/VideoItem.h"

#include "dialogs/RawMessage.h"

constexpr int MSG_RIGHT_MARGIN = 7;
constexpr int MSG_PADDING = 20;

Expand Down Expand Up @@ -185,8 +187,10 @@ TimelineItem::init()
contextMenu_ = new QMenu(this);
showReadReceipts_ = new QAction("Read receipts", this);
markAsRead_ = new QAction("Mark as read", this);
viewRawMessage_ = new QAction("View raw message", this);
redactMsg_ = new QAction("Redact message", this);
contextMenu_->addAction(showReadReceipts_);
contextMenu_->addAction(viewRawMessage_);
contextMenu_->addAction(markAsRead_);
contextMenu_->addAction(redactMsg_);

Expand Down Expand Up @@ -218,7 +222,8 @@ TimelineItem::init()
});
});

connect(markAsRead_, &QAction::triggered, this, [this]() { sendReadReceipt(); });
connect(markAsRead_, &QAction::triggered, this, &TimelineItem::sendReadReceipt);
connect(viewRawMessage_, &QAction::triggered, this, &TimelineItem::openRawMessageViewer);

topLayout_ = new QHBoxLayout(this);
mainLayout_ = new QVBoxLayout;
Expand Down Expand Up @@ -816,3 +821,37 @@ TimelineItem::sendReadReceipt() const
}
});
}

void
TimelineItem::openRawMessageViewer() const
{
const auto event_id = event_id_.toStdString();
const auto room_id = room_id_.toStdString();

auto proxy = std::make_shared<EventProxy>();
connect(proxy.get(), &EventProxy::eventRetrieved, this, [](const nlohmann::json &obj) {
auto dialog = new dialogs::RawMessage{QString::fromStdString(obj.dump(4))};
Q_UNUSED(dialog);
});

http::client()->get_event(
room_id,
event_id,
[event_id, room_id, proxy = std::move(proxy)](
const mtx::events::collections::TimelineEvents &res, mtx::http::RequestErr err) {
using namespace mtx::events;

if (err) {
nhlog::net()->warn(
"failed to retrieve event {} from {}", event_id, room_id);
return;
}

try {
emit proxy->eventRetrieved(utils::serialize_event(res));
} catch (const nlohmann::json::exception &e) {
nhlog::net()->warn(
"failed to serialize event ({}, {})", room_id, event_id);
}
});
}
10 changes: 10 additions & 0 deletions src/timeline/TimelineItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class StatusIndicator : public QWidget
static constexpr int MaxWidth = 24;
};

class EventProxy : public QObject
{
Q_OBJECT

signals:
void eventRetrieved(const nlohmann::json &);
};

class TextLabel : public QTextBrowser
{
Q_OBJECT
Expand Down Expand Up @@ -220,6 +228,7 @@ class TimelineItem : public QWidget
bool isReceived() { return isReceived_; };
void setRoomId(QString room_id) { room_id_ = room_id; }
void sendReadReceipt() const;
void openRawMessageViewer() const;

//! Add a user avatar for this event.
void addAvatar();
Expand Down Expand Up @@ -273,6 +282,7 @@ class TimelineItem : public QWidget
QAction *showReadReceipts_;
QAction *markAsRead_;
QAction *redactMsg_;
QAction *viewRawMessage_;
QAction *replyMsg_;

QHBoxLayout *topLayout_ = nullptr;
Expand Down

0 comments on commit e9ee299

Please sign in to comment.