Utils.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #ifndef UTILS_H
  2. #define UTILS_H
  3. #include <QString>
  4. #include <QUrl>
  5. #include <QByteArray>
  6. #include <QVariant>
  7. #include <QException>
  8. #include <QtCore/qjsondocument.h>
  9. #ifdef Q_OS_MAC
  10. #include "osx/OSXUtils.h"
  11. #elif defined(Q_OS_WIN)
  12. #include "win/WinUtils.h"
  13. #endif
  14. #define DEFINE_SINGLETON(cls) \
  15. public: \
  16. static cls& Get() \
  17. { \
  18. static cls __instance; \
  19. return __instance; \
  20. } \
  21. class FatalException : public QException
  22. {
  23. public:
  24. explicit FatalException(const QString& message) : m_message(message) {}
  25. const QString& message() const { return m_message; }
  26. ~FatalException() throw() override { }
  27. private:
  28. QString m_message;
  29. };
  30. enum Platform
  31. {
  32. PLATFORM_UNKNOWN = 0,
  33. PLATFORM_OSX = (1 << 0),
  34. PLATFORM_LINUX = (1 << 1),
  35. PLATFORM_OE_X86 = (1 << 2),
  36. PLATFORM_OE_RPI = (1 << 3),
  37. PLATFORM_WINDOWS = (1 << 4),
  38. PLATFORM_OE = (PLATFORM_OE_RPI | PLATFORM_OE_X86),
  39. PLATFORM_ANY = (PLATFORM_OSX | PLATFORM_WINDOWS | PLATFORM_LINUX | PLATFORM_OE)
  40. };
  41. #define PLATFORM_ANY_EXCEPT(x) (PLATFORM_ANY & (~(x)))
  42. namespace Utils
  43. {
  44. Platform CurrentPlatform();
  45. QJsonDocument OpenJsonDocument(const QString& path, QJsonParseError* err);
  46. QString CurrentUserId();
  47. QString ComputerName();
  48. QString PrimaryIPv4Address();
  49. bool safelyWriteFile(const QString& filename, const QByteArray& data);
  50. QString sanitizeForHttpSeparators(const QString& input);
  51. }
  52. #endif // UTILS_H