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) && !defined (USE_UNIX_LAYOUT) 71 candidates << QApplication::applicationDirPath () +
"/../Resources/share/" + suffix;
74 candidates << INSTALL_PREFIX
"/share/leechcraft/" + suffix;
76 candidates <<
"/usr/local/share/leechcraft/" + suffix
77 <<
"/usr/share/leechcraft/" + suffix;
82 qWarning () << Q_FUNC_INFO
83 <<
"unknown system path" 84 <<
static_cast<int> (path);
85 return QStringList ();
91 if (QFile::exists (cand + filename))
92 return cand + filename;
94 qWarning () << Q_FUNC_INFO
103 return QUrl::fromLocalFile (
GetSysPath (path, subfolder, filename));
108 return QString (qgetenv (
"PATH")).split (
":", QString::SkipEmptyParts);
112 const std::function<
bool (QFileInfo)>& filter)
114 for (
const auto& dir : paths)
116 const QFileInfo fi (dir +
'/' + name);
120 if (filter && !filter (fi))
123 return fi.absoluteFilePath ();
135 #if QT_VERSION < 0x050000 136 path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
138 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
142 path = QDir::home ().path () +
"/.leechcraft/";
147 throw std::runtime_error (
"cannot get root path");
149 if (!path.endsWith (
'/'))
152 #if QT_VERSION < 0x050000 153 path +=
"leechcraft/";
155 path +=
"leechcraft5/";
159 if (!QDir {}.exists (path) &&
160 !QDir {}.mkpath (path))
161 throw std::runtime_error (
"cannot create path " + path.toStdString ());
168 auto home = QDir::home ();
169 path.prepend (
".leechcraft/");
171 if (!home.exists (path) &&
173 throw std::runtime_error (qPrintable (QObject::tr (
"Could not create %1")
174 .arg (QDir::toNativeSeparators (home.filePath (path)))));
179 throw std::runtime_error (qPrintable (QObject::tr (
"Could not cd into %1")
180 .arg (QDir::toNativeSeparators (home.filePath (path)))));
185 QTemporaryFile file (QDir::tempPath () +
"/" + pattern);
187 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.