Add dialog to create rooms

fixes #25
This commit is contained in:
Konstantinos Sideris 2017-12-11 23:00:37 +02:00
parent 3c5241ccd0
commit 544b623512
11 changed files with 286 additions and 1 deletions

View file

@ -141,6 +141,7 @@ endif()
# #
set(SRC_FILES set(SRC_FILES
# Dialogs # Dialogs
src/dialogs/CreateRoom.cc
src/dialogs/ImageOverlay.cc src/dialogs/ImageOverlay.cc
src/dialogs/InviteUsers.cc src/dialogs/InviteUsers.cc
src/dialogs/JoinRoom.cc src/dialogs/JoinRoom.cc
@ -219,6 +220,7 @@ include_directories(${LMDB_INCLUDE_DIR})
qt5_wrap_cpp(MOC_HEADERS qt5_wrap_cpp(MOC_HEADERS
# Dialogs # Dialogs
include/dialogs/CreateRoom.h
include/dialogs/ImageOverlay.h include/dialogs/ImageOverlay.h
include/dialogs/InviteUsers.h include/dialogs/InviteUsers.h
include/dialogs/JoinRoom.h include/dialogs/JoinRoom.h

View file

@ -61,6 +61,7 @@ public:
void removeTypingNotification(const QString &roomid); void removeTypingNotification(const QString &roomid);
void readEvent(const QString &room_id, const QString &event_id); void readEvent(const QString &room_id, const QString &event_id);
void inviteUser(const QString &room_id, const QString &user); void inviteUser(const QString &room_id, const QString &user);
void createRoom(const mtx::requests::CreateRoom &request);
QUrl getHomeServer() { return server_; }; QUrl getHomeServer() { return server_; };
int transactionId() { return txn_id_; }; int transactionId() { return txn_id_; };
@ -86,6 +87,7 @@ signals:
void loggedOut(); void loggedOut();
void invitedUser(const QString &room_id, const QString &user); void invitedUser(const QString &room_id, const QString &user);
void roomCreated(const QString &room_id);
void loginSuccess(const QString &userid, const QString &homeserver, const QString &token); void loginSuccess(const QString &userid, const QString &homeserver, const QString &token);
void registerSuccess(const QString &userid, void registerSuccess(const QString &userid,
@ -115,6 +117,7 @@ signals:
void messagesRetrieved(const QString &room_id, const mtx::responses::Messages &msgs); void messagesRetrieved(const QString &room_id, const mtx::responses::Messages &msgs);
void joinedRoom(const QString &room_id); void joinedRoom(const QString &room_id);
void leftRoom(const QString &room_id); void leftRoom(const QString &room_id);
void roomCreationFailed(const QString &msg);
private: private:
QNetworkReply *makeUploadRequest(const QString &filename); QNetworkReply *makeUploadRequest(const QString &filename);

View file

@ -8,6 +8,7 @@
#include "FlatButton.h" #include "FlatButton.h"
#include "Menu.h" #include "Menu.h"
#include "dialogs/CreateRoom.h"
#include "dialogs/JoinRoom.h" #include "dialogs/JoinRoom.h"
class OverlayModal; class OverlayModal;
@ -23,6 +24,7 @@ public:
signals: signals:
void showSettings(); void showSettings();
void joinRoom(const QString &room); void joinRoom(const QString &room);
void createRoom(const mtx::requests::CreateRoom &request);
protected: protected:
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
@ -37,6 +39,9 @@ private:
QSharedPointer<OverlayModal> joinRoomModal_; QSharedPointer<OverlayModal> joinRoomModal_;
QSharedPointer<dialogs::JoinRoom> joinRoomDialog_; QSharedPointer<dialogs::JoinRoom> joinRoomDialog_;
QSharedPointer<OverlayModal> createRoomModal_;
QSharedPointer<dialogs::CreateRoom> createRoomDialog_;
FlatButton *settingsBtn_; FlatButton *settingsBtn_;
FlatButton *createRoomBtn_; FlatButton *createRoomBtn_;
FlatButton *joinRoomBtn_; FlatButton *joinRoomBtn_;

View file

@ -0,0 +1,44 @@
#pragma once
#include <QFrame>
#include <mtx.hpp>
class FlatButton;
class TextField;
class QComboBox;
class Toggle;
namespace dialogs {
class CreateRoom : public QFrame
{
Q_OBJECT
public:
CreateRoom(QWidget *parent = nullptr);
signals:
void closing(bool isCreating, const mtx::requests::CreateRoom &request);
protected:
void paintEvent(QPaintEvent *event) override;
private:
void clearFields();
QComboBox *visibilityCombo_;
QComboBox *presetCombo_;
Toggle *directToggle_;
FlatButton *confirmBtn_;
FlatButton *cancelBtn_;
TextField *nameInput_;
TextField *topicInput_;
TextField *aliasInput_;
mtx::requests::CreateRoom request_;
};
} // dialogs

@ -1 +1 @@
Subproject commit 9946ed125e5cc3b2fb425648679ada615c862be3 Subproject commit d03a370ffd1bbdd5623afbe9817d1b929bc76cd7

View file

@ -79,6 +79,7 @@ Avatar {
dialogs--Logout, dialogs--Logout,
dialogs--LeaveRoom, dialogs--LeaveRoom,
dialogs--CreateRoom,
dialogs--InviteUsers, dialogs--InviteUsers,
dialogs--JoinRoom { dialogs--JoinRoom {
background-color: #383c4a; background-color: #383c4a;

View file

@ -81,6 +81,7 @@ Avatar {
dialogs--Logout, dialogs--Logout,
dialogs--LeaveRoom, dialogs--LeaveRoom,
dialogs--CreateRoom,
dialogs--InviteUsers, dialogs--InviteUsers,
dialogs--JoinRoom { dialogs--JoinRoom {
background-color: white; background-color: white;

View file

@ -70,6 +70,8 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
sidebarActions_, &SideBarActions::showSettings, this, &ChatPage::showUserSettingsPage); sidebarActions_, &SideBarActions::showSettings, this, &ChatPage::showUserSettingsPage);
connect( connect(
sidebarActions_, &SideBarActions::joinRoom, client_.data(), &MatrixClient::joinRoom); sidebarActions_, &SideBarActions::joinRoom, client_.data(), &MatrixClient::joinRoom);
connect(
sidebarActions_, &SideBarActions::createRoom, client_.data(), &MatrixClient::createRoom);
user_info_widget_ = new UserInfoWidget(sideBar_); user_info_widget_ = new UserInfoWidget(sideBar_);
room_list_ = new RoomList(client, sideBar_); room_list_ = new RoomList(client, sideBar_);
@ -202,6 +204,8 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
client_->uploadAudio(current_room_, filename); client_->uploadAudio(current_room_, filename);
}); });
connect(
client_.data(), &MatrixClient::roomCreationFailed, this, &ChatPage::showNotification);
connect(client_.data(), &MatrixClient::joinFailed, this, &ChatPage::showNotification); connect(client_.data(), &MatrixClient::joinFailed, this, &ChatPage::showNotification);
connect(client_.data(), connect(client_.data(),
&MatrixClient::imageUploaded, &MatrixClient::imageUploaded,
@ -268,6 +272,9 @@ ChatPage::ChatPage(QSharedPointer<MatrixClient> client, QWidget *parent)
connect(client_.data(), &MatrixClient::invitedUser, this, [=](QString, QString user) { connect(client_.data(), &MatrixClient::invitedUser, this, [=](QString, QString user) {
emit showNotification(QString("Invited user %1").arg(user)); emit showNotification(QString("Invited user %1").arg(user));
}); });
connect(client_.data(), &MatrixClient::roomCreated, this, [=](QString room_id) {
emit showNotification(QString("Room %1 created").arg(room_id));
});
connect(client_.data(), &MatrixClient::leftRoom, this, &ChatPage::removeRoom); connect(client_.data(), &MatrixClient::leftRoom, this, &ChatPage::removeRoom);
showContentTimer_ = new QTimer(this); showContentTimer_ = new QTimer(this);

View file

@ -890,6 +890,49 @@ MatrixClient::inviteUser(const QString &roomId, const QString &user)
emit invitedUser(roomId, user); emit invitedUser(roomId, user);
}); });
} }
void
MatrixClient::createRoom(const mtx::requests::CreateRoom &create_room_request)
{
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_);
endpoint.setPath(clientApiUrl_ + QString("/createRoom"));
endpoint.setQuery(query);
QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
nlohmann::json body = create_room_request;
auto reply = post(request, QString::fromStdString(body.dump()).toUtf8());
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status == 0 || status >= 400) {
auto data = reply->readAll();
auto response = QJsonDocument::fromJson(data);
auto json = response.object();
if (json.contains("error"))
emit roomCreationFailed(json["error"].toString());
else
qDebug() << reply->errorString();
return;
}
auto data = reply->readAll();
auto response = QJsonDocument::fromJson(data);
auto room_id = response.object()["room_id"].toString();
emit roomCreated(room_id);
});
}
void void
MatrixClient::sendTypingNotification(const QString &roomid, int timeoutInMillis) MatrixClient::sendTypingNotification(const QString &roomid, int timeoutInMillis)
{ {

View file

@ -1,3 +1,4 @@
#include <QDebug>
#include <QIcon> #include <QIcon>
#include "Config.h" #include "Config.h"
@ -59,6 +60,32 @@ SideBarActions::SideBarActions(QWidget *parent)
joinRoomModal_->fadeIn(); joinRoomModal_->fadeIn();
}); });
connect(createRoomAction_, &QAction::triggered, this, [=]() {
if (createRoomDialog_.isNull()) {
createRoomDialog_ =
QSharedPointer<dialogs::CreateRoom>(new dialogs::CreateRoom(this));
connect(createRoomDialog_.data(),
&dialogs::CreateRoom::closing,
this,
[=](bool isCreating, const mtx::requests::CreateRoom &request) {
createRoomModal_->fadeOut();
if (isCreating)
emit createRoom(request);
});
}
if (createRoomModal_.isNull()) {
createRoomModal_ = QSharedPointer<OverlayModal>(
new OverlayModal(MainWindow::instance(), createRoomDialog_.data()));
createRoomModal_->setDuration(0);
createRoomModal_->setColor(QColor(30, 30, 30, 170));
}
createRoomModal_->fadeIn();
});
addMenu_->addAction(createRoomAction_); addMenu_->addAction(createRoomAction_);
addMenu_->addAction(joinRoomAction_); addMenu_->addAction(joinRoomAction_);

152
src/dialogs/CreateRoom.cc Normal file
View file

@ -0,0 +1,152 @@
#include <QComboBox>
#include <QLabel>
#include <QStyleOption>
#include <QVBoxLayout>
#include "Config.h"
#include "FlatButton.h"
#include "TextField.h"
#include "Theme.h"
#include "ToggleButton.h"
#include "dialogs/CreateRoom.h"
using namespace dialogs;
CreateRoom::CreateRoom(QWidget *parent)
: QFrame(parent)
{
setMaximumSize(520, 600);
auto layout = new QVBoxLayout(this);
layout->setSpacing(30);
layout->setMargin(20);
auto buttonLayout = new QHBoxLayout();
buttonLayout->setSpacing(0);
buttonLayout->setMargin(0);
confirmBtn_ = new FlatButton("CREATE", this);
confirmBtn_->setFontSize(conf::btn::fontSize);
cancelBtn_ = new FlatButton(tr("CANCEL"), this);
cancelBtn_->setFontSize(conf::btn::fontSize);
buttonLayout->addStretch(1);
buttonLayout->addWidget(confirmBtn_);
buttonLayout->addWidget(cancelBtn_);
QFont font;
font.setPixelSize(conf::headerFontSize);
nameInput_ = new TextField(this);
nameInput_->setLabel(tr("Name"));
topicInput_ = new TextField(this);
topicInput_->setLabel(tr("Topic"));
aliasInput_ = new TextField(this);
aliasInput_->setLabel(tr("Alias"));
auto visibilityLayout = new QHBoxLayout;
visibilityLayout->setContentsMargins(0, 10, 0, 10);
auto presetLayout = new QHBoxLayout;
presetLayout->setContentsMargins(0, 10, 0, 10);
auto visibilityLabel = new QLabel(tr("Room visibility"), this);
visibilityCombo_ = new QComboBox(this);
visibilityCombo_->addItem("Private");
visibilityCombo_->addItem("Public");
visibilityLayout->addWidget(visibilityLabel);
visibilityLayout->addWidget(visibilityCombo_, 0, Qt::AlignBottom | Qt::AlignRight);
auto presetLabel = new QLabel(tr("Room preset"), this);
presetCombo_ = new QComboBox(this);
presetCombo_->addItem("Private Chat");
presetCombo_->addItem("Public Chat");
presetCombo_->addItem("Trusted Private Chat");
presetLayout->addWidget(presetLabel);
presetLayout->addWidget(presetCombo_, 0, Qt::AlignBottom | Qt::AlignRight);
auto directLabel_ = new QLabel(tr("Direct Chat"), this);
directToggle_ = new Toggle(this);
directToggle_->setActiveColor(QColor("#38A3D8"));
directToggle_->setInactiveColor(QColor("gray"));
directToggle_->setState(true);
directLabel_->setStyleSheet("font-size: 15px;");
auto directLayout = new QHBoxLayout;
directLayout->setContentsMargins(0, 10, 0, 10);
directLayout->addWidget(directLabel_);
directLayout->addWidget(directToggle_, 0, Qt::AlignBottom | Qt::AlignRight);
layout->addWidget(nameInput_);
layout->addWidget(topicInput_);
layout->addWidget(aliasInput_);
layout->addLayout(visibilityLayout);
layout->addLayout(presetLayout);
layout->addLayout(directLayout);
layout->addLayout(buttonLayout);
connect(confirmBtn_, &QPushButton::clicked, this, [=]() {
request_.name = nameInput_->text().toStdString();
request_.topic = topicInput_->text().toStdString();
request_.room_alias_name = aliasInput_->text().toStdString();
emit closing(true, request_);
clearFields();
});
connect(cancelBtn_, &QPushButton::clicked, this, [=]() {
emit closing(false, request_);
clearFields();
});
connect(visibilityCombo_,
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[=](const QString &text) {
if (text == "Private") {
request_.visibility = mtx::requests::Visibility::Private;
} else {
request_.visibility = mtx::requests::Visibility::Public;
}
});
connect(presetCombo_,
static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::activated),
[=](const QString &text) {
if (text == "Private Chat") {
request_.preset = mtx::requests::Preset::PrivateChat;
} else if (text == "Public Chat") {
request_.preset = mtx::requests::Preset::PublicChat;
} else {
request_.preset = mtx::requests::Preset::TrustedPrivateChat;
}
});
connect(directToggle_, &Toggle::toggled, this, [=](bool isDisabled) {
request_.is_direct = !isDisabled;
});
}
void
CreateRoom::clearFields()
{
nameInput_->clear();
topicInput_->clear();
aliasInput_->clear();
}
void
CreateRoom::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}