File: passwordsmodel.cpp

package info (click to toggle)
plasma-pass 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 504 kB
  • sloc: cpp: 1,316; xml: 28; makefile: 2; sh: 1
file content (220 lines) | stat: -rw-r--r-- 6,214 bytes parent folder | download | duplicates (2)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: LGPL-2.1-or-later

#include "passwordsmodel.h"
#include "passwordprovider.h"
#include "otpprovider.h"

#include <QDebug>
#include <QPointer>

using namespace PlasmaPass;

static constexpr const char *passwordStoreDir = "PASSWORD_STORE_DIR";

struct PasswordsModel::Node {
    explicit Node() = default;
    Node(QString name, PasswordsModel::EntryType type, Node *nodeParent)
        : name(std::move(name))
        , type(type)
        , parent(nodeParent)
    {
        if (parent != nullptr) {
            parent->children.push_back(std::unique_ptr<Node>(this));
        }
    }

    Node(const Node &other) = delete;
    Node(Node &&other) = default;
    Node &operator=(const Node &other) = delete;
    Node &operator=(Node &&other) = delete;

    ~Node() = default;

    QString path() const
    {
        if (parent == nullptr) {
            return name;
        }

        QString fileName = name;
        if (type == PasswordsModel::PasswordEntry) {
            fileName += QStringLiteral(".gpg");
        }
        return parent->path() + QLatin1Char('/') + fileName;
    }

    QString fullName() const
    {
        if (!mFullName.isNull()) {
            return mFullName;
        }

        if (parent == nullptr) {
            return {};
        }
        const auto p = parent->fullName();
        if (p.isEmpty()) {
            mFullName = name;
        } else {
            mFullName = p + QLatin1Char('/') + name;
        }
        return mFullName;
    }

    QString name;
    PasswordsModel::EntryType type = PasswordsModel::FolderEntry;
    QPointer<PasswordProvider> provider;
    QPointer<OTPProvider> otpProvider;
    Node *parent = nullptr;
    std::vector<std::unique_ptr<Node>> children;

private:
    mutable QString mFullName;
};

PasswordsModel::PasswordsModel(QObject *parent)
    : QAbstractItemModel(parent)
    , mWatcher(this)
{
    if (qEnvironmentVariableIsSet(passwordStoreDir)) {
        mPassStore = QDir(QString::fromUtf8(qgetenv(passwordStoreDir)));
    } else {
        mPassStore = QDir(QStringLiteral("%1/.password-store").arg(QDir::homePath()));
    }

    // FIXME: Try to figure out what has actually changed and update the model
    // accordingly instead of reseting it
    connect(&mWatcher, &QFileSystemWatcher::directoryChanged, this, &PasswordsModel::populate);

    populate();
}

PasswordsModel::~PasswordsModel() = default;

PasswordsModel::Node *PasswordsModel::node(const QModelIndex &index)
{
    return static_cast<Node *>(index.internalPointer());
}

QHash<int, QByteArray> PasswordsModel::roleNames() const
{
    return {{NameRole, "name"},
            {EntryTypeRole, "type"},
            {FullNameRole, "fullName"},
            {PathRole, "path"},
            {HasPasswordRole, "hasPassword"},
            {PasswordRole, "password"},
            {OTPRole, "otp"},
            {HasOTPRole, "hasOtp"}};

}

int PasswordsModel::rowCount(const QModelIndex &parent) const
{
    const auto parentNode = parent.isValid() ? node(parent) : mRoot.get();
    return parentNode != nullptr ? static_cast<int>(parentNode->children.size()) : 0;
}

int PasswordsModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 1;
}

QModelIndex PasswordsModel::index(int row, int column, const QModelIndex &parent) const
{
    const auto parentNode = parent.isValid() ? node(parent) : mRoot.get();
    if (parentNode == nullptr || row < 0 || static_cast<std::size_t>(row) >= parentNode->children.size() || column != 0) {
        return {};
    }

    return createIndex(row, column, parentNode->children.at(row).get());
}

QModelIndex PasswordsModel::parent(const QModelIndex &child) const
{
    if (!child.isValid()) {
        return {};
    }

    const auto childNode = node(child);
    if (childNode == nullptr || childNode->parent == nullptr) {
        return {};
    }
    const auto parentNode = childNode->parent;
    if (parentNode == mRoot.get()) {
        return {};
    }

    auto &children = parentNode->parent->children;
    const auto it = std::find_if(children.cbegin(), children.cend(), [parentNode](const auto &node) {
        return node.get() == parentNode;
    });
    Q_ASSERT(it != children.cend());
    return createIndex(std::distance(children.cbegin(), it), 0, parentNode);
}

QVariant PasswordsModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return {};
    }
    const auto node = this->node(index);
    if (node == nullptr) {
        return {};
    }

    switch (role) {
    case Qt::DisplayRole:
        return node->name;
    case EntryTypeRole:
        return node->type;
    case PathRole:
        return node->path();
    case FullNameRole:
        return node->fullName();
    case PasswordRole:
        if (node->provider == nullptr) {
            node->provider = new PasswordProvider(node->path());
        }
        return QVariant::fromValue(node->provider.data());
    case OTPRole:
        if (node->otpProvider == nullptr) {
            node->otpProvider = new OTPProvider(node->path());
        }
        return QVariant::fromValue(node->otpProvider.data());
    case HasPasswordRole:
        return !node->provider.isNull();
    case HasOTPRole:
        return !node->otpProvider.isNull();
    default:
        return {};
    }
}

void PasswordsModel::populate()
{
    beginResetModel();
    mRoot = std::make_unique<Node>();
    mRoot->name = mPassStore.absolutePath();
    populateDir(mPassStore, mRoot.get());
    endResetModel();
}

void PasswordsModel::populateDir(const QDir &dir, Node *parent)
{
    mWatcher.addPath(dir.absolutePath());
    auto entries = dir.entryInfoList({QStringLiteral("*.gpg")}, QDir::Files, QDir::NoSort);
    for (const auto &entry : qAsConst(entries)) {
        new Node(entry.completeBaseName(), PasswordEntry, parent);
    }
    entries = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::NoSort);
    for (const auto &entry : qAsConst(entries)) {
        auto node = new Node(entry.fileName(), FolderEntry, parent);
        populateDir(entry.absoluteFilePath(), node);
    }
}

#include "moc_passwordsmodel.cpp"