SystemComponent.cpp 12 KB

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