Debian Codegraph @

src/filesystem/dtrashmanager_dummy.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dtrashmanager.h"
6
7 #include "DObjectPrivate"
8
9 #include <QDirIterator>
10 #include <QStorageInfo>
11 #include <QCryptographicHash>
12 #include <QDateTime>
13 #include <QDebug>
14
15 DCORE_BEGIN_NAMESPACE
16
17 class DTrashManager_ : public DTrashManager {};
18 Q_GLOBAL_STATIC(DTrashManager_, globalTrashManager)
19
20 static QString getNotExistsFileName(const QString &fileName, const QString &targetPath)
21 {
22 QByteArray name = fileName.toUtf8();
23
24 int index = name.lastIndexOf('.');
25 QByteArray suffix;
26
27 if (index >= 0)
28 {
29 suffix = name.mid(index);
30 }
31
32 if (suffix.size() > 200)
33 {
34 suffix = suffix.left(200);
35 }
36
37 name.chop(suffix.size());
38 name = name.left(200 - suffix.size());
39
40 while (QFile::exists(targetPath + "/" + name + suffix))
41 {
42 name = QCryptographicHash::hash(name, QCryptographicHash::Md5).toHex();
43 }
44
45 return QString::fromUtf8(name + suffix);
46 }
47
48 static bool renameFile(const QFileInfo &fileInfo, const QString &target, QString *errorString = NULL)
49 {
50 if (fileInfo.isFile() || fileInfo.isSymLink())
51 {
52 QFile file(fileInfo.filePath());
53
54 if (!file.rename(target))
55 {
56 if (errorString)
57 {
58 *errorString = file.errorString();
59 }
60
61 return false;
62 }
63
64 return true;
65 }
66 else
67 {
68 QDirIterator iterator(fileInfo.filePath(),
69 QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden | QDir::System);
70
71 while (iterator.hasNext())
72 {
73 iterator.next();
74
75 const QString newFile = iterator.filePath().replace(0, fileInfo.filePath().length(), target);
76
77 if (!QDir().mkpath(QFileInfo(newFile).path()))
78 {
79 if (errorString)
80 {
81 *errorString = QString("Make the %1 path is failed").arg(QFileInfo(newFile).path());
82 }
83
84 return false;
85 }
86
87 if (!renameFile(iterator.fileInfo(), newFile, errorString))
88 {
89 return false;
90 }
91 }
92
93 if (!QDir().rmdir(fileInfo.filePath()))
94 {
95 if (errorString)
96 {
97 *errorString = QString("Cannot remove the %1 dir").arg(fileInfo.filePath());
98 }
99
100 return false;
101 }
102 }
103
104 return true;
105 }
106
107 class DTrashManagerPrivate : public DTK_CORE_NAMESPACE::DObjectPrivate
108 {
109 public:
110 DTrashManagerPrivate(DTrashManager *q_ptr)
111 : DObjectPrivate(q_ptr) {}
112
113 D_DECLARE_PUBLIC(DTrashManager)
114 };
115
116 DTrashManager *DTrashManager::instance()
117 {
118 return globalTrashManager;
119 }
120
121 bool DTrashManager::trashIsEmpty() const
122 {
123 return false;
124 }
125
126 bool DTrashManager::cleanTrash()
127 {
128 return false;
129 }
130
131 bool DTrashManager::moveToTrash(const QString &filePath, bool followSymlink)
132 {
133 return false;
134 }
135
136 DTrashManager::DTrashManager()
137 : QObject()
138 , DObject(*new DTrashManagerPrivate(this))
139 {
140
141 }
142
143 DCORE_END_NAMESPACE