Integrated file selection dialogue with the main code. Improved the
[openvpnui.git] / src / filebrowse / filemodel.h
1 #ifndef FILEMODEL_H
2 #define FILEMODEL_H
3
4 #include <QAbstractListModel>
5 #include <QDir>
6 #include <QFileSystemWatcher>
7
8 // struct to hold data for a single file
9 struct FileData
10 {
11 QFileInfo info;
12
13 bool operator==(const FileData &other) const {
14 return other.info == info;
15 }
16 };
17
18 /**
19 * @brief The FileModel class can be used as a model in a ListView to display a list of files
20 * in the current directory. It has methods to change the current directory and to access
21 * file info.
22 * It also actively monitors the directory. If the directory changes, then the model is
23 * updated automatically if active is true. If active is false, then the directory is
24 * updated when active becomes true.
25 */
26 class FileModel : public QAbstractListModel
27 {
28 Q_OBJECT
29 Q_PROPERTY(QString dir READ dir() WRITE setDir(QString) NOTIFY dirChanged())
30 Q_PROPERTY(int fileCount READ fileCount() NOTIFY fileCountChanged())
31 Q_PROPERTY(QString errorMessage READ errorMessage() NOTIFY errorMessageChanged())
32 Q_PROPERTY(bool active READ active() WRITE setActive() NOTIFY activeChanged())
33 Q_PROPERTY(bool showAll READ showAll() WRITE setShowAll() NOTIFY showAllChanged())
34
35 public:
36 explicit FileModel(QObject *parent = 0);
37 ~FileModel();
38
39 // methods needed by ListView
40 int rowCount(const QModelIndex &parent = QModelIndex()) const;
41 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
42 QHash<int, QByteArray> roleNames() const;
43
44 // property accessors
45 QString dir() const { return m_dir; }
46 void setDir(QString dir);
47 int fileCount() const;
48 QString errorMessage() const;
49 bool active() const { return m_active; }
50 void setActive(bool active);
51 bool showAll() const { return m_showAll; }
52 void setShowAll(bool showAll);
53
54 // methods accessible from QML
55 Q_INVOKABLE QString appendPath(QString dirName);
56 Q_INVOKABLE QString parentPath();
57 Q_INVOKABLE QString fileNameAt(int fileIndex);
58
59 public slots:
60 // reads the directory and inserts/removes model items as needed
61 Q_INVOKABLE void refresh();
62 // reads the directory and sets all model items
63 Q_INVOKABLE void refreshFull();
64
65 signals:
66 void dirChanged();
67 void fileCountChanged();
68 void errorMessageChanged();
69 void activeChanged();
70 void showAllChanged();
71
72 private slots:
73 void readDirectory();
74
75 private:
76 void readEntries();
77 void refreshEntries();
78
79 QString m_dir;
80 QList<FileData> m_files;
81 QString m_errorMessage;
82 bool m_active;
83 bool m_dirty;
84 QFileSystemWatcher *m_watcher;
85 bool m_showAll;
86 };
87
88
89
90 #endif // FILEMODEL_H