SystemComponent.cpp 9.6 KB

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