32 #include <boost/filesystem.hpp> 34 #include <QTemporaryFile> 35 #if defined (Q_OS_WIN32) || defined (Q_OS_MAC) 36 #include <QApplication> 41 #include <QStandardPaths> 50 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) !=
'/')
53 QStringList candidates;
60 candidates << QApplication::applicationDirPath () +
"/share/" + suffix;
61 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT) 62 candidates << QApplication::applicationDirPath () +
"/../Resources/share/" + suffix;
65 candidates << INSTALL_PREFIX
"/share/leechcraft/" + suffix;
67 candidates <<
"/usr/local/share/leechcraft/" + suffix
68 <<
"/usr/share/leechcraft/" + suffix;
73 qWarning () << Q_FUNC_INFO
74 <<
"unknown system path" 75 <<
static_cast<int> (path);
76 return QStringList ();
82 if (QFile::exists (cand + filename))
83 return cand + filename;
85 qWarning () << Q_FUNC_INFO
94 return QUrl::fromLocalFile (
GetSysPath (path, subfolder, filename));
99 return QString (qgetenv (
"PATH")).split (
":", QString::SkipEmptyParts);
103 const std::function<
bool (QFileInfo)>& filter)
105 for (
const auto& dir : paths)
107 const QFileInfo fi (dir +
'/' + name);
111 if (filter && !filter (fi))
114 return fi.absoluteFilePath ();
126 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
129 path = QDir::home ().path () +
"/.leechcraft/";
134 throw std::runtime_error (
"cannot get root path");
136 if (!path.endsWith (
'/'))
139 path +=
"leechcraft5/";
142 if (!QDir {}.exists (path) &&
143 !QDir {}.mkpath (path))
144 throw std::runtime_error (
"cannot create path " + path.toStdString ());
151 auto home = QDir::home ();
152 path.prepend (
".leechcraft/");
154 if (!home.exists (path) &&
156 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
157 .arg (QDir::toNativeSeparators (home.filePath (path)))));
162 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
163 .arg (QDir::toNativeSeparators (home.filePath (path)))));
168 QTemporaryFile file (QDir::tempPath () +
"/" + pattern);
170 QString name = file.fileName ();
178 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.