matrixion/src/ui/NhekoGlobalObject.cpp

150 lines
4.7 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"
2021-05-13 09:23:56 +03:00
Nheko::Nheko()
{
connect(
UserSettings::instance().get(), &UserSettings::themeChanged, this, &Nheko::colorsChanged);
2021-05-15 00:35:34 +03:00
connect(ChatPage::instance(), &ChatPage::contentLoaded, this, &Nheko::updateUserProfile);
}
void
Nheko::updateUserProfile()
{
if (cache::client() && cache::client()->isInitialized())
currentUser_.reset(
new UserProfile("", utils::localUser(), ChatPage::instance()->timelineManager()));
else
currentUser_.reset();
emit profileChanged();
2021-05-13 09:23:56 +03:00
}
QPalette
Nheko::colors() const
{
2021-05-15 00:35:34 +03:00
return Theme::paletteFromTheme(UserSettings::instance()->theme().toStdString());
2021-05-13 09:23:56 +03:00
}
QPalette
Nheko::inactiveColors() const
{
2021-05-15 00:35:34 +03:00
auto p = colors();
2021-05-13 09:23:56 +03:00
p.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
return p;
}
2021-05-13 09:52:02 +03:00
2021-05-15 00:35:34 +03:00
Theme
Nheko::theme() const
{
return Theme(UserSettings::instance()->theme().toStdString());
}
2021-05-13 09:52:02 +03:00
void
Nheko::openLink(QString link) const
{
QUrl url(link);
if (url.scheme() == "https" && url.host() == "matrix.to") {
// handle matrix.to links internally
QString p = url.fragment(QUrl::FullyEncoded);
if (p.startsWith("/"))
p.remove(0, 1);
auto temp = p.split("?");
QString query;
if (temp.size() >= 2)
query = QUrl::fromPercentEncoding(temp.takeAt(1).toUtf8());
temp = temp.first().split("/");
auto identifier = QUrl::fromPercentEncoding(temp.takeFirst().toUtf8());
QString eventId = QUrl::fromPercentEncoding(temp.join('/').toUtf8());
if (!identifier.isEmpty()) {
if (identifier.startsWith("@")) {
QByteArray uri =
"matrix:u/" + QUrl::toPercentEncoding(identifier.remove(0, 1));
if (!query.isEmpty())
uri.append("?" + query.toUtf8());
ChatPage::instance()->handleMatrixUri(QUrl::fromEncoded(uri));
} else if (identifier.startsWith("#")) {
QByteArray uri =
"matrix:r/" + QUrl::toPercentEncoding(identifier.remove(0, 1));
if (!eventId.isEmpty())
uri.append("/e/" +
QUrl::toPercentEncoding(eventId.remove(0, 1)));
if (!query.isEmpty())
uri.append("?" + query.toUtf8());
ChatPage::instance()->handleMatrixUri(QUrl::fromEncoded(uri));
} else if (identifier.startsWith("!")) {
QByteArray uri = "matrix:roomid/" +
QUrl::toPercentEncoding(identifier.remove(0, 1));
if (!eventId.isEmpty())
uri.append("/e/" +
QUrl::toPercentEncoding(eventId.remove(0, 1)));
if (!query.isEmpty())
uri.append("?" + query.toUtf8());
ChatPage::instance()->handleMatrixUri(QUrl::fromEncoded(uri));
}
}
} else {
QDesktopServices::openUrl(url);
}
}
2021-05-22 16:19:44 +03:00
void
Nheko::setStatusMessage(QString msg) const
{
ChatPage::instance()->setStatus(msg);
}
2021-05-15 00:35:34 +03:00
UserProfile *
Nheko::currentUser() const
{
nhlog::ui()->debug("Profile requested");
return currentUser_.get();
}
2021-05-30 13:41:44 +03:00
void
Nheko::showUserSettingsPage() const
{
ChatPage::instance()->showUserSettingsPage();
}
void
Nheko::openLogoutDialog() const
{
MainWindow::instance()->openLogoutDialog();
}
void
Nheko::openCreateRoomDialog() const
{
MainWindow::instance()->openCreateRoomDialog(
[](const mtx::requests::CreateRoom &req) { ChatPage::instance()->createRoom(req); });
}
void
Nheko::openJoinRoomDialog() const
{
MainWindow::instance()->openJoinRoomDialog(
[](const QString &room_id) { ChatPage::instance()->joinRoom(room_id); });
}
void
Nheko::reparent(QWindow *win) const
{
win->setTransientParent(MainWindow::instance()->windowHandle());
}