LeechCraft
0.6.70-18450-gabe19ee3b0
Modular cross-platform feature rich live environment.
Toggle main menu visibility
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
22
namespace
LC::Util
23
{
24
QStringList
GetPathCandidates
(
SysPath
path, QString suffix)
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
,
"qml"
QT_STRINGIFY (QT_VERSION_MAJOR)
"/"
+ 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
50
qWarning () << Q_FUNC_INFO
51
<<
"unknown system path"
52
<<
static_cast<
int
>
(path);
53
return
{};
54
}
55
56
QString
GetSysPath
(
SysPath
path,
const
QString& suffix,
const
QString& filename)
57
{
58
for
(
const
QString& cand :
GetPathCandidates
(path, suffix))
59
if
(QFile::exists (cand + filename))
60
return
cand + filename;
61
62
qWarning () << Q_FUNC_INFO
63
<<
"unable to find"
64
<< suffix
65
<< filename;
66
return
QString ();
67
}
68
69
QUrl
GetSysPathUrl
(
SysPath
path,
const
QString& subfolder,
const
QString& filename)
70
{
71
return
QUrl::fromLocalFile (
GetSysPath
(path, subfolder, filename));
72
}
73
74
QStringList
GetSystemPaths
()
75
{
76
return
QString (qgetenv (
"PATH"
)).split (
':'
, Qt::SkipEmptyParts);
77
}
78
79
QString
FindInSystemPath
(
const
QString& name,
const
QStringList& paths,
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
97
QDir
GetUserDir
(
UserDir
dir,
const
QString& subpath)
98
{
99
QString path;
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
126
QDir
CreateIfNotExists
(QString path)
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
143
QString
GetTemporaryName
(
const
QString& pattern)
144
{
145
static
const
auto
defaultPattern = QStringLiteral (
"lc_temp.XXXXXX"
);
146
QTemporaryFile file (QDir::tempPath () +
'/'
+ (pattern.isEmpty () ? defaultPattern : pattern));
147
if
(!file.open ())
148
qWarning () <<
"unable to open temporary file"
<< file.errorString ();
149
const
auto
name = file.fileName ();
150
file.close ();
151
file.remove ();
152
return
name;
153
}
154
155
SpaceInfo
GetSpaceInfo
(
const
QString& path)
156
{
157
const
auto
& info = std::filesystem::space (path.toStdString ());
158
return
159
{
160
.Capacity_ = info.capacity,
161
.Free_ = info.free,
162
.Available_ = info.available
163
};
164
}
165
}
LC::Util
Definition
icoreproxy.h:34
LC::Util::GetSysPath
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
LC::Util::GetSystemPaths
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
Definition
paths.cpp:74
LC::Util::GetTemporaryName
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition
paths.cpp:143
LC::Util::FindInSystemPath
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
LC::Util::UserDir
UserDir
Describes various user-specific paths.
Definition
paths.h:148
LC::Util::UserDir::LC
@ LC
Root LeechCraft directory (something like ~/.leechcraft).
Definition
paths.h:155
LC::Util::UserDir::Cache
@ Cache
Cache for volatile data.
Definition
paths.h:151
LC::Util::GetPathCandidates
QStringList GetPathCandidates(SysPath path, QString suffix)
Returns possible full paths for the path and subfolder.
Definition
paths.cpp:24
LC::Util::GetUserDir
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition
paths.cpp:97
LC::Util::GetSysPathUrl
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
LC::Util::SysPath
SysPath
Describes various root paths recognized by GetSysPath().
Definition
paths.h:26
LC::Util::SysPath::Share
@ Share
Directory with shared data files.
Definition
paths.h:39
LC::Util::SysPath::QML
@ QML
Root path for QML files.
Definition
paths.h:32
LC::Util::GetSpaceInfo
SpaceInfo GetSpaceInfo(const QString &path)
Returns the disk space info of the partition containing path.
Definition
paths.cpp:155
LC::Util::CreateIfNotExists
QDir CreateIfNotExists(QString path)
Creates a path if it doesn't exist.
Definition
paths.cpp:126
paths.h
LC::Util::SpaceInfo
Contains information about a partition's disk space.
Definition
paths.h:186
src
util
sys
paths.cpp
Generated by
1.17.0