|
| 1 | +// Copyright (c) 2024 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/models/walletlistmodel.h> |
| 6 | + |
| 7 | +#include <interfaces/node.h> |
| 8 | + |
| 9 | +#include <QSet> |
| 10 | + |
| 11 | +WalletListModel::WalletListModel(interfaces::Node& node, QObject *parent) |
| 12 | +: QAbstractListModel(parent) |
| 13 | +, m_node(node) |
| 14 | +{ |
| 15 | + setSelectedWallet("Singlesig Wallet"); |
| 16 | +} |
| 17 | + |
| 18 | +void WalletListModel::listWalletDir() |
| 19 | +{ |
| 20 | + QSet<QString> existing_names; |
| 21 | + for (int i = 0; i < rowCount(); ++i) { |
| 22 | + QModelIndex index = this->index(i, 0); |
| 23 | + QString name = data(index, NameRole).toString(); |
| 24 | + existing_names.insert(name); |
| 25 | + } |
| 26 | + |
| 27 | + for (const std::string &name : m_node.walletLoader().listWalletDir()) { |
| 28 | + QString qname = QString::fromStdString(name); |
| 29 | + if (!existing_names.contains(qname)) { |
| 30 | + addItem({ qname }); |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +void WalletListModel::setSelectedWallet(QString wallet_name) |
| 36 | +{ |
| 37 | + if (m_selected_wallet != wallet_name) { |
| 38 | + m_selected_wallet = wallet_name; |
| 39 | + Q_EMIT selectedWalletChanged(); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +QString WalletListModel::selectedWallet() const |
| 44 | +{ |
| 45 | + return m_selected_wallet; |
| 46 | +} |
| 47 | + |
| 48 | +int WalletListModel::rowCount(const QModelIndex &parent) const |
| 49 | +{ |
| 50 | + Q_UNUSED(parent); |
| 51 | + return m_items.size(); |
| 52 | +} |
| 53 | + |
| 54 | +QVariant WalletListModel::data(const QModelIndex &index, int role) const |
| 55 | +{ |
| 56 | + if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) |
| 57 | + return QVariant(); |
| 58 | + |
| 59 | + const auto &item = m_items[index.row()]; |
| 60 | + switch (role) { |
| 61 | + case Qt::DisplayRole: |
| 62 | + case NameRole: |
| 63 | + return item.name; |
| 64 | + default: |
| 65 | + return QVariant(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +QHash<int, QByteArray> WalletListModel::roleNames() const |
| 70 | +{ |
| 71 | + QHash<int, QByteArray> roles; |
| 72 | + roles[NameRole] = "name"; |
| 73 | + return roles; |
| 74 | +} |
| 75 | + |
| 76 | +void WalletListModel::addItem(const Item &item) |
| 77 | +{ |
| 78 | + beginInsertRows(QModelIndex(), rowCount(), rowCount()); |
| 79 | + m_items.append(item); |
| 80 | + endInsertRows(); |
| 81 | +} |
0 commit comments