From 76ddfb792b8e99a14b2adae23cfc221314e98f00 Mon Sep 17 00:00:00 2001 From: Konstantinos Sideris Date: Sun, 8 Oct 2017 22:38:38 +0300 Subject: [PATCH] Add /join command support --- include/ChatPage.h | 1 + include/MainWindow.h | 4 ++++ include/MatrixClient.h | 1 + include/TextInputWidget.h | 3 +++ src/ChatPage.cc | 17 +++++++++++------ src/MainWindow.cc | 11 ++++++++++- src/MatrixClient.cc | 18 ++++++++++++++---- src/TextInputWidget.cc | 16 ++++++++++++++++ src/ui/SnackBar.cc | 2 +- 9 files changed, 61 insertions(+), 12 deletions(-) diff --git a/include/ChatPage.h b/include/ChatPage.h index ad1ec9e3..04464bc5 100644 --- a/include/ChatPage.h +++ b/include/ChatPage.h @@ -54,6 +54,7 @@ signals: void close(); void changeWindowTitle(const QString &msg); void unreadMessages(int count); + void showNotification(const QString &msg); private slots: void showUnreadMessageNotification(int count); diff --git a/include/MainWindow.h b/include/MainWindow.h index a7a2b2e6..0c2316a3 100644 --- a/include/MainWindow.h +++ b/include/MainWindow.h @@ -26,6 +26,7 @@ #include "MatrixClient.h" #include "OverlayModal.h" #include "RegisterPage.h" +#include "SnackBar.h" #include "TrayIcon.h" #include "WelcomePage.h" @@ -91,4 +92,7 @@ private: // Tray icon that shows the unread message count. TrayIcon *trayIcon_; + + // Notifications display. + QSharedPointer snackBar_; }; diff --git a/include/MatrixClient.h b/include/MatrixClient.h index c87f0668..927fe3a6 100644 --- a/include/MatrixClient.h +++ b/include/MatrixClient.h @@ -93,6 +93,7 @@ signals: void initialSyncCompleted(const SyncResponse &response); void syncCompleted(const SyncResponse &response); void syncFailed(const QString &msg); + void joinFailed(const QString &msg); void messageSent(const QString &event_id, const QString &roomid, const int txn_id); void emoteSent(const QString &event_id, const QString &roomid, const int txn_id); void messagesRetrieved(const QString &room_id, const RoomMessages &msgs); diff --git a/include/TextInputWidget.h b/include/TextInputWidget.h index 732f4f61..772bdd46 100644 --- a/include/TextInputWidget.h +++ b/include/TextInputWidget.h @@ -30,6 +30,7 @@ namespace msgs = matrix::events::messages; static const QString EMOTE_COMMAND("/me "); +static const QString JOIN_COMMAND("/join "); class FilteredTextEdit : public QTextEdit { @@ -63,10 +64,12 @@ signals: void sendTextMessage(QString msg); void sendEmoteMessage(QString msg); void uploadImage(QString filename); + void sendJoinRoomRequest(const QString &room); private: void showUploadSpinner(); QString parseEmoteCommand(const QString &cmd); + QString parseJoinCommand(const QString &cmd); QHBoxLayout *topLayout_; FilteredTextEdit *input_; diff --git a/src/ChatPage.cc b/src/ChatPage.cc index 5648a830..92692fc1 100644 --- a/src/ChatPage.cc +++ b/src/ChatPage.cc @@ -166,10 +166,16 @@ ChatPage::ChatPage(QSharedPointer client, QWidget *parent) view_manager_, SLOT(sendEmoteMessage(const QString &))); + connect(text_input_, + &TextInputWidget::sendJoinRoomRequest, + client_.data(), + &MatrixClient::joinRoom); + connect(text_input_, &TextInputWidget::uploadImage, this, [=](QString filename) { client_->uploadImage(current_room_, filename); }); + connect(client_.data(), &MatrixClient::joinFailed, this, &ChatPage::showNotification); connect(client_.data(), &MatrixClient::imageUploaded, this, @@ -203,10 +209,9 @@ ChatPage::ChatPage(QSharedPointer client, QWidget *parent) SIGNAL(ownAvatarRetrieved(const QPixmap &)), this, SLOT(setOwnAvatar(const QPixmap &))); - connect(client_.data(), - SIGNAL(joinedRoom(const QString &)), - this, - SLOT(addRoom(const QString &))); + connect(client_.data(), &MatrixClient::joinedRoom, this, [=]() { + emit showNotification("You joined the room."); + }); connect(client_.data(), SIGNAL(leftRoom(const QString &)), this, @@ -636,9 +641,9 @@ ChatPage::addRoom(const QString &room_id) QSharedPointer(new RoomSettings(room_id))); room_list_->addRoom(settingsManager_[room_id], state_manager_[room_id], room_id); - - this->changeTopRoomInfo(room_id); room_list_->highlightSelectedRoom(room_id); + + changeTopRoomInfo(room_id); } } diff --git a/src/MainWindow.cc b/src/MainWindow.cc index 80726683..06f8245c 100644 --- a/src/MainWindow.cc +++ b/src/MainWindow.cc @@ -15,8 +15,8 @@ * along with this program. If not, see . */ -#include "Config.h" #include "MainWindow.h" +#include "Config.h" #include #include @@ -142,6 +142,15 @@ MainWindow::removeOverlayProgressBar() spinner_.reset(); }); + // FIXME: Snackbar doesn't work if it's initialized in the constructor. + QTimer::singleShot(100, this, [=]() { + snackBar_ = QSharedPointer(new SnackBar(this)); + connect(chat_page_, + &ChatPage::showNotification, + snackBar_.data(), + &SnackBar::showMessage); + }); + timer->start(500); } diff --git a/src/MatrixClient.cc b/src/MatrixClient.cc index 708e1176..e9e47fcb 100644 --- a/src/MatrixClient.cc +++ b/src/MatrixClient.cc @@ -477,13 +477,22 @@ MatrixClient::onJoinRoomResponse(QNetworkReply *reply) int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if (status == 0 || status >= 400) { - qWarning() << reply->errorString(); + auto data = reply->readAll(); + auto response = QJsonDocument::fromJson(data); + auto json = response.object(); + + if (json.contains("error")) + emit joinFailed(json["error"].toString()); + else + qDebug() << reply->errorString(); + return; } - auto data = reply->readAll(); - QJsonDocument response = QJsonDocument::fromJson(data); - QString room_id = response.object()["room_id"].toString(); + auto data = reply->readAll(); + auto response = QJsonDocument::fromJson(data); + auto room_id = response.object()["room_id"].toString(); + emit joinedRoom(room_id); } @@ -899,6 +908,7 @@ MatrixClient::joinRoom(const QString &roomIdOrAlias) QNetworkReply *reply = post(request, "{}"); reply->setProperty("endpoint", static_cast(Endpoint::JoinRoom)); + reply->setProperty("room", roomIdOrAlias); } void diff --git a/src/TextInputWidget.cc b/src/TextInputWidget.cc index f894a247..b90a7caa 100644 --- a/src/TextInputWidget.cc +++ b/src/TextInputWidget.cc @@ -148,6 +148,11 @@ TextInputWidget::onSendButtonClicked() if (!text.isEmpty()) emit sendEmoteMessage(text); + } else if (msgText.startsWith(JOIN_COMMAND)) { + auto room = parseJoinCommand(msgText); + + if (!room.isEmpty()) + emit sendJoinRoomRequest(room); } else { emit sendTextMessage(msgText); } @@ -155,6 +160,17 @@ TextInputWidget::onSendButtonClicked() input_->clear(); } +QString +TextInputWidget::parseJoinCommand(const QString &cmd) +{ + auto room = cmd.right(cmd.size() - JOIN_COMMAND.size()).trimmed(); + + if (!room.isEmpty()) + return room; + + return QString(""); +} + QString TextInputWidget::parseEmoteCommand(const QString &cmd) { diff --git a/src/ui/SnackBar.cc b/src/ui/SnackBar.cc index 673c2f93..39566e99 100644 --- a/src/ui/SnackBar.cc +++ b/src/ui/SnackBar.cc @@ -84,7 +84,7 @@ SnackBar::showMessage(const QString &msg) void SnackBar::onTimeout() { - offset_ -= 0.5; + offset_ -= 1.1; if (offset_ <= 0.0) { showTimer_->stop();