From fb9ec8ad44decba7b2878370f3711b61614f035a Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Wed, 19 Aug 2020 13:40:34 +0200 Subject: Move QFileSystemModel into QtGui This requires a QAbstractFileIconProvider in QtGui, as the standard QFileIconProvider depends on QStyle, and cannot be moved out of QtWidgets. QAbstractFileIconProvider returns strings for file types, but returns no icons yet. Support for a default icon set might be added in a follow-up commit. Change-Id: Ib9d095cd612fdcf04db62f2e40709fcffe3dc2b7 Fixes: QTBUG-66177 Reviewed-by: Fabian Kosmale --- src/gui/.prev_CMakeLists.txt | 8 + src/gui/CMakeLists.txt | 8 + src/gui/configure.cmake | 7 + src/gui/configure.json | 7 + src/gui/gui.pro | 5 +- src/gui/image/image.pri | 7 +- src/gui/image/qabstractfileiconprovider.cpp | 197 ++ src/gui/image/qabstractfileiconprovider.h | 84 + src/gui/image/qabstractfileiconprovider_p.h | 73 + src/gui/itemmodels/itemmodels.pri | 23 +- src/gui/itemmodels/qfileinfogatherer.cpp | 443 ++++ src/gui/itemmodels/qfileinfogatherer_p.h | 230 +++ src/gui/itemmodels/qfilesystemmodel.cpp | 2165 ++++++++++++++++++++ src/gui/itemmodels/qfilesystemmodel.h | 190 ++ src/gui/itemmodels/qfilesystemmodel_p.h | 323 +++ src/printsupport/dialogs/qprintdialog_unix.cpp | 2 +- src/tools/uic/qclass_lib_map.h | 2 +- src/widgets/CMakeLists.txt | 6 - src/widgets/configure.cmake | 7 - src/widgets/configure.json | 7 - src/widgets/dialogs/dialogs.pri | 11 - src/widgets/dialogs/qfiledialog.cpp | 5 +- src/widgets/dialogs/qfiledialog.h | 6 +- src/widgets/dialogs/qfiledialog_p.h | 4 +- src/widgets/dialogs/qfileinfogatherer.cpp | 443 ---- src/widgets/dialogs/qfileinfogatherer_p.h | 230 --- src/widgets/dialogs/qfilesystemmodel.cpp | 2164 ------------------- src/widgets/dialogs/qfilesystemmodel.h | 190 -- src/widgets/dialogs/qfilesystemmodel_p.h | 323 --- src/widgets/dialogs/qfscompleter_p.h | 2 +- src/widgets/dialogs/qsidebar.cpp | 8 +- src/widgets/itemviews/qfileiconprovider.cpp | 102 +- src/widgets/itemviews/qfileiconprovider.h | 22 +- src/widgets/itemviews/qfileiconprovider_p.h | 5 +- src/widgets/util/qcompleter.cpp | 2 +- tests/auto/gui/itemmodels/CMakeLists.txt | 3 + tests/auto/gui/itemmodels/itemmodels.pro | 5 +- .../gui/itemmodels/qfilesystemmodel/.gitignore | 1 + .../auto/gui/itemmodels/qfilesystemmodel/BLACKLIST | 5 + .../gui/itemmodels/qfilesystemmodel/CMakeLists.txt | 27 + .../qfilesystemmodel/qfilesystemmodel.pro | 13 + .../qfilesystemmodel/tst_qfilesystemmodel.cpp | 1207 +++++++++++ tests/auto/widgets/dialogs/CMakeLists.txt | 3 - tests/auto/widgets/dialogs/dialogs.pro | 3 +- .../dialogs/qfiledialog2/tst_qfiledialog2.cpp | 2 +- .../widgets/dialogs/qfilesystemmodel/.gitignore | 1 - .../widgets/dialogs/qfilesystemmodel/BLACKLIST | 5 - .../dialogs/qfilesystemmodel/CMakeLists.txt | 27 - .../dialogs/qfilesystemmodel/qfilesystemmodel.pro | 13 - .../qfilesystemmodel/tst_qfilesystemmodel.cpp | 1207 ----------- .../auto/widgets/dialogs/qsidebar/tst_qsidebar.cpp | 10 +- 51 files changed, 5062 insertions(+), 4781 deletions(-) create mode 100644 src/gui/image/qabstractfileiconprovider.cpp create mode 100644 src/gui/image/qabstractfileiconprovider.h create mode 100644 src/gui/image/qabstractfileiconprovider_p.h create mode 100644 src/gui/itemmodels/qfileinfogatherer.cpp create mode 100644 src/gui/itemmodels/qfileinfogatherer_p.h create mode 100644 src/gui/itemmodels/qfilesystemmodel.cpp create mode 100644 src/gui/itemmodels/qfilesystemmodel.h create mode 100644 src/gui/itemmodels/qfilesystemmodel_p.h delete mode 100644 src/widgets/dialogs/qfileinfogatherer.cpp delete mode 100644 src/widgets/dialogs/qfileinfogatherer_p.h delete mode 100644 src/widgets/dialogs/qfilesystemmodel.cpp delete mode 100644 src/widgets/dialogs/qfilesystemmodel.h delete mode 100644 src/widgets/dialogs/qfilesystemmodel_p.h create mode 100644 tests/auto/gui/itemmodels/qfilesystemmodel/.gitignore create mode 100644 tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST create mode 100644 tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt create mode 100644 tests/auto/gui/itemmodels/qfilesystemmodel/qfilesystemmodel.pro create mode 100644 tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp delete mode 100644 tests/auto/widgets/dialogs/qfilesystemmodel/.gitignore delete mode 100644 tests/auto/widgets/dialogs/qfilesystemmodel/BLACKLIST delete mode 100644 tests/auto/widgets/dialogs/qfilesystemmodel/CMakeLists.txt delete mode 100644 tests/auto/widgets/dialogs/qfilesystemmodel/qfilesystemmodel.pro delete mode 100644 tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp diff --git a/src/gui/.prev_CMakeLists.txt b/src/gui/.prev_CMakeLists.txt index 8a93a8752a..3dbae0069d 100644 --- a/src/gui/.prev_CMakeLists.txt +++ b/src/gui/.prev_CMakeLists.txt @@ -9,6 +9,7 @@ qt_add_module(Gui PLUGIN_TYPES accessiblebridge platforms platforms/darwin xcbglintegrations platformthemes platforminputcontexts generic iconengines imageformats egldeviceintegrations SOURCES image/qabstractfileiconengine.cpp image/qabstractfileiconengine_p.h + image/qabstractfileiconprovider.cpp image/qabstractfileiconprovider.h image/qabstractfileiconprovider_p.h image/qbitmap.cpp image/qbitmap.h image/qbmphandler.cpp image/qbmphandler_p.h image/qicon.cpp image/qicon.h image/qicon_p.h @@ -323,6 +324,7 @@ qt_extend_target(Gui CONDITION WIN32 gdi32 ole32 user32 + shell32 PUBLIC_LIBRARIES d3d11 dxgi @@ -439,6 +441,12 @@ qt_extend_target(Gui CONDITION QT_FEATURE_draganddrop kernel/qsimpledrag.cpp kernel/qsimpledrag_p.h ) +qt_extend_target(Gui CONDITION QT_FEATURE_filesystemmodel + SOURCES + itemmodels/qfileinfogatherer.cpp itemmodels/qfileinfogatherer_p.h + itemmodels/qfilesystemmodel.cpp itemmodels/qfilesystemmodel.h itemmodels/qfilesystemmodel_p.h +) + qt_extend_target(Gui CONDITION QT_FEATURE_shortcut SOURCES kernel/qkeysequence.cpp kernel/qkeysequence.h kernel/qkeysequence_p.h diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index 79f3bafcd5..3396adf23c 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -60,6 +60,7 @@ qt_add_module(Gui QMAKE_MODULE_CONFIG "${qmake_module_config}" # special case SOURCES image/qabstractfileiconengine.cpp image/qabstractfileiconengine_p.h + image/qabstractfileiconprovider.cpp image/qabstractfileiconprovider.h image/qabstractfileiconprovider_p.h image/qbitmap.cpp image/qbitmap.h image/qbmphandler.cpp image/qbmphandler_p.h image/qicon.cpp image/qicon.h image/qicon_p.h @@ -403,6 +404,7 @@ qt_extend_target(Gui CONDITION WIN32 gdi32 ole32 user32 + shell32 PUBLIC_LIBRARIES d3d11 dxgi @@ -537,6 +539,12 @@ qt_extend_target(Gui CONDITION QT_FEATURE_draganddrop kernel/qsimpledrag.cpp kernel/qsimpledrag_p.h ) +qt_extend_target(Gui CONDITION QT_FEATURE_filesystemmodel + SOURCES + itemmodels/qfileinfogatherer.cpp itemmodels/qfileinfogatherer_p.h + itemmodels/qfilesystemmodel.cpp itemmodels/qfilesystemmodel.h itemmodels/qfilesystemmodel_p.h +) + qt_extend_target(Gui CONDITION QT_FEATURE_shortcut SOURCES kernel/qkeysequence.cpp kernel/qkeysequence.h kernel/qkeysequence_p.h diff --git a/src/gui/configure.cmake b/src/gui/configure.cmake index f0b5d9b1fe..a6d265183f 100644 --- a/src/gui/configure.cmake +++ b/src/gui/configure.cmake @@ -830,6 +830,13 @@ qt_feature("eglfs_x11" PRIVATE LABEL "EGLFS X11" CONDITION QT_FEATURE_eglfs AND QT_FEATURE_xcb_xlib AND QT_FEATURE_egl_x11 ) +qt_feature("filesystemmodel" PUBLIC + SECTION "File I/O" + LABEL "QFileSystemModel" + PURPOSE "Provides a data model for the local filesystem." + CONDITION QT_FEATURE_itemmodel +) +qt_feature_definition("filesystemmodel" "QT_NO_FILESYSTEMMODEL" NEGATE VALUE "1") qt_feature("gif" PRIVATE LABEL "GIF" CONDITION QT_FEATURE_imageformatplugin diff --git a/src/gui/configure.json b/src/gui/configure.json index a88ce54360..a29f870f05 100644 --- a/src/gui/configure.json +++ b/src/gui/configure.json @@ -1548,6 +1548,13 @@ "condition": "features.itemmodel", "output": [ "publicFeature", "feature" ] }, + "filesystemmodel": { + "label": "QFileSystemModel", + "purpose": "Provides a data model for the local filesystem.", + "section": "File I/O", + "condition": "features.itemmodel", + "output": [ "publicFeature", "feature" ] + }, "imageformatplugin": { "label": "QImageIOPlugin", "purpose": "Provides a base for writing a image format plugins.", diff --git a/src/gui/gui.pro b/src/gui/gui.pro index 4e79e31e6e..13631aba67 100644 --- a/src/gui/gui.pro +++ b/src/gui/gui.pro @@ -51,7 +51,10 @@ QMAKE_LIBS += $$QMAKE_LIBS_GUI load(qt_module) load(cmake_functions) -win32: CMAKE_WINDOWS_BUILD = True +win32: { + CMAKE_WINDOWS_BUILD = True + QMAKE_USE_PRIVATE += shell32 +} qtConfig(egl) { CMAKE_EGL_LIBS = $$cmakeProcessLibs($$QMAKE_LIBS_EGL) diff --git a/src/gui/image/image.pri b/src/gui/image/image.pri index bf0aa519a8..f503c97b82 100644 --- a/src/gui/image/image.pri +++ b/src/gui/image/image.pri @@ -26,7 +26,9 @@ HEADERS += \ image/qiconloader_p.h \ image/qiconengine.h \ image/qiconengineplugin.h \ - image/qabstractfileiconengine_p.h + image/qabstractfileiconengine_p.h \ + image/qabstractfileiconprovider.h \ + image/qabstractfileiconprovider_p.h SOURCES += \ image/qbitmap.cpp \ @@ -48,7 +50,8 @@ SOURCES += \ image/qiconloader.cpp \ image/qiconengine.cpp \ image/qiconengineplugin.cpp \ - image/qabstractfileiconengine.cpp + image/qabstractfileiconengine.cpp \ + image/qabstractfileiconprovider.cpp qtConfig(movie) { HEADERS += image/qmovie.h diff --git a/src/gui/image/qabstractfileiconprovider.cpp b/src/gui/image/qabstractfileiconprovider.cpp new file mode 100644 index 0000000000..2c2a694b60 --- /dev/null +++ b/src/gui/image/qabstractfileiconprovider.cpp @@ -0,0 +1,197 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module 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$ +** +****************************************************************************/ + +#include "qabstractfileiconprovider.h" + +#include +#include +#include + +QT_BEGIN_NAMESPACE + +QAbstractFileIconProviderPrivate::QAbstractFileIconProviderPrivate(QAbstractFileIconProvider *q) + : q_ptr(q) +{} + +QAbstractFileIconProviderPrivate::~QAbstractFileIconProviderPrivate() = default; + +/*! + \class QAbstractFileIconProvider + + \inmodule QtGui + \since 6.0 + + \brief The QAbstractFileIconProvider class provides file icons for the QFileSystemModel class. +*/ + +/*! + \enum QAbstractFileIconProvider::IconType + + \value Computer + \value Desktop + \value Trashcan + \value Network + \value Drive + \value Folder + \value File +*/ + +/*! + \enum QAbstractFileIconProvider::Option + + \value DontUseCustomDirectoryIcons Always use the default directory icon. + Some platforms allow the user to set a different icon. Custom icon lookup + cause a big performance impact over network or removable drives. +*/ + +/*! + Constructs a file icon provider. +*/ +QAbstractFileIconProvider::QAbstractFileIconProvider() + : d_ptr(new QAbstractFileIconProviderPrivate(this)) +{ +} + +/*! + \internal +*/ +QAbstractFileIconProvider::QAbstractFileIconProvider(QAbstractFileIconProviderPrivate &dd) + : d_ptr(&dd) +{} + +/*! + Destroys the file icon provider. +*/ + +QAbstractFileIconProvider::~QAbstractFileIconProvider() = default; + + +/*! + Sets \a options that affect the icon provider. + \sa options() +*/ + +void QAbstractFileIconProvider::setOptions(QAbstractFileIconProvider::Options options) +{ + Q_D(QAbstractFileIconProvider); + d->options = options; +} + +/*! + Returns all the options that affect the icon provider. + By default, all options are disabled. + \sa setOptions() +*/ + +QAbstractFileIconProvider::Options QAbstractFileIconProvider::options() const +{ + Q_D(const QAbstractFileIconProvider); + return d->options; +} + +/*! + Returns an icon set for the given \a type. +*/ + +QIcon QAbstractFileIconProvider::icon(IconType type) const +{ + Q_UNUSED(type); + return {}; +} + +/*! + Returns an icon for the file described by \a info. +*/ + +QIcon QAbstractFileIconProvider::icon(const QFileInfo &info) const +{ + Q_UNUSED(info); + return {}; +} + +/*! + Returns the type of the file described by \a info. +*/ + +QString QAbstractFileIconProvider::type(const QFileInfo &info) const +{ + /* ### Qt 6 These string translations being in the QFileDialog context is not ideal, + but translating them in QFileDialog context only in the QFileIconProvider subclass + isn't either (it basically requires a duplication of the entire function). + Either we change it to QAbstractFileIconProvider context (invalidates existing + translations, but that's probably ok), or make a runtime lookup and use QFileDIalog + context only if QGuiApplication is a QApplication. + */ + if (QFileSystemEntry::isRootPath(info.absoluteFilePath())) + return QGuiApplication::translate("QFileDialog", "Drive"); + if (info.isFile()) { + // ### could use QMimeDatabase::mimeTypeForFile(const QFileInfo&) here + if (!info.suffix().isEmpty()) { + //: %1 is a file name suffix, for example txt + return QGuiApplication::translate("QFileDialog", "%1 File").arg(info.suffix()); + } + return QGuiApplication::translate("QFileDialog", "File"); + } + + if (info.isDir()) +#ifdef Q_OS_WIN + return QGuiApplication::translate("QFileDialog", "File Folder", "Match Windows Explorer"); +#else + return QGuiApplication::translate("QFileDialog", "Folder", "All other platforms"); +#endif + // Windows - "File Folder" + // macOS - "Folder" + // Konqueror - "Folder" + // Nautilus - "folder" + + if (info.isSymLink()) +#ifdef Q_OS_MACOS + return QGuiApplication::translate("QFileDialog", "Alias", "macOS Finder"); +#else + return QGuiApplication::translate("QFileDialog", "Shortcut", "All other platforms"); +#endif + // macOS - "Alias" + // Windows - "Shortcut" + // Konqueror - "Folder" or "TXT File" i.e. what it is pointing to + // Nautilus - "link to folder" or "link to object file", same as Konqueror + + return QGuiApplication::translate("QFileDialog", "Unknown"); +} + +QT_END_NAMESPACE diff --git a/src/gui/image/qabstractfileiconprovider.h b/src/gui/image/qabstractfileiconprovider.h new file mode 100644 index 0000000000..fc107e0ca3 --- /dev/null +++ b/src/gui/image/qabstractfileiconprovider.h @@ -0,0 +1,84 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module 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$ +** +****************************************************************************/ + +#ifndef QABSTRACTFILEICONPROVIDER_H +#define QABSTRACTFILEICONPROVIDER_H + +#include +#include +#include +#include + +QT_BEGIN_NAMESPACE + +class QAbstractFileIconProviderPrivate; + +class Q_GUI_EXPORT QAbstractFileIconProvider +{ +public: + enum IconType { Computer, Desktop, Trashcan, Network, Drive, Folder, File }; + enum Option { + DontUseCustomDirectoryIcons = 0x00000001 + }; + Q_DECLARE_FLAGS(Options, Option) + + QAbstractFileIconProvider(); + virtual ~QAbstractFileIconProvider(); + + virtual QIcon icon(IconType) const; + virtual QIcon icon(const QFileInfo &) const; + virtual QString type(const QFileInfo &) const; + + virtual void setOptions(Options); + virtual Options options() const; + +protected: + QAbstractFileIconProvider(QAbstractFileIconProviderPrivate &dd); + QScopedPointer d_ptr; + +private: + Q_DECLARE_PRIVATE(QAbstractFileIconProvider) + Q_DISABLE_COPY(QAbstractFileIconProvider) +}; + +Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractFileIconProvider::Options) + +QT_END_NAMESPACE + +#endif // QABSTRACTFILEICONPROVIDER_H diff --git a/src/gui/image/qabstractfileiconprovider_p.h b/src/gui/image/qabstractfileiconprovider_p.h new file mode 100644 index 0000000000..db7ac6db1b --- /dev/null +++ b/src/gui/image/qabstractfileiconprovider_p.h @@ -0,0 +1,73 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtGui module 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$ +** +****************************************************************************/ + +#ifndef QABSTRACTFILEICONPROVIDER_P_H +#define QABSTRACTFILEICONPROVIDER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include "qabstractfileiconprovider.h" + +QT_BEGIN_NAMESPACE + +class Q_GUI_EXPORT QAbstractFileIconProviderPrivate +{ + Q_DECLARE_PUBLIC(QAbstractFileIconProvider) + +public: + QAbstractFileIconProviderPrivate(QAbstractFileIconProvider *q); + virtual ~QAbstractFileIconProviderPrivate(); + + QAbstractFileIconProvider *q_ptr = nullptr; + QAbstractFileIconProvider::Options options = {}; +}; + +QT_END_NAMESPACE + +#endif // QABSTRACTFILEICONPROVIDER_P_H diff --git a/src/gui/itemmodels/itemmodels.pri b/src/gui/itemmodels/itemmodels.pri index cab0594174..4de6cc29e2 100644 --- a/src/gui/itemmodels/itemmodels.pri +++ b/src/gui/itemmodels/itemmodels.pri @@ -1,8 +1,19 @@ -!qtConfig(standarditemmodel): return() +qtConfig(standarditemmodel) { + HEADERS += \ + itemmodels/qstandarditemmodel.h \ + itemmodels/qstandarditemmodel_p.h \ -HEADERS += \ - itemmodels/qstandarditemmodel.h \ - itemmodels/qstandarditemmodel_p.h \ + SOURCES += \ + itemmodels/qstandarditemmodel.cpp \ +} -SOURCES += \ - itemmodels/qstandarditemmodel.cpp \ +qtConfig(filesystemmodel) { + HEADERS += \ + itemmodels/qfilesystemmodel.h \ + itemmodels/qfilesystemmodel_p.h \ + itemmodels/qfileinfogatherer_p.h + + SOURCES += \ + itemmodels/qfilesystemmodel.cpp \ + itemmodels/qfileinfogatherer.cpp +} diff --git a/src/gui/itemmodels/qfileinfogatherer.cpp b/src/gui/itemmodels/qfileinfogatherer.cpp new file mode 100644 index 0000000000..417c3d7e42 --- /dev/null +++ b/src/gui/itemmodels/qfileinfogatherer.cpp @@ -0,0 +1,443 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWidgets module 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$ +** +****************************************************************************/ + +#include "qfileinfogatherer_p.h" +#include +#include +#include +#ifndef Q_OS_WIN +# include +# include +#endif +#if defined(Q_OS_VXWORKS) +# include "qplatformdefs.h" +#endif + +QT_BEGIN_NAMESPACE + +#ifdef QT_BUILD_INTERNAL +static QBasicAtomicInt fetchedRoot = Q_BASIC_ATOMIC_INITIALIZER(false); +Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot() +{ + fetchedRoot.storeRelaxed(false); +} + +Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot() +{ + return fetchedRoot.loadRelaxed(); +} +#endif + +static QString translateDriveName(const QFileInfo &drive) +{ + QString driveName = drive.absoluteFilePath(); +#ifdef Q_OS_WIN + if (driveName.startsWith(QLatin1Char('/'))) // UNC host + return drive.fileName(); + if (driveName.endsWith(QLatin1Char('/'))) + driveName.chop(1); +#endif // Q_OS_WIN + return driveName; +} + +/*! + Creates thread +*/ +QFileInfoGatherer::QFileInfoGatherer(QObject *parent) + : QThread(parent) + , m_iconProvider(&defaultProvider) +{ + start(LowPriority); +} + +/*! + Destroys thread +*/ +QFileInfoGatherer::~QFileInfoGatherer() +{ + abort.storeRelaxed(true); + QMutexLocker locker(&mutex); + condition.wakeAll(); + locker.unlock(); + wait(); +} + +void QFileInfoGatherer::setResolveSymlinks(bool enable) +{ + Q_UNUSED(enable); +#ifdef Q_OS_WIN + m_resolveSymlinks = enable; +#endif +} + +void QFileInfoGatherer::driveAdded() +{ + fetchExtendedInformation(QString(), QStringList()); +} + +void QFileInfoGatherer::driveRemoved() +{ + QStringList drives; + const QFileInfoList driveInfoList = QDir::drives(); + for (const QFileInfo &fi : driveInfoList) + drives.append(translateDriveName(fi)); + newListOfFiles(QString(), drives); +} + +bool QFileInfoGatherer::resolveSymlinks() const +{ +#ifdef Q_OS_WIN + return m_resolveSymlinks; +#else + return false; +#endif +} + +void QFileInfoGatherer::setIconProvider(QAbstractFileIconProvider *provider) +{ + m_iconProvider = provider; +} + +QAbstractFileIconProvider *QFileInfoGatherer::iconProvider() const +{ + return m_iconProvider; +} + +/*! + Fetch extended information for all \a files in \a path + + \sa updateFile(), update(), resolvedName() +*/ +void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files) +{ + QMutexLocker locker(&mutex); + // See if we already have this dir/file in our queue + int loc = this->path.lastIndexOf(path); + while (loc > 0) { + if (this->files.at(loc) == files) { + return; + } + loc = this->path.lastIndexOf(path, loc - 1); + } + this->path.push(path); + this->files.push(files); + condition.wakeAll(); + +#if QT_CONFIG(filesystemwatcher) + if (files.isEmpty() + && !path.isEmpty() + && !path.startsWith(QLatin1String("//")) /*don't watch UNC path*/) { + if (!watchedDirectories().contains(path)) + watchPaths(QStringList(path)); + } +#endif +} + +/*! + Fetch extended information for all \a filePath + + \sa fetchExtendedInformation() +*/ +void QFileInfoGatherer::updateFile(const QString &filePath) +{ + QString dir = filePath.mid(0, filePath.lastIndexOf(QLatin1Char('/'))); + QString fileName = filePath.mid(dir.length() + 1); + fetchExtendedInformation(dir, QStringList(fileName)); +} + +QStringList QFileInfoGatherer::watchedFiles() const +{ +#if QT_CONFIG(filesystemwatcher) + if (m_watcher) + return m_watcher->files(); +#endif + return {}; +} + +QStringList QFileInfoGatherer::watchedDirectories() const +{ +#if QT_CONFIG(filesystemwatcher) + if (m_watcher) + return m_watcher->directories(); +#endif + return {}; +} + +void QFileInfoGatherer::createWatcher() +{ +#if QT_CONFIG(filesystemwatcher) + m_watcher = new QFileSystemWatcher(this); + connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &QFileInfoGatherer::list); + connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &QFileInfoGatherer::updateFile); +# if defined(Q_OS_WIN) + const QVariant listener = m_watcher->property("_q_driveListener"); + if (listener.canConvert()) { + if (QObject *driveListener = listener.value()) { + connect(driveListener, SIGNAL(driveAdded()), this, SLOT(driveAdded())); + connect(driveListener, SIGNAL(driveRemoved()), this, SLOT(driveRemoved())); + } + } +# endif // Q_OS_WIN +#endif +} + +void QFileInfoGatherer::watchPaths(const QStringList &paths) +{ +#if QT_CONFIG(filesystemwatcher) + if (m_watching) { + if (m_watcher == nullptr) + createWatcher(); + m_watcher->addPaths(paths); + } +#else + Q_UNUSED(paths); +#endif +} + +void QFileInfoGatherer::unwatchPaths(const QStringList &paths) +{ +#if QT_CONFIG(filesystemwatcher) + if (m_watcher && !paths.isEmpty()) + m_watcher->removePaths(paths); +#else + Q_UNUSED(paths); +#endif +} + +bool QFileInfoGatherer::isWatching() const +{ + bool result = false; +#if QT_CONFIG(filesystemwatcher) + QMutexLocker locker(&mutex); + result = m_watching; +#endif + return result; +} + +void QFileInfoGatherer::setWatching(bool v) +{ +#if QT_CONFIG(filesystemwatcher) + QMutexLocker locker(&mutex); + if (v != m_watching) { + if (!v) { + delete m_watcher; + m_watcher = nullptr; + } + m_watching = v; + } +#else + Q_UNUSED(v); +#endif +} + +/* + List all files in \a directoryPath + + \sa listed() +*/ +void QFileInfoGatherer::clear() +{ +#if QT_CONFIG(filesystemwatcher) + QMutexLocker locker(&mutex); + unwatchPaths(watchedFiles()); + unwatchPaths(watchedDirectories()); +#endif +} + +/* + Remove a \a path from the watcher + + \sa listed() +*/ +void QFileInfoGatherer::removePath(const QString &path) +{ +#if QT_CONFIG(filesystemwatcher) + QMutexLocker locker(&mutex); + unwatchPaths(QStringList(path)); +#else + Q_UNUSED(path); +#endif +} + +/* + List all files in \a directoryPath + + \sa listed() +*/ +void QFileInfoGatherer::list(const QString &directoryPath) +{ + fetchExtendedInformation(directoryPath, QStringList()); +} + +/* + Until aborted wait to fetch a directory or files +*/ +void QFileInfoGatherer::run() +{ + forever { + QMutexLocker locker(&mutex); + while (!abort.loadRelaxed() && path.isEmpty()) + condition.wait(&mutex); + if (abort.loadRelaxed()) + return; + const QString thisPath = qAsConst(path).front(); + path.pop_front(); + const QStringList thisList = qAsConst(files).front(); + files.pop_front(); + locker.unlock(); + + getFileInfos(thisPath, thisList); + } +} + +QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const +{ + QExtendedInformation info(fileInfo); + info.icon = m_iconProvider->icon(fileInfo); + info.displayType = m_iconProvider->type(fileInfo); +#if QT_CONFIG(filesystemwatcher) + // ### Not ready to listen all modifications by default + static const bool watchFiles = qEnvironmentVariableIsSet("QT_FILESYSTEMMODEL_WATCH_FILES"); + if (watchFiles) { + if (!fileInfo.exists() && !fileInfo.isSymLink()) { + const_cast(this)-> + unwatchPaths(QStringList(fileInfo.absoluteFilePath())); + } else { + const QString path = fileInfo.absoluteFilePath(); + if (!path.isEmpty() && fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() + && !watchedFiles().contains(path)) { + const_cast(this)->watchPaths(QStringList(path)); + } + } + } +#endif // filesystemwatcher + +#ifdef Q_OS_WIN + if (m_resolveSymlinks && info.isSymLink(/* ignoreNtfsSymLinks = */ true)) { + QFileInfo resolvedInfo(fileInfo.symLinkTarget()); + resolvedInfo = resolvedInfo.canonicalFilePath(); + if (resolvedInfo.exists()) { + emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName()); + } + } +#endif + return info; +} + +/* + Get specific file info's, batch the files so update when we have 100 + items and every 200ms after that + */ +void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files) +{ + // List drives + if (path.isEmpty()) { +#ifdef QT_BUILD_INTERNAL + fetchedRoot.storeRelaxed(true); +#endif + QFileInfoList infoList; + if (files.isEmpty()) { + infoList = QDir::drives(); + } else { + infoList.reserve(files.count()); + for (const auto &file : files) + infoList << QFileInfo(file); + } + QList> updatedFiles; + updatedFiles.reserve(infoList.count()); + for (int i = infoList.count() - 1; i >= 0; --i) { + QFileInfo driveInfo = infoList.at(i); + driveInfo.stat(); + QString driveName = translateDriveName(driveInfo); + updatedFiles.append(QPair(driveName, driveInfo)); + } + emit updates(path, updatedFiles); + return; + } + + QElapsedTimer base; + base.start(); + QFileInfo fileInfo; + bool firstTime = true; + QList> updatedFiles; + QStringList filesToCheck = files; + + QStringList allFiles; + if (files.isEmpty()) { + QDirIterator dirIt(path, QDir::AllEntries | QDir::System | QDir::Hidden); + while (!abort.loadRelaxed() && dirIt.hasNext()) { + dirIt.next(); + fileInfo = dirIt.fileInfo(); + fileInfo.stat(); + allFiles.append(fileInfo.fileName()); + fetch(fileInfo, base, firstTime, updatedFiles, path); + } + } + if (!allFiles.isEmpty()) + emit newListOfFiles(path, allFiles); + + QStringList::const_iterator filesIt = filesToCheck.constBegin(); + while (!abort.loadRelaxed() && filesIt != filesToCheck.constEnd()) { + fileInfo.setFile(path + QDir::separator() + *filesIt); + ++filesIt; + fileInfo.stat(); + fetch(fileInfo, base, firstTime, updatedFiles, path); + } + if (!updatedFiles.isEmpty()) + emit updates(path, updatedFiles); + emit directoryLoaded(path); +} + +void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, + QList> &updatedFiles, const QString &path) +{ + updatedFiles.append(QPair(fileInfo.fileName(), fileInfo)); + QElapsedTimer current; + current.start(); + if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(current) > 1000) { + emit updates(path, updatedFiles); + updatedFiles.clear(); + base = current; + firstTime = false; + } +} + +QT_END_NAMESPACE + +#include "moc_qfileinfogatherer_p.cpp" diff --git a/src/gui/itemmodels/qfileinfogatherer_p.h b/src/gui/itemmodels/qfileinfogatherer_p.h new file mode 100644 index 0000000000..374331a588 --- /dev/null +++ b/src/gui/itemmodels/qfileinfogatherer_p.h @@ -0,0 +1,230 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWidgets module 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$ +** +****************************************************************************/ + +#ifndef QFILEINFOGATHERER_H +#define QFILEINFOGATHERER_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include + +#include +#include +#include +#if QT_CONFIG(filesystemwatcher) +#include +#endif +#include +#include +#include +#include +#include +#include + +#include + +QT_REQUIRE_CONFIG(filesystemmodel); + +QT_BEGIN_NAMESPACE + +class QExtendedInformation { +public: + enum Type { Dir, File, System }; + + QExtendedInformation() {} + QExtendedInformation(const QFileInfo &info) : mFileInfo(info) {} + + inline bool isDir() { return type() == Dir; } + inline bool isFile() { return type() == File; } + inline bool isSystem() { return type() == System; } + + bool operator ==(const QExtendedInformation &fileInfo) const { + return mFileInfo == fileInfo.mFileInfo + && displayType == fileInfo.displayType + && permissions() == fileInfo.permissions() + && lastModified() == fileInfo.lastModified(); + } + +#ifndef QT_NO_FSFILEENGINE + bool isCaseSensitive() const { + return QFileSystemEngine::isCaseSensitive(); + } +#endif + + QFile::Permissions permissions() const { + return mFileInfo.permissions(); + } + + Type type() const { + if (mFileInfo.isDir()) { + return QExtendedInformation::Dir; + } + if (mFileInfo.isFile()) { + return QExtendedInformation::File; + } + if (!mFileInfo.exists() && mFileInfo.isSymLink()) { + return QExtendedInformation::System; + } + return QExtendedInformation::System; + } + + bool isSymLink(bool ignoreNtfsSymLinks = false) const + { + if (ignoreNtfsSymLinks) { +#ifdef Q_OS_WIN + return !mFileInfo.suffix().compare(QLatin1String("lnk"), Qt::CaseInsensitive); +#endif + } + return mFileInfo.isSymLink(); + } + + bool isHidden() const { + return mFileInfo.isHidden(); + } + + QFileInfo fileInfo() const { + return mFileInfo; + } + + QDateTime lastModified() const { + return mFileInfo.lastModified(); + } + + qint64 size() const { + qint64 size = -1; + if (type() == QExtendedInformation::Dir) + size = 0; + if (type() == QExtendedInformation::File) + size = mFileInfo.size(); + if (!mFileInfo.exists() && !mFileInfo.isSymLink()) + size = -1; + return size; + } + + QString displayType; + QIcon icon; + +private : + QFileInfo mFileInfo; +}; + +class QFileIconProvider; + +class Q_GUI_EXPORT QFileInfoGatherer : public QThread +{ +Q_OBJECT + +Q_SIGNALS: + void updates(const QString &directory, const QList> &updates); + void newListOfFiles(const QString &directory, const QStringList &listOfFiles) const; + void nameResolved(const QString &fileName, const QString &resolvedName) const; + void directoryLoaded(const QString &path); + +public: + explicit QFileInfoGatherer(QObject *parent = nullptr); + ~QFileInfoGatherer(); + + QStringList watchedFiles() const; + QStringList watchedDirectories() const; + void watchPaths(const QStringList &paths); + void unwatchPaths(const QStringList &paths); + + bool isWatching() const; + void setWatching(bool v); + + // only callable from this->thread(): + void clear(); + void removePath(const QString &path); + QExtendedInformation getInfo(const QFileInfo &info) const; + QAbstractFileIconProvider *iconProvider() const; + bool resolveSymlinks() const; + +public Q_SLOTS: + void list(const QString &directoryPath); + void fetchExtendedInformation(const QString &path, const QStringList &files); + void updateFile(const QString &path); + void setResolveSymlinks(bool enable); + void setIconProvider(QAbstractFileIconProvider *provider); + +private Q_SLOTS: + void driveAdded(); + void driveRemoved(); + +private: + void run() override; + // called by run(): + void getFileInfos(const QString &path, const QStringList &files); + void fetch(const QFileInfo &info, QElapsedTimer &base, bool &firstTime, + QList> &updatedFiles, const QString &path); + +private: + void createWatcher(); + + mutable QMutex mutex; + // begin protected by mutex + QWaitCondition condition; + QStack path; + QStack files; + // end protected by mutex + QAtomicInt abort; + +#if QT_CONFIG(filesystemwatcher) + QFileSystemWatcher *m_watcher = nullptr; +#endif + QAbstractFileIconProvider *m_iconProvider; // not accessed by run() + QAbstractFileIconProvider defaultProvider; +#ifdef Q_OS_WIN + bool m_resolveSymlinks = true; // not accessed by run() +#endif +#if QT_CONFIG(filesystemwatcher) + bool m_watching = true; +#endif +}; + +QT_END_NAMESPACE +#endif // QFILEINFOGATHERER_H diff --git a/src/gui/itemmodels/qfilesystemmodel.cpp b/src/gui/itemmodels/qfilesystemmodel.cpp new file mode 100644 index 0000000000..481f11162a --- /dev/null +++ b/src/gui/itemmodels/qfilesystemmodel.cpp @@ -0,0 +1,2165 @@ +/**************************************************************************** +** +** Copyright (C) 2020 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWidgets module 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$ +** +****************************************************************************/ + +#include "qfilesystemmodel_p.h" +#include "qfilesystemmodel.h" +#include +#include +#include +#include +#include +#include +#if QT_CONFIG(regularexpression) +# include +#endif + +#include + +#ifdef Q_OS_WIN +# include +# include +# include +#endif + +QT_BEGIN_NAMESPACE + +/*! + \enum QFileSystemModel::Roles + \value FileIconRole + \value FilePathRole + \value FileNameRole + \value FilePermissions +*/ + +/*! + \class QFileSystemModel + \since 4.4 + + \brief The QFileSystemModel class provides a data model for the local filesystem. + + \ingroup model-view + \inmodule QtWidgets + + This class provides access to the local filesystem, providing functions + for renaming and removing files and directories, and for creating new + directories. In the simplest case, it can be used with a suitable display + widget as part of a browser or filter. + + QFileSystemModel can be accessed using the standard interface provided by + QAbstractItemModel, but it also provides some convenience functions that are + specific to a directory model. + The fileInfo(), isDir(), fileName() and filePath() functions provide information + about the underlying files and directories related to items in the model. + Directories can be created and removed using mkdir(), rmdir(). + + \note QFileSystemModel requires an instance of \l QApplication. + + \section1 Example Usage + + A directory model that displays the contents of a default directory + is usually constructed with a parent object: + + \snippet shareddirmodel/main.cpp 2 + + A tree view can be used to display the contents of the model + + \snippet shareddirmodel/main.cpp 4 + + and the contents of a particular directory can be displayed by + setting the tree view's root index: + + \snippet shareddirmodel/main.cpp 7 + + The view's root index can be used to control how much of a + hierarchical model is displayed. QFileSystemModel provides a convenience + function that returns a suitable model index for a path to a + directory within the model. + + \section1 Caching and Performance + + QFileSystemModel will not fetch any files or directories until setRootPath() + is called. This will prevent any unnecessary querying on the file system + until that point such as listing the drives on Windows. + + QFileSystemModel uses a separate thread to populate itself so it will not + cause the main thread to hang as the file system is being queried. + Calls to rowCount() will return 0 until the model populates a directory. + + QFileSystemModel keeps a cache with file information. The cache is + automatically kept up to date using the QFileSystemWatcher. + + \sa {Model Classes} +*/ + +/*! + \fn bool QFileSystemModel::rmdir(const QModelIndex &index) + + Removes the directory corresponding to the model item \a index in the + file system model and \b{deletes the corresponding directory from the + file system}, returning true if successful. If the directory cannot be + removed, false is returned. + + \warning This function deletes directories from the file system; it does + \b{not} move them to a location where they can be recovered. + + \sa remove() +*/ + +/*! + \fn QIcon QFileSystemModel::fileName(const QModelIndex &index) const + + Returns the file name for the item stored in the model under the given + \a index. +*/ + +/*! + \fn QIcon QFileSystemModel::fileIcon(const QModelIndex &index) const + + Returns the icon for the item stored in the model under the given + \a index. +*/ + +/*! + \fn QFileInfo QFileSystemModel::fileInfo(const QModelIndex &index) const + + Returns the QFileInfo for the item stored in the model under the given + \a index. +*/ +QFileInfo QFileSystemModel::fileInfo(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + return d->node(index)->fileInfo(); +} + +/*! + \fn void QFileSystemModel::rootPathChanged(const QString &newPath); + + This signal is emitted whenever the root path has been changed to a \a newPath. +*/ + +/*! + \fn void QFileSystemModel::fileRenamed(const QString &path, const QString &oldName, const QString &newName) + + This signal is emitted whenever a file with the \a oldName is successfully + renamed to \a newName. The file is located in in the directory \a path. +*/ + +/*! + \since 4.7 + \fn void QFileSystemModel::directoryLoaded(const QString &path) + + This signal is emitted when the gatherer thread has finished to load the \a path. + +*/ + +/*! + \fn bool QFileSystemModel::remove(const QModelIndex &index) + + Removes the model item \a index from the file system model and \b{deletes the + corresponding file from the file system}, returning true if successful. If the + item cannot be removed, false is returned. + + \warning This function deletes files from the file system; it does \b{not} + move them to a location where they can be recovered. + + \sa rmdir() +*/ + +bool QFileSystemModel::remove(const QModelIndex &aindex) +{ + Q_D(QFileSystemModel); + + const QString path = d->filePath(aindex); + const QFileInfo fileInfo(path); +#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) + // QTBUG-65683: Remove file system watchers prior to deletion to prevent + // failure due to locked files on Windows. + const QStringList watchedPaths = d->unwatchPathsAt(aindex); +#endif // filesystemwatcher && Q_OS_WIN + const bool success = (fileInfo.isFile() || fileInfo.isSymLink()) + ? QFile::remove(path) : QDir(path).removeRecursively(); +#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) + if (!success) + d->watchPaths(watchedPaths); +#endif // filesystemwatcher && Q_OS_WIN + return success; +} + +/*! + Constructs a file system model with the given \a parent. +*/ +QFileSystemModel::QFileSystemModel(QObject *parent) : + QFileSystemModel(*new QFileSystemModelPrivate, parent) +{ +} + +/*! + \internal +*/ +QFileSystemModel::QFileSystemModel(QFileSystemModelPrivate &dd, QObject *parent) + : QAbstractItemModel(dd, parent) +{ + Q_D(QFileSystemModel); + d->init(); +} + +/*! + Destroys this file system model. +*/ +QFileSystemModel::~QFileSystemModel() = default; + +/*! + \reimp +*/ +QModelIndex QFileSystemModel::index(int row, int column, const QModelIndex &parent) const +{ + Q_D(const QFileSystemModel); + if (row < 0 || column < 0 || row >= rowCount(parent) || column >= columnCount(parent)) + return QModelIndex(); + + // get the parent node + QFileSystemModelPrivate::QFileSystemNode *parentNode = (d->indexValid(parent) ? d->node(parent) : + const_cast(&d->root)); + Q_ASSERT(parentNode); + + // now get the internal pointer for the index + const int i = d->translateVisibleLocation(parentNode, row); + if (i >= parentNode->visibleChildren.size()) + return QModelIndex(); + const QString &childName = parentNode->visibleChildren.at(i); + const QFileSystemModelPrivate::QFileSystemNode *indexNode = parentNode->children.value(childName); + Q_ASSERT(indexNode); + + return createIndex(row, column, const_cast(indexNode)); +} + +/*! + \reimp +*/ +QModelIndex QFileSystemModel::sibling(int row, int column, const QModelIndex &idx) const +{ + if (row == idx.row() && column < QFileSystemModelPrivate::NumColumns) { + // cheap sibling operation: just adjust the column: + return createIndex(row, column, idx.internalPointer()); + } else { + // for anything else: call the default implementation + // (this could probably be optimized, too): + return QAbstractItemModel::sibling(row, column, idx); + } +} + +/*! + \overload + + Returns the model item index for the given \a path and \a column. +*/ +QModelIndex QFileSystemModel::index(const QString &path, int column) const +{ + Q_D(const QFileSystemModel); + QFileSystemModelPrivate::QFileSystemNode *node = d->node(path, false); + return d->index(node, column); +} + +/*! + \internal + + Return the QFileSystemNode that goes to index. + */ +QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QModelIndex &index) const +{ + if (!index.isValid()) + return const_cast(&root); + QFileSystemModelPrivate::QFileSystemNode *indexNode = static_cast(index.internalPointer()); + Q_ASSERT(indexNode); + return indexNode; +} + +#ifdef Q_OS_WIN32 +static QString qt_GetLongPathName(const QString &strShortPath) +{ + if (strShortPath.isEmpty() + || strShortPath == QLatin1String(".") || strShortPath == QLatin1String("..")) + return strShortPath; + if (strShortPath.length() == 2 && strShortPath.endsWith(QLatin1Char(':'))) + return strShortPath.toUpper(); + const QString absPath = QDir(strShortPath).absolutePath(); + if (absPath.startsWith(QLatin1String("//")) + || absPath.startsWith(QLatin1String("\\\\"))) // unc + return QDir::fromNativeSeparators(absPath); + if (absPath.startsWith(QLatin1Char('/'))) + return QString(); + const QString inputString = QLatin1String("\\\\?\\") + QDir::toNativeSeparators(absPath); + QVarLengthArray buffer(MAX_PATH); + DWORD result = ::GetLongPathName((wchar_t*)inputString.utf16(), + buffer.data(), + buffer.size()); + if (result > DWORD(buffer.size())) { + buffer.resize(result); + result = ::GetLongPathName((wchar_t*)inputString.utf16(), + buffer.data(), + buffer.size()); + } + if (result > 4) { + QString longPath = QString::fromWCharArray(buffer.data() + 4); // ignoring prefix + longPath[0] = longPath.at(0).toUpper(); // capital drive letters + return QDir::fromNativeSeparators(longPath); + } else { + return QDir::fromNativeSeparators(strShortPath); + } +} +#endif + +/*! + \internal + + Given a path return the matching QFileSystemNode or &root if invalid +*/ +QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QString &path, bool fetch) const +{ + Q_Q(const QFileSystemModel); + Q_UNUSED(q); + if (path.isEmpty() || path == myComputer() || path.startsWith(QLatin1Char(':'))) + return const_cast(&root); + + // Construct the nodes up to the new root path if they need to be built + QString absolutePath; +#ifdef Q_OS_WIN32 + QString longPath = qt_GetLongPathName(path); +#else + QString longPath = path; +#endif + if (longPath == rootDir.path()) + absolutePath = rootDir.absolutePath(); + else + absolutePath = QDir(longPath).absolutePath(); + + // ### TODO can we use bool QAbstractFileEngine::caseSensitive() const? + QStringList pathElements = absolutePath.split(QLatin1Char('/'), Qt::SkipEmptyParts); + if ((pathElements.isEmpty()) +#if !defined(Q_OS_WIN) + && QDir::fromNativeSeparators(longPath) != QLatin1String("/") +#endif + ) + return const_cast(&root); + QModelIndex index = QModelIndex(); // start with "My Computer" + QString elementPath; + QChar separator = QLatin1Char('/'); + QString trailingSeparator; +#if defined(Q_OS_WIN) + if (absolutePath.startsWith(QLatin1String("//"))) { // UNC path + QString host = QLatin1String("\\\\") + pathElements.constFirst(); + if (absolutePath == QDir::fromNativeSeparators(host)) + absolutePath.append(QLatin1Char('/')); + if (longPath.endsWith(QLatin1Char('/')) && !absolutePath.endsWith(QLatin1Char('/'))) + absolutePath.append(QLatin1Char('/')); + if (absolutePath.endsWith(QLatin1Char('/'))) + trailingSeparator = QLatin1String("\\"); + int r = 0; + QFileSystemModelPrivate::QFileSystemNode *rootNode = const_cast(&root); + if (!root.children.contains(host.toLower())) { + if (pathElements.count() == 1 && !absolutePath.endsWith(QLatin1Char('/'))) + return rootNode; + QFileInfo info(host); + if (!info.exists()) + return rootNode; + QFileSystemModelPrivate *p = const_cast(this); + p->addNode(rootNode, host,info); + p->addVisibleFiles(rootNode, QStringList(host)); + } + r = rootNode->visibleLocation(host); + r = translateVisibleLocation(rootNode, r); + index = q->index(r, 0, QModelIndex()); + pathElements.pop_front(); + separator = QLatin1Char('\\'); + elementPath = host; + elementPath.append(separator); + } else { + if (!pathElements.at(0).contains(QLatin1Char(':'))) { + QString rootPath = QDir(longPath).rootPath(); + pathElements.prepend(rootPath); + } + if (pathElements.at(0).endsWith(QLatin1Char('/'))) + pathElements[0].chop(1); + } +#else + // add the "/" item, since it is a valid path element on Unix + if (absolutePath[0] == QLatin1Char('/')) + pathElements.prepend(QLatin1String("/")); +#endif + + QFileSystemModelPrivate::QFileSystemNode *parent = node(index); + + for (int i = 0; i < pathElements.count(); ++i) { + QString element = pathElements.at(i); + if (i != 0) + elementPath.append(separator); + elementPath.append(element); + if (i == pathElements.count() - 1) + elementPath.append(trailingSeparator); +#ifdef Q_OS_WIN + // On Windows, "filename " and "filename" are equivalent and + // "filename . " and "filename" are equivalent + // "filename......." and "filename" are equivalent Task #133928 + // whereas "filename .txt" is still "filename .txt" + // If after stripping the characters there is nothing left then we + // just return the parent directory as it is assumed that the path + // is referring to the parent + while (element.endsWith(QLatin1Char('.')) || element.endsWith(QLatin1Char(' '))) + element.chop(1); + // Only filenames that can't possibly exist will be end up being empty + if (element.isEmpty()) + return parent; +#endif + bool alreadyExisted = parent->children.contains(element); + + // we couldn't find the path element, we create a new node since we + // _know_ that the path is valid + if (alreadyExisted) { + if ((parent->children.count() == 0) + || (parent->caseSensitive() + && parent->children.value(element)->fileName != element) + || (!parent->caseSensitive() + && parent->children.value(element)->fileName.toLower() != element.toLower())) + alreadyExisted = false; + } + + QFileSystemModelPrivate::QFileSystemNode *node; + if (!alreadyExisted) { + // Someone might call ::index("file://cookie/monster/doesn't/like/veggies"), + // a path that doesn't exists, I.E. don't blindly create directories. + QFileInfo info(elementPath); + if (!info.exists()) + return const_cast(&root); + QFileSystemModelPrivate *p = const_cast(this); + node = p->addNode(parent, element,info); +#if QT_CONFIG(filesystemwatcher) + node->populate(fileInfoGatherer.getInfo(info)); +#endif + } else { + node = parent->children.value(element); + } + + Q_ASSERT(node); + if (!node->isVisible) { + // It has been filtered out + if (alreadyExisted && node->hasInformation() && !fetch) + return const_cast(&root); + + QFileSystemModelPrivate *p = const_cast(this); + p->addVisibleFiles(parent, QStringList(element)); + if (!p->bypassFilters.contains(node)) + p->bypassFilters[node] = 1; + QString dir = q->filePath(this->index(parent)); + if (!node->hasInformation() && fetch) { + Fetching f = { std::move(dir), std::move(element), node }; + p->toFetch.append(std::move(f)); + p->fetchingTimer.start(0, const_cast(q)); + } + } + parent = node; + } + + return parent; +} + +/*! + \reimp +*/ +void QFileSystemModel::timerEvent(QTimerEvent *event) +{ + Q_D(QFileSystemModel); + if (event->timerId() == d->fetchingTimer.timerId()) { + d->fetchingTimer.stop(); +#if QT_CONFIG(filesystemwatcher) + for (int i = 0; i < d->toFetch.count(); ++i) { + const QFileSystemModelPrivate::QFileSystemNode *node = d->toFetch.at(i).node; + if (!node->hasInformation()) { + d->fileInfoGatherer.fetchExtendedInformation(d->toFetch.at(i).dir, + QStringList(d->toFetch.at(i).file)); + } else { + // qDebug("yah!, you saved a little gerbil soul"); + } + } +#endif + d->toFetch.clear(); + } +} + +/*! + Returns \c true if the model item \a index represents a directory; + otherwise returns \c false. +*/ +bool QFileSystemModel::isDir(const QModelIndex &index) const +{ + // This function is for public usage only because it could create a file info + Q_D(const QFileSystemModel); + if (!index.isValid()) + return true; + QFileSystemModelPrivate::QFileSystemNode *n = d->node(index); + if (n->hasInformation()) + return n->isDir(); + return fileInfo(index).isDir(); +} + +/*! + Returns the size in bytes of \a index. If the file does not exist, 0 is returned. + */ +qint64 QFileSystemModel::size(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + if (!index.isValid()) + return 0; + return d->node(index)->size(); +} + +/*! + Returns the type of file \a index such as "Directory" or "JPEG file". + */ +QString QFileSystemModel::type(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + if (!index.isValid()) + return QString(); + return d->node(index)->type(); +} + +/*! + Returns the date and time when \a index was last modified. + */ +QDateTime QFileSystemModel::lastModified(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + if (!index.isValid()) + return QDateTime(); + return d->node(index)->lastModified(); +} + +/*! + \reimp +*/ +QModelIndex QFileSystemModel::parent(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + if (!d->indexValid(index)) + return QModelIndex(); + + QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(index); + Q_ASSERT(indexNode != nullptr); + QFileSystemModelPrivate::QFileSystemNode *parentNode = indexNode->parent; + if (parentNode == nullptr || parentNode == &d->root) + return QModelIndex(); + + // get the parent's row + QFileSystemModelPrivate::QFileSystemNode *grandParentNode = parentNode->parent; + Q_ASSERT(grandParentNode->children.contains(parentNode->fileName)); + int visualRow = d->translateVisibleLocation(grandParentNode, grandParentNode->visibleLocation(grandParentNode->children.value(parentNode->fileName)->fileName)); + if (visualRow == -1) + return QModelIndex(); + return createIndex(visualRow, 0, parentNode); +} + +/* + \internal + + return the index for node +*/ +QModelIndex QFileSystemModelPrivate::index(const QFileSystemModelPrivate::QFileSystemNode *node, int column) const +{ + Q_Q(const QFileSystemModel); + QFileSystemModelPrivate::QFileSystemNode *parentNode = (node ? node->parent : nullptr); + if (node == &root || !parentNode) + return QModelIndex(); + + // get the parent's row + Q_ASSERT(node); + if (!node->isVisible) + return QModelIndex(); + + int visualRow = translateVisibleLocation(parentNode, parentNode->visibleLocation(node->fileName)); + return q->createIndex(visualRow, column, const_cast(node)); +} + +/*! + \reimp +*/ +bool QFileSystemModel::hasChildren(const QModelIndex &parent) const +{ + Q_D(const QFileSystemModel); + if (parent.column() > 0) + return false; + + if (!parent.isValid()) // drives + return true; + + const QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(parent); + Q_ASSERT(indexNode); + return (indexNode->isDir()); +} + +/*! + \reimp + */ +bool QFileSystemModel::canFetchMore(const QModelIndex &parent) const +{ + Q_D(const QFileSystemModel); + const QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(parent); + return (!indexNode->populatedChildren); +} + +/*! + \reimp + */ +void QFileSystemModel::fetchMore(const QModelIndex &parent) +{ + Q_D(QFileSystemModel); + if (!d->setRootPath) + return; + QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(parent); + if (indexNode->populatedChildren) + return; + indexNode->populatedChildren = true; +#if QT_CONFIG(filesystemwatcher) + d->fileInfoGatherer.list(filePath(parent)); +#endif +} + +/*! + \reimp +*/ +int QFileSystemModel::rowCount(const QModelIndex &parent) const +{ + Q_D(const QFileSystemModel); + if (parent.column() > 0) + return 0; + + if (!parent.isValid()) + return d->root.visibleChildren.count(); + + const QFileSystemModelPrivate::QFileSystemNode *parentNode = d->node(parent); + return parentNode->visibleChildren.count(); +} + +/*! + \reimp +*/ +int QFileSystemModel::columnCount(const QModelIndex &parent) const +{ + return (parent.column() > 0) ? 0 : QFileSystemModelPrivate::NumColumns; +} + +/*! + Returns the data stored under the given \a role for the item "My Computer". + + \sa Qt::ItemDataRole + */ +QVariant QFileSystemModel::myComputer(int role) const +{ +#if QT_CONFIG(filesystemwatcher) + Q_D(const QFileSystemModel); +#endif + switch (role) { + case Qt::DisplayRole: + return QFileSystemModelPrivate::myComputer(); +#if QT_CONFIG(filesystemwatcher) + case Qt::DecorationRole: + return d->fileInfoGatherer.iconProvider()->icon(QAbstractFileIconProvider::Computer); +#endif + } + return QVariant(); +} + +/*! + \reimp +*/ +QVariant QFileSystemModel::data(const QModelIndex &index, int role) const +{ + Q_D(const QFileSystemModel); + if (!index.isValid() || index.model() != this) + return QVariant(); + + switch (role) { + case Qt::EditRole: + case Qt::DisplayRole: + switch (index.column()) { + case 0: return d->displayName(index); + case 1: return d->size(index); + case 2: return d->type(index); + case 3: return d->time(index); + default: + qWarning("data: invalid display value column %d", index.column()); + break; + } + break; + case FilePathRole: + return filePath(index); + case FileNameRole: + return d->name(index); + case Qt::DecorationRole: + if (index.column() == 0) { + QIcon icon = d->icon(index); +#if QT_CONFIG(filesystemwatcher) + if (icon.isNull()) { + if (d->node(index)->isDir()) + icon = d->fileInfoGatherer.iconProvider()->icon(QAbstractFileIconProvider::Folder); + else + icon = d->fileInfoGatherer.iconProvider()->icon(QAbstractFileIconProvider::File); + } +#endif // filesystemwatcher + return icon; + } + break; + case Qt::TextAlignmentRole: + if (index.column() == 1) + return QVariant(Qt::AlignTrailing | Qt::AlignVCenter); + break; + case FilePermissions: + int p = permissions(index); + return p; + } + + return QVariant(); +} + +/*! + \internal +*/ +QString QFileSystemModelPrivate::size(const QModelIndex &index) const +{ + if (!index.isValid()) + return QString(); + const QFileSystemNode *n = node(index); + if (n->isDir()) { +#ifdef Q_OS_MAC + return QLatin1String("--"); +#else + return QLatin1String(""); +#endif + // Windows - "" + // OS X - "--" + // Konqueror - "4 KB" + // Nautilus - "9 items" (the number of children) + } + return size(n->size()); +} + +QString QFileSystemModelPrivate::size(qint64 bytes) +{ + return QLocale::system().formattedDataSize(bytes); +} + +/*! + \internal +*/ +QString QFileSystemModelPrivate::time(const QModelIndex &index) const +{ + if (!index.isValid()) + return QString(); +#if QT_CONFIG(datestring) + return QLocale::system().toString(node(index)->lastModified(), QLocale::ShortFormat); +#else + Q_UNUSED(index); + return QString(); +#endif +} + +/* + \internal +*/ +QString QFileSystemModelPrivate::type(const QModelIndex &index) const +{ + if (!index.isValid()) + return QString(); + return node(index)->type(); +} + +/*! + \internal +*/ +QString QFileSystemModelPrivate::name(const QModelIndex &index) const +{ + if (!index.isValid()) + return QString(); + QFileSystemNode *dirNode = node(index); + if ( +#if QT_CONFIG(filesystemwatcher) + fileInfoGatherer.resolveSymlinks() && +#endif + !resolvedSymLinks.isEmpty() && dirNode->isSymLink(/* ignoreNtfsSymLinks = */ true)) { + QString fullPath = QDir::fromNativeSeparators(filePath(index)); + return resolvedSymLinks.value(fullPath, dirNode->fileName); + } + return dirNode->fileName; +} + +/*! + \internal +*/ +QString QFileSystemModelPrivate::displayName(const QModelIndex &index) const +{ +#if defined(Q_OS_WIN) + QFileSystemNode *dirNode = node(index); + if (!dirNode->volumeName.isEmpty()) + return dirNode->volumeName; +#endif + return name(index); +} + +/*! + \internal +*/ +QIcon QFileSystemModelPrivate::icon(const QModelIndex &index) const +{ + if (!index.isValid()) + return QIcon(); + return node(index)->icon(); +} + +/*! + \reimp +*/ +bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, int role) +{ + Q_D(QFileSystemModel); + if (!idx.isValid() + || idx.column() != 0 + || role != Qt::EditRole + || (flags(idx) & Qt::ItemIsEditable) == 0) { + return false; + } + + QString newName = value.toString(); + QString oldName = idx.data().toString(); + if (newName == oldName) + return true; + + const QString parentPath = filePath(parent(idx)); + + if (newName.isEmpty() || QDir::toNativeSeparators(newName).contains(QDir::separator())) + return false; + +#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) + // QTBUG-65683: Remove file system watchers prior to renaming to prevent + // failure due to locked files on Windows. + const QStringList watchedPaths = d->unwatchPathsAt(idx); +#endif // filesystemwatcher && Q_OS_WIN + if (!QDir(parentPath).rename(oldName, newName)) { +#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) + d->watchPaths(watchedPaths); +#endif + return false; + } else { + /* + *After re-naming something we don't want the selection to change* + - can't remove rows and later insert + - can't quickly remove and insert + - index pointer can't change because treeview doesn't use persistant index's + + - if this get any more complicated think of changing it to just + use layoutChanged + */ + + QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(idx); + QFileSystemModelPrivate::QFileSystemNode *parentNode = indexNode->parent; + int visibleLocation = parentNode->visibleLocation(parentNode->children.value(indexNode->fileName)->fileName); + + parentNode->visibleChildren.removeAt(visibleLocation); + QScopedPointer nodeToRename(parentNode->children.take(oldName)); + nodeToRename->fileName = newName; + nodeToRename->parent = parentNode; +#if QT_CONFIG(filesystemwatcher) + nodeToRename->populate(d->fileInfoGatherer.getInfo(QFileInfo(parentPath, newName))); +#endif + nodeToRename->isVisible = true; + parentNode->children[newName] = nodeToRename.take(); + parentNode->visibleChildren.insert(visibleLocation, newName); + + d->delayedSort(); + emit fileRenamed(parentPath, oldName, newName); + } + return true; +} + +/*! + \reimp +*/ +QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + switch (role) { + case Qt::DecorationRole: + if (section == 0) { + // ### TODO oh man this is ugly and doesn't even work all the way! + // it is still 2 pixels off + QImage pixmap(16, 1, QImage::Format_ARGB32_Premultiplied); + pixmap.fill(Qt::transparent); + return pixmap; + } + break; + case Qt::TextAlignmentRole: + return Qt::AlignLeft; + } + + if (orientation != Qt::Horizontal || role != Qt::DisplayRole) + return QAbstractItemModel::headerData(section, orientation, role); + + QString returnValue; + switch (section) { + case 0: returnValue = tr("Name"); + break; + case 1: returnValue = tr("Size"); + break; + case 2: returnValue = +#ifdef Q_OS_MAC + tr("Kind", "Match OS X Finder"); +#else + tr("Type", "All other platforms"); +#endif + break; + // Windows - Type + // OS X - Kind + // Konqueror - File Type + // Nautilus - Type + case 3: returnValue = tr("Date Modified"); + break; + default: return QVariant(); + } + return returnValue; +} + +/*! + \reimp +*/ +Qt::ItemFlags QFileSystemModel::flags(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + Qt::ItemFlags flags = QAbstractItemModel::flags(index); + if (!index.isValid()) + return flags; + + QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(index); + if (d->nameFilterDisables && !d->passNameFilters(indexNode)) { + flags &= ~Qt::ItemIsEnabled; + // ### TODO you shouldn't be able to set this as the current item, task 119433 + return flags; + } + + flags |= Qt::ItemIsDragEnabled; + if (d->readOnly) + return flags; + if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) { + flags |= Qt::ItemIsEditable; + if (indexNode->isDir()) + flags |= Qt::ItemIsDropEnabled; + else + flags |= Qt::ItemNeverHasChildren; + } + return flags; +} + +/*! + \internal +*/ +void QFileSystemModelPrivate::_q_performDelayedSort() +{ + Q_Q(QFileSystemModel); + q->sort(sortColumn, sortOrder); +} + + +/* + \internal + Helper functor used by sort() +*/ +class QFileSystemModelSorter +{ +public: + inline QFileSystemModelSorter(int column) : sortColumn(column) + { + naturalCompare.setNumericMode(true); + naturalCompare.setCaseSensitivity(Qt::CaseInsensitive); + } + + bool compareNodes(const QFileSystemModelPrivate::QFileSystemNode *l, + const QFileSystemModelPrivate::QFileSystemNode *r) const + { + switch (sortColumn) { + case 0: { +#ifndef Q_OS_MAC + // place directories before files + bool left = l->isDir(); + bool right = r->isDir(); + if (left ^ right) + return left; +#endif + return naturalCompare.compare(l->fileName, r->fileName) < 0; + } + case 1: + { + // Directories go first + bool left = l->isDir(); + bool right = r->isDir(); + if (left ^ right) + return left; + + qint64 sizeDifference = l->size() - r->size(); + if (sizeDifference == 0) + return naturalCompare.compare(l->fileName, r->fileName) < 0; + + return sizeDifference < 0; + } + case 2: + { + int compare = naturalCompare.compare(l->type(), r->type()); + if (compare == 0) + return naturalCompare.compare(l->fileName, r->fileName) < 0; + + return compare < 0; + } + case 3: + { + if (l->lastModified() == r->lastModified()) + return naturalCompare.compare(l->fileName, r->fileName) < 0; + + return l->lastModified() < r->lastModified(); + } + } + Q_ASSERT(false); + return false; + } + + bool operator()(const QFileSystemModelPrivate::QFileSystemNode *l, + const QFileSystemModelPrivate::QFileSystemNode *r) const + { + return compareNodes(l, r); + } + + +private: + QCollator naturalCompare; + int sortColumn; +}; + +/* + \internal + + Sort all of the children of parent +*/ +void QFileSystemModelPrivate::sortChildren(int column, const QModelIndex &parent) +{ + Q_Q(QFileSystemModel); + QFileSystemModelPrivate::QFileSystemNode *indexNode = node(parent); + if (indexNode->children.count() == 0) + return; + + QList values; + + for (auto iterator = indexNode->children.constBegin(), cend = indexNode->children.constEnd(); iterator != cend; ++iterator) { + if (filtersAcceptsNode(iterator.value())) { + values.append(iterator.value()); + } else { + iterator.value()->isVisible = false; + } + } + QFileSystemModelSorter ms(column); + std::sort(values.begin(), values.end(), ms); + // First update the new visible list + indexNode->visibleChildren.clear(); + //No more dirty item we reset our internal dirty index + indexNode->dirtyChildrenIndex = -1; + const int numValues = values.count(); + indexNode->visibleChildren.reserve(numValues); + for (int i = 0; i < numValues; ++i) { + indexNode->visibleChildren.append(values.at(i)->fileName); + values.at(i)->isVisible = true; + } + + if (!disableRecursiveSort) { + for (int i = 0; i < q->rowCount(parent); ++i) { + const QModelIndex childIndex = q->index(i, 0, parent); + QFileSystemModelPrivate::QFileSystemNode *indexNode = node(childIndex); + //Only do a recursive sort on visible nodes + if (indexNode->isVisible) + sortChildren(column, childIndex); + } + } +} + +/*! + \reimp +*/ +void QFileSystemModel::sort(int column, Qt::SortOrder order) +{ + Q_D(QFileSystemModel); + if (d->sortOrder == order && d->sortColumn == column && !d->forceSort) + return; + + emit layoutAboutToBeChanged(); + QModelIndexList oldList = persistentIndexList(); + QList> oldNodes; + const int nodeCount = oldList.count(); + oldNodes.reserve(nodeCount); + for (int i = 0; i < nodeCount; ++i) { + const QModelIndex &oldNode = oldList.at(i); + QPair pair(d->node(oldNode), oldNode.column()); + oldNodes.append(pair); + } + + if (!(d->sortColumn == column && d->sortOrder != order && !d->forceSort)) { + //we sort only from where we are, don't need to sort all the model + d->sortChildren(column, index(rootPath())); + d->sortColumn = column; + d->forceSort = false; + } + d->sortOrder = order; + + QModelIndexList newList; + const int numOldNodes = oldNodes.size(); + newList.reserve(numOldNodes); + for (int i = 0; i < numOldNodes; ++i) { + const QPair &oldNode = oldNodes.at(i); + newList.append(d->index(oldNode.first, oldNode.second)); + } + changePersistentIndexList(oldList, newList); + emit layoutChanged(); +} + +/*! + Returns a list of MIME types that can be used to describe a list of items + in the model. +*/ +QStringList QFileSystemModel::mimeTypes() const +{ + return QStringList(QLatin1String("text/uri-list")); +} + +/*! + Returns an object that contains a serialized description of the specified + \a indexes. The format used to describe the items corresponding to the + indexes is obtained from the mimeTypes() function. + + If the list of indexes is empty, \nullptr is returned rather than a + serialized empty list. +*/ +QMimeData *QFileSystemModel::mimeData(const QModelIndexList &indexes) const +{ + QList urls; + QList::const_iterator it = indexes.begin(); + for (; it != indexes.end(); ++it) + if ((*it).column() == 0) + urls << QUrl::fromLocalFile(filePath(*it)); + QMimeData *data = new QMimeData(); + data->setUrls(urls); + return data; +} + +/*! + Handles the \a data supplied by a drag and drop operation that ended with + the given \a action over the row in the model specified by the \a row and + \a column and by the \a parent index. Returns true if the operation was + successful. + + \sa supportedDropActions() +*/ +bool QFileSystemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) +{ + Q_UNUSED(row); + Q_UNUSED(column); + if (!parent.isValid() || isReadOnly()) + return false; + + bool success = true; + QString to = filePath(parent) + QDir::separator(); + + QList urls = data->urls(); + QList::const_iterator it = urls.constBegin(); + + switch (action) { + case Qt::CopyAction: + for (; it != urls.constEnd(); ++it) { + QString path = (*it).toLocalFile(); + success = QFile::copy(path, to + QFileInfo(path).fileName()) && success; + } + break; + case Qt::LinkAction: + for (; it != urls.constEnd(); ++it) { + QString path = (*it).toLocalFile(); + success = QFile::link(path, to + QFileInfo(path).fileName()) && success; + } + break; + case Qt::MoveAction: + for (; it != urls.constEnd(); ++it) { + QString path = (*it).toLocalFile(); + success = QFile::rename(path, to + QFileInfo(path).fileName()) && success; + } + break; + default: + return false; + } + + return success; +} + +/*! + \reimp +*/ +Qt::DropActions QFileSystemModel::supportedDropActions() const +{ + return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction; +} + +/*! + \reimp +*/ +QHash QFileSystemModel::roleNames() const +{ + auto ret = QAbstractItemModel::roleNames(); + ret.insert(QFileSystemModel::FileIconRole, + QByteArrayLiteral("fileIcon")); // == Qt::decoration + ret.insert(QFileSystemModel::FilePathRole, QByteArrayLiteral("filePath")); + ret.insert(QFileSystemModel::FileNameRole, QByteArrayLiteral("fileName")); + ret.insert(QFileSystemModel::FilePermissions, QByteArrayLiteral("filePermissions")); + return ret; +} + +/*! + \enum QFileSystemModel::Option + \since 5.14 + + \value DontWatchForChanges Do not add file watchers to the paths. + This reduces overhead when using the model for simple tasks + like line edit completion. + + \value DontResolveSymlinks Don't resolve symlinks in the file + system model. By default, symlinks are resolved. + + \value DontUseCustomDirectoryIcons Always use the default directory icon. + Some platforms allow the user to set a different icon. Custom icon lookup + causes a big performance impact over network or removable drives. + This sets the QFileIconProvider::DontUseCustomDirectoryIcons + option in the icon provider accordingly. + + \sa resolveSymlinks +*/ + +/*! + \since 5.14 + Sets the given \a option to be enabled if \a on is true; otherwise, + clears the given \a option. + + Options should be set before changing properties. + + \sa options, testOption() +*/ +void QFileSystemModel::setOption(Option option, bool on) +{ + QFileSystemModel::Options previousOptions = options(); + setOptions(previousOptions.setFlag(option, on)); +} + +/*! + \since 5.14 + + Returns \c true if the given \a option is enabled; otherwise, returns + false. + + \sa options, setOption() +*/ +bool QFileSystemModel::testOption(Option option) const +{ + return options().testFlag(option); +} + +/*! + \property QFileSystemModel::options + \brief the various options that affect the model + \since 5.14 + + By default, all options are disabled. + + Options should be set before changing properties. + + \sa setOption(), testOption() +*/ +void QFileSystemModel::setOptions(Options options) +{ + const Options changed = (options ^ QFileSystemModel::options()); + + if (changed.testFlag(DontResolveSymlinks)) + setResolveSymlinks(!options.testFlag(DontResolveSymlinks)); + +#if QT_CONFIG(filesystemwatcher) + Q_D(QFileSystemModel); + if (changed.testFlag(DontWatchForChanges)) + d->fileInfoGatherer.setWatching(!options.testFlag(DontWatchForChanges)); +#endif + + if (changed.testFlag(DontUseCustomDirectoryIcons)) { + if (auto provider = iconProvider()) { + QAbstractFileIconProvider::Options providerOptions = provider->options(); + providerOptions.setFlag(QAbstractFileIconProvider::DontUseCustomDirectoryIcons, + options.testFlag(QFileSystemModel::DontUseCustomDirectoryIcons)); + provider->setOptions(providerOptions); + } else { + qWarning("Setting QFileSystemModel::DontUseCustomDirectoryIcons has no effect when no provider is used"); + } + } +} + +QFileSystemModel::Options QFileSystemModel::options() const +{ + QFileSystemModel::Options result; + result.setFlag(DontResolveSymlinks, !resolveSymlinks()); +#if QT_CONFIG(filesystemwatcher) + Q_D(const QFileSystemModel); + result.setFlag(DontWatchForChanges, !d->fileInfoGatherer.isWatching()); +#else + result.setFlag(DontWatchForChanges); +#endif + if (auto provider = iconProvider()) { + result.setFlag(DontUseCustomDirectoryIcons, + provider->options().testFlag(QAbstractFileIconProvider::DontUseCustomDirectoryIcons)); + } + return result; +} + +/*! + Returns the path of the item stored in the model under the + \a index given. +*/ +QString QFileSystemModel::filePath(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + QString fullPath = d->filePath(index); + QFileSystemModelPrivate::QFileSystemNode *dirNode = d->node(index); + if (dirNode->isSymLink() +#if QT_CONFIG(filesystemwatcher) + && d->fileInfoGatherer.resolveSymlinks() +#endif + && d->resolvedSymLinks.contains(fullPath) + && dirNode->isDir()) { + QFileInfo fullPathInfo(dirNode->fileInfo()); + if (!dirNode->hasInformation()) + fullPathInfo = QFileInfo(fullPath); + QString canonicalPath = fullPathInfo.canonicalFilePath(); + auto *canonicalNode = d->node(fullPathInfo.canonicalFilePath(), false); + QFileInfo resolvedInfo = canonicalNode->fileInfo(); + if (!canonicalNode->hasInformation()) + resolvedInfo = QFileInfo(canonicalPath); + if (resolvedInfo.exists()) + return resolvedInfo.filePath(); + } + return fullPath; +} + +QString QFileSystemModelPrivate::filePath(const QModelIndex &index) const +{ + Q_Q(const QFileSystemModel); + Q_UNUSED(q); + if (!index.isValid()) + return QString(); + Q_ASSERT(index.model() == q); + + QStringList path; + QModelIndex idx = index; + while (idx.isValid()) { + QFileSystemModelPrivate::QFileSystemNode *dirNode = node(idx); + if (dirNode) + path.prepend(dirNode->fileName); + idx = idx.parent(); + } + QString fullPath = QDir::fromNativeSeparators(path.join(QDir::separator())); +#if !defined(Q_OS_WIN) + if ((fullPath.length() > 2) && fullPath[0] == QLatin1Char('/') && fullPath[1] == QLatin1Char('/')) + fullPath = fullPath.mid(1); +#else + if (fullPath.length() == 2 && fullPath.endsWith(QLatin1Char(':'))) + fullPath.append(QLatin1Char('/')); +#endif + return fullPath; +} + +/*! + Create a directory with the \a name in the \a parent model index. +*/ +QModelIndex QFileSystemModel::mkdir(const QModelIndex &parent, const QString &name) +{ + Q_D(QFileSystemModel); + if (!parent.isValid()) + return parent; + + QDir dir(filePath(parent)); + if (!dir.mkdir(name)) + return QModelIndex(); + QFileSystemModelPrivate::QFileSystemNode *parentNode = d->node(parent); + d->addNode(parentNode, name, QFileInfo()); + Q_ASSERT(parentNode->children.contains(name)); + QFileSystemModelPrivate::QFileSystemNode *node = parentNode->children[name]; +#if QT_CONFIG(filesystemwatcher) + node->populate(d->fileInfoGatherer.getInfo(QFileInfo(dir.absolutePath() + QDir::separator() + name))); +#endif + d->addVisibleFiles(parentNode, QStringList(name)); + return d->index(node); +} + +/*! + Returns the complete OR-ed together combination of QFile::Permission for the \a index. + */ +QFile::Permissions QFileSystemModel::permissions(const QModelIndex &index) const +{ + Q_D(const QFileSystemModel); + return d->node(index)->permissions(); +} + +/*! + Sets the directory that is being watched by the model to \a newPath by + installing a \l{QFileSystemWatcher}{file system watcher} on it. Any + changes to files and directories within this directory will be + reflected in the model. + + If the path is changed, the rootPathChanged() signal will be emitted. + + \note This function does not change the structure of the model or + modify the data available to views. In other words, the "root" of + the model is \e not changed to include only files and directories + within the directory specified by \a newPath in the file system. + */ +QModelIndex QFileSystemModel::setRootPath(const QString &newPath) +{ + Q_D(QFileSystemModel); +#ifdef Q_OS_WIN +#ifdef Q_OS_WIN32 + QString longNewPath = qt_GetLongPathName(newPath); +#else + QString longNewPath = QDir::fromNativeSeparators(newPath); +#endif +#else + QString longNewPath = newPath; +#endif + //we remove .. and . from the given path if exist + if (!newPath.isEmpty()) + longNewPath = QDir::cleanPath(longNewPath); + + d->setRootPath = true; + + //user don't ask for the root path ("") but the conversion failed + if (!newPath.isEmpty() && longNewPath.isEmpty()) + return d->index(rootPath()); + + if (d->rootDir.path() == longNewPath) + return d->index(rootPath()); + + auto node = d->node(longNewPath); + QFileInfo newPathInfo; + if (node && node->hasInformation()) + newPathInfo = node->fileInfo(); + else + newPathInfo = QFileInfo(longNewPath); + + bool showDrives = (longNewPath.isEmpty() || longNewPath == QFileSystemModelPrivate::myComputer()); + if (!showDrives && !newPathInfo.exists()) + return d->index(rootPath()); + + //We remove the watcher on the previous path + if (!rootPath().isEmpty() && rootPath() != QLatin1String(".")) { + //This remove the watcher for the old rootPath +#if QT_CONFIG(filesystemwatcher) + d->fileInfoGatherer.removePath(rootPath()); +#endif + //This line "marks" the node as dirty, so the next fetchMore + //call on the path will ask the gatherer to install a watcher again + //But it doesn't re-fetch everything + d->node(rootPath())->populatedChildren = false; + } + + // We have a new valid root path + d->rootDir = QDir(longNewPath); + QModelIndex newRootIndex; + if (showDrives) { + // otherwise dir will become '.' + d->rootDir.setPath(QLatin1String("")); + } else { + newRootIndex = d->index(d->rootDir.path()); + } + fetchMore(newRootIndex); + emit rootPathChanged(longNewPath); + d->forceSort = true; + d->delayedSort(); + return newRootIndex; +} + +/*! + The currently set root path + + \sa rootDirectory() +*/ +QString QFileSystemModel::rootPath() const +{ + Q_D(const QFileSystemModel); + return d->rootDir.path(); +} + +/*! + The currently set directory + + \sa rootPath() +*/ +QDir QFileSystemModel::rootDirectory() const +{ + Q_D(const QFileSystemModel); + QDir dir(d->rootDir); + dir.setNameFilters(nameFilters()); + dir.setFilter(filter()); + return dir; +} + +/*! + Sets the \a provider of file icons for the directory model. +*/ +void QFileSystemModel::setIconProvider(QAbstractFileIconProvider *provider) +{ + Q_D(QFileSystemModel); +#if QT_CONFIG(filesystemwatcher) + d->fileInfoGatherer.setIconProvider(provider); +#endif + d->root.updateIcon(provider, QString()); +} + +/*! + Returns the file icon provider for this directory model. +*/ +QAbstractFileIconProvider *QFileSystemModel::iconProvider() const +{ +#if QT_CONFIG(filesystemwatcher) + Q_D(const QFileSystemModel); + return d->fileInfoGatherer.iconProvider(); +#else + return 0; +#endif +} + +/*! + Sets the directory model's filter to that specified by \a filters. + + Note that the filter you set should always include the QDir::AllDirs enum value, + otherwise QFileSystemModel won't be able to read the directory structure. + + \sa QDir::Filters +*/ +void QFileSystemModel::setFilter(QDir::Filters filters) +{ + Q_D(QFileSystemModel); + if (d->filters == filters) + return; + d->filters = filters; + // CaseSensitivity might have changed + setNameFilters(nameFilters()); + d->forceSort = true; + d->delayedSort(); +} + +/*! + Returns the filter specified for the directory model. + + If a filter has not been set, the default filter is QDir::AllEntries | + QDir::NoDotAndDotDot | QDir::AllDirs. + + \sa QDir::Filters +*/ +QDir::Filters QFileSystemModel::filter() const +{ + Q_D(const QFileSystemModel); + return d->filters; +} + +/*! + \property QFileSystemModel::resolveSymlinks + \brief Whether the directory model should resolve symbolic links + + This is only relevant on Windows. + + By default, this property is \c true. + + \sa QFileSystemModel::Options +*/ +void QFileSystemModel::setResolveSymlinks(bool enable) +{ +#if QT_CONFIG(filesystemwatcher) + Q_D(QFileSystemModel); + d->fileInfoGatherer.setResolveSymlinks(enable); +#else + Q_UNUSED(enable); +#endif +} + +bool QFileSystemModel::resolveSymlinks() const +{ +#if QT_CONFIG(filesystemwatcher) + Q_D(const QFileSystemModel); + return d->fileInfoGatherer.resolveSymlinks(); +#else + return false; +#endif +} + +/*! + \property QFileSystemModel::readOnly + \brief Whether the directory model allows writing to the file system + + If this property is set to false, the directory model will allow renaming, copying + and deleting of files and directories. + + This property is \c true by default +*/ +void QFileSystemModel::setReadOnly(bool enable) +{ + Q_D(QFileSystemModel); + d->readOnly = enable; +} + +bool QFileSystemModel::isReadOnly() const +{ + Q_D(const QFileSystemModel); + return d->readOnly; +} + +/*! + \property QFileSystemModel::nameFilterDisables + \brief Whether files that don't pass the name filter are hidden or disabled + + This property is \c true by default +*/ +void QFileSystemModel::setNameFilterDisables(bool enable) +{ + Q_D(QFileSystemModel); + if (d->nameFilterDisables == enable) + return; + d->nameFilterDisables = enable; + d->forceSort = true; + d->delayedSort(); +} + +bool QFileSystemModel::nameFilterDisables() const +{ + Q_D(const QFileSystemModel); + return d->nameFilterDisables; +} + +/*! + Sets the name \a filters to apply against the existing files. +*/ +void QFileSystemModel::setNameFilters(const QStringList &filters) +{ + // Prep the regexp's ahead of time +#if QT_CONFIG(regularexpression) + Q_D(QFileSystemModel); + + if (!d->bypassFilters.isEmpty()) { + // update the bypass filter to only bypass the stuff that must be kept around + d->bypassFilters.clear(); + // We guarantee that rootPath will stick around + QPersistentModelIndex root(index(rootPath())); + const QModelIndexList persistentList = persistentIndexList(); + for (const auto &persistentIndex : persistentList) { + QFileSystemModelPrivate::QFileSystemNode *node = d->node(persistentIndex); + while (node) { + if (d->bypassFilters.contains(node)) + break; + if (node->isDir()) + d->bypassFilters[node] = true; + node = node->parent; + } + } + } + + d->nameFilters = filters; + d->forceSort = true; + d->delayedSort(); +#else + Q_UNUSED(filters); +#endif +} + +/*! + Returns a list of filters applied to the names in the model. +*/ +QStringList QFileSystemModel::nameFilters() const +{ +#if QT_CONFIG(regularexpression) + Q_D(const QFileSystemModel); + return d->nameFilters; +#else + return QStringList(); +#endif +} + +/*! + \reimp +*/ +bool QFileSystemModel::event(QEvent *event) +{ +#if QT_CONFIG(filesystemwatcher) + Q_D(QFileSystemModel); + if (event->type() == QEvent::LanguageChange) { + d->root.retranslateStrings(d->fileInfoGatherer.iconProvider(), QString()); + return true; + } +#endif + return QAbstractItemModel::event(event); +} + +bool QFileSystemModel::rmdir(const QModelIndex &aindex) +{ + QString path = filePath(aindex); + const bool success = QDir().rmdir(path); +#if QT_CONFIG(filesystemwatcher) + if (success) { + QFileSystemModelPrivate * d = const_cast(d_func()); + d->fileInfoGatherer.removePath(path); + } +#endif + return success; +} + +/*! + \internal + + Performed quick listing and see if any files have been added or removed, + then fetch more information on visible files. + */ +void QFileSystemModelPrivate::_q_directoryChanged(const QString &directory, const QStringList &files) +{ + QFileSystemModelPrivate::QFileSystemNode *parentNode = node(directory, false); + if (parentNode->children.count() == 0) + return; + QStringList toRemove; + QStringList newFiles = files; + std::sort(newFiles.begin(), newFiles.end()); + for (auto i = parentNode->children.constBegin(), cend = parentNode->children.constEnd(); i != cend; ++i) { + QStringList::iterator iterator = std::lower_bound(newFiles.begin(), newFiles.end(), i.value()->fileName); + if ((iterator == newFiles.end()) || (i.value()->fileName < *iterator)) + toRemove.append(i.value()->fileName); + } + for (int i = 0 ; i < toRemove.count() ; ++i ) + removeNode(parentNode, toRemove[i]); +} + +#if defined(Q_OS_WIN) +static QString volumeName(const QString &path) +{ + IShellItem *item = nullptr; + const QString native = QDir::toNativeSeparators(path); + HRESULT hr = SHCreateItemFromParsingName(reinterpret_cast(native.utf16()), + nullptr, IID_IShellItem, + reinterpret_cast(&item)); + if (FAILED(hr)) + return QString(); + LPWSTR name = nullptr; + hr = item->GetDisplayName(SIGDN_NORMALDISPLAY, &name); + if (FAILED(hr)) + return QString(); + QString result = QString::fromWCharArray(name); + CoTaskMemFree(name); + item->Release(); + return result; +} +#endif // Q_OS_WIN + +/*! + \internal + + Adds a new file to the children of parentNode + + *WARNING* this will change the count of children +*/ +QFileSystemModelPrivate::QFileSystemNode* QFileSystemModelPrivate::addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo& info) +{ + // In the common case, itemLocation == count() so check there first + QFileSystemModelPrivate::QFileSystemNode *node = new QFileSystemModelPrivate::QFileSystemNode(fileName, parentNode); +#if QT_CONFIG(filesystemwatcher) + node->populate(info); +#else + Q_UNUSED(info); +#endif +#if defined(Q_OS_WIN) + //The parentNode is "" so we are listing the drives + if (parentNode->fileName.isEmpty()) + node->volumeName = volumeName(fileName); +#endif + Q_ASSERT(!parentNode->children.contains(fileName)); + parentNode->children.insert(fileName, node); + return node; +} + +/*! + \internal + + File at parentNode->children(itemLocation) has been removed, remove from the lists + and emit signals if necessary + + *WARNING* this will change the count of children and could change visibleChildren + */ +void QFileSystemModelPrivate::removeNode(QFileSystemModelPrivate::QFileSystemNode *parentNode, const QString& name) +{ + Q_Q(QFileSystemModel); + QModelIndex parent = index(parentNode); + bool indexHidden = isHiddenByFilter(parentNode, parent); + + int vLocation = parentNode->visibleLocation(name); + if (vLocation >= 0 && !indexHidden) + q->beginRemoveRows(parent, translateVisibleLocation(parentNode, vLocation), + translateVisibleLocation(parentNode, vLocation)); + QFileSystemNode * node = parentNode->children.take(name); + delete node; + // cleanup sort files after removing rather then re-sorting which is O(n) + if (vLocation >= 0) + parentNode->visibleChildren.removeAt(vLocation); + if (vLocation >= 0 && !indexHidden) + q->endRemoveRows(); +} + +/*! + \internal + + File at parentNode->children(itemLocation) was not visible before, but now should be + and emit signals if necessary. + + *WARNING* this will change the visible count + */ +void QFileSystemModelPrivate::addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles) +{ + Q_Q(QFileSystemModel); + QModelIndex parent = index(parentNode); + bool indexHidden = isHiddenByFilter(parentNode, parent); + if (!indexHidden) { + q->beginInsertRows(parent, parentNode->visibleChildren.count() , parentNode->visibleChildren.count() + newFiles.count() - 1); + } + + if (parentNode->dirtyChildrenIndex == -1) + parentNode->dirtyChildrenIndex = parentNode->visibleChildren.count(); + + for (const auto &newFile : newFiles) { + parentNode->visibleChildren.append(newFile); + parentNode->children.value(newFile)->isVisible = true; + } + if (!indexHidden) + q->endInsertRows(); +} + +/*! + \internal + + File was visible before, but now should NOT be + + *WARNING* this will change the visible count + */ +void QFileSystemModelPrivate::removeVisibleFile(QFileSystemNode *parentNode, int vLocation) +{ + Q_Q(QFileSystemModel); + if (vLocation == -1) + return; + QModelIndex parent = index(parentNode); + bool indexHidden = isHiddenByFilter(parentNode, parent); + if (!indexHidden) + q->beginRemoveRows(parent, translateVisibleLocation(parentNode, vLocation), + translateVisibleLocation(parentNode, vLocation)); + parentNode->children.value(parentNode->visibleChildren.at(vLocation))->isVisible = false; + parentNode->visibleChildren.removeAt(vLocation); + if (!indexHidden) + q->endRemoveRows(); +} + +/*! + \internal + + The thread has received new information about files, + update and emit dataChanged if it has actually changed. + */ +void QFileSystemModelPrivate::_q_fileSystemChanged(const QString &path, + const QList> &updates) +{ +#if QT_CONFIG(filesystemwatcher) + Q_Q(QFileSystemModel); + QList rowsToUpdate; + QStringList newFiles; + QFileSystemModelPrivate::QFileSystemNode *parentNode = node(path, false); + QModelIndex parentIndex = index(parentNode); + for (const auto &update : updates) { + QString fileName = update.first; + Q_ASSERT(!fileName.isEmpty()); + QExtendedInformation info = fileInfoGatherer.getInfo(update.second); + bool previouslyHere = parentNode->children.contains(fileName); + if (!previouslyHere) { + addNode(parentNode, fileName, info.fileInfo()); + } + QFileSystemModelPrivate::QFileSystemNode * node = parentNode->children.value(fileName); + bool isCaseSensitive = parentNode->caseSensitive(); + if (isCaseSensitive) { + if (node->fileName != fileName) + continue; + } else { + if (QString::compare(node->fileName,fileName,Qt::CaseInsensitive) != 0) + continue; + } + if (isCaseSensitive) { + Q_ASSERT(node->fileName == fileName); + } else { + node->fileName = fileName; + } + + if (*node != info ) { + node->populate(info); + bypassFilters.remove(node); + // brand new information. + if (filtersAcceptsNode(node)) { + if (!node->isVisible) { + newFiles.append(fileName); + } else { + rowsToUpdate.append(fileName); + } + } else { + if (node->isVisible) { + int visibleLocation = parentNode->visibleLocation(fileName); + removeVisibleFile(parentNode, visibleLocation); + } else { + // The file is not visible, don't do anything + } + } + } + } + + // bundle up all of the changed signals into as few as possible. + std::sort(rowsToUpdate.begin(), rowsToUpdate.end()); + QString min; + QString max; + for (const QString &value : qAsConst(rowsToUpdate)) { + //##TODO is there a way to bundle signals with QString as the content of the list? + /*if (min.isEmpty()) { + min = value; + if (i != rowsToUpdate.count() - 1) + continue; + } + if (i != rowsToUpdate.count() - 1) { + if ((value == min + 1 && max.isEmpty()) || value == max + 1) { + max = value; + continue; + } + }*/ + max = value; + min = value; + int visibleMin = parentNode->visibleLocation(min); + int visibleMax = parentNode->visibleLocation(max); + if (visibleMin >= 0 + && visibleMin < parentNode->visibleChildren.count() + && parentNode->visibleChildren.at(visibleMin) == min + && visibleMax >= 0) { + QModelIndex bottom = q->index(translateVisibleLocation(parentNode, visibleMin), 0, parentIndex); + QModelIndex top = q->index(translateVisibleLocation(parentNode, visibleMax), 3, parentIndex); + emit q->dataChanged(bottom, top); + } + + /*min = QString(); + max = QString();*/ + } + + if (newFiles.count() > 0) { + addVisibleFiles(parentNode, newFiles); + } + + if (newFiles.count() > 0 || (sortColumn != 0 && rowsToUpdate.count() > 0)) { + forceSort = true; + delayedSort(); + } +#else + Q_UNUSED(path); + Q_UNUSED(updates); +#endif // filesystemwatcher +} + +/*! + \internal +*/ +void QFileSystemModelPrivate::_q_resolvedName(const QString &fileName, const QString &resolvedName) +{ + resolvedSymLinks[fileName] = resolvedName; +} + +#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) +// Remove file system watchers at/below the index and return a list of previously +// watched files. This should be called prior to operations like rename/remove +// which might fail due to watchers on platforms like Windows. The watchers +// should be restored on failure. +QStringList QFileSystemModelPrivate::unwatchPathsAt(const QModelIndex &index) +{ + const QFileSystemModelPrivate::QFileSystemNode *indexNode = node(index); + if (indexNode == nullptr) + return QStringList(); + const Qt::CaseSensitivity caseSensitivity = indexNode->caseSensitive() + ? Qt::CaseSensitive : Qt::CaseInsensitive; + const QString path = indexNode->fileInfo().absoluteFilePath(); + + QStringList result; + const auto filter = [path, caseSensitivity] (const QString &watchedPath) + { + const int pathSize = path.size(); + if (pathSize == watchedPath.size()) { + return path.compare(watchedPath, caseSensitivity) == 0; + } else if (watchedPath.size() > pathSize) { + return watchedPath.at(pathSize) == QLatin1Char('/') + && watchedPath.startsWith(path, caseSensitivity); + } + return false; + }; + + const QStringList &watchedFiles = fileInfoGatherer.watchedFiles(); + std::copy_if(watchedFiles.cbegin(), watchedFiles.cend(), + std::back_inserter(result), filter); + + const QStringList &watchedDirectories = fileInfoGatherer.watchedDirectories(); + std::copy_if(watchedDirectories.cbegin(), watchedDirectories.cend(), + std::back_inserter(result), filter); + + fileInfoGatherer.unwatchPaths(result); + return result; +} +#endif // filesystemwatcher && Q_OS_WIN + +/*! + \internal +*/ +void QFileSystemModelPrivate::init() +{ + Q_Q(QFileSystemModel); + + delayedSortTimer.setSingleShot(true); + + qRegisterMetaType>>(); +#if QT_CONFIG(filesystemwatcher) + q->connect(&fileInfoGatherer, SIGNAL(newListOfFiles(QString,QStringList)), + q, SLOT(_q_directoryChanged(QString,QStringList))); + q->connect(&fileInfoGatherer, SIGNAL(updates(QString, QList>)), q, + SLOT(_q_fileSystemChanged(QString, QList>))); + q->connect(&fileInfoGatherer, SIGNAL(nameResolved(QString,QString)), + q, SLOT(_q_resolvedName(QString,QString))); + q->connect(&fileInfoGatherer, SIGNAL(directoryLoaded(QString)), + q, SIGNAL(directoryLoaded(QString))); +#endif // filesystemwatcher + q->connect(&delayedSortTimer, SIGNAL(timeout()), q, SLOT(_q_performDelayedSort()), Qt::QueuedConnection); +} + +/*! + \internal + + Returns \c false if node doesn't pass the filters otherwise true + + QDir::Modified is not supported + QDir::Drives is not supported +*/ +bool QFileSystemModelPrivate::filtersAcceptsNode(const QFileSystemNode *node) const +{ + // always accept drives + if (node->parent == &root || bypassFilters.contains(node)) + return true; + + // If we don't know anything yet don't accept it + if (!node->hasInformation()) + return false; + + const bool filterPermissions = ((filters & QDir::PermissionMask) + && (filters & QDir::PermissionMask) != QDir::PermissionMask); + const bool hideDirs = !(filters & (QDir::Dirs | QDir::AllDirs)); + const bool hideFiles = !(filters & QDir::Files); + const bool hideReadable = !(!filterPermissions || (filters & QDir::Readable)); + const bool hideWritable = !(!filterPermissions || (filters & QDir::Writable)); + const bool hideExecutable = !(!filterPermissions || (filters & QDir::Executable)); + const bool hideHidden = !(filters & QDir::Hidden); + const bool hideSystem = !(filters & QDir::System); + const bool hideSymlinks = (filters & QDir::NoSymLinks); + const bool hideDot = (filters & QDir::NoDot); + const bool hideDotDot = (filters & QDir::NoDotDot); + + // Note that we match the behavior of entryList and not QFileInfo on this. + bool isDot = (node->fileName == QLatin1String(".")); + bool isDotDot = (node->fileName == QLatin1String("..")); + if ( (hideHidden && !(isDot || isDotDot) && node->isHidden()) + || (hideSystem && node->isSystem()) + || (hideDirs && node->isDir()) + || (hideFiles && node->isFile()) + || (hideSymlinks && node->isSymLink()) + || (hideReadable && node->isReadable()) + || (hideWritable && node->isWritable()) + || (hideExecutable && node->isExecutable()) + || (hideDot && isDot) + || (hideDotDot && isDotDot)) + return false; + + return nameFilterDisables || passNameFilters(node); +} + +/* + \internal + + Returns \c true if node passes the name filters and should be visible. + */ +bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const +{ +#if QT_CONFIG(regularexpression) + if (nameFilters.isEmpty()) + return true; + + // Check the name regularexpression filters + if (!(node->isDir() && (filters & QDir::AllDirs))) { + auto cs = (filters & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive; + + for (const auto &nameFilter : nameFilters) { + auto rx = QRegularExpression::fromWildcard(nameFilter, cs); + QRegularExpressionMatch match = rx.match(node->fileName); + if (match.hasMatch()) + return true; + } + return false; + } +#else + Q_UNUSED(node); +#endif + return true; +} + +QT_END_NAMESPACE + +#include "moc_qfilesystemmodel.cpp" diff --git a/src/gui/itemmodels/qfilesystemmodel.h b/src/gui/itemmodels/qfilesystemmodel.h new file mode 100644 index 0000000000..a79c4c14df --- /dev/null +++ b/src/gui/itemmodels/qfilesystemmodel.h @@ -0,0 +1,190 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWidgets module 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$ +** +****************************************************************************/ + +#ifndef QFILESYSTEMMODEL_H +#define QFILESYSTEMMODEL_H + +#include +#include +#include +#include +#include +#include + +QT_REQUIRE_CONFIG(filesystemmodel); + +QT_BEGIN_NAMESPACE + +class ExtendedInformation; +class QFileSystemModelPrivate; +class QAbstractFileIconProvider; + +class Q_GUI_EXPORT QFileSystemModel : public QAbstractItemModel +{ + Q_OBJECT + Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks) + Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) + Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables) + Q_PROPERTY(Options options READ options WRITE setOptions) + +Q_SIGNALS: + void rootPathChanged(const QString &newPath); + void fileRenamed(const QString &path, const QString &oldName, const QString &newName); + void directoryLoaded(const QString &path); + +public: + enum Roles { + FileIconRole = Qt::DecorationRole, + FilePathRole = Qt::UserRole + 1, + FileNameRole = Qt::UserRole + 2, + FilePermissions = Qt::UserRole + 3 + }; + + enum Option + { + DontWatchForChanges = 0x00000001, + DontResolveSymlinks = 0x00000002, + DontUseCustomDirectoryIcons = 0x00000004 + }; + Q_ENUM(Option) + Q_DECLARE_FLAGS(Options, Option) + + explicit QFileSystemModel(QObject *parent = nullptr); + ~QFileSystemModel(); + + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; + QModelIndex index(const QString &path, int column = 0) const; + QModelIndex parent(const QModelIndex &child) const override; + using QObject::parent; + QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; + bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; + bool canFetchMore(const QModelIndex &parent) const override; + void fetchMore(const QModelIndex &parent) override; + + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + int columnCount(const QModelIndex &parent = QModelIndex()) const override; + + QVariant myComputer(int role = Qt::DisplayRole) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; + + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; + + Qt::ItemFlags flags(const QModelIndex &index) const override; + + void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; + + QStringList mimeTypes() const override; + QMimeData *mimeData(const QModelIndexList &indexes) const override; + bool dropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) override; + Qt::DropActions supportedDropActions() const override; + QHash roleNames() const override; + + // QFileSystemModel specific API + QModelIndex setRootPath(const QString &path); + QString rootPath() const; + QDir rootDirectory() const; + + void setIconProvider(QAbstractFileIconProvider *provider); + QAbstractFileIconProvider *iconProvider() const; + + void setFilter(QDir::Filters filters); + QDir::Filters filter() const; + + void setResolveSymlinks(bool enable); + bool resolveSymlinks() const; + + void setReadOnly(bool enable); + bool isReadOnly() const; + + void setNameFilterDisables(bool enable); + bool nameFilterDisables() const; + + void setNameFilters(const QStringList &filters); + QStringList nameFilters() const; + + void setOption(Option option, bool on = true); + bool testOption(Option option) const; + void setOptions(Options options); + Options options() const; + + QString filePath(const QModelIndex &index) const; + bool isDir(const QModelIndex &index) const; + qint64 size(const QModelIndex &index) const; + QString type(const QModelIndex &index) const; + QDateTime lastModified(const QModelIndex &index) const; + + QModelIndex mkdir(const QModelIndex &parent, const QString &name); + bool rmdir(const QModelIndex &index); + inline QString fileName(const QModelIndex &index) const; + inline QIcon fileIcon(const QModelIndex &index) const; + QFile::Permissions permissions(const QModelIndex &index) const; + QFileInfo fileInfo(const QModelIndex &index) const; + bool remove(const QModelIndex &index); + +protected: + QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr); + void timerEvent(QTimerEvent *event) override; + bool event(QEvent *event) override; + +private: + Q_DECLARE_PRIVATE(QFileSystemModel) + Q_DISABLE_COPY(QFileSystemModel) + + Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list)) + Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort()) + Q_PRIVATE_SLOT(d_func(), + void _q_fileSystemChanged(const QString &path, + const QList> &)) + Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName)) + + friend class QFileDialogPrivate; +}; + +inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const +{ return aindex.data(Qt::DisplayRole).toString(); } +inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const +{ return qvariant_cast(aindex.data(Qt::DecorationRole)); } + +Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options) + +QT_END_NAMESPACE + +#endif // QFILESYSTEMMODEL_H diff --git a/src/gui/itemmodels/qfilesystemmodel_p.h b/src/gui/itemmodels/qfilesystemmodel_p.h new file mode 100644 index 0000000000..092eae3b2e --- /dev/null +++ b/src/gui/itemmodels/qfilesystemmodel_p.h @@ -0,0 +1,323 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtWidgets module 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$ +** +****************************************************************************/ + +#ifndef QFILESYSTEMMODEL_P_H +#define QFILESYSTEMMODEL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include +#include "qfilesystemmodel.h" + +#include +#include +#include "qfileinfogatherer_p.h" +#include +#include +#include +#include +#include +#include + +QT_REQUIRE_CONFIG(filesystemmodel); + +QT_BEGIN_NAMESPACE + +class ExtendedInformation; +class QFileSystemModelPrivate; +class QFileIconProvider; + +#if defined(Q_OS_WIN) +class QFileSystemModelNodePathKey : public QString +{ +public: + QFileSystemModelNodePathKey() {} + QFileSystemModelNodePathKey(const QString &other) : QString(other) {} + QFileSystemModelNodePathKey(const QFileSystemModelNodePathKey &other) : QString(other) {} + bool operator==(const QFileSystemModelNodePathKey &other) const { return !compare(other, Qt::CaseInsensitive); } +}; + +Q_DECLARE_TYPEINFO(QFileSystemModelNodePathKey, Q_MOVABLE_TYPE); + +inline size_t qHash(const QFileSystemModelNodePathKey &key) { return qHash(key.toCaseFolded()); } +#else // Q_OS_WIN +typedef QString QFileSystemModelNodePathKey; +#endif + +class Q_GUI_EXPORT QFileSystemModelPrivate : public QAbstractItemModelPrivate +{ + Q_DECLARE_PUBLIC(QFileSystemModel) + +public: + enum { NumColumns = 4 }; + + class QFileSystemNode + { + public: + Q_DISABLE_COPY_MOVE(QFileSystemNode) + + explicit QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = nullptr) + : fileName(filename), parent(p) {} + ~QFileSystemNode() { + qDeleteAll(children); + delete info; + } + + QString fileName; +#if defined(Q_OS_WIN) + QString volumeName; +#endif + + inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; } + inline QString type() const { if (info) return info->displayType; return QLatin1String(""); } + inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); } + inline QFile::Permissions permissions() const { if (info) return info->permissions(); return { }; } + inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); } + inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); } + inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); } + inline bool isDir() const { + if (info) + return info->isDir(); + if (children.count() > 0) + return true; + return false; + } + inline QFileInfo fileInfo() const { if (info) return info->fileInfo(); return QFileInfo(); } + inline bool isFile() const { if (info) return info->isFile(); return true; } + inline bool isSystem() const { if (info) return info->isSystem(); return true; } + inline bool isHidden() const { if (info) return info->isHidden(); return false; } + inline bool isSymLink(bool ignoreNtfsSymLinks = false) const { return info && info->isSymLink(ignoreNtfsSymLinks); } + inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; } + inline QIcon icon() const { if (info) return info->icon; return QIcon(); } + + inline bool operator <(const QFileSystemNode &node) const { + if (caseSensitive() || node.caseSensitive()) + return fileName < node.fileName; + return QString::compare(fileName, node.fileName, Qt::CaseInsensitive) < 0; + } + inline bool operator >(const QString &name) const { + if (caseSensitive()) + return fileName > name; + return QString::compare(fileName, name, Qt::CaseInsensitive) > 0; + } + inline bool operator <(const QString &name) const { + if (caseSensitive()) + return fileName < name; + return QString::compare(fileName, name, Qt::CaseInsensitive) < 0; + } + inline bool operator !=(const QExtendedInformation &fileInfo) const { + return !operator==(fileInfo); + } + bool operator ==(const QString &name) const { + if (caseSensitive()) + return fileName == name; + return QString::compare(fileName, name, Qt::CaseInsensitive) == 0; + } + bool operator ==(const QExtendedInformation &fileInfo) const { + return info && (*info == fileInfo); + } + + inline bool hasInformation() const { return info != nullptr; } + + void populate(const QExtendedInformation &fileInfo) { + if (!info) + info = new QExtendedInformation(fileInfo.fileInfo()); + (*info) = fileInfo; + } + + // children shouldn't normally be accessed directly, use node() + inline int visibleLocation(const QString &childName) { + return visibleChildren.indexOf(childName); + } + void updateIcon(QAbstractFileIconProvider *iconProvider, const QString &path) { + if (info) + info->icon = iconProvider->icon(QFileInfo(path)); + for (QFileSystemNode *child : qAsConst(children)) { + //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/) + if (!path.isEmpty()) { + if (path.endsWith(QLatin1Char('/'))) + child->updateIcon(iconProvider, path + child->fileName); + else + child->updateIcon(iconProvider, path + QLatin1Char('/') + child->fileName); + } else + child->updateIcon(iconProvider, child->fileName); + } + } + + void retranslateStrings(QAbstractFileIconProvider *iconProvider, const QString &path) { + if (info) + info->displayType = iconProvider->type(QFileInfo(path)); + for (QFileSystemNode *child : qAsConst(children)) { + //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/) + if (!path.isEmpty()) { + if (path.endsWith(QLatin1Char('/'))) + child->retranslateStrings(iconProvider, path + child->fileName); + else + child->retranslateStrings(iconProvider, path + QLatin1Char('/') + child->fileName); + } else + child->retranslateStrings(iconProvider, child->fileName); + } + } + + QHash children; + QList visibleChildren; + QExtendedInformation *info = nullptr; + QFileSystemNode *parent; + int dirtyChildrenIndex = -1; + bool populatedChildren = false; + bool isVisible = false; + }; + + QFileSystemModelPrivate() = default; + void init(); + /* + \internal + + Return true if index which is owned by node is hidden by the filter. + */ + inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const + { + return (indexNode != &root && !index.isValid()); + } + QFileSystemNode *node(const QModelIndex &index) const; + QFileSystemNode *node(const QString &path, bool fetch = true) const; + inline QModelIndex index(const QString &path, int column = 0) { return index(node(path), column); } + QModelIndex index(const QFileSystemNode *node, int column = 0) const; + bool filtersAcceptsNode(const QFileSystemNode *node) const; + bool passNameFilters(const QFileSystemNode *node) const; + void removeNode(QFileSystemNode *parentNode, const QString &name); + QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info); + void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles); + void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation); + void sortChildren(int column, const QModelIndex &parent); + + inline int translateVisibleLocation(QFileSystemNode *parent, int row) const { + if (sortOrder != Qt::AscendingOrder) { + if (parent->dirtyChildrenIndex == -1) + return parent->visibleChildren.count() - row - 1; + + if (row < parent->dirtyChildrenIndex) + return parent->dirtyChildrenIndex - row - 1; + } + + return row; + } + + inline static QString myComputer() { + // ### TODO We should query the system to find out what the string should be + // XP == "My Computer", + // Vista == "Computer", + // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4" +#ifdef Q_OS_WIN + return QFileSystemModel::tr("My Computer"); +#else + return QFileSystemModel::tr("Computer"); +#endif + } + + inline void delayedSort() { + if (!delayedSortTimer.isActive()) + delayedSortTimer.start(0); + } + + QIcon icon(const QModelIndex &index) const; + QString name(const QModelIndex &index) const; + QString displayName(const QModelIndex &index) const; + QString filePath(const QModelIndex &index) const; + QString size(const QModelIndex &index) const; + static QString size(qint64 bytes); + QString type(const QModelIndex &index) const; + QString time(const QModelIndex &index) const; + + void _q_directoryChanged(const QString &directory, const QStringList &list); + void _q_performDelayedSort(); + void _q_fileSystemChanged(const QString &path, const QList> &); + void _q_resolvedName(const QString &fileName, const QString &resolvedName); + + QDir rootDir; +#if QT_CONFIG(filesystemwatcher) +# ifdef Q_OS_WIN + QStringList unwatchPathsAt(const QModelIndex &); + void watchPaths(const QStringList &paths) { fileInfoGatherer.watchPaths(paths); } +# endif // Q_OS_WIN + QFileInfoGatherer fileInfoGatherer; +#endif // filesystemwatcher + QTimer delayedSortTimer; + QHash bypassFilters; +#if QT_CONFIG(regularexpression) + QStringList nameFilters; +#endif + QHash resolvedSymLinks; + + QFileSystemNode root; + + struct Fetching { + QString dir; + QString file; + const QFileSystemNode *node; + }; + QList toFetch; + + QBasicTimer fetchingTimer; + + QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs; + int sortColumn = 0; + Qt::SortOrder sortOrder = Qt::AscendingOrder; + bool forceSort = true; + bool readOnly = true; + bool setRootPath = false; + bool nameFilterDisables = true; // false on windows, true on mac and unix + // This flag is an optimization for QFileDialog. It enables a sort which is + // not recursive, meaning we sort only what we see. + bool disableRecursiveSort = false; +}; +Q_DECLARE_TYPEINFO(QFileSystemModelPrivate::Fetching, Q_MOVABLE_TYPE); + +QT_END_NAMESPACE + +#endif diff --git a/src/printsupport/dialogs/qprintdialog_unix.cpp b/src/printsupport/dialogs/qprintdialog_unix.cpp index 6dcb613ff5..60e177f3e8 100644 --- a/src/printsupport/dialogs/qprintdialog_unix.cpp +++ b/src/printsupport/dialogs/qprintdialog_unix.cpp @@ -54,7 +54,7 @@ #include #include #if QT_CONFIG(filesystemmodel) -#include +#include #endif #include #include diff --git a/src/tools/uic/qclass_lib_map.h b/src/tools/uic/qclass_lib_map.h index 797bcf6e76..8558b74361 100644 --- a/src/tools/uic/qclass_lib_map.h +++ b/src/tools/uic/qclass_lib_map.h @@ -536,7 +536,7 @@ QT_CLASS_LIB(QColorDialog, QtWidgets, qcolordialog.h) QT_CLASS_LIB(QDialog, QtWidgets, qdialog.h) QT_CLASS_LIB(QErrorMessage, QtWidgets, qerrormessage.h) QT_CLASS_LIB(QFileDialog, QtWidgets, qfiledialog.h) -QT_CLASS_LIB(QFileSystemModel, QtWidgets, qfilesystemmodel.h) +QT_CLASS_LIB(QFileSystemModel, QtGui, qfilesystemmodel.h) QT_CLASS_LIB(QFontDialog, QtWidgets, qfontdialog.h) QT_CLASS_LIB(QInputDialog, QtWidgets, qinputdialog.h) QT_CLASS_LIB(QMessageBox, QtWidgets, qmessagebox.h) diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index 915fd7a99f..b20f4a514d 100644 --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -624,12 +624,6 @@ qt_extend_target(Widgets CONDITION QT_FEATURE_filedialog uic ) -qt_extend_target(Widgets CONDITION QT_FEATURE_filesystemmodel - SOURCES - dialogs/qfileinfogatherer.cpp dialogs/qfileinfogatherer_p.h - dialogs/qfilesystemmodel.cpp dialogs/qfilesystemmodel.h dialogs/qfilesystemmodel_p.h -) - qt_extend_target(Widgets CONDITION QT_FEATURE_fontdialog SOURCES dialogs/qfontdialog.cpp dialogs/qfontdialog.h dialogs/qfontdialog_p.h diff --git a/src/widgets/configure.cmake b/src/widgets/configure.cmake index 090fb7b64c..fa55073f62 100644 --- a/src/widgets/configure.cmake +++ b/src/widgets/configure.cmake @@ -50,13 +50,6 @@ qt_feature("effects" PRIVATE LABEL "Effects" PURPOSE "Provides special widget effects (e.g. fading and scrolling)." ) -qt_feature("filesystemmodel" PUBLIC - SECTION "File I/O" - LABEL "QFileSystemModel" - PURPOSE "Provides a data model for the local filesystem." - CONDITION QT_FEATURE_itemmodel -) -qt_feature_definition("filesystemmodel" "QT_NO_FILESYSTEMMODEL" NEGATE VALUE "1") qt_feature("itemviews" PUBLIC SECTION "ItemViews" LABEL "The Model/View Framework" diff --git a/src/widgets/configure.json b/src/widgets/configure.json index 5b984c3bf2..f2e88608de 100644 --- a/src/widgets/configure.json +++ b/src/widgets/configure.json @@ -69,13 +69,6 @@ "section": "Kernel", "output": [ "privateFeature" ] }, - "filesystemmodel": { - "label": "QFileSystemModel", - "purpose": "Provides a data model for the local filesystem.", - "section": "File I/O", - "condition": "features.itemmodel", - "output": [ "publicFeature", "feature" ] - }, "itemviews": { "label": "The Model/View Framework", "purpose": "Provides the model/view architecture managing the relationship between data and the way it is presented to the user.", diff --git a/src/widgets/dialogs/dialogs.pri b/src/widgets/dialogs/dialogs.pri index a6a6b2d352..75e0f5fcf8 100644 --- a/src/widgets/dialogs/dialogs.pri +++ b/src/widgets/dialogs/dialogs.pri @@ -34,17 +34,6 @@ qtConfig(filedialog) { FORMS += dialogs/qfiledialog.ui } -qtConfig(filesystemmodel) { - HEADERS += \ - dialogs/qfilesystemmodel.h \ - dialogs/qfilesystemmodel_p.h \ - dialogs/qfileinfogatherer_p.h - - SOURCES += \ - dialogs/qfilesystemmodel.cpp \ - dialogs/qfileinfogatherer.cpp -} - qtConfig(fontdialog) { HEADERS += \ dialogs/qfontdialog.h \ diff --git a/src/widgets/dialogs/qfiledialog.cpp b/src/widgets/dialogs/qfiledialog.cpp index bd58062375..81e182c352 100644 --- a/src/widgets/dialogs/qfiledialog.cpp +++ b/src/widgets/dialogs/qfiledialog.cpp @@ -1952,7 +1952,7 @@ QAbstractItemDelegate *QFileDialog::itemDelegate() const /*! Sets the icon provider used by the filedialog to the specified \a provider. */ -void QFileDialog::setIconProvider(QFileIconProvider *provider) +void QFileDialog::setIconProvider(QAbstractFileIconProvider *provider) { Q_D(QFileDialog); if (!d->usingWidgets()) @@ -1965,7 +1965,7 @@ void QFileDialog::setIconProvider(QFileIconProvider *provider) /*! Returns the icon provider used by the filedialog. */ -QFileIconProvider *QFileDialog::iconProvider() const +QAbstractFileIconProvider *QFileDialog::iconProvider() const { Q_D(const QFileDialog); if (!d->model) @@ -2977,6 +2977,7 @@ void QFileDialogPrivate::createWidgets() Qt::WindowStates preState = q->windowState(); model = new QFileSystemModel(q); + model->setIconProvider(&defaultIconProvider); model->setFilter(options->filter()); model->setObjectName(QLatin1String("qt_filesystem_model")); if (QPlatformFileDialogHelper *helper = platformFileDialogHelper()) diff --git a/src/widgets/dialogs/qfiledialog.h b/src/widgets/dialogs/qfiledialog.h index 0949302ef5..0a6b9e62ef 100644 --- a/src/widgets/dialogs/qfiledialog.h +++ b/src/widgets/dialogs/qfiledialog.h @@ -55,8 +55,8 @@ QT_BEGIN_NAMESPACE class QModelIndex; class QItemSelection; struct QFileDialogArgs; -class QFileIconProvider; class QFileDialogPrivate; +class QAbstractFileIconProvider; class QAbstractItemDelegate; class QAbstractProxyModel; @@ -153,8 +153,8 @@ public: void setItemDelegate(QAbstractItemDelegate *delegate); QAbstractItemDelegate *itemDelegate() const; - void setIconProvider(QFileIconProvider *provider); - QFileIconProvider *iconProvider() const; + void setIconProvider(QAbstractFileIconProvider *provider); + QAbstractFileIconProvider *iconProvider() const; void setLabelText(DialogLabel label, const QString &text); QString labelText(DialogLabel label) const; diff --git a/src/widgets/dialogs/qfiledialog_p.h b/src/widgets/dialogs/qfiledialog_p.h index 1d9929e36a..16b8b2f45b 100644 --- a/src/widgets/dialogs/qfiledialog_p.h +++ b/src/widgets/dialogs/qfiledialog_p.h @@ -57,7 +57,7 @@ #include "private/qdialog_p.h" #include "qplatformdefs.h" -#include "qfilesystemmodel_p.h" +#include #include #include #include @@ -69,6 +69,7 @@ #include #include #include +#include #if QT_CONFIG(completer) #include #endif @@ -285,6 +286,7 @@ public: QByteArray splitterState; QByteArray headerData; QList sidebarUrls; + QFileIconProvider defaultIconProvider; ~QFileDialogPrivate(); diff --git a/src/widgets/dialogs/qfileinfogatherer.cpp b/src/widgets/dialogs/qfileinfogatherer.cpp deleted file mode 100644 index 11d963226f..0000000000 --- a/src/widgets/dialogs/qfileinfogatherer.cpp +++ /dev/null @@ -1,443 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module 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$ -** -****************************************************************************/ - -#include "qfileinfogatherer_p.h" -#include -#include -#include -#ifndef Q_OS_WIN -# include -# include -#endif -#if defined(Q_OS_VXWORKS) -# include "qplatformdefs.h" -#endif - -QT_BEGIN_NAMESPACE - -#ifdef QT_BUILD_INTERNAL -static QBasicAtomicInt fetchedRoot = Q_BASIC_ATOMIC_INITIALIZER(false); -Q_AUTOTEST_EXPORT void qt_test_resetFetchedRoot() -{ - fetchedRoot.storeRelaxed(false); -} - -Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot() -{ - return fetchedRoot.loadRelaxed(); -} -#endif - -static QString translateDriveName(const QFileInfo &drive) -{ - QString driveName = drive.absoluteFilePath(); -#ifdef Q_OS_WIN - if (driveName.startsWith(QLatin1Char('/'))) // UNC host - return drive.fileName(); - if (driveName.endsWith(QLatin1Char('/'))) - driveName.chop(1); -#endif // Q_OS_WIN - return driveName; -} - -/*! - Creates thread -*/ -QFileInfoGatherer::QFileInfoGatherer(QObject *parent) - : QThread(parent) - , m_iconProvider(&defaultProvider) -{ - start(LowPriority); -} - -/*! - Destroys thread -*/ -QFileInfoGatherer::~QFileInfoGatherer() -{ - abort.storeRelaxed(true); - QMutexLocker locker(&mutex); - condition.wakeAll(); - locker.unlock(); - wait(); -} - -void QFileInfoGatherer::setResolveSymlinks(bool enable) -{ - Q_UNUSED(enable); -#ifdef Q_OS_WIN - m_resolveSymlinks = enable; -#endif -} - -void QFileInfoGatherer::driveAdded() -{ - fetchExtendedInformation(QString(), QStringList()); -} - -void QFileInfoGatherer::driveRemoved() -{ - QStringList drives; - const QFileInfoList driveInfoList = QDir::drives(); - for (const QFileInfo &fi : driveInfoList) - drives.append(translateDriveName(fi)); - newListOfFiles(QString(), drives); -} - -bool QFileInfoGatherer::resolveSymlinks() const -{ -#ifdef Q_OS_WIN - return m_resolveSymlinks; -#else - return false; -#endif -} - -void QFileInfoGatherer::setIconProvider(QFileIconProvider *provider) -{ - m_iconProvider = provider; -} - -QFileIconProvider *QFileInfoGatherer::iconProvider() const -{ - return m_iconProvider; -} - -/*! - Fetch extended information for all \a files in \a path - - \sa updateFile(), update(), resolvedName() -*/ -void QFileInfoGatherer::fetchExtendedInformation(const QString &path, const QStringList &files) -{ - QMutexLocker locker(&mutex); - // See if we already have this dir/file in our queue - int loc = this->path.lastIndexOf(path); - while (loc > 0) { - if (this->files.at(loc) == files) { - return; - } - loc = this->path.lastIndexOf(path, loc - 1); - } - this->path.push(path); - this->files.push(files); - condition.wakeAll(); - -#if QT_CONFIG(filesystemwatcher) - if (files.isEmpty() - && !path.isEmpty() - && !path.startsWith(QLatin1String("//")) /*don't watch UNC path*/) { - if (!watchedDirectories().contains(path)) - watchPaths(QStringList(path)); - } -#endif -} - -/*! - Fetch extended information for all \a filePath - - \sa fetchExtendedInformation() -*/ -void QFileInfoGatherer::updateFile(const QString &filePath) -{ - QString dir = filePath.mid(0, filePath.lastIndexOf(QLatin1Char('/'))); - QString fileName = filePath.mid(dir.length() + 1); - fetchExtendedInformation(dir, QStringList(fileName)); -} - -QStringList QFileInfoGatherer::watchedFiles() const -{ -#if QT_CONFIG(filesystemwatcher) - if (m_watcher) - return m_watcher->files(); -#endif - return {}; -} - -QStringList QFileInfoGatherer::watchedDirectories() const -{ -#if QT_CONFIG(filesystemwatcher) - if (m_watcher) - return m_watcher->directories(); -#endif - return {}; -} - -void QFileInfoGatherer::createWatcher() -{ -#if QT_CONFIG(filesystemwatcher) - m_watcher = new QFileSystemWatcher(this); - connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &QFileInfoGatherer::list); - connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &QFileInfoGatherer::updateFile); -# if defined(Q_OS_WIN) - const QVariant listener = m_watcher->property("_q_driveListener"); - if (listener.canConvert()) { - if (QObject *driveListener = listener.value()) { - connect(driveListener, SIGNAL(driveAdded()), this, SLOT(driveAdded())); - connect(driveListener, SIGNAL(driveRemoved()), this, SLOT(driveRemoved())); - } - } -# endif // Q_OS_WIN -#endif -} - -void QFileInfoGatherer::watchPaths(const QStringList &paths) -{ -#if QT_CONFIG(filesystemwatcher) - if (m_watching) { - if (m_watcher == nullptr) - createWatcher(); - m_watcher->addPaths(paths); - } -#else - Q_UNUSED(paths); -#endif -} - -void QFileInfoGatherer::unwatchPaths(const QStringList &paths) -{ -#if QT_CONFIG(filesystemwatcher) - if (m_watcher && !paths.isEmpty()) - m_watcher->removePaths(paths); -#else - Q_UNUSED(paths); -#endif -} - -bool QFileInfoGatherer::isWatching() const -{ - bool result = false; -#if QT_CONFIG(filesystemwatcher) - QMutexLocker locker(&mutex); - result = m_watching; -#endif - return result; -} - -void QFileInfoGatherer::setWatching(bool v) -{ -#if QT_CONFIG(filesystemwatcher) - QMutexLocker locker(&mutex); - if (v != m_watching) { - if (!v) { - delete m_watcher; - m_watcher = nullptr; - } - m_watching = v; - } -#else - Q_UNUSED(v); -#endif -} - -/* - List all files in \a directoryPath - - \sa listed() -*/ -void QFileInfoGatherer::clear() -{ -#if QT_CONFIG(filesystemwatcher) - QMutexLocker locker(&mutex); - unwatchPaths(watchedFiles()); - unwatchPaths(watchedDirectories()); -#endif -} - -/* - Remove a \a path from the watcher - - \sa listed() -*/ -void QFileInfoGatherer::removePath(const QString &path) -{ -#if QT_CONFIG(filesystemwatcher) - QMutexLocker locker(&mutex); - unwatchPaths(QStringList(path)); -#else - Q_UNUSED(path); -#endif -} - -/* - List all files in \a directoryPath - - \sa listed() -*/ -void QFileInfoGatherer::list(const QString &directoryPath) -{ - fetchExtendedInformation(directoryPath, QStringList()); -} - -/* - Until aborted wait to fetch a directory or files -*/ -void QFileInfoGatherer::run() -{ - forever { - QMutexLocker locker(&mutex); - while (!abort.loadRelaxed() && path.isEmpty()) - condition.wait(&mutex); - if (abort.loadRelaxed()) - return; - const QString thisPath = qAsConst(path).front(); - path.pop_front(); - const QStringList thisList = qAsConst(files).front(); - files.pop_front(); - locker.unlock(); - - getFileInfos(thisPath, thisList); - } -} - -QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const -{ - QExtendedInformation info(fileInfo); - info.icon = m_iconProvider->icon(fileInfo); - info.displayType = m_iconProvider->type(fileInfo); -#if QT_CONFIG(filesystemwatcher) - // ### Not ready to listen all modifications by default - static const bool watchFiles = qEnvironmentVariableIsSet("QT_FILESYSTEMMODEL_WATCH_FILES"); - if (watchFiles) { - if (!fileInfo.exists() && !fileInfo.isSymLink()) { - const_cast(this)-> - unwatchPaths(QStringList(fileInfo.absoluteFilePath())); - } else { - const QString path = fileInfo.absoluteFilePath(); - if (!path.isEmpty() && fileInfo.exists() && fileInfo.isFile() && fileInfo.isReadable() - && !watchedFiles().contains(path)) { - const_cast(this)->watchPaths(QStringList(path)); - } - } - } -#endif // filesystemwatcher - -#ifdef Q_OS_WIN - if (m_resolveSymlinks && info.isSymLink(/* ignoreNtfsSymLinks = */ true)) { - QFileInfo resolvedInfo(fileInfo.symLinkTarget()); - resolvedInfo = resolvedInfo.canonicalFilePath(); - if (resolvedInfo.exists()) { - emit nameResolved(fileInfo.filePath(), resolvedInfo.fileName()); - } - } -#endif - return info; -} - -/* - Get specific file info's, batch the files so update when we have 100 - items and every 200ms after that - */ -void QFileInfoGatherer::getFileInfos(const QString &path, const QStringList &files) -{ - // List drives - if (path.isEmpty()) { -#ifdef QT_BUILD_INTERNAL - fetchedRoot.storeRelaxed(true); -#endif - QFileInfoList infoList; - if (files.isEmpty()) { - infoList = QDir::drives(); - } else { - infoList.reserve(files.count()); - for (const auto &file : files) - infoList << QFileInfo(file); - } - QList> updatedFiles; - updatedFiles.reserve(infoList.count()); - for (int i = infoList.count() - 1; i >= 0; --i) { - QFileInfo driveInfo = infoList.at(i); - driveInfo.stat(); - QString driveName = translateDriveName(driveInfo); - updatedFiles.append(QPair(driveName, driveInfo)); - } - emit updates(path, updatedFiles); - return; - } - - QElapsedTimer base; - base.start(); - QFileInfo fileInfo; - bool firstTime = true; - QList> updatedFiles; - QStringList filesToCheck = files; - - QStringList allFiles; - if (files.isEmpty()) { - QDirIterator dirIt(path, QDir::AllEntries | QDir::System | QDir::Hidden); - while (!abort.loadRelaxed() && dirIt.hasNext()) { - dirIt.next(); - fileInfo = dirIt.fileInfo(); - fileInfo.stat(); - allFiles.append(fileInfo.fileName()); - fetch(fileInfo, base, firstTime, updatedFiles, path); - } - } - if (!allFiles.isEmpty()) - emit newListOfFiles(path, allFiles); - - QStringList::const_iterator filesIt = filesToCheck.constBegin(); - while (!abort.loadRelaxed() && filesIt != filesToCheck.constEnd()) { - fileInfo.setFile(path + QDir::separator() + *filesIt); - ++filesIt; - fileInfo.stat(); - fetch(fileInfo, base, firstTime, updatedFiles, path); - } - if (!updatedFiles.isEmpty()) - emit updates(path, updatedFiles); - emit directoryLoaded(path); -} - -void QFileInfoGatherer::fetch(const QFileInfo &fileInfo, QElapsedTimer &base, bool &firstTime, - QList> &updatedFiles, const QString &path) -{ - updatedFiles.append(QPair(fileInfo.fileName(), fileInfo)); - QElapsedTimer current; - current.start(); - if ((firstTime && updatedFiles.count() > 100) || base.msecsTo(current) > 1000) { - emit updates(path, updatedFiles); - updatedFiles.clear(); - base = current; - firstTime = false; - } -} - -QT_END_NAMESPACE - -#include "moc_qfileinfogatherer_p.cpp" diff --git a/src/widgets/dialogs/qfileinfogatherer_p.h b/src/widgets/dialogs/qfileinfogatherer_p.h deleted file mode 100644 index 664ea74c02..0000000000 --- a/src/widgets/dialogs/qfileinfogatherer_p.h +++ /dev/null @@ -1,230 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module 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$ -** -****************************************************************************/ - -#ifndef QFILEINFOGATHERER_H -#define QFILEINFOGATHERER_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include - -#include -#include -#include -#if QT_CONFIG(filesystemwatcher) -#include -#endif -#include -#include -#include -#include -#include -#include - -#include - -QT_REQUIRE_CONFIG(filesystemmodel); - -QT_BEGIN_NAMESPACE - -class QExtendedInformation { -public: - enum Type { Dir, File, System }; - - QExtendedInformation() {} - QExtendedInformation(const QFileInfo &info) : mFileInfo(info) {} - - inline bool isDir() { return type() == Dir; } - inline bool isFile() { return type() == File; } - inline bool isSystem() { return type() == System; } - - bool operator ==(const QExtendedInformation &fileInfo) const { - return mFileInfo == fileInfo.mFileInfo - && displayType == fileInfo.displayType - && permissions() == fileInfo.permissions() - && lastModified() == fileInfo.lastModified(); - } - -#ifndef QT_NO_FSFILEENGINE - bool isCaseSensitive() const { - return QFileSystemEngine::isCaseSensitive(); - } -#endif - - QFile::Permissions permissions() const { - return mFileInfo.permissions(); - } - - Type type() const { - if (mFileInfo.isDir()) { - return QExtendedInformation::Dir; - } - if (mFileInfo.isFile()) { - return QExtendedInformation::File; - } - if (!mFileInfo.exists() && mFileInfo.isSymLink()) { - return QExtendedInformation::System; - } - return QExtendedInformation::System; - } - - bool isSymLink(bool ignoreNtfsSymLinks = false) const - { - if (ignoreNtfsSymLinks) { -#ifdef Q_OS_WIN - return !mFileInfo.suffix().compare(QLatin1String("lnk"), Qt::CaseInsensitive); -#endif - } - return mFileInfo.isSymLink(); - } - - bool isHidden() const { - return mFileInfo.isHidden(); - } - - QFileInfo fileInfo() const { - return mFileInfo; - } - - QDateTime lastModified() const { - return mFileInfo.lastModified(); - } - - qint64 size() const { - qint64 size = -1; - if (type() == QExtendedInformation::Dir) - size = 0; - if (type() == QExtendedInformation::File) - size = mFileInfo.size(); - if (!mFileInfo.exists() && !mFileInfo.isSymLink()) - size = -1; - return size; - } - - QString displayType; - QIcon icon; - -private : - QFileInfo mFileInfo; -}; - -class QFileIconProvider; - -class Q_AUTOTEST_EXPORT QFileInfoGatherer : public QThread -{ -Q_OBJECT - -Q_SIGNALS: - void updates(const QString &directory, const QList> &updates); - void newListOfFiles(const QString &directory, const QStringList &listOfFiles) const; - void nameResolved(const QString &fileName, const QString &resolvedName) const; - void directoryLoaded(const QString &path); - -public: - explicit QFileInfoGatherer(QObject *parent = nullptr); - ~QFileInfoGatherer(); - - QStringList watchedFiles() const; - QStringList watchedDirectories() const; - void watchPaths(const QStringList &paths); - void unwatchPaths(const QStringList &paths); - - bool isWatching() const; - void setWatching(bool v); - - // only callable from this->thread(): - void clear(); - void removePath(const QString &path); - QExtendedInformation getInfo(const QFileInfo &info) const; - QFileIconProvider *iconProvider() const; - bool resolveSymlinks() const; - -public Q_SLOTS: - void list(const QString &directoryPath); - void fetchExtendedInformation(const QString &path, const QStringList &files); - void updateFile(const QString &path); - void setResolveSymlinks(bool enable); - void setIconProvider(QFileIconProvider *provider); - -private Q_SLOTS: - void driveAdded(); - void driveRemoved(); - -private: - void run() override; - // called by run(): - void getFileInfos(const QString &path, const QStringList &files); - void fetch(const QFileInfo &info, QElapsedTimer &base, bool &firstTime, - QList> &updatedFiles, const QString &path); - -private: - void createWatcher(); - - mutable QMutex mutex; - // begin protected by mutex - QWaitCondition condition; - QStack path; - QStack files; - // end protected by mutex - QAtomicInt abort; - -#if QT_CONFIG(filesystemwatcher) - QFileSystemWatcher *m_watcher = nullptr; -#endif - QFileIconProvider *m_iconProvider; // not accessed by run() - QFileIconProvider defaultProvider; -#ifdef Q_OS_WIN - bool m_resolveSymlinks = true; // not accessed by run() -#endif -#if QT_CONFIG(filesystemwatcher) - bool m_watching = true; -#endif -}; - -QT_END_NAMESPACE -#endif // QFILEINFOGATHERER_H diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp deleted file mode 100644 index 4b933cdb8d..0000000000 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ /dev/null @@ -1,2164 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module 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$ -** -****************************************************************************/ - -#include "qfilesystemmodel_p.h" -#include "qfilesystemmodel.h" -#include -#include -#include -#include -#include -#if QT_CONFIG(regularexpression) -# include -#endif - -#include - -#ifdef Q_OS_WIN -# include -# include -# include -#endif - -QT_BEGIN_NAMESPACE - -/*! - \enum QFileSystemModel::Roles - \value FileIconRole - \value FilePathRole - \value FileNameRole - \value FilePermissions -*/ - -/*! - \class QFileSystemModel - \since 4.4 - - \brief The QFileSystemModel class provides a data model for the local filesystem. - - \ingroup model-view - \inmodule QtWidgets - - This class provides access to the local filesystem, providing functions - for renaming and removing files and directories, and for creating new - directories. In the simplest case, it can be used with a suitable display - widget as part of a browser or filter. - - QFileSystemModel can be accessed using the standard interface provided by - QAbstractItemModel, but it also provides some convenience functions that are - specific to a directory model. - The fileInfo(), isDir(), fileName() and filePath() functions provide information - about the underlying files and directories related to items in the model. - Directories can be created and removed using mkdir(), rmdir(). - - \note QFileSystemModel requires an instance of \l QApplication. - - \section1 Example Usage - - A directory model that displays the contents of a default directory - is usually constructed with a parent object: - - \snippet shareddirmodel/main.cpp 2 - - A tree view can be used to display the contents of the model - - \snippet shareddirmodel/main.cpp 4 - - and the contents of a particular directory can be displayed by - setting the tree view's root index: - - \snippet shareddirmodel/main.cpp 7 - - The view's root index can be used to control how much of a - hierarchical model is displayed. QFileSystemModel provides a convenience - function that returns a suitable model index for a path to a - directory within the model. - - \section1 Caching and Performance - - QFileSystemModel will not fetch any files or directories until setRootPath() - is called. This will prevent any unnecessary querying on the file system - until that point such as listing the drives on Windows. - - QFileSystemModel uses a separate thread to populate itself so it will not - cause the main thread to hang as the file system is being queried. - Calls to rowCount() will return 0 until the model populates a directory. - - QFileSystemModel keeps a cache with file information. The cache is - automatically kept up to date using the QFileSystemWatcher. - - \sa {Model Classes} -*/ - -/*! - \fn bool QFileSystemModel::rmdir(const QModelIndex &index) - - Removes the directory corresponding to the model item \a index in the - file system model and \b{deletes the corresponding directory from the - file system}, returning true if successful. If the directory cannot be - removed, false is returned. - - \warning This function deletes directories from the file system; it does - \b{not} move them to a location where they can be recovered. - - \sa remove() -*/ - -/*! - \fn QIcon QFileSystemModel::fileName(const QModelIndex &index) const - - Returns the file name for the item stored in the model under the given - \a index. -*/ - -/*! - \fn QIcon QFileSystemModel::fileIcon(const QModelIndex &index) const - - Returns the icon for the item stored in the model under the given - \a index. -*/ - -/*! - \fn QFileInfo QFileSystemModel::fileInfo(const QModelIndex &index) const - - Returns the QFileInfo for the item stored in the model under the given - \a index. -*/ -QFileInfo QFileSystemModel::fileInfo(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - return d->node(index)->fileInfo(); -} - -/*! - \fn void QFileSystemModel::rootPathChanged(const QString &newPath); - - This signal is emitted whenever the root path has been changed to a \a newPath. -*/ - -/*! - \fn void QFileSystemModel::fileRenamed(const QString &path, const QString &oldName, const QString &newName) - - This signal is emitted whenever a file with the \a oldName is successfully - renamed to \a newName. The file is located in in the directory \a path. -*/ - -/*! - \since 4.7 - \fn void QFileSystemModel::directoryLoaded(const QString &path) - - This signal is emitted when the gatherer thread has finished to load the \a path. - -*/ - -/*! - \fn bool QFileSystemModel::remove(const QModelIndex &index) - - Removes the model item \a index from the file system model and \b{deletes the - corresponding file from the file system}, returning true if successful. If the - item cannot be removed, false is returned. - - \warning This function deletes files from the file system; it does \b{not} - move them to a location where they can be recovered. - - \sa rmdir() -*/ - -bool QFileSystemModel::remove(const QModelIndex &aindex) -{ - Q_D(QFileSystemModel); - - const QString path = d->filePath(aindex); - const QFileInfo fileInfo(path); -#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) - // QTBUG-65683: Remove file system watchers prior to deletion to prevent - // failure due to locked files on Windows. - const QStringList watchedPaths = d->unwatchPathsAt(aindex); -#endif // filesystemwatcher && Q_OS_WIN - const bool success = (fileInfo.isFile() || fileInfo.isSymLink()) - ? QFile::remove(path) : QDir(path).removeRecursively(); -#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) - if (!success) - d->watchPaths(watchedPaths); -#endif // filesystemwatcher && Q_OS_WIN - return success; -} - -/*! - Constructs a file system model with the given \a parent. -*/ -QFileSystemModel::QFileSystemModel(QObject *parent) : - QFileSystemModel(*new QFileSystemModelPrivate, parent) -{ -} - -/*! - \internal -*/ -QFileSystemModel::QFileSystemModel(QFileSystemModelPrivate &dd, QObject *parent) - : QAbstractItemModel(dd, parent) -{ - Q_D(QFileSystemModel); - d->init(); -} - -/*! - Destroys this file system model. -*/ -QFileSystemModel::~QFileSystemModel() = default; - -/*! - \reimp -*/ -QModelIndex QFileSystemModel::index(int row, int column, const QModelIndex &parent) const -{ - Q_D(const QFileSystemModel); - if (row < 0 || column < 0 || row >= rowCount(parent) || column >= columnCount(parent)) - return QModelIndex(); - - // get the parent node - QFileSystemModelPrivate::QFileSystemNode *parentNode = (d->indexValid(parent) ? d->node(parent) : - const_cast(&d->root)); - Q_ASSERT(parentNode); - - // now get the internal pointer for the index - const int i = d->translateVisibleLocation(parentNode, row); - if (i >= parentNode->visibleChildren.size()) - return QModelIndex(); - const QString &childName = parentNode->visibleChildren.at(i); - const QFileSystemModelPrivate::QFileSystemNode *indexNode = parentNode->children.value(childName); - Q_ASSERT(indexNode); - - return createIndex(row, column, const_cast(indexNode)); -} - -/*! - \reimp -*/ -QModelIndex QFileSystemModel::sibling(int row, int column, const QModelIndex &idx) const -{ - if (row == idx.row() && column < QFileSystemModelPrivate::NumColumns) { - // cheap sibling operation: just adjust the column: - return createIndex(row, column, idx.internalPointer()); - } else { - // for anything else: call the default implementation - // (this could probably be optimized, too): - return QAbstractItemModel::sibling(row, column, idx); - } -} - -/*! - \overload - - Returns the model item index for the given \a path and \a column. -*/ -QModelIndex QFileSystemModel::index(const QString &path, int column) const -{ - Q_D(const QFileSystemModel); - QFileSystemModelPrivate::QFileSystemNode *node = d->node(path, false); - return d->index(node, column); -} - -/*! - \internal - - Return the QFileSystemNode that goes to index. - */ -QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QModelIndex &index) const -{ - if (!index.isValid()) - return const_cast(&root); - QFileSystemModelPrivate::QFileSystemNode *indexNode = static_cast(index.internalPointer()); - Q_ASSERT(indexNode); - return indexNode; -} - -#ifdef Q_OS_WIN32 -static QString qt_GetLongPathName(const QString &strShortPath) -{ - if (strShortPath.isEmpty() - || strShortPath == QLatin1String(".") || strShortPath == QLatin1String("..")) - return strShortPath; - if (strShortPath.length() == 2 && strShortPath.endsWith(QLatin1Char(':'))) - return strShortPath.toUpper(); - const QString absPath = QDir(strShortPath).absolutePath(); - if (absPath.startsWith(QLatin1String("//")) - || absPath.startsWith(QLatin1String("\\\\"))) // unc - return QDir::fromNativeSeparators(absPath); - if (absPath.startsWith(QLatin1Char('/'))) - return QString(); - const QString inputString = QLatin1String("\\\\?\\") + QDir::toNativeSeparators(absPath); - QVarLengthArray buffer(MAX_PATH); - DWORD result = ::GetLongPathName((wchar_t*)inputString.utf16(), - buffer.data(), - buffer.size()); - if (result > DWORD(buffer.size())) { - buffer.resize(result); - result = ::GetLongPathName((wchar_t*)inputString.utf16(), - buffer.data(), - buffer.size()); - } - if (result > 4) { - QString longPath = QString::fromWCharArray(buffer.data() + 4); // ignoring prefix - longPath[0] = longPath.at(0).toUpper(); // capital drive letters - return QDir::fromNativeSeparators(longPath); - } else { - return QDir::fromNativeSeparators(strShortPath); - } -} -#endif - -/*! - \internal - - Given a path return the matching QFileSystemNode or &root if invalid -*/ -QFileSystemModelPrivate::QFileSystemNode *QFileSystemModelPrivate::node(const QString &path, bool fetch) const -{ - Q_Q(const QFileSystemModel); - Q_UNUSED(q); - if (path.isEmpty() || path == myComputer() || path.startsWith(QLatin1Char(':'))) - return const_cast(&root); - - // Construct the nodes up to the new root path if they need to be built - QString absolutePath; -#ifdef Q_OS_WIN32 - QString longPath = qt_GetLongPathName(path); -#else - QString longPath = path; -#endif - if (longPath == rootDir.path()) - absolutePath = rootDir.absolutePath(); - else - absolutePath = QDir(longPath).absolutePath(); - - // ### TODO can we use bool QAbstractFileEngine::caseSensitive() const? - QStringList pathElements = absolutePath.split(QLatin1Char('/'), Qt::SkipEmptyParts); - if ((pathElements.isEmpty()) -#if !defined(Q_OS_WIN) - && QDir::fromNativeSeparators(longPath) != QLatin1String("/") -#endif - ) - return const_cast(&root); - QModelIndex index = QModelIndex(); // start with "My Computer" - QString elementPath; - QChar separator = QLatin1Char('/'); - QString trailingSeparator; -#if defined(Q_OS_WIN) - if (absolutePath.startsWith(QLatin1String("//"))) { // UNC path - QString host = QLatin1String("\\\\") + pathElements.constFirst(); - if (absolutePath == QDir::fromNativeSeparators(host)) - absolutePath.append(QLatin1Char('/')); - if (longPath.endsWith(QLatin1Char('/')) && !absolutePath.endsWith(QLatin1Char('/'))) - absolutePath.append(QLatin1Char('/')); - if (absolutePath.endsWith(QLatin1Char('/'))) - trailingSeparator = QLatin1String("\\"); - int r = 0; - QFileSystemModelPrivate::QFileSystemNode *rootNode = const_cast(&root); - if (!root.children.contains(host.toLower())) { - if (pathElements.count() == 1 && !absolutePath.endsWith(QLatin1Char('/'))) - return rootNode; - QFileInfo info(host); - if (!info.exists()) - return rootNode; - QFileSystemModelPrivate *p = const_cast(this); - p->addNode(rootNode, host,info); - p->addVisibleFiles(rootNode, QStringList(host)); - } - r = rootNode->visibleLocation(host); - r = translateVisibleLocation(rootNode, r); - index = q->index(r, 0, QModelIndex()); - pathElements.pop_front(); - separator = QLatin1Char('\\'); - elementPath = host; - elementPath.append(separator); - } else { - if (!pathElements.at(0).contains(QLatin1Char(':'))) { - QString rootPath = QDir(longPath).rootPath(); - pathElements.prepend(rootPath); - } - if (pathElements.at(0).endsWith(QLatin1Char('/'))) - pathElements[0].chop(1); - } -#else - // add the "/" item, since it is a valid path element on Unix - if (absolutePath[0] == QLatin1Char('/')) - pathElements.prepend(QLatin1String("/")); -#endif - - QFileSystemModelPrivate::QFileSystemNode *parent = node(index); - - for (int i = 0; i < pathElements.count(); ++i) { - QString element = pathElements.at(i); - if (i != 0) - elementPath.append(separator); - elementPath.append(element); - if (i == pathElements.count() - 1) - elementPath.append(trailingSeparator); -#ifdef Q_OS_WIN - // On Windows, "filename " and "filename" are equivalent and - // "filename . " and "filename" are equivalent - // "filename......." and "filename" are equivalent Task #133928 - // whereas "filename .txt" is still "filename .txt" - // If after stripping the characters there is nothing left then we - // just return the parent directory as it is assumed that the path - // is referring to the parent - while (element.endsWith(QLatin1Char('.')) || element.endsWith(QLatin1Char(' '))) - element.chop(1); - // Only filenames that can't possibly exist will be end up being empty - if (element.isEmpty()) - return parent; -#endif - bool alreadyExisted = parent->children.contains(element); - - // we couldn't find the path element, we create a new node since we - // _know_ that the path is valid - if (alreadyExisted) { - if ((parent->children.count() == 0) - || (parent->caseSensitive() - && parent->children.value(element)->fileName != element) - || (!parent->caseSensitive() - && parent->children.value(element)->fileName.toLower() != element.toLower())) - alreadyExisted = false; - } - - QFileSystemModelPrivate::QFileSystemNode *node; - if (!alreadyExisted) { - // Someone might call ::index("file://cookie/monster/doesn't/like/veggies"), - // a path that doesn't exists, I.E. don't blindly create directories. - QFileInfo info(elementPath); - if (!info.exists()) - return const_cast(&root); - QFileSystemModelPrivate *p = const_cast(this); - node = p->addNode(parent, element,info); -#if QT_CONFIG(filesystemwatcher) - node->populate(fileInfoGatherer.getInfo(info)); -#endif - } else { - node = parent->children.value(element); - } - - Q_ASSERT(node); - if (!node->isVisible) { - // It has been filtered out - if (alreadyExisted && node->hasInformation() && !fetch) - return const_cast(&root); - - QFileSystemModelPrivate *p = const_cast(this); - p->addVisibleFiles(parent, QStringList(element)); - if (!p->bypassFilters.contains(node)) - p->bypassFilters[node] = 1; - QString dir = q->filePath(this->index(parent)); - if (!node->hasInformation() && fetch) { - Fetching f = { std::move(dir), std::move(element), node }; - p->toFetch.append(std::move(f)); - p->fetchingTimer.start(0, const_cast(q)); - } - } - parent = node; - } - - return parent; -} - -/*! - \reimp -*/ -void QFileSystemModel::timerEvent(QTimerEvent *event) -{ - Q_D(QFileSystemModel); - if (event->timerId() == d->fetchingTimer.timerId()) { - d->fetchingTimer.stop(); -#if QT_CONFIG(filesystemwatcher) - for (int i = 0; i < d->toFetch.count(); ++i) { - const QFileSystemModelPrivate::QFileSystemNode *node = d->toFetch.at(i).node; - if (!node->hasInformation()) { - d->fileInfoGatherer.fetchExtendedInformation(d->toFetch.at(i).dir, - QStringList(d->toFetch.at(i).file)); - } else { - // qDebug("yah!, you saved a little gerbil soul"); - } - } -#endif - d->toFetch.clear(); - } -} - -/*! - Returns \c true if the model item \a index represents a directory; - otherwise returns \c false. -*/ -bool QFileSystemModel::isDir(const QModelIndex &index) const -{ - // This function is for public usage only because it could create a file info - Q_D(const QFileSystemModel); - if (!index.isValid()) - return true; - QFileSystemModelPrivate::QFileSystemNode *n = d->node(index); - if (n->hasInformation()) - return n->isDir(); - return fileInfo(index).isDir(); -} - -/*! - Returns the size in bytes of \a index. If the file does not exist, 0 is returned. - */ -qint64 QFileSystemModel::size(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - if (!index.isValid()) - return 0; - return d->node(index)->size(); -} - -/*! - Returns the type of file \a index such as "Directory" or "JPEG file". - */ -QString QFileSystemModel::type(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - if (!index.isValid()) - return QString(); - return d->node(index)->type(); -} - -/*! - Returns the date and time when \a index was last modified. - */ -QDateTime QFileSystemModel::lastModified(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - if (!index.isValid()) - return QDateTime(); - return d->node(index)->lastModified(); -} - -/*! - \reimp -*/ -QModelIndex QFileSystemModel::parent(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - if (!d->indexValid(index)) - return QModelIndex(); - - QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(index); - Q_ASSERT(indexNode != nullptr); - QFileSystemModelPrivate::QFileSystemNode *parentNode = indexNode->parent; - if (parentNode == nullptr || parentNode == &d->root) - return QModelIndex(); - - // get the parent's row - QFileSystemModelPrivate::QFileSystemNode *grandParentNode = parentNode->parent; - Q_ASSERT(grandParentNode->children.contains(parentNode->fileName)); - int visualRow = d->translateVisibleLocation(grandParentNode, grandParentNode->visibleLocation(grandParentNode->children.value(parentNode->fileName)->fileName)); - if (visualRow == -1) - return QModelIndex(); - return createIndex(visualRow, 0, parentNode); -} - -/* - \internal - - return the index for node -*/ -QModelIndex QFileSystemModelPrivate::index(const QFileSystemModelPrivate::QFileSystemNode *node, int column) const -{ - Q_Q(const QFileSystemModel); - QFileSystemModelPrivate::QFileSystemNode *parentNode = (node ? node->parent : nullptr); - if (node == &root || !parentNode) - return QModelIndex(); - - // get the parent's row - Q_ASSERT(node); - if (!node->isVisible) - return QModelIndex(); - - int visualRow = translateVisibleLocation(parentNode, parentNode->visibleLocation(node->fileName)); - return q->createIndex(visualRow, column, const_cast(node)); -} - -/*! - \reimp -*/ -bool QFileSystemModel::hasChildren(const QModelIndex &parent) const -{ - Q_D(const QFileSystemModel); - if (parent.column() > 0) - return false; - - if (!parent.isValid()) // drives - return true; - - const QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(parent); - Q_ASSERT(indexNode); - return (indexNode->isDir()); -} - -/*! - \reimp - */ -bool QFileSystemModel::canFetchMore(const QModelIndex &parent) const -{ - Q_D(const QFileSystemModel); - const QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(parent); - return (!indexNode->populatedChildren); -} - -/*! - \reimp - */ -void QFileSystemModel::fetchMore(const QModelIndex &parent) -{ - Q_D(QFileSystemModel); - if (!d->setRootPath) - return; - QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(parent); - if (indexNode->populatedChildren) - return; - indexNode->populatedChildren = true; -#if QT_CONFIG(filesystemwatcher) - d->fileInfoGatherer.list(filePath(parent)); -#endif -} - -/*! - \reimp -*/ -int QFileSystemModel::rowCount(const QModelIndex &parent) const -{ - Q_D(const QFileSystemModel); - if (parent.column() > 0) - return 0; - - if (!parent.isValid()) - return d->root.visibleChildren.count(); - - const QFileSystemModelPrivate::QFileSystemNode *parentNode = d->node(parent); - return parentNode->visibleChildren.count(); -} - -/*! - \reimp -*/ -int QFileSystemModel::columnCount(const QModelIndex &parent) const -{ - return (parent.column() > 0) ? 0 : QFileSystemModelPrivate::NumColumns; -} - -/*! - Returns the data stored under the given \a role for the item "My Computer". - - \sa Qt::ItemDataRole - */ -QVariant QFileSystemModel::myComputer(int role) const -{ -#if QT_CONFIG(filesystemwatcher) - Q_D(const QFileSystemModel); -#endif - switch (role) { - case Qt::DisplayRole: - return QFileSystemModelPrivate::myComputer(); -#if QT_CONFIG(filesystemwatcher) - case Qt::DecorationRole: - return d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::Computer); -#endif - } - return QVariant(); -} - -/*! - \reimp -*/ -QVariant QFileSystemModel::data(const QModelIndex &index, int role) const -{ - Q_D(const QFileSystemModel); - if (!index.isValid() || index.model() != this) - return QVariant(); - - switch (role) { - case Qt::EditRole: - case Qt::DisplayRole: - switch (index.column()) { - case 0: return d->displayName(index); - case 1: return d->size(index); - case 2: return d->type(index); - case 3: return d->time(index); - default: - qWarning("data: invalid display value column %d", index.column()); - break; - } - break; - case FilePathRole: - return filePath(index); - case FileNameRole: - return d->name(index); - case Qt::DecorationRole: - if (index.column() == 0) { - QIcon icon = d->icon(index); -#if QT_CONFIG(filesystemwatcher) - if (icon.isNull()) { - if (d->node(index)->isDir()) - icon = d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::Folder); - else - icon = d->fileInfoGatherer.iconProvider()->icon(QFileIconProvider::File); - } -#endif // filesystemwatcher - return icon; - } - break; - case Qt::TextAlignmentRole: - if (index.column() == 1) - return QVariant(Qt::AlignTrailing | Qt::AlignVCenter); - break; - case FilePermissions: - int p = permissions(index); - return p; - } - - return QVariant(); -} - -/*! - \internal -*/ -QString QFileSystemModelPrivate::size(const QModelIndex &index) const -{ - if (!index.isValid()) - return QString(); - const QFileSystemNode *n = node(index); - if (n->isDir()) { -#ifdef Q_OS_MAC - return QLatin1String("--"); -#else - return QLatin1String(""); -#endif - // Windows - "" - // OS X - "--" - // Konqueror - "4 KB" - // Nautilus - "9 items" (the number of children) - } - return size(n->size()); -} - -QString QFileSystemModelPrivate::size(qint64 bytes) -{ - return QLocale::system().formattedDataSize(bytes); -} - -/*! - \internal -*/ -QString QFileSystemModelPrivate::time(const QModelIndex &index) const -{ - if (!index.isValid()) - return QString(); -#if QT_CONFIG(datestring) - return QLocale::system().toString(node(index)->lastModified(), QLocale::ShortFormat); -#else - Q_UNUSED(index); - return QString(); -#endif -} - -/* - \internal -*/ -QString QFileSystemModelPrivate::type(const QModelIndex &index) const -{ - if (!index.isValid()) - return QString(); - return node(index)->type(); -} - -/*! - \internal -*/ -QString QFileSystemModelPrivate::name(const QModelIndex &index) const -{ - if (!index.isValid()) - return QString(); - QFileSystemNode *dirNode = node(index); - if ( -#if QT_CONFIG(filesystemwatcher) - fileInfoGatherer.resolveSymlinks() && -#endif - !resolvedSymLinks.isEmpty() && dirNode->isSymLink(/* ignoreNtfsSymLinks = */ true)) { - QString fullPath = QDir::fromNativeSeparators(filePath(index)); - return resolvedSymLinks.value(fullPath, dirNode->fileName); - } - return dirNode->fileName; -} - -/*! - \internal -*/ -QString QFileSystemModelPrivate::displayName(const QModelIndex &index) const -{ -#if defined(Q_OS_WIN) - QFileSystemNode *dirNode = node(index); - if (!dirNode->volumeName.isEmpty()) - return dirNode->volumeName; -#endif - return name(index); -} - -/*! - \internal -*/ -QIcon QFileSystemModelPrivate::icon(const QModelIndex &index) const -{ - if (!index.isValid()) - return QIcon(); - return node(index)->icon(); -} - -/*! - \reimp -*/ -bool QFileSystemModel::setData(const QModelIndex &idx, const QVariant &value, int role) -{ - Q_D(QFileSystemModel); - if (!idx.isValid() - || idx.column() != 0 - || role != Qt::EditRole - || (flags(idx) & Qt::ItemIsEditable) == 0) { - return false; - } - - QString newName = value.toString(); - QString oldName = idx.data().toString(); - if (newName == oldName) - return true; - - const QString parentPath = filePath(parent(idx)); - - if (newName.isEmpty() || QDir::toNativeSeparators(newName).contains(QDir::separator())) - return false; - -#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) - // QTBUG-65683: Remove file system watchers prior to renaming to prevent - // failure due to locked files on Windows. - const QStringList watchedPaths = d->unwatchPathsAt(idx); -#endif // filesystemwatcher && Q_OS_WIN - if (!QDir(parentPath).rename(oldName, newName)) { -#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) - d->watchPaths(watchedPaths); -#endif - return false; - } else { - /* - *After re-naming something we don't want the selection to change* - - can't remove rows and later insert - - can't quickly remove and insert - - index pointer can't change because treeview doesn't use persistant index's - - - if this get any more complicated think of changing it to just - use layoutChanged - */ - - QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(idx); - QFileSystemModelPrivate::QFileSystemNode *parentNode = indexNode->parent; - int visibleLocation = parentNode->visibleLocation(parentNode->children.value(indexNode->fileName)->fileName); - - parentNode->visibleChildren.removeAt(visibleLocation); - QScopedPointer nodeToRename(parentNode->children.take(oldName)); - nodeToRename->fileName = newName; - nodeToRename->parent = parentNode; -#if QT_CONFIG(filesystemwatcher) - nodeToRename->populate(d->fileInfoGatherer.getInfo(QFileInfo(parentPath, newName))); -#endif - nodeToRename->isVisible = true; - parentNode->children[newName] = nodeToRename.take(); - parentNode->visibleChildren.insert(visibleLocation, newName); - - d->delayedSort(); - emit fileRenamed(parentPath, oldName, newName); - } - return true; -} - -/*! - \reimp -*/ -QVariant QFileSystemModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - switch (role) { - case Qt::DecorationRole: - if (section == 0) { - // ### TODO oh man this is ugly and doesn't even work all the way! - // it is still 2 pixels off - QImage pixmap(16, 1, QImage::Format_ARGB32_Premultiplied); - pixmap.fill(Qt::transparent); - return pixmap; - } - break; - case Qt::TextAlignmentRole: - return Qt::AlignLeft; - } - - if (orientation != Qt::Horizontal || role != Qt::DisplayRole) - return QAbstractItemModel::headerData(section, orientation, role); - - QString returnValue; - switch (section) { - case 0: returnValue = tr("Name"); - break; - case 1: returnValue = tr("Size"); - break; - case 2: returnValue = -#ifdef Q_OS_MAC - tr("Kind", "Match OS X Finder"); -#else - tr("Type", "All other platforms"); -#endif - break; - // Windows - Type - // OS X - Kind - // Konqueror - File Type - // Nautilus - Type - case 3: returnValue = tr("Date Modified"); - break; - default: return QVariant(); - } - return returnValue; -} - -/*! - \reimp -*/ -Qt::ItemFlags QFileSystemModel::flags(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - Qt::ItemFlags flags = QAbstractItemModel::flags(index); - if (!index.isValid()) - return flags; - - QFileSystemModelPrivate::QFileSystemNode *indexNode = d->node(index); - if (d->nameFilterDisables && !d->passNameFilters(indexNode)) { - flags &= ~Qt::ItemIsEnabled; - // ### TODO you shouldn't be able to set this as the current item, task 119433 - return flags; - } - - flags |= Qt::ItemIsDragEnabled; - if (d->readOnly) - return flags; - if ((index.column() == 0) && indexNode->permissions() & QFile::WriteUser) { - flags |= Qt::ItemIsEditable; - if (indexNode->isDir()) - flags |= Qt::ItemIsDropEnabled; - else - flags |= Qt::ItemNeverHasChildren; - } - return flags; -} - -/*! - \internal -*/ -void QFileSystemModelPrivate::_q_performDelayedSort() -{ - Q_Q(QFileSystemModel); - q->sort(sortColumn, sortOrder); -} - - -/* - \internal - Helper functor used by sort() -*/ -class QFileSystemModelSorter -{ -public: - inline QFileSystemModelSorter(int column) : sortColumn(column) - { - naturalCompare.setNumericMode(true); - naturalCompare.setCaseSensitivity(Qt::CaseInsensitive); - } - - bool compareNodes(const QFileSystemModelPrivate::QFileSystemNode *l, - const QFileSystemModelPrivate::QFileSystemNode *r) const - { - switch (sortColumn) { - case 0: { -#ifndef Q_OS_MAC - // place directories before files - bool left = l->isDir(); - bool right = r->isDir(); - if (left ^ right) - return left; -#endif - return naturalCompare.compare(l->fileName, r->fileName) < 0; - } - case 1: - { - // Directories go first - bool left = l->isDir(); - bool right = r->isDir(); - if (left ^ right) - return left; - - qint64 sizeDifference = l->size() - r->size(); - if (sizeDifference == 0) - return naturalCompare.compare(l->fileName, r->fileName) < 0; - - return sizeDifference < 0; - } - case 2: - { - int compare = naturalCompare.compare(l->type(), r->type()); - if (compare == 0) - return naturalCompare.compare(l->fileName, r->fileName) < 0; - - return compare < 0; - } - case 3: - { - if (l->lastModified() == r->lastModified()) - return naturalCompare.compare(l->fileName, r->fileName) < 0; - - return l->lastModified() < r->lastModified(); - } - } - Q_ASSERT(false); - return false; - } - - bool operator()(const QFileSystemModelPrivate::QFileSystemNode *l, - const QFileSystemModelPrivate::QFileSystemNode *r) const - { - return compareNodes(l, r); - } - - -private: - QCollator naturalCompare; - int sortColumn; -}; - -/* - \internal - - Sort all of the children of parent -*/ -void QFileSystemModelPrivate::sortChildren(int column, const QModelIndex &parent) -{ - Q_Q(QFileSystemModel); - QFileSystemModelPrivate::QFileSystemNode *indexNode = node(parent); - if (indexNode->children.count() == 0) - return; - - QList values; - - for (auto iterator = indexNode->children.constBegin(), cend = indexNode->children.constEnd(); iterator != cend; ++iterator) { - if (filtersAcceptsNode(iterator.value())) { - values.append(iterator.value()); - } else { - iterator.value()->isVisible = false; - } - } - QFileSystemModelSorter ms(column); - std::sort(values.begin(), values.end(), ms); - // First update the new visible list - indexNode->visibleChildren.clear(); - //No more dirty item we reset our internal dirty index - indexNode->dirtyChildrenIndex = -1; - const int numValues = values.count(); - indexNode->visibleChildren.reserve(numValues); - for (int i = 0; i < numValues; ++i) { - indexNode->visibleChildren.append(values.at(i)->fileName); - values.at(i)->isVisible = true; - } - - if (!disableRecursiveSort) { - for (int i = 0; i < q->rowCount(parent); ++i) { - const QModelIndex childIndex = q->index(i, 0, parent); - QFileSystemModelPrivate::QFileSystemNode *indexNode = node(childIndex); - //Only do a recursive sort on visible nodes - if (indexNode->isVisible) - sortChildren(column, childIndex); - } - } -} - -/*! - \reimp -*/ -void QFileSystemModel::sort(int column, Qt::SortOrder order) -{ - Q_D(QFileSystemModel); - if (d->sortOrder == order && d->sortColumn == column && !d->forceSort) - return; - - emit layoutAboutToBeChanged(); - QModelIndexList oldList = persistentIndexList(); - QList> oldNodes; - const int nodeCount = oldList.count(); - oldNodes.reserve(nodeCount); - for (int i = 0; i < nodeCount; ++i) { - const QModelIndex &oldNode = oldList.at(i); - QPair pair(d->node(oldNode), oldNode.column()); - oldNodes.append(pair); - } - - if (!(d->sortColumn == column && d->sortOrder != order && !d->forceSort)) { - //we sort only from where we are, don't need to sort all the model - d->sortChildren(column, index(rootPath())); - d->sortColumn = column; - d->forceSort = false; - } - d->sortOrder = order; - - QModelIndexList newList; - const int numOldNodes = oldNodes.size(); - newList.reserve(numOldNodes); - for (int i = 0; i < numOldNodes; ++i) { - const QPair &oldNode = oldNodes.at(i); - newList.append(d->index(oldNode.first, oldNode.second)); - } - changePersistentIndexList(oldList, newList); - emit layoutChanged(); -} - -/*! - Returns a list of MIME types that can be used to describe a list of items - in the model. -*/ -QStringList QFileSystemModel::mimeTypes() const -{ - return QStringList(QLatin1String("text/uri-list")); -} - -/*! - Returns an object that contains a serialized description of the specified - \a indexes. The format used to describe the items corresponding to the - indexes is obtained from the mimeTypes() function. - - If the list of indexes is empty, \nullptr is returned rather than a - serialized empty list. -*/ -QMimeData *QFileSystemModel::mimeData(const QModelIndexList &indexes) const -{ - QList urls; - QList::const_iterator it = indexes.begin(); - for (; it != indexes.end(); ++it) - if ((*it).column() == 0) - urls << QUrl::fromLocalFile(filePath(*it)); - QMimeData *data = new QMimeData(); - data->setUrls(urls); - return data; -} - -/*! - Handles the \a data supplied by a drag and drop operation that ended with - the given \a action over the row in the model specified by the \a row and - \a column and by the \a parent index. Returns true if the operation was - successful. - - \sa supportedDropActions() -*/ -bool QFileSystemModel::dropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) -{ - Q_UNUSED(row); - Q_UNUSED(column); - if (!parent.isValid() || isReadOnly()) - return false; - - bool success = true; - QString to = filePath(parent) + QDir::separator(); - - QList urls = data->urls(); - QList::const_iterator it = urls.constBegin(); - - switch (action) { - case Qt::CopyAction: - for (; it != urls.constEnd(); ++it) { - QString path = (*it).toLocalFile(); - success = QFile::copy(path, to + QFileInfo(path).fileName()) && success; - } - break; - case Qt::LinkAction: - for (; it != urls.constEnd(); ++it) { - QString path = (*it).toLocalFile(); - success = QFile::link(path, to + QFileInfo(path).fileName()) && success; - } - break; - case Qt::MoveAction: - for (; it != urls.constEnd(); ++it) { - QString path = (*it).toLocalFile(); - success = QFile::rename(path, to + QFileInfo(path).fileName()) && success; - } - break; - default: - return false; - } - - return success; -} - -/*! - \reimp -*/ -Qt::DropActions QFileSystemModel::supportedDropActions() const -{ - return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction; -} - -/*! - \reimp -*/ -QHash QFileSystemModel::roleNames() const -{ - auto ret = QAbstractItemModel::roleNames(); - ret.insert(QFileSystemModel::FileIconRole, - QByteArrayLiteral("fileIcon")); // == Qt::decoration - ret.insert(QFileSystemModel::FilePathRole, QByteArrayLiteral("filePath")); - ret.insert(QFileSystemModel::FileNameRole, QByteArrayLiteral("fileName")); - ret.insert(QFileSystemModel::FilePermissions, QByteArrayLiteral("filePermissions")); - return ret; -} - -/*! - \enum QFileSystemModel::Option - \since 5.14 - - \value DontWatchForChanges Do not add file watchers to the paths. - This reduces overhead when using the model for simple tasks - like line edit completion. - - \value DontResolveSymlinks Don't resolve symlinks in the file - system model. By default, symlinks are resolved. - - \value DontUseCustomDirectoryIcons Always use the default directory icon. - Some platforms allow the user to set a different icon. Custom icon lookup - causes a big performance impact over network or removable drives. - This sets the QFileIconProvider::DontUseCustomDirectoryIcons - option in the icon provider accordingly. - - \sa resolveSymlinks -*/ - -/*! - \since 5.14 - Sets the given \a option to be enabled if \a on is true; otherwise, - clears the given \a option. - - Options should be set before changing properties. - - \sa options, testOption() -*/ -void QFileSystemModel::setOption(Option option, bool on) -{ - QFileSystemModel::Options previousOptions = options(); - setOptions(previousOptions.setFlag(option, on)); -} - -/*! - \since 5.14 - - Returns \c true if the given \a option is enabled; otherwise, returns - false. - - \sa options, setOption() -*/ -bool QFileSystemModel::testOption(Option option) const -{ - return options().testFlag(option); -} - -/*! - \property QFileSystemModel::options - \brief the various options that affect the model - \since 5.14 - - By default, all options are disabled. - - Options should be set before changing properties. - - \sa setOption(), testOption() -*/ -void QFileSystemModel::setOptions(Options options) -{ - const Options changed = (options ^ QFileSystemModel::options()); - - if (changed.testFlag(DontResolveSymlinks)) - setResolveSymlinks(!options.testFlag(DontResolveSymlinks)); - -#if QT_CONFIG(filesystemwatcher) - Q_D(QFileSystemModel); - if (changed.testFlag(DontWatchForChanges)) - d->fileInfoGatherer.setWatching(!options.testFlag(DontWatchForChanges)); -#endif - - if (changed.testFlag(DontUseCustomDirectoryIcons)) { - if (auto provider = iconProvider()) { - QFileIconProvider::Options providerOptions = provider->options(); - providerOptions.setFlag(QFileIconProvider::DontUseCustomDirectoryIcons, - options.testFlag(QFileSystemModel::DontUseCustomDirectoryIcons)); - provider->setOptions(providerOptions); - } else { - qWarning("Setting QFileSystemModel::DontUseCustomDirectoryIcons has no effect when no provider is used"); - } - } -} - -QFileSystemModel::Options QFileSystemModel::options() const -{ - QFileSystemModel::Options result; - result.setFlag(DontResolveSymlinks, !resolveSymlinks()); -#if QT_CONFIG(filesystemwatcher) - Q_D(const QFileSystemModel); - result.setFlag(DontWatchForChanges, !d->fileInfoGatherer.isWatching()); -#else - result.setFlag(DontWatchForChanges); -#endif - if (auto provider = iconProvider()) { - result.setFlag(DontUseCustomDirectoryIcons, - provider->options().testFlag(QFileIconProvider::DontUseCustomDirectoryIcons)); - } - return result; -} - -/*! - Returns the path of the item stored in the model under the - \a index given. -*/ -QString QFileSystemModel::filePath(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - QString fullPath = d->filePath(index); - QFileSystemModelPrivate::QFileSystemNode *dirNode = d->node(index); - if (dirNode->isSymLink() -#if QT_CONFIG(filesystemwatcher) - && d->fileInfoGatherer.resolveSymlinks() -#endif - && d->resolvedSymLinks.contains(fullPath) - && dirNode->isDir()) { - QFileInfo fullPathInfo(dirNode->fileInfo()); - if (!dirNode->hasInformation()) - fullPathInfo = QFileInfo(fullPath); - QString canonicalPath = fullPathInfo.canonicalFilePath(); - auto *canonicalNode = d->node(fullPathInfo.canonicalFilePath(), false); - QFileInfo resolvedInfo = canonicalNode->fileInfo(); - if (!canonicalNode->hasInformation()) - resolvedInfo = QFileInfo(canonicalPath); - if (resolvedInfo.exists()) - return resolvedInfo.filePath(); - } - return fullPath; -} - -QString QFileSystemModelPrivate::filePath(const QModelIndex &index) const -{ - Q_Q(const QFileSystemModel); - Q_UNUSED(q); - if (!index.isValid()) - return QString(); - Q_ASSERT(index.model() == q); - - QStringList path; - QModelIndex idx = index; - while (idx.isValid()) { - QFileSystemModelPrivate::QFileSystemNode *dirNode = node(idx); - if (dirNode) - path.prepend(dirNode->fileName); - idx = idx.parent(); - } - QString fullPath = QDir::fromNativeSeparators(path.join(QDir::separator())); -#if !defined(Q_OS_WIN) - if ((fullPath.length() > 2) && fullPath[0] == QLatin1Char('/') && fullPath[1] == QLatin1Char('/')) - fullPath = fullPath.mid(1); -#else - if (fullPath.length() == 2 && fullPath.endsWith(QLatin1Char(':'))) - fullPath.append(QLatin1Char('/')); -#endif - return fullPath; -} - -/*! - Create a directory with the \a name in the \a parent model index. -*/ -QModelIndex QFileSystemModel::mkdir(const QModelIndex &parent, const QString &name) -{ - Q_D(QFileSystemModel); - if (!parent.isValid()) - return parent; - - QDir dir(filePath(parent)); - if (!dir.mkdir(name)) - return QModelIndex(); - QFileSystemModelPrivate::QFileSystemNode *parentNode = d->node(parent); - d->addNode(parentNode, name, QFileInfo()); - Q_ASSERT(parentNode->children.contains(name)); - QFileSystemModelPrivate::QFileSystemNode *node = parentNode->children[name]; -#if QT_CONFIG(filesystemwatcher) - node->populate(d->fileInfoGatherer.getInfo(QFileInfo(dir.absolutePath() + QDir::separator() + name))); -#endif - d->addVisibleFiles(parentNode, QStringList(name)); - return d->index(node); -} - -/*! - Returns the complete OR-ed together combination of QFile::Permission for the \a index. - */ -QFile::Permissions QFileSystemModel::permissions(const QModelIndex &index) const -{ - Q_D(const QFileSystemModel); - return d->node(index)->permissions(); -} - -/*! - Sets the directory that is being watched by the model to \a newPath by - installing a \l{QFileSystemWatcher}{file system watcher} on it. Any - changes to files and directories within this directory will be - reflected in the model. - - If the path is changed, the rootPathChanged() signal will be emitted. - - \note This function does not change the structure of the model or - modify the data available to views. In other words, the "root" of - the model is \e not changed to include only files and directories - within the directory specified by \a newPath in the file system. - */ -QModelIndex QFileSystemModel::setRootPath(const QString &newPath) -{ - Q_D(QFileSystemModel); -#ifdef Q_OS_WIN -#ifdef Q_OS_WIN32 - QString longNewPath = qt_GetLongPathName(newPath); -#else - QString longNewPath = QDir::fromNativeSeparators(newPath); -#endif -#else - QString longNewPath = newPath; -#endif - //we remove .. and . from the given path if exist - if (!newPath.isEmpty()) - longNewPath = QDir::cleanPath(longNewPath); - - d->setRootPath = true; - - //user don't ask for the root path ("") but the conversion failed - if (!newPath.isEmpty() && longNewPath.isEmpty()) - return d->index(rootPath()); - - if (d->rootDir.path() == longNewPath) - return d->index(rootPath()); - - auto node = d->node(longNewPath); - QFileInfo newPathInfo; - if (node && node->hasInformation()) - newPathInfo = node->fileInfo(); - else - newPathInfo = QFileInfo(longNewPath); - - bool showDrives = (longNewPath.isEmpty() || longNewPath == QFileSystemModelPrivate::myComputer()); - if (!showDrives && !newPathInfo.exists()) - return d->index(rootPath()); - - //We remove the watcher on the previous path - if (!rootPath().isEmpty() && rootPath() != QLatin1String(".")) { - //This remove the watcher for the old rootPath -#if QT_CONFIG(filesystemwatcher) - d->fileInfoGatherer.removePath(rootPath()); -#endif - //This line "marks" the node as dirty, so the next fetchMore - //call on the path will ask the gatherer to install a watcher again - //But it doesn't re-fetch everything - d->node(rootPath())->populatedChildren = false; - } - - // We have a new valid root path - d->rootDir = QDir(longNewPath); - QModelIndex newRootIndex; - if (showDrives) { - // otherwise dir will become '.' - d->rootDir.setPath(QLatin1String("")); - } else { - newRootIndex = d->index(d->rootDir.path()); - } - fetchMore(newRootIndex); - emit rootPathChanged(longNewPath); - d->forceSort = true; - d->delayedSort(); - return newRootIndex; -} - -/*! - The currently set root path - - \sa rootDirectory() -*/ -QString QFileSystemModel::rootPath() const -{ - Q_D(const QFileSystemModel); - return d->rootDir.path(); -} - -/*! - The currently set directory - - \sa rootPath() -*/ -QDir QFileSystemModel::rootDirectory() const -{ - Q_D(const QFileSystemModel); - QDir dir(d->rootDir); - dir.setNameFilters(nameFilters()); - dir.setFilter(filter()); - return dir; -} - -/*! - Sets the \a provider of file icons for the directory model. -*/ -void QFileSystemModel::setIconProvider(QFileIconProvider *provider) -{ - Q_D(QFileSystemModel); -#if QT_CONFIG(filesystemwatcher) - d->fileInfoGatherer.setIconProvider(provider); -#endif - d->root.updateIcon(provider, QString()); -} - -/*! - Returns the file icon provider for this directory model. -*/ -QFileIconProvider *QFileSystemModel::iconProvider() const -{ -#if QT_CONFIG(filesystemwatcher) - Q_D(const QFileSystemModel); - return d->fileInfoGatherer.iconProvider(); -#else - return 0; -#endif -} - -/*! - Sets the directory model's filter to that specified by \a filters. - - Note that the filter you set should always include the QDir::AllDirs enum value, - otherwise QFileSystemModel won't be able to read the directory structure. - - \sa QDir::Filters -*/ -void QFileSystemModel::setFilter(QDir::Filters filters) -{ - Q_D(QFileSystemModel); - if (d->filters == filters) - return; - d->filters = filters; - // CaseSensitivity might have changed - setNameFilters(nameFilters()); - d->forceSort = true; - d->delayedSort(); -} - -/*! - Returns the filter specified for the directory model. - - If a filter has not been set, the default filter is QDir::AllEntries | - QDir::NoDotAndDotDot | QDir::AllDirs. - - \sa QDir::Filters -*/ -QDir::Filters QFileSystemModel::filter() const -{ - Q_D(const QFileSystemModel); - return d->filters; -} - -/*! - \property QFileSystemModel::resolveSymlinks - \brief Whether the directory model should resolve symbolic links - - This is only relevant on Windows. - - By default, this property is \c true. - - \sa QFileSystemModel::Options -*/ -void QFileSystemModel::setResolveSymlinks(bool enable) -{ -#if QT_CONFIG(filesystemwatcher) - Q_D(QFileSystemModel); - d->fileInfoGatherer.setResolveSymlinks(enable); -#else - Q_UNUSED(enable); -#endif -} - -bool QFileSystemModel::resolveSymlinks() const -{ -#if QT_CONFIG(filesystemwatcher) - Q_D(const QFileSystemModel); - return d->fileInfoGatherer.resolveSymlinks(); -#else - return false; -#endif -} - -/*! - \property QFileSystemModel::readOnly - \brief Whether the directory model allows writing to the file system - - If this property is set to false, the directory model will allow renaming, copying - and deleting of files and directories. - - This property is \c true by default -*/ -void QFileSystemModel::setReadOnly(bool enable) -{ - Q_D(QFileSystemModel); - d->readOnly = enable; -} - -bool QFileSystemModel::isReadOnly() const -{ - Q_D(const QFileSystemModel); - return d->readOnly; -} - -/*! - \property QFileSystemModel::nameFilterDisables - \brief Whether files that don't pass the name filter are hidden or disabled - - This property is \c true by default -*/ -void QFileSystemModel::setNameFilterDisables(bool enable) -{ - Q_D(QFileSystemModel); - if (d->nameFilterDisables == enable) - return; - d->nameFilterDisables = enable; - d->forceSort = true; - d->delayedSort(); -} - -bool QFileSystemModel::nameFilterDisables() const -{ - Q_D(const QFileSystemModel); - return d->nameFilterDisables; -} - -/*! - Sets the name \a filters to apply against the existing files. -*/ -void QFileSystemModel::setNameFilters(const QStringList &filters) -{ - // Prep the regexp's ahead of time -#if QT_CONFIG(regularexpression) - Q_D(QFileSystemModel); - - if (!d->bypassFilters.isEmpty()) { - // update the bypass filter to only bypass the stuff that must be kept around - d->bypassFilters.clear(); - // We guarantee that rootPath will stick around - QPersistentModelIndex root(index(rootPath())); - const QModelIndexList persistentList = persistentIndexList(); - for (const auto &persistentIndex : persistentList) { - QFileSystemModelPrivate::QFileSystemNode *node = d->node(persistentIndex); - while (node) { - if (d->bypassFilters.contains(node)) - break; - if (node->isDir()) - d->bypassFilters[node] = true; - node = node->parent; - } - } - } - - d->nameFilters = filters; - d->forceSort = true; - d->delayedSort(); -#else - Q_UNUSED(filters); -#endif -} - -/*! - Returns a list of filters applied to the names in the model. -*/ -QStringList QFileSystemModel::nameFilters() const -{ -#if QT_CONFIG(regularexpression) - Q_D(const QFileSystemModel); - return d->nameFilters; -#else - return QStringList(); -#endif -} - -/*! - \reimp -*/ -bool QFileSystemModel::event(QEvent *event) -{ -#if QT_CONFIG(filesystemwatcher) - Q_D(QFileSystemModel); - if (event->type() == QEvent::LanguageChange) { - d->root.retranslateStrings(d->fileInfoGatherer.iconProvider(), QString()); - return true; - } -#endif - return QAbstractItemModel::event(event); -} - -bool QFileSystemModel::rmdir(const QModelIndex &aindex) -{ - QString path = filePath(aindex); - const bool success = QDir().rmdir(path); -#if QT_CONFIG(filesystemwatcher) - if (success) { - QFileSystemModelPrivate * d = const_cast(d_func()); - d->fileInfoGatherer.removePath(path); - } -#endif - return success; -} - -/*! - \internal - - Performed quick listing and see if any files have been added or removed, - then fetch more information on visible files. - */ -void QFileSystemModelPrivate::_q_directoryChanged(const QString &directory, const QStringList &files) -{ - QFileSystemModelPrivate::QFileSystemNode *parentNode = node(directory, false); - if (parentNode->children.count() == 0) - return; - QStringList toRemove; - QStringList newFiles = files; - std::sort(newFiles.begin(), newFiles.end()); - for (auto i = parentNode->children.constBegin(), cend = parentNode->children.constEnd(); i != cend; ++i) { - QStringList::iterator iterator = std::lower_bound(newFiles.begin(), newFiles.end(), i.value()->fileName); - if ((iterator == newFiles.end()) || (i.value()->fileName < *iterator)) - toRemove.append(i.value()->fileName); - } - for (int i = 0 ; i < toRemove.count() ; ++i ) - removeNode(parentNode, toRemove[i]); -} - -#if defined(Q_OS_WIN) -static QString volumeName(const QString &path) -{ - IShellItem *item = nullptr; - const QString native = QDir::toNativeSeparators(path); - HRESULT hr = SHCreateItemFromParsingName(reinterpret_cast(native.utf16()), - nullptr, IID_IShellItem, - reinterpret_cast(&item)); - if (FAILED(hr)) - return QString(); - LPWSTR name = nullptr; - hr = item->GetDisplayName(SIGDN_NORMALDISPLAY, &name); - if (FAILED(hr)) - return QString(); - QString result = QString::fromWCharArray(name); - CoTaskMemFree(name); - item->Release(); - return result; -} -#endif // Q_OS_WIN - -/*! - \internal - - Adds a new file to the children of parentNode - - *WARNING* this will change the count of children -*/ -QFileSystemModelPrivate::QFileSystemNode* QFileSystemModelPrivate::addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo& info) -{ - // In the common case, itemLocation == count() so check there first - QFileSystemModelPrivate::QFileSystemNode *node = new QFileSystemModelPrivate::QFileSystemNode(fileName, parentNode); -#if QT_CONFIG(filesystemwatcher) - node->populate(info); -#else - Q_UNUSED(info); -#endif -#if defined(Q_OS_WIN) - //The parentNode is "" so we are listing the drives - if (parentNode->fileName.isEmpty()) - node->volumeName = volumeName(fileName); -#endif - Q_ASSERT(!parentNode->children.contains(fileName)); - parentNode->children.insert(fileName, node); - return node; -} - -/*! - \internal - - File at parentNode->children(itemLocation) has been removed, remove from the lists - and emit signals if necessary - - *WARNING* this will change the count of children and could change visibleChildren - */ -void QFileSystemModelPrivate::removeNode(QFileSystemModelPrivate::QFileSystemNode *parentNode, const QString& name) -{ - Q_Q(QFileSystemModel); - QModelIndex parent = index(parentNode); - bool indexHidden = isHiddenByFilter(parentNode, parent); - - int vLocation = parentNode->visibleLocation(name); - if (vLocation >= 0 && !indexHidden) - q->beginRemoveRows(parent, translateVisibleLocation(parentNode, vLocation), - translateVisibleLocation(parentNode, vLocation)); - QFileSystemNode * node = parentNode->children.take(name); - delete node; - // cleanup sort files after removing rather then re-sorting which is O(n) - if (vLocation >= 0) - parentNode->visibleChildren.removeAt(vLocation); - if (vLocation >= 0 && !indexHidden) - q->endRemoveRows(); -} - -/*! - \internal - - File at parentNode->children(itemLocation) was not visible before, but now should be - and emit signals if necessary. - - *WARNING* this will change the visible count - */ -void QFileSystemModelPrivate::addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles) -{ - Q_Q(QFileSystemModel); - QModelIndex parent = index(parentNode); - bool indexHidden = isHiddenByFilter(parentNode, parent); - if (!indexHidden) { - q->beginInsertRows(parent, parentNode->visibleChildren.count() , parentNode->visibleChildren.count() + newFiles.count() - 1); - } - - if (parentNode->dirtyChildrenIndex == -1) - parentNode->dirtyChildrenIndex = parentNode->visibleChildren.count(); - - for (const auto &newFile : newFiles) { - parentNode->visibleChildren.append(newFile); - parentNode->children.value(newFile)->isVisible = true; - } - if (!indexHidden) - q->endInsertRows(); -} - -/*! - \internal - - File was visible before, but now should NOT be - - *WARNING* this will change the visible count - */ -void QFileSystemModelPrivate::removeVisibleFile(QFileSystemNode *parentNode, int vLocation) -{ - Q_Q(QFileSystemModel); - if (vLocation == -1) - return; - QModelIndex parent = index(parentNode); - bool indexHidden = isHiddenByFilter(parentNode, parent); - if (!indexHidden) - q->beginRemoveRows(parent, translateVisibleLocation(parentNode, vLocation), - translateVisibleLocation(parentNode, vLocation)); - parentNode->children.value(parentNode->visibleChildren.at(vLocation))->isVisible = false; - parentNode->visibleChildren.removeAt(vLocation); - if (!indexHidden) - q->endRemoveRows(); -} - -/*! - \internal - - The thread has received new information about files, - update and emit dataChanged if it has actually changed. - */ -void QFileSystemModelPrivate::_q_fileSystemChanged(const QString &path, - const QList> &updates) -{ -#if QT_CONFIG(filesystemwatcher) - Q_Q(QFileSystemModel); - QList rowsToUpdate; - QStringList newFiles; - QFileSystemModelPrivate::QFileSystemNode *parentNode = node(path, false); - QModelIndex parentIndex = index(parentNode); - for (const auto &update : updates) { - QString fileName = update.first; - Q_ASSERT(!fileName.isEmpty()); - QExtendedInformation info = fileInfoGatherer.getInfo(update.second); - bool previouslyHere = parentNode->children.contains(fileName); - if (!previouslyHere) { - addNode(parentNode, fileName, info.fileInfo()); - } - QFileSystemModelPrivate::QFileSystemNode * node = parentNode->children.value(fileName); - bool isCaseSensitive = parentNode->caseSensitive(); - if (isCaseSensitive) { - if (node->fileName != fileName) - continue; - } else { - if (QString::compare(node->fileName,fileName,Qt::CaseInsensitive) != 0) - continue; - } - if (isCaseSensitive) { - Q_ASSERT(node->fileName == fileName); - } else { - node->fileName = fileName; - } - - if (*node != info ) { - node->populate(info); - bypassFilters.remove(node); - // brand new information. - if (filtersAcceptsNode(node)) { - if (!node->isVisible) { - newFiles.append(fileName); - } else { - rowsToUpdate.append(fileName); - } - } else { - if (node->isVisible) { - int visibleLocation = parentNode->visibleLocation(fileName); - removeVisibleFile(parentNode, visibleLocation); - } else { - // The file is not visible, don't do anything - } - } - } - } - - // bundle up all of the changed signals into as few as possible. - std::sort(rowsToUpdate.begin(), rowsToUpdate.end()); - QString min; - QString max; - for (const QString &value : qAsConst(rowsToUpdate)) { - //##TODO is there a way to bundle signals with QString as the content of the list? - /*if (min.isEmpty()) { - min = value; - if (i != rowsToUpdate.count() - 1) - continue; - } - if (i != rowsToUpdate.count() - 1) { - if ((value == min + 1 && max.isEmpty()) || value == max + 1) { - max = value; - continue; - } - }*/ - max = value; - min = value; - int visibleMin = parentNode->visibleLocation(min); - int visibleMax = parentNode->visibleLocation(max); - if (visibleMin >= 0 - && visibleMin < parentNode->visibleChildren.count() - && parentNode->visibleChildren.at(visibleMin) == min - && visibleMax >= 0) { - QModelIndex bottom = q->index(translateVisibleLocation(parentNode, visibleMin), 0, parentIndex); - QModelIndex top = q->index(translateVisibleLocation(parentNode, visibleMax), 3, parentIndex); - emit q->dataChanged(bottom, top); - } - - /*min = QString(); - max = QString();*/ - } - - if (newFiles.count() > 0) { - addVisibleFiles(parentNode, newFiles); - } - - if (newFiles.count() > 0 || (sortColumn != 0 && rowsToUpdate.count() > 0)) { - forceSort = true; - delayedSort(); - } -#else - Q_UNUSED(path); - Q_UNUSED(updates); -#endif // filesystemwatcher -} - -/*! - \internal -*/ -void QFileSystemModelPrivate::_q_resolvedName(const QString &fileName, const QString &resolvedName) -{ - resolvedSymLinks[fileName] = resolvedName; -} - -#if QT_CONFIG(filesystemwatcher) && defined(Q_OS_WIN) -// Remove file system watchers at/below the index and return a list of previously -// watched files. This should be called prior to operations like rename/remove -// which might fail due to watchers on platforms like Windows. The watchers -// should be restored on failure. -QStringList QFileSystemModelPrivate::unwatchPathsAt(const QModelIndex &index) -{ - const QFileSystemModelPrivate::QFileSystemNode *indexNode = node(index); - if (indexNode == nullptr) - return QStringList(); - const Qt::CaseSensitivity caseSensitivity = indexNode->caseSensitive() - ? Qt::CaseSensitive : Qt::CaseInsensitive; - const QString path = indexNode->fileInfo().absoluteFilePath(); - - QStringList result; - const auto filter = [path, caseSensitivity] (const QString &watchedPath) - { - const int pathSize = path.size(); - if (pathSize == watchedPath.size()) { - return path.compare(watchedPath, caseSensitivity) == 0; - } else if (watchedPath.size() > pathSize) { - return watchedPath.at(pathSize) == QLatin1Char('/') - && watchedPath.startsWith(path, caseSensitivity); - } - return false; - }; - - const QStringList &watchedFiles = fileInfoGatherer.watchedFiles(); - std::copy_if(watchedFiles.cbegin(), watchedFiles.cend(), - std::back_inserter(result), filter); - - const QStringList &watchedDirectories = fileInfoGatherer.watchedDirectories(); - std::copy_if(watchedDirectories.cbegin(), watchedDirectories.cend(), - std::back_inserter(result), filter); - - fileInfoGatherer.unwatchPaths(result); - return result; -} -#endif // filesystemwatcher && Q_OS_WIN - -/*! - \internal -*/ -void QFileSystemModelPrivate::init() -{ - Q_Q(QFileSystemModel); - - delayedSortTimer.setSingleShot(true); - - qRegisterMetaType>>(); -#if QT_CONFIG(filesystemwatcher) - q->connect(&fileInfoGatherer, SIGNAL(newListOfFiles(QString,QStringList)), - q, SLOT(_q_directoryChanged(QString,QStringList))); - q->connect(&fileInfoGatherer, SIGNAL(updates(QString, QList>)), q, - SLOT(_q_fileSystemChanged(QString, QList>))); - q->connect(&fileInfoGatherer, SIGNAL(nameResolved(QString,QString)), - q, SLOT(_q_resolvedName(QString,QString))); - q->connect(&fileInfoGatherer, SIGNAL(directoryLoaded(QString)), - q, SIGNAL(directoryLoaded(QString))); -#endif // filesystemwatcher - q->connect(&delayedSortTimer, SIGNAL(timeout()), q, SLOT(_q_performDelayedSort()), Qt::QueuedConnection); -} - -/*! - \internal - - Returns \c false if node doesn't pass the filters otherwise true - - QDir::Modified is not supported - QDir::Drives is not supported -*/ -bool QFileSystemModelPrivate::filtersAcceptsNode(const QFileSystemNode *node) const -{ - // always accept drives - if (node->parent == &root || bypassFilters.contains(node)) - return true; - - // If we don't know anything yet don't accept it - if (!node->hasInformation()) - return false; - - const bool filterPermissions = ((filters & QDir::PermissionMask) - && (filters & QDir::PermissionMask) != QDir::PermissionMask); - const bool hideDirs = !(filters & (QDir::Dirs | QDir::AllDirs)); - const bool hideFiles = !(filters & QDir::Files); - const bool hideReadable = !(!filterPermissions || (filters & QDir::Readable)); - const bool hideWritable = !(!filterPermissions || (filters & QDir::Writable)); - const bool hideExecutable = !(!filterPermissions || (filters & QDir::Executable)); - const bool hideHidden = !(filters & QDir::Hidden); - const bool hideSystem = !(filters & QDir::System); - const bool hideSymlinks = (filters & QDir::NoSymLinks); - const bool hideDot = (filters & QDir::NoDot); - const bool hideDotDot = (filters & QDir::NoDotDot); - - // Note that we match the behavior of entryList and not QFileInfo on this. - bool isDot = (node->fileName == QLatin1String(".")); - bool isDotDot = (node->fileName == QLatin1String("..")); - if ( (hideHidden && !(isDot || isDotDot) && node->isHidden()) - || (hideSystem && node->isSystem()) - || (hideDirs && node->isDir()) - || (hideFiles && node->isFile()) - || (hideSymlinks && node->isSymLink()) - || (hideReadable && node->isReadable()) - || (hideWritable && node->isWritable()) - || (hideExecutable && node->isExecutable()) - || (hideDot && isDot) - || (hideDotDot && isDotDot)) - return false; - - return nameFilterDisables || passNameFilters(node); -} - -/* - \internal - - Returns \c true if node passes the name filters and should be visible. - */ -bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const -{ -#if QT_CONFIG(regularexpression) - if (nameFilters.isEmpty()) - return true; - - // Check the name regularexpression filters - if (!(node->isDir() && (filters & QDir::AllDirs))) { - auto cs = (filters & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive; - - for (const auto &nameFilter : nameFilters) { - auto rx = QRegularExpression::fromWildcard(nameFilter, cs); - QRegularExpressionMatch match = rx.match(node->fileName); - if (match.hasMatch()) - return true; - } - return false; - } -#else - Q_UNUSED(node); -#endif - return true; -} - -QT_END_NAMESPACE - -#include "moc_qfilesystemmodel.cpp" diff --git a/src/widgets/dialogs/qfilesystemmodel.h b/src/widgets/dialogs/qfilesystemmodel.h deleted file mode 100644 index 518a45a477..0000000000 --- a/src/widgets/dialogs/qfilesystemmodel.h +++ /dev/null @@ -1,190 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module 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$ -** -****************************************************************************/ - -#ifndef QFILESYSTEMMODEL_H -#define QFILESYSTEMMODEL_H - -#include -#include -#include -#include -#include -#include - -QT_REQUIRE_CONFIG(filesystemmodel); - -QT_BEGIN_NAMESPACE - -class ExtendedInformation; -class QFileSystemModelPrivate; -class QFileIconProvider; - -class Q_WIDGETS_EXPORT QFileSystemModel : public QAbstractItemModel -{ - Q_OBJECT - Q_PROPERTY(bool resolveSymlinks READ resolveSymlinks WRITE setResolveSymlinks) - Q_PROPERTY(bool readOnly READ isReadOnly WRITE setReadOnly) - Q_PROPERTY(bool nameFilterDisables READ nameFilterDisables WRITE setNameFilterDisables) - Q_PROPERTY(Options options READ options WRITE setOptions) - -Q_SIGNALS: - void rootPathChanged(const QString &newPath); - void fileRenamed(const QString &path, const QString &oldName, const QString &newName); - void directoryLoaded(const QString &path); - -public: - enum Roles { - FileIconRole = Qt::DecorationRole, - FilePathRole = Qt::UserRole + 1, - FileNameRole = Qt::UserRole + 2, - FilePermissions = Qt::UserRole + 3 - }; - - enum Option - { - DontWatchForChanges = 0x00000001, - DontResolveSymlinks = 0x00000002, - DontUseCustomDirectoryIcons = 0x00000004 - }; - Q_ENUM(Option) - Q_DECLARE_FLAGS(Options, Option) - - explicit QFileSystemModel(QObject *parent = nullptr); - ~QFileSystemModel(); - - QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; - QModelIndex index(const QString &path, int column = 0) const; - QModelIndex parent(const QModelIndex &child) const override; - using QObject::parent; - QModelIndex sibling(int row, int column, const QModelIndex &idx) const override; - bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; - bool canFetchMore(const QModelIndex &parent) const override; - void fetchMore(const QModelIndex &parent) override; - - int rowCount(const QModelIndex &parent = QModelIndex()) const override; - int columnCount(const QModelIndex &parent = QModelIndex()) const override; - - QVariant myComputer(int role = Qt::DisplayRole) const; - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; - - QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; - - Qt::ItemFlags flags(const QModelIndex &index) const override; - - void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override; - - QStringList mimeTypes() const override; - QMimeData *mimeData(const QModelIndexList &indexes) const override; - bool dropMimeData(const QMimeData *data, Qt::DropAction action, - int row, int column, const QModelIndex &parent) override; - Qt::DropActions supportedDropActions() const override; - QHash roleNames() const override; - - // QFileSystemModel specific API - QModelIndex setRootPath(const QString &path); - QString rootPath() const; - QDir rootDirectory() const; - - void setIconProvider(QFileIconProvider *provider); - QFileIconProvider *iconProvider() const; - - void setFilter(QDir::Filters filters); - QDir::Filters filter() const; - - void setResolveSymlinks(bool enable); - bool resolveSymlinks() const; - - void setReadOnly(bool enable); - bool isReadOnly() const; - - void setNameFilterDisables(bool enable); - bool nameFilterDisables() const; - - void setNameFilters(const QStringList &filters); - QStringList nameFilters() const; - - void setOption(Option option, bool on = true); - bool testOption(Option option) const; - void setOptions(Options options); - Options options() const; - - QString filePath(const QModelIndex &index) const; - bool isDir(const QModelIndex &index) const; - qint64 size(const QModelIndex &index) const; - QString type(const QModelIndex &index) const; - QDateTime lastModified(const QModelIndex &index) const; - - QModelIndex mkdir(const QModelIndex &parent, const QString &name); - bool rmdir(const QModelIndex &index); - inline QString fileName(const QModelIndex &index) const; - inline QIcon fileIcon(const QModelIndex &index) const; - QFile::Permissions permissions(const QModelIndex &index) const; - QFileInfo fileInfo(const QModelIndex &index) const; - bool remove(const QModelIndex &index); - -protected: - QFileSystemModel(QFileSystemModelPrivate &, QObject *parent = nullptr); - void timerEvent(QTimerEvent *event) override; - bool event(QEvent *event) override; - -private: - Q_DECLARE_PRIVATE(QFileSystemModel) - Q_DISABLE_COPY(QFileSystemModel) - - Q_PRIVATE_SLOT(d_func(), void _q_directoryChanged(const QString &directory, const QStringList &list)) - Q_PRIVATE_SLOT(d_func(), void _q_performDelayedSort()) - Q_PRIVATE_SLOT(d_func(), - void _q_fileSystemChanged(const QString &path, - const QList> &)) - Q_PRIVATE_SLOT(d_func(), void _q_resolvedName(const QString &fileName, const QString &resolvedName)) - - friend class QFileDialogPrivate; -}; - -inline QString QFileSystemModel::fileName(const QModelIndex &aindex) const -{ return aindex.data(Qt::DisplayRole).toString(); } -inline QIcon QFileSystemModel::fileIcon(const QModelIndex &aindex) const -{ return qvariant_cast(aindex.data(Qt::DecorationRole)); } - -Q_DECLARE_OPERATORS_FOR_FLAGS(QFileSystemModel::Options) - -QT_END_NAMESPACE - -#endif // QFILESYSTEMMODEL_H diff --git a/src/widgets/dialogs/qfilesystemmodel_p.h b/src/widgets/dialogs/qfilesystemmodel_p.h deleted file mode 100644 index 1870b08df1..0000000000 --- a/src/widgets/dialogs/qfilesystemmodel_p.h +++ /dev/null @@ -1,323 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module 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$ -** -****************************************************************************/ - -#ifndef QFILESYSTEMMODEL_P_H -#define QFILESYSTEMMODEL_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include -#include "qfilesystemmodel.h" - -#include -#include -#include "qfileinfogatherer_p.h" -#include -#include -#include -#include -#include -#include - -QT_REQUIRE_CONFIG(filesystemmodel); - -QT_BEGIN_NAMESPACE - -class ExtendedInformation; -class QFileSystemModelPrivate; -class QFileIconProvider; - -#if defined(Q_OS_WIN) -class QFileSystemModelNodePathKey : public QString -{ -public: - QFileSystemModelNodePathKey() {} - QFileSystemModelNodePathKey(const QString &other) : QString(other) {} - QFileSystemModelNodePathKey(const QFileSystemModelNodePathKey &other) : QString(other) {} - bool operator==(const QFileSystemModelNodePathKey &other) const { return !compare(other, Qt::CaseInsensitive); } -}; - -Q_DECLARE_TYPEINFO(QFileSystemModelNodePathKey, Q_MOVABLE_TYPE); - -inline size_t qHash(const QFileSystemModelNodePathKey &key) { return qHash(key.toCaseFolded()); } -#else // Q_OS_WIN -typedef QString QFileSystemModelNodePathKey; -#endif - -class Q_AUTOTEST_EXPORT QFileSystemModelPrivate : public QAbstractItemModelPrivate -{ - Q_DECLARE_PUBLIC(QFileSystemModel) - -public: - enum { NumColumns = 4 }; - - class QFileSystemNode - { - public: - Q_DISABLE_COPY_MOVE(QFileSystemNode) - - explicit QFileSystemNode(const QString &filename = QString(), QFileSystemNode *p = nullptr) - : fileName(filename), parent(p) {} - ~QFileSystemNode() { - qDeleteAll(children); - delete info; - } - - QString fileName; -#if defined(Q_OS_WIN) - QString volumeName; -#endif - - inline qint64 size() const { if (info && !info->isDir()) return info->size(); return 0; } - inline QString type() const { if (info) return info->displayType; return QLatin1String(""); } - inline QDateTime lastModified() const { if (info) return info->lastModified(); return QDateTime(); } - inline QFile::Permissions permissions() const { if (info) return info->permissions(); return { }; } - inline bool isReadable() const { return ((permissions() & QFile::ReadUser) != 0); } - inline bool isWritable() const { return ((permissions() & QFile::WriteUser) != 0); } - inline bool isExecutable() const { return ((permissions() & QFile::ExeUser) != 0); } - inline bool isDir() const { - if (info) - return info->isDir(); - if (children.count() > 0) - return true; - return false; - } - inline QFileInfo fileInfo() const { if (info) return info->fileInfo(); return QFileInfo(); } - inline bool isFile() const { if (info) return info->isFile(); return true; } - inline bool isSystem() const { if (info) return info->isSystem(); return true; } - inline bool isHidden() const { if (info) return info->isHidden(); return false; } - inline bool isSymLink(bool ignoreNtfsSymLinks = false) const { return info && info->isSymLink(ignoreNtfsSymLinks); } - inline bool caseSensitive() const { if (info) return info->isCaseSensitive(); return false; } - inline QIcon icon() const { if (info) return info->icon; return QIcon(); } - - inline bool operator <(const QFileSystemNode &node) const { - if (caseSensitive() || node.caseSensitive()) - return fileName < node.fileName; - return QString::compare(fileName, node.fileName, Qt::CaseInsensitive) < 0; - } - inline bool operator >(const QString &name) const { - if (caseSensitive()) - return fileName > name; - return QString::compare(fileName, name, Qt::CaseInsensitive) > 0; - } - inline bool operator <(const QString &name) const { - if (caseSensitive()) - return fileName < name; - return QString::compare(fileName, name, Qt::CaseInsensitive) < 0; - } - inline bool operator !=(const QExtendedInformation &fileInfo) const { - return !operator==(fileInfo); - } - bool operator ==(const QString &name) const { - if (caseSensitive()) - return fileName == name; - return QString::compare(fileName, name, Qt::CaseInsensitive) == 0; - } - bool operator ==(const QExtendedInformation &fileInfo) const { - return info && (*info == fileInfo); - } - - inline bool hasInformation() const { return info != nullptr; } - - void populate(const QExtendedInformation &fileInfo) { - if (!info) - info = new QExtendedInformation(fileInfo.fileInfo()); - (*info) = fileInfo; - } - - // children shouldn't normally be accessed directly, use node() - inline int visibleLocation(const QString &childName) { - return visibleChildren.indexOf(childName); - } - void updateIcon(QFileIconProvider *iconProvider, const QString &path) { - if (info) - info->icon = iconProvider->icon(QFileInfo(path)); - for (QFileSystemNode *child : qAsConst(children)) { - //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/) - if (!path.isEmpty()) { - if (path.endsWith(QLatin1Char('/'))) - child->updateIcon(iconProvider, path + child->fileName); - else - child->updateIcon(iconProvider, path + QLatin1Char('/') + child->fileName); - } else - child->updateIcon(iconProvider, child->fileName); - } - } - - void retranslateStrings(QFileIconProvider *iconProvider, const QString &path) { - if (info) - info->displayType = iconProvider->type(QFileInfo(path)); - for (QFileSystemNode *child : qAsConst(children)) { - //On windows the root (My computer) has no path so we don't want to add a / for nothing (e.g. /C:/) - if (!path.isEmpty()) { - if (path.endsWith(QLatin1Char('/'))) - child->retranslateStrings(iconProvider, path + child->fileName); - else - child->retranslateStrings(iconProvider, path + QLatin1Char('/') + child->fileName); - } else - child->retranslateStrings(iconProvider, child->fileName); - } - } - - QHash children; - QList visibleChildren; - QExtendedInformation *info = nullptr; - QFileSystemNode *parent; - int dirtyChildrenIndex = -1; - bool populatedChildren = false; - bool isVisible = false; - }; - - QFileSystemModelPrivate() = default; - void init(); - /* - \internal - - Return true if index which is owned by node is hidden by the filter. - */ - inline bool isHiddenByFilter(QFileSystemNode *indexNode, const QModelIndex &index) const - { - return (indexNode != &root && !index.isValid()); - } - QFileSystemNode *node(const QModelIndex &index) const; - QFileSystemNode *node(const QString &path, bool fetch = true) const; - inline QModelIndex index(const QString &path, int column = 0) { return index(node(path), column); } - QModelIndex index(const QFileSystemNode *node, int column = 0) const; - bool filtersAcceptsNode(const QFileSystemNode *node) const; - bool passNameFilters(const QFileSystemNode *node) const; - void removeNode(QFileSystemNode *parentNode, const QString &name); - QFileSystemNode* addNode(QFileSystemNode *parentNode, const QString &fileName, const QFileInfo &info); - void addVisibleFiles(QFileSystemNode *parentNode, const QStringList &newFiles); - void removeVisibleFile(QFileSystemNode *parentNode, int visibleLocation); - void sortChildren(int column, const QModelIndex &parent); - - inline int translateVisibleLocation(QFileSystemNode *parent, int row) const { - if (sortOrder != Qt::AscendingOrder) { - if (parent->dirtyChildrenIndex == -1) - return parent->visibleChildren.count() - row - 1; - - if (row < parent->dirtyChildrenIndex) - return parent->dirtyChildrenIndex - row - 1; - } - - return row; - } - - inline static QString myComputer() { - // ### TODO We should query the system to find out what the string should be - // XP == "My Computer", - // Vista == "Computer", - // OS X == "Computer" (sometime user generated) "Benjamin's PowerBook G4" -#ifdef Q_OS_WIN - return QFileSystemModel::tr("My Computer"); -#else - return QFileSystemModel::tr("Computer"); -#endif - } - - inline void delayedSort() { - if (!delayedSortTimer.isActive()) - delayedSortTimer.start(0); - } - - QIcon icon(const QModelIndex &index) const; - QString name(const QModelIndex &index) const; - QString displayName(const QModelIndex &index) const; - QString filePath(const QModelIndex &index) const; - QString size(const QModelIndex &index) const; - static QString size(qint64 bytes); - QString type(const QModelIndex &index) const; - QString time(const QModelIndex &index) const; - - void _q_directoryChanged(const QString &directory, const QStringList &list); - void _q_performDelayedSort(); - void _q_fileSystemChanged(const QString &path, const QList> &); - void _q_resolvedName(const QString &fileName, const QString &resolvedName); - - QDir rootDir; -#if QT_CONFIG(filesystemwatcher) -# ifdef Q_OS_WIN - QStringList unwatchPathsAt(const QModelIndex &); - void watchPaths(const QStringList &paths) { fileInfoGatherer.watchPaths(paths); } -# endif // Q_OS_WIN - QFileInfoGatherer fileInfoGatherer; -#endif // filesystemwatcher - QTimer delayedSortTimer; - QHash bypassFilters; -#if QT_CONFIG(regularexpression) - QStringList nameFilters; -#endif - QHash resolvedSymLinks; - - QFileSystemNode root; - - struct Fetching { - QString dir; - QString file; - const QFileSystemNode *node; - }; - QList toFetch; - - QBasicTimer fetchingTimer; - - QDir::Filters filters = QDir::AllEntries | QDir::NoDotAndDotDot | QDir::AllDirs; - int sortColumn = 0; - Qt::SortOrder sortOrder = Qt::AscendingOrder; - bool forceSort = true; - bool readOnly = true; - bool setRootPath = false; - bool nameFilterDisables = true; // false on windows, true on mac and unix - // This flag is an optimization for QFileDialog. It enables a sort which is - // not recursive, meaning we sort only what we see. - bool disableRecursiveSort = false; -}; -Q_DECLARE_TYPEINFO(QFileSystemModelPrivate::Fetching, Q_MOVABLE_TYPE); - -QT_END_NAMESPACE - -#endif diff --git a/src/widgets/dialogs/qfscompleter_p.h b/src/widgets/dialogs/qfscompleter_p.h index f5110a7622..be46f5be43 100644 --- a/src/widgets/dialogs/qfscompleter_p.h +++ b/src/widgets/dialogs/qfscompleter_p.h @@ -53,7 +53,7 @@ #include #include "qcompleter.h" -#include +#include QT_REQUIRE_CONFIG(fscompleter); diff --git a/src/widgets/dialogs/qsidebar.cpp b/src/widgets/dialogs/qsidebar.cpp index 4272b612ec..296542a187 100644 --- a/src/widgets/dialogs/qsidebar.cpp +++ b/src/widgets/dialogs/qsidebar.cpp @@ -38,7 +38,6 @@ ****************************************************************************/ #include "qsidebar_p.h" -#include "qfilesystemmodel.h" #include #include @@ -48,7 +47,8 @@ #include #include #include -#include +#include +#include #include QT_BEGIN_NAMESPACE @@ -198,9 +198,9 @@ void QUrlModel::setUrl(const QModelIndex &index, const QUrl &url, const QModelIn QIcon newIcon = qvariant_cast(dirIndex.data(Qt::DecorationRole)); if (!dirIndex.isValid()) { - const QFileIconProvider *provider = fileSystemModel->iconProvider(); + const QAbstractFileIconProvider *provider = fileSystemModel->iconProvider(); if (provider) - newIcon = provider->icon(QFileIconProvider::Folder); + newIcon = provider->icon(QAbstractFileIconProvider::Folder); newName = QFileInfo(url.toLocalFile()).fileName(); if (!invalidUrls.contains(url)) invalidUrls.append(url); diff --git a/src/widgets/itemviews/qfileiconprovider.cpp b/src/widgets/itemviews/qfileiconprovider.cpp index d70d47be5e..59f132e17c 100644 --- a/src/widgets/itemviews/qfileiconprovider.cpp +++ b/src/widgets/itemviews/qfileiconprovider.cpp @@ -67,29 +67,8 @@ QT_BEGIN_NAMESPACE \brief The QFileIconProvider class provides file icons for the QFileSystemModel class. */ -/*! - \enum QFileIconProvider::IconType - \value Computer - \value Desktop - \value Trashcan - \value Network - \value Drive - \value Folder - \value File -*/ - - -/*! - \enum QFileIconProvider::Option - \since 5.2 - - \value DontUseCustomDirectoryIcons Always use the default directory icon. - Some platforms allow the user to set a different icon. Custom icon lookup - cause a big performance impact over network or removable drives. -*/ - -QFileIconProviderPrivate::QFileIconProviderPrivate(QFileIconProvider *q) : - q_ptr(q), homePath(QDir::home().absolutePath()) +QFileIconProviderPrivate::QFileIconProviderPrivate(QFileIconProvider *q) + : QAbstractFileIconProviderPrivate(q), homePath(QDir::home().absolutePath()) { } @@ -155,46 +134,18 @@ QIcon QFileIconProviderPrivate::getIcon(QStyle::StandardPixmap name) const */ QFileIconProvider::QFileIconProvider() - : d_ptr(new QFileIconProviderPrivate(this)) + : QAbstractFileIconProvider(*new QFileIconProviderPrivate(this)) { } /*! Destroys the file icon provider. - -*/ - -QFileIconProvider::~QFileIconProvider() -{ -} - -/*! - \since 5.2 - Sets \a options that affect the icon provider. - \sa options() -*/ - -void QFileIconProvider::setOptions(QFileIconProvider::Options options) -{ - Q_D(QFileIconProvider); - d->options = options; -} - -/*! - \since 5.2 - Returns all the options that affect the icon provider. - By default, all options are disabled. - \sa setOptions() */ -QFileIconProvider::Options QFileIconProvider::options() const -{ - Q_D(const QFileIconProvider); - return d->options; -} +QFileIconProvider::~QFileIconProvider() = default; /*! - Returns an icon set for the given \a type. + \reimpl */ QIcon QFileIconProvider::icon(IconType type) const @@ -235,7 +186,7 @@ QIcon QFileIconProviderPrivate::getIcon(const QFileInfo &fi) const } /*! - Returns an icon for the file described by \a info. + \reimpl */ QIcon QFileIconProvider::icon(const QFileInfo &info) const @@ -292,45 +243,4 @@ QIcon QFileIconProvider::icon(const QFileInfo &info) const return QIcon(); } -/*! - Returns the type of the file described by \a info. -*/ - -QString QFileIconProvider::type(const QFileInfo &info) const -{ - if (QFileSystemEntry::isRootPath(info.absoluteFilePath())) - return QApplication::translate("QFileDialog", "Drive"); - if (info.isFile()) { - if (!info.suffix().isEmpty()) { - //: %1 is a file name suffix, for example txt - return QApplication::translate("QFileDialog", "%1 File").arg(info.suffix()); - } - return QApplication::translate("QFileDialog", "File"); - } - - if (info.isDir()) -#ifdef Q_OS_WIN - return QApplication::translate("QFileDialog", "File Folder", "Match Windows Explorer"); -#else - return QApplication::translate("QFileDialog", "Folder", "All other platforms"); -#endif - // Windows - "File Folder" - // OS X - "Folder" - // Konqueror - "Folder" - // Nautilus - "folder" - - if (info.isSymLink()) -#ifdef Q_OS_MAC - return QApplication::translate("QFileDialog", "Alias", "OS X Finder"); -#else - return QApplication::translate("QFileDialog", "Shortcut", "All other platforms"); -#endif - // OS X - "Alias" - // Windows - "Shortcut" - // Konqueror - "Folder" or "TXT File" i.e. what it is pointing to - // Nautilus - "link to folder" or "link to object file", same as Konqueror - - return QApplication::translate("QFileDialog", "Unknown"); -} - QT_END_NAMESPACE diff --git a/src/widgets/itemviews/qfileiconprovider.h b/src/widgets/itemviews/qfileiconprovider.h index 732ca1ac8e..35db39bfd8 100644 --- a/src/widgets/itemviews/qfileiconprovider.h +++ b/src/widgets/itemviews/qfileiconprovider.h @@ -44,39 +44,27 @@ #include #include #include +#include QT_BEGIN_NAMESPACE class QFileIconProviderPrivate; -class Q_WIDGETS_EXPORT QFileIconProvider +class Q_WIDGETS_EXPORT QFileIconProvider : public QAbstractFileIconProvider { public: QFileIconProvider(); - virtual ~QFileIconProvider(); - enum IconType { Computer, Desktop, Trashcan, Network, Drive, Folder, File }; + ~QFileIconProvider(); - enum Option { - DontUseCustomDirectoryIcons = 0x00000001 - }; - Q_DECLARE_FLAGS(Options, Option) - - virtual QIcon icon(IconType type) const; - virtual QIcon icon(const QFileInfo &info) const; - virtual QString type(const QFileInfo &info) const; - - void setOptions(Options options); - Options options() const; + QIcon icon(IconType type) const override; + QIcon icon(const QFileInfo &info) const override; private: Q_DECLARE_PRIVATE(QFileIconProvider) - QScopedPointer d_ptr; Q_DISABLE_COPY(QFileIconProvider) }; -Q_DECLARE_OPERATORS_FOR_FLAGS(QFileIconProvider::Options) - QT_END_NAMESPACE #endif // QFILEICONPROVIDER_H diff --git a/src/widgets/itemviews/qfileiconprovider_p.h b/src/widgets/itemviews/qfileiconprovider_p.h index 4726b15816..6a9a1f2e0e 100644 --- a/src/widgets/itemviews/qfileiconprovider_p.h +++ b/src/widgets/itemviews/qfileiconprovider_p.h @@ -54,6 +54,7 @@ #include #include "qfileiconprovider.h" +#include #include #include #include @@ -62,7 +63,7 @@ QT_BEGIN_NAMESPACE class QFileInfo; -class QFileIconProviderPrivate +class QFileIconProviderPrivate : public QAbstractFileIconProviderPrivate { Q_DECLARE_PUBLIC(QFileIconProvider) @@ -71,9 +72,7 @@ public: QIcon getIcon(QStyle::StandardPixmap name) const; QIcon getIcon(const QFileInfo &fi) const; - QFileIconProvider *q_ptr; const QString homePath; - QFileIconProvider::Options options; private: mutable QIcon file; diff --git a/src/widgets/util/qcompleter.cpp b/src/widgets/util/qcompleter.cpp index b3833c5f50..7bfd112f5c 100644 --- a/src/widgets/util/qcompleter.cpp +++ b/src/widgets/util/qcompleter.cpp @@ -149,7 +149,7 @@ #include "QtCore/qstringlistmodel.h" #endif #if QT_CONFIG(filesystemmodel) -#include "QtWidgets/qfilesystemmodel.h" +#include "QtGui/qfilesystemmodel.h" #endif #include "QtWidgets/qheaderview.h" #if QT_CONFIG(listview) diff --git a/tests/auto/gui/itemmodels/CMakeLists.txt b/tests/auto/gui/itemmodels/CMakeLists.txt index b8c55489c4..e1d3ad3f6b 100644 --- a/tests/auto/gui/itemmodels/CMakeLists.txt +++ b/tests/auto/gui/itemmodels/CMakeLists.txt @@ -1,6 +1,9 @@ # Generated from itemmodels.pro. add_subdirectory(qstandarditem) +if(NOT MINGW) + add_subdirectory(qfilesystemmodel) +endif() if(TARGET Qt::Widgets) add_subdirectory(qstandarditemmodel) endif() diff --git a/tests/auto/gui/itemmodels/itemmodels.pro b/tests/auto/gui/itemmodels/itemmodels.pro index 8a300d0210..cde70aa2b2 100644 --- a/tests/auto/gui/itemmodels/itemmodels.pro +++ b/tests/auto/gui/itemmodels/itemmodels.pro @@ -1,7 +1,10 @@ TEMPLATE=subdirs SUBDIRS= \ qstandarditem \ - qstandarditemmodel + qstandarditemmodel \ + qfilesystemmodel + +mingw: SUBDIRS -= qfilesystemmodel # QTBUG-29403 !qtHaveModule(widgets): SUBDIRS -= \ qstandarditemmodel diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/.gitignore b/tests/auto/gui/itemmodels/qfilesystemmodel/.gitignore new file mode 100644 index 0000000000..9804e5a3d7 --- /dev/null +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/.gitignore @@ -0,0 +1 @@ +tst_qfilesystemmodel diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST b/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST new file mode 100644 index 0000000000..4119afce84 --- /dev/null +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/BLACKLIST @@ -0,0 +1,5 @@ +[sort:QFileDialog usage] +ubuntu +b2qt +[specialFiles] +b2qt diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt new file mode 100644 index 0000000000..595b30bd56 --- /dev/null +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/CMakeLists.txt @@ -0,0 +1,27 @@ +# Generated from qfilesystemmodel.pro. + +##################################################################### +## tst_qfilesystemmodel Test: +##################################################################### + +qt_add_test(tst_qfilesystemmodel + SOURCES + ../../../../shared/emulationdetector.h + tst_qfilesystemmodel.cpp + INCLUDE_DIRECTORIES + ../../../../shared + PUBLIC_LIBRARIES + Qt::CorePrivate + Qt::Gui + Qt::Widgets + Qt::WidgetsPrivate +) + +## Scopes: +##################################################################### + +#### Keys ignored in scope 2:.:.:qfilesystemmodel.pro:WIN32: +# testcase.timeout = "900" + +#### Keys ignored in scope 3:.:.:qfilesystemmodel.pro:MACOS: +# testcase.timeout = "900" diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/qfilesystemmodel.pro b/tests/auto/gui/itemmodels/qfilesystemmodel/qfilesystemmodel.pro new file mode 100644 index 0000000000..db8cf7de3f --- /dev/null +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/qfilesystemmodel.pro @@ -0,0 +1,13 @@ +INCLUDEPATH += ../../../../shared +HEADERS += ../../../../shared/emulationdetector.h + +CONFIG += testcase +# This testcase can be slow on Windows and OS X, and may interfere with other file system tests. +win32:testcase.timeout = 900 +macx:testcase.timeout = 900 + +QT += widgets widgets-private +QT += core-private testlib + +SOURCES += tst_qfilesystemmodel.cpp +TARGET = tst_qfilesystemmodel diff --git a/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp new file mode 100644 index 0000000000..e814e79dbe --- /dev/null +++ b/tests/auto/gui/itemmodels/qfilesystemmodel/tst_qfilesystemmodel.cpp @@ -0,0 +1,1207 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** 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 General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** 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-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include +#include +#ifdef QT_BUILD_INTERNAL +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined(Q_OS_WIN) +# include // for SetFileAttributes +#endif +#include + +#include + +#define WAITTIME 1000 + +// Will try to wait for the condition while allowing event processing +// for a maximum of 5 seconds. +#define TRY_WAIT(expr, timedOut) \ + do { \ + *timedOut = true; \ + const int step = 50; \ + for (int __i = 0; __i < 5000; __i += step) { \ + if (expr) { \ + *timedOut = false; \ + break; \ + } \ + QTest::qWait(step); \ + } \ + } while(0) + +Q_DECLARE_METATYPE(QDir::Filters) +Q_DECLARE_METATYPE(QFileDevice::Permissions) + +Q_LOGGING_CATEGORY(lcFileSystemModel, "qt.widgets.tests.qfilesystemmodel") + +class tst_QFileSystemModel : public QObject { + Q_OBJECT + +private slots: + void initTestCase(); + void cleanup(); + + void indexPath(); + + void rootPath(); + void readOnly(); + void iconProvider(); + + void rowCount(); + + void rowsInserted_data(); + void rowsInserted(); + + void rowsRemoved_data(); + void rowsRemoved(); + + void dataChanged_data(); + void dataChanged(); + + void filters_data(); + void filters(); + + void nameFilters(); + + void setData_data(); + void setData(); + + void sortPersistentIndex(); + void sort_data(); + void sort(); + + void mkdir(); + void deleteFile(); + void deleteDirectory(); + + void caseSensitivity(); + + void drives_data(); + void drives(); + void dirsBeforeFiles(); + + void roleNames_data(); + void roleNames(); + + void permissions_data(); + void permissions(); + + void doNotUnwatchOnFailedRmdir(); + void specialFiles(); + + void fileInfo(); + +protected: + bool createFiles(QFileSystemModel *model, const QString &test_path, + const QStringList &initial_files, int existingFileCount = 0, + const QStringList &initial_dirs = QStringList()); + QModelIndex prepareTestModelRoot(QFileSystemModel *model, const QString &test_path, + QSignalSpy **spy2 = nullptr, QSignalSpy **spy3 = nullptr); + +private: + QString flatDirTestPath; + QTemporaryDir m_tempDir; +}; + +void tst_QFileSystemModel::cleanup() +{ + QDir dir(flatDirTestPath); + if (dir.exists()) { + const QDir::Filters filters = QDir::AllEntries | QDir::System | QDir::Hidden | QDir::NoDotAndDotDot; + const QFileInfoList list = dir.entryInfoList(filters); + for (const QFileInfo &fi : list) { + if (fi.isDir()) { + QVERIFY(dir.rmdir(fi.fileName())); + } else { + QFile dead(fi.absoluteFilePath()); + dead.setPermissions(QFile::ReadUser | QFile::ReadOwner | QFile::ExeOwner | QFile::ExeUser | QFile::WriteUser | QFile::WriteOwner | QFile::WriteOther); + QVERIFY(dead.remove()); + } + } + QVERIFY(dir.entryInfoList(filters).isEmpty()); + } +} + +void tst_QFileSystemModel::initTestCase() +{ + QVERIFY2(m_tempDir.isValid(), qPrintable(m_tempDir.errorString())); + flatDirTestPath = m_tempDir.path(); +} + +void tst_QFileSystemModel::indexPath() +{ +#if !defined(Q_OS_WIN) + QScopedPointer model(new QFileSystemModel); + int depth = QDir::currentPath().count('/'); + model->setRootPath(QDir::currentPath()); + QString backPath; + for (int i = 0; i <= depth * 2 + 1; ++i) { + backPath += "../"; + QModelIndex idx = model->index(backPath); + QVERIFY(i != depth - 1 ? idx.isValid() : !idx.isValid()); + } +#endif +} + +void tst_QFileSystemModel::rootPath() +{ + QScopedPointer model(new QFileSystemModel); + QCOMPARE(model->rootPath(), QString(QDir().path())); + + QSignalSpy rootChanged(model.data(), &QFileSystemModel::rootPathChanged); + QModelIndex root = model->setRootPath(model->rootPath()); + root = model->setRootPath("this directory shouldn't exist"); + QCOMPARE(rootChanged.count(), 0); + + QString oldRootPath = model->rootPath(); + const QStringList documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + QVERIFY(!documentPaths.isEmpty()); + QString documentPath = documentPaths.front(); + // In particular on Linux, ~/Documents (the first + // DocumentsLocation) may not exist, so choose ~ in that case: + if (!QFile::exists(documentPath)) { + documentPath = QDir::homePath(); + qWarning("%s: first documentPath \"%s\" does not exist. Using ~ (\"%s\") instead.", + Q_FUNC_INFO, qPrintable(documentPaths.front()), qPrintable(documentPath)); + } + root = model->setRootPath(documentPath); + + QTRY_VERIFY(model->rowCount(root) >= 0); + QCOMPARE(model->rootPath(), QString(documentPath)); + QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? 0 : 1); + QCOMPARE(model->rootDirectory().absolutePath(), documentPath); + + model->setRootPath(QDir::rootPath()); + int oldCount = rootChanged.count(); + oldRootPath = model->rootPath(); + root = model->setRootPath(documentPath + QLatin1String("/.")); + QTRY_VERIFY(model->rowCount(root) >= 0); + QCOMPARE(model->rootPath(), documentPath); + QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1); + QCOMPARE(model->rootDirectory().absolutePath(), documentPath); + + QDir newdir = documentPath; + if (newdir.cdUp()) { + oldCount = rootChanged.count(); + oldRootPath = model->rootPath(); + root = model->setRootPath(documentPath + QLatin1String("/..")); + QTRY_VERIFY(model->rowCount(root) >= 0); + QCOMPARE(model->rootPath(), newdir.path()); + QCOMPARE(rootChanged.count(), oldCount + 1); + QCOMPARE(model->rootDirectory().absolutePath(), newdir.path()); + } +} + +void tst_QFileSystemModel::readOnly() +{ + QScopedPointer model(new QFileSystemModel); + QCOMPARE(model->isReadOnly(), true); + QTemporaryFile file(flatDirTestPath + QStringLiteral("/XXXXXX.dat")); + QVERIFY2(file.open(), qPrintable(file.errorString())); + const QString fileName = file.fileName(); + file.close(); + + const QFileInfo fileInfo(fileName); + QTRY_VERIFY(QDir(flatDirTestPath).entryInfoList().contains(fileInfo)); + QModelIndex root = model->setRootPath(flatDirTestPath); + + QTRY_VERIFY(model->rowCount(root) > 0); + QVERIFY(!(model->flags(model->index(fileName)) & Qt::ItemIsEditable)); + model->setReadOnly(false); + QCOMPARE(model->isReadOnly(), false); + QVERIFY(model->flags(model->index(fileName)) & Qt::ItemIsEditable); +} + +class CustomFileIconProvider : public QFileIconProvider +{ +public: + CustomFileIconProvider() : QFileIconProvider() + { + auto style = QApplication::style(); + mb = style->standardIcon(QStyle::SP_MessageBoxCritical); + dvd = style->standardIcon(QStyle::SP_DriveDVDIcon); + } + + QIcon icon(const QFileInfo &info) const override + { + if (info.isDir()) + return mb; + + return QFileIconProvider::icon(info); + } + QIcon icon(IconType type) const override + { + if (type == QFileIconProvider::Folder) + return dvd; + + return QFileIconProvider::icon(type); + } +private: + QIcon mb; + QIcon dvd; +}; + +void tst_QFileSystemModel::iconProvider() +{ + QScopedPointer model(new QFileSystemModel); + QVERIFY(model->iconProvider()); + QScopedPointer provider(new QFileIconProvider); + model->setIconProvider(provider.data()); + QCOMPARE(model->iconProvider(), provider.data()); + model->setIconProvider(nullptr); + provider.reset(); + + QScopedPointer myModel(new QFileSystemModel); + const QStringList documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); + QVERIFY(!documentPaths.isEmpty()); + myModel->setRootPath(documentPaths.constFirst()); + //We change the provider, icons must be updated + provider.reset(new CustomFileIconProvider); + myModel->setIconProvider(provider.data()); + + QPixmap mb = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(50, 50); + QCOMPARE(myModel->fileIcon(myModel->index(QDir::homePath())).pixmap(50, 50), mb); +} + +bool tst_QFileSystemModel::createFiles(QFileSystemModel *model, const QString &test_path, + const QStringList &initial_files, int existingFileCount, + const QStringList &initial_dirs) +{ + qCDebug(lcFileSystemModel) << (model->rowCount(model->index(test_path))) << existingFileCount << initial_files; + bool timedOut = false; + TRY_WAIT((model->rowCount(model->index(test_path)) == existingFileCount), &timedOut); + if (timedOut) + return false; + + QDir dir(test_path); + if (!dir.exists()) { + qWarning() << "error" << test_path << "doesn't exist"; + return false; + } + for (const auto &initial_dir : initial_dirs) { + if (!dir.mkdir(initial_dir)) { + qWarning() << "error" << "failed to make" << initial_dir; + return false; + } + qCDebug(lcFileSystemModel) << test_path + '/' + initial_dir << (QFile::exists(test_path + '/' + initial_dir)); + } + for (const auto &initial_file : initial_files) { + QFile file(test_path + '/' + initial_file); + if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) { + qDebug() << "failed to open file" << initial_file; + return false; + } + if (!file.resize(1024 + file.size())) { + qDebug() << "failed to resize file" << initial_file; + return false; + } + if (!file.flush()) { + qDebug() << "failed to flush file" << initial_file; + return false; + } + file.close(); +#if defined(Q_OS_WIN) + if (initial_file[0] == '.') { + const QString hiddenFile = QDir::toNativeSeparators(file.fileName()); + const auto nativeHiddenFile = reinterpret_cast(hiddenFile.utf16()); + DWORD currentAttributes = ::GetFileAttributes(nativeHiddenFile); + if (currentAttributes == 0xFFFFFFFF) { + qErrnoWarning("failed to get file attributes: %s", qPrintable(hiddenFile)); + return false; + } + if (!::SetFileAttributes(nativeHiddenFile, currentAttributes | FILE_ATTRIBUTE_HIDDEN)) { + qErrnoWarning("failed to set file hidden: %s", qPrintable(hiddenFile)); + return false; + } + } +#endif + qCDebug(lcFileSystemModel) << test_path + '/' + initial_file << (QFile::exists(test_path + '/' + initial_file)); + } + return true; +} + +QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model, const QString &test_path, + QSignalSpy **spy2, QSignalSpy **spy3) +{ + if (model->rowCount(model->index(test_path)) != 0) + return QModelIndex(); + + if (spy2) + *spy2 = new QSignalSpy(model, &QFileSystemModel::rowsInserted); + if (spy3) + *spy3 = new QSignalSpy(model, &QFileSystemModel::rowsAboutToBeInserted); + + QStringList files = { "b", "d", "f", "h", "j", ".a", ".c", ".e", ".g" }; + + if (!createFiles(model, test_path, files)) + return QModelIndex(); + + QModelIndex root = model->setRootPath(test_path); + if (!root.isValid()) + return QModelIndex(); + + bool timedOut = false; + TRY_WAIT(model->rowCount(root) == 5, &timedOut); + if (timedOut) + return QModelIndex(); + + return root; +} + +void tst_QFileSystemModel::rowCount() +{ + QSignalSpy *spy2 = nullptr; + QSignalSpy *spy3 = nullptr; + QScopedPointer model(new QFileSystemModel); + QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3); + QVERIFY(root.isValid()); + + QVERIFY(spy2 && spy2->count() > 0); + QVERIFY(spy3 && spy3->count() > 0); +} + +void tst_QFileSystemModel::rowsInserted_data() +{ + QTest::addColumn("count"); + QTest::addColumn("ascending"); + for (int i = 0; i < 4; ++i) { + const QByteArray iB = QByteArray::number(i); + QTest::newRow(("Qt::AscendingOrder " + iB).constData()) << i << Qt::AscendingOrder; + QTest::newRow(("Qt::DescendingOrder " + iB).constData()) << i << Qt::DescendingOrder; + } +} + +static inline QString lastEntry(const QModelIndex &root) +{ + const QAbstractItemModel *model = root.model(); + return model->index(model->rowCount(root) - 1, 0, root).data().toString(); +} + +void tst_QFileSystemModel::rowsInserted() +{ + const QString tmp = flatDirTestPath; + QScopedPointer model(new QFileSystemModel); + QModelIndex root = prepareTestModelRoot(model.data(), tmp); + QVERIFY(root.isValid()); + + QFETCH(Qt::SortOrder, ascending); + QFETCH(int, count); + model->sort(0, ascending); + + QSignalSpy spy0(model.data(), &QAbstractItemModel::rowsInserted); + QSignalSpy spy1(model.data(), &QAbstractItemModel::rowsAboutToBeInserted); + int oldCount = model->rowCount(root); + QStringList files; + for (int i = 0; i < count; ++i) + files.append(QLatin1Char('c') + QString::number(i)); + QVERIFY(createFiles(model.data(), tmp, files, 5)); + QTRY_COMPARE(model->rowCount(root), oldCount + count); + int totalRowsInserted = 0; + for (int i = 0; i < spy0.count(); ++i) { + int start = spy0[i].value(1).toInt(); + int end = spy0[i].value(2).toInt(); + totalRowsInserted += end - start + 1; + } + QCOMPARE(totalRowsInserted, count); + const QString expected = ascending == Qt::AscendingOrder ? QStringLiteral("j") : QStringLiteral("b"); + QTRY_COMPARE(lastEntry(root), expected); + + if (spy0.count() > 0) { + if (count == 0) + QCOMPARE(spy0.count(), 0); + else + QVERIFY(spy0.count() >= 1); + } + if (count == 0) QCOMPARE(spy1.count(), 0); else QVERIFY(spy1.count() >= 1); + + QVERIFY(createFiles(model.data(), tmp, QStringList(".hidden_file"), 5 + count)); + + if (count != 0) + QTRY_VERIFY(spy0.count() >= 1); + else + QTRY_COMPARE(spy0.count(), 0); + if (count != 0) + QTRY_VERIFY(spy1.count() >= 1); + else + QTRY_COMPARE(spy1.count(), 0); +} + +void tst_QFileSystemModel::rowsRemoved_data() +{ + rowsInserted_data(); +} + +void tst_QFileSystemModel::rowsRemoved() +{ + const QString tmp = flatDirTestPath; + QScopedPointer model(new QFileSystemModel); + QModelIndex root = prepareTestModelRoot(model.data(), tmp); + QVERIFY(root.isValid()); + + QFETCH(int, count); + QFETCH(Qt::SortOrder, ascending); + model->sort(0, ascending); + + QSignalSpy spy0(model.data(), &QAbstractItemModel::rowsRemoved); + QSignalSpy spy1(model.data(), &QAbstractItemModel::rowsAboutToBeRemoved); + int oldCount = model->rowCount(root); + for (int i = count - 1; i >= 0; --i) { + const QString fileName = model->index(i, 0, root).data().toString(); + qCDebug(lcFileSystemModel) << "removing" << fileName; + QVERIFY(QFile::remove(tmp + QLatin1Char('/') + fileName)); + } + for (int i = 0 ; i < 10; ++i) { + if (count != 0) { + if (i == 10 || spy0.count() != 0) { + QVERIFY(spy0.count() >= 1); + QVERIFY(spy1.count() >= 1); + } + } else { + if (i == 10 || spy0.count() == 0) { + QCOMPARE(spy0.count(), 0); + QCOMPARE(spy1.count(), 0); + } + } + QStringList lst; + for (int i = 0; i < model->rowCount(root); ++i) + lst.append(model->index(i, 0, root).data().toString()); + if (model->rowCount(root) == oldCount - count) + break; + qCDebug(lcFileSystemModel) << "still have:" << lst << QFile::exists(tmp + QLatin1String("/.a")); + QDir tmpLister(tmp); + qCDebug(lcFileSystemModel) << tmpLister.entryList(); + } + QTRY_COMPARE(model->rowCount(root), oldCount - count); + + QVERIFY(QFile::exists(tmp + QLatin1String("/.a"))); + QVERIFY(QFile::remove(tmp + QLatin1String("/.a"))); + QVERIFY(QFile::remove(tmp + QLatin1String("/.c"))); + + if (count != 0) { + QVERIFY(spy0.count() >= 1); + QVERIFY(spy1.count() >= 1); + } else { + QCOMPARE(spy0.count(), 0); + QCOMPARE(spy1.count(), 0); + } +} + +void tst_QFileSystemModel::dataChanged_data() +{ + rowsInserted_data(); +} + +void tst_QFileSystemModel::dataChanged() +{ + QSKIP("This can't be tested right now since we don't watch files, only directories."); + + const QString tmp = flatDirTestPath; + QScopedPointer model(new QFileSystemModel); + QModelIndex root = prepareTestModelRoot(model.data(), tmp); + QVERIFY(root.isValid()); + + QFETCH(int, count); + QFETCH(Qt::SortOrder, ascending); + model->sort(0, ascending); + + QSignalSpy spy(model.data(), &QAbstractItemModel::dataChanged); + QStringList files; + for (int i = 0; i < count; ++i) + files.append(model->index(i, 0, root).data().toString()); + createFiles(model.data(), tmp, files); + + QTest::qWait(WAITTIME); + + if (count != 0) QVERIFY(spy.count() >= 1); else QCOMPARE(spy.count(), 0); +} + +void tst_QFileSystemModel::filters_data() +{ + QTest::addColumn("files"); + QTest::addColumn("dirs"); + QTest::addColumn("dirFilters"); + QTest::addColumn("nameFilters"); + QTest::addColumn("rowCount"); + + const QStringList abcList{QLatin1String("a"), QLatin1String("b"), QLatin1String("c")}; + const QStringList zList{QLatin1String("Z")}; + + QTest::newRow("no dirs") << abcList << QStringList() << QDir::Filters(QDir::Dirs) << QStringList() << 2; + QTest::newRow("no dirs - dot") << abcList << QStringList() << (QDir::Dirs | QDir::NoDot) << QStringList() << 1; + QTest::newRow("no dirs - dotdot") << abcList << QStringList() << (QDir::Dirs | QDir::NoDotDot) << QStringList() << 1; + QTest::newRow("no dirs - dotanddotdot") << abcList << QStringList() << (QDir::Dirs | QDir::NoDotAndDotDot) << QStringList() << 0; + QTest::newRow("one dir - dot") << abcList << zList << (QDir::Dirs | QDir::NoDot) << QStringList() << 2; + QTest::newRow("one dir - dotdot") << abcList << zList << (QDir::Dirs | QDir::NoDotDot) << QStringList() << 2; + QTest::newRow("one dir - dotanddotdot") << abcList << zList << (QDir::Dirs | QDir::NoDotAndDotDot) << QStringList() << 1; + QTest::newRow("one dir") << abcList << zList << QDir::Filters(QDir::Dirs) << QStringList() << 3; + QTest::newRow("no dir + hidden") << abcList << QStringList() << (QDir::Dirs | QDir::Hidden) << QStringList() << 2; + QTest::newRow("dir+hid+files") << abcList << QStringList() << + (QDir::Dirs | QDir::Files | QDir::Hidden) << QStringList() << 5; + QTest::newRow("dir+file+hid-dot .A") << abcList << QStringList{QLatin1String(".A")} << + (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << QStringList() << 4; + QTest::newRow("dir+files+hid+dot A") << abcList << QStringList{QLatin1String("AFolder")} << + (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << QStringList{QLatin1String("A*")} << 2; + QTest::newRow("dir+files+hid+dot+cas1") << abcList << zList << + (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << zList << 1; + QTest::newRow("dir+files+hid+dot+cas2") << abcList << zList << + (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << QStringList{QLatin1String("a")} << 1; + QTest::newRow("dir+files+hid+dot+cas+alldir") << abcList << zList << + (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive | QDir::AllDirs) << zList << 1; + + QTest::newRow("case sensitive") << QStringList{QLatin1String("Antiguagdb"), QLatin1String("Antiguamtd"), + QLatin1String("Antiguamtp"), QLatin1String("afghanistangdb"), QLatin1String("afghanistanmtd")} + << QStringList() << QDir::Filters(QDir::Files) << QStringList() << 5; +} + +void tst_QFileSystemModel::filters() +{ + QString tmp = flatDirTestPath; + QScopedPointer model(new QFileSystemModel); + QVERIFY(createFiles(model.data(), tmp, QStringList())); + QModelIndex root = model->setRootPath(tmp); + QFETCH(QStringList, files); + QFETCH(QStringList, dirs); + QFETCH(QDir::Filters, dirFilters); + QFETCH(QStringList, nameFilters); + QFETCH(int, rowCount); + + if (nameFilters.count() > 0) + model->setNameFilters(nameFilters); + model->setNameFilterDisables(false); + model->setFilter(dirFilters); + + QVERIFY(createFiles(model.data(), tmp, files, 0, dirs)); + QTRY_COMPARE(model->rowCount(root), rowCount); + + // Make sure that we do what QDir does + QDir xFactor(tmp); + QStringList dirEntries; + + if (nameFilters.count() > 0) + dirEntries = xFactor.entryList(nameFilters, dirFilters); + else + dirEntries = xFactor.entryList(dirFilters); + + QCOMPARE(dirEntries.count(), rowCount); + + QStringList modelEntries; + + for (int i = 0; i < rowCount; ++i) + modelEntries.append(model->data(model->index(i, 0, root), QFileSystemModel::FileNameRole).toString()); + + std::sort(dirEntries.begin(), dirEntries.end()); + std::sort(modelEntries.begin(), modelEntries.end()); + QCOMPARE(dirEntries, modelEntries); + +#ifdef Q_OS_LINUX + if (files.count() >= 3 && rowCount >= 3 && rowCount != 5) { + QString fileName1 = (tmp + '/' + files.at(0)); + QString fileName2 = (tmp + '/' + files.at(1)); + QString fileName3 = (tmp + '/' + files.at(2)); + QFile::Permissions originalPermissions = QFile::permissions(fileName1); + QVERIFY(QFile::setPermissions(fileName1, QFile::WriteOwner)); + QVERIFY(QFile::setPermissions(fileName2, QFile::ReadOwner)); + QVERIFY(QFile::setPermissions(fileName3, QFile::ExeOwner)); + + model->setFilter((QDir::Files | QDir::Readable)); + QTRY_COMPARE(model->rowCount(root), 1); + + model->setFilter((QDir::Files | QDir::Writable)); + QTRY_COMPARE(model->rowCount(root), 1); + + model->setFilter((QDir::Files | QDir::Executable)); + QTRY_COMPARE(model->rowCount(root), 1); + + // reset permissions + QVERIFY(QFile::setPermissions(fileName1, originalPermissions)); + QVERIFY(QFile::setPermissions(fileName2, originalPermissions)); + QVERIFY(QFile::setPermissions(fileName3, originalPermissions)); + } +#endif +} + +void tst_QFileSystemModel::nameFilters() +{ + QStringList list; + list << "a" << "b" << "c"; + QScopedPointer model(new QFileSystemModel); + model->setNameFilters(list); + model->setNameFilterDisables(false); + QCOMPARE(model->nameFilters(), list); + + QString tmp = flatDirTestPath; + QVERIFY(createFiles(model.data(), tmp, list)); + QModelIndex root = model->setRootPath(tmp); + QTRY_COMPARE(model->rowCount(root), 3); + + QStringList filters; + filters << "a" << "b"; + model->setNameFilters(filters); + QTRY_COMPARE(model->rowCount(root), 2); +} +void tst_QFileSystemModel::setData_data() +{ + QTest::addColumn("subdirName"); + QTest::addColumn("files"); + QTest::addColumn("oldFileName"); + QTest::addColumn("newFileName"); + QTest::addColumn("success"); + /*QTest::newRow("outside current dir") << (QStringList() << "a" << "b" << "c") + << flatDirTestPath + '/' + "a" + << QDir::temp().absolutePath() + '/' + "a" + << false; + */ + + const QStringList abcList{QLatin1String("a"), QLatin1String("b"), QLatin1String("c")}; + QTest::newRow("in current dir") + << QString() + << abcList + << "a" + << "d" + << true; + QTest::newRow("in subdir") + << "s" + << abcList + << "a" + << "d" + << true; +} + +void tst_QFileSystemModel::setData() +{ + QScopedPointer model(new QFileSystemModel); + QSignalSpy spy(model.data(), &QFileSystemModel::fileRenamed); + QFETCH(QString, subdirName); + QFETCH(QStringList, files); + QFETCH(QString, oldFileName); + QFETCH(QString, newFileName); + QFETCH(bool, success); + + QString tmp = flatDirTestPath; + if (!subdirName.isEmpty()) { + QDir dir(tmp); + QVERIFY(dir.mkdir(subdirName)); + tmp.append('/' + subdirName); + } + QVERIFY(createFiles(model.data(), tmp, files)); + QModelIndex tmpIdx = model->setRootPath(flatDirTestPath); + if (!subdirName.isEmpty()) { + tmpIdx = model->index(tmp); + model->fetchMore(tmpIdx); + } + QTRY_COMPARE(model->rowCount(tmpIdx), files.count()); + + QModelIndex idx = model->index(tmp + '/' + oldFileName); + QCOMPARE(idx.isValid(), true); + QCOMPARE(model->setData(idx, newFileName), false); + + model->setReadOnly(false); + QCOMPARE(model->setData(idx, newFileName), success); + model->setReadOnly(true); + if (success) { + QCOMPARE(spy.count(), 1); + QList arguments = spy.takeFirst(); + QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName); + QCOMPARE(model->fileInfo(idx).filePath(), tmp + '/' + newFileName); + QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp)); + QCOMPARE(arguments.at(1).toString(), oldFileName); + QCOMPARE(arguments.at(2).toString(), newFileName); + QCOMPARE(QFile::rename(tmp + '/' + newFileName, tmp + '/' + oldFileName), true); + } + QTRY_COMPARE(model->rowCount(tmpIdx), files.count()); + // cleanup + if (!subdirName.isEmpty()) + QVERIFY(QDir(tmp).removeRecursively()); +} + +void tst_QFileSystemModel::sortPersistentIndex() +{ + QTemporaryFile file(flatDirTestPath + QStringLiteral("/XXXXXX.dat")); + QVERIFY2(file.open(), qPrintable(file.errorString())); + const QFileInfo fileInfo(file.fileName()); + file.close(); + QTRY_VERIFY(QDir(flatDirTestPath).entryInfoList().contains(fileInfo)); + QScopedPointer model(new QFileSystemModel); + QModelIndex root = model->setRootPath(flatDirTestPath); + QTRY_VERIFY(model->rowCount(root) > 0); + + QPersistentModelIndex idx = model->index(0, 1, root); + model->sort(0, Qt::AscendingOrder); + model->sort(0, Qt::DescendingOrder); + QVERIFY(idx.column() != 0); +} + +class MyFriendFileSystemModel : public QFileSystemModel +{ + friend class tst_QFileSystemModel; + Q_DECLARE_PRIVATE(QFileSystemModel) +}; + +void tst_QFileSystemModel::sort_data() +{ + QTest::addColumn("fileDialogMode"); + QTest::newRow("standard usage") << false; + QTest::newRow("QFileDialog usage") << true; +} + +void tst_QFileSystemModel::sort() +{ + QFETCH(bool, fileDialogMode); + + QScopedPointer myModel(new MyFriendFileSystemModel); + QTreeView tree; + tree.setWindowTitle(QTest::currentTestFunction()); + + if (fileDialogMode && EmulationDetector::isRunningArmOnX86()) + QSKIP("Crashes in QEMU. QTBUG-70572"); + +#ifdef QT_BUILD_INTERNAL + if (fileDialogMode) + myModel->d_func()->disableRecursiveSort = true; +#endif + + QDir dir(flatDirTestPath); + const QString dirPath = dir.absolutePath(); + + //Create a file that will be at the end when sorting by name (For Mac, the default) + //but if we sort by size descending it will be the first + QFile tempFile(dirPath + "/plop2.txt"); + tempFile.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream out(&tempFile); + out << "The magic number is: " << 49 << "\n"; + tempFile.close(); + + QFile tempFile2(dirPath + "/plop.txt"); + tempFile2.open(QIODevice::WriteOnly | QIODevice::Text); + QTextStream out2(&tempFile2); + out2 << "The magic number is : " << 49 << " but i write some stuff in the file \n"; + tempFile2.close(); + + myModel->setRootPath(""); + myModel->setFilter(QDir::AllEntries | QDir::System | QDir::Hidden); + tree.setSortingEnabled(true); + tree.setModel(myModel.data()); + tree.show(); + tree.resize(800, 800); + QVERIFY(QTest::qWaitForWindowExposed(&tree)); + tree.header()->setSortIndicator(1, Qt::DescendingOrder); + tree.header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); + QStringList dirsToOpen; + do { + dirsToOpen << dir.absolutePath(); + } while (dir.cdUp()); + + for (int i = dirsToOpen.size() -1 ; i > 0 ; --i) { + QString path = dirsToOpen[i]; + tree.expand(myModel->index(path, 0)); + } + tree.expand(myModel->index(dirPath, 0)); + QModelIndex parent = myModel->index(dirPath, 0); + QList expectedOrder; + expectedOrder << tempFile2.fileName() << tempFile.fileName() << dirPath + QChar('/') + ".." << dirPath + QChar('/') + "."; + + if (fileDialogMode) { + QTRY_COMPARE(myModel->rowCount(parent), expectedOrder.count()); + // File dialog Mode means sub trees are not sorted, only the current root. + // There's no way we can check that the sub tree is "not sorted"; just check if it + // has the same contents of the expected list + QList actualRows; + for(int i = 0; i < myModel->rowCount(parent); ++i) + { + actualRows << dirPath + QChar('/') + myModel->index(i, 1, parent).data(QFileSystemModel::FileNameRole).toString(); + } + + std::sort(expectedOrder.begin(), expectedOrder.end()); + std::sort(actualRows.begin(), actualRows.end()); + + QCOMPARE(actualRows, expectedOrder); + } else { + for(int i = 0; i < myModel->rowCount(parent); ++i) + { + QTRY_COMPARE(dirPath + QChar('/') + myModel->index(i, 1, parent).data(QFileSystemModel::FileNameRole).toString(), expectedOrder.at(i)); + } + } +} + +void tst_QFileSystemModel::mkdir() +{ + QString tmp = flatDirTestPath; + QString newFolderPath = QDir::toNativeSeparators(tmp + '/' + "NewFoldermkdirtest4"); + QScopedPointer model(new QFileSystemModel); + QModelIndex tmpDir = model->index(tmp); + QVERIFY(tmpDir.isValid()); + QDir bestatic(newFolderPath); + if (bestatic.exists()) { + if (!bestatic.rmdir(newFolderPath)) + qWarning() << "unable to remove" << newFolderPath; + QTest::qWait(WAITTIME); + } + model->mkdir(tmpDir, "NewFoldermkdirtest3"); + model->mkdir(tmpDir, "NewFoldermkdirtest5"); + QModelIndex idx = model->mkdir(tmpDir, "NewFoldermkdirtest4"); + QVERIFY(idx.isValid()); + int oldRow = idx.row(); + idx = model->index(newFolderPath); + QVERIFY(idx.isValid()); + QVERIFY(model->remove(idx)); + QVERIFY(!bestatic.exists()); + QVERIFY(0 != idx.row()); + QCOMPARE(oldRow, idx.row()); +} + +void tst_QFileSystemModel::deleteFile() +{ + QString newFilePath = QDir::temp().filePath("NewFileDeleteTest"); + QFile newFile(newFilePath); + if (newFile.exists()) { + if (!newFile.remove()) + qWarning() << "unable to remove" << newFilePath; + QTest::qWait(WAITTIME); + } + if (!newFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + qWarning() << "unable to create" << newFilePath; + } + newFile.close(); + QScopedPointer model(new QFileSystemModel); + QModelIndex idx = model->index(newFilePath); + QVERIFY(idx.isValid()); + QVERIFY(model->remove(idx)); + QVERIFY(!newFile.exists()); +} + +void tst_QFileSystemModel::deleteDirectory() +{ + // QTBUG-65683: Verify that directories can be removed recursively despite + // file system watchers being active on them or their sub-directories (Windows). + // Create a temporary directory, a nested directory and expand a treeview + // to show them to ensure watcher creation. Then delete the directory. + QTemporaryDir dirToBeDeleted(flatDirTestPath + QStringLiteral("/deleteDirectory-XXXXXX")); + QVERIFY(dirToBeDeleted.isValid()); + const QString dirToBeDeletedPath = dirToBeDeleted.path(); + const QString nestedTestDir = QStringLiteral("test"); + QVERIFY(QDir(dirToBeDeletedPath).mkpath(nestedTestDir)); + const QString nestedTestDirPath = dirToBeDeletedPath + QLatin1Char('/') + nestedTestDir; + QFile testFile(nestedTestDirPath + QStringLiteral("/test.txt")); + QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Text)); + testFile.write("Hello\n"); + testFile.close(); + + QFileSystemModel model; + const QModelIndex rootIndex = model.setRootPath(flatDirTestPath); + QTreeView treeView; + treeView.setWindowTitle(QTest::currentTestFunction()); + treeView.setModel(&model); + treeView.setRootIndex(rootIndex); + + const QModelIndex dirToBeDeletedPathIndex = model.index(dirToBeDeletedPath); + QVERIFY(dirToBeDeletedPathIndex.isValid()); + treeView.setExpanded(dirToBeDeletedPathIndex, true); + const QModelIndex nestedTestDirIndex = model.index(nestedTestDirPath); + QVERIFY(nestedTestDirIndex.isValid()); + treeView.setExpanded(nestedTestDirIndex, true); + + treeView.show(); + QVERIFY(QTest::qWaitForWindowExposed(&treeView)); + + QVERIFY(model.remove(dirToBeDeletedPathIndex)); + dirToBeDeleted.setAutoRemove(false); +} + +static QString flipCase(QString s) +{ + for (int i = 0, size = s.size(); i < size; ++i) { + const QChar c = s.at(i); + if (c.isUpper()) + s[i] = c.toLower(); + else if (c.isLower()) + s[i] = c.toUpper(); + } + return s; +} + +void tst_QFileSystemModel::caseSensitivity() +{ + QString tmp = flatDirTestPath; + QStringList files; + files << "a" << "c" << "C"; + QScopedPointer model(new QFileSystemModel); + QVERIFY(createFiles(model.data(), tmp, files)); + QModelIndex root = model->index(tmp); + QStringList paths; + QModelIndexList indexes; + QCOMPARE(model->rowCount(root), 0); + for (int i = 0; i < files.count(); ++i) { + const QString path = tmp + '/' + files.at(i); + const QModelIndex index = model->index(path); + QVERIFY(index.isValid()); + paths.append(path); + indexes.append(index); + } + + if (!QFileSystemEngine::isCaseSensitive()) { + // QTBUG-31103, QTBUG-64147: Verify that files can be accessed by paths with fLipPeD case. + for (int i = 0; i < paths.count(); ++i) { + const QModelIndex flippedCaseIndex = model->index(flipCase(paths.at(i))); + QCOMPARE(indexes.at(i), flippedCaseIndex); + } + } +} + +void tst_QFileSystemModel::drives_data() +{ + QTest::addColumn("path"); + QTest::newRow("current") << QDir::currentPath(); + QTest::newRow("slash") << "/"; + QTest::newRow("My Computer") << "My Computer"; +} + +void tst_QFileSystemModel::drives() +{ + QFETCH(QString, path); + QFileSystemModel model; + model.setRootPath(path); + model.fetchMore(QModelIndex()); + QFileInfoList drives = QDir::drives(); + int driveCount = 0; + foreach(const QFileInfo& driveRoot, drives) + if (driveRoot.exists()) + driveCount++; + QTRY_COMPARE(model.rowCount(), driveCount); +} + +void tst_QFileSystemModel::dirsBeforeFiles() +{ + auto diagnosticMsg = [](int row, const QFileInfo &left, const QFileInfo &right) -> QByteArray { + QString message; + QDebug(&message).noquote() << "Unexpected sort order at #" << row << ':' << left << right; + return message.toLocal8Bit(); + }; + QTemporaryDir testDir(flatDirTestPath); + QVERIFY2(testDir.isValid(), qPrintable(testDir.errorString())); + QDir dir(testDir.path()); + + const int itemCount = 3; + for (int i = 0; i < itemCount; ++i) { + QLatin1Char c('a' + char(i)); + QVERIFY(dir.mkdir(c + QLatin1String("-dir"))); + QFile file(dir.filePath(c + QLatin1String("-file"))); + QVERIFY(file.open(QIODevice::ReadWrite)); + file.close(); + } + + QScopedPointer model(new QFileSystemModel); + QModelIndex root = model->setRootPath(dir.absolutePath()); + // Wait for model to be notified by the file system watcher + QTRY_COMPARE(model->rowCount(root), 2 * itemCount); + // sort explicitly - dirs before files (except on macOS), and then by name + model->sort(0); + // Ensure that no file occurs before any directory (see QFileSystemModelSorter): + for (int i = 1, count = model->rowCount(root); i < count; ++i) { + const QFileInfo previous = model->fileInfo(model->index(i - 1, 0, root)); + const QFileInfo current = model->fileInfo(model->index(i, 0, root)); +#ifndef Q_OS_MAC + QVERIFY2(!(previous.isFile() && current.isDir()), diagnosticMsg(i, previous, current).constData()); +#else + QVERIFY2(previous.fileName() < current.fileName(), diagnosticMsg(i, previous, current).constData()); +#endif + } +} + +void tst_QFileSystemModel::roleNames_data() +{ + QTest::addColumn("role"); + QTest::addColumn("roleName"); + QTest::newRow("decoration") << int(Qt::DecorationRole) << QByteArray("fileIcon"); + QTest::newRow("display") << int(Qt::DisplayRole) << QByteArray("display"); + QTest::newRow("fileIcon") << int(QFileSystemModel::FileIconRole) << QByteArray("fileIcon"); + QTest::newRow("filePath") << int(QFileSystemModel::FilePathRole) << QByteArray("filePath"); + QTest::newRow("fileName") << int(QFileSystemModel::FileNameRole) << QByteArray("fileName"); + QTest::newRow("filePermissions") << int(QFileSystemModel::FilePermissions) << QByteArray("filePermissions"); +} + +void tst_QFileSystemModel::roleNames() +{ + QFileSystemModel model; + QHash roles = model.roleNames(); + + QFETCH(int, role); + QVERIFY(roles.contains(role)); + + QFETCH(QByteArray, roleName); + QCOMPARE(roles.contains(role), true); + QCOMPARE(roles.value(role), roleName); +} + +static inline QByteArray permissionRowName(bool readOnly, int permission) +{ + QByteArray result = readOnly ? QByteArrayLiteral("ro") : QByteArrayLiteral("rw"); + result += QByteArrayLiteral("-0"); + result += QByteArray::number(permission, 16); + return result; +} + +void tst_QFileSystemModel::permissions_data() +{ + QTest::addColumn("permissions"); + QTest::addColumn("readOnly"); + + static const int permissions[] = { + QFile::WriteOwner, + QFile::ReadOwner, + QFile::WriteOwner|QFile::ReadOwner, + }; + for (int permission : permissions) { + QTest::newRow(permissionRowName(false, permission).constData()) << QFileDevice::Permissions(permission) << false; + QTest::newRow(permissionRowName(true, permission).constData()) << QFileDevice::Permissions(permission) << true; + } +} + +void tst_QFileSystemModel::permissions() // checks QTBUG-20503 +{ + QFETCH(QFileDevice::Permissions, permissions); + QFETCH(bool, readOnly); + + const QString tmp = flatDirTestPath; + const QString file = tmp + QLatin1String("/f"); + QScopedPointer model(new QFileSystemModel); + QVERIFY(createFiles(model.data(), tmp, QStringList{QLatin1String("f")})); + + QVERIFY(QFile::setPermissions(file, permissions)); + + const QModelIndex root = model->setRootPath(tmp); + + model->setReadOnly(readOnly); + + QCOMPARE(model->isReadOnly(), readOnly); + + QTRY_COMPARE(model->rowCount(root), 1); + + const QFile::Permissions modelPermissions = model->permissions(model->index(0, 0, root)); + const QFile::Permissions modelFileInfoPermissions = model->fileInfo(model->index(0, 0, root)).permissions(); + const QFile::Permissions fileInfoPermissions = QFileInfo(file).permissions(); + + QCOMPARE(modelPermissions, modelFileInfoPermissions); + QCOMPARE(modelFileInfoPermissions, fileInfoPermissions); + QCOMPARE(fileInfoPermissions, modelPermissions); +} + +void tst_QFileSystemModel::doNotUnwatchOnFailedRmdir() +{ + const QString tmp = flatDirTestPath; + + QFileSystemModel model; + + const QTemporaryDir tempDir(tmp + '/' + QStringLiteral("doNotUnwatchOnFailedRmdir-XXXXXX")); + QVERIFY(tempDir.isValid()); + + const QModelIndex rootIndex = model.setRootPath(tempDir.path()); + + // create a file in the directory so to prevent it from deletion + { + QFile file(tempDir.path() + '/' + QStringLiteral("file1")); + QVERIFY(file.open(QIODevice::WriteOnly)); + } + + QCOMPARE(model.rmdir(rootIndex), false); + + // create another file + { + QFile file(tempDir.path() + '/' + QStringLiteral("file2")); + QVERIFY(file.open(QIODevice::WriteOnly)); + } + + // the model must now detect this second file + QTRY_COMPARE(model.rowCount(rootIndex), 2); +} + +static QSet fileListUnderIndex(const QFileSystemModel *model, const QModelIndex &parent) +{ + QSet fileNames; + const int rowCount = model->rowCount(parent); + for (int i = 0; i < rowCount; ++i) + fileNames.insert(model->index(i, 0, parent).data(QFileSystemModel::FileNameRole).toString()); + return fileNames; +} + +void tst_QFileSystemModel::specialFiles() +{ +#ifndef Q_OS_UNIX + QSKIP("Not implemented"); +#endif + + QFileSystemModel model; + + model.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden); + + // Can't simply verify if the model returns a valid model index for a special file + // as it will always return a valid index for existing files, + // even if the file is not visible with the given filter. + + const QModelIndex rootIndex = model.setRootPath(QStringLiteral("/dev/")); + const QString testFileName = QStringLiteral("null"); + + QTRY_VERIFY(fileListUnderIndex(&model, rootIndex).contains(testFileName)); + + model.setFilter(QDir::AllEntries | QDir::Hidden); + + QTRY_VERIFY(!fileListUnderIndex(&model, rootIndex).contains(testFileName)); +} + +void tst_QFileSystemModel::fileInfo() +{ + QFileSystemModel model; + QModelIndex idx; + + QVERIFY(model.fileInfo(idx).filePath().isEmpty()); + + const QString dirPath = flatDirTestPath; + QDir dir(dirPath); + const QString subdir = QStringLiteral("subdir"); + QVERIFY(dir.mkdir(subdir)); + const QString subdirPath = dir.absoluteFilePath(subdir); + + idx = model.setRootPath(subdirPath); + QCOMPARE(model.fileInfo(idx), QFileInfo(subdirPath)); + idx = model.setRootPath(dirPath); + QCOMPARE(model.fileInfo(idx), QFileInfo(dirPath)); +} + +QTEST_MAIN(tst_QFileSystemModel) +#include "tst_qfilesystemmodel.moc" + diff --git a/tests/auto/widgets/dialogs/CMakeLists.txt b/tests/auto/widgets/dialogs/CMakeLists.txt index 0c52076efe..13c4e6cad8 100644 --- a/tests/auto/widgets/dialogs/CMakeLists.txt +++ b/tests/auto/widgets/dialogs/CMakeLists.txt @@ -10,9 +10,6 @@ add_subdirectory(qinputdialog) add_subdirectory(qmessagebox) add_subdirectory(qprogressdialog) add_subdirectory(qwizard) -if(NOT MINGW) - add_subdirectory(qfilesystemmodel) -endif() if(QT_FEATURE_private_tests) add_subdirectory(qsidebar) endif() diff --git a/tests/auto/widgets/dialogs/dialogs.pro b/tests/auto/widgets/dialogs/dialogs.pro index cf548f2dea..b4217a219d 100644 --- a/tests/auto/widgets/dialogs/dialogs.pro +++ b/tests/auto/widgets/dialogs/dialogs.pro @@ -5,7 +5,6 @@ SUBDIRS=\ qerrormessage \ qfiledialog \ qfiledialog2 \ - qfilesystemmodel \ qfontdialog \ qinputdialog \ qmessagebox \ @@ -17,4 +16,4 @@ SUBDIRS=\ qsidebar \ mac:qinputdialog.CONFIG += no_check_target # QTBUG-25496 -mingw: SUBDIRS -= qfilesystemmodel # QTBUG-29403 + diff --git a/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp b/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp index e6130c56b1..cbb1c72b11 100644 --- a/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp +++ b/tests/auto/widgets/dialogs/qfiledialog2/tst_qfiledialog2.cpp @@ -49,7 +49,7 @@ #include #include #include "../../../../../src/widgets/dialogs/qsidebar_p.h" -#include "../../../../../src/widgets/dialogs/qfilesystemmodel_p.h" +#include "../../../../../src/gui/itemmodels/qfilesystemmodel_p.h" #include "../../../../../src/widgets/dialogs/qfiledialog_p.h" #include diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/.gitignore b/tests/auto/widgets/dialogs/qfilesystemmodel/.gitignore deleted file mode 100644 index 9804e5a3d7..0000000000 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/.gitignore +++ /dev/null @@ -1 +0,0 @@ -tst_qfilesystemmodel diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/BLACKLIST b/tests/auto/widgets/dialogs/qfilesystemmodel/BLACKLIST deleted file mode 100644 index 4119afce84..0000000000 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/BLACKLIST +++ /dev/null @@ -1,5 +0,0 @@ -[sort:QFileDialog usage] -ubuntu -b2qt -[specialFiles] -b2qt diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/CMakeLists.txt b/tests/auto/widgets/dialogs/qfilesystemmodel/CMakeLists.txt deleted file mode 100644 index 595b30bd56..0000000000 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -# Generated from qfilesystemmodel.pro. - -##################################################################### -## tst_qfilesystemmodel Test: -##################################################################### - -qt_add_test(tst_qfilesystemmodel - SOURCES - ../../../../shared/emulationdetector.h - tst_qfilesystemmodel.cpp - INCLUDE_DIRECTORIES - ../../../../shared - PUBLIC_LIBRARIES - Qt::CorePrivate - Qt::Gui - Qt::Widgets - Qt::WidgetsPrivate -) - -## Scopes: -##################################################################### - -#### Keys ignored in scope 2:.:.:qfilesystemmodel.pro:WIN32: -# testcase.timeout = "900" - -#### Keys ignored in scope 3:.:.:qfilesystemmodel.pro:MACOS: -# testcase.timeout = "900" diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/qfilesystemmodel.pro b/tests/auto/widgets/dialogs/qfilesystemmodel/qfilesystemmodel.pro deleted file mode 100644 index db8cf7de3f..0000000000 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/qfilesystemmodel.pro +++ /dev/null @@ -1,13 +0,0 @@ -INCLUDEPATH += ../../../../shared -HEADERS += ../../../../shared/emulationdetector.h - -CONFIG += testcase -# This testcase can be slow on Windows and OS X, and may interfere with other file system tests. -win32:testcase.timeout = 900 -macx:testcase.timeout = 900 - -QT += widgets widgets-private -QT += core-private testlib - -SOURCES += tst_qfilesystemmodel.cpp -TARGET = tst_qfilesystemmodel diff --git a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp b/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp deleted file mode 100644 index e814e79dbe..0000000000 --- a/tests/auto/widgets/dialogs/qfilesystemmodel/tst_qfilesystemmodel.cpp +++ /dev/null @@ -1,1207 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** 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-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include -#include -#ifdef QT_BUILD_INTERNAL -#include -#endif -#include -#include -#include -#include -#include -#include -#include -#include -#include -#if defined(Q_OS_WIN) -# include // for SetFileAttributes -#endif -#include - -#include - -#define WAITTIME 1000 - -// Will try to wait for the condition while allowing event processing -// for a maximum of 5 seconds. -#define TRY_WAIT(expr, timedOut) \ - do { \ - *timedOut = true; \ - const int step = 50; \ - for (int __i = 0; __i < 5000; __i += step) { \ - if (expr) { \ - *timedOut = false; \ - break; \ - } \ - QTest::qWait(step); \ - } \ - } while(0) - -Q_DECLARE_METATYPE(QDir::Filters) -Q_DECLARE_METATYPE(QFileDevice::Permissions) - -Q_LOGGING_CATEGORY(lcFileSystemModel, "qt.widgets.tests.qfilesystemmodel") - -class tst_QFileSystemModel : public QObject { - Q_OBJECT - -private slots: - void initTestCase(); - void cleanup(); - - void indexPath(); - - void rootPath(); - void readOnly(); - void iconProvider(); - - void rowCount(); - - void rowsInserted_data(); - void rowsInserted(); - - void rowsRemoved_data(); - void rowsRemoved(); - - void dataChanged_data(); - void dataChanged(); - - void filters_data(); - void filters(); - - void nameFilters(); - - void setData_data(); - void setData(); - - void sortPersistentIndex(); - void sort_data(); - void sort(); - - void mkdir(); - void deleteFile(); - void deleteDirectory(); - - void caseSensitivity(); - - void drives_data(); - void drives(); - void dirsBeforeFiles(); - - void roleNames_data(); - void roleNames(); - - void permissions_data(); - void permissions(); - - void doNotUnwatchOnFailedRmdir(); - void specialFiles(); - - void fileInfo(); - -protected: - bool createFiles(QFileSystemModel *model, const QString &test_path, - const QStringList &initial_files, int existingFileCount = 0, - const QStringList &initial_dirs = QStringList()); - QModelIndex prepareTestModelRoot(QFileSystemModel *model, const QString &test_path, - QSignalSpy **spy2 = nullptr, QSignalSpy **spy3 = nullptr); - -private: - QString flatDirTestPath; - QTemporaryDir m_tempDir; -}; - -void tst_QFileSystemModel::cleanup() -{ - QDir dir(flatDirTestPath); - if (dir.exists()) { - const QDir::Filters filters = QDir::AllEntries | QDir::System | QDir::Hidden | QDir::NoDotAndDotDot; - const QFileInfoList list = dir.entryInfoList(filters); - for (const QFileInfo &fi : list) { - if (fi.isDir()) { - QVERIFY(dir.rmdir(fi.fileName())); - } else { - QFile dead(fi.absoluteFilePath()); - dead.setPermissions(QFile::ReadUser | QFile::ReadOwner | QFile::ExeOwner | QFile::ExeUser | QFile::WriteUser | QFile::WriteOwner | QFile::WriteOther); - QVERIFY(dead.remove()); - } - } - QVERIFY(dir.entryInfoList(filters).isEmpty()); - } -} - -void tst_QFileSystemModel::initTestCase() -{ - QVERIFY2(m_tempDir.isValid(), qPrintable(m_tempDir.errorString())); - flatDirTestPath = m_tempDir.path(); -} - -void tst_QFileSystemModel::indexPath() -{ -#if !defined(Q_OS_WIN) - QScopedPointer model(new QFileSystemModel); - int depth = QDir::currentPath().count('/'); - model->setRootPath(QDir::currentPath()); - QString backPath; - for (int i = 0; i <= depth * 2 + 1; ++i) { - backPath += "../"; - QModelIndex idx = model->index(backPath); - QVERIFY(i != depth - 1 ? idx.isValid() : !idx.isValid()); - } -#endif -} - -void tst_QFileSystemModel::rootPath() -{ - QScopedPointer model(new QFileSystemModel); - QCOMPARE(model->rootPath(), QString(QDir().path())); - - QSignalSpy rootChanged(model.data(), &QFileSystemModel::rootPathChanged); - QModelIndex root = model->setRootPath(model->rootPath()); - root = model->setRootPath("this directory shouldn't exist"); - QCOMPARE(rootChanged.count(), 0); - - QString oldRootPath = model->rootPath(); - const QStringList documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); - QVERIFY(!documentPaths.isEmpty()); - QString documentPath = documentPaths.front(); - // In particular on Linux, ~/Documents (the first - // DocumentsLocation) may not exist, so choose ~ in that case: - if (!QFile::exists(documentPath)) { - documentPath = QDir::homePath(); - qWarning("%s: first documentPath \"%s\" does not exist. Using ~ (\"%s\") instead.", - Q_FUNC_INFO, qPrintable(documentPaths.front()), qPrintable(documentPath)); - } - root = model->setRootPath(documentPath); - - QTRY_VERIFY(model->rowCount(root) >= 0); - QCOMPARE(model->rootPath(), QString(documentPath)); - QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? 0 : 1); - QCOMPARE(model->rootDirectory().absolutePath(), documentPath); - - model->setRootPath(QDir::rootPath()); - int oldCount = rootChanged.count(); - oldRootPath = model->rootPath(); - root = model->setRootPath(documentPath + QLatin1String("/.")); - QTRY_VERIFY(model->rowCount(root) >= 0); - QCOMPARE(model->rootPath(), documentPath); - QCOMPARE(rootChanged.count(), oldRootPath == model->rootPath() ? oldCount : oldCount + 1); - QCOMPARE(model->rootDirectory().absolutePath(), documentPath); - - QDir newdir = documentPath; - if (newdir.cdUp()) { - oldCount = rootChanged.count(); - oldRootPath = model->rootPath(); - root = model->setRootPath(documentPath + QLatin1String("/..")); - QTRY_VERIFY(model->rowCount(root) >= 0); - QCOMPARE(model->rootPath(), newdir.path()); - QCOMPARE(rootChanged.count(), oldCount + 1); - QCOMPARE(model->rootDirectory().absolutePath(), newdir.path()); - } -} - -void tst_QFileSystemModel::readOnly() -{ - QScopedPointer model(new QFileSystemModel); - QCOMPARE(model->isReadOnly(), true); - QTemporaryFile file(flatDirTestPath + QStringLiteral("/XXXXXX.dat")); - QVERIFY2(file.open(), qPrintable(file.errorString())); - const QString fileName = file.fileName(); - file.close(); - - const QFileInfo fileInfo(fileName); - QTRY_VERIFY(QDir(flatDirTestPath).entryInfoList().contains(fileInfo)); - QModelIndex root = model->setRootPath(flatDirTestPath); - - QTRY_VERIFY(model->rowCount(root) > 0); - QVERIFY(!(model->flags(model->index(fileName)) & Qt::ItemIsEditable)); - model->setReadOnly(false); - QCOMPARE(model->isReadOnly(), false); - QVERIFY(model->flags(model->index(fileName)) & Qt::ItemIsEditable); -} - -class CustomFileIconProvider : public QFileIconProvider -{ -public: - CustomFileIconProvider() : QFileIconProvider() - { - auto style = QApplication::style(); - mb = style->standardIcon(QStyle::SP_MessageBoxCritical); - dvd = style->standardIcon(QStyle::SP_DriveDVDIcon); - } - - QIcon icon(const QFileInfo &info) const override - { - if (info.isDir()) - return mb; - - return QFileIconProvider::icon(info); - } - QIcon icon(IconType type) const override - { - if (type == QFileIconProvider::Folder) - return dvd; - - return QFileIconProvider::icon(type); - } -private: - QIcon mb; - QIcon dvd; -}; - -void tst_QFileSystemModel::iconProvider() -{ - QScopedPointer model(new QFileSystemModel); - QVERIFY(model->iconProvider()); - QScopedPointer provider(new QFileIconProvider); - model->setIconProvider(provider.data()); - QCOMPARE(model->iconProvider(), provider.data()); - model->setIconProvider(nullptr); - provider.reset(); - - QScopedPointer myModel(new QFileSystemModel); - const QStringList documentPaths = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); - QVERIFY(!documentPaths.isEmpty()); - myModel->setRootPath(documentPaths.constFirst()); - //We change the provider, icons must be updated - provider.reset(new CustomFileIconProvider); - myModel->setIconProvider(provider.data()); - - QPixmap mb = QApplication::style()->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(50, 50); - QCOMPARE(myModel->fileIcon(myModel->index(QDir::homePath())).pixmap(50, 50), mb); -} - -bool tst_QFileSystemModel::createFiles(QFileSystemModel *model, const QString &test_path, - const QStringList &initial_files, int existingFileCount, - const QStringList &initial_dirs) -{ - qCDebug(lcFileSystemModel) << (model->rowCount(model->index(test_path))) << existingFileCount << initial_files; - bool timedOut = false; - TRY_WAIT((model->rowCount(model->index(test_path)) == existingFileCount), &timedOut); - if (timedOut) - return false; - - QDir dir(test_path); - if (!dir.exists()) { - qWarning() << "error" << test_path << "doesn't exist"; - return false; - } - for (const auto &initial_dir : initial_dirs) { - if (!dir.mkdir(initial_dir)) { - qWarning() << "error" << "failed to make" << initial_dir; - return false; - } - qCDebug(lcFileSystemModel) << test_path + '/' + initial_dir << (QFile::exists(test_path + '/' + initial_dir)); - } - for (const auto &initial_file : initial_files) { - QFile file(test_path + '/' + initial_file); - if (!file.open(QIODevice::WriteOnly | QIODevice::Append)) { - qDebug() << "failed to open file" << initial_file; - return false; - } - if (!file.resize(1024 + file.size())) { - qDebug() << "failed to resize file" << initial_file; - return false; - } - if (!file.flush()) { - qDebug() << "failed to flush file" << initial_file; - return false; - } - file.close(); -#if defined(Q_OS_WIN) - if (initial_file[0] == '.') { - const QString hiddenFile = QDir::toNativeSeparators(file.fileName()); - const auto nativeHiddenFile = reinterpret_cast(hiddenFile.utf16()); - DWORD currentAttributes = ::GetFileAttributes(nativeHiddenFile); - if (currentAttributes == 0xFFFFFFFF) { - qErrnoWarning("failed to get file attributes: %s", qPrintable(hiddenFile)); - return false; - } - if (!::SetFileAttributes(nativeHiddenFile, currentAttributes | FILE_ATTRIBUTE_HIDDEN)) { - qErrnoWarning("failed to set file hidden: %s", qPrintable(hiddenFile)); - return false; - } - } -#endif - qCDebug(lcFileSystemModel) << test_path + '/' + initial_file << (QFile::exists(test_path + '/' + initial_file)); - } - return true; -} - -QModelIndex tst_QFileSystemModel::prepareTestModelRoot(QFileSystemModel *model, const QString &test_path, - QSignalSpy **spy2, QSignalSpy **spy3) -{ - if (model->rowCount(model->index(test_path)) != 0) - return QModelIndex(); - - if (spy2) - *spy2 = new QSignalSpy(model, &QFileSystemModel::rowsInserted); - if (spy3) - *spy3 = new QSignalSpy(model, &QFileSystemModel::rowsAboutToBeInserted); - - QStringList files = { "b", "d", "f", "h", "j", ".a", ".c", ".e", ".g" }; - - if (!createFiles(model, test_path, files)) - return QModelIndex(); - - QModelIndex root = model->setRootPath(test_path); - if (!root.isValid()) - return QModelIndex(); - - bool timedOut = false; - TRY_WAIT(model->rowCount(root) == 5, &timedOut); - if (timedOut) - return QModelIndex(); - - return root; -} - -void tst_QFileSystemModel::rowCount() -{ - QSignalSpy *spy2 = nullptr; - QSignalSpy *spy3 = nullptr; - QScopedPointer model(new QFileSystemModel); - QModelIndex root = prepareTestModelRoot(model.data(), flatDirTestPath, &spy2, &spy3); - QVERIFY(root.isValid()); - - QVERIFY(spy2 && spy2->count() > 0); - QVERIFY(spy3 && spy3->count() > 0); -} - -void tst_QFileSystemModel::rowsInserted_data() -{ - QTest::addColumn("count"); - QTest::addColumn("ascending"); - for (int i = 0; i < 4; ++i) { - const QByteArray iB = QByteArray::number(i); - QTest::newRow(("Qt::AscendingOrder " + iB).constData()) << i << Qt::AscendingOrder; - QTest::newRow(("Qt::DescendingOrder " + iB).constData()) << i << Qt::DescendingOrder; - } -} - -static inline QString lastEntry(const QModelIndex &root) -{ - const QAbstractItemModel *model = root.model(); - return model->index(model->rowCount(root) - 1, 0, root).data().toString(); -} - -void tst_QFileSystemModel::rowsInserted() -{ - const QString tmp = flatDirTestPath; - QScopedPointer model(new QFileSystemModel); - QModelIndex root = prepareTestModelRoot(model.data(), tmp); - QVERIFY(root.isValid()); - - QFETCH(Qt::SortOrder, ascending); - QFETCH(int, count); - model->sort(0, ascending); - - QSignalSpy spy0(model.data(), &QAbstractItemModel::rowsInserted); - QSignalSpy spy1(model.data(), &QAbstractItemModel::rowsAboutToBeInserted); - int oldCount = model->rowCount(root); - QStringList files; - for (int i = 0; i < count; ++i) - files.append(QLatin1Char('c') + QString::number(i)); - QVERIFY(createFiles(model.data(), tmp, files, 5)); - QTRY_COMPARE(model->rowCount(root), oldCount + count); - int totalRowsInserted = 0; - for (int i = 0; i < spy0.count(); ++i) { - int start = spy0[i].value(1).toInt(); - int end = spy0[i].value(2).toInt(); - totalRowsInserted += end - start + 1; - } - QCOMPARE(totalRowsInserted, count); - const QString expected = ascending == Qt::AscendingOrder ? QStringLiteral("j") : QStringLiteral("b"); - QTRY_COMPARE(lastEntry(root), expected); - - if (spy0.count() > 0) { - if (count == 0) - QCOMPARE(spy0.count(), 0); - else - QVERIFY(spy0.count() >= 1); - } - if (count == 0) QCOMPARE(spy1.count(), 0); else QVERIFY(spy1.count() >= 1); - - QVERIFY(createFiles(model.data(), tmp, QStringList(".hidden_file"), 5 + count)); - - if (count != 0) - QTRY_VERIFY(spy0.count() >= 1); - else - QTRY_COMPARE(spy0.count(), 0); - if (count != 0) - QTRY_VERIFY(spy1.count() >= 1); - else - QTRY_COMPARE(spy1.count(), 0); -} - -void tst_QFileSystemModel::rowsRemoved_data() -{ - rowsInserted_data(); -} - -void tst_QFileSystemModel::rowsRemoved() -{ - const QString tmp = flatDirTestPath; - QScopedPointer model(new QFileSystemModel); - QModelIndex root = prepareTestModelRoot(model.data(), tmp); - QVERIFY(root.isValid()); - - QFETCH(int, count); - QFETCH(Qt::SortOrder, ascending); - model->sort(0, ascending); - - QSignalSpy spy0(model.data(), &QAbstractItemModel::rowsRemoved); - QSignalSpy spy1(model.data(), &QAbstractItemModel::rowsAboutToBeRemoved); - int oldCount = model->rowCount(root); - for (int i = count - 1; i >= 0; --i) { - const QString fileName = model->index(i, 0, root).data().toString(); - qCDebug(lcFileSystemModel) << "removing" << fileName; - QVERIFY(QFile::remove(tmp + QLatin1Char('/') + fileName)); - } - for (int i = 0 ; i < 10; ++i) { - if (count != 0) { - if (i == 10 || spy0.count() != 0) { - QVERIFY(spy0.count() >= 1); - QVERIFY(spy1.count() >= 1); - } - } else { - if (i == 10 || spy0.count() == 0) { - QCOMPARE(spy0.count(), 0); - QCOMPARE(spy1.count(), 0); - } - } - QStringList lst; - for (int i = 0; i < model->rowCount(root); ++i) - lst.append(model->index(i, 0, root).data().toString()); - if (model->rowCount(root) == oldCount - count) - break; - qCDebug(lcFileSystemModel) << "still have:" << lst << QFile::exists(tmp + QLatin1String("/.a")); - QDir tmpLister(tmp); - qCDebug(lcFileSystemModel) << tmpLister.entryList(); - } - QTRY_COMPARE(model->rowCount(root), oldCount - count); - - QVERIFY(QFile::exists(tmp + QLatin1String("/.a"))); - QVERIFY(QFile::remove(tmp + QLatin1String("/.a"))); - QVERIFY(QFile::remove(tmp + QLatin1String("/.c"))); - - if (count != 0) { - QVERIFY(spy0.count() >= 1); - QVERIFY(spy1.count() >= 1); - } else { - QCOMPARE(spy0.count(), 0); - QCOMPARE(spy1.count(), 0); - } -} - -void tst_QFileSystemModel::dataChanged_data() -{ - rowsInserted_data(); -} - -void tst_QFileSystemModel::dataChanged() -{ - QSKIP("This can't be tested right now since we don't watch files, only directories."); - - const QString tmp = flatDirTestPath; - QScopedPointer model(new QFileSystemModel); - QModelIndex root = prepareTestModelRoot(model.data(), tmp); - QVERIFY(root.isValid()); - - QFETCH(int, count); - QFETCH(Qt::SortOrder, ascending); - model->sort(0, ascending); - - QSignalSpy spy(model.data(), &QAbstractItemModel::dataChanged); - QStringList files; - for (int i = 0; i < count; ++i) - files.append(model->index(i, 0, root).data().toString()); - createFiles(model.data(), tmp, files); - - QTest::qWait(WAITTIME); - - if (count != 0) QVERIFY(spy.count() >= 1); else QCOMPARE(spy.count(), 0); -} - -void tst_QFileSystemModel::filters_data() -{ - QTest::addColumn("files"); - QTest::addColumn("dirs"); - QTest::addColumn("dirFilters"); - QTest::addColumn("nameFilters"); - QTest::addColumn("rowCount"); - - const QStringList abcList{QLatin1String("a"), QLatin1String("b"), QLatin1String("c")}; - const QStringList zList{QLatin1String("Z")}; - - QTest::newRow("no dirs") << abcList << QStringList() << QDir::Filters(QDir::Dirs) << QStringList() << 2; - QTest::newRow("no dirs - dot") << abcList << QStringList() << (QDir::Dirs | QDir::NoDot) << QStringList() << 1; - QTest::newRow("no dirs - dotdot") << abcList << QStringList() << (QDir::Dirs | QDir::NoDotDot) << QStringList() << 1; - QTest::newRow("no dirs - dotanddotdot") << abcList << QStringList() << (QDir::Dirs | QDir::NoDotAndDotDot) << QStringList() << 0; - QTest::newRow("one dir - dot") << abcList << zList << (QDir::Dirs | QDir::NoDot) << QStringList() << 2; - QTest::newRow("one dir - dotdot") << abcList << zList << (QDir::Dirs | QDir::NoDotDot) << QStringList() << 2; - QTest::newRow("one dir - dotanddotdot") << abcList << zList << (QDir::Dirs | QDir::NoDotAndDotDot) << QStringList() << 1; - QTest::newRow("one dir") << abcList << zList << QDir::Filters(QDir::Dirs) << QStringList() << 3; - QTest::newRow("no dir + hidden") << abcList << QStringList() << (QDir::Dirs | QDir::Hidden) << QStringList() << 2; - QTest::newRow("dir+hid+files") << abcList << QStringList() << - (QDir::Dirs | QDir::Files | QDir::Hidden) << QStringList() << 5; - QTest::newRow("dir+file+hid-dot .A") << abcList << QStringList{QLatin1String(".A")} << - (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << QStringList() << 4; - QTest::newRow("dir+files+hid+dot A") << abcList << QStringList{QLatin1String("AFolder")} << - (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot) << QStringList{QLatin1String("A*")} << 2; - QTest::newRow("dir+files+hid+dot+cas1") << abcList << zList << - (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << zList << 1; - QTest::newRow("dir+files+hid+dot+cas2") << abcList << zList << - (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive) << QStringList{QLatin1String("a")} << 1; - QTest::newRow("dir+files+hid+dot+cas+alldir") << abcList << zList << - (QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::CaseSensitive | QDir::AllDirs) << zList << 1; - - QTest::newRow("case sensitive") << QStringList{QLatin1String("Antiguagdb"), QLatin1String("Antiguamtd"), - QLatin1String("Antiguamtp"), QLatin1String("afghanistangdb"), QLatin1String("afghanistanmtd")} - << QStringList() << QDir::Filters(QDir::Files) << QStringList() << 5; -} - -void tst_QFileSystemModel::filters() -{ - QString tmp = flatDirTestPath; - QScopedPointer model(new QFileSystemModel); - QVERIFY(createFiles(model.data(), tmp, QStringList())); - QModelIndex root = model->setRootPath(tmp); - QFETCH(QStringList, files); - QFETCH(QStringList, dirs); - QFETCH(QDir::Filters, dirFilters); - QFETCH(QStringList, nameFilters); - QFETCH(int, rowCount); - - if (nameFilters.count() > 0) - model->setNameFilters(nameFilters); - model->setNameFilterDisables(false); - model->setFilter(dirFilters); - - QVERIFY(createFiles(model.data(), tmp, files, 0, dirs)); - QTRY_COMPARE(model->rowCount(root), rowCount); - - // Make sure that we do what QDir does - QDir xFactor(tmp); - QStringList dirEntries; - - if (nameFilters.count() > 0) - dirEntries = xFactor.entryList(nameFilters, dirFilters); - else - dirEntries = xFactor.entryList(dirFilters); - - QCOMPARE(dirEntries.count(), rowCount); - - QStringList modelEntries; - - for (int i = 0; i < rowCount; ++i) - modelEntries.append(model->data(model->index(i, 0, root), QFileSystemModel::FileNameRole).toString()); - - std::sort(dirEntries.begin(), dirEntries.end()); - std::sort(modelEntries.begin(), modelEntries.end()); - QCOMPARE(dirEntries, modelEntries); - -#ifdef Q_OS_LINUX - if (files.count() >= 3 && rowCount >= 3 && rowCount != 5) { - QString fileName1 = (tmp + '/' + files.at(0)); - QString fileName2 = (tmp + '/' + files.at(1)); - QString fileName3 = (tmp + '/' + files.at(2)); - QFile::Permissions originalPermissions = QFile::permissions(fileName1); - QVERIFY(QFile::setPermissions(fileName1, QFile::WriteOwner)); - QVERIFY(QFile::setPermissions(fileName2, QFile::ReadOwner)); - QVERIFY(QFile::setPermissions(fileName3, QFile::ExeOwner)); - - model->setFilter((QDir::Files | QDir::Readable)); - QTRY_COMPARE(model->rowCount(root), 1); - - model->setFilter((QDir::Files | QDir::Writable)); - QTRY_COMPARE(model->rowCount(root), 1); - - model->setFilter((QDir::Files | QDir::Executable)); - QTRY_COMPARE(model->rowCount(root), 1); - - // reset permissions - QVERIFY(QFile::setPermissions(fileName1, originalPermissions)); - QVERIFY(QFile::setPermissions(fileName2, originalPermissions)); - QVERIFY(QFile::setPermissions(fileName3, originalPermissions)); - } -#endif -} - -void tst_QFileSystemModel::nameFilters() -{ - QStringList list; - list << "a" << "b" << "c"; - QScopedPointer model(new QFileSystemModel); - model->setNameFilters(list); - model->setNameFilterDisables(false); - QCOMPARE(model->nameFilters(), list); - - QString tmp = flatDirTestPath; - QVERIFY(createFiles(model.data(), tmp, list)); - QModelIndex root = model->setRootPath(tmp); - QTRY_COMPARE(model->rowCount(root), 3); - - QStringList filters; - filters << "a" << "b"; - model->setNameFilters(filters); - QTRY_COMPARE(model->rowCount(root), 2); -} -void tst_QFileSystemModel::setData_data() -{ - QTest::addColumn("subdirName"); - QTest::addColumn("files"); - QTest::addColumn("oldFileName"); - QTest::addColumn("newFileName"); - QTest::addColumn("success"); - /*QTest::newRow("outside current dir") << (QStringList() << "a" << "b" << "c") - << flatDirTestPath + '/' + "a" - << QDir::temp().absolutePath() + '/' + "a" - << false; - */ - - const QStringList abcList{QLatin1String("a"), QLatin1String("b"), QLatin1String("c")}; - QTest::newRow("in current dir") - << QString() - << abcList - << "a" - << "d" - << true; - QTest::newRow("in subdir") - << "s" - << abcList - << "a" - << "d" - << true; -} - -void tst_QFileSystemModel::setData() -{ - QScopedPointer model(new QFileSystemModel); - QSignalSpy spy(model.data(), &QFileSystemModel::fileRenamed); - QFETCH(QString, subdirName); - QFETCH(QStringList, files); - QFETCH(QString, oldFileName); - QFETCH(QString, newFileName); - QFETCH(bool, success); - - QString tmp = flatDirTestPath; - if (!subdirName.isEmpty()) { - QDir dir(tmp); - QVERIFY(dir.mkdir(subdirName)); - tmp.append('/' + subdirName); - } - QVERIFY(createFiles(model.data(), tmp, files)); - QModelIndex tmpIdx = model->setRootPath(flatDirTestPath); - if (!subdirName.isEmpty()) { - tmpIdx = model->index(tmp); - model->fetchMore(tmpIdx); - } - QTRY_COMPARE(model->rowCount(tmpIdx), files.count()); - - QModelIndex idx = model->index(tmp + '/' + oldFileName); - QCOMPARE(idx.isValid(), true); - QCOMPARE(model->setData(idx, newFileName), false); - - model->setReadOnly(false); - QCOMPARE(model->setData(idx, newFileName), success); - model->setReadOnly(true); - if (success) { - QCOMPARE(spy.count(), 1); - QList arguments = spy.takeFirst(); - QCOMPARE(model->data(idx, QFileSystemModel::FileNameRole).toString(), newFileName); - QCOMPARE(model->fileInfo(idx).filePath(), tmp + '/' + newFileName); - QCOMPARE(model->index(arguments.at(0).toString()), model->index(tmp)); - QCOMPARE(arguments.at(1).toString(), oldFileName); - QCOMPARE(arguments.at(2).toString(), newFileName); - QCOMPARE(QFile::rename(tmp + '/' + newFileName, tmp + '/' + oldFileName), true); - } - QTRY_COMPARE(model->rowCount(tmpIdx), files.count()); - // cleanup - if (!subdirName.isEmpty()) - QVERIFY(QDir(tmp).removeRecursively()); -} - -void tst_QFileSystemModel::sortPersistentIndex() -{ - QTemporaryFile file(flatDirTestPath + QStringLiteral("/XXXXXX.dat")); - QVERIFY2(file.open(), qPrintable(file.errorString())); - const QFileInfo fileInfo(file.fileName()); - file.close(); - QTRY_VERIFY(QDir(flatDirTestPath).entryInfoList().contains(fileInfo)); - QScopedPointer model(new QFileSystemModel); - QModelIndex root = model->setRootPath(flatDirTestPath); - QTRY_VERIFY(model->rowCount(root) > 0); - - QPersistentModelIndex idx = model->index(0, 1, root); - model->sort(0, Qt::AscendingOrder); - model->sort(0, Qt::DescendingOrder); - QVERIFY(idx.column() != 0); -} - -class MyFriendFileSystemModel : public QFileSystemModel -{ - friend class tst_QFileSystemModel; - Q_DECLARE_PRIVATE(QFileSystemModel) -}; - -void tst_QFileSystemModel::sort_data() -{ - QTest::addColumn("fileDialogMode"); - QTest::newRow("standard usage") << false; - QTest::newRow("QFileDialog usage") << true; -} - -void tst_QFileSystemModel::sort() -{ - QFETCH(bool, fileDialogMode); - - QScopedPointer myModel(new MyFriendFileSystemModel); - QTreeView tree; - tree.setWindowTitle(QTest::currentTestFunction()); - - if (fileDialogMode && EmulationDetector::isRunningArmOnX86()) - QSKIP("Crashes in QEMU. QTBUG-70572"); - -#ifdef QT_BUILD_INTERNAL - if (fileDialogMode) - myModel->d_func()->disableRecursiveSort = true; -#endif - - QDir dir(flatDirTestPath); - const QString dirPath = dir.absolutePath(); - - //Create a file that will be at the end when sorting by name (For Mac, the default) - //but if we sort by size descending it will be the first - QFile tempFile(dirPath + "/plop2.txt"); - tempFile.open(QIODevice::WriteOnly | QIODevice::Text); - QTextStream out(&tempFile); - out << "The magic number is: " << 49 << "\n"; - tempFile.close(); - - QFile tempFile2(dirPath + "/plop.txt"); - tempFile2.open(QIODevice::WriteOnly | QIODevice::Text); - QTextStream out2(&tempFile2); - out2 << "The magic number is : " << 49 << " but i write some stuff in the file \n"; - tempFile2.close(); - - myModel->setRootPath(""); - myModel->setFilter(QDir::AllEntries | QDir::System | QDir::Hidden); - tree.setSortingEnabled(true); - tree.setModel(myModel.data()); - tree.show(); - tree.resize(800, 800); - QVERIFY(QTest::qWaitForWindowExposed(&tree)); - tree.header()->setSortIndicator(1, Qt::DescendingOrder); - tree.header()->setSectionResizeMode(0, QHeaderView::ResizeToContents); - QStringList dirsToOpen; - do { - dirsToOpen << dir.absolutePath(); - } while (dir.cdUp()); - - for (int i = dirsToOpen.size() -1 ; i > 0 ; --i) { - QString path = dirsToOpen[i]; - tree.expand(myModel->index(path, 0)); - } - tree.expand(myModel->index(dirPath, 0)); - QModelIndex parent = myModel->index(dirPath, 0); - QList expectedOrder; - expectedOrder << tempFile2.fileName() << tempFile.fileName() << dirPath + QChar('/') + ".." << dirPath + QChar('/') + "."; - - if (fileDialogMode) { - QTRY_COMPARE(myModel->rowCount(parent), expectedOrder.count()); - // File dialog Mode means sub trees are not sorted, only the current root. - // There's no way we can check that the sub tree is "not sorted"; just check if it - // has the same contents of the expected list - QList actualRows; - for(int i = 0; i < myModel->rowCount(parent); ++i) - { - actualRows << dirPath + QChar('/') + myModel->index(i, 1, parent).data(QFileSystemModel::FileNameRole).toString(); - } - - std::sort(expectedOrder.begin(), expectedOrder.end()); - std::sort(actualRows.begin(), actualRows.end()); - - QCOMPARE(actualRows, expectedOrder); - } else { - for(int i = 0; i < myModel->rowCount(parent); ++i) - { - QTRY_COMPARE(dirPath + QChar('/') + myModel->index(i, 1, parent).data(QFileSystemModel::FileNameRole).toString(), expectedOrder.at(i)); - } - } -} - -void tst_QFileSystemModel::mkdir() -{ - QString tmp = flatDirTestPath; - QString newFolderPath = QDir::toNativeSeparators(tmp + '/' + "NewFoldermkdirtest4"); - QScopedPointer model(new QFileSystemModel); - QModelIndex tmpDir = model->index(tmp); - QVERIFY(tmpDir.isValid()); - QDir bestatic(newFolderPath); - if (bestatic.exists()) { - if (!bestatic.rmdir(newFolderPath)) - qWarning() << "unable to remove" << newFolderPath; - QTest::qWait(WAITTIME); - } - model->mkdir(tmpDir, "NewFoldermkdirtest3"); - model->mkdir(tmpDir, "NewFoldermkdirtest5"); - QModelIndex idx = model->mkdir(tmpDir, "NewFoldermkdirtest4"); - QVERIFY(idx.isValid()); - int oldRow = idx.row(); - idx = model->index(newFolderPath); - QVERIFY(idx.isValid()); - QVERIFY(model->remove(idx)); - QVERIFY(!bestatic.exists()); - QVERIFY(0 != idx.row()); - QCOMPARE(oldRow, idx.row()); -} - -void tst_QFileSystemModel::deleteFile() -{ - QString newFilePath = QDir::temp().filePath("NewFileDeleteTest"); - QFile newFile(newFilePath); - if (newFile.exists()) { - if (!newFile.remove()) - qWarning() << "unable to remove" << newFilePath; - QTest::qWait(WAITTIME); - } - if (!newFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - qWarning() << "unable to create" << newFilePath; - } - newFile.close(); - QScopedPointer model(new QFileSystemModel); - QModelIndex idx = model->index(newFilePath); - QVERIFY(idx.isValid()); - QVERIFY(model->remove(idx)); - QVERIFY(!newFile.exists()); -} - -void tst_QFileSystemModel::deleteDirectory() -{ - // QTBUG-65683: Verify that directories can be removed recursively despite - // file system watchers being active on them or their sub-directories (Windows). - // Create a temporary directory, a nested directory and expand a treeview - // to show them to ensure watcher creation. Then delete the directory. - QTemporaryDir dirToBeDeleted(flatDirTestPath + QStringLiteral("/deleteDirectory-XXXXXX")); - QVERIFY(dirToBeDeleted.isValid()); - const QString dirToBeDeletedPath = dirToBeDeleted.path(); - const QString nestedTestDir = QStringLiteral("test"); - QVERIFY(QDir(dirToBeDeletedPath).mkpath(nestedTestDir)); - const QString nestedTestDirPath = dirToBeDeletedPath + QLatin1Char('/') + nestedTestDir; - QFile testFile(nestedTestDirPath + QStringLiteral("/test.txt")); - QVERIFY(testFile.open(QIODevice::WriteOnly | QIODevice::Text)); - testFile.write("Hello\n"); - testFile.close(); - - QFileSystemModel model; - const QModelIndex rootIndex = model.setRootPath(flatDirTestPath); - QTreeView treeView; - treeView.setWindowTitle(QTest::currentTestFunction()); - treeView.setModel(&model); - treeView.setRootIndex(rootIndex); - - const QModelIndex dirToBeDeletedPathIndex = model.index(dirToBeDeletedPath); - QVERIFY(dirToBeDeletedPathIndex.isValid()); - treeView.setExpanded(dirToBeDeletedPathIndex, true); - const QModelIndex nestedTestDirIndex = model.index(nestedTestDirPath); - QVERIFY(nestedTestDirIndex.isValid()); - treeView.setExpanded(nestedTestDirIndex, true); - - treeView.show(); - QVERIFY(QTest::qWaitForWindowExposed(&treeView)); - - QVERIFY(model.remove(dirToBeDeletedPathIndex)); - dirToBeDeleted.setAutoRemove(false); -} - -static QString flipCase(QString s) -{ - for (int i = 0, size = s.size(); i < size; ++i) { - const QChar c = s.at(i); - if (c.isUpper()) - s[i] = c.toLower(); - else if (c.isLower()) - s[i] = c.toUpper(); - } - return s; -} - -void tst_QFileSystemModel::caseSensitivity() -{ - QString tmp = flatDirTestPath; - QStringList files; - files << "a" << "c" << "C"; - QScopedPointer model(new QFileSystemModel); - QVERIFY(createFiles(model.data(), tmp, files)); - QModelIndex root = model->index(tmp); - QStringList paths; - QModelIndexList indexes; - QCOMPARE(model->rowCount(root), 0); - for (int i = 0; i < files.count(); ++i) { - const QString path = tmp + '/' + files.at(i); - const QModelIndex index = model->index(path); - QVERIFY(index.isValid()); - paths.append(path); - indexes.append(index); - } - - if (!QFileSystemEngine::isCaseSensitive()) { - // QTBUG-31103, QTBUG-64147: Verify that files can be accessed by paths with fLipPeD case. - for (int i = 0; i < paths.count(); ++i) { - const QModelIndex flippedCaseIndex = model->index(flipCase(paths.at(i))); - QCOMPARE(indexes.at(i), flippedCaseIndex); - } - } -} - -void tst_QFileSystemModel::drives_data() -{ - QTest::addColumn("path"); - QTest::newRow("current") << QDir::currentPath(); - QTest::newRow("slash") << "/"; - QTest::newRow("My Computer") << "My Computer"; -} - -void tst_QFileSystemModel::drives() -{ - QFETCH(QString, path); - QFileSystemModel model; - model.setRootPath(path); - model.fetchMore(QModelIndex()); - QFileInfoList drives = QDir::drives(); - int driveCount = 0; - foreach(const QFileInfo& driveRoot, drives) - if (driveRoot.exists()) - driveCount++; - QTRY_COMPARE(model.rowCount(), driveCount); -} - -void tst_QFileSystemModel::dirsBeforeFiles() -{ - auto diagnosticMsg = [](int row, const QFileInfo &left, const QFileInfo &right) -> QByteArray { - QString message; - QDebug(&message).noquote() << "Unexpected sort order at #" << row << ':' << left << right; - return message.toLocal8Bit(); - }; - QTemporaryDir testDir(flatDirTestPath); - QVERIFY2(testDir.isValid(), qPrintable(testDir.errorString())); - QDir dir(testDir.path()); - - const int itemCount = 3; - for (int i = 0; i < itemCount; ++i) { - QLatin1Char c('a' + char(i)); - QVERIFY(dir.mkdir(c + QLatin1String("-dir"))); - QFile file(dir.filePath(c + QLatin1String("-file"))); - QVERIFY(file.open(QIODevice::ReadWrite)); - file.close(); - } - - QScopedPointer model(new QFileSystemModel); - QModelIndex root = model->setRootPath(dir.absolutePath()); - // Wait for model to be notified by the file system watcher - QTRY_COMPARE(model->rowCount(root), 2 * itemCount); - // sort explicitly - dirs before files (except on macOS), and then by name - model->sort(0); - // Ensure that no file occurs before any directory (see QFileSystemModelSorter): - for (int i = 1, count = model->rowCount(root); i < count; ++i) { - const QFileInfo previous = model->fileInfo(model->index(i - 1, 0, root)); - const QFileInfo current = model->fileInfo(model->index(i, 0, root)); -#ifndef Q_OS_MAC - QVERIFY2(!(previous.isFile() && current.isDir()), diagnosticMsg(i, previous, current).constData()); -#else - QVERIFY2(previous.fileName() < current.fileName(), diagnosticMsg(i, previous, current).constData()); -#endif - } -} - -void tst_QFileSystemModel::roleNames_data() -{ - QTest::addColumn("role"); - QTest::addColumn("roleName"); - QTest::newRow("decoration") << int(Qt::DecorationRole) << QByteArray("fileIcon"); - QTest::newRow("display") << int(Qt::DisplayRole) << QByteArray("display"); - QTest::newRow("fileIcon") << int(QFileSystemModel::FileIconRole) << QByteArray("fileIcon"); - QTest::newRow("filePath") << int(QFileSystemModel::FilePathRole) << QByteArray("filePath"); - QTest::newRow("fileName") << int(QFileSystemModel::FileNameRole) << QByteArray("fileName"); - QTest::newRow("filePermissions") << int(QFileSystemModel::FilePermissions) << QByteArray("filePermissions"); -} - -void tst_QFileSystemModel::roleNames() -{ - QFileSystemModel model; - QHash roles = model.roleNames(); - - QFETCH(int, role); - QVERIFY(roles.contains(role)); - - QFETCH(QByteArray, roleName); - QCOMPARE(roles.contains(role), true); - QCOMPARE(roles.value(role), roleName); -} - -static inline QByteArray permissionRowName(bool readOnly, int permission) -{ - QByteArray result = readOnly ? QByteArrayLiteral("ro") : QByteArrayLiteral("rw"); - result += QByteArrayLiteral("-0"); - result += QByteArray::number(permission, 16); - return result; -} - -void tst_QFileSystemModel::permissions_data() -{ - QTest::addColumn("permissions"); - QTest::addColumn("readOnly"); - - static const int permissions[] = { - QFile::WriteOwner, - QFile::ReadOwner, - QFile::WriteOwner|QFile::ReadOwner, - }; - for (int permission : permissions) { - QTest::newRow(permissionRowName(false, permission).constData()) << QFileDevice::Permissions(permission) << false; - QTest::newRow(permissionRowName(true, permission).constData()) << QFileDevice::Permissions(permission) << true; - } -} - -void tst_QFileSystemModel::permissions() // checks QTBUG-20503 -{ - QFETCH(QFileDevice::Permissions, permissions); - QFETCH(bool, readOnly); - - const QString tmp = flatDirTestPath; - const QString file = tmp + QLatin1String("/f"); - QScopedPointer model(new QFileSystemModel); - QVERIFY(createFiles(model.data(), tmp, QStringList{QLatin1String("f")})); - - QVERIFY(QFile::setPermissions(file, permissions)); - - const QModelIndex root = model->setRootPath(tmp); - - model->setReadOnly(readOnly); - - QCOMPARE(model->isReadOnly(), readOnly); - - QTRY_COMPARE(model->rowCount(root), 1); - - const QFile::Permissions modelPermissions = model->permissions(model->index(0, 0, root)); - const QFile::Permissions modelFileInfoPermissions = model->fileInfo(model->index(0, 0, root)).permissions(); - const QFile::Permissions fileInfoPermissions = QFileInfo(file).permissions(); - - QCOMPARE(modelPermissions, modelFileInfoPermissions); - QCOMPARE(modelFileInfoPermissions, fileInfoPermissions); - QCOMPARE(fileInfoPermissions, modelPermissions); -} - -void tst_QFileSystemModel::doNotUnwatchOnFailedRmdir() -{ - const QString tmp = flatDirTestPath; - - QFileSystemModel model; - - const QTemporaryDir tempDir(tmp + '/' + QStringLiteral("doNotUnwatchOnFailedRmdir-XXXXXX")); - QVERIFY(tempDir.isValid()); - - const QModelIndex rootIndex = model.setRootPath(tempDir.path()); - - // create a file in the directory so to prevent it from deletion - { - QFile file(tempDir.path() + '/' + QStringLiteral("file1")); - QVERIFY(file.open(QIODevice::WriteOnly)); - } - - QCOMPARE(model.rmdir(rootIndex), false); - - // create another file - { - QFile file(tempDir.path() + '/' + QStringLiteral("file2")); - QVERIFY(file.open(QIODevice::WriteOnly)); - } - - // the model must now detect this second file - QTRY_COMPARE(model.rowCount(rootIndex), 2); -} - -static QSet fileListUnderIndex(const QFileSystemModel *model, const QModelIndex &parent) -{ - QSet fileNames; - const int rowCount = model->rowCount(parent); - for (int i = 0; i < rowCount; ++i) - fileNames.insert(model->index(i, 0, parent).data(QFileSystemModel::FileNameRole).toString()); - return fileNames; -} - -void tst_QFileSystemModel::specialFiles() -{ -#ifndef Q_OS_UNIX - QSKIP("Not implemented"); -#endif - - QFileSystemModel model; - - model.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden); - - // Can't simply verify if the model returns a valid model index for a special file - // as it will always return a valid index for existing files, - // even if the file is not visible with the given filter. - - const QModelIndex rootIndex = model.setRootPath(QStringLiteral("/dev/")); - const QString testFileName = QStringLiteral("null"); - - QTRY_VERIFY(fileListUnderIndex(&model, rootIndex).contains(testFileName)); - - model.setFilter(QDir::AllEntries | QDir::Hidden); - - QTRY_VERIFY(!fileListUnderIndex(&model, rootIndex).contains(testFileName)); -} - -void tst_QFileSystemModel::fileInfo() -{ - QFileSystemModel model; - QModelIndex idx; - - QVERIFY(model.fileInfo(idx).filePath().isEmpty()); - - const QString dirPath = flatDirTestPath; - QDir dir(dirPath); - const QString subdir = QStringLiteral("subdir"); - QVERIFY(dir.mkdir(subdir)); - const QString subdirPath = dir.absoluteFilePath(subdir); - - idx = model.setRootPath(subdirPath); - QCOMPARE(model.fileInfo(idx), QFileInfo(subdirPath)); - idx = model.setRootPath(dirPath); - QCOMPARE(model.fileInfo(idx), QFileInfo(dirPath)); -} - -QTEST_MAIN(tst_QFileSystemModel) -#include "tst_qfilesystemmodel.moc" - diff --git a/tests/auto/widgets/dialogs/qsidebar/tst_qsidebar.cpp b/tests/auto/widgets/dialogs/qsidebar/tst_qsidebar.cpp index 146a9e262d..20866a0fa7 100644 --- a/tests/auto/widgets/dialogs/qsidebar/tst_qsidebar.cpp +++ b/tests/auto/widgets/dialogs/qsidebar/tst_qsidebar.cpp @@ -29,7 +29,8 @@ #include #include -#include +#include +#include class tst_QSidebar : public QObject { Q_OBJECT @@ -40,12 +41,16 @@ private slots: void addUrls(); void goToUrl(); + +private: + QFileIconProvider defaultIconProvider; }; void tst_QSidebar::setUrls() { QList urls; QFileSystemModel fsmodel; + fsmodel.setIconProvider(&defaultIconProvider); QSidebar qsidebar; qsidebar.setModelAndUrls(&fsmodel, urls); QAbstractItemModel *model = qsidebar.model(); @@ -67,6 +72,7 @@ void tst_QSidebar::selectUrls() urls << QUrl::fromLocalFile(QDir::rootPath()) << QUrl::fromLocalFile(QDir::temp().absolutePath()); QFileSystemModel fsmodel; + fsmodel.setIconProvider(&defaultIconProvider); QSidebar qsidebar; qsidebar.setModelAndUrls(&fsmodel, urls); @@ -79,6 +85,7 @@ void tst_QSidebar::addUrls() { QList emptyUrls; QFileSystemModel fsmodel; + fsmodel.setIconProvider(&defaultIconProvider); QSidebar qsidebar; qsidebar.setModelAndUrls(&fsmodel, emptyUrls); QAbstractItemModel *model = qsidebar.model(); @@ -179,6 +186,7 @@ void tst_QSidebar::goToUrl() urls << QUrl::fromLocalFile(QDir::rootPath()) << QUrl::fromLocalFile(QDir::temp().absolutePath()); QFileSystemModel fsmodel; + fsmodel.setIconProvider(&defaultIconProvider); QSidebar qsidebar; qsidebar.setModelAndUrls(&fsmodel, urls); qsidebar.show(); -- cgit v1.2.3