Paths.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <QtGui/qguiapplication.h>
  10. #include <QDebug>
  11. #include "Names.h"
  12. #include "Version.h"
  13. /////////////////////////////////////////////////////////////////////////////////////////
  14. static QDir writableLocation(QStandardPaths::StandardLocation loc)
  15. {
  16. QDir d(QStandardPaths::writableLocation(loc));
  17. if (!d.mkpath(d.absolutePath() + "/" + Names::MainName()))
  18. {
  19. qWarning() << "Failed to create directory:" << d.absolutePath();
  20. return QDir();
  21. }
  22. d.cd(Names::MainName());
  23. return d;
  24. }
  25. /////////////////////////////////////////////////////////////////////////////////////////
  26. // Try a couple of different strategies to find the file we are looking for.
  27. // 1) By looking next to the application binary
  28. // 2) By looking in binary/../Resources
  29. // 3) By looking in PREFIX/share/jellyfinmediaplayer
  30. // 4) By looking in PREFIX/jellyfinmediaplayer
  31. //
  32. QString Paths::resourceDir(const QString& file)
  33. {
  34. auto appResourceDir = QGuiApplication::applicationDirPath() + "/";
  35. auto prefixDir = QString(PREFIX);
  36. QStringList possibleResourceDirs = {
  37. appResourceDir,
  38. appResourceDir + "../Resources/",
  39. prefixDir + "/share/jellyfinmediaplayer/",
  40. prefixDir + "/jellyfinmediaplayer/"
  41. };
  42. for (const auto& fileStr : possibleResourceDirs)
  43. {
  44. if (QFile::exists(fileStr + file))
  45. return fileStr + file;
  46. }
  47. return appResourceDir + file;
  48. }
  49. /////////////////////////////////////////////////////////////////////////////////////////
  50. QString Paths::dataDir(const QString& file)
  51. {
  52. QDir d = writableLocation(QStandardPaths::GenericDataLocation);
  53. if (file.isEmpty())
  54. return d.absolutePath();
  55. return d.filePath(file);
  56. }
  57. /////////////////////////////////////////////////////////////////////////////////////////
  58. QString Paths::cacheDir(const QString& file)
  59. {
  60. QDir d = writableLocation(QStandardPaths::GenericCacheLocation);
  61. if (file.isEmpty())
  62. return d.absolutePath();
  63. return d.filePath(file);
  64. }
  65. /////////////////////////////////////////////////////////////////////////////////////////
  66. QString Paths::logDir(const QString& file)
  67. {
  68. #ifdef Q_OS_MAC
  69. QDir ldir = QDir(QStandardPaths::locate(QStandardPaths::HomeLocation, "", QStandardPaths::LocateDirectory));
  70. ldir.mkpath(ldir.absolutePath() + "/Library/Logs/" + Names::MainName());
  71. ldir.cd("Library/Logs/" + Names::MainName());
  72. return ldir.filePath(file);
  73. #else
  74. QDir ldir = writableLocation(QStandardPaths::GenericDataLocation);
  75. ldir.mkpath(ldir.absolutePath() + "/logs");
  76. ldir.cd("logs");
  77. return ldir.filePath(file);
  78. #endif
  79. }
  80. /////////////////////////////////////////////////////////////////////////////////////////
  81. QString Paths::socketName(const QString& serverName)
  82. {
  83. QString userName = qgetenv("USER");
  84. if(userName.isEmpty())
  85. userName = qgetenv("USERNAME");
  86. if(userName.isEmpty())
  87. userName = "unknown";
  88. #ifdef Q_OS_UNIX
  89. return QString("/tmp/jmp_%1_%2.sock").arg(serverName).arg(userName);
  90. #else
  91. return QString("jmp_%1_%2.sock").arg(serverName).arg(userName);
  92. #endif
  93. }
  94. /////////////////////////////////////////////////////////////////////////////////////////
  95. QString Paths::soundsPath(const QString& sound)
  96. {
  97. // check local filesystem first
  98. auto localSound = dataDir("sounds/" + sound);
  99. QFileInfo f(localSound);
  100. if (f.exists())
  101. return f.absoluteFilePath();
  102. f = QFileInfo(":/sounds/" + sound);
  103. if (!f.exists())
  104. {
  105. qWarning() << "Can't find sound:" << sound;
  106. return QString();
  107. }
  108. return f.absoluteFilePath();
  109. }
  110. /////////////////////////////////////////////////////////////////////////////////////////
  111. QString Paths::webClientPath(const QString& mode)
  112. {
  113. QString webName = QString("web-client/%1").arg(mode);
  114. return resourceDir(webName + "/index.html");
  115. }
  116. /////////////////////////////////////////////////////////////////////////////////////////
  117. QString Paths::webExtensionPath(const QString& mode)
  118. {
  119. QString webName = QString("web-client/%1").arg(mode);
  120. return resourceDir(webName + "/");
  121. }