SystemComponent.cpp 14 KB

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