SystemComponent.cpp 11 KB

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