SystemComponent.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #include <QSysInfo>
  2. #include <QProcess>
  3. #include <QMap>
  4. #include <QtNetwork/qnetworkinterface.h>
  5. #include <QGuiApplication>
  6. #include <QDesktopServices>
  7. #include "SystemComponent.h"
  8. #include "Version.h"
  9. #include "QsLog.h"
  10. #include "settings/SettingsComponent.h"
  11. #include "ui/KonvergoWindow.h"
  12. #include "Paths.h"
  13. #include "Names.h"
  14. #define MOUSE_TIMEOUT 5 * 1000
  15. #define KONVERGO_PRODUCTID_DEFAULT 3
  16. #define KONVERGO_PRODUCTID_OPENELEC 4
  17. // Platform types map
  18. QMap<SystemComponent::PlatformType, QString> platformTypeNames = { \
  19. { SystemComponent::platformTypeOsx, "macosx" }, \
  20. { SystemComponent::platformTypeWindows, "windows" },
  21. { SystemComponent::platformTypeLinux, "linux" },
  22. { SystemComponent::platformTypeUnknown, "unknown" },
  23. };
  24. // platform Archictecture map
  25. QMap<SystemComponent::PlatformArch, QString> platformArchNames = { \
  26. { SystemComponent::platformArchX86, "x86" }, \
  27. { SystemComponent::platformArchRpi2, "rpi2" },
  28. { SystemComponent::platformArchUnknown, "unknown" }
  29. };
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////
  31. SystemComponent::SystemComponent(QObject* parent) : ComponentBase(parent), m_platformType(platformTypeUnknown), m_platformArch(platformArchUnknown), m_doLogMessages(false)
  32. {
  33. m_mouseOutTimer = new QTimer(this);
  34. m_mouseOutTimer->setSingleShot(true);
  35. connect(m_mouseOutTimer, &QTimer::timeout, [&] () { setCursorVisibility(false); });
  36. m_mouseOutTimer->start(MOUSE_TIMEOUT);
  37. // define OS Type
  38. #if defined(Q_OS_MAC)
  39. m_platformType = platformTypeOsx;
  40. #elif defined(Q_OS_WIN)
  41. m_platformType = platformTypeWindows;
  42. #elif defined(Q_OS_LINUX)
  43. m_platformType = platformTypeLinux;
  44. #endif
  45. // define target type
  46. #if TARGET_RPI
  47. m_platformArch = platformArchRpi2;
  48. #else
  49. m_platformArch = platformArchX86;
  50. #endif
  51. #if KONVERGO_OPENELEC
  52. m_platformModfiers << SYSTEM_MODIFIER_OPENELEC;
  53. #endif
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. QString SystemComponent::getPlatformTypeString() const
  57. {
  58. return platformTypeNames[m_platformType];
  59. }
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////
  61. QString SystemComponent::getPlatformArchString() const
  62. {
  63. return platformArchNames[m_platformArch];
  64. }
  65. ///////////////////////////////////////////////////////////////////////////////////////////////////
  66. QVariantMap SystemComponent::systemInformation() const
  67. {
  68. QVariantMap info;
  69. QString build;
  70. QString dist;
  71. QString arch;
  72. int productid = KONVERGO_PRODUCTID_DEFAULT;
  73. #ifdef Q_OS_WIN
  74. arch = "x86";
  75. #else
  76. arch = QSysInfo::currentCpuArchitecture();
  77. #endif
  78. build = getPlatformTypeString();
  79. dist = getPlatformTypeString();
  80. #if defined(KONVERGO_OPENELEC)
  81. productid = KONVERGO_PRODUCTID_OPENELEC;
  82. dist = "openelec";
  83. if (m_platformArch == platformArchRpi2)
  84. {
  85. build = "rpi2";
  86. }
  87. else
  88. {
  89. build = "generic";
  90. }
  91. #endif
  92. info["build"] = build + "-" + arch;
  93. info["dist"] = dist;
  94. info["version"] = Version::GetVersionString();
  95. info["productid"] = productid;
  96. QLOG_DEBUG() << QString(
  97. "System Information : build(%1)-arch(%2).dist(%3).version(%4).productid(%5)")
  98. .arg(build)
  99. .arg(arch)
  100. .arg(dist)
  101. .arg(Version::GetVersionString())
  102. .arg(productid);
  103. return info;
  104. }
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////
  106. void SystemComponent::exit()
  107. {
  108. qApp->quit();
  109. }
  110. ///////////////////////////////////////////////////////////////////////////////////////////////////
  111. void SystemComponent::restart()
  112. {
  113. qApp->quit();
  114. QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
  115. }
  116. ///////////////////////////////////////////////////////////////////////////////////////////////////
  117. void SystemComponent::info(QString text)
  118. {
  119. if (QsLogging::Logger::instance().loggingLevel() <= QsLogging::InfoLevel)
  120. QsLogging::Logger::Helper(QsLogging::InfoLevel).stream() << "JS:" << qPrintable(text);
  121. }
  122. ///////////////////////////////////////////////////////////////////////////////////////////////////
  123. void SystemComponent::setCursorVisibility(bool visible)
  124. {
  125. if (visible)
  126. {
  127. m_mouseOutTimer->start(MOUSE_TIMEOUT);
  128. while (qApp->overrideCursor())
  129. qApp->restoreOverrideCursor();
  130. }
  131. else
  132. {
  133. if (!qApp->overrideCursor())
  134. {
  135. if (m_mouseOutTimer->isActive())
  136. m_mouseOutTimer->stop();
  137. qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
  138. }
  139. }
  140. }
  141. ///////////////////////////////////////////////////////////////////////////////////////////////////
  142. QString SystemComponent::getUserAgent()
  143. {
  144. QString userAgent = QString("Konvergo-%1-%2 %3").arg(getPlatformTypeString())
  145. .arg(getPlatformArchString())
  146. .arg(Version::GetVersionString());
  147. if (m_platformModfiers.size())
  148. userAgent += QString(" (%1)").arg(m_platformModfiers.join(','));
  149. return userAgent;
  150. }
  151. #ifdef Q_OS_UNIX
  152. /////////////////////////////////////////////////////////////////////////////////////////
  153. QMap<QString, QString> SystemComponent::networkInterfaces()
  154. {
  155. QMap<QString, QString> info;
  156. foreach(const QNetworkInterface& interface, QNetworkInterface::allInterfaces())
  157. {
  158. if (interface.isValid() == false || (interface.hardwareAddress().isEmpty() == true) || interface.IsLoopBack == false)
  159. continue;
  160. QString interfaceName = QString("Interface%1 ").arg(interface.index());
  161. info[interfaceName + "Name"] = interface.humanReadableName();
  162. info[interfaceName + "HW address"] = interface.hardwareAddress();
  163. info[interfaceName + "Status"] = (interface.flags() & QNetworkInterface::IsUp) ? "Up" : "Down";
  164. int i = 0;
  165. foreach(const QNetworkAddressEntry& address, interface.addressEntries())
  166. {
  167. info[interfaceName + QString("Address%1").arg(i)] = QString("%1/%2").arg(address.ip().toString()).arg(address.netmask().toString());
  168. i++;
  169. }
  170. }
  171. return info;
  172. }
  173. #endif
  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 << " Platform: " << getPlatformTypeString() << "-" << getPlatformArchString() << endl;
  183. stream << " Qt version: " << qVersion() << endl;
  184. stream << endl;
  185. stream << "Files" << endl;
  186. stream << " Log file: " << Paths::logDir(Names::MainName() + ".log") << endl;
  187. stream << " Config file: " << Paths::dataDir(Names::MainName() + ".conf") << endl;
  188. stream << endl;
  189. #ifdef Q_OS_UNIX
  190. stream << "Network" << endl;
  191. QMap<QString, QString> networks = networkInterfaces();
  192. foreach(const QString& net, networks.keys())
  193. stream << " " << net << ": " << networks[net] << endl;
  194. #endif
  195. stream << flush;
  196. return debugInfo;
  197. }
  198. /////////////////////////////////////////////////////////////////////////////////////////
  199. int SystemComponent::networkPort() const
  200. {
  201. return SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "webserverport").toInt();
  202. }
  203. /////////////////////////////////////////////////////////////////////////////////////////
  204. QStringList SystemComponent::networkAddresses() const
  205. {
  206. QStringList list;
  207. foreach(const QHostAddress& address, QNetworkInterface::allAddresses())
  208. {
  209. if (! address.isLoopback() && (address.protocol() == QAbstractSocket::IPv4Protocol))
  210. list << address.toString();
  211. }
  212. return list;
  213. }
  214. /////////////////////////////////////////////////////////////////////////////////////////
  215. void SystemComponent::userInformation(const QVariantMap& userModel)
  216. {
  217. QStringList roleList;
  218. auto roles = userModel.value("roles").toMap();
  219. foreach (const QString& key, roles.keys())
  220. {
  221. if (roles.value(key).toBool())
  222. roleList << key;
  223. }
  224. SettingsComponent::Get().setUserRoleList(roleList);
  225. }
  226. /////////////////////////////////////////////////////////////////////////////////////////
  227. void SystemComponent::openExternalUrl(const QString& url)
  228. {
  229. QDesktopServices::openUrl(QUrl(url));
  230. }