matrixion/src/ui/NhekoGlobalObject.cpp

135 lines
3.4 KiB
C++
Raw Normal View History

2021-05-13 09:23:56 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "NhekoGlobalObject.h"
2021-05-13 09:52:02 +03:00
#include <QDesktopServices>
#include <QUrl>
#include <QWindow>
2021-05-13 09:52:02 +03:00
2021-05-15 00:35:34 +03:00
#include "Cache_p.h"
2021-05-13 09:52:02 +03:00
#include "ChatPage.h"
2021-05-15 00:35:34 +03:00
#include "Logging.h"
2021-05-30 13:41:44 +03:00
#include "MainWindow.h"
2021-05-13 09:23:56 +03:00
#include "UserSettingsPage.h"
2021-05-15 00:35:34 +03:00
#include "Utils.h"
#include "voip/WebRTCSession.h"
2021-05-13 09:23:56 +03:00
Nheko::Nheko()
{
2021-09-18 01:22:33 +03:00
connect(
UserSettings::instance().get(), &UserSettings::themeChanged, this, &Nheko::colorsChanged);
connect(ChatPage::instance(), &ChatPage::contentLoaded, this, &Nheko::updateUserProfile);
2021-09-25 04:32:06 +03:00
connect(this, &Nheko::joinRoom, ChatPage::instance(), &ChatPage::joinRoom);
2021-05-15 00:35:34 +03:00
}
void
Nheko::updateUserProfile()
{
2021-09-18 01:22:33 +03:00
if (cache::client() && cache::client()->isInitialized())
currentUser_.reset(new UserProfile(
QLatin1String(""), utils::localUser(), ChatPage::instance()->timelineManager()));
2021-09-18 01:22:33 +03:00
else
currentUser_.reset();
emit profileChanged();
2021-05-13 09:23:56 +03:00
}
QPalette
Nheko::colors() const
{
2021-12-29 10:05:29 +03:00
return Theme::paletteFromTheme(UserSettings::instance()->theme());
2021-05-13 09:23:56 +03:00
}
QPalette
Nheko::inactiveColors() const
{
2021-12-29 10:05:29 +03:00
auto theme = UserSettings::instance()->theme();
if (theme == QLatin1String("light")) {
static QPalette lightInactive = [] {
auto lightInactive = Theme::paletteFromTheme(u"light");
lightInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return lightInactive;
}();
return lightInactive;
} else if (theme == QLatin1String("dark")) {
static QPalette darkInactive = [] {
auto darkInactive = Theme::paletteFromTheme(u"dark");
darkInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return darkInactive;
}();
return darkInactive;
} else {
static QPalette originalInactive = [] {
auto originalInactive = Theme::paletteFromTheme(u"system");
originalInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return originalInactive;
}();
return originalInactive;
}
2021-05-13 09:23:56 +03:00
}
2021-05-13 09:52:02 +03:00
2021-05-15 00:35:34 +03:00
Theme
Nheko::theme() const
{
2021-12-29 10:05:29 +03:00
return Theme(UserSettings::instance()->theme());
2021-05-15 00:35:34 +03:00
}
2021-05-13 09:52:02 +03:00
void
Nheko::openLink(QString link) const
{
2021-09-18 01:22:33 +03:00
QUrl url(link);
// Open externally if we couldn't handle it internally
if (!ChatPage::instance()->handleMatrixUri(url)) {
2021-12-29 10:05:29 +03:00
static const QStringList allowedUrlSchemes = {
QStringLiteral("http"),
QStringLiteral("https"),
QStringLiteral("mailto"),
2021-10-17 18:18:02 +03:00
};
if (allowedUrlSchemes.contains(url.scheme()))
QDesktopServices::openUrl(url);
else
nhlog::ui()->warn("Url '{}' not opened, because the scheme is not in the allow list",
url.toDisplayString().toStdString());
2021-09-18 01:22:33 +03:00
}
2021-05-13 09:52:02 +03:00
}
2021-05-22 16:19:44 +03:00
void
Nheko::setStatusMessage(QString msg) const
{
2021-09-18 01:22:33 +03:00
ChatPage::instance()->setStatus(msg);
2021-05-22 16:19:44 +03:00
}
2021-05-15 00:35:34 +03:00
UserProfile *
Nheko::currentUser() const
{
2021-09-18 01:22:33 +03:00
nhlog::ui()->debug("Profile requested");
2021-05-15 00:35:34 +03:00
2021-09-18 01:22:33 +03:00
return currentUser_.get();
2021-05-15 00:35:34 +03:00
}
2021-05-30 13:41:44 +03:00
void
Nheko::showUserSettingsPage() const
{
2021-09-18 01:22:33 +03:00
ChatPage::instance()->showUserSettingsPage();
2021-05-30 13:41:44 +03:00
}
void
2021-09-30 03:15:25 +03:00
Nheko::logout() const
2021-05-30 13:41:44 +03:00
{
2021-09-30 03:15:25 +03:00
ChatPage::instance()->initiateLogout();
2021-05-30 13:41:44 +03:00
}
void
Nheko::openCreateRoomDialog() const
{
2021-09-18 01:22:33 +03:00
MainWindow::instance()->openCreateRoomDialog(
[](const mtx::requests::CreateRoom &req) { ChatPage::instance()->createRoom(req); });
2021-05-30 13:41:44 +03:00
}
void
Nheko::reparent(QWindow *win) const
{
2021-09-18 01:22:33 +03:00
win->setTransientParent(MainWindow::instance()->windowHandle());
}