X-Git-Url: https://www.flypig.org.uk/git/?p=openvpnui.git;a=blobdiff_plain;f=src%2Ffilebrowse%2Ffilemodel.h;fp=src%2Ffilebrowse%2Ffilemodel.h;h=0704d14e6cd7dc5dc19303924adf4527cbe4da22;hp=0000000000000000000000000000000000000000;hb=e24363e314aca32e7bee952f02f517a04a8dc5f2;hpb=ee3968ffa08d4e0fcbad87765efa3aeb32ff0554 diff --git a/src/filebrowse/filemodel.h b/src/filebrowse/filemodel.h new file mode 100644 index 0000000..0704d14 --- /dev/null +++ b/src/filebrowse/filemodel.h @@ -0,0 +1,90 @@ +#ifndef FILEMODEL_H +#define FILEMODEL_H + +#include +#include +#include + +// struct to hold data for a single file +struct FileData +{ + QFileInfo info; + + bool operator==(const FileData &other) const { + return other.info == info; + } +}; + +/** + * @brief The FileModel class can be used as a model in a ListView to display a list of files + * in the current directory. It has methods to change the current directory and to access + * file info. + * It also actively monitors the directory. If the directory changes, then the model is + * updated automatically if active is true. If active is false, then the directory is + * updated when active becomes true. + */ +class FileModel : public QAbstractListModel +{ + Q_OBJECT + Q_PROPERTY(QString dir READ dir() WRITE setDir(QString) NOTIFY dirChanged()) + Q_PROPERTY(int fileCount READ fileCount() NOTIFY fileCountChanged()) + Q_PROPERTY(QString errorMessage READ errorMessage() NOTIFY errorMessageChanged()) + Q_PROPERTY(bool active READ active() WRITE setActive() NOTIFY activeChanged()) + Q_PROPERTY(bool showAll READ showAll() WRITE setShowAll() NOTIFY showAllChanged()) + +public: + explicit FileModel(QObject *parent = 0); + ~FileModel(); + + // methods needed by ListView + int rowCount(const QModelIndex &parent = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QHash roleNames() const; + + // property accessors + QString dir() const { return m_dir; } + void setDir(QString dir); + int fileCount() const; + QString errorMessage() const; + bool active() const { return m_active; } + void setActive(bool active); + bool showAll() const { return m_showAll; } + void setShowAll(bool showAll); + + // methods accessible from QML + Q_INVOKABLE QString appendPath(QString dirName); + Q_INVOKABLE QString parentPath(); + Q_INVOKABLE QString fileNameAt(int fileIndex); + +public slots: + // reads the directory and inserts/removes model items as needed + Q_INVOKABLE void refresh(); + // reads the directory and sets all model items + Q_INVOKABLE void refreshFull(); + +signals: + void dirChanged(); + void fileCountChanged(); + void errorMessageChanged(); + void activeChanged(); + void showAllChanged(); + +private slots: + void readDirectory(); + +private: + void readEntries(); + void refreshEntries(); + + QString m_dir; + QList m_files; + QString m_errorMessage; + bool m_active; + bool m_dirty; + QFileSystemWatcher *m_watcher; + bool m_showAll; +}; + + + +#endif // FILEMODEL_H