SysInfo.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "HyperionConfig.h"
  2. #if defined(ENABLE_EFFECTENGINE)
  3. // Python includes
  4. #include <Python.h>
  5. #endif
  6. #include "utils/SysInfo.h"
  7. #include "utils/FileUtils.h"
  8. #include <QHostInfo>
  9. #include <QSysInfo>
  10. #include <QRegularExpression>
  11. #include <QRegularExpressionMatch>
  12. #include <iostream>
  13. #ifndef _WIN32
  14. #include <unistd.h>
  15. #endif
  16. #ifdef _WIN32
  17. #include <shlobj_core.h>
  18. #endif
  19. std::unique_ptr<SysInfo> SysInfo::_instance = nullptr;
  20. SysInfo::SysInfo()
  21. : QObject()
  22. {
  23. _sysinfo.kernelType = QSysInfo::kernelType();
  24. _sysinfo.kernelVersion = QSysInfo::kernelVersion();
  25. _sysinfo.architecture = QSysInfo::currentCpuArchitecture();
  26. _sysinfo.wordSize = QString::number(QSysInfo::WordSize);
  27. _sysinfo.productType = QSysInfo::productType();
  28. _sysinfo.productVersion = QSysInfo::productVersion();
  29. _sysinfo.prettyName = QSysInfo::prettyProductName();
  30. _sysinfo.hostName = QHostInfo::localHostName();
  31. _sysinfo.domainName = QHostInfo::localDomainName();
  32. _sysinfo.isUserAdmin = isUserAdmin();
  33. getCPUInfo();
  34. _sysinfo.qtVersion = QT_VERSION_STR;
  35. #if defined(ENABLE_EFFECTENGINE)
  36. _sysinfo.pyVersion = PY_VERSION;
  37. #endif
  38. }
  39. SysInfo::HyperionSysInfo SysInfo::get()
  40. {
  41. if (SysInfo::_instance == nullptr)
  42. SysInfo::_instance = std::unique_ptr<SysInfo>(new SysInfo());
  43. return SysInfo::_instance->_sysinfo;
  44. }
  45. void SysInfo::getCPUInfo()
  46. {
  47. QString cpuInfo;
  48. if (FileUtils::readFile("/proc/cpuinfo", cpuInfo, Logger::getInstance("DAEMON"), true))
  49. {
  50. QRegularExpression regEx("^model\\s*:\\s(.*)", QRegularExpression::CaseInsensitiveOption | QRegularExpression::MultilineOption);
  51. QRegularExpressionMatch match;
  52. match = regEx.match(cpuInfo);
  53. if (match.hasMatch())
  54. {
  55. _sysinfo.cpuModelType = match.captured(1);
  56. }
  57. regEx.setPattern("^model name\\s*:\\s(.*)");
  58. match = regEx.match(cpuInfo);
  59. if (match.hasMatch())
  60. {
  61. _sysinfo.cpuModelName = match.captured(1);
  62. }
  63. regEx.setPattern("^hardware\\s*:\\s(.*)");
  64. match = regEx.match(cpuInfo);
  65. if (match.hasMatch())
  66. {
  67. _sysinfo.cpuHardware = match.captured(1);
  68. }
  69. regEx.setPattern("^revision\\s*:\\s(.*)");
  70. match = regEx.match(cpuInfo);
  71. if (match.hasMatch())
  72. {
  73. _sysinfo.cpuRevision = match.captured(1);
  74. }
  75. regEx.setPattern("^revision\\s*:\\s(.*)");
  76. match = regEx.match(cpuInfo);
  77. if (match.hasMatch())
  78. {
  79. _sysinfo.cpuRevision = match.captured(1);
  80. }
  81. }
  82. }
  83. QString SysInfo::userName()
  84. {
  85. #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
  86. #if defined (WIN32)
  87. return qEnvironmentVariable("USERNAME");
  88. #else
  89. return qEnvironmentVariable("USER");
  90. #endif
  91. #else
  92. #if defined (WIN32)
  93. return getenv("USERNAME");
  94. #else
  95. return getenv("USER");
  96. #endif
  97. #endif
  98. }
  99. bool SysInfo::isUserAdmin()
  100. {
  101. bool isAdmin{ false };
  102. #ifdef _WIN32
  103. BOOL b;
  104. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  105. PSID AdministratorsGroup;
  106. b = AllocateAndInitializeSid(
  107. &NtAuthority,
  108. 2,
  109. SECURITY_BUILTIN_DOMAIN_RID,
  110. DOMAIN_ALIAS_RID_ADMINS,
  111. 0, 0, 0, 0, 0, 0,
  112. &AdministratorsGroup);
  113. if (b)
  114. {
  115. if (!CheckTokenMembership(NULL, AdministratorsGroup, &b))
  116. {
  117. b = false;
  118. }
  119. FreeSid(AdministratorsGroup);
  120. }
  121. // TRUE - User has Administrators local group.
  122. // FALSE - User does not have Administrators local group.
  123. isAdmin = b;
  124. #else
  125. if (getuid() == 0U)
  126. {
  127. isAdmin = true;
  128. }
  129. #endif
  130. return isAdmin;
  131. }