SystemComponent.cpp 9.4 KB

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