nheko/src/MainWindow.cpp

421 lines
15 KiB
C++
Raw Normal View History

2021-03-05 00:35:15 +01:00
// SPDX-FileCopyrightText: 2017 Konstantinos Sideris <siderisk@auth.gr>
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
2021-03-05 00:35:15 +01:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
2017-04-06 01:06:42 +02:00
2017-09-24 16:39:06 +02:00
#include <QApplication>
#include <QMessageBox>
2017-10-28 14:46:39 +02:00
#include <mtx/requests.hpp>
2020-10-27 17:45:28 +01:00
#include <mtx/responses/login.hpp>
2022-01-12 19:09:46 +01:00
#include "BlurhashProvider.h"
#include "Cache.h"
2020-12-16 22:10:09 +01:00
#include "Cache_p.h"
2017-10-28 14:46:39 +02:00
#include "ChatPage.h"
2022-01-12 19:09:46 +01:00
#include "Clipboard.h"
#include "ColorImageProvider.h"
#include "CombinedImagePackModel.h"
#include "CompletionProxyModel.h"
2017-10-28 14:46:39 +02:00
#include "Config.h"
2022-01-12 19:09:46 +01:00
#include "EventAccessors.h"
#include "ImagePackListModel.h"
#include "InviteesModel.h"
2020-12-25 15:14:00 +01:00
#include "JdenticonProvider.h"
2018-07-17 15:37:25 +02:00
#include "Logging.h"
2017-10-28 14:46:39 +02:00
#include "LoginPage.h"
#include "MainWindow.h"
#include "MatrixClient.h"
2021-05-30 03:09:21 +02:00
#include "MemberList.h"
2022-01-12 19:09:46 +01:00
#include "MxcImageProvider.h"
#include "ReadReceiptsModel.h"
2017-10-28 14:46:39 +02:00
#include "RegisterPage.h"
2022-01-12 19:09:46 +01:00
#include "RoomDirectoryModel.h"
#include "RoomsModel.h"
#include "SingleImagePackModel.h"
2017-10-28 14:46:39 +02:00
#include "TrayIcon.h"
2017-11-01 23:41:13 +01:00
#include "UserSettingsPage.h"
2022-01-12 19:09:46 +01:00
#include "UsersModel.h"
2018-09-21 10:30:02 +02:00
#include "Utils.h"
2022-01-12 19:09:46 +01:00
#include "emoji/EmojiModel.h"
#include "emoji/Provider.h"
#include "encryption/DeviceVerificationFlow.h"
#include "encryption/SelfVerificationStatus.h"
#include "timeline/DelegateChooser.h"
#include "timeline/TimelineViewManager.h"
#include "ui/HiddenEvents.h"
2022-01-12 19:09:46 +01:00
#include "ui/MxcAnimatedImage.h"
#include "ui/MxcMediaProxy.h"
#include "ui/NhekoCursorShape.h"
#include "ui/NhekoDropArea.h"
#include "ui/NhekoEventObserver.h"
2022-01-12 19:09:46 +01:00
#include "ui/NhekoGlobalObject.h"
#include "ui/UIA.h"
#include "voip/WebRTCSession.h"
2022-01-12 19:09:46 +01:00
Q_DECLARE_METATYPE(mtx::events::collections::TimelineEvents)
Q_DECLARE_METATYPE(std::vector<DeviceInfo>)
Q_DECLARE_METATYPE(std::vector<mtx::responses::PublicRoomsChunk>)
MainWindow *MainWindow::instance_ = nullptr;
2022-01-12 19:09:46 +01:00
MainWindow::MainWindow(QWindow *parent)
: QQuickView(parent)
, userSettings_{UserSettings::instance()}
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02:00
instance_ = this;
2022-01-12 19:09:46 +01:00
MainWindow::setWindowTitle(0);
setObjectName(QStringLiteral("MainWindow"));
2022-01-12 19:09:46 +01:00
setResizeMode(QQuickView::SizeRootObjectToView);
setMinimumHeight(conf::window::minHeight);
setMinimumWidth(conf::window::minWidth);
2022-01-12 19:09:46 +01:00
restoreWindowSize();
2021-09-18 00:22:33 +02:00
2022-01-12 19:09:46 +01:00
chat_page_ = new ChatPage(userSettings_, this);
registerQmlTypes();
2021-09-18 00:22:33 +02:00
2022-01-12 19:09:46 +01:00
setColor(Theme::paletteFromTheme(userSettings_->theme()).window().color());
setSource(QUrl(QStringLiteral("qrc:///qml/Root.qml")));
2021-09-18 00:22:33 +02:00
2022-01-12 19:09:46 +01:00
trayIcon_ = new TrayIcon(QStringLiteral(":/logos/nheko.svg"), this);
2021-09-18 00:22:33 +02:00
2022-01-24 00:41:55 +01:00
connect(chat_page_, &ChatPage::closing, this, [this] { switchToLoginPage(""); });
2021-09-18 00:22:33 +02:00
connect(chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle);
connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int)));
2022-01-30 19:14:33 +01:00
connect(chat_page_, &ChatPage::showLoginPage, this, &MainWindow::switchToLoginPage);
connect(chat_page_, &ChatPage::showNotification, this, &MainWindow::showNotification);
2021-09-18 00:22:33 +02:00
2022-01-09 00:28:03 +01:00
connect(userSettings_.get(), &UserSettings::trayChanged, trayIcon_, &TrayIcon::setVisible);
2021-09-18 00:22:33 +02:00
connect(trayIcon_,
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
trayIcon_->setVisible(userSettings_->tray());
// load cache on event loop
QTimer::singleShot(0, this, [this] {
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()));
} catch (const std::invalid_argument &) {
nhlog::ui()->critical("bootstrapped with invalid user_id: {}",
user_id.toStdString());
}
2022-01-12 19:09:46 +01:00
nhlog::ui()->info("User already signed in, showing chat page");
2021-09-18 00:22:33 +02:00
showChatPage();
}
});
2017-04-06 01:06:42 +02:00
}
2022-01-12 19:09:46 +01:00
void
MainWindow::registerQmlTypes()
{
qRegisterMetaType<mtx::events::msg::KeyVerificationAccept>();
qRegisterMetaType<mtx::events::msg::KeyVerificationCancel>();
qRegisterMetaType<mtx::events::msg::KeyVerificationDone>();
qRegisterMetaType<mtx::events::msg::KeyVerificationKey>();
qRegisterMetaType<mtx::events::msg::KeyVerificationMac>();
qRegisterMetaType<mtx::events::msg::KeyVerificationReady>();
qRegisterMetaType<mtx::events::msg::KeyVerificationRequest>();
qRegisterMetaType<mtx::events::msg::KeyVerificationStart>();
qRegisterMetaType<CombinedImagePackModel *>();
qRegisterMetaType<mtx::events::collections::TimelineEvents>();
qRegisterMetaType<std::vector<DeviceInfo>>();
qRegisterMetaType<std::vector<mtx::responses::PublicRoomsChunk>>();
qmlRegisterUncreatableMetaObject(qml_mtx_events::staticMetaObject,
"im.nheko",
1,
0,
"MtxEvent",
QStringLiteral("Can't instantiate enum!"));
qmlRegisterUncreatableMetaObject(
olm::staticMetaObject, "im.nheko", 1, 0, "Olm", QStringLiteral("Can't instantiate enum!"));
qmlRegisterUncreatableMetaObject(crypto::staticMetaObject,
"im.nheko",
1,
0,
"Crypto",
QStringLiteral("Can't instantiate enum!"));
qmlRegisterUncreatableMetaObject(verification::staticMetaObject,
"im.nheko",
1,
0,
"VerificationStatus",
QStringLiteral("Can't instantiate enum!"));
qmlRegisterType<DelegateChoice>("im.nheko", 1, 0, "DelegateChoice");
qmlRegisterType<DelegateChooser>("im.nheko", 1, 0, "DelegateChooser");
qmlRegisterType<NhekoDropArea>("im.nheko", 1, 0, "NhekoDropArea");
qmlRegisterType<NhekoCursorShape>("im.nheko", 1, 0, "CursorShape");
qmlRegisterType<NhekoEventObserver>("im.nheko", 1, 0, "EventObserver");
2022-01-12 19:09:46 +01:00
qmlRegisterType<MxcAnimatedImage>("im.nheko", 1, 0, "MxcAnimatedImage");
qmlRegisterType<MxcMediaProxy>("im.nheko", 1, 0, "MxcMedia");
qmlRegisterType<RoomDirectoryModel>("im.nheko", 1, 0, "RoomDirectoryModel");
2022-01-24 00:41:55 +01:00
qmlRegisterType<LoginPage>("im.nheko", 1, 0, "Login");
2022-01-28 15:24:56 +01:00
qmlRegisterType<RegisterPage>("im.nheko", 1, 0, "Registration");
qmlRegisterType<HiddenEvents>("im.nheko", 1, 0, "HiddenEvents");
2022-01-12 19:09:46 +01:00
qmlRegisterUncreatableType<DeviceVerificationFlow>(
"im.nheko",
1,
0,
"DeviceVerificationFlow",
QStringLiteral("Can't create verification flow from QML!"));
qmlRegisterUncreatableType<UserProfile>(
"im.nheko",
1,
0,
"UserProfileModel",
QStringLiteral("UserProfile needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<MemberList>(
"im.nheko",
1,
0,
"MemberList",
QStringLiteral("MemberList needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<RoomSettings>(
"im.nheko",
1,
0,
"RoomSettingsModel",
QStringLiteral("Room Settings needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<TimelineModel>(
"im.nheko", 1, 0, "Room", QStringLiteral("Room needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<ImagePackListModel>(
"im.nheko",
1,
0,
"ImagePackListModel",
QStringLiteral("ImagePackListModel needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<SingleImagePackModel>(
"im.nheko",
1,
0,
"SingleImagePackModel",
QStringLiteral("SingleImagePackModel needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<InviteesModel>(
"im.nheko",
1,
0,
"InviteesModel",
QStringLiteral("InviteesModel needs to be instantiated on the C++ side"));
qmlRegisterUncreatableType<ReadReceiptsProxy>(
"im.nheko",
1,
0,
"ReadReceiptsProxy",
QStringLiteral("ReadReceiptsProxy needs to be instantiated on the C++ side"));
qmlRegisterSingletonType<Clipboard>(
"im.nheko", 1, 0, "Clipboard", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new Clipboard();
});
qmlRegisterSingletonType<Nheko>(
"im.nheko", 1, 0, "Nheko", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new Nheko();
});
qmlRegisterSingletonType<UserSettingsModel>(
"im.nheko", 1, 0, "UserSettingsModel", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new UserSettingsModel();
});
qmlRegisterSingletonInstance("im.nheko", 1, 0, "Settings", userSettings_.data());
qRegisterMetaType<mtx::events::collections::TimelineEvents>();
qRegisterMetaType<std::vector<DeviceInfo>>();
qmlRegisterUncreatableType<FilteredCommunitiesModel>(
"im.nheko",
1,
0,
"FilteredCommunitiesModel",
QStringLiteral("Use Communities.filtered() to create a FilteredCommunitiesModel"));
qmlRegisterType<emoji::EmojiModel>("im.nheko.EmojiModel", 1, 0, "EmojiModel");
qmlRegisterUncreatableType<emoji::Emoji>(
"im.nheko.EmojiModel", 1, 0, "Emoji", QStringLiteral("Used by emoji models"));
2022-03-21 05:05:29 +01:00
qmlRegisterUncreatableType<MediaUpload>(
"im.nheko", 1, 0, "MediaUpload", QStringLiteral("MediaUploads can not be created in Qml"));
2022-01-12 19:09:46 +01:00
qmlRegisterUncreatableMetaObject(emoji::staticMetaObject,
"im.nheko.EmojiModel",
1,
0,
"EmojiCategory",
QStringLiteral("Error: Only enums"));
qmlRegisterType<RoomDirectoryModel>("im.nheko", 1, 0, "RoomDirectoryModel");
qmlRegisterSingletonType<SelfVerificationStatus>(
"im.nheko", 1, 0, "SelfVerificationStatus", [](QQmlEngine *, QJSEngine *) -> QObject * {
auto ptr = new SelfVerificationStatus();
QObject::connect(ChatPage::instance(),
&ChatPage::initializeEmptyViews,
ptr,
&SelfVerificationStatus::invalidate);
return ptr;
});
qmlRegisterSingletonInstance("im.nheko", 1, 0, "MainWindow", this);
qmlRegisterSingletonInstance("im.nheko", 1, 0, "UIA", UIA::instance());
qmlRegisterSingletonInstance(
"im.nheko", 1, 0, "CallManager", ChatPage::instance()->callManager());
imgProvider = new MxcImageProvider();
engine()->addImageProvider(QStringLiteral("MxcImage"), imgProvider);
engine()->addImageProvider(QStringLiteral("colorimage"), new ColorImageProvider());
engine()->addImageProvider(QStringLiteral("blurhash"), new BlurhashProvider());
if (JdenticonProvider::isAvailable())
engine()->addImageProvider(QStringLiteral("jdenticon"), new JdenticonProvider());
2022-01-29 17:38:59 +01:00
QObject::connect(engine(), &QQmlEngine::quit, &QGuiApplication::quit);
2022-01-12 19:09:46 +01:00
}
2020-10-22 01:20:02 +02:00
void
MainWindow::setWindowTitle(int notificationCount)
{
QString name = QStringLiteral("nheko");
2021-09-18 00:22:33 +02:00
if (!userSettings_.data()->profile().isEmpty())
name += " | " + userSettings_.data()->profile();
if (notificationCount > 0) {
name.append(QString{QStringLiteral(" (%1)")}.arg(notificationCount));
2021-09-18 00:22:33 +02:00
}
2022-01-12 19:09:46 +01:00
QQuickView::setTitle(name);
2020-10-22 01:20:02 +02:00
}
bool
MainWindow::event(QEvent *event)
{
2021-09-18 00:22:33 +02:00
auto type = event->type();
2022-01-12 19:09:46 +01:00
if (type == QEvent::Close) {
closeEvent(static_cast<QCloseEvent *>(event));
2021-09-18 00:22:33 +02:00
}
2022-01-12 19:09:46 +01:00
return QQuickView::event(event);
}
2017-08-20 12:47:22 +02:00
void
MainWindow::restoreWindowSize()
{
int savedWidth = userSettings_->qsettings()->value(QStringLiteral("window/width")).toInt();
int savedheight = userSettings_->qsettings()->value(QStringLiteral("window/height")).toInt();
2021-08-29 14:57:32 +02:00
2021-09-18 00:22:33 +02:00
nhlog::ui()->info("Restoring window size {}x{}", savedWidth, savedheight);
2017-09-10 11:58:00 +02:00
2021-09-18 00:22:33 +02:00
if (savedWidth == 0 || savedheight == 0)
resize(conf::window::width, conf::window::height);
else
resize(savedWidth, savedheight);
}
2017-08-20 12:47:22 +02:00
void
MainWindow::saveCurrentWindowSize()
{
2021-09-18 00:22:33 +02:00
auto settings = userSettings_->qsettings();
QSize current = size();
settings->setValue(QStringLiteral("window/width"), current.width());
settings->setValue(QStringLiteral("window/height"), current.height());
}
2017-08-20 12:47:22 +02:00
void
MainWindow::showChatPage()
2017-04-06 01:06:42 +02:00
{
2021-09-18 00:22:33 +02: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_url());
2021-09-18 00:22:33 +02:00
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);
chat_page_->bootstrap(userid, homeserver, token);
2022-01-09 00:28:03 +01:00
connect(cache::client(), &Cache::databaseReady, this, &MainWindow::secretsChanged);
connect(cache::client(), &Cache::secretChanged, this, &MainWindow::secretsChanged);
2022-01-12 19:09:46 +01:00
2021-09-18 00:22:33 +02:00
emit reload();
2022-01-12 19:09:46 +01:00
nhlog::ui()->info("Switching to chat page");
emit switchToChatPage();
2017-04-06 01:06:42 +02:00
}
2017-08-20 12:47:22 +02:00
void
MainWindow::closeEvent(QCloseEvent *event)
2017-05-21 15:36:06 +02:00
{
2021-09-18 00:22:33 +02:00
if (WebRTCSession::instance().state() != webrtc::State::DISCONNECTED) {
if (QMessageBox::question(
2022-01-12 19:09:46 +01:00
nullptr, QStringLiteral("nheko"), QStringLiteral("A call is in progress. Quit?")) !=
2021-09-18 00:22:33 +02:00
QMessageBox::Yes) {
event->ignore();
return;
}
2021-09-18 00:22:33 +02:00
}
2021-09-18 00:22:33 +02:00
if (!qApp->isSavingSession() && isVisible() && pageSupportsTray() && userSettings_->tray()) {
event->ignore();
hide();
}
2017-05-21 15:36:06 +02:00
}
2017-08-20 12:47:22 +02:00
void
MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
2017-05-21 15:36:06 +02:00
{
2021-09-18 00:22:33 +02:00
switch (reason) {
case QSystemTrayIcon::Trigger:
if (!isVisible()) {
show();
} else {
hide();
2017-09-10 11:58:00 +02:00
}
2021-09-18 00:22:33 +02:00
break;
default:
break;
}
2017-05-21 15:36:06 +02:00
}
2017-08-20 12:47:22 +02:00
bool
MainWindow::hasActiveUser()
{
2021-09-18 00:22:33 +02:00
auto settings = userSettings_->qsettings();
QString prefix;
if (userSettings_->profile() != QLatin1String(""))
2021-09-18 00:22:33 +02:00
prefix = "profile/" + userSettings_->profile() + "/";
return settings->contains(prefix + "auth/access_token") &&
settings->contains(prefix + "auth/home_server") &&
settings->contains(prefix + "auth/user_id");
}
bool
MainWindow::pageSupportsTray() const
{
2022-01-29 19:07:27 +01:00
return !http::client()->access_token().empty();
}
2018-08-11 12:50:56 +02:00
2018-09-21 10:30:02 +02:00
inline void
MainWindow::showDialog(QWidget *dialog)
{
2022-01-29 19:07:27 +01:00
dialog->setWindowFlags(Qt::WindowType::Dialog | Qt::WindowType::WindowCloseButtonHint |
Qt::WindowType::WindowTitleHint);
2021-09-18 00:22:33 +02:00
dialog->raise();
dialog->show();
2022-01-29 19:07:27 +01:00
utils::centerWidget(dialog, this);
dialog->window()->windowHandle()->setTransientParent(this);
2018-09-21 10:30:02 +02:00
}