Paths.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Created by Tobias Hieta on 01/09/15.
  3. //
  4. #include "settings/SettingsSection.h"
  5. #include "Paths.h"
  6. #include <QDir>
  7. #include <QStandardPaths>
  8. #include <QGuiApplication>
  9. #include <QsLog.h>
  10. #include <QtGui/qguiapplication.h>
  11. #include "Names.h"
  12. /////////////////////////////////////////////////////////////////////////////////////////
  13. static QDir writableLocation(QStandardPaths::StandardLocation loc)
  14. {
  15. QDir d(QStandardPaths::writableLocation(loc));
  16. if (!d.mkpath(d.absolutePath() + "/" + Names::MainName()))
  17. {
  18. QLOG_WARN() << "Failed to create directory:" << d.absolutePath();
  19. return QDir();
  20. }
  21. d.cd(Names::MainName());
  22. return d;
  23. }
  24. /////////////////////////////////////////////////////////////////////////////////////////
  25. QString Paths::resourceDir(const QString& file)
  26. {
  27. auto resourceDir = QDir(QGuiApplication::applicationDirPath());
  28. #ifdef Q_OS_MAC
  29. resourceDir.cdUp();
  30. resourceDir.cd("Resources");
  31. #endif
  32. return resourceDir.filePath(file);
  33. }
  34. /////////////////////////////////////////////////////////////////////////////////////////
  35. QString Paths::dataDir(const QString& file)
  36. {
  37. QDir d = writableLocation(QStandardPaths::GenericDataLocation);
  38. if (file.isEmpty())
  39. return d.absolutePath();
  40. return d.filePath(file);
  41. }
  42. /////////////////////////////////////////////////////////////////////////////////////////
  43. QString Paths::cacheDir(const QString& file)
  44. {
  45. QDir d = writableLocation(QStandardPaths::GenericCacheLocation);
  46. if (file.isEmpty())
  47. return d.absolutePath();
  48. return d.filePath(file);
  49. }
  50. /////////////////////////////////////////////////////////////////////////////////////////
  51. QString Paths::logDir(const QString& file)
  52. {
  53. #ifdef Q_OS_MAC
  54. QDir ldir = QDir(QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory));
  55. ldir.mkpath(ldir.absolutePath() + "/Library/Logs/" + Names::MainName());
  56. ldir.cd("Library/Logs/" + Names::MainName());
  57. return ldir.filePath(file);
  58. #else
  59. QDir ldir = writableLocation(QStandardPaths::GenericDataLocation);
  60. ldir.mkpath(ldir.absolutePath() + "/logs");
  61. ldir.cd("logs");
  62. return ldir.filePath(file);
  63. #endif
  64. }
  65. /////////////////////////////////////////////////////////////////////////////////////////
  66. QString Paths::socketName(const QString& serverName)
  67. {
  68. QString userName = qgetenv("USER");
  69. if(userName.isEmpty())
  70. userName = qgetenv("USERNAME");
  71. if(userName.isEmpty())
  72. userName = "unknown";
  73. #ifdef Q_OS_UNIX
  74. return QString("/tmp/pmp_%1_%2.sock").arg(serverName).arg(userName);
  75. #else
  76. return QString("pmp_%1_%2.sock").arg(serverName).arg(userName);
  77. #endif
  78. }
  79. /////////////////////////////////////////////////////////////////////////////////////////
  80. QString Paths::soundsPath(const QString& sound)
  81. {
  82. // check local filesystem first
  83. auto localSound = dataDir("sounds/" + sound);
  84. QFileInfo f(localSound);
  85. if (f.exists())
  86. return f.absoluteFilePath();
  87. f = QFileInfo(":/sounds/" + sound);
  88. if (!f.exists())
  89. {
  90. QLOG_WARN() << "Can't find sound:" << sound;
  91. return QString();
  92. }
  93. return f.absoluteFilePath();
  94. }