13#include <QTemporaryFile>
14#if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
15#include <QApplication>
20#include <QStandardPaths>
26 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) !=
'/')
29 QStringList candidates;
36 candidates << QApplication::applicationDirPath () +
"/share/" + suffix;
37#elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
38 candidates << QApplication::applicationDirPath () +
"/../Resources/share/" + suffix;
41 candidates << INSTALL_PREFIX
"/share/leechcraft/" + suffix;
43 candidates <<
"/usr/local/share/leechcraft/" + suffix
44 <<
"/usr/share/leechcraft/" + suffix;
49 qWarning () << Q_FUNC_INFO
50 <<
"unknown system path"
51 <<
static_cast<int> (path);
52 return QStringList ();
58 if (QFile::exists (cand + filename))
59 return cand + filename;
61 qWarning () << Q_FUNC_INFO
70 return QUrl::fromLocalFile (
GetSysPath (path, subfolder, filename));
75 return QString (qgetenv (
"PATH")).split (
':', Qt::SkipEmptyParts);
79 const std::function<
bool (QFileInfo)>& filter)
81 for (
const auto& dir : paths)
83 const QFileInfo fi (dir +
'/' + name);
87 if (filter && !filter (fi))
90 return fi.absoluteFilePath ();
102 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
105 path = QDir::home ().path () +
"/.leechcraft/";
110 throw std::runtime_error (
"cannot get root path");
112 if (!path.endsWith (
'/'))
115 path += QLatin1String (
"leechcraft5/");
118 if (!QDir {}.exists (path) &&
119 !QDir {}.mkpath (path))
120 throw std::runtime_error (
"cannot create path " + path.toStdString ());
127 auto home = QDir::home ();
128 path.prepend (
".leechcraft/");
130 if (!home.exists (path) &&
132 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
133 .arg (QDir::toNativeSeparators (home.filePath (path)))));
136 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
137 .arg (QDir::toNativeSeparators (home.filePath (path)))));
144 static const auto defaultPattern = QStringLiteral (
"lc_temp.XXXXXX");
145 QTemporaryFile file (QDir::tempPath () +
'/' + (pattern.isEmpty () ? defaultPattern : pattern));
147 QString name = file.fileName ();
155 const auto& info = std::filesystem::space (path.toStdString ());
158 .Capacity_ = info.capacity,
160 .Available_ = info.available
QString GetSysPath(SysPath path, const QString &suffix, const QString &filename)
Returns path to the file in the given root path and subfolder.
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
QString FindInSystemPath(const QString &name, const QStringList &paths, const std::function< bool(QFileInfo)> &filter)
Searches for a file in system paths according to a filter.
UserDir
Describes various user-specific paths.
@ LC
Root LeechCraft directory (something like ~/.leechcraft).
@ Cache
Cache for volatile data.
QStringList GetPathCandidates(SysPath path, QString suffix)
Returns possible full paths for the path and subfolder.
QDir GetUserDir(UserDir dir, const QString &subpath)
QUrl GetSysPathUrl(SysPath path, const QString &subfolder, const QString &filename)
Returns path to the file in the given root path and subfolder.
SysPath
Describes various root paths recognized by GetSysPath().
@ Share
Directory with shared data files.
@ QML
Root path for QML files.
SpaceInfo GetSpaceInfo(const QString &path)
Returns the disk space info of the partition containing path.
QDir CreateIfNotExists(QString path)
Creates a path if it doesn't exist.
Contains information about a partition's disk space.