File indexing completed on 2024-12-22 04:14:55
0001 /* 0002 * SPDX-FileCopyrightText: 2020 Dmitrii Utkin <loentar@gmail.com> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-only 0005 */ 0006 0007 #include "recorder_snapshots_scanner.h" 0008 #include "recorder_const.h" 0009 0010 #include <QDebug> 0011 #include <QThread> 0012 #include <QDir> 0013 #include <QDirIterator> 0014 #include <QRegularExpression> 0015 #include <KisStaticInitializer.h> 0016 0017 KIS_DECLARE_STATIC_INITIALIZER { 0018 qRegisterMetaType<SnapshotDirInfoList>("SnapshotDirInfoList"); 0019 } 0020 0021 RecorderSnapshotsScanner::RecorderSnapshotsScanner() 0022 { 0023 moveToThread(this); 0024 } 0025 0026 RecorderSnapshotsScanner::~RecorderSnapshotsScanner() 0027 { 0028 stop(); 0029 } 0030 0031 void RecorderSnapshotsScanner::setup(const QString &snapshotDirectory) 0032 { 0033 this->snapshotDirectory = snapshotDirectory; 0034 } 0035 0036 void RecorderSnapshotsScanner::stop() 0037 { 0038 if (!isRunning()) 0039 return; 0040 0041 requestInterruption(); 0042 if (!wait(RecorderConst::waitThreadTimeoutMs)) { 0043 terminate(); 0044 if (!wait(RecorderConst::waitThreadTimeoutMs)) { 0045 qCritical() << "Unable to stop RecorderSnapshotsScanner"; 0046 } 0047 } 0048 } 0049 0050 void RecorderSnapshotsScanner::run() 0051 { 0052 QList<SnapshotDirInfo> result; 0053 0054 QDirIterator dirIterator(snapshotDirectory, QDir::Dirs | QDir::NoDotAndDotDot); 0055 0056 while (dirIterator.hasNext()) { 0057 dirIterator.next(); 0058 0059 const SnapshotDirInfo &info = readSnapshotDirInfo(dirIterator.filePath()); 0060 if (isInterruptionRequested()) { 0061 return; 0062 } 0063 if (info.size > 0) { 0064 result.append(info); 0065 } 0066 } 0067 0068 emit scanningFinished(result); 0069 } 0070 0071 0072 SnapshotDirInfo RecorderSnapshotsScanner::readSnapshotDirInfo(const QString &path) const 0073 { 0074 SnapshotDirInfo result; 0075 QFileInfo fileInfo(path); 0076 result.path = path; 0077 result.name = fileInfo.fileName(); 0078 0079 int recordIndex = -1; 0080 QDirIterator dirIterator(path, QDir::Files | QDir::NoDotAndDotDot); 0081 const QRegularExpression &snapshotFilePattern = RecorderConst::snapshotFilePatternFor(".*"); 0082 0083 while (dirIterator.hasNext()) { 0084 dirIterator.next(); 0085 0086 if (isInterruptionRequested()) { 0087 return {}; 0088 } 0089 0090 const QRegularExpressionMatch &match = snapshotFilePattern.match(dirIterator.fileName()); 0091 if (!match.hasMatch()) 0092 continue; 0093 0094 const QString &filePath = dirIterator.filePath(); 0095 0096 fileInfo.setFile(filePath); 0097 result.size += fileInfo.size(); 0098 0099 int index = match.captured(1).toInt(); 0100 if (recordIndex < index) { 0101 recordIndex = index; 0102 result.thumbnail = filePath; 0103 result.dateTime = fileInfo.lastModified(); 0104 } 0105 } 0106 0107 return result; 0108 }