2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// 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>
|
2020-07-24 00:20:32 +03:00
|
|
|
#include <QMessageBox>
|
2017-10-28 15:46:39 +03:00
|
|
|
|
2023-06-02 02:37:53 +03:00
|
|
|
#include <mtx/events/collections.hpp>
|
2018-03-04 14:40:30 +03:00
|
|
|
#include <mtx/requests.hpp>
|
2020-10-27 19:45:28 +03:00
|
|
|
#include <mtx/responses/login.hpp>
|
2018-03-04 14:40:30 +03:00
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
#include "BlurhashProvider.h"
|
2020-12-17 00:10:09 +03:00
|
|
|
#include "Cache_p.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "ChatPage.h"
|
2022-01-12 21:09:46 +03:00
|
|
|
#include "ColorImageProvider.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "Config.h"
|
2020-12-25 17:14:00 +03:00
|
|
|
#include "JdenticonProvider.h"
|
2018-07-17 16:37:25 +03:00
|
|
|
#include "Logging.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#include "MainWindow.h"
|
|
|
|
#include "MatrixClient.h"
|
2022-01-12 21:09:46 +03:00
|
|
|
#include "MxcImageProvider.h"
|
2017-10-28 15:46:39 +03:00
|
|
|
#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"
|
2022-06-05 14:51:29 +03:00
|
|
|
#include "dock/Dock.h"
|
2022-01-12 21:09:46 +03:00
|
|
|
#include "encryption/DeviceVerificationFlow.h"
|
|
|
|
#include "timeline/TimelineViewManager.h"
|
2023-10-26 00:22:39 +03:00
|
|
|
#include "ui/Theme.h"
|
2022-06-16 02:19:26 +03:00
|
|
|
#include "voip/CallManager.h"
|
2021-10-14 23:53:11 +03:00
|
|
|
#include "voip/WebRTCSession.h"
|
2018-03-04 14:40:30 +03:00
|
|
|
|
2022-04-14 18:02:55 +03:00
|
|
|
#ifdef NHEKO_DBUS_SYS
|
|
|
|
#include "dbus/NhekoDBusApi.h"
|
|
|
|
#endif
|
|
|
|
|
2017-06-02 18:04:51 +03:00
|
|
|
MainWindow *MainWindow::instance_ = nullptr;
|
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
MainWindow::MainWindow(QWindow *parent)
|
|
|
|
: QQuickView(parent)
|
2020-12-28 00:56:43 +03:00
|
|
|
, userSettings_{UserSettings::instance()}
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
instance_ = this;
|
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
MainWindow::setWindowTitle(0);
|
2021-12-29 06:28:08 +03:00
|
|
|
setObjectName(QStringLiteral("MainWindow"));
|
2022-01-12 21:09:46 +03:00
|
|
|
setResizeMode(QQuickView::SizeRootObjectToView);
|
2022-02-15 00:56:35 +03:00
|
|
|
setMinimumHeight(conf::window::minHeight);
|
|
|
|
setMinimumWidth(conf::window::minWidth);
|
2022-01-12 21:09:46 +03:00
|
|
|
restoreWindowSize();
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
chat_page_ = new ChatPage(userSettings_, this);
|
|
|
|
registerQmlTypes();
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
setColor(Theme::paletteFromTheme(userSettings_->theme()).window().color());
|
2023-06-19 02:38:40 +03:00
|
|
|
setSource(QUrl(QStringLiteral("qrc:///resources/qml/Root.qml")));
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
trayIcon_ = new TrayIcon(QStringLiteral(":/logos/nheko.svg"), this);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-01-24 02:41:55 +03:00
|
|
|
connect(chat_page_, &ChatPage::closing, this, [this] { switchToLoginPage(""); });
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(chat_page_, &ChatPage::unreadMessages, this, &MainWindow::setWindowTitle);
|
|
|
|
connect(chat_page_, SIGNAL(unreadMessages(int)), trayIcon_, SLOT(setUnreadCount(int)));
|
2022-01-30 21:14:33 +03:00
|
|
|
connect(chat_page_, &ChatPage::showLoginPage, this, &MainWindow::switchToLoginPage);
|
|
|
|
connect(chat_page_, &ChatPage::showNotification, this, &MainWindow::showNotification);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-01-09 02:28:03 +03:00
|
|
|
connect(userSettings_.get(), &UserSettings::trayChanged, trayIcon_, &TrayIcon::setVisible);
|
2021-09-18 01:22:33 +03:00
|
|
|
connect(trayIcon_,
|
|
|
|
SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
|
|
|
this,
|
|
|
|
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
|
|
|
|
|
|
|
|
trayIcon_->setVisible(userSettings_->tray());
|
2022-06-05 14:51:29 +03:00
|
|
|
dock_ = new Dock(this);
|
|
|
|
connect(chat_page_, SIGNAL(unreadMessages(int)), dock_, SLOT(setUnreadCount(int)));
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
// 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 21:09:46 +03:00
|
|
|
nhlog::ui()->info("User already signed in, showing chat page");
|
2021-09-18 01:22:33 +03:00
|
|
|
showChatPage();
|
|
|
|
}
|
|
|
|
});
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2022-01-12 21:09:46 +03:00
|
|
|
void
|
|
|
|
MainWindow::registerQmlTypes()
|
|
|
|
{
|
|
|
|
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 19:38:59 +03:00
|
|
|
|
|
|
|
QObject::connect(engine(), &QQmlEngine::quit, &QGuiApplication::quit);
|
2022-04-14 18:02:55 +03:00
|
|
|
|
|
|
|
#ifdef NHEKO_DBUS_SYS
|
|
|
|
if (UserSettings::instance()->exposeDBusApi()) {
|
|
|
|
if (QDBusConnection::sessionBus().isConnected() &&
|
|
|
|
QDBusConnection::sessionBus().registerService(NHEKO_DBUS_SERVICE_NAME)) {
|
|
|
|
nheko::dbus::init();
|
|
|
|
nhlog::ui()->info("Initialized D-Bus");
|
|
|
|
dbusAvailable_ = true;
|
|
|
|
} else
|
|
|
|
nhlog::ui()->warn("Could not connect to D-Bus!");
|
|
|
|
}
|
|
|
|
#endif
|
2022-01-12 21:09:46 +03:00
|
|
|
}
|
|
|
|
|
2020-10-22 02:20:02 +03:00
|
|
|
void
|
|
|
|
MainWindow::setWindowTitle(int notificationCount)
|
|
|
|
{
|
2021-12-29 06:28:08 +03:00
|
|
|
QString name = QStringLiteral("nheko");
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
if (!userSettings_.data()->profile().isEmpty())
|
|
|
|
name += " | " + userSettings_.data()->profile();
|
|
|
|
if (notificationCount > 0) {
|
2021-12-29 06:28:08 +03:00
|
|
|
name.append(QString{QStringLiteral(" (%1)")}.arg(notificationCount));
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2022-01-12 21:09:46 +03:00
|
|
|
QQuickView::setTitle(name);
|
2020-10-22 02:20:02 +03:00
|
|
|
}
|
|
|
|
|
2023-01-23 15:15:43 +03:00
|
|
|
// HACK: https://bugreports.qt.io/browse/QTBUG-83972, qtwayland cannot auto hide menu
|
|
|
|
void
|
|
|
|
MainWindow::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2023-01-26 15:34:38 +03:00
|
|
|
#if defined(Q_OS_LINUX)
|
|
|
|
if (QGuiApplication::platformName() == "wayland") {
|
2023-01-28 12:10:45 +03:00
|
|
|
emit hideMenu();
|
2023-01-23 15:15:43 +03:00
|
|
|
}
|
2023-01-26 15:34:38 +03:00
|
|
|
#endif
|
2023-01-23 15:15:43 +03:00
|
|
|
return QQuickView::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::restoreWindowSize()
|
2017-07-30 13:59:28 +03:00
|
|
|
{
|
2021-12-29 06:28:08 +03:00
|
|
|
int savedWidth = userSettings_->qsettings()->value(QStringLiteral("window/width")).toInt();
|
|
|
|
int savedheight = userSettings_->qsettings()->value(QStringLiteral("window/height")).toInt();
|
2021-08-29 15:57:32 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
nhlog::ui()->info("Restoring window size {}x{}", savedWidth, savedheight);
|
2017-09-10 12:58:00 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (savedWidth == 0 || savedheight == 0)
|
|
|
|
resize(conf::window::width, conf::window::height);
|
|
|
|
else
|
|
|
|
resize(savedWidth, savedheight);
|
2017-07-30 13:59:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::saveCurrentWindowSize()
|
2017-07-30 13:59:28 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto settings = userSettings_->qsettings();
|
|
|
|
QSize current = size();
|
2017-07-30 13:59:28 +03:00
|
|
|
|
2021-12-29 06:28:08 +03:00
|
|
|
settings->setValue(QStringLiteral("window/width"), current.width());
|
|
|
|
settings->setValue(QStringLiteral("window/height"), current.height());
|
2017-07-30 13:59:28 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
2018-06-09 16:03:14 +03:00
|
|
|
MainWindow::showChatPage()
|
2017-04-06 02:06:42 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto userid = QString::fromStdString(http::client()->user_id().to_string());
|
|
|
|
auto device_id = QString::fromStdString(http::client()->device_id());
|
2022-03-07 07:26:12 +03:00
|
|
|
auto homeserver = QString::fromStdString(http::client()->server_url());
|
2021-09-18 01:22:33 +03: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 02:28:03 +03:00
|
|
|
connect(cache::client(), &Cache::databaseReady, this, &MainWindow::secretsChanged);
|
|
|
|
connect(cache::client(), &Cache::secretChanged, this, &MainWindow::secretsChanged);
|
2022-01-12 21:09:46 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
emit reload();
|
2022-01-12 21:09:46 +03:00
|
|
|
nhlog::ui()->info("Switching to chat page");
|
|
|
|
emit switchToChatPage();
|
2017-04-06 02:06:42 +03:00
|
|
|
}
|
|
|
|
|
2023-06-05 01:18:17 +03:00
|
|
|
bool
|
|
|
|
NhekoFixupPaletteEventFilter::eventFilter(QObject *obj, QEvent *event)
|
|
|
|
{
|
|
|
|
// Workaround for the QGuiApplication palette not being applied to toplevel windows for some
|
|
|
|
// reason?!?
|
|
|
|
if (event->type() == QEvent::ChildAdded &&
|
|
|
|
obj->metaObject()->className() == QStringLiteral("QQuickRootItem")) {
|
|
|
|
for (const auto window : QGuiApplication::topLevelWindows()) {
|
2023-12-29 09:51:37 +03:00
|
|
|
if (window->property("posted").isValid())
|
2023-12-16 13:04:15 +03:00
|
|
|
continue;
|
2023-06-05 01:18:17 +03:00
|
|
|
QGuiApplication::postEvent(window, new QEvent(QEvent::ApplicationPaletteChange));
|
2023-12-29 09:51:37 +03:00
|
|
|
window->setProperty("posted", true);
|
2023-06-05 01:18:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
void
|
|
|
|
MainWindow::closeEvent(QCloseEvent *event)
|
2017-05-21 16:36:06 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (WebRTCSession::instance().state() != webrtc::State::DISCONNECTED) {
|
2021-12-29 08:01:38 +03:00
|
|
|
if (QMessageBox::question(
|
2022-01-12 21:09:46 +03:00
|
|
|
nullptr, QStringLiteral("nheko"), QStringLiteral("A call is in progress. Quit?")) !=
|
2021-09-18 01:22:33 +03:00
|
|
|
QMessageBox::Yes) {
|
|
|
|
event->ignore();
|
|
|
|
return;
|
2020-07-24 00:20:32 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2020-07-24 00:20:32 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!qApp->isSavingSession() && isVisible() && pageSupportsTray() && userSettings_->tray()) {
|
|
|
|
event->ignore();
|
|
|
|
hide();
|
2023-06-03 02:43:48 +03:00
|
|
|
return;
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
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
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
switch (reason) {
|
|
|
|
case QSystemTrayIcon::Trigger:
|
|
|
|
if (!isVisible()) {
|
|
|
|
show();
|
|
|
|
} else {
|
|
|
|
hide();
|
2017-09-10 12:58:00 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-05-21 16:36:06 +03:00
|
|
|
}
|
|
|
|
|
2017-08-20 13:47:22 +03:00
|
|
|
bool
|
|
|
|
MainWindow::hasActiveUser()
|
2017-05-22 18:03:37 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto settings = userSettings_->qsettings();
|
|
|
|
QString prefix;
|
2021-12-29 06:28:08 +03:00
|
|
|
if (userSettings_->profile() != QLatin1String(""))
|
2021-09-18 01:22:33 +03:00
|
|
|
prefix = "profile/" + userSettings_->profile() + "/";
|
|
|
|
|
2023-11-05 14:09:12 +03:00
|
|
|
return !settings->value(prefix + "auth/access_token").toString().isEmpty() &&
|
|
|
|
!settings->value(prefix + "auth/home_server").toString().isEmpty() &&
|
|
|
|
!settings->value(prefix + "auth/user_id").toString().isEmpty();
|
2017-05-22 18:03:37 +03:00
|
|
|
}
|
|
|
|
|
2018-07-11 20:55:13 +03:00
|
|
|
bool
|
|
|
|
MainWindow::pageSupportsTray() const
|
|
|
|
{
|
2022-01-29 21:07:27 +03:00
|
|
|
return !http::client()->access_token().empty();
|
2018-07-11 20:55:13 +03:00
|
|
|
}
|
2018-08-11 13:50:56 +03:00
|
|
|
|
2018-09-21 11:30:02 +03:00
|
|
|
inline void
|
|
|
|
MainWindow::showDialog(QWidget *dialog)
|
|
|
|
{
|
2022-01-29 21:07:27 +03:00
|
|
|
dialog->setWindowFlags(Qt::WindowType::Dialog | Qt::WindowType::WindowCloseButtonHint |
|
|
|
|
Qt::WindowType::WindowTitleHint);
|
2021-09-18 01:22:33 +03:00
|
|
|
dialog->raise();
|
|
|
|
dialog->show();
|
2022-01-29 21:07:27 +03:00
|
|
|
utils::centerWidget(dialog, this);
|
|
|
|
dialog->window()->windowHandle()->setTransientParent(this);
|
2018-09-21 11:30:02 +03:00
|
|
|
}
|
2022-05-07 19:53:16 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
MainWindow::addPerRoomWindow(const QString &room, QWindow *window)
|
|
|
|
{
|
|
|
|
roomWindows_.insert(room, window);
|
|
|
|
}
|
|
|
|
void
|
|
|
|
MainWindow::removePerRoomWindow(const QString &room, QWindow *window)
|
|
|
|
{
|
|
|
|
roomWindows_.remove(room, window);
|
|
|
|
}
|
2022-05-07 20:03:58 +03:00
|
|
|
QWindow *
|
|
|
|
MainWindow::windowForRoom(const QString &room)
|
2022-05-07 19:53:16 +03:00
|
|
|
{
|
|
|
|
auto currMainWindowRoom = ChatPage::instance()->timelineManager()->rooms()->currentRoom();
|
|
|
|
if ((currMainWindowRoom && currMainWindowRoom->roomId() == room) ||
|
|
|
|
ChatPage::instance()->timelineManager()->rooms()->currentRoomPreview().roomid_ == room)
|
|
|
|
return this;
|
|
|
|
else if (auto res = roomWindows_.find(room); res != roomWindows_.end())
|
|
|
|
return res.value();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString
|
|
|
|
MainWindow::focusedRoom() const
|
|
|
|
{
|
|
|
|
auto focus = QGuiApplication::focusWindow();
|
|
|
|
if (!focus)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
if (focus == this) {
|
|
|
|
auto currMainWindowRoom = ChatPage::instance()->timelineManager()->rooms()->currentRoom();
|
|
|
|
if (currMainWindowRoom)
|
|
|
|
return currMainWindowRoom->roomId();
|
|
|
|
else
|
|
|
|
return ChatPage::instance()->timelineManager()->rooms()->currentRoomPreview().roomid_;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto i = roomWindows_.constBegin();
|
|
|
|
while (i != roomWindows_.constEnd()) {
|
|
|
|
if (i.value() == focus)
|
|
|
|
return i.key();
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
2024-03-16 03:24:33 +03:00
|
|
|
|
|
|
|
#include "moc_MainWindow.cpp"
|