passwordsmodel.h (2362B)
1 /* 2 * Copyright (C) 2018 Daniel Vrátil <dvratil@kde.org> 3 * 2021 Willy Goiffon <contact@z3bra.org> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU Library General Public License as 7 * published by the Free Software Foundation; either version 2, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details 14 * 15 * You should have received a copy of the GNU Library General Public 16 * License along with this program; if not, write to the 17 * Free Software Foundation, Inc., 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 21 #ifndef PASSWORDSMODEL_H_ 22 #define PASSWORDSMODEL_H_ 23 24 #include <QAbstractItemModel> 25 #include <QFileSystemWatcher> 26 #include <QDir> 27 28 class PasswordsModel : public QAbstractItemModel 29 { 30 Q_OBJECT 31 32 class Node; 33 public: 34 enum EntryType { 35 FolderEntry, 36 PasswordEntry 37 }; 38 Q_ENUM(EntryType) 39 40 enum Roles { 41 NameRole = Qt::DisplayRole, 42 EntryTypeRole = Qt::UserRole, 43 FullNameRole, 44 PathRole, 45 PasswordRole, 46 HasPasswordRole, 47 }; 48 49 explicit PasswordsModel(QObject *parent = nullptr); 50 ~PasswordsModel() override; 51 52 QHash<int, QByteArray> roleNames() const override; 53 54 int rowCount(const QModelIndex & parent) const override; 55 int columnCount(const QModelIndex & parent) const override; 56 57 QModelIndex index(int row, int column, const QModelIndex & parent) const override; 58 QModelIndex parent(const QModelIndex & child) const override; 59 60 QVariant data(const QModelIndex &index, int role) const override; 61 62 Q_INVOKABLE void setPassphrase(const QString &passphrase); 63 Q_INVOKABLE void forgetPassphrase(); 64 Q_INVOKABLE void addPassword(const QModelIndex &parent, const QString &name, 65 const QString &password, const QString &extras); 66 private: 67 void populate(); 68 void populateDir(const QDir &dir, Node *parent); 69 70 Node *node(const QModelIndex &index) const; 71 72 QFileSystemWatcher mWatcher; 73 QDir mPassStore; 74 75 Node *mRoot = nullptr; 76 }; 77 78 #endif