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.
void bootstrap(QString userid, QString homeserver, QString token);
signals:
void close();
public slots:
// Updates the user info box.
void updateOwnProfileInfo(const QUrl &avatar_url, const QString &display_name);
@ -58,6 +61,7 @@ public slots:
void sendTextMessage(const QString &msg);
void messageSent(const QString event_id, int txn_id);
void startSync();
void logout();
private:
Ui::ChatPage *ui;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -38,6 +38,11 @@ public:
void setDisplayName(const QString &name);
void setUserId(const QString &userid);
void reset();
signals:
void logout();
private:
Avatar *userAvatar_;
@ -46,7 +51,7 @@ private:
QVBoxLayout *textLayout_;
QHBoxLayout *buttonLayout_;
FlatButton *settingsButton_;
FlatButton *logoutButton_;
QLabel *displayNameLabel_;
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/share-dark.png</file>
<file>icons/user-shape.png</file>
<file>icons/power-button-off.png</file>
</qresource>
<qresource prefix="/fonts">

View file

@ -53,6 +53,9 @@ ChatPage::ChatPage(QWidget *parent)
sync_timer_ = new QTimer(this);
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_,
SIGNAL(roomChanged(const RoomInfo &)),
this,
@ -94,6 +97,29 @@ ChatPage::ChatPage(QWidget *parent)
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)
{
Q_UNUSED(event_id);

View file

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

View file

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

View file

@ -43,6 +43,15 @@ MatrixClient::MatrixClient(QString server, QObject *parent)
connect(this, SIGNAL(finished(QNetworkReply *)), this, SLOT(onResponse(QNetworkReply *)));
}
void MatrixClient::reset()
{
next_batch_ = "";
server_ = "";
token_ = "";
txn_id_ = 0;
}
void MatrixClient::onVersionsResponse(QNetworkReply *reply)
{
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)
{
reply->deleteLater();
@ -249,6 +272,9 @@ void MatrixClient::onResponse(QNetworkReply *reply)
case Endpoint::Login:
onLoginResponse(reply);
break;
case Endpoint::Logout:
onLogoutResponse(reply);
break;
case Endpoint::Register:
onRegisterResponse(reply);
break;
@ -283,6 +309,23 @@ void MatrixClient::login(const QString &username, const QString &password)
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)
{
setServer(server);

View file

@ -30,6 +30,7 @@ RoomList::RoomList(QWidget *parent)
, ui(new Ui::RoomList)
{
ui->setupUi(this);
ui->scrollVerticalLayout->addStretch(1);
}
RoomList::~RoomList()
@ -37,22 +38,30 @@ RoomList::~RoomList()
delete ui;
}
void RoomList::clear()
{
for (const auto &room : rooms_)
room->deleteLater();
rooms_.clear();
}
RoomInfo RoomList::extractRoomInfo(const State &room_state)
{
RoomInfo info;
auto events = room_state.events();
for (int i = 0; i < events.count(); i++) {
if (events[i].type() == "m.room.name") {
info.setName(events[i].content().value("name").toString());
} else if (events[i].type() == "m.room.topic") {
info.setTopic(events[i].content().value("topic").toString());
} else if (events[i].type() == "m.room.avatar") {
info.setAvatarUrl(QUrl(events[i].content().value("url").toString()));
} else if (events[i].type() == "m.room.canonical_alias") {
for (const auto &event : events) {
if (event.type() == "m.room.name") {
info.setName(event.content().value("name").toString());
} else if (event.type() == "m.room.topic") {
info.setTopic(event.content().value("topic").toString());
} else if (event.type() == "m.room.avatar") {
info.setAvatarUrl(QUrl(event.content().value("url").toString()));
} else if (event.type() == "m.room.canonical_alias") {
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)
{
available_rooms_.clear();
rooms_.clear();
for (auto it = rooms.join().constBegin(); it != rooms.join().constEnd(); it++) {
RoomInfo info = RoomList::extractRoomInfo(it.value().state());
@ -79,24 +88,23 @@ void RoomList::setInitialRooms(const Rooms &rooms)
this,
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.
auto first_room = available_rooms_.first();
auto first_room = rooms_.first();
first_room->setPressedState(true);
emit roomChanged(first_room->info());
ui->scrollVerticalLayout->addStretch(1);
}
void RoomList::highlightSelectedRoom(const RoomInfo &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())
it.value()->setPressedState(false);
}
@ -104,16 +112,11 @@ void RoomList::highlightSelectedRoom(const RoomInfo &info)
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;
return;
}
auto list_item = available_rooms_.value(roomid);
auto list_item = rooms_.value(roomid);
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_);
}
void TopRoomBar::reset()
{
name_label_->setText("");
topic_label_->setText("");
avatar_->setLetter(QChar('?'));
}
void TopRoomBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

View file

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