File indexing completed on 2024-04-21 05:48:30

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include "fileCleaner.h"
0005 
0006 #include <mutex>
0007 
0008 #include <QScopeGuard>
0009 #include <QThread>
0010 
0011 void FileCleaner::clean(const QList<std::shared_ptr<File>> &files)
0012 {
0013     // move into our thread
0014     QMetaObject::invokeMethod(
0015         this,
0016         [this, files] {
0017             cleanUp(files);
0018         },
0019         Qt::QueuedConnection);
0020 }
0021 
0022 FileCleaner *FileCleaner::instance()
0023 {
0024     static auto cleaner = new FileCleaner;
0025     static QThread thread;
0026     auto *threadPtr = &thread;
0027     static auto joinThread = qScopeGuard([threadPtr] { // join the thread before it gets stack unwound
0028         threadPtr->quit();
0029         threadPtr->wait();
0030     });
0031 
0032     static std::once_flag once;
0033     std::call_once(once, [] {
0034         cleaner->moveToThread(&thread);
0035         thread.start(QThread::IdlePriority);
0036     });
0037     return cleaner;
0038 }
0039 
0040 void FileCleaner::cleanUp(QList<std::shared_ptr<File>> files)
0041 {
0042     files.clear();
0043 }