33 #include <QTemporaryFile>
34 #if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
35 #include <QApplication>
40 #include <QDesktopServices>
49 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) !=
'/')
52 QStringList candidates;
59 candidates << QApplication::applicationDirPath () +
"/share/" + suffix;
60 #elif defined (Q_OS_MAC)
61 if (QApplication::arguments ().contains (
"-nobundle"))
62 candidates <<
"/usr/local/share/leechcraft/" + suffix;
64 candidates << QApplication::applicationDirPath () +
"/../Resources/share/" + suffix;
66 candidates <<
"/usr/local/share/leechcraft/" + suffix
67 <<
"/usr/share/leechcraft/" + suffix;
72 qWarning () << Q_FUNC_INFO
73 <<
"unknown system path"
74 <<
static_cast<int> (path);
75 return QStringList ();
81 if (QFile::exists (cand + filename))
82 return cand + filename;
84 qWarning () << Q_FUNC_INFO
93 return QUrl::fromLocalFile (
GetSysPath (path, subfolder, filename));
98 return QString (qgetenv (
"PATH")).split (
":", QString::SkipEmptyParts);
102 const std::function<
bool (QFileInfo)>& filter)
104 for (
const auto& dir : paths)
106 const QFileInfo fi (dir +
'/' + name);
110 if (filter && !filter (fi))
113 return fi.absoluteFilePath ();
125 path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
128 path = QDir::home ().path () +
"/.leechcraft/";
133 throw std::runtime_error (
"cannot get root path");
135 if (!path.endsWith (
'/'))
138 path +=
"leechcraft/";
141 if (!QDir {}.exists (path) &&
142 !QDir {}.mkpath (path))
143 throw std::runtime_error (
"cannot create path " + path.toStdString ());
150 auto home = QDir::home ();
151 path.prepend (
".leechcraft/");
153 if (!home.exists (path) &&
155 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
156 .arg (QDir::toNativeSeparators (home.filePath (path)))));
161 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
162 .arg (QDir::toNativeSeparators (home.filePath (path)))));
167 QTemporaryFile file (QDir::tempPath () +
"/" + pattern);
169 QString name = file.fileName ();
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.
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).
UserDir
Describes various user-specific paths.