diff options
Diffstat (limited to 'src/labs/folderlistmodel/fileinfothread.cpp')
-rw-r--r-- | src/labs/folderlistmodel/fileinfothread.cpp | 138 |
1 files changed, 75 insertions, 63 deletions
diff --git a/src/labs/folderlistmodel/fileinfothread.cpp b/src/labs/folderlistmodel/fileinfothread.cpp index a93edd3b1b..7b0bea721d 100644 --- a/src/labs/folderlistmodel/fileinfothread.cpp +++ b/src/labs/folderlistmodel/fileinfothread.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "fileinfothread_p.h" #include <qdiriterator.h> @@ -43,7 +7,11 @@ #include <qtimer.h> #include <QDebug> +#include <QtCore/qloggingcategory.h> +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(lcFileInfoThread, "qt.labs.folderlistmodel.fileinfothread") FileInfoThread::FileInfoThread(QObject *parent) : QThread(parent), @@ -54,8 +22,7 @@ FileInfoThread::FileInfoThread(QObject *parent) #endif sortFlags(QDir::Name), needUpdate(true), - folderUpdate(false), - sortUpdate(false), + updateTypes(UpdateType::None), showFiles(true), showDirs(true), showDirsFirst(false), @@ -103,6 +70,7 @@ void FileInfoThread::removePath(const QString &path) void FileInfoThread::setPath(const QString &path) { + qCDebug(lcFileInfoThread) << "setPath called with path" << path; Q_ASSERT(!path.isEmpty()); QMutexLocker locker(&mutex); @@ -117,6 +85,7 @@ void FileInfoThread::setPath(const QString &path) void FileInfoThread::setRootPath(const QString &path) { + qCDebug(lcFileInfoThread) << "setRootPath called with path" << path; Q_ASSERT(!path.isEmpty()); QMutexLocker locker(&mutex); @@ -126,94 +95,106 @@ void FileInfoThread::setRootPath(const QString &path) #if QT_CONFIG(filesystemwatcher) void FileInfoThread::dirChanged(const QString &directoryPath) { + qCDebug(lcFileInfoThread) << "dirChanged called with directoryPath" << directoryPath; Q_UNUSED(directoryPath); QMutexLocker locker(&mutex); - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } #endif void FileInfoThread::setSortFlags(QDir::SortFlags flags) { + qCDebug(lcFileInfoThread) << "setSortFlags called with flags" << flags; + Q_ASSERT(flags != sortFlags); QMutexLocker locker(&mutex); sortFlags = flags; - sortUpdate = true; + updateTypes |= UpdateType::Sort; needUpdate = true; initiateScan(); } void FileInfoThread::setNameFilters(const QStringList & filters) { + qCDebug(lcFileInfoThread) << "setNameFilters called with filters" << filters; QMutexLocker locker(&mutex); nameFilters = filters; - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } void FileInfoThread::setShowFiles(bool show) { + qCDebug(lcFileInfoThread) << "setShowFiles called with show" << show; QMutexLocker locker(&mutex); showFiles = show; - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } void FileInfoThread::setShowDirs(bool showFolders) { + qCDebug(lcFileInfoThread) << "setShowDirs called with showFolders" << showFolders; QMutexLocker locker(&mutex); showDirs = showFolders; - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } void FileInfoThread::setShowDirsFirst(bool show) { + qCDebug(lcFileInfoThread) << "setShowDirsFirst called with show" << show; QMutexLocker locker(&mutex); showDirsFirst = show; - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } void FileInfoThread::setShowDotAndDotDot(bool on) { + qCDebug(lcFileInfoThread) << "setShowDotAndDotDot called with on" << on; QMutexLocker locker(&mutex); showDotAndDotDot = on; - folderUpdate = true; + updateTypes |= UpdateType::Contents; needUpdate = true; initiateScan(); } void FileInfoThread::setShowHidden(bool on) { + qCDebug(lcFileInfoThread) << "setShowHidden called with on" << on; QMutexLocker locker(&mutex); showHidden = on; - folderUpdate = true; + updateTypes |= UpdateType::Contents; needUpdate = true; initiateScan(); } void FileInfoThread::setShowOnlyReadable(bool on) { + qCDebug(lcFileInfoThread) << "setShowOnlyReadable called with on" << on; QMutexLocker locker(&mutex); showOnlyReadable = on; - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } void FileInfoThread::setCaseSensitive(bool on) { + qCDebug(lcFileInfoThread) << "setCaseSensitive called with on" << on; QMutexLocker locker(&mutex); caseSensitive = on; - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } #if QT_CONFIG(filesystemwatcher) void FileInfoThread::updateFile(const QString &path) { + qCDebug(lcFileInfoThread) << "updateFile called with path" << path; Q_UNUSED(path); QMutexLocker locker(&mutex); - folderUpdate = true; + updateTypes |= UpdateType::Contents; initiateScan(); } #endif @@ -271,14 +252,25 @@ void FileInfoThread::runOnce() void FileInfoThread::initiateScan() { #if QT_CONFIG(thread) + qCDebug(lcFileInfoThread) << "initiateScan is about to call condition.wakeAll()"; condition.wakeAll(); #else + qCDebug(lcFileInfoThread) << "initiateScan is about to call runOnce()"; runOnce(); #endif } +QString fileInfoListToString(const QFileInfoList &fileInfoList) +{ + return fileInfoList.size() <= 10 + ? QDebug::toString(fileInfoList) + : QString::fromLatin1("%1 files").arg(fileInfoList.size()); +} + void FileInfoThread::getFileInfos(const QString &path) { + qCDebug(lcFileInfoThread) << "getFileInfos called with path" << path << "- updateType" << updateTypes; + QDir::Filters filter; if (caseSensitive) filter = QDir::CaseSensitive; @@ -303,40 +295,46 @@ void FileInfoThread::getFileInfos(const QString &path) const QFileInfoList fileInfoList = currentDir.entryInfoList(nameFilters, filter, sortFlags); if (!fileInfoList.isEmpty()) { - filePropertyList.reserve(fileInfoList.count()); - for (const QFileInfo &info : fileInfoList) { - //qDebug() << "Adding file : " << info.fileName() << "to list "; + filePropertyList.reserve(fileInfoList.size()); + for (const QFileInfo &info : fileInfoList) filePropertyList << FileProperty(info); - } - if (folderUpdate) { + + if (updateTypes & UpdateType::Contents) { int fromIndex = 0; int toIndex = currentFileList.size()-1; findChangeRange(filePropertyList, fromIndex, toIndex); - folderUpdate = false; currentFileList = filePropertyList; - //qDebug() << "emit directoryUpdated : " << fromIndex << " " << toIndex; + qCDebug(lcFileInfoThread) << "- about to emit directoryUpdated with fromIndex" << fromIndex + << "toIndex" << toIndex << "fileInfoList" << fileInfoListToString(fileInfoList); emit directoryUpdated(path, filePropertyList, fromIndex, toIndex); } else { currentFileList = filePropertyList; - if (sortUpdate) { + if (updateTypes & UpdateType::Sort) { + qCDebug(lcFileInfoThread) << "- about to emit sortFinished - fileInfoList:" + << fileInfoListToString(fileInfoList); emit sortFinished(filePropertyList); - sortUpdate = false; - } else + } else { + qCDebug(lcFileInfoThread) << "- about to emit directoryChanged - fileInfoList:" + << fileInfoListToString(fileInfoList); emit directoryChanged(path, filePropertyList); + } } } else { // The directory is empty - if (folderUpdate) { + if (updateTypes & UpdateType::Contents) { int fromIndex = 0; int toIndex = currentFileList.size()-1; - folderUpdate = false; currentFileList.clear(); + qCDebug(lcFileInfoThread) << "- directory is empty, about to emit directoryUpdated with fromIndex" + << fromIndex << "toIndex" << toIndex; emit directoryUpdated(path, filePropertyList, fromIndex, toIndex); } else { currentFileList.clear(); + qCDebug(lcFileInfoThread) << "- directory is empty, about to emit directoryChanged"; emit directoryChanged(path, filePropertyList); } } + updateTypes = UpdateType::None; needUpdate = false; } @@ -367,3 +365,17 @@ void FileInfoThread::findChangeRange(const QList<FileProperty> &list, int &fromI // For now I let the rest of the list be updated.. toIndex = list.size() > currentFileList.size() ? list.size() - 1 : currentFileList.size() - 1; } + +constexpr FileInfoThread::UpdateTypes operator|(FileInfoThread::UpdateType f1, FileInfoThread::UpdateTypes f2) noexcept +{ + return f2 | f1; +} + +constexpr FileInfoThread::UpdateTypes operator&(FileInfoThread::UpdateType f1, FileInfoThread::UpdateTypes f2) noexcept +{ + return f2 & f1; +} + +QT_END_NAMESPACE + +#include "moc_fileinfothread_p.cpp" |