SystemComponent.cpp 9.8 KB

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