forked from karip/harbour-file-browser
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
engine.cpp
480 lines (402 loc) · 13.7 KB
/
engine.cpp
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* This file is part of File Browser.
*
* SPDX-FileCopyrightText: 2013-2019 Kari Pihkala
* SPDX-FileCopyrightText: 2016 Malte Veerman
* SPDX-FileCopyrightText: 2019-2023 Mirian Margiani
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* File Browser is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* File Browser is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "engine.h"
#include <unistd.h>
#include <QDateTime>
#include <QTextStream>
#include <QSettings>
#include <QStandardPaths>
#include <QDir>
#include <QCoreApplication>
#include <QProcess>
#include <QtConcurrent/QtConcurrent>
#include <QFuture>
#include <QFutureWatcher>
#include <QStorageInfo>
#include "globals.h"
#include "fileworker.h"
#include "statfileinfo.h"
Engine::Engine(QObject *parent) :
QObject(parent),
m_progress(0)
{
m_diskSpaceWorkers.reserve(600);
m_fileWorker = new FileWorker;
// update progress property when worker progresses
connect(m_fileWorker, &FileWorker::progressChanged, this, &Engine::setProgress);
// pass worker end signals to QML
connect(m_fileWorker, &FileWorker::done, this, &Engine::workerDone);
connect(m_fileWorker, &FileWorker::errorOccurred, this, &Engine::workerErrorOccurred);
connect(m_fileWorker, &FileWorker::fileDeleted, this, &Engine::fileDeleted);
}
Engine::~Engine()
{
m_fileWorker->cancel(); // ask the background thread to exit its loop
// is this the way to force stop the worker thread?
m_fileWorker->wait(); // wait until thread stops
m_fileWorker->deleteLater(); // delete it
}
void Engine::deleteFiles(QStringList filenames)
{
setProgress(0, "");
m_fileWorker->startDeleteFiles(filenames);
}
void Engine::pasteFiles(QStringList files, QString destDirectory, FileClipMode::Enum mode)
{
// TODO use FileOperations directly from QML instead
bool isCopy = (mode == FileClipMode::Copy);
bool isCut = (mode == FileClipMode::Cut);
bool isLink = (mode == FileClipMode::Link);
if (files.isEmpty()) {
emit workerErrorOccurred(tr("No files to paste"), "");
return;
}
setProgress(0, "");
QDir dest(destDirectory);
if (!dest.exists()) {
emit workerErrorOccurred(tr("Destination does not exist"), destDirectory);
return;
}
// validate that the files can be pasted
for (const auto& filename : std::as_const(files)) {
QFileInfo fileInfo(filename);
QString newname = dest.absoluteFilePath(fileInfo.fileName());
// moving and source and dest filenames are the same?
if (!(isCopy || isLink) && filename == newname) {
emit workerErrorOccurred(tr("Cannot overwrite itself"), newname);
return;
}
// dest is under source? (directory)
if (newname.startsWith(filename) && newname != filename) {
emit workerErrorOccurred(tr("Cannot move/copy to itself"), filename);
return;
}
}
if (isLink) {
m_fileWorker->startSymlinkFiles(files, destDirectory);
} else if (isCopy) {
m_fileWorker->startCopyFiles(files, destDirectory);
} else if (isCut) {
m_fileWorker->startMoveFiles(files, destDirectory);
} else {
emit workerErrorOccurred(QStringLiteral("Bug: unknown file operation mode requested: %1").arg(mode), destDirectory);
}
}
int Engine::runDiskSpaceWorker(std::function<void (int, QStringList)> signal,
std::function<QStringList (void)> function)
{
m_diskSpaceWorkers.append({
QSharedPointer<QFutureWatcher<QStringList>>{new QFutureWatcher<QStringList>},
QtConcurrent::run(function)
});
auto& worker = m_diskSpaceWorkers.last();
auto& future = worker.second;
int index = m_diskSpaceWorkers.length() - 1;
connect(worker.first.data(), &QFutureWatcherBase::finished, this, [=](){
emit signal(index, future.result());
m_diskSpaceWorkers[index] = {};
});
worker.first->setFuture(future);
return index;
}
int Engine::requestDiskSpaceInfo(const QString& path)
{
return runDiskSpaceWorker([&](int handle, QStringList result){
emit diskSpaceInfoReady(handle, result);
}, [path]() -> QStringList {
QStorageInfo info(path);
if (!info.isValid()) {
return {{}, {}, {}, {}};
}
while (!info.isReady()) {
qDebug() << "waiting for device to become ready..." << path;
sleep(2);
}
info.refresh();
info.bytesAvailable();
auto total = info.bytesTotal();
auto free = info.bytesAvailable();
auto used = total - free;
auto usedPercent = used / (total / 100);
return {
QStringLiteral("ok"),
QString::number(usedPercent),
QStringLiteral("%1/%2").arg(filesizeToString(used), filesizeToString(total)),
filesizeToString(free)
};
});
}
int Engine::requestFileSizeInfo(const QStringList& paths)
{
return runDiskSpaceWorker([&](int handle, QStringList result){
emit fileSizeInfoReady(handle, result);
}, [paths]() -> QStringList {
if (paths.isEmpty()) {
return {{}, {}, {}, {}};
}
int files = 0;
int dirs = 0;
qint64 bytes = 0;
auto process = [&files, &dirs, &bytes](const QString& dir){
QDirIterator it(dir, QDir::AllEntries | QDir::System | QDir::NoDotAndDotDot | QDir::Hidden,
QDirIterator::Subdirectories | QDirIterator::FollowSymlinks);
while (!it.next().isEmpty()) {
const auto& info = it.fileInfo();
bytes += info.size();
if (info.isDir()) ++dirs;
else ++files;
}
};
for (const auto& i : paths) {
auto info = QFileInfo(i);
bytes += info.size();
if (info.isDir()) {
++dirs;
process(i);
} else {
++files;
}
}
return {
QStringLiteral("ok"),
filesizeToString(bytes),
QString::number(dirs),
QString::number(files)
};
});
}
void Engine::cancel()
{
m_fileWorker->cancel();
}
bool Engine::exists(QString filename)
{
if (filename.isEmpty())
return false;
return QFile::exists(filename);
}
QStringList Engine::readFile(QString filename)
{
int maxLines = 1000;
int maxSize = 10240;
int maxBinSize = 2048;
// check existence
StatFileInfo fileInfo(filename);
if (!fileInfo.exists()) {
if (!fileInfo.isSymLink())
return makeStringList(tr("File does not exist") + "\n" + filename);
else
return makeStringList(tr("Broken symbolic link") + "\n" + filename);
}
// don't read unsafe system files
if (!fileInfo.isSafeToRead()) {
return makeStringList(tr("Cannot read this type of file") + "\n" + filename);
}
// check permissions
QFileInfo info(filename);
if (!info.isReadable())
return makeStringList(tr("No permission to read the file") + "\n" + filename);
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
return makeStringList(tr("Error reading file") + "\n" + filename);
// read start of file
char buffer[maxSize+1];
qint64 readSize = file.read(buffer, maxSize);
if (readSize < 0)
return makeStringList(tr("Error reading file") + "\n" + filename);
if (readSize == 0)
return makeStringList(tr("Empty file"));
bool atEnd = file.atEnd();
file.close();
// detect binary or text file, it is binary if it contains zeros
bool isText = true;
for (int i = 0; i < readSize; ++i) {
if (buffer[i] == 0) {
isText = false;
break;
}
}
// binary output
if (!isText) {
// two different line widths
if (readSize > maxBinSize) {
readSize = maxBinSize;
atEnd = false;
}
QString out8 = createHexDump(buffer, readSize, 8);
QString out16 = createHexDump(buffer, readSize, 16);
QString msg = "";
if (!atEnd) {
msg = tr("Binary file preview clipped at %1 kB").arg(maxBinSize/1024);
}
return QStringList() << msg << out8 << out16;
}
// read lines to a string list and join
QByteArray ba(buffer, readSize);
QTextStream in(&ba);
QStringList lines;
int lineCount = 0;
while (!in.atEnd() && lineCount < maxLines) {
QString line = in.readLine();
lines.append(line);
lineCount++;
}
QString msg = "";
if (lineCount == maxLines) {
msg = tr("Text file preview clipped at %1 lines").arg(maxLines);
} else if (!atEnd) {
msg = tr("Text file preview clipped at %1 kB").arg(maxSize/1024);
}
return makeStringList(msg, lines.join("\n"));
}
QString Engine::createDirectory(QString path, QString name)
{
if (path.isEmpty() || name.isEmpty()) {
qWarning() << "bug: Engine::createDirectory: path or name is empty --" << path << name;
return QLatin1Literal("");
}
QDir dir(path);
if (!dir.mkpath(name)) {
QFileInfo info(path);
if (!info.isWritable()) {
return tr("No permissions to create %1").arg(name);
}
return tr("Cannot create folder %1").arg(name);
}
return QLatin1Literal("");
}
QString Engine::createFile(QString path, QString name)
{
if (path.isEmpty() || name.isEmpty()) {
qWarning() << "bug: Engine::createFile: path or name is empty --" << path << name;
return QLatin1Literal("");
}
QFileInfo info(path);
if (!info.isWritable()) {
return tr("No permissions to create “%1” in “%2”").arg(name, path);
}
QFile file(path + QStringLiteral("/") + name);
if (file.exists()) {
qWarning() << "bug: Engine::createFile: file already exists --" << path << name;
return QLatin1Literal("");
}
if (!file.open(QFile::WriteOnly | QFile::Truncate)) {
return tr("Cannot create file “%1” in “%2”").arg(name, path);
}
QTextStream out(&file);
out << QStringLiteral("\n");
file.close();
return QLatin1Literal("");
}
QStringList Engine::rename(QString fullOldFilename, QString newName)
{
QFile file(fullOldFilename);
QFileInfo fileInfo(fullOldFilename);
QDir dir = fileInfo.absoluteDir();
QString fullNewFilename = dir.absoluteFilePath(newName);
QString errorMessage;
if (!file.rename(fullNewFilename)) {
QString oldName = fileInfo.fileName();
errorMessage = tr("Cannot rename %1").arg(oldName) + "\n" + file.errorString();
}
return QStringList() << fullNewFilename << errorMessage;
}
QString Engine::chmod(QString path,
bool ownerRead, bool ownerWrite, bool ownerExecute,
bool groupRead, bool groupWrite, bool groupExecute,
bool othersRead, bool othersWrite, bool othersExecute)
{
QFile file(path);
QFileDevice::Permissions p;
if (ownerRead) p |= QFileDevice::ReadOwner;
if (ownerWrite) p |= QFileDevice::WriteOwner;
if (ownerExecute) p |= QFileDevice::ExeOwner;
if (groupRead) p |= QFileDevice::ReadGroup;
if (groupWrite) p |= QFileDevice::WriteGroup;
if (groupExecute) p |= QFileDevice::ExeGroup;
if (othersRead) p |= QFileDevice::ReadOther;
if (othersWrite) p |= QFileDevice::WriteOther;
if (othersExecute) p |= QFileDevice::ExeOther;
if (!file.setPermissions(p))
return tr("Cannot change permissions") + "\n" + file.errorString();
return QString();
}
bool Engine::openNewWindow(QStringList arguments) const
{
QFileInfo info(QCoreApplication::applicationFilePath());
if (!info.exists() || !info.isFile()) {
return false;
} else {
return QProcess::startDetached(info.absoluteFilePath(), arguments);
}
}
bool Engine::pathIsDirectory(QString path) const
{
StatFileInfo info(path);
return info.isDirAtEnd();
}
bool Engine::pathIsFile(QString path) const
{
StatFileInfo info(path);
return info.isFileAtEnd();
}
void Engine::setProgress(int progress, QString filename)
{
m_progress = progress;
m_progressFilename = filename;
emit progressChanged();
emit progressFilenameChanged();
}
QString Engine::createHexDump(char *buffer, int size, int bytesPerLine)
{
QString out;
QString ascDump;
int i;
for (i = 0; i < size; ++i) {
if ((i % bytesPerLine) == 0) { // line change
out += " "+ascDump+"\n"+
QString("%1").arg(QString::number(i, 16), 4, QLatin1Char('0'))+": ";
ascDump.clear();
}
out += QString("%1").arg(QString::number((unsigned char)buffer[i], 16),
2, QLatin1Char('0'))+" ";
if (buffer[i] >= 32 && buffer[i] <= 126)
ascDump += buffer[i];
else
ascDump += ".";
}
// write out remaining asc dump
if ((i % bytesPerLine) > 0) {
int emptyBytes = bytesPerLine - (i % bytesPerLine);
for (int j = 0; j < emptyBytes; ++j) {
out += " ";
}
}
out += " "+ascDump;
return out;
}
QStringList Engine::makeStringList(QString msg, QString str)
{
QStringList list;
list << msg << str << str;
return list;
}