Add logout button

Logout from the current session and invalidate the current token
This commit is contained in:
Konstantinos Sideris 2017-04-09 02:17:04 +03:00
parent f50fb34fb6
commit 239780557f
17 changed files with 166 additions and 41 deletions

View file

@ -48,6 +48,9 @@ public:
// Initialize all the components of the UI. // Initialize all the components of the UI.
void bootstrap(QString userid, QString homeserver, QString token); void bootstrap(QString userid, QString homeserver, QString token);
signals:
void close();
public slots: public slots:
// Updates the user info box. // Updates the user info box.
void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name); void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name);
@ -58,6 +61,7 @@ public slots:
void sendTextMessage(const QString &msg); void sendTextMessage(const QString &msg);
void messageSent(const QString event_id, int txn_id); void messageSent(const QString event_id, int txn_id);
void startSync(); void startSync();
void logout();
private: private:
Ui::ChatPage *ui; Ui::ChatPage *ui;

View file

@ -38,6 +38,7 @@ public:
void addHistoryItem(const Event &event, const QString &color, bool with_sender); void addHistoryItem(const Event &event, const QString &color, bool with_sender);
void addEvents(const QList<Event> &events); void addEvents(const QList<Event> &events);
void clear();
public slots: public slots:
void sliderRangeChanged(int min, int max); void sliderRangeChanged(int min, int max);

View file

@ -36,6 +36,7 @@ public:
void initialize(const Rooms &rooms); void initialize(const Rooms &rooms);
void sync(const Rooms &rooms); void sync(const Rooms &rooms);
void clearAll();
public slots: public slots:
void setHistoryView(const RoomInfo &info); void setHistoryView(const RoomInfo &info);

View file

@ -44,9 +44,11 @@ public:
inline QString getHomeServer(); inline QString getHomeServer();
inline void incrementTransactionId(); inline void incrementTransactionId();
void reset();
public slots: public slots:
// Profile
void getOwnProfile(); void getOwnProfile();
void logout();
inline void setServer(const QString &server); inline void setServer(const QString &server);
inline void setAccessToken(const QString &token); inline void setAccessToken(const QString &token);
@ -56,6 +58,8 @@ signals:
void loginError(const QString &error); void loginError(const QString &error);
void registerError(const QString &error); void registerError(const QString &error);
void loggedOut();
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, const QString &homeserver, const QString &token); void registerSuccess(const QString &userid, const QString &homeserver, const QString &token);
@ -74,6 +78,7 @@ private:
GetProfile, GetProfile,
InitialSync, InitialSync,
Login, Login,
Logout,
Register, Register,
SendTextMessage, SendTextMessage,
Sync, Sync,
@ -82,6 +87,7 @@ private:
// Response handlers. // Response handlers.
void onLoginResponse(QNetworkReply *reply); void onLoginResponse(QNetworkReply *reply);
void onLogoutResponse(QNetworkReply *reply);
void onRegisterResponse(QNetworkReply *reply); void onRegisterResponse(QNetworkReply *reply);
void onVersionsResponse(QNetworkReply *reply); void onVersionsResponse(QNetworkReply *reply);
void onGetOwnProfileResponse(QNetworkReply *reply); void onGetOwnProfileResponse(QNetworkReply *reply);

View file

@ -39,9 +39,10 @@ public:
explicit RoomList(QWidget *parent = 0); explicit RoomList(QWidget *parent = 0);
~RoomList(); ~RoomList();
void appendRoom(QString name);
void setInitialRooms(const Rooms &rooms); void setInitialRooms(const Rooms &rooms);
void updateRoomAvatar(const QString &roomid, const QImage &avatar_image); void updateRoomAvatar(const QString &roomid, const QImage &avatar_image);
void clear();
RoomInfo extractRoomInfo(const State &room_state); RoomInfo extractRoomInfo(const State &room_state);
signals: signals:
@ -54,7 +55,7 @@ public slots:
private: private:
Ui::RoomList *ui; Ui::RoomList *ui;
QMap<QString, RoomInfoListItem *> available_rooms_; QMap<QString, RoomInfoListItem *> rooms_;
}; };
#endif // ROOMLIST_H #endif // ROOMLIST_H

View file

@ -40,6 +40,8 @@ public:
inline void updateRoomName(const QString &name); inline void updateRoomName(const QString &name);
inline void updateRoomTopic(const QString &topic); inline void updateRoomTopic(const QString &topic);
void reset();
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;

View file

@ -38,6 +38,11 @@ public:
void setDisplayName(const QString &name); void setDisplayName(const QString &name);
void setUserId(const QString &userid); void setUserId(const QString &userid);
void reset();
signals:
void logout();
private: private:
Avatar *userAvatar_; Avatar *userAvatar_;
@ -46,7 +51,7 @@ private:
QVBoxLayout *textLayout_; QVBoxLayout *textLayout_;
QHBoxLayout *buttonLayout_; QHBoxLayout *buttonLayout_;
FlatButton *settingsButton_; FlatButton *logoutButton_;
QLabel *displayNameLabel_; QLabel *displayNameLabel_;
QLabel *userIdLabel_; QLabel *userIdLabel_;

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 B

View file

@ -10,6 +10,7 @@
<file>icons/clip-dark.png</file> <file>icons/clip-dark.png</file>
<file>icons/share-dark.png</file> <file>icons/share-dark.png</file>
<file>icons/user-shape.png</file> <file>icons/user-shape.png</file>
<file>icons/power-button-off.png</file>
</qresource> </qresource>
<qresource prefix="/fonts"> <qresource prefix="/fonts">

View file

@ -53,6 +53,9 @@ ChatPage::ChatPage(QWidget *parent)
sync_timer_ = new QTimer(this); sync_timer_ = new QTimer(this);
connect(sync_timer_, SIGNAL(timeout()), this, SLOT(startSync())); connect(sync_timer_, SIGNAL(timeout()), this, SLOT(startSync()));
connect(user_info_widget_, SIGNAL(logout()), matrix_client_, SLOT(logout()));
connect(matrix_client_, SIGNAL(loggedOut()), this, SLOT(logout()));
connect(room_list_, connect(room_list_,
SIGNAL(roomChanged(const RoomInfo &)), SIGNAL(roomChanged(const RoomInfo &)),
this, this,
@ -94,6 +97,29 @@ ChatPage::ChatPage(QWidget *parent)
SLOT(messageSent(QString, int))); SLOT(messageSent(QString, int)));
} }
void ChatPage::logout()
{
sync_timer_->stop();
QSettings settings;
settings.remove("auth/access_token");
settings.remove("auth/home_server");
settings.remove("auth/user_id");
settings.remove("client/transaction_id");
// Clear the environment.
room_list_->clear();
view_manager_->clearAll();
top_bar_->reset();
user_info_widget_->reset();
matrix_client_->reset();
room_avatars_.clear();
emit close();
}
void ChatPage::messageSent(QString event_id, int txn_id) void ChatPage::messageSent(QString event_id, int txn_id)
{ {
Q_UNUSED(event_id); Q_UNUSED(event_id);

View file

@ -63,6 +63,14 @@ HistoryView::HistoryView(QWidget *parent)
init(); init();
} }
void HistoryView::clear()
{
nick_colors_.clear();
for (const auto msg : scroll_layout_->children())
msg->deleteLater();
}
void HistoryView::sliderRangeChanged(int min, int max) void HistoryView::sliderRangeChanged(int min, int max)
{ {
Q_UNUSED(min); Q_UNUSED(min);
@ -80,9 +88,7 @@ QString HistoryView::chooseRandomColor()
void HistoryView::addEvents(const QList<Event> &events) void HistoryView::addEvents(const QList<Event> &events)
{ {
for (int i = 0; i < events.size(); i++) { for (const auto &event : events) {
auto event = events[i];
if (event.type() == "m.room.message") { if (event.type() == "m.room.message") {
auto msg_type = event.content().value("msgtype").toString(); auto msg_type = event.content().value("msgtype").toString();
@ -97,8 +103,6 @@ void HistoryView::addEvents(const QList<Event> &events)
addHistoryItem(event, color, with_sender); addHistoryItem(event, color, with_sender);
last_sender_ = event.sender(); last_sender_ = event.sender();
} else {
qDebug() << "Ignoring message" << msg_type;
} }
} }
} }

View file

@ -37,6 +37,17 @@ HistoryViewManager::~HistoryViewManager()
{ {
} }
void HistoryViewManager::clearAll()
{
for (const auto &view: views_) {
view->clear();
removeWidget(view);
view->deleteLater();
}
views_.clear();
}
void HistoryViewManager::initialize(const Rooms &rooms) void HistoryViewManager::initialize(const Rooms &rooms)
{ {
for (auto it = rooms.join().constBegin(); it != rooms.join().constEnd(); it++) { for (auto it = rooms.join().constBegin(); it != rooms.join().constEnd(); it++) {

View file

@ -57,6 +57,8 @@ MainWindow::MainWindow(QWidget *parent)
this, this,
SLOT(matrixRegister(const QString &, const QString &, const QString &))); SLOT(matrixRegister(const QString &, const QString &, const QString &)));
connect(chat_page_, SIGNAL(close()), this, SLOT(showWelcomePage()));
connect(matrix_client_, connect(matrix_client_,
SIGNAL(registerError(const QString &)), SIGNAL(registerError(const QString &)),
register_page_, register_page_,

View file

@ -43,6 +43,15 @@ MatrixClient::MatrixClient(QString server, QObject *parent)
connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(onResponse(QNetworkReply *))); connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(onResponse(QNetworkReply *)));
} }
void MatrixClient::reset()
{
next_batch_ = "";
server_ = "";
token_ = "";
txn_id_ = 0;
}
void MatrixClient::onVersionsResponse(QNetworkReply *reply) void MatrixClient::onVersionsResponse(QNetworkReply *reply)
{ {
reply->deleteLater(); reply->deleteLater();
@ -93,6 +102,20 @@ void MatrixClient::onLoginResponse(QNetworkReply *reply)
} }
} }
void MatrixClient::onLogoutResponse(QNetworkReply *reply)
{
reply->deleteLater();
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (status != 200) {
qWarning() << "Logout error: " << reply->errorString();
return;
}
emit loggedOut();
}
void MatrixClient::onRegisterResponse(QNetworkReply *reply) void MatrixClient::onRegisterResponse(QNetworkReply *reply)
{ {
reply->deleteLater(); reply->deleteLater();
@ -249,6 +272,9 @@ void MatrixClient::onResponse(QNetworkReply *reply)
case Endpoint::Login: case Endpoint::Login:
onLoginResponse(reply); onLoginResponse(reply);
break; break;
case Endpoint::Logout:
onLogoutResponse(reply);
break;
case Endpoint::Register: case Endpoint::Register:
onRegisterResponse(reply); onRegisterResponse(reply);
break; break;
@ -283,6 +309,23 @@ void MatrixClient::login(const QString &username, const QString &password)
reply->setProperty("endpoint", Endpoint::Login); reply->setProperty("endpoint", Endpoint::Login);
} }
void MatrixClient::logout()
{
QUrlQuery query;
query.addQueryItem("access_token", token_);
QUrl endpoint(server_);
endpoint.setPath(api_url_ + "/logout");
endpoint.setQuery(query);
QNetworkRequest request(endpoint);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QJsonObject body{};
QNetworkReply *reply = post(request, QJsonDocument(body).toJson(QJsonDocument::Compact));
reply->setProperty("endpoint", Endpoint::Logout);
}
void MatrixClient::registerUser(const QString &user, const QString &pass, const QString &server) void MatrixClient::registerUser(const QString &user, const QString &pass, const QString &server)
{ {
setServer(server); setServer(server);

View file

@ -30,6 +30,7 @@ RoomList::RoomList(QWidget *parent)
, ui(new Ui::RoomList) , ui(new Ui::RoomList)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->scrollVerticalLayout->addStretch(1);
} }
RoomList::~RoomList() RoomList::~RoomList()
@ -37,22 +38,30 @@ RoomList::~RoomList()
delete ui; delete ui;
} }
void RoomList::clear()
{
for (const auto &room : rooms_)
room->deleteLater();
rooms_.clear();
}
RoomInfo RoomList::extractRoomInfo(const State &room_state) RoomInfo RoomList::extractRoomInfo(const State &room_state)
{ {
RoomInfo info; RoomInfo info;
auto events = room_state.events(); auto events = room_state.events();
for (int i = 0; i < events.count(); i++) { for (const auto &event : events) {
if (events[i].type() == "m.room.name") { if (event.type() == "m.room.name") {
info.setName(events[i].content().value("name").toString()); info.setName(event.content().value("name").toString());
} else if (events[i].type() == "m.room.topic") { } else if (event.type() == "m.room.topic") {
info.setTopic(events[i].content().value("topic").toString()); info.setTopic(event.content().value("topic").toString());
} else if (events[i].type() == "m.room.avatar") { } else if (event.type() == "m.room.avatar") {
info.setAvatarUrl(QUrl(events[i].content().value("url").toString())); info.setAvatarUrl(QUrl(event.content().value("url").toString()));
} else if (events[i].type() == "m.room.canonical_alias") { } else if (event.type() == "m.room.canonical_alias") {
if (info.name().isEmpty()) if (info.name().isEmpty())
info.setName(events[i].content().value("alias").toString()); info.setName(event.content().value("alias").toString());
} }
} }
@ -61,7 +70,7 @@ RoomInfo RoomList::extractRoomInfo(const State &room_state)
void RoomList::setInitialRooms(const Rooms &rooms) void RoomList::setInitialRooms(const Rooms &rooms)
{ {
available_rooms_.clear(); rooms_.clear();
for (auto it = rooms.join().constBegin(); it != rooms.join().constEnd(); it++) { for (auto it = rooms.join().constBegin(); it != rooms.join().constEnd(); it++) {
RoomInfo info = RoomList::extractRoomInfo(it.value().state()); RoomInfo info = RoomList::extractRoomInfo(it.value().state());
@ -79,24 +88,23 @@ void RoomList::setInitialRooms(const Rooms &rooms)
this, this,
SLOT(highlightSelectedRoom(const RoomInfo &))); SLOT(highlightSelectedRoom(const RoomInfo &)));
available_rooms_.insert(it.key(), room_item); rooms_.insert(it.key(), room_item);
ui->scrollVerticalLayout->addWidget(room_item); int pos = ui->scrollVerticalLayout->count() - 1;
ui->scrollVerticalLayout->insertWidget(pos, room_item);
} }
// TODO: Move this into its own function. // TODO: Move this into its own function.
auto first_room = available_rooms_.first(); auto first_room = rooms_.first();
first_room->setPressedState(true); first_room->setPressedState(true);
emit roomChanged(first_room->info()); emit roomChanged(first_room->info());
ui->scrollVerticalLayout->addStretch(1);
} }
void RoomList::highlightSelectedRoom(const RoomInfo &info) void RoomList::highlightSelectedRoom(const RoomInfo &info)
{ {
emit roomChanged(info); emit roomChanged(info);
for (auto it = available_rooms_.constBegin(); it != available_rooms_.constEnd(); it++) { for (auto it = rooms_.constBegin(); it != rooms_.constEnd(); it++) {
if (it.key() != info.id()) if (it.key() != info.id())
it.value()->setPressedState(false); it.value()->setPressedState(false);
} }
@ -104,16 +112,11 @@ void RoomList::highlightSelectedRoom(const RoomInfo &info)
void RoomList::updateRoomAvatar(const QString &roomid, const QImage &avatar_image) void RoomList::updateRoomAvatar(const QString &roomid, const QImage &avatar_image)
{ {
if (!available_rooms_.contains(roomid)) { if (!rooms_.contains(roomid)) {
qDebug() << "Avatar update on non existent room" << roomid; qDebug() << "Avatar update on non existent room" << roomid;
return; return;
} }
auto list_item = available_rooms_.value(roomid); auto list_item = rooms_.value(roomid);
list_item->setAvatar(avatar_image); list_item->setAvatar(avatar_image);
} }
void RoomList::appendRoom(QString name)
{
Q_UNUSED(name);
}

View file

@ -77,6 +77,13 @@ TopRoomBar::TopRoomBar(QWidget *parent)
setLayout(top_layout_); setLayout(top_layout_);
} }
void TopRoomBar::reset()
{
name_label_->setText("");
topic_label_->setText("");
avatar_->setLetter(QChar('?'));
}
void TopRoomBar::paintEvent(QPaintEvent *event) void TopRoomBar::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event); Q_UNUSED(event);

View file

@ -24,7 +24,6 @@ UserInfoWidget::UserInfoWidget(QWidget *parent)
, userid_("@user:homeserver.org") , userid_("@user:homeserver.org")
{ {
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed); QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
setSizePolicy(sizePolicy); setSizePolicy(sizePolicy);
setMinimumSize(QSize(0, 65)); setMinimumSize(QSize(0, 65));
@ -65,26 +64,35 @@ UserInfoWidget::UserInfoWidget(QWidget *parent)
buttonLayout_ = new QHBoxLayout(); buttonLayout_ = new QHBoxLayout();
settingsButton_ = new FlatButton(this); logoutButton_ = new FlatButton(this);
settingsButton_->setForegroundColor(QColor("#ebebeb")); logoutButton_->setForegroundColor(QColor("#ebebeb"));
settingsButton_->setCursor(QCursor(Qt::PointingHandCursor)); logoutButton_->setCursor(QCursor(Qt::PointingHandCursor));
settingsButton_->setStyleSheet("width: 30px; height: 30px;"); logoutButton_->setStyleSheet("width: 30px; height: 30px;");
QIcon icon; QIcon icon;
icon.addFile(":/icons/icons/user-shape.png", QSize(), QIcon::Normal, QIcon::Off); icon.addFile(":/icons/icons/power-button-off.png", QSize(), QIcon::Normal, QIcon::Off);
settingsButton_->setIcon(icon); logoutButton_->setIcon(icon);
settingsButton_->setIconSize(QSize(16, 16)); logoutButton_->setIconSize(QSize(16, 16));
buttonLayout_->addWidget(settingsButton_); buttonLayout_->addWidget(logoutButton_);
topLayout_->addLayout(buttonLayout_); topLayout_->addLayout(buttonLayout_);
connect(logoutButton_, SIGNAL(clicked()), this, SIGNAL(logout()));
} }
UserInfoWidget::~UserInfoWidget() UserInfoWidget::~UserInfoWidget()
{ {
} }
void UserInfoWidget::reset()
{
displayNameLabel_->setText("");
userIdLabel_->setText("");
userAvatar_->setLetter(QChar('?'));
}
void UserInfoWidget::setAvatar(const QImage &img) void UserInfoWidget::setAvatar(const QImage &img)
{ {
avatar_image_ = img; avatar_image_ = img;