Browse Source

qml: unify to single WalletListModel, WalletDB trigger actions on every path set,

camelcase more QML exposed functions/slots.
patch-4
Sander van Grieken 2 years ago
parent
commit
12086ba0de
  1. 10
      electrum/gui/qml/components/WalletDetails.qml
  2. 2
      electrum/gui/qml/components/Wallets.qml
  3. 2
      electrum/gui/qml/qeapp.py
  4. 95
      electrum/gui/qml/qedaemon.py
  5. 5
      electrum/gui/qml/qewalletdb.py

10
electrum/gui/qml/components/WalletDetails.qml

@ -26,14 +26,14 @@ Pane {
var dialog = app.messageDialog.createObject(rootItem, var dialog = app.messageDialog.createObject(rootItem,
{'text': qsTr('Really delete this wallet?'), 'yesno': true}) {'text': qsTr('Really delete this wallet?'), 'yesno': true})
dialog.yesClicked.connect(function() { dialog.yesClicked.connect(function() {
Daemon.check_then_delete_wallet(Daemon.currentWallet) Daemon.checkThenDeleteWallet(Daemon.currentWallet)
}) })
dialog.open() dialog.open()
} }
function changePassword() { function changePassword() {
// trigger dialog via wallet (auth then signal) // trigger dialog via wallet (auth then signal)
Daemon.start_change_password() Daemon.startChangePassword()
} }
function importAddressesKeys() { function importAddressesKeys() {
@ -343,7 +343,7 @@ Pane {
restore from seed. Please make sure you have your seed stored safely') restore from seed. Please make sure you have your seed stored safely')
} ) } )
dialog.accepted.connect(function() { dialog.accepted.connect(function() {
Daemon.set_password(dialog.password) Daemon.setPassword(dialog.password)
}) })
dialog.open() dialog.open()
} }
@ -351,13 +351,13 @@ Pane {
if (code == 'unpaid_requests') { if (code == 'unpaid_requests') {
var dialog = app.messageDialog.createObject(app, {text: message, yesno: true }) var dialog = app.messageDialog.createObject(app, {text: message, yesno: true })
dialog.yesClicked.connect(function() { dialog.yesClicked.connect(function() {
Daemon.check_then_delete_wallet(Daemon.currentWallet, true) Daemon.checkThenDeleteWallet(Daemon.currentWallet, true)
}) })
dialog.open() dialog.open()
} else if (code == 'balance') { } else if (code == 'balance') {
var dialog = app.messageDialog.createObject(app, {text: message, yesno: true }) var dialog = app.messageDialog.createObject(app, {text: message, yesno: true })
dialog.yesClicked.connect(function() { dialog.yesClicked.connect(function() {
Daemon.check_then_delete_wallet(Daemon.currentWallet, true, true) Daemon.checkThenDeleteWallet(Daemon.currentWallet, true, true)
}) })
dialog.open() dialog.open()
} else { } else {

2
electrum/gui/qml/components/Wallets.qml

@ -13,8 +13,6 @@ Pane {
padding: 0 padding: 0
// property string title: qsTr('Wallets')
function createWallet() { function createWallet() {
var dialog = app.newWalletWizard.createObject(rootItem) var dialog = app.newWalletWizard.createObject(rootItem)
dialog.open() dialog.open()

2
electrum/gui/qml/qeapp.py

@ -12,7 +12,7 @@ from electrum.logging import Logger, get_logger
from electrum.util import BITCOIN_BIP21_URI_SCHEME, LIGHTNING_URI_SCHEME from electrum.util import BITCOIN_BIP21_URI_SCHEME, LIGHTNING_URI_SCHEME
from .qeconfig import QEConfig from .qeconfig import QEConfig
from .qedaemon import QEDaemon, QEWalletListModel from .qedaemon import QEDaemon
from .qenetwork import QENetwork from .qenetwork import QENetwork
from .qewallet import QEWallet from .qewallet import QEWallet
from .qeqr import QEQRParser, QEQRImageProvider, QEQRImageProviderHelper from .qeqr import QEQRParser, QEQRImageProvider, QEQRImageProviderHelper

95
electrum/gui/qml/qedaemon.py

@ -20,15 +20,18 @@ from .qewizard import QENewWalletWizard, QEServerConnectWizard
# and whole Wallet instances (loaded wallets) # and whole Wallet instances (loaded wallets)
class QEWalletListModel(QAbstractListModel): class QEWalletListModel(QAbstractListModel):
_logger = get_logger(__name__) _logger = get_logger(__name__)
def __init__(self, parent=None):
QAbstractListModel.__init__(self, parent)
self.wallets = []
# define listmodel rolemap # define listmodel rolemap
_ROLE_NAMES= ('name','path','active') _ROLE_NAMES= ('name','path','active')
_ROLE_KEYS = range(Qt.UserRole, Qt.UserRole + len(_ROLE_NAMES)) _ROLE_KEYS = range(Qt.UserRole, Qt.UserRole + len(_ROLE_NAMES))
_ROLE_MAP = dict(zip(_ROLE_KEYS, [bytearray(x.encode()) for x in _ROLE_NAMES])) _ROLE_MAP = dict(zip(_ROLE_KEYS, [bytearray(x.encode()) for x in _ROLE_NAMES]))
def __init__(self, daemon, parent=None):
QAbstractListModel.__init__(self, parent)
self.daemon = daemon
self.wallets = []
self.reload()
def rowCount(self, index): def rowCount(self, index):
return len(self.wallets) return len(self.wallets)
@ -36,7 +39,7 @@ class QEWalletListModel(QAbstractListModel):
return self._ROLE_MAP return self._ROLE_MAP
def data(self, index, role): def data(self, index, role):
(wallet_name, wallet_path, wallet) = self.wallets[index.row()] (wallet_name, wallet_path) = self.wallets[index.row()]
role_index = role - Qt.UserRole role_index = role - Qt.UserRole
role_name = self._ROLE_NAMES[role_index] role_name = self._ROLE_NAMES[role_index]
if role_name == 'name': if role_name == 'name':
@ -44,23 +47,31 @@ class QEWalletListModel(QAbstractListModel):
if role_name == 'path': if role_name == 'path':
return wallet_path return wallet_path
if role_name == 'active': if role_name == 'active':
return wallet is not None return self.daemon.get_wallet(wallet_path) is not None
def add_wallet(self, wallet_path = None, wallet: Abstract_Wallet = None): @pyqtSlot()
if wallet_path is None and wallet is None: def reload(self):
return self._logger.debug('enumerating available wallets')
# only add wallet instance if instance not yet in model if len(self.wallets) > 0:
if wallet: self.beginRemoveRows(QModelIndex(), 0, len(self.wallets) - 1)
for name,path,w in self.wallets: self.wallets = []
if w == wallet: self.endRemoveRows()
return
available = []
wallet_folder = os.path.dirname(self.daemon.config.get_wallet_path())
with os.scandir(wallet_folder) as it:
for i in it:
if i.is_file() and not i.name.startswith('.'):
available.append(i.path)
for path in sorted(available):
wallet = self.daemon.get_wallet(path)
self.add_wallet(wallet_path = path)
def add_wallet(self, wallet_path):
self.beginInsertRows(QModelIndex(), len(self.wallets), len(self.wallets)) self.beginInsertRows(QModelIndex(), len(self.wallets), len(self.wallets))
if wallet is None: wallet_name = os.path.basename(wallet_path)
wallet_name = os.path.basename(wallet_path)
else:
wallet_name = wallet.basename()
wallet_path = standardize_path(wallet_path) wallet_path = standardize_path(wallet_path)
item = (wallet_name, wallet_path, wallet) item = (wallet_name, wallet_path)
self.wallets.append(item) self.wallets.append(item)
self.endInsertRows() self.endInsertRows()
@ -68,12 +79,12 @@ class QEWalletListModel(QAbstractListModel):
i = 0 i = 0
wallets = [] wallets = []
remove = -1 remove = -1
for wallet_name, wallet_path, wallet in self.wallets: for wallet_name, wallet_path in self.wallets:
if wallet_path == path: if wallet_path == path:
remove = i remove = i
else: else:
self._logger.debug('HM, %s is not %s', wallet_path, path) self._logger.debug('HM, %s is not %s', wallet_path, path)
wallets.append((wallet_name, wallet_path, wallet)) wallets.append((wallet_name, wallet_path))
i += 1 i += 1
if remove >= 0: if remove >= 0:
@ -81,31 +92,8 @@ class QEWalletListModel(QAbstractListModel):
self.wallets = wallets self.wallets = wallets
self.endRemoveRows() self.endRemoveRows()
class QEAvailableWalletListModel(QEWalletListModel):
def __init__(self, daemon, parent=None):
QEWalletListModel.__init__(self, parent)
self.daemon = daemon
self.reload()
@pyqtSlot()
def reload(self):
if len(self.wallets) > 0:
self.beginRemoveRows(QModelIndex(), 0, len(self.wallets) - 1)
self.wallets = []
self.endRemoveRows()
available = []
wallet_folder = os.path.dirname(self.daemon.config.get_wallet_path())
with os.scandir(wallet_folder) as it:
for i in it:
if i.is_file() and not i.name.startswith('.'):
available.append(i.path)
for path in sorted(available):
wallet = self.daemon.get_wallet(path)
self.add_wallet(wallet_path = path, wallet = wallet)
def wallet_name_exists(self, name): def wallet_name_exists(self, name):
for wallet_name, wallet_path, wallet in self.wallets: for wallet_name, wallet_path in self.wallets:
if name == wallet_name: if name == wallet_name:
return True return True
return False return False
@ -119,7 +107,6 @@ class QEDaemon(AuthMixin, QObject):
self._walletdb.validPasswordChanged.connect(self.passwordValidityCheck) self._walletdb.validPasswordChanged.connect(self.passwordValidityCheck)
_logger = get_logger(__name__) _logger = get_logger(__name__)
_loaded_wallets = QEWalletListModel()
_available_wallets = None _available_wallets = None
_current_wallet = None _current_wallet = None
_new_wallet_wizard = None _new_wallet_wizard = None
@ -128,7 +115,6 @@ class QEDaemon(AuthMixin, QObject):
_use_single_password = False _use_single_password = False
_password = None _password = None
activeWalletsChanged = pyqtSignal()
availableWalletsChanged = pyqtSignal() availableWalletsChanged = pyqtSignal()
fxChanged = pyqtSignal() fxChanged = pyqtSignal()
newWalletWizardChanged = pyqtSignal() newWalletWizardChanged = pyqtSignal()
@ -178,7 +164,6 @@ class QEDaemon(AuthMixin, QObject):
if wallet is not None: if wallet is not None:
self._current_wallet = QEWallet.getInstanceFor(wallet) self._current_wallet = QEWallet.getInstanceFor(wallet)
if not wallet_already_open: if not wallet_already_open:
self._loaded_wallets.add_wallet(wallet_path=self._path, wallet=wallet)
self._current_wallet.password = password self._current_wallet.password = password
self.walletLoaded.emit() self.walletLoaded.emit()
@ -202,7 +187,7 @@ class QEDaemon(AuthMixin, QObject):
@pyqtSlot(QEWallet) @pyqtSlot(QEWallet)
@pyqtSlot(QEWallet, bool) @pyqtSlot(QEWallet, bool)
@pyqtSlot(QEWallet, bool, bool) @pyqtSlot(QEWallet, bool, bool)
def check_then_delete_wallet(self, wallet, confirm_requests=False, confirm_balance=False): def checkThenDeleteWallet(self, wallet, confirm_requests=False, confirm_balance=False):
if wallet.wallet.lnworker: if wallet.wallet.lnworker:
lnchannels = wallet.wallet.lnworker.get_channel_objects() lnchannels = wallet.wallet.lnworker.get_channel_objects()
if any([channel.get_state() != ChannelState.REDEEMED for channel in lnchannels.values()]): if any([channel.get_state() != ChannelState.REDEEMED for channel in lnchannels.values()]):
@ -221,7 +206,6 @@ class QEDaemon(AuthMixin, QObject):
self.delete_wallet(wallet) self.delete_wallet(wallet)
@pyqtSlot(QEWallet)
@auth_protect @auth_protect
def delete_wallet(self, wallet): def delete_wallet(self, wallet):
path = standardize_path(wallet.wallet.storage.path) path = standardize_path(wallet.wallet.storage.path)
@ -234,7 +218,6 @@ class QEDaemon(AuthMixin, QObject):
self.walletDeleteError.emit('error', _('Problem deleting wallet')) self.walletDeleteError.emit('error', _('Problem deleting wallet'))
return return
self.activeWallets.remove_wallet(path)
self.availableWallets.remove_wallet(path) self.availableWallets.remove_wallet(path)
@pyqtProperty('QString') @pyqtProperty('QString')
@ -245,14 +228,10 @@ class QEDaemon(AuthMixin, QObject):
def currentWallet(self): def currentWallet(self):
return self._current_wallet return self._current_wallet
@pyqtProperty(QEWalletListModel, notify=activeWalletsChanged) @pyqtProperty(QEWalletListModel, notify=availableWalletsChanged)
def activeWallets(self):
return self._loaded_wallets
@pyqtProperty(QEAvailableWalletListModel, notify=availableWalletsChanged)
def availableWallets(self): def availableWallets(self):
if not self._available_wallets: if not self._available_wallets:
self._available_wallets = QEAvailableWalletListModel(self.daemon) self._available_wallets = QEWalletListModel(self.daemon)
return self._available_wallets return self._available_wallets
@ -279,14 +258,14 @@ class QEDaemon(AuthMixin, QObject):
requestNewPassword = pyqtSignal() requestNewPassword = pyqtSignal()
@pyqtSlot() @pyqtSlot()
@auth_protect @auth_protect
def start_change_password(self): def startChangePassword(self):
if self._use_single_password: if self._use_single_password:
self.requestNewPassword.emit() self.requestNewPassword.emit()
else: else:
self.currentWallet.requestNewPassword.emit() self.currentWallet.requestNewPassword.emit()
@pyqtSlot(str) @pyqtSlot(str)
def set_password(self, password): def setPassword(self, password):
assert self._use_single_password assert self._use_single_password
self._logger.debug('about to set password for ALL wallets') self._logger.debug('about to set password for ALL wallets')
self.daemon.update_password_for_directory(old_password=self._password, new_password=password) self.daemon.update_password_for_directory(old_password=self._password, new_password=password)

5
electrum/gui/qml/qewalletdb.py

@ -50,10 +50,7 @@ class QEWalletDB(QObject):
@path.setter @path.setter
def path(self, wallet_path): def path(self, wallet_path):
if wallet_path == self._path: self._logger.debug('setting path: ' + wallet_path)
return
self._logger.info('setting path: ' + wallet_path)
self.reset() self.reset()
self._path = wallet_path self._path = wallet_path

Loading…
Cancel
Save