LeechCraft 0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
paths.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "paths.h"
10#include <stdexcept>
11#include <filesystem>
12#include <QFile>
13#include <QTemporaryFile>
14#if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
15#include <QApplication>
16#endif
17#include <QtDebug>
18#include <QDir>
19#include <QUrl>
20#include <QStandardPaths>
21
22namespace LC::Util
23{
25 {
26 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) != '/')
27 suffix += '/';
28
29 switch (path)
30 {
31 case SysPath::QML:
32 return GetPathCandidates (SysPath::Share, "qml5/" + suffix);
33 case SysPath::Share:
34#ifdef Q_OS_WIN32
35 return { QApplication::applicationDirPath () + "/share/" + suffix };
36#elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
37 return { QApplication::applicationDirPath () + "/../Resources/share/" + suffix };
38#else
39 #ifdef INSTALL_PREFIX
40 return { INSTALL_PREFIX "/share/leechcraft/" + suffix };
41 #endif
42 return
43 {
44 "/usr/local/share/leechcraft/" + suffix,
45 "/usr/share/leechcraft/" + suffix
46 };
47#endif
48 }
49
51 << "unknown system path"
52 << static_cast<int> (path);
53 return {};
54 }
55
57 {
58 for (const QString& cand : GetPathCandidates (path, suffix))
59 if (QFile::exists (cand + filename))
60 return cand + filename;
61
63 << "unable to find"
64 << suffix
65 << filename;
66 return QString ();
67 }
68
70 {
71 return QUrl::fromLocalFile (GetSysPath (path, subfolder, filename));
72 }
73
75 {
76 return QString (qgetenv ("PATH")).split (':', Qt::SkipEmptyParts);
77 }
78
80 const std::function<bool (QFileInfo)>& filter)
81 {
82 for (const auto& dir : paths)
83 {
84 const QFileInfo fi (dir + '/' + name);
85 if (!fi.exists ())
86 continue;
87
88 if (filter && !filter (fi))
89 continue;
90
91 return fi.absoluteFilePath ();
92 }
93
94 return {};
95 }
96
98 {
100 switch (dir)
101 {
102 case UserDir::Cache:
103 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
104 break;
105 case UserDir::LC:
106 path = QDir::home ().path () + "/.leechcraft/";
107 break;
108 }
109
110 if (path.isEmpty ())
111 throw std::runtime_error ("cannot get root path");
112
113 if (!path.endsWith ('/'))
114 path += '/';
115 if (dir == UserDir::Cache)
116 path += QLatin1String ("leechcraft5/");
117 path += subpath;
118
119 if (!QDir {}.exists (path) &&
120 !QDir {}.mkpath (path))
121 throw std::runtime_error ("cannot create path " + path.toStdString ());
122
123 return { path };
124 }
125
127 {
128 auto home = QDir::home ();
129 path.prepend (".leechcraft/");
130
131 if (!home.exists (path) &&
132 !home.mkpath (path))
133 throw std::runtime_error (qPrintable (QObject::tr ("Could not create %1")
134 .arg (QDir::toNativeSeparators (home.filePath (path)))));
135
136 if (!home.cd (path))
137 throw std::runtime_error (qPrintable (QObject::tr ("Could not cd into %1")
138 .arg (QDir::toNativeSeparators (home.filePath (path)))));
139
140 return home;
141 }
142
144 {
145 static const auto defaultPattern = QStringLiteral ("lc_temp.XXXXXX");
146 QTemporaryFile file (QDir::tempPath () + '/' + (pattern.isEmpty () ? defaultPattern : pattern));
147 file.open ();
148 QString name = file.fileName ();
149 file.close ();
150 file.remove ();
151 return name;
152 }
153
155 {
156 const auto& info = std::filesystem::space (path.toStdString ());
157 return
158 {
159 .Capacity_ = info.capacity,
160 .Free_ = info.free,
161 .Available_ = info.available
162 };
163 }
164}
Container< T > Filter(const Container< T > &c, F f)
Definition prelude.h:118
QString GetSysPath(SysPath path, const QString &suffix, const QString &filename)
Returns path to the file in the given root path and subfolder.
Definition paths.cpp:56
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
Definition paths.cpp:74
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition paths.cpp:143
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.
Definition paths.cpp:79
UserDir
Describes various user-specific paths.
Definition paths.h:148
@ LC
Root LeechCraft directory (something like ~/.leechcraft).
@ Cache
Cache for volatile data.
QStringList GetPathCandidates(SysPath path, QString suffix)
Returns possible full paths for the path and subfolder.
Definition paths.cpp:24
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition paths.cpp:97
QUrl GetSysPathUrl(SysPath path, const QString &subfolder, const QString &filename)
Returns path to the file in the given root path and subfolder.
Definition paths.cpp:69
SysPath
Describes various root paths recognized by GetSysPath().
Definition paths.h:26
@ Share
Directory with shared data files.
@ QML
Root path for QML files.
SpaceInfo GetSpaceInfo(const QString &path)
Returns the disk space info of the partition containing path.
Definition paths.cpp:154
QDir CreateIfNotExists(QString path)
Creates a path if it doesn't exist.
Definition paths.cpp:126
Contains information about a partition's disk space.
Definition paths.h:186