32 #include <boost/filesystem.hpp>
34 #include <QTemporaryFile>
35 #if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
36 #include <QApplication>
42 #if QT_VERSION < 0x050000
43 #include <QDesktopServices>
45 #include <QStandardPaths>
56 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) !=
'/')
59 QStringList candidates;
63 #if QT_VERSION < 0x050000
70 candidates << QApplication::applicationDirPath () +
"/share/" + suffix;
71 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
72 candidates << QApplication::applicationDirPath () +
"/../Resources/share/" + suffix;
75 candidates << INSTALL_PREFIX
"/share/leechcraft/" + suffix;
77 candidates <<
"/usr/local/share/leechcraft/" + suffix
78 <<
"/usr/share/leechcraft/" + suffix;
83 qWarning () << Q_FUNC_INFO
84 <<
"unknown system path"
85 <<
static_cast<int> (path);
86 return QStringList ();
92 if (QFile::exists (cand + filename))
93 return cand + filename;
95 qWarning () << Q_FUNC_INFO
104 return QUrl::fromLocalFile (
GetSysPath (path, subfolder, filename));
109 return QString (qgetenv (
"PATH")).split (
":", QString::SkipEmptyParts);
113 const std::function<
bool (QFileInfo)>& filter)
115 for (
const auto& dir : paths)
117 const QFileInfo fi (dir +
'/' + name);
121 if (filter && !filter (fi))
124 return fi.absoluteFilePath ();
136 #if QT_VERSION < 0x050000
137 path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
139 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
143 path = QDir::home ().path () +
"/.leechcraft/";
148 throw std::runtime_error (
"cannot get root path");
150 if (!path.endsWith (
'/'))
153 #if QT_VERSION < 0x050000
154 path +=
"leechcraft/";
156 path +=
"leechcraft5/";
160 if (!QDir {}.exists (path) &&
161 !QDir {}.mkpath (path))
162 throw std::runtime_error (
"cannot create path " + path.toStdString ());
169 auto home = QDir::home ();
170 path.prepend (
".leechcraft/");
172 if (!home.exists (path) &&
174 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
175 .arg (QDir::toNativeSeparators (home.filePath (path)))));
180 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
181 .arg (QDir::toNativeSeparators (home.filePath (path)))));
186 QTemporaryFile file (QDir::tempPath () +
"/" + pattern);
188 QString name = file.fileName ();
196 const auto& info = boost::filesystem::space (path.toStdString ());
QStringList GetPathCandidates(SysPath path, QString suffix)
Returns possible full paths for the path and subfolder.
QDir CreateIfNotExists(QString path)
Creates a path if it doesn't exist.
SysPath
Describes various root paths recognized by GetSysPath().
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.
QUrl GetSysPathUrl(SysPath path, const QString &subfolder, const QString &filename)
Returns path to the file in the given root path and subfolder.
Directory with shared data files.
Contains information about a partition's disk space.
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.
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
QDir GetUserDir(UserDir dir, const QString &subpath)
Root LeechCraft directory (something like ~/.leechcraft).
SpaceInfo GetSpaceInfo(const QString &path)
Returns the disk space info of the partition containing path.
UserDir
Describes various user-specific paths.