File indexing completed on 2024-05-12 04:58:27

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2013-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #include "delayedfilewatcher.h"
0019 
0020 #include <QTimer>
0021 
0022 DelayedFileWatcher::DelayedFileWatcher(QObject* parent)
0023     : QFileSystemWatcher(parent)
0024 {
0025     init();
0026 }
0027 
0028 DelayedFileWatcher::DelayedFileWatcher(const QStringList &paths, QObject* parent)
0029     : QFileSystemWatcher(paths, parent)
0030 {
0031     init();
0032 }
0033 
0034 void DelayedFileWatcher::init()
0035 {
0036     connect(this, &QFileSystemWatcher::directoryChanged, this, &DelayedFileWatcher::slotDirectoryChanged);
0037     connect(this, &QFileSystemWatcher::fileChanged, this, &DelayedFileWatcher::slotFileChanged);
0038 }
0039 
0040 void DelayedFileWatcher::slotDirectoryChanged(const QString &path)
0041 {
0042     m_dirQueue.enqueue(path);
0043     QTimer::singleShot(500, this, &DelayedFileWatcher::dequeueDirectory);
0044 }
0045 
0046 void DelayedFileWatcher::slotFileChanged(const QString &path)
0047 {
0048     m_fileQueue.enqueue(path);
0049     QTimer::singleShot(500, this, &DelayedFileWatcher::dequeueFile);
0050 }
0051 
0052 void DelayedFileWatcher::dequeueDirectory()
0053 {
0054     Q_EMIT delayedDirectoryChanged(m_dirQueue.dequeue());
0055 }
0056 
0057 void DelayedFileWatcher::dequeueFile()
0058 {
0059     Q_EMIT delayedFileChanged(m_fileQueue.dequeue());
0060 }