addresstablemodel.h raw
1 // Copyright (c) 2011-present 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 #ifndef BITCOIN_QT_ADDRESSTABLEMODEL_H
6 #define BITCOIN_QT_ADDRESSTABLEMODEL_H
7
8 #include <optional>
9
10 #include <QAbstractTableModel>
11 #include <QStringList>
12
13 enum class OutputType;
14
15 class AddressTablePriv;
16 class WalletModel;
17
18 namespace interfaces {
19 class Wallet;
20 }
21 namespace wallet {
22 enum class AddressPurpose;
23 } // namespace wallet
24
25 /**
26 Qt model of the address book in the core. This allows views to access and modify the address book.
27 */
28 class AddressTableModel : public QAbstractTableModel
29 {
30 Q_OBJECT
31
32 public:
33 explicit AddressTableModel(WalletModel *parent = nullptr, bool pk_hash_only = false);
34 ~AddressTableModel();
35
36 enum ColumnIndex {
37 Label = 0, /**< User specified label */
38 Address = 1 /**< Bitcoin address */
39 };
40
41 enum RoleIndex {
42 TypeRole = Qt::UserRole /**< Type of address (#Send or #Receive) */
43 };
44
45 /** Return status of edit/insert operation */
46 enum EditStatus {
47 OK, /**< Everything ok */
48 NO_CHANGES, /**< No changes were made during edit operation */
49 INVALID_ADDRESS, /**< Unparseable address */
50 DUPLICATE_ADDRESS, /**< Address already in address book */
51 WALLET_UNLOCK_FAILURE, /**< Wallet could not be unlocked to create new receiving address */
52 KEY_GENERATION_FAILURE /**< Generating a new public key for a receiving address failed */
53 };
54
55 static const QString Send; /**< Specifies send address */
56 static const QString Receive; /**< Specifies receive address */
57
58 /** @name Methods overridden from QAbstractTableModel
59 @{*/
60 int rowCount(const QModelIndex &parent) const override;
61 int columnCount(const QModelIndex &parent) const override;
62 QVariant data(const QModelIndex &index, int role) const override;
63 bool setData(const QModelIndex &index, const QVariant &value, int role) override;
64 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
65 QModelIndex index(int row, int column, const QModelIndex &parent) const override;
66 bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
67 Qt::ItemFlags flags(const QModelIndex &index) const override;
68 /*@}*/
69
70 /* Add an address to the model.
71 Returns the added address on success, and an empty string otherwise.
72 */
73 QString addRow(const QString& type, const QString& label, const QString& address, OutputType address_type);
74
75 /** Look up label for address in address book, if not found return empty string. */
76 QString labelForAddress(const QString &address) const;
77
78 /** Look up purpose for address in address book, if not found return empty string. */
79 std::optional<wallet::AddressPurpose> purposeForAddress(const QString &address) const;
80
81 /* Look up row index of an address in the model.
82 Return -1 if not found.
83 */
84 int lookupAddress(const QString &address) const;
85
86 EditStatus getEditStatus() const { return editStatus; }
87
88 OutputType GetDefaultAddressType() const;
89
90 QString GetWalletDisplayName() const;
91
92 private:
93 WalletModel* const walletModel;
94 AddressTablePriv *priv = nullptr;
95 QStringList columns;
96 EditStatus editStatus = OK;
97
98 /** Look up address book data given an address string. */
99 bool getAddressData(const QString &address, std::string* name, wallet::AddressPurpose* purpose) const;
100
101 /** Notify listeners that data changed. */
102 void emitDataChanged(int index);
103
104 public Q_SLOTS:
105 /* Update address list from core.
106 */
107 void updateEntry(const QString &address, const QString &label, bool isMine, wallet::AddressPurpose purpose, int status);
108
109 friend class AddressTablePriv;
110 };
111
112 #endif // BITCOIN_QT_ADDRESSTABLEMODEL_H
113