-
-
Notifications
You must be signed in to change notification settings - Fork 323
/
SystemComponent.cpp
416 lines (357 loc) · 14.1 KB
/
SystemComponent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <QSysInfo>
#include <QProcess>
#include <QMap>
#include <QtNetwork/qnetworkinterface.h>
#include <QGuiApplication>
#include <QDesktopServices>
#include <QDir>
#include <QJsonObject>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QDebug>
#include "input/InputComponent.h"
#include "SystemComponent.h"
#include "Version.h"
#include "settings/SettingsComponent.h"
#include "ui/KonvergoWindow.h"
#include "settings/SettingsSection.h"
#include "Paths.h"
#include "Names.h"
#include "utils/Utils.h"
#include "utils/Log.h"
#define MOUSE_TIMEOUT 5 * 1000
#define KONVERGO_PRODUCTID_DEFAULT 3
#define KONVERGO_PRODUCTID_OPENELEC 4
// Platform types map
QMap<SystemComponent::PlatformType, QString> g_platformTypeNames = { \
{ SystemComponent::platformTypeOsx, "macosx" }, \
{ SystemComponent::platformTypeWindows, "windows" },
{ SystemComponent::platformTypeLinux, "linux" },
{ SystemComponent::platformTypeOpenELEC, "openelec" },
{ SystemComponent::platformTypeUnknown, "unknown" },
};
// platform Archictecture map
QMap<SystemComponent::PlatformArch, QString> g_platformArchNames = {
{ SystemComponent::platformArchX86_32, "i386" },
{ SystemComponent::platformArchX86_64, "x86_64" },
{ SystemComponent::platformArchRpi2, "rpi2" },
{ SystemComponent::platformArchUnknown, "unknown" }
};
///////////////////////////////////////////////////////////////////////////////////////////////////
SystemComponent::SystemComponent(QObject* parent) : ComponentBase(parent), m_platformType(platformTypeUnknown), m_platformArch(platformArchUnknown), m_doLogMessages(false), m_cursorVisible(true), m_scale(1)
{
m_mouseOutTimer = new QTimer(this);
m_mouseOutTimer->setSingleShot(true);
connect(m_mouseOutTimer, &QTimer::timeout, [&] () { setCursorVisibility(false); });
// define OS Type
#if defined(Q_OS_MAC)
m_platformType = platformTypeOsx;
#elif defined(Q_OS_WIN)
m_platformType = platformTypeWindows;
#elif defined(KONVERGO_OPENELEC)
m_platformType = platformTypeOpenELEC;
#elif defined(Q_OS_LINUX)
m_platformType = platformTypeLinux;
#endif
// define target type
#if TARGET_RPI
m_platformArch = platformArchRpi2;
#elif defined(Q_PROCESSOR_X86_32)
m_platformArch = platformArchX86_32;
#elif defined(Q_PROCESSOR_X86_64)
m_platformArch = platformArchX86_64;
#endif
connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_AUDIO), &SettingsSection::valuesUpdated, [=]()
{
emit capabilitiesChanged(getCapabilitiesString());
});
}
/////////////////////////////////////////////////////////////////////////////////////////
bool SystemComponent::componentInitialize()
{
QDir().mkpath(Paths::dataDir("scripts"));
QDir().mkpath(Paths::dataDir("sounds"));
// Hide mouse pointer on any keyboard input
connect(&InputComponent::Get(), &InputComponent::receivedInput, [=]() { setCursorVisibility(false); });
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::crashApp()
{
*(volatile int*)nullptr=0;
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::componentPostInitialize()
{
InputComponent::Get().registerHostCommand("crash!", this, "crashApp");
InputComponent::Get().registerHostCommand("script", this, "runUserScript");
InputComponent::Get().registerHostCommand("message", this, "hostMessage");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
QString SystemComponent::getPlatformTypeString() const
{
return g_platformTypeNames[m_platformType];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
QString SystemComponent::getPlatformArchString() const
{
return g_platformArchNames[m_platformArch];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
QVariantMap SystemComponent::systemInformation() const
{
QVariantMap info;
QString build;
QString dist;
QString arch;
int productid = KONVERGO_PRODUCTID_DEFAULT;
#ifdef Q_OS_WIN
arch = (sizeof(void *) == 8) ? "x86_64" : "i386";
#else
arch = QSysInfo::currentCpuArchitecture();
#endif
build = getPlatformTypeString();
dist = getPlatformTypeString();
#if defined(KONVERGO_OPENELEC)
productid = KONVERGO_PRODUCTID_OPENELEC;
dist = "openelec";
if (m_platformArch == platformArchRpi2)
{
build = "rpi2";
}
else
{
build = "generic";
}
#endif
info["build"] = build + "-" + arch;
info["dist"] = dist;
info["version"] = Version::GetVersionString();
info["productid"] = productid;
qDebug() << QString(
"System Information : build(%1)-arch(%2).dist(%3).version(%4).productid(%5)")
.arg(build)
.arg(arch)
.arg(dist)
.arg(Version::GetVersionString())
.arg(productid);
return info;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::exit()
{
qApp->quit();
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::restart()
{
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::info(QString text)
{
if (Log::ShouldLogInfo())
qInfo() << "JS:" << qPrintable(text);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::setCursorVisibility(bool visible)
{
if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "webMode") == "desktop")
visible = true;
if (visible == m_cursorVisible)
return;
m_cursorVisible = visible;
if (visible)
{
qApp->restoreOverrideCursor();
m_mouseOutTimer->start(MOUSE_TIMEOUT);
}
else
{
qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
m_mouseOutTimer->stop();
}
#ifdef Q_OS_MAC
// OSX notifications will reset the cursor image (without Qt's knowledge). The
// only thing we can do override this is using Cocoa's native cursor hiding.
OSXUtils::SetCursorVisible(visible);
#endif
}
///////////////////////////////////////////////////////////////////////////////////////////////////
QString SystemComponent::getUserAgent()
{
QString osVersion = QSysInfo::productVersion();
QString userAgent = QString("JellyfinMediaPlayer %1 (%2-%3 %4)").arg(Version::GetVersionString()).arg(getPlatformTypeString()).arg(getPlatformArchString()).arg(osVersion);
return userAgent;
}
/////////////////////////////////////////////////////////////////////////////////////////
QString SystemComponent::debugInformation()
{
QString debugInfo;
QTextStream stream(&debugInfo);
stream << "Jellyfin Media Player\n";
stream << " Version: " << Version::GetVersionString() << " built: " << Version::GetBuildDate() << "\n";
stream << " Web Client Version: " << Version::GetWebVersion() << "\n";
stream << " Web Client URL: " << SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "startupurl").toString() << "\n";
stream << " Platform: " << getPlatformTypeString() << "-" << getPlatformArchString() << "\n";
stream << " User-Agent: " << getUserAgent() << "\n";
stream << " Qt version: " << qVersion() << QString("(%1)").arg(Version::GetQtDepsVersion()) << "\n";
stream << " Depends version: " << Version::GetDependenciesVersion() << "\n";
stream << "\n";
stream << "Files\n";
stream << " Log file: " << Paths::logDir(Names::MainName() + ".log") << "\n";
stream << " Config file: " << Paths::dataDir(Names::MainName() + ".conf") << "\n";
stream << "\n";
stream << "Network Addresses\n";
for(const QString& addr : networkAddresses())
{
stream << " " << addr << "\n";
}
stream << "\n";
stream.flush();
return debugInfo;
}
/////////////////////////////////////////////////////////////////////////////////////////
QStringList SystemComponent::networkAddresses() const
{
QStringList list;
for(const QHostAddress& address : QNetworkInterface::allAddresses())
{
if (! address.isLoopback() && (address.protocol() == QAbstractSocket::IPv4Protocol ||
address.protocol() == QAbstractSocket::IPv6Protocol))
{
auto s = address.toString();
if (!s.startsWith("fe80::"))
list << s;
}
}
return list;
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::openExternalUrl(const QString& url)
{
QDesktopServices::openUrl(QUrl(url));
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::runUserScript(QString script)
{
// We take the path the user supplied and run it through fileInfo and
// look for the fileName() part, this is to avoid people sharing keymaps
// that tries to execute things like ../../ etc. Note that this function
// is still not safe, people can do nasty things with it, so users needs
// to be careful with their keymaps.
//
QFileInfo fi(script);
QString scriptPath = Paths::dataDir("scripts/" + fi.fileName());
QFile scriptFile(scriptPath);
if (scriptFile.exists())
{
if (!QFileInfo(scriptFile).isExecutable())
{
qWarning() << "Script:" << script << "is not executable";
return;
}
qInfo() << "Running script:" << scriptPath;
if (QProcess::startDetached(scriptPath, QStringList()))
qDebug() << "Script started successfully";
else
qWarning() << "Error running script:" << scriptPath;
}
else
{
qWarning() << "Could not find script:" << scriptPath;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::hello(const QString& version)
{
qDebug() << QString("Web-client (%1) fully inited.").arg(version);
m_webClientVersion = version;
}
/////////////////////////////////////////////////////////////////////////////////////////
QString SystemComponent::getNativeShellScript()
{
auto path = SettingsComponent::Get().getExtensionPath();
qDebug() << QString("Using path for extension: %1").arg(path);
QFile file {path + "nativeshell.js"};
file.open(QIODevice::ReadOnly);
auto nativeshellString = QTextStream(&file).readAll();
QJsonObject clientData;
clientData.insert("deviceName", QJsonValue::fromVariant(SettingsComponent::Get().getClientName()));
clientData.insert("scriptPath", QJsonValue::fromVariant("file:///" + path));
QString defaultMode = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "layout").toString();
QFile flatpakOsFile {"/run/host/os-release"};
if (flatpakOsFile.exists()) {
qDebug() << "Found flatpak os-release file";
flatpakOsFile.open(QIODevice::ReadOnly);
QString flatpakOsFileString = QTextStream(&flatpakOsFile).readAll();
if (flatpakOsFileString.contains("NAME=\"SteamOS\"")) {
qDebug() << "Detected SteamOS";
defaultMode = "tv";
}
}
clientData.insert("mode", QJsonValue::fromVariant(defaultMode));
QVariantList settingsDescriptionsList = SettingsComponent::Get().settingDescriptions();
QVariantMap settingsDescriptions = QVariantMap();
for (auto setting : settingsDescriptionsList) {
QVariantMap settingMap = setting.toMap();
settingsDescriptions.insert(settingMap["key"].toString(), settingMap["settings"]);
}
clientData.insert("sections", QJsonValue::fromVariant(SettingsComponent::Get().orderedSections()));
clientData.insert("settingsDescriptions", QJsonValue::fromVariant(settingsDescriptions));
clientData.insert("settings", QJsonValue::fromVariant(SettingsComponent::Get().allValues()));
nativeshellString.replace("@@data@@", QJsonDocument(clientData).toJson(QJsonDocument::Compact).toBase64());
return nativeshellString;
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::checkForUpdates()
{
if (SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "checkForUpdates").toBool()) {
#if !defined(Q_OS_WIN) && !defined(Q_OS_MAC)
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
QString checkUrl = "https://github.com/jellyfin/jellyfin-media-player/releases/latest";
QUrl qCheckUrl = QUrl(checkUrl);
qDebug() << QString("Checking URL for updates: %1").arg(checkUrl);
QNetworkRequest req(qCheckUrl);
connect(manager, &QNetworkAccessManager::finished, this, &SystemComponent::updateInfoHandler);
manager->get(req);
#else
emit updateInfoEmitted("SSL_UNAVAILABLE");
#endif
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void SystemComponent::updateInfoHandler(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError) {
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode == 302) {
QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
emit updateInfoEmitted(redirectUrl.toString());
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
#define BASESTR "protocols=shoutcast,http-video;videoDecoders=h264{profile:high&resolution:2160&level:52};audioDecoders=mp3,aac,dts{bitrate:800000&channels:%1},ac3{bitrate:800000&channels:%2}"
/////////////////////////////////////////////////////////////////////////////////////////
QString SystemComponent::getCapabilitiesString()
{
auto capstring = QString(BASESTR);
auto channels = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "channels").toString();
auto dtsenabled = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "passthrough.dts").toBool();
auto ac3enabled = SettingsComponent::Get().value(SETTINGS_SECTION_AUDIO, "passthrough.ac3").toBool();
// Assume that auto means that we want to select multi-channel tracks by default.
// So really only disable it when 2.0 is selected.
//
int ac3channels = 2;
int dtschannels = 2;
if (channels != "2.0")
dtschannels = ac3channels = 8;
else if (dtsenabled)
dtschannels = 8;
else if (ac3enabled)
ac3channels = 8;
return capstring.arg(dtschannels).arg(ac3channels);
}