Krita Source Code Documentation
Loading...
Searching...
No Matches
recorder_snapshots_scanner.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020 Dmitrii Utkin <loentar@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
8#include "recorder_const.h"
9
10#include <QDebug>
11#include <QThread>
12#include <QDir>
13#include <QDirIterator>
14#include <QRegularExpression>
16
18 qRegisterMetaType<SnapshotDirInfoList>("SnapshotDirInfoList");
19}
20
25
30
31void RecorderSnapshotsScanner::setup(const QString &snapshotDirectory)
32{
33 this->snapshotDirectory = snapshotDirectory;
34}
35
37{
38 if (!isRunning())
39 return;
40
41 requestInterruption();
43 terminate();
45 qCritical() << "Unable to stop RecorderSnapshotsScanner";
46 }
47 }
48}
49
51{
53
54 QDirIterator dirIterator(snapshotDirectory, QDir::Dirs | QDir::NoDotAndDotDot);
55
56 while (dirIterator.hasNext()) {
57 dirIterator.next();
58
59 const SnapshotDirInfo &info = readSnapshotDirInfo(dirIterator.filePath());
60 if (isInterruptionRequested()) {
61 return;
62 }
63 if (info.size > 0) {
64 result.append(info);
65 }
66 }
67
68 Q_EMIT scanningFinished(result);
69}
70
71
73{
74 SnapshotDirInfo result;
75 QFileInfo fileInfo(path);
76 result.path = path;
77 result.name = fileInfo.fileName();
78
79 int recordIndex = -1;
80 QDirIterator dirIterator(path, QDir::Files | QDir::NoDotAndDotDot);
81 const QRegularExpression &snapshotFilePattern = RecorderConst::snapshotFilePatternFor(".*");
82
83 while (dirIterator.hasNext()) {
84 dirIterator.next();
85
86 if (isInterruptionRequested()) {
87 return {};
88 }
89
90 const QRegularExpressionMatch &match = snapshotFilePattern.match(dirIterator.fileName());
91 if (!match.hasMatch())
92 continue;
93
94 const QString &filePath = dirIterator.filePath();
95
96 fileInfo.setFile(filePath);
97 result.size += fileInfo.size();
98
99 int index = match.captured(1).toInt();
100 if (recordIndex < index) {
101 recordIndex = index;
102 result.thumbnail = filePath;
103 result.dateTime = fileInfo.lastModified();
104 }
105 }
106
107 return result;
108}
void scanningFinished(SnapshotDirInfoList snapshots)
SnapshotDirInfo readSnapshotDirInfo(const QString &path) const
void setup(const QString &snapshotDirectory)
QRegularExpression snapshotFilePatternFor(const QString &extension)
constexpr int waitThreadTimeoutMs