Debian Codegraph @

src/ui/NhekoGlobalObject.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #include "NhekoGlobalObject.h"
6
7 #include <QApplication>
8 #include <QDesktopServices>
9 #include <QGuiApplication>
10 #include <QStyle>
11 #include <QUrl>
12 #include <QWindow>
13
14 #include <mtx/requests.hpp>
15
16 #include "Cache_p.h"
17 #include "ChatPage.h"
18 #include "Logging.h"
19 #include "UserSettingsPage.h"
20 #include "Utils.h"
21
22 #if XCB_AVAILABLE && QT_CONFIG(xcb)
23 #include <xcb/xproto.h>
24 #endif
25
26 Nheko::Nheko()
27 {
28 connect(
29 UserSettings::instance().get(), &UserSettings::themeChanged, this, &Nheko::colorsChanged);
30 connect(ChatPage::instance(), &ChatPage::contentLoaded, this, &Nheko::updateUserProfile);
31 connect(ChatPage::instance(), &ChatPage::showRoomJoinPrompt, this, &Nheko::showRoomJoinPrompt);
32 connect(this, &Nheko::joinRoom, ChatPage::instance(), &ChatPage::joinRoom);
33 }
34
35 void
36 Nheko::updateUserProfile()
37 {
38 if (cache::client() && cache::client()->isInitialized())
39 currentUser_.reset(new UserProfile(
40 QLatin1String(""), utils::localUser(), ChatPage::instance()->timelineManager()));
41 else
42 currentUser_.reset();
43 emit profileChanged();
44 }
45
46 QPalette
47 Nheko::colors() const
48 {
49 return Theme::paletteFromTheme(UserSettings::instance()->theme());
50 }
51
52 QPalette
53 Nheko::inactiveColors() const
54 {
55 auto theme = UserSettings::instance()->theme();
56 if (theme == QLatin1String("light")) {
57 static QPalette lightInactive = [] {
58 auto lightInactive = Theme::paletteFromTheme(u"light");
59 lightInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
60 return lightInactive;
61 }();
62 return lightInactive;
63 } else if (theme == QLatin1String("dark")) {
64 static QPalette darkInactive = [] {
65 auto darkInactive = Theme::paletteFromTheme(u"dark");
66 darkInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
67 return darkInactive;
68 }();
69 return darkInactive;
70 } else {
71 static QPalette originalInactive = [] {
72 auto originalInactive = Theme::paletteFromTheme(u"system");
73 originalInactive.setCurrentColorGroup(QPalette::ColorGroup::Inactive);
74 return originalInactive;
75 }();
76 return originalInactive;
77 }
78 }
79
80 Theme
81 Nheko::theme() const
82 {
83 return Theme(UserSettings::instance()->theme());
84 }
85
86 int
87 Nheko::tooltipDelay() const
88 {
89 return QApplication::style()->styleHint(QStyle::StyleHint::SH_ToolTip_WakeUpDelay);
90 }
91
92 void
93 Nheko::openLink(QString link) const
94 {
95 QUrl url(link);
96 // Open externally if we couldn't handle it internally
97 if (!ChatPage::instance()->handleMatrixUri(url)) {
98 static const QStringList allowedUrlSchemes = {
99 QStringLiteral("http"),
100 QStringLiteral("https"),
101 QStringLiteral("mailto"),
102 };
103
104 if (allowedUrlSchemes.contains(url.scheme()))
105 QDesktopServices::openUrl(url);
106 else
107 nhlog::ui()->warn("Url '{}' not opened, because the scheme is not in the allow list",
108 url.toDisplayString().toStdString());
109 }
110 }
111 QString
112 Nheko::punyLink(QString link) const
113 {
114 QUrl url(link);
115 return url.toDisplayString(QUrl::FullyEncoded);
116 }
117 void
118 Nheko::setStatusMessage(QString msg) const
119 {
120 ChatPage::instance()->setStatus(msg);
121 }
122
123 UserProfile *
124 Nheko::currentUser() const
125 {
126 nhlog::ui()->debug("Profile requested");
127
128 return currentUser_.get();
129 }
130
131 void
132 Nheko::showUserSettingsPage() const
133 {
134 ChatPage::instance()->showUserSettingsPage();
135 }
136
137 void
138 Nheko::logout() const
139 {
140 ChatPage::instance()->initiateLogout();
141 }
142
143 void
144 Nheko::setTransientParent(QWindow *window, QWindow *parentWindow) const
145 {
146 if (window)
147 window->setTransientParent(parentWindow);
148 }
149
150 void
151 Nheko::createRoom(bool space,
152 const QString &name,
153 const QString &topic,
154 const QString &aliasLocalpart,
155 bool isEncrypted,
156 int preset)
157 {
158 mtx::requests::CreateRoom req;
159
160 if (space) {
161 req.creation_content = mtx::events::state::Create{};
162 req.creation_content->type = mtx::events::state::room_type::space;
163 req.creation_content->room_version.clear();
164 }
165
166 switch (preset) {
167 case 1:
168 req.preset = mtx::requests::Preset::PublicChat;
169 break;
170 case 2:
171 req.preset = mtx::requests::Preset::TrustedPrivateChat;
172 break;
173 case 0:
174 default:
175 req.preset = mtx::requests::Preset::PrivateChat;
176 }
177
178 req.name = name.toStdString();
179 req.topic = topic.toStdString();
180 req.room_alias_name = aliasLocalpart.toStdString();
181
182 if (isEncrypted) {
183 mtx::events::StrippedEvent<mtx::events::state::Encryption> enc;
184 enc.type = mtx::events::EventType::RoomEncryption;
185 enc.content.algorithm = mtx::crypto::MEGOLM_ALGO;
186 req.initial_state.emplace_back(std::move(enc));
187 }
188
189 emit ChatPage::instance()->createRoom(req);
190 }
191
192 void
193 Nheko::setWindowRole([[maybe_unused]] QWindow *win, [[maybe_unused]] QString newRole) const
194 {
195 #if XCB_AVAILABLE && QT_CONFIG(xcb)
196 const QNativeInterface::QX11Application *x11Interface =
197 qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
198
199 if (!x11Interface)
200 return;
201
202 auto connection = x11Interface->connection();
203
204 auto role = newRole.toStdString();
205
206 char WM_WINDOW_ROLE[] = "WM_WINDOW_ROLE";
207 auto cookie = xcb_intern_atom(connection, false, std::size(WM_WINDOW_ROLE) - 1, WM_WINDOW_ROLE);
208 xcb_intern_atom_reply_t *reply = xcb_intern_atom_reply(connection, cookie, nullptr);
209 auto atom = reply->atom;
210 free(reply);
211
212 xcb_change_property(connection,
213 XCB_PROP_MODE_REPLACE,
214 win->winId(),
215 atom,
216 XCB_ATOM_STRING,
217 8,
218 role.size(),
219 role.data());
220 #endif
221 }
222
223 #include "moc_NhekoGlobalObject.cpp"