Skip to content
Snippets Groups Projects
Commit 293dbe40 authored by inso's avatar inso
Browse files

Refactoring the code to handle lifecycles better

parent 2c65bba6
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,7 @@ RESOURCE_DIR = res/ui
COMPILED_DIR = src/cutecoin/gen_resources
#UI files to compile
UI_FILES = mainwindow.ui accountConfigurationDialog.ui addCommunityDialog.ui communityTabWidget.ui issuanceDialog.ui transferDialog.ui addContactDialog.ui communityParametersWidget.ui
UI_FILES = mainwindow.ui accountConfigurationDialog.ui communityConfigurationDialog.ui communityTabWidget.ui issuanceDialog.ui transferDialog.ui addContactDialog.ui communityParametersWidget.ui
#Qt resource files to compile
RESOURCES =
......
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Account parameters</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Account name</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>PGPKey </string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>AddCommunityDialog</class>
<widget class="QDialog" name="AddCommunityDialog">
<class>CommunityConfigurationDialog</class>
<widget class="QDialog" name="CommunityConfigurationDialog">
<property name="geometry">
<rect>
<x>0</x>
......@@ -80,7 +80,7 @@
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>AddCommunityDialog</receiver>
<receiver>CommunityConfigurationDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
......@@ -96,7 +96,7 @@
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>AddCommunityDialog</receiver>
<receiver>CommunityConfigurationDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
......@@ -112,8 +112,8 @@
<connection>
<sender>addButton</sender>
<signal>clicked()</signal>
<receiver>AddCommunityDialog</receiver>
<slot>addCommunity()</slot>
<receiver>CommunityConfigurationDialog</receiver>
<slot>addNode()</slot>
<hints>
<hint type="sourcelabel">
<x>337</x>
......@@ -127,6 +127,6 @@
</connection>
</connections>
<slots>
<slot>addCommunity()</slot>
<slot>addNode()</slot>
</slots>
</ui>
'''
Created on 2 févr. 2014
@author: inso
'''
from cutecoin.gen_resources.accountConfigurationDialog_uic import Ui_AccountConfigurationDialog
from PyQt5.QtWidgets import QDialog
from cutecoin.gui.addCommunityDialog import AddCommunityDialog
from cutecoin.models.account import Account
from cutecoin.models.account.communities import Communities
from cutecoin.models.account.communities.listModel import CommunitiesListModel
import gnupg
class AddAccountDialog(QDialog, Ui_AccountConfigurationDialog):
'''
classdocs
'''
def __init__(self, mainWindow):
'''
Constructor
'''
# Set up the user interface from Designer.
super(AddAccountDialog, self).__init__()
self.setupUi(self)
self.mainWindow = mainWindow
self.buttonBox.accepted.connect(self.mainWindow.actionAddAccount)
self.setData()
def setData(self):
gpg = gnupg.GPG()
self.combo_keysList.clear()
availableKeys = gpg.list_keys(True)
for key in availableKeys:
self.combo_keysList.addItem(key['uids'][0])
self.account = Account.create(availableKeys[0]['keyid'], "", Communities())
self.combo_keysList.setEnabled(True)
self.combo_keysList.currentIndexChanged[int].connect(self.keyChanged)
def openAddCommunityDialog(self):
dialog = AddCommunityDialog(self)
dialog.setAccount(self.account)
dialog.exec_()
def actionAddCommunity(self):
self.combo_keysList.setEnabled(False)
self.combo_keysList.disconnect()
self.list_communities.setModel(CommunitiesListModel(self.account))
def actionRemoveCommunity(self):
#TODO:Remove selected community
pass
def actionEditCommunity(self):
#TODO: Edit selected community
pass
def keyChanged(self, keyIndex):
gpg = gnupg.GPG()
availableKeys = gpg.list_keys(True)
self.account.keyId = availableKeys[keyIndex]['keyid']
'''
Created on 2 févr. 2014
@author: inso
'''
from cutecoin.gen_resources.addCommunityDialog_uic import Ui_AddCommunityDialog
from PyQt5.QtWidgets import QDialog, QErrorMessage
from cutecoin.models.community.treeModel import CommunityTreeModel
from cutecoin.models.node import MainNode
from cutecoin.core.exceptions import NotMemberOfCommunityError
class AddCommunityDialog(QDialog, Ui_AddCommunityDialog):
'''
classdocs
'''
def __init__(self, accountDialog):
'''
Constructor
'''
super(AddCommunityDialog, self).__init__()
self.setupUi(self)
self.accountDialog = accountDialog
self.buttonBox.accepted.connect(self.accountDialog.actionAddCommunity)
def setAccount(self, account):
self.account = account
def addCommunity(self):
'''
Add community slot
'''
server = self.serverEdit.text()
port = self.portBox.value()
try:
community = self.account.communities.addCommunity(MainNode(server, port), self.account.keyFingerprint())
self.account.wallets.addWallet(community)
self.communityView.setModel( CommunityTreeModel(community) )
except NotMemberOfCommunityError as e:
QErrorMessage(self).showMessage(e.message)
......@@ -3,7 +3,6 @@ Created on 2 févr. 2014
@author: inso
'''
import logging
import re
from PyQt5.QtWidgets import QDialog, QDialogButtonBox
......
......@@ -4,12 +4,11 @@ Created on 6 mars 2014
@author: inso
'''
from cutecoin.gen_resources.accountConfigurationDialog_uic import Ui_AccountConfigurationDialog
from cutecoin.gui.addCommunityDialog import AddCommunityDialog
from cutecoin.models.account import Account
from cutecoin.models.account.communities import Communities
from cutecoin.gui.configureCommunityDialog import ConfigureCommunityDialog
from cutecoin.models.account.communities.listModel import CommunitiesListModel
from PyQt5.QtWidgets import QDialog
from cutecoin.models.account import Account
from cutecoin.models.account import Communities
import gnupg
......@@ -19,36 +18,50 @@ class ConfigureAccountDialog(QDialog, Ui_AccountConfigurationDialog):
'''
def __init__(self, mainWindow):
def __init__(self, account):
'''
Constructor
'''
# Set up the user interface from Designer.
super(ConfigureAccountDialog, self).__init__()
self.setupUi(self)
self.account = mainWindow.core.currentAccount
self.setWindowTitle("Configure " + self.account.name)
self.account = account
if self.account is None:
self.setWindowTitle("New account")
else:
self.setWindowTitle("Configure " + self.account.name)
self.combo_keysList.setEnabled(False)
self.setData()
def setData(self):
gpg = gnupg.GPG()
self.combo_keysList.clear()
availableKeys = gpg.list_keys(True)
if self.account is None:
self.account = Account.create(availableKeys[0]['keyid'], "", Communities())
self.combo_keysList.currentIndexChanged[int].connect(self.keyChanged)
for index, key in enumerate(availableKeys):
self.combo_keysList.addItem(key['uids'][0])
if (key['keyid']) == self.account.keyId:
self.combo_keysList.setCurrentIndex(index)
self.combo_keysList.setEnabled(False)
self.list_communities.setModel(CommunitiesListModel(self.account))
self.edit_accountName.setText(self.account.name)
def openAddCommunityDialog(self):
dialog = AddCommunityDialog(self)
dialog = ConfigureCommunityDialog(None)
dialog.setWindowTitle("Add a community")
dialog.buttonBox.accepted.connect(self.actionAddCommunity)
dialog.setAccount(self.account)
dialog.exec_()
def actionAddCommunity(self):
self.combo_keysList.setEnabled(False)
self.combo_keysList.disconnect()
self.list_communities.setModel(CommunitiesListModel(self.account))
def actionRemoveCommunity(self):
......@@ -59,4 +72,8 @@ class ConfigureAccountDialog(QDialog, Ui_AccountConfigurationDialog):
#TODO: Edit selected community
pass
def keyChanged(self, keyIndex):
gpg = gnupg.GPG()
availableKeys = gpg.list_keys(True)
self.account.keyId = availableKeys[keyIndex]['keyid']
'''
Created on 8 mars 2014
@author: inso
'''
from cutecoin.gen_resources.communityConfigurationDialog_uic import Ui_CommunityConfigurationDialog
from PyQt5.QtWidgets import QDialog, QErrorMessage
from cutecoin.models.community.treeModel import CommunityTreeModel
from cutecoin.models.node import TrustedNode
from cutecoin.core.exceptions import NotMemberOfCommunityError
class ConfigureCommunityDialog(QDialog, Ui_CommunityConfigurationDialog):
'''
classdocs
'''
def __init__(self, community):
'''
Constructor
'''
super(ConfigureCommunityDialog, self).__init__()
self.setupUi(self)
self.community = community
def setData(self):
if self.community is not None:
self.communityView.setModel( CommunityTreeModel(self.community))
def setAccount(self, account):
self.account = account
def addNode(self):
'''
Add node slot
'''
server = self.serverEdit.text()
port = self.portBox.value()
if self.community == None:
try:
self.community = self.account.communities.addCommunity(TrustedNode(server, port), self.account.keyFingerprint())
self.account.wallets.addWallet(self.community)
self.communityView.setModel( CommunityTreeModel(self.community) )
except NotMemberOfCommunityError as e:
QErrorMessage(self).showMessage(e.message)
else:
self.community.addTrustedNode( TrustedNode(server, port ))
self.communityView.setModel( CommunityTreeModel(self.community ))
def accept(self):
self.close()
......@@ -6,7 +6,6 @@ Created on 1 févr. 2014
from cutecoin.gen_resources.mainwindow_uic import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QAction, QErrorMessage
from PyQt5.QtCore import QSignalMapper
from cutecoin.gui.addAccountDialog import AddAccountDialog
from cutecoin.gui.configureAccountDialog import ConfigureAccountDialog
from cutecoin.gui.transferMoneyDialog import TransferMoneyDialog
from cutecoin.gui.communityTabWidget import CommunityTabWidget
......@@ -36,7 +35,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
self.refreshMainWindow()
def openAddAccountDialog(self):
self.addAccountDialog = AddAccountDialog(self)
self.addAccountDialog = ConfigureAccountDialog(None)
self.addAccountDialog.buttonBox.accepted.connect(self.actionAddAccount)
self.addAccountDialog.setData()
self.addAccountDialog.exec_()
......@@ -63,7 +63,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
AddContactDialog(self.core.currentAccount, self).exec_()
def openConfigureAccountDialog(self):
ConfigureAccountDialog(self).exec_()
ConfigureAccountDialog(self.core.currentAccount).exec_()
'''
Refresh main window
......
......@@ -9,20 +9,20 @@ import hashlib
import json
import logging
from cutecoin.models.node import MainNode
from cutecoin.models.node import TrustedNode
from cutecoin.models.wallet import Wallet
class Community(object):
'''
classdocs
'''
def __init__(self, knownNodes):
def __init__(self, trustedNodes):
'''
A community is a group of nodes using the same currency.
They are all using the same amendment and are syncing their datas.
An account is a member of a community if he is a member of the current amendment.
'''
self.knownNodes = knownNodes
self.trustedNodes = trustedNodes
currentAmendment = self.ucoinRequest(ucoin.hdc.amendments.Current())
self.currency = currentAmendment['currency']
......@@ -37,7 +37,7 @@ class Community(object):
def load(cls, jsonData, account):
knownNodes = []
for nodeData in jsonData['nodes']:
knownNodes.append(MainNode(nodeData['server'], nodeData['port']))
knownNodes.append(TrustedNode(nodeData['server'], nodeData['port']))
community = cls(knownNodes)
......@@ -59,7 +59,7 @@ class Community(object):
return members
def ucoinRequest(self, request, get_args={}):
for node in self.knownNodes:
for node in self.trustedNodes:
logging.debug("Trying to connect to : " + node.getText())
request = node.use(request)
return request.get(**get_args)
......@@ -103,7 +103,7 @@ class Community(object):
def jsonifyNodesList(self):
data = []
for node in self.knownNodes:
for node in self.trustedNodes:
data.append(node.jsonify())
return data
......
......@@ -89,7 +89,7 @@ class CommunityTreeModel(QAbstractItemModel):
def refreshTreeNodes(self):
logging.debug("root : " + self.rootItem.data(0))
for mainNode in self.community.knownNodes:
for mainNode in self.community.trustedNodes:
mainNodeItem = MainNodeItem(mainNode, self.rootItem)
logging.debug("mainNode : " + mainNode.getText() + " / " + mainNodeItem.data(0))
self.rootItem.appendChild(mainNodeItem)
......
......@@ -24,9 +24,9 @@ class Node(object):
return self.server + ":" + str(self.port)
class MainNode(Node):
class TrustedNode(Node):
'''
MainNode is a node the community is reading to get informations.
TrustedNode is a node the community is reading to get informations.
The account sends data one of the community main nodes.
'''
def downstreamPeers(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment