33 #include <QTemporaryFile>
34 #if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
35 #include <QApplication>
41 #if QT_VERSION < 0x050000
42 #include <QDesktopServices>
44 #include <QStandardPaths>
55 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) !=
'/')
58 QStringList candidates;
62 #if QT_VERSION < 0x050000
69 candidates << QApplication::applicationDirPath () +
"/share/" + suffix;
70 #elif defined (Q_OS_MAC)
71 if (QApplication::arguments ().contains (
"-nobundle"))
72 candidates <<
"/usr/local/share/leechcraft/" + suffix;
74 candidates << QApplication::applicationDirPath () +
"/../Resources/share/" + suffix;
77 candidates << INSTALL_PREFIX
"/share/leechcraft/" + suffix;
79 candidates <<
"/usr/local/share/leechcraft/" + suffix
80 <<
"/usr/share/leechcraft/" + suffix;
85 qWarning () << Q_FUNC_INFO
86 <<
"unknown system path"
87 <<
static_cast<int> (path);
88 return QStringList ();
94 if (QFile::exists (cand + filename))
95 return cand + filename;
97 qWarning () << Q_FUNC_INFO
106 return QUrl::fromLocalFile (
GetSysPath (path, subfolder, filename));
111 return QString (qgetenv (
"PATH")).split (
":", QString::SkipEmptyParts);
115 const std::function<
bool (QFileInfo)>& filter)
117 for (
const auto& dir : paths)
119 const QFileInfo fi (dir +
'/' + name);
123 if (filter && !filter (fi))
126 return fi.absoluteFilePath ();
138 #if QT_VERSION < 0x050000
139 path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
141 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
145 path = QDir::home ().path () +
"/.leechcraft/";
150 throw std::runtime_error (
"cannot get root path");
152 if (!path.endsWith (
'/'))
155 #if QT_VERSION < 0x050000
156 path +=
"leechcraft/";
158 path +=
"leechcraft5/";
162 if (!QDir {}.exists (path) &&
163 !QDir {}.mkpath (path))
164 throw std::runtime_error (
"cannot create path " + path.toStdString ());
171 auto home = QDir::home ();
172 path.prepend (
".leechcraft/");
174 if (!home.exists (path) &&
176 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
177 .arg (QDir::toNativeSeparators (home.filePath (path)))));
182 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
183 .arg (QDir::toNativeSeparators (home.filePath (path)))));
188 QTemporaryFile file (QDir::tempPath () +
"/" + pattern);
190 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.