Utils.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "Utils.h"
  2. #include <QtGlobal>
  3. #include <QStandardPaths>
  4. #include <QByteArray>
  5. #include <QCoreApplication>
  6. #include <QDir>
  7. #include <QProcess>
  8. #include <QDateTime>
  9. #include <QHostInfo>
  10. #include <QJsonDocument>
  11. #include <QVariant>
  12. #include <qnetworkinterface.h>
  13. #include <QUuid>
  14. #include "settings/SettingsComponent.h"
  15. #include "settings/SettingsSection.h"
  16. #include "osx/OSXUtils.h"
  17. #include "QsLog.h"
  18. /////////////////////////////////////////////////////////////////////////////////////////
  19. QString Utils::ComputerName()
  20. {
  21. #ifdef Q_OS_MAC
  22. return OSXUtils::ComputerName();
  23. #else
  24. return QHostInfo::localHostName();
  25. #endif
  26. }
  27. /////////////////////////////////////////////////////////////////////////////////////////
  28. QJsonDocument Utils::OpenJsonDocument(const QString& path, QJsonParseError* err)
  29. {
  30. QFile fp(path);
  31. QByteArray fdata;
  32. QRegExp commentMatch("^\\s*//");
  33. if (fp.open(QFile::ReadOnly))
  34. {
  35. while(true)
  36. {
  37. QByteArray row = fp.readLine();
  38. if (row.isEmpty())
  39. break;
  40. // filter all comments
  41. if (commentMatch.indexIn(row) != -1)
  42. continue;
  43. fdata.append(row);
  44. }
  45. fp.close();
  46. }
  47. return QJsonDocument::fromJson(fdata, err);
  48. }
  49. /////////////////////////////////////////////////////////////////////////////////////////
  50. Platform Utils::CurrentPlatform()
  51. {
  52. #if defined(Q_OS_MAC)
  53. return PLATFORM_OSX;
  54. #elif KONVERGO_OPENELEC
  55. #if TARGET_RPI
  56. return PLATFORM_OE_RPI;
  57. #else
  58. return PLATFORM_OE_X86;
  59. #endif
  60. #elif defined(Q_OS_LINUX)
  61. return PLATFORM_LINUX;
  62. #elif defined(Q_OS_WIN32)
  63. return PLATFORM_WINDOWS;
  64. #else
  65. return PLATFORM_UNKNOWN;
  66. #endif
  67. }
  68. /////////////////////////////////////////////////////////////////////////////////////////
  69. QString Utils::CurrentUserId()
  70. {
  71. SettingsSection* connections = SettingsComponent::Get().getSection("connections");
  72. if (!connections)
  73. return QString();
  74. QVariant ulist = connections->value("users");
  75. if (ulist.isValid())
  76. {
  77. QVariantList users = ulist.toList();
  78. if (users.size() > 0)
  79. {
  80. QVariantMap user = users.at(0).toMap();
  81. return user.value("id").toString();
  82. }
  83. }
  84. return QString();
  85. }
  86. /////////////////////////////////////////////////////////////////////////////////////////
  87. QString Utils::PrimaryIPv4Address()
  88. {
  89. QList<QNetworkInterface> ifs = QNetworkInterface::allInterfaces();
  90. foreach(const QNetworkInterface& iface, ifs)
  91. {
  92. if (iface.isValid() && iface.flags() & QNetworkInterface::IsUp)
  93. {
  94. QList<QHostAddress> addresses = iface.allAddresses();
  95. foreach(const QHostAddress& addr, addresses)
  96. {
  97. if (!addr.isLoopback() && !addr.isMulticast() && addr.protocol() == QAbstractSocket::IPv4Protocol)
  98. {
  99. QLOG_DEBUG() << "I think that" << addr.toString() << "is my primary address";
  100. return addr.toString();
  101. }
  102. }
  103. }
  104. }
  105. return "";
  106. }
  107. /////////////////////////////////////////////////////////////////////////////////////////
  108. QString Utils::ClientUUID()
  109. {
  110. QString storedUUID = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "clientUUID").toString();
  111. if (storedUUID.isEmpty())
  112. {
  113. QString newUUID = QUuid::createUuid().toString();
  114. newUUID = newUUID.replace("{", "").replace("}", "");
  115. SettingsComponent::Get().setValue(SETTINGS_SECTION_MAIN, "clientUUID", newUUID);
  116. return newUUID;
  117. }
  118. return storedUUID;
  119. }