forked from AngryFender/Remini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavigationmodel.cpp
More file actions
370 lines (302 loc) · 10 KB
/
Copy pathnavigationmodel.cpp
File metadata and controls
370 lines (302 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "navigationmodel.h"
#include <QApplication>
#include <QClipboard>
#include <QDesktopServices>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QFileSystemModel>
#include <QSortFilterProxyModel>
#include <QUrl>
NavigationProxyModel::NavigationProxyModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
}
QModelIndex NavigationProxyModel::setRootIndexFromPath(QString path)
{
QFileSystemModel *model =
dynamic_cast<QFileSystemModel *>(this->sourceModel());
if (model)
{
QModelIndex result = this->mapFromSource(model->index(path));
return result;
}
return QModelIndex();
}
QFileInfo NavigationProxyModel::getFileInfoMappedToSource(
const QModelIndex &index)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return QFileInfo();
QModelIndex sourceIndex = this->mapToSource(index);
return model->fileInfo(sourceIndex);
}
QFileInfo NavigationProxyModel::getFileInfo(const QModelIndex &index)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return QFileInfo();
return model->fileInfo(index);
}
void NavigationProxyModel::createFileHandler(QModelIndex &index, QString &name)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
QModelIndex sourceIndex = this->mapToSource(index);
QString fileName = QStringLiteral("Untitled");
QString fileType = QStringLiteral(".txt");
QString filePath;
if (sourceIndex.isValid())
filePath = QDir(model->filePath(sourceIndex)).absolutePath();
else
filePath = QDir(model->rootPath()).absolutePath();
QFile file;
uniqueFileName(file, fileName, fileType, filePath + QDir::separator());
file.open(QIODevice::ReadWrite);
file.close();
name = fileName + fileType;
}
void NavigationProxyModel::createFolderHandler(QModelIndex &index,
QString &name)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
QModelIndex sourceIndex = this->mapToSource(index);
QString folderName = QStringLiteral("New Folder");
QString folderPath;
if (sourceIndex.isValid())
{
QFileInfo info = model->fileInfo(sourceIndex);
folderPath = info.absoluteFilePath();
}
else
{
folderPath = model->rootPath();
}
QDir dir(folderPath);
uniqueFolderName(dir, folderName, folderPath + QDir::separator());
dir.mkdir(folderName);
name = folderName;
}
void NavigationProxyModel::deleteFileFolderHandler(QModelIndex &index)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
QModelIndex sourceIndex = this->mapToSource(index);
QFileInfo fileInfo = model->fileInfo(sourceIndex);
if (!fileInfo.exists())
return;
// Cross-platform trash: Windows Recycle Bin / XDG trash / macOS Trash.
QFile::moveToTrash(fileInfo.absoluteFilePath());
}
void NavigationProxyModel::openLocationHandler(QModelIndex &index)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
QModelIndex sourceIndex = this->mapToSource(index);
QString folderPath;
if (!sourceIndex.isValid())
{
folderPath = model->rootPath();
}
else
{
QFileInfo fileInfo = model->fileInfo(sourceIndex);
if (!fileInfo.isDir())
fileInfo.setFile(fileInfo.absoluteFilePath());
folderPath = fileInfo.absolutePath();
}
QDesktopServices::openUrl(QUrl::fromLocalFile(folderPath));
}
void NavigationProxyModel::copyFileFolderHandler(QModelIndex &index)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
QModelIndex sourceIndex = this->mapToSource(index);
QString path;
if (!sourceIndex.isValid())
path = model->rootPath();
else
path = model->fileInfo(sourceIndex).absoluteFilePath();
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(path);
}
void NavigationProxyModel::createAllFoldersList(QModelIndex index,
QStringList &listPath)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
QModelIndex rootIndex = this->mapToSource(index);
QFileInfo info = model->fileInfo(rootIndex);
QString path = info.absoluteFilePath();
if (info.isDir())
{
QDir dir(path);
if (dir.isEmpty())
return;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
if (model->hasChildren(rootIndex))
{
QDirIterator di(path, QDir::Dirs, QDirIterator::Subdirectories);
while (di.hasNext())
{
di.next();
if (di.fileInfo().isDir())
listPath.append(di.fileInfo().absoluteFilePath());
}
}
}
}
void NavigationProxyModel::createAllFilesList(QModelIndex index,
QStringList &listPath)
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return;
listPath.clear();
QModelIndex rootIndex = this->mapToSource(index);
QFileInfo info = model->fileInfo(rootIndex);
QString path = info.absoluteFilePath();
if (info.isDir())
{
QDir dir(path);
if (dir.isEmpty())
return;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
if (model->hasChildren(rootIndex))
{
QDirIterator di(path, QDir::Files, QDirIterator::Subdirectories);
while (di.hasNext())
{
di.next();
if (di.fileInfo().isFile())
listPath.append(di.fileInfo().absoluteFilePath());
}
}
}
}
void NavigationProxyModel::uniqueFileName(QFile &file, QString &name,
QString &type, const QString &path)
{
file.setFileName(path + name + type);
if (file.exists())
{
name = name + QStringLiteral("_new");
uniqueFileName(file, name, type, path);
}
}
void NavigationProxyModel::uniqueFolderName(QDir &dir, QString &name,
const QString &path)
{
dir.setPath(path + name);
if (dir.exists())
{
name = name + QStringLiteral("_new");
uniqueFolderName(dir, name, path);
}
}
bool NavigationProxyModel::filterAcceptsRow(
int source_row, const QModelIndex &source_parent) const
{
QFileSystemModel *model =
qobject_cast<QFileSystemModel *>(this->sourceModel());
if (!model)
return false;
QModelIndex childIndex = model->index(source_row, 0, source_parent);
if (filterRegularExpression().pattern().isEmpty())
return true;
QString rootPath = model->rootPath();
QString infoPath = model->fileInfo(childIndex).absoluteFilePath();
if (!isSubdirectory(infoPath, rootPath))
return true;
return filterChildIndex(model, source_row, childIndex);
}
bool NavigationProxyModel::filterChildIndex(
QFileSystemModel *model, int source_row,
const QModelIndex &source_parent) const
{
Q_UNUSED(source_row);
QFileInfo info = model->fileInfo(source_parent);
QString fileName = info.fileName().toLower();
const QString pattern = filterRegularExpression().pattern();
if (!pattern.isEmpty() && pattern.at(0) == QLatin1Char('/'))
{
QString folderName = pattern.mid(1).toLower();
if (info.isDir())
{
if (fileName.contains(folderName))
return true;
QDir dir(info.absoluteFilePath());
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
int rowCount = dir.count();
for (int row = 0; row < rowCount; row++)
{
QModelIndex childIndex = model->index(row, 0, source_parent);
if (filterChildIndex(model, row, childIndex))
return true;
}
}
if (info.absolutePath().toLower().contains(folderName))
return true;
}
else
{
if (info.isFile())
{
if (fileName.contains(pattern))
return true;
return false;
}
if (info.isDir())
{
QDir dir(info.absoluteFilePath());
if (dir.isEmpty())
return false;
dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
int rowCount = dir.count();
for (int row = 0; row < rowCount; row++)
{
QModelIndex childIndex = model->index(row, 0, source_parent);
if (filterChildIndex(model, row, childIndex))
return true;
}
}
}
return false;
}
// -----------------------------------------------------------------------------
// Check whether subDirPath is located under parentDirPath.
//
// FIX: The naive `subDir.startsWith(parent)` check gives false positives.
// e.g. "/home/alice2/file" starts with "/home/al" even though alice2 is not
// inside /home/al. Append a trailing separator to the parent path so the
// comparison only succeeds on an actual path-component boundary.
// -----------------------------------------------------------------------------
bool NavigationProxyModel::isSubdirectory(const QString &subDirPath,
const QString &parentDirPath) const
{
const QString sub = QDir(subDirPath).absolutePath();
QString parent = QDir(parentDirPath).absolutePath();
if (sub == parent)
return true;
if (!parent.endsWith(QLatin1Char('/')))
parent += QLatin1Char('/');
return sub.startsWith(parent);
}