Utils.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "Utils.h"
  2. #include <QtGlobal>
  3. #include <QStandardPaths>
  4. #include <QCoreApplication>
  5. #include <QDir>
  6. #include <QProcess>
  7. #include <QDateTime>
  8. #include <QHostInfo>
  9. #include <QJsonDocument>
  10. #include <QVariant>
  11. #include <qnetworkinterface.h>
  12. #include <QUuid>
  13. #include <QFile>
  14. #include <QSaveFile>
  15. #include <mutex>
  16. #include "settings/SettingsComponent.h"
  17. #include "settings/SettingsSection.h"
  18. #include "QsLog.h"
  19. QList<QChar> httpSeparators = { '(', ')', '<', '>', '@', ',', ';', ':', '\\', '\"', '/', '[', ']', '?', '=', '{', '}', '\'' };
  20. /////////////////////////////////////////////////////////////////////////////////////////
  21. QString Utils::sanitizeForHttpSeparators(const QString& input)
  22. {
  23. auto output = input;
  24. for (const QChar& c : httpSeparators)
  25. output.replace(c, "");
  26. for (int i = 0; i < output.size(); i ++)
  27. {
  28. if (output.at(i).unicode() > 127)
  29. output[i] = '_';
  30. }
  31. return output;
  32. }
  33. /////////////////////////////////////////////////////////////////////////////////////////
  34. QString Utils::ComputerName()
  35. {
  36. static std::once_flag flag;
  37. static QString name;
  38. std::call_once(flag, [](){
  39. #ifdef Q_OS_MAC
  40. name = OSXUtils::ComputerName();
  41. #else
  42. name = QHostInfo::localHostName();
  43. #endif
  44. });
  45. return name;
  46. }
  47. /////////////////////////////////////////////////////////////////////////////////////////
  48. QJsonDocument Utils::OpenJsonDocument(const QString& path, QJsonParseError* err)
  49. {
  50. QFile fp(path);
  51. QByteArray fdata;
  52. QRegExp commentMatch("^\\s*//");
  53. if (fp.open(QFile::ReadOnly))
  54. {
  55. while(true)
  56. {
  57. QByteArray row = fp.readLine();
  58. if (row.isEmpty())
  59. break;
  60. // filter all comments
  61. if (commentMatch.indexIn(row) != -1)
  62. continue;
  63. fdata.append(row);
  64. }
  65. fp.close();
  66. }
  67. return QJsonDocument::fromJson(fdata, err);
  68. }
  69. /////////////////////////////////////////////////////////////////////////////////////////
  70. Platform Utils::CurrentPlatform()
  71. {
  72. #if defined(Q_OS_MAC)
  73. return PLATFORM_OSX;
  74. #elif KONVERGO_OPENELEC
  75. #if TARGET_RPI
  76. return PLATFORM_OE_RPI;
  77. #else
  78. return PLATFORM_OE_X86;
  79. #endif
  80. #elif defined(Q_OS_LINUX)
  81. return PLATFORM_LINUX;
  82. #elif defined(Q_OS_WIN32)
  83. return PLATFORM_WINDOWS;
  84. #else
  85. return PLATFORM_UNKNOWN;
  86. #endif
  87. }
  88. /////////////////////////////////////////////////////////////////////////////////////////
  89. QString Utils::CurrentUserId()
  90. {
  91. SettingsSection* connections = SettingsComponent::Get().getSection("connections");
  92. if (!connections)
  93. return QString();
  94. QVariant ulist = connections->value("users");
  95. if (ulist.isValid())
  96. {
  97. QVariantList users = ulist.toList();
  98. if (users.size() > 0)
  99. {
  100. QVariantMap user = users.at(0).toMap();
  101. return user.value("id").toString();
  102. }
  103. }
  104. return QString();
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////
  107. QString Utils::PrimaryIPv4Address()
  108. {
  109. QList<QNetworkInterface> ifs = QNetworkInterface::allInterfaces();
  110. for(const QNetworkInterface& iface : ifs)
  111. {
  112. if (iface.isValid() && iface.flags() & QNetworkInterface::IsUp)
  113. {
  114. QList<QHostAddress> addresses = iface.allAddresses();
  115. for(const QHostAddress& addr : addresses)
  116. {
  117. if (!addr.isLoopback() && !addr.isMulticast() && addr.protocol() == QAbstractSocket::IPv4Protocol)
  118. return addr.toString();
  119. }
  120. }
  121. }
  122. return "";
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////////////////////////
  125. bool Utils::safelyWriteFile(const QString& filename, const QByteArray& data)
  126. {
  127. QSaveFile file(filename);
  128. if (!file.open(QIODevice::WriteOnly))
  129. return false;
  130. file.write(data);
  131. return file.commit();
  132. }