Skip to content

Commit

Permalink
Merge bitcoin#14784: qt: Use WalletModel* instead of the wallet name …
Browse files Browse the repository at this point in the history
…as map key

91b0c5b qt: Use WalletModel* instead of wallet name in console window (João Barbosa)
b2ce86c qt: Use WalletModel* instead of wallet name in main window (João Barbosa)
d2a1adf qt: Factor out WalletModel::getDisplayName() (João Barbosa)

Pull request description:

  This a small refactor that doesn't change behavior. This is also necessary if in the future we allow renaming wallets.

Tree-SHA512: 1820d0ff28e84b1d862097f1f55b52f94520fa50c9b1939d235a448a48159748c3bbf99b19e4cb1ff4f91efc008c0971b4c25a91f645f9d43792c8aeaa93cf9e
  • Loading branch information
laanwj authored and linuxsh2 committed Jul 27, 2021
1 parent df07019 commit ebffbe0
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 59 deletions.
18 changes: 8 additions & 10 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,10 +792,9 @@ bool BitcoinGUI::addWallet(WalletModel *walletModel)
{
if(!walletFrame)
return false;
const QString name = walletModel->getWalletName();
QString display_name = name.isEmpty() ? "["+tr("default wallet")+"]" : name;
const QString display_name = walletModel->getDisplayName();
setWalletActionsEnabled(true);
m_wallet_selector->addItem(display_name, name);
m_wallet_selector->addItem(display_name, QVariant::fromValue(walletModel));
if (m_wallet_selector->count() == 2) {
m_wallet_selector_action->setVisible(true);
}
Expand All @@ -806,29 +805,28 @@ bool BitcoinGUI::addWallet(WalletModel *walletModel)
bool BitcoinGUI::removeWallet(WalletModel* walletModel)
{
if (!walletFrame) return false;
QString name = walletModel->getWalletName();
int index = m_wallet_selector->findData(name);
int index = m_wallet_selector->findData(QVariant::fromValue(walletModel));
m_wallet_selector->removeItem(index);
if (m_wallet_selector->count() == 0) {
setWalletActionsEnabled(false);
} else if (m_wallet_selector->count() == 1) {
m_wallet_selector_action->setVisible(false);
}
rpcConsole->removeWallet(walletModel);
return walletFrame->removeWallet(name);
return walletFrame->removeWallet(walletModel);
}

bool BitcoinGUI::setCurrentWallet(const QString& name)
bool BitcoinGUI::setCurrentWallet(WalletModel* wallet_model)
{
if(!walletFrame)
return false;
return walletFrame->setCurrentWallet(name);
return walletFrame->setCurrentWallet(wallet_model);
}

bool BitcoinGUI::setCurrentWalletBySelectorIndex(int index)
{
QString internal_name = m_wallet_selector->itemData(index).toString();
return setCurrentWallet(internal_name);
WalletModel* wallet_model = m_wallet_selector->itemData(index).value<WalletModel*>();
return setCurrentWallet(wallet_model);
}

void BitcoinGUI::removeAllWallets()
Expand Down
2 changes: 1 addition & 1 deletion src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public Q_SLOTS:
void message(const QString &title, const QString &message, unsigned int style, bool *ret = nullptr);

#ifdef ENABLE_WALLET
bool setCurrentWallet(const QString& name);
bool setCurrentWallet(WalletModel* wallet_model);
bool setCurrentWalletBySelectorIndex(int index);
/** Set the UI status indicators based on the currently selected wallet.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/qt/dash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ void BitcoinApplication::addWallet(WalletModel* walletModel)
window->addWallet(walletModel);

if (m_wallet_models.empty()) {
window->setCurrentWallet(walletModel->getWalletName());
window->setCurrentWallet(walletModel);
}

connect(walletModel, SIGNAL(coinsSent(WalletModel*, SendCoinsRecipient, QByteArray)),
Expand Down
39 changes: 17 additions & 22 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RPCExecutor : public QObject
RPCExecutor(interfaces::Node& node) : m_node(node) {}

public Q_SLOTS:
void request(const QString &command, const QString &walletID);
void request(const QString &command, const WalletModel* wallet_model);

Q_SIGNALS:
void reply(int category, const QString &command);
Expand Down Expand Up @@ -151,7 +151,7 @@ class QtRPCTimerInterface: public RPCTimerInterface
* @param[out] pstrFilteredOut Command line, filtered to remove any sensitive data
*/

bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const std::string *walletID)
bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, const bool fExecute, std::string * const pstrFilteredOut, const WalletModel* wallet_model)
{
std::vector< std::vector<std::string> > stack;
stack.push_back(std::vector<std::string>());
Expand Down Expand Up @@ -309,8 +309,8 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
std::string method = stack.back()[0];
std::string uri;
#ifdef ENABLE_WALLET
if (walletID) {
QByteArray encodedName = QUrl::toPercentEncoding(QString::fromStdString(*walletID));
if (wallet_model) {
QByteArray encodedName = QUrl::toPercentEncoding(wallet_model->getWalletName());
uri = "/wallet/"+std::string(encodedName.constData(), encodedName.length());
}
#endif
Expand Down Expand Up @@ -390,7 +390,7 @@ bool RPCConsole::RPCParseCommandLine(interfaces::Node* node, std::string &strRes
}
}

void RPCExecutor::request(const QString &command, const QString &walletID)
void RPCExecutor::request(const QString &command, const WalletModel* wallet_model)
{
try
{
Expand Down Expand Up @@ -421,9 +421,7 @@ void RPCExecutor::request(const QString &command, const QString &walletID)
" example: getblock(getblockhash(0),true)[tx][0]\n\n")));
return;
}
std::string wallet_id = walletID.toStdString();
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, executableCommand, nullptr, walletID.isNull() ? nullptr : &wallet_id))
{
if (!RPCConsole::RPCExecuteCommandLine(m_node, result, executableCommand, nullptr, wallet_model)) {
Q_EMIT reply(RPCConsole::CMD_ERROR, QString("Parse error: unbalanced ' or \""));
return;
}
Expand Down Expand Up @@ -740,10 +738,8 @@ void RPCConsole::setClientModel(ClientModel *model)
#ifdef ENABLE_WALLET
void RPCConsole::addWallet(WalletModel * const walletModel)
{
const QString name = walletModel->getWalletName();
// use name for text and internal data object (to allow to move to a wallet id later)
QString display_name = name.isEmpty() ? "["+tr("default wallet")+"]" : name;
ui->WalletSelector->addItem(display_name, name);
// use name for text and wallet model for internal data object (to allow to move to a wallet id later)
ui->WalletSelector->addItem(walletModel->getDisplayName(), QVariant::fromValue(walletModel));
if (ui->WalletSelector->count() == 2 && !isVisible()) {
// First wallet added, set to default so long as the window isn't presently visible (and potentially in use)
ui->WalletSelector->setCurrentIndex(1);
Expand All @@ -756,8 +752,7 @@ void RPCConsole::addWallet(WalletModel * const walletModel)

void RPCConsole::removeWallet(WalletModel * const walletModel)
{
const QString name = walletModel->getWalletName();
ui->WalletSelector->removeItem(ui->WalletSelector->findData(name));
ui->WalletSelector->removeItem(ui->WalletSelector->findData(QVariant::fromValue(walletModel)));
if (ui->WalletSelector->count() == 2) {
ui->WalletSelector->setVisible(false);
ui->WalletSelectorLabel->setVisible(false);
Expand Down Expand Up @@ -1038,25 +1033,25 @@ void RPCConsole::on_lineEdit_returnPressed()

cmdBeforeBrowsing = QString();

QString walletID;
WalletModel* wallet_model{nullptr};
#ifdef ENABLE_WALLET
const int wallet_index = ui->WalletSelector->currentIndex();
if (wallet_index > 0) {
walletID = (QString)ui->WalletSelector->itemData(wallet_index).value<QString>();
wallet_model = ui->WalletSelector->itemData(wallet_index).value<WalletModel*>();
}

if (m_last_wallet_id != walletID) {
if (walletID.isNull()) {
message(CMD_REQUEST, tr("Executing command without any wallet"));
if (m_last_wallet_model != wallet_model) {
if (wallet_model) {
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(wallet_model->getWalletName()));
} else {
message(CMD_REQUEST, tr("Executing command using \"%1\" wallet").arg(walletID));
message(CMD_REQUEST, tr("Executing command without any wallet"));
}
m_last_wallet_id = walletID;
m_last_wallet_model = wallet_model;
}
#endif

message(CMD_REQUEST, QString::fromStdString(strFilteredCmd));
Q_EMIT cmdRequest(cmd, walletID);
Q_EMIT cmdRequest(cmd, m_last_wallet_model);

cmd = QString::fromStdString(strFilteredCmd);

Expand Down
10 changes: 5 additions & 5 deletions src/qt/rpcconsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class RPCConsole: public QWidget
explicit RPCConsole(interfaces::Node& node, QWidget* parent, Qt::WindowFlags flags);
~RPCConsole();

static bool RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr);
static bool RPCExecuteCommandLine(interfaces::Node& node, std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const std::string *walletID = nullptr) {
return RPCParseCommandLine(&node, strResult, strCommand, true, pstrFilteredOut, walletID);
static bool RPCParseCommandLine(interfaces::Node* node, std::string &strResult, const std::string &strCommand, bool fExecute, std::string * const pstrFilteredOut = nullptr, const WalletModel* wallet_model = nullptr);
static bool RPCExecuteCommandLine(interfaces::Node& node, std::string &strResult, const std::string &strCommand, std::string * const pstrFilteredOut = nullptr, const WalletModel* wallet_model = nullptr) {
return RPCParseCommandLine(&node, strResult, strCommand, true, pstrFilteredOut, wallet_model);
}

void setClientModel(ClientModel *model);
Expand Down Expand Up @@ -144,7 +144,7 @@ public Q_SLOTS:
Q_SIGNALS:
// For RPC command executor
void stopExecutor();
void cmdRequest(const QString &command, const QString &walletID);
void cmdRequest(const QString &command, const WalletModel* wallet_model);
/** Get restart command-line parameters and handle restart */
void handleRestart(QStringList args);

Expand Down Expand Up @@ -184,7 +184,7 @@ public Q_SLOTS:
int consoleFontSize;
QCompleter *autoCompleter;
QThread thread;
QString m_last_wallet_id;
WalletModel* m_last_wallet_model{nullptr};

/** Update UI with latest network info from model. */
void updateNetworkState();
Expand Down
33 changes: 16 additions & 17 deletions src/qt/walletframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ bool WalletFrame::addWallet(WalletModel *walletModel)
return false;
}

const QString name = walletModel->getWalletName();
if (mapWalletViews.count(name) > 0) {
if (mapWalletViews.count(walletModel) > 0) {
return false;
}

Expand All @@ -64,7 +63,7 @@ bool WalletFrame::addWallet(WalletModel *walletModel)
}

walletStack->addWidget(walletView);
mapWalletViews[name] = walletView;
mapWalletViews[walletModel] = walletView;

// Ensure a walletView is able to show the main window
connect(walletView, SIGNAL(showNormalIfMinimized()), gui, SLOT(showNormalIfMinimized()));
Expand All @@ -74,32 +73,32 @@ bool WalletFrame::addWallet(WalletModel *walletModel)
return true;
}

bool WalletFrame::setCurrentWallet(const QString& name)
bool WalletFrame::setCurrentWallet(WalletModel* wallet_model)
{
if (mapWalletViews.count(name) == 0)
if (mapWalletViews.count(wallet_model) == 0)
return false;

WalletView *walletView = mapWalletViews.value(name);
WalletView *walletView = mapWalletViews.value(wallet_model);
walletStack->setCurrentWidget(walletView);
assert(walletView);
walletView->updateEncryptionStatus();
return true;
}

bool WalletFrame::removeWallet(const QString &name)
bool WalletFrame::removeWallet(WalletModel* wallet_model)
{
if (mapWalletViews.count(name) == 0)
if (mapWalletViews.count(wallet_model) == 0)
return false;

WalletView *walletView = mapWalletViews.take(name);
WalletView *walletView = mapWalletViews.take(wallet_model);
walletStack->removeWidget(walletView);
delete walletView;
return true;
}

void WalletFrame::removeAllWallets()
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) {
walletStack->removeWidget(i.value());
delete i.value();
Expand All @@ -119,49 +118,49 @@ bool WalletFrame::handlePaymentRequest(const SendCoinsRecipient &recipient)
void WalletFrame::showOutOfSyncWarning(bool fShow)
{
bOutOfSync = fShow;
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
i.value()->showOutOfSyncWarning(fShow);
}

void WalletFrame::gotoOverviewPage()
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
i.value()->gotoOverviewPage();
}

void WalletFrame::gotoHistoryPage()
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
i.value()->gotoHistoryPage();
}

void WalletFrame::gotoMasternodePage()
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
i.value()->gotoMasternodePage();
}

void WalletFrame::gotoReceiveCoinsPage()
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
i.value()->gotoReceiveCoinsPage();
}

void WalletFrame::gotoSendCoinsPage(QString addr)
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i)
i.value()->gotoSendCoinsPage(addr);
}

void WalletFrame::gotoCoinJoinCoinsPage(QString addr)
{
QMap<QString, WalletView*>::const_iterator i;
QMap<WalletModel*, WalletView*>::const_iterator i;
for (auto i = mapWalletViews.constBegin(); i != mapWalletViews.constEnd(); ++i) {
i.value()->gotoCoinJoinCoinsPage(addr);
}
Expand Down
6 changes: 3 additions & 3 deletions src/qt/walletframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class WalletFrame : public QFrame
void setClientModel(ClientModel *clientModel);

bool addWallet(WalletModel *walletModel);
bool setCurrentWallet(const QString& name);
bool removeWallet(const QString &name);
bool setCurrentWallet(WalletModel* wallet_model);
bool removeWallet(WalletModel* wallet_model);
void removeAllWallets();

bool handlePaymentRequest(const SendCoinsRecipient& recipient);
Expand All @@ -52,7 +52,7 @@ class WalletFrame : public QFrame
QStackedWidget *walletStack;
BitcoinGUI *gui;
ClientModel *clientModel;
QMap<QString, WalletView*> mapWalletViews;
QMap<WalletModel*, WalletView*> mapWalletViews;

bool bOutOfSync;

Expand Down
6 changes: 6 additions & 0 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ QString WalletModel::getWalletName() const
return QString::fromStdString(m_wallet->getWalletName());
}

QString WalletModel::getDisplayName() const
{
const QString name = getWalletName();
return name.isEmpty() ? "["+tr("default wallet")+"]" : name;
}

bool WalletModel::isMultiwallet()
{
return m_node.getWallets().size() > 1;
Expand Down
1 change: 1 addition & 0 deletions src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class WalletModel : public QObject
interfaces::CoinJoin::Client& coinJoin() const { return m_wallet->coinJoin(); }

QString getWalletName() const;
QString getDisplayName() const;

bool isMultiwallet();
private:
Expand Down

0 comments on commit ebffbe0

Please sign in to comment.