Debian Codegraph @

src/filesystem/dcapmanager.cpp

raw
1 // SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dcapmanager.h"
6 #include "dobject_p.h"
7 #include "dstandardpaths.h"
8 #include "private/dcapfsfileengine_p.h"
9
10 #include <QStandardPaths>
11 #include <QDebug>
12
13 DCORE_BEGIN_NAMESPACE
14
15 QString _d_cleanPath(const QString &path) {
16 return path.size() < 2 || !path.endsWith(QDir::separator()) ? path : path.chopped(1);
17 }
18
19 bool _d_isSubFileOf(const QString &filePath, const QString &directoryPath)
20 {
21 QString path = _d_cleanPath(filePath);
22 bool ret = path.startsWith(directoryPath);
23 return ret;
24 }
25
26 static QStringList defaultWriteablePaths() {
27 QStringList paths;
28 int list[] = {QStandardPaths::AppConfigLocation,
29 QStandardPaths::AppDataLocation,
30 QStandardPaths::CacheLocation,
31 QStandardPaths::TempLocation,
32 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
33 QStandardPaths::DataLocation,
34 #endif
35 QStandardPaths::GenericConfigLocation,
36 QStandardPaths::HomeLocation,
37 QStandardPaths::MusicLocation,
38 QStandardPaths::DocumentsLocation,
39 QStandardPaths::MoviesLocation,
40 QStandardPaths::PicturesLocation,
41 QStandardPaths::DownloadLocation};
42
43 for (uint i = 0; i < sizeof (list) / sizeof (int); ++i) {
44 const QString &path = QStandardPaths::writableLocation(QStandardPaths::StandardLocation(list[i]));
45 if (path.isEmpty())
46 continue;
47
48 paths.append(path);
49 }
50
51 for (int i = 0; i <= static_cast<int>(DStandardPaths::XDG::RuntimeDir); ++i) {
52 const QString &path = DStandardPaths::path(DStandardPaths::XDG(i));
53 if (path.isEmpty())
54 continue;
55
56 paths.append(path);
57 }
58
59 for (int i = 0; i <= static_cast<int>(DStandardPaths::DSG::DataDir); ++i) {
60 const QStringList &pathList = DStandardPaths::paths(DStandardPaths::DSG(i));
61 if (pathList.isEmpty())
62 continue;
63
64 for (auto path : pathList) {
65 if (path.isEmpty() || paths.contains(path))
66 continue;
67
68 paths.append(path);
69 }
70 }
71 return paths;
72 }
73
74 class DCapManagerPrivate : public DObjectPrivate
75 {
76 D_DECLARE_PUBLIC(DCapManager)
77 public:
78 DCapManagerPrivate(DCapManager *qq);
79
80 QStringList pathList;
81 };
82
83 class DCapManager_ : public DCapManager {};
84 Q_GLOBAL_STATIC(DCapManager_, capManager)
85
86 DCapManagerPrivate::DCapManagerPrivate(DCapManager *qq)
87 : DObjectPrivate(qq)
88 {
89 pathList = defaultWriteablePaths();
90 }
91
92 DCapManager::DCapManager()
93 : DObject(*new DCapManagerPrivate(this))
94 {
95
96 }
97
98 DCapManager *DCapManager::instance()
99 {
100 return capManager;
101 }
102
103 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
104 void DCapManager::registerFileEngine()
105 {
106 }
107
108 void DCapManager::unregisterFileEngine()
109 {
110 }
111 #endif
112
113 void DCapManager::appendPath(const QString &path)
114 {
115 D_D(DCapManager);
116 const QString &targetPath = _d_cleanPath(path);
117 bool exist = std::any_of(d->pathList.cbegin(), d->pathList.cend(),
118 std::bind(_d_isSubFileOf, targetPath, std::placeholders::_1));
119 if (exist)
120 return;
121 d->pathList.append(targetPath);
122 }
123
124 void DCapManager::appendPaths(const QStringList &pathList)
125 {
126 for (auto path : pathList)
127 appendPath(path);
128 }
129
130 void DCapManager::removePath(const QString &path)
131 {
132 D_D(DCapManager);
133 const QString &targetPath = _d_cleanPath(path);
134 if (!d->pathList.contains(targetPath))
135 return;
136 d->pathList.removeOne(targetPath);
137 }
138
139 void DCapManager::removePaths(const QStringList &paths)
140 {
141 for (auto path : paths)
142 removePath(path);
143 }
144
145 QStringList DCapManager::paths() const
146 {
147 D_DC(DCapManager);
148 return d->pathList;
149 }
150
151 DCORE_END_NAMESPACE