aboutsummaryrefslogtreecommitdiffstats
path: root/src/labs/folderlistmodel/fileinfothread_p.h
blob: 379c7a981dd242b2f3cbc65444976e1e7ecfb9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef FILEINFOTHREAD_P_H
#define FILEINFOTHREAD_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 <QThread>
#include <QMutex>
#include <QWaitCondition>
#if QT_CONFIG(filesystemwatcher)
#include <QFileSystemWatcher>
#endif
#include <QFileInfo>
#include <QDir>

#include "fileproperty_p.h"
#include "qquickfolderlistmodel_p.h"

QT_BEGIN_NAMESPACE

class FileInfoThread : public QThread
{
    Q_OBJECT

Q_SIGNALS:
    void directoryChanged(const QString &directory, const QList<FileProperty> &list) const;
    void directoryUpdated(const QString &directory, const QList<FileProperty> &list, int fromIndex, int toIndex) const;
    void sortFinished(const QList<FileProperty> &list) const;
    void statusChanged(QQuickFolderListModel::Status status) const;

public:
    FileInfoThread(QObject *parent = nullptr);
    ~FileInfoThread();

    void clear();
    void removePath(const QString &path);
    void setPath(const QString &path);
    void setRootPath(const QString &path);
    void setSortFlags(QDir::SortFlags flags);
    void setNameFilters(const QStringList & nameFilters);
    void setShowFiles(bool show);
    void setShowDirs(bool showFolders);
    void setShowDirsFirst(bool show);
    void setShowDotAndDotDot(bool on);
    void setShowHidden(bool on);
    void setShowOnlyReadable(bool on);
    void setCaseSensitive(bool on);

public Q_SLOTS:
#if QT_CONFIG(filesystemwatcher)
    void dirChanged(const QString &directoryPath);
    void updateFile(const QString &path);
#endif

protected:
    void run() override;
    void runOnce();
    void initiateScan();
    void getFileInfos(const QString &path);
    void findChangeRange(const QList<FileProperty> &list, int &fromIndex, int &toIndex);

private:
    enum class UpdateType {
        None = 1 << 0,
        // The order of the files in the current folder changed.
        Sort = 1 << 1,
        // A subset of files in the current folder changed.
        Contents = 1 << 2
    };
    Q_DECLARE_FLAGS(UpdateTypes, UpdateType)

    // Declare these ourselves, as Q_DECLARE_OPERATORS_FOR_FLAGS needs the enum to be public.
    friend constexpr UpdateTypes operator|(UpdateType f1, UpdateTypes f2) noexcept;
    friend constexpr UpdateTypes operator&(UpdateType f1, UpdateTypes f2) noexcept;

    QMutex mutex;
    QWaitCondition condition;
    volatile bool abort;
    bool scanPending;

#if QT_CONFIG(filesystemwatcher)
    QFileSystemWatcher *watcher;
#endif
    QList<FileProperty> currentFileList;
    QDir::SortFlags sortFlags;
    QString currentPath;
    QString rootPath;
    QStringList nameFilters;
    bool needUpdate;
    UpdateTypes updateTypes;
    bool showFiles;
    bool showDirs;
    bool showDirsFirst;
    bool showDotAndDotDot;
    bool showHidden;
    bool showOnlyReadable;
    bool caseSensitive;
};

QT_END_NAMESPACE

#endif // FILEINFOTHREAD_P_H