matrixion/src/MainWindow.cpp

524 lines
16 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-04-06 02:06:42 +03:00
2017-09-24 17:39:06 +03:00
#include <QApplication>
2017-04-06 02:06:42 +03:00
#include <QLayout>
#include <QMessageBox>
2019-01-26 21:11:30 +03:00
#include <QPluginLoader>
2017-04-06 02:06:42 +03:00
#include <QSettings>
#include <QShortcut>
2017-10-28 15:46:39 +03:00
#include <mtx/requests.hpp>
2020-10-27 19:45:28 +03:00
#include <mtx/responses/login.hpp>
#include "Cache.h"
2020-12-17 00:10:09 +03:00
#include "Cache_p.h"
2017-10-28 15:46:39 +03:00
#include "ChatPage.h"
#include "Config.h"
2018-07-17 16:37:25 +03:00
#include "Logging.h"
2017-10-28 15:46:39 +03:00
#include "LoginPage.h"
#include "MainWindow.h"
#include "MatrixClient.h"
#include "RegisterPage.h"
#include "TrayIcon.h"
2017-11-02 01:41:13 +03:00
#include "UserSettingsPage.h"
2018-09-21 11:30:02 +03:00
#include "Utils.h"
#include "WebRTCSession.h"
2017-10-28 15:46:39 +03:00
#include "WelcomePage.h"
2018-07-17 16:37:25 +03:00
#include "ui/LoadingIndicator.h"
#include "ui/OverlayModal.h"
#include "ui/SnackBar.h"
#include "dialogs/CreateRoom.h"
#include "dialogs/InviteUsers.h"
#include "dialogs/JoinRoom.h"
#include "dialogs/LeaveRoom.h"
#include "dialogs/Logout.h"
2018-07-17 16:37:25 +03:00
#include "dialogs/MemberList.h"
2018-08-11 13:50:56 +03:00
#include "dialogs/ReadReceipts.h"
2017-04-06 02:06:42 +03:00
MainWindow *MainWindow::instance_ = nullptr;
MainWindow::MainWindow(QWidget *parent)
2020-12-25 21:20:15 +03:00
: QMainWindow(parent)
, userSettings_{UserSettings::instance()}
2017-04-06 02:06:42 +03:00
{
2021-02-02 02:57:59 +03:00
instance_ = this;
2020-10-22 02:20:02 +03:00
setWindowTitle(0);
2017-09-10 12:58:00 +03:00
setObjectName("MainWindow");
2017-05-14 21:10:03 +03:00
2018-08-11 13:50:56 +03:00
modal_ = new OverlayModal(this);
2017-09-10 12:58:00 +03:00
restoreWindowSize();
2017-05-14 21:10:03 +03:00
2018-09-30 13:24:36 +03:00
QFont font;
2017-09-10 12:58:00 +03:00
font.setStyleStrategy(QFont::PreferAntialias);
setFont(font);
2017-05-14 21:10:03 +03:00
trayIcon_ = new TrayIcon(":/logos/nheko.svg", this);
2017-11-02 01:41:13 +03:00
welcome_page_ = new WelcomePage(this);
login_page_ = new LoginPage(this);
register_page_ = new RegisterPage(this);
chat_page_ = new ChatPage(userSettings_, this);
2017-11-02 01:41:13 +03:00
userSettingsPage_ = new UserSettingsPage(userSettings_, this);
2017-04-06 02:06:42 +03:00
2017-09-10 12:58:00 +03:00
// Initialize sliding widget manager.
2017-09-30 17:05:05 +03:00
pageStack_ = new QStackedWidget(this);
pageStack_->addWidget(welcome_page_);
pageStack_->addWidget(login_page_);
pageStack_->addWidget(register_page_);
pageStack_->addWidget(chat_page_);
2017-11-02 01:41:13 +03:00
pageStack_->addWidget(userSettingsPage_);
2017-04-06 02:06:42 +03:00
2017-09-30 17:05:05 +03:00
setCentralWidget(pageStack_);
2017-04-06 02:06:42 +03:00
2017-09-10 12:58:00 +03:00
connect(welcome_page_, SIGNAL(userLogin()), this, SLOT(showLoginPage()));
connect(welcome_page_, SIGNAL(userRegister()), this, SLOT(showRegisterPage()));
2017-04-06 02:06:42 +03:00
2017-09-10 12:58:00 +03:00
connect(login_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));
connect(login_page_, &LoginPage::loggingIn, this, &MainWindow::showOverlayProgressBar);
connect(
register_page_, &RegisterPage::registering, this, &MainWindow::showOverlayProgressBar);
connect(
login_page_, &LoginPage::errorOccurred, this, [this]() { removeOverlayProgressBar(); });
connect(register_page_, &RegisterPage::errorOccurred, this, [this]() {
removeOverlayProgressBar();
});
2017-09-10 12:58:00 +03:00
connect(register_page_, SIGNAL(backButtonClicked()), this, SLOT(showWelcomePage()));
2017-04-06 02:06:42 +03:00
2018-04-24 23:57:49 +03:00
connect(chat_page_, &ChatPage::closing, this, &MainWindow::showWelcomePage);
connect(
chat_page_, &ChatPage::showOverlayProgressBar, this, &MainWindow::showOverlayProgressBar);
2020-10-27 19:45:28 +03:00
connect(chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle);
2017-09-10 12:58:00 +03:00
connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int)));
connect(chat_page_, &ChatPage::showLoginPage, this, [this](const QString &msg) {
login_page_->showError(msg);
2017-10-20 22:32:48 +03:00
showLoginPage();
});
2017-05-21 16:36:06 +03:00
connect(userSettingsPage_, &UserSettingsPage::moveBack, this, [this]() {
2017-11-02 01:41:13 +03:00
pageStack_->setCurrentWidget(chat_page_);
});
2017-11-02 23:02:31 +03:00
connect(
userSettingsPage_, SIGNAL(trayOptionChanged(bool)), trayIcon_, SLOT(setVisible(bool)));
connect(
userSettingsPage_, &UserSettingsPage::themeChanged, chat_page_, &ChatPage::themeChanged);
2017-09-10 12:58:00 +03:00
connect(trayIcon_,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
2017-09-10 12:58:00 +03:00
connect(chat_page_, SIGNAL(contentLoaded()), this, SLOT(removeOverlayProgressBar()));
connect(this, &MainWindow::focusChanged, chat_page_, &ChatPage::chatFocusChanged);
2017-11-02 01:41:13 +03:00
connect(
chat_page_, &ChatPage::showUserSettingsPage, this, &MainWindow::showUserSettingsPage);
connect(login_page_, &LoginPage::loginOk, this, [this](const mtx::responses::Login &res) {
http::client()->set_user(res.user_id);
showChatPage();
2018-05-02 15:30:08 +03:00
});
connect(register_page_, &RegisterPage::registerOk, this, &MainWindow::showChatPage);
QShortcut *quitShortcut = new QShortcut(QKeySequence::Quit, this);
connect(quitShortcut, &QShortcut::activated, this, QApplication::quit);
2020-05-26 23:53:21 +03:00
trayIcon_->setVisible(userSettings_->tray());
2017-09-10 12:58:00 +03:00
if (hasActiveUser()) {
QString token = userSettings_->accessToken();
QString home_server = userSettings_->homeserver();
QString user_id = userSettings_->userId();
QString device_id = userSettings_->deviceId();
http::client()->set_access_token(token.toStdString());
http::client()->set_server(home_server.toStdString());
http::client()->set_device_id(device_id.toStdString());
try {
using namespace mtx::identifiers;
http::client()->set_user(parse<User>(user_id.toStdString()));
2020-12-25 03:08:06 +03:00
} catch (const std::invalid_argument &) {
nhlog::ui()->critical("bootstrapped with invalid user_id: {}",
user_id.toStdString());
}
showChatPage();
2017-09-10 12:58:00 +03:00
}
if (loadJdenticonPlugin()) {
nhlog::ui()->info("loaded jdenticon.");
}
2017-04-06 02:06:42 +03:00
}
2020-10-22 02:20:02 +03:00
void
MainWindow::setWindowTitle(int notificationCount)
{
QString name = "nheko";
if (!userSettings_.data()->profile().isEmpty())
name += " | " + userSettings_.data()->profile();
2020-10-27 19:45:28 +03:00
if (notificationCount > 0) {
2020-10-22 02:20:02 +03:00
name.append(QString{" (%1)"}.arg(notificationCount));
}
QMainWindow::setWindowTitle(name);
}
bool
MainWindow::event(QEvent *event)
{
auto type = event->type();
if (type == QEvent::WindowActivate) {
emit focusChanged(true);
} else if (type == QEvent::WindowDeactivate) {
emit focusChanged(false);
}
return QMainWindow::event(event);
}
2017-08-20 13:47:22 +03:00
void
MainWindow::restoreWindowSize()
{
2017-09-10 12:58:00 +03:00
QSettings settings;
int savedWidth = settings.value("window/width").toInt();
int savedheight = settings.value("window/height").toInt();
if (savedWidth == 0 || savedheight == 0)
resize(conf::window::width, conf::window::height);
else
resize(savedWidth, savedheight);
}
2017-08-20 13:47:22 +03:00
void
MainWindow::saveCurrentWindowSize()
{
2017-09-10 12:58:00 +03:00
QSettings settings;
QSize current = size();
2017-09-10 12:58:00 +03:00
settings.setValue("window/width", current.width());
settings.setValue("window/height", current.height());
}
2017-08-20 13:47:22 +03:00
void
MainWindow::removeOverlayProgressBar()
{
2017-09-10 12:58:00 +03:00
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [this, timer]() {
2017-09-10 12:58:00 +03:00
timer->deleteLater();
2018-08-11 13:50:56 +03:00
if (modal_)
modal_->hide();
2018-08-11 13:50:56 +03:00
if (spinner_)
2017-10-07 20:09:34 +03:00
spinner_->stop();
2017-09-10 12:58:00 +03:00
});
2017-10-08 22:38:38 +03:00
// FIXME: Snackbar doesn't work if it's initialized in the constructor.
QTimer::singleShot(0, this, [this]() {
2018-08-11 13:50:56 +03:00
snackBar_ = new SnackBar(this);
connect(chat_page_, &ChatPage::showNotification, snackBar_, &SnackBar::showMessage);
2017-10-08 22:38:38 +03:00
});
2018-08-11 13:50:56 +03:00
timer->start(50);
}
2017-08-20 13:47:22 +03:00
void
MainWindow::showChatPage()
2017-04-06 02:06:42 +03:00
{
auto userid = QString::fromStdString(http::client()->user_id().to_string());
auto device_id = QString::fromStdString(http::client()->device_id());
auto homeserver = QString::fromStdString(http::client()->server() + ":" +
std::to_string(http::client()->port()));
auto token = QString::fromStdString(http::client()->access_token());
userSettings_.data()->setUserId(userid);
userSettings_.data()->setAccessToken(token);
userSettings_.data()->setDeviceId(device_id);
userSettings_.data()->setHomeserver(homeserver);
2017-09-10 12:58:00 +03:00
showOverlayProgressBar();
2017-09-30 17:05:05 +03:00
pageStack_->setCurrentWidget(chat_page_);
2017-09-10 12:58:00 +03:00
pageStack_->removeWidget(welcome_page_);
pageStack_->removeWidget(login_page_);
pageStack_->removeWidget(register_page_);
2017-09-10 12:58:00 +03:00
login_page_->reset();
chat_page_->bootstrap(userid, homeserver, token);
2020-12-17 00:10:09 +03:00
connect(cache::client(),
&Cache::secretChanged,
userSettingsPage_,
&UserSettingsPage::updateSecretStatus);
2021-05-15 00:35:34 +03:00
emit reload();
2017-04-06 02:06:42 +03:00
}
2017-08-20 13:47:22 +03:00
void
MainWindow::closeEvent(QCloseEvent *event)
2017-05-21 16:36:06 +03:00
{
2020-09-22 19:07:36 +03:00
if (WebRTCSession::instance().state() != webrtc::State::DISCONNECTED) {
if (QMessageBox::question(this, "nheko", "A call is in progress. Quit?") !=
QMessageBox::Yes) {
event->ignore();
return;
}
}
if (!qApp->isSavingSession() && isVisible() && pageSupportsTray() &&
2020-05-26 23:53:21 +03:00
userSettings_->tray()) {
2017-09-10 12:58:00 +03:00
event->ignore();
hide();
}
2017-05-21 16:36:06 +03:00
}
2017-08-20 13:47:22 +03:00
void
MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
2017-05-21 16:36:06 +03:00
{
2017-09-10 12:58:00 +03:00
switch (reason) {
case QSystemTrayIcon::Trigger:
if (!isVisible()) {
show();
} else {
hide();
}
break;
default:
break;
}
2017-05-21 16:36:06 +03:00
}
2017-08-20 13:47:22 +03:00
bool
MainWindow::hasActiveUser()
{
2017-09-10 12:58:00 +03:00
QSettings settings;
QString prefix;
if (userSettings_->profile() != "")
prefix = "profile/" + userSettings_->profile() + "/";
return settings.contains(prefix + "auth/access_token") &&
settings.contains(prefix + "auth/home_server") &&
settings.contains(prefix + "auth/user_id");
}
2018-05-01 19:35:28 +03:00
void
MainWindow::openMemberListDialog(const QString &room_id)
{
auto dialog = new dialogs::MemberList(room_id, this);
2018-05-01 19:35:28 +03:00
2018-09-21 11:30:02 +03:00
showDialog(dialog);
2018-05-01 19:35:28 +03:00
}
void
MainWindow::openLeaveRoomDialog(const QString &room_id)
{
2018-08-11 13:50:56 +03:00
auto dialog = new dialogs::LeaveRoom(this);
connect(dialog, &dialogs::LeaveRoom::leaving, this, [this, room_id]() {
chat_page_->leaveRoom(room_id);
2018-08-11 13:50:56 +03:00
});
2018-09-21 11:30:02 +03:00
showDialog(dialog);
}
void
MainWindow::showOverlayProgressBar()
{
2018-08-11 13:50:56 +03:00
spinner_ = new LoadingIndicator(this);
spinner_->setFixedHeight(100);
spinner_->setFixedWidth(100);
spinner_->setObjectName("ChatPageLoadSpinner");
spinner_->start();
2018-08-11 13:50:56 +03:00
showSolidOverlayModal(spinner_);
}
void
MainWindow::openInviteUsersDialog(std::function<void(const QStringList &invitees)> callback)
{
2018-08-11 13:50:56 +03:00
auto dialog = new dialogs::InviteUsers(this);
2018-09-19 22:42:26 +03:00
connect(dialog, &dialogs::InviteUsers::sendInvites, this, [callback](QStringList invitees) {
if (!invitees.isEmpty())
callback(invitees);
});
2018-09-21 11:30:02 +03:00
showDialog(dialog);
}
void
MainWindow::openJoinRoomDialog(std::function<void(const QString &room_id)> callback)
{
2018-08-11 13:50:56 +03:00
auto dialog = new dialogs::JoinRoom(this);
2018-09-19 22:42:26 +03:00
connect(dialog, &dialogs::JoinRoom::joinRoom, this, [callback](const QString &room) {
if (!room.isEmpty())
callback(room);
});
2018-09-21 11:30:02 +03:00
showDialog(dialog);
}
void
MainWindow::openCreateRoomDialog(
std::function<void(const mtx::requests::CreateRoom &request)> callback)
{
2018-08-11 13:50:56 +03:00
auto dialog = new dialogs::CreateRoom(this);
connect(dialog,
2018-09-19 22:42:26 +03:00
&dialogs::CreateRoom::createRoom,
2018-08-11 13:50:56 +03:00
this,
2018-09-19 22:42:26 +03:00
[callback](const mtx::requests::CreateRoom &request) { callback(request); });
2018-09-21 11:30:02 +03:00
showDialog(dialog);
2018-08-11 13:50:56 +03:00
}
void
MainWindow::showTransparentOverlayModal(QWidget *content, QFlags<Qt::AlignmentFlag> flags)
{
modal_->setWidget(content);
modal_->setColor(QColor(30, 30, 30, 150));
modal_->setDismissible(true);
modal_->setContentAlignment(flags);
modal_->raise();
modal_->show();
}
void
MainWindow::showSolidOverlayModal(QWidget *content, QFlags<Qt::AlignmentFlag> flags)
{
modal_->setWidget(content);
modal_->setColor(QColor(30, 30, 30));
modal_->setDismissible(false);
modal_->setContentAlignment(flags);
modal_->raise();
modal_->show();
}
void
MainWindow::openLogoutDialog()
{
2018-08-11 13:50:56 +03:00
auto dialog = new dialogs::Logout(this);
connect(dialog, &dialogs::Logout::loggingOut, this, [this]() {
2020-09-22 19:07:36 +03:00
if (WebRTCSession::instance().state() != webrtc::State::DISCONNECTED) {
if (QMessageBox::question(
this, "nheko", "A call is in progress. Log out?") !=
QMessageBox::Yes) {
return;
}
WebRTCSession::instance().end();
}
chat_page_->initiateLogout();
});
2018-08-11 13:50:56 +03:00
2018-09-21 11:30:02 +03:00
showDialog(dialog);
2018-08-11 13:50:56 +03:00
}
void
MainWindow::openReadReceiptsDialog(const QString &event_id)
{
auto dialog = new dialogs::ReadReceipts(this);
const auto room_id = chat_page_->currentRoom();
try {
2019-12-15 04:56:04 +03:00
dialog->addUsers(cache::readReceipts(event_id, room_id));
2020-12-25 03:08:06 +03:00
} catch (const lmdb::error &) {
2018-08-11 13:50:56 +03:00
nhlog::db()->warn("failed to retrieve read receipts for {} {}",
event_id.toStdString(),
chat_page_->currentRoom().toStdString());
dialog->deleteLater();
return;
}
2018-09-21 11:30:02 +03:00
showDialog(dialog);
}
bool
MainWindow::hasActiveDialogs() const
{
2018-08-11 13:50:56 +03:00
return !modal_ && modal_->isVisible();
}
bool
MainWindow::pageSupportsTray() const
{
return !welcome_page_->isVisible() && !login_page_->isVisible() &&
!register_page_->isVisible();
}
2018-08-11 13:50:56 +03:00
void
MainWindow::hideOverlay()
{
if (modal_)
modal_->hide();
}
2018-09-21 11:30:02 +03:00
inline void
MainWindow::showDialog(QWidget *dialog)
{
utils::centerWidget(dialog, this);
dialog->raise();
dialog->show();
}
bool
MainWindow::loadJdenticonPlugin()
{
2019-01-26 21:11:30 +03:00
QDir pluginsDir(qApp->applicationDirPath());
bool plugins = pluginsDir.cd("plugins");
if (plugins) {
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
QObject *plugin = pluginLoader.instance();
if (plugin) {
jdenticonInteface_ = qobject_cast<JdenticonInterface *>(plugin);
if (jdenticonInteface_) {
nhlog::ui()->info("Found jdenticon plugin.");
return true;
}
}
}
}
2019-01-26 21:11:30 +03:00
nhlog::ui()->info("jdenticon plugin not found.");
return false;
2019-12-15 04:56:04 +03:00
}
2020-02-23 13:42:29 +03:00
void
MainWindow::showWelcomePage()
{
removeOverlayProgressBar();
pageStack_->addWidget(welcome_page_);
pageStack_->setCurrentWidget(welcome_page_);
}
void
MainWindow::showLoginPage()
{
if (modal_)
modal_->hide();
pageStack_->addWidget(login_page_);
pageStack_->setCurrentWidget(login_page_);
}
void
MainWindow::showRegisterPage()
{
pageStack_->addWidget(register_page_);
pageStack_->setCurrentWidget(register_page_);
}
void
MainWindow::showUserSettingsPage()
{
pageStack_->setCurrentWidget(userSettingsPage_);
}