LeechCraft 0.6.70-14794-g33744ae6ce
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{
24 QStringList GetPathCandidates (SysPath path, QString suffix)
25 {
26 if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) != '/')
27 suffix += '/';
28
29 QStringList candidates;
30 switch (path)
31 {
32 case SysPath::QML:
33 return GetPathCandidates (SysPath::Share, "qml5/" + suffix);
34 case SysPath::Share:
35#ifdef Q_OS_WIN32
36 candidates << QApplication::applicationDirPath () + "/share/" + suffix;
37#elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
38 candidates << QApplication::applicationDirPath () + "/../Resources/share/" + suffix;
39#else
40 #ifdef INSTALL_PREFIX
41 candidates << INSTALL_PREFIX "/share/leechcraft/" + suffix;
42 #endif
43 candidates << "/usr/local/share/leechcraft/" + suffix
44 << "/usr/share/leechcraft/" + suffix;
45#endif
46 return candidates;
47 }
48
49 qWarning () << Q_FUNC_INFO
50 << "unknown system path"
51 << static_cast<int> (path);
52 return QStringList ();
53 }
54
55 QString GetSysPath (SysPath path, const QString& suffix, const QString& filename)
56 {
57 for (const QString& cand : GetPathCandidates (path, suffix))
58 if (QFile::exists (cand + filename))
59 return cand + filename;
60
61 qWarning () << Q_FUNC_INFO
62 << "unable to find"
63 << suffix
64 << filename;
65 return QString ();
66 }
67
68 QUrl GetSysPathUrl (SysPath path, const QString& subfolder, const QString& filename)
69 {
70 return QUrl::fromLocalFile (GetSysPath (path, subfolder, filename));
71 }
72
73 QStringList GetSystemPaths ()
74 {
75 return QString (qgetenv ("PATH")).split (':', Qt::SkipEmptyParts);
76 }
77
78 QString FindInSystemPath (const QString& name, const QStringList& paths,
79 const std::function<bool (QFileInfo)>& filter)
80 {
81 for (const auto& dir : paths)
82 {
83 const QFileInfo fi (dir + '/' + name);
84 if (!fi.exists ())
85 continue;
86
87 if (filter && !filter (fi))
88 continue;
89
90 return fi.absoluteFilePath ();
91 }
92
93 return {};
94 }
95
96 QDir GetUserDir (UserDir dir, const QString& subpath)
97 {
98 QString path;
99 switch (dir)
100 {
101 case UserDir::Cache:
102 path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
103 break;
104 case UserDir::LC:
105 path = QDir::home ().path () + "/.leechcraft/";
106 break;
107 }
108
109 if (path.isEmpty ())
110 throw std::runtime_error ("cannot get root path");
111
112 if (!path.endsWith ('/'))
113 path += '/';
114 if (dir == UserDir::Cache)
115 path += QLatin1String ("leechcraft5/");
116 path += subpath;
117
118 if (!QDir {}.exists (path) &&
119 !QDir {}.mkpath (path))
120 throw std::runtime_error ("cannot create path " + path.toStdString ());
121
122 return { path };
123 }
124
125 QDir CreateIfNotExists (QString path)
126 {
127 auto home = QDir::home ();
128 path.prepend (".leechcraft/");
129
130 if (!home.exists (path) &&
131 !home.mkpath (path))
132 throw std::runtime_error (qPrintable (QObject::tr ("Could not create %1")
133 .arg (QDir::toNativeSeparators (home.filePath (path)))));
134
135 if (!home.cd (path))
136 throw std::runtime_error (qPrintable (QObject::tr ("Could not cd into %1")
137 .arg (QDir::toNativeSeparators (home.filePath (path)))));
138
139 return home;
140 }
141
142 QString GetTemporaryName (const QString& pattern)
143 {
144 static const auto defaultPattern = QStringLiteral ("lc_temp.XXXXXX");
145 QTemporaryFile file (QDir::tempPath () + '/' + (pattern.isEmpty () ? defaultPattern : pattern));
146 file.open ();
147 QString name = file.fileName ();
148 file.close ();
149 file.remove ();
150 return name;
151 }
152
153 SpaceInfo GetSpaceInfo (const QString& path)
154 {
155 const auto& info = std::filesystem::space (path.toStdString ());
156 return
157 {
158 .Capacity_ = info.capacity,
159 .Free_ = info.free,
160 .Available_ = info.available
161 };
162 }
163}
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:55
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
Definition: paths.cpp:73
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:142
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:78
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:96
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:68
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:153
QDir CreateIfNotExists(QString path)
Creates a path if it doesn't exist.
Definition: paths.cpp:125
Contains information about a partition's disk space.
Definition: paths.h:186