diff --git a/res/ui/network_tab.ui b/res/ui/network_tab.ui index 62812626bc46e74edf436af254f08019fa02dd75..ff6022e1bf8048d243bccdc742b9f1d6cfcf4163 100644 --- a/res/ui/network_tab.ui +++ b/res/ui/network_tab.ui @@ -18,6 +18,18 @@ <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QTableView" name="table_network"> + <property name="horizontalScrollBarPolicy"> + <enum>Qt::ScrollBarAlwaysOff</enum> + </property> + <property name="sizeAdjustPolicy"> + <enum>QAbstractScrollArea::AdjustToContents</enum> + </property> + <property name="selectionBehavior"> + <enum>QAbstractItemView::SelectRows</enum> + </property> + <property name="sortingEnabled"> + <bool>true</bool> + </property> <attribute name="horizontalHeaderShowSortIndicator" stdset="0"> <bool>true</bool> </attribute> diff --git a/src/cutecoin/core/community.py b/src/cutecoin/core/community.py index 1c0bffb488b5faba974599342d788f5f5df31417..6242a98e0582f4bf6aca2edbd7dca56758c1fe10 100644 --- a/src/cutecoin/core/community.py +++ b/src/cutecoin/core/community.py @@ -9,6 +9,7 @@ from ucoinpy import PROTOCOL_VERSION from ucoinpy.documents.peer import Peer, Endpoint, BMAEndpoint from ucoinpy.documents.block import Block from ..tools.exceptions import NoPeerAvailable +from .network.node import Node import logging import inspect import hashlib @@ -80,6 +81,7 @@ class Community(object): ''' self.currency = currency self.peers = [p for p in peers if p.currency == currency] + self._nodes = [Node.from_peer(p) for p in peers] self._cache = Cache(self) self._cache.refresh() @@ -243,6 +245,10 @@ class Community(object): logging.debug("Peers found : {0}".format(peers)) return peers + @property + def nodes(self): + return self._nodes + def get_block(self, number=None): if number is None: data = self.request(bma.blockchain.Current) diff --git a/src/cutecoin/core/network/__init__.py b/src/cutecoin/core/network/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/cutecoin/core/network/node.py b/src/cutecoin/core/network/node.py new file mode 100644 index 0000000000000000000000000000000000000000..10e6f39197f02945c2ba2d09a0bc2a8aace7ef42 --- /dev/null +++ b/src/cutecoin/core/network/node.py @@ -0,0 +1,50 @@ +''' +Created on 21 févr. 2015 + +@author: inso +''' + +from ucoinpy.documents.peer import Peer, BMAEndpoint +from ucoinpy.api import bma +from ...core.person import Person +from ...tools.exceptions import PersonNotFoundError + + +class Node(object): + ''' + classdocs + ''' + + def __init__(self, endpoint, pubkey, block): + ''' + Constructor + ''' + self._endpoint = endpoint + self._pubkey = pubkey + self._block = block + + @classmethod + def from_peer(cls, peer): + e = next((e for e in peer.endpoints if type(e) is BMAEndpoint)) + informations = bma.network.Peering(e.conn_handler()).get() + try: + block = bma.blockchain.Current(e.conn_handler()).get() + block_number = block["number"] + except ValueError as e: + if '404' in e: + block_number = 0 + node_pubkey = informations["pubkey"] + + return cls(e, node_pubkey, block_number) + + @property + def pubkey(self): + return self._pubkey + + @property + def endpoint(self): + return self._endpoint + + @property + def block(self): + return self._block diff --git a/src/cutecoin/gui/network_tab.py b/src/cutecoin/gui/network_tab.py index b1da42f0a5b1208014fb5c4ad6b0417d68ff9918..6783e33be18411fe8ac94b661eb95854c7b112af 100644 --- a/src/cutecoin/gui/network_tab.py +++ b/src/cutecoin/gui/network_tab.py @@ -21,7 +21,7 @@ class NetworkTabWidget(QWidget, Ui_NetworkTabWidget): super().__init__() self.setupUi(self) model = NetworkTableModel(community) - proxy = NetworkFilterProxyModel() + proxy = NetworkFilterProxyModel(self) proxy.setSourceModel(model) self.table_network.setModel(proxy) diff --git a/src/cutecoin/models/network.py b/src/cutecoin/models/network.py index d0b0006682d2265dc64174437ff4d2457ec851c2..dc72f09a449221a7a627bb78f3d615d1a64d7e3d 100644 --- a/src/cutecoin/models/network.py +++ b/src/cutecoin/models/network.py @@ -5,14 +5,10 @@ Created on 5 févr. 2014 ''' import logging -from ..core.transfer import Transfer, Received from ..core.person import Person from ..tools.exceptions import PersonNotFoundError -from ucoinpy.documents.peer import BMAEndpoint from ucoinpy.api import bma -from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant, QSortFilterProxyModel, \ - QDateTime -from PyQt5.QtGui import QFont, QColor +from PyQt5.QtCore import QAbstractTableModel, Qt, QVariant, QSortFilterProxyModel class NetworkFilterProxyModel(QSortFilterProxyModel): @@ -35,6 +31,19 @@ class NetworkFilterProxyModel(QSortFilterProxyModel): right_data = self.sourceModel().data(right, Qt.DisplayRole) return (left_data < right_data) + def headerData(self, section, orientation, role): + if role != Qt.DisplayRole: + return QVariant() + + header_names = {'pubkey': 'Pubkey', + 'is_member': 'Membre', + 'uid': 'UID', + 'address': 'Address', + 'port': 'Port', + 'current_block': 'Block'} + type = self.sourceModel().headerData(section, orientation, role) + return header_names[type] + def data(self, index, role): source_index = self.mapToSource(index) source_data = self.sourceModel().data(source_index, role) @@ -54,30 +63,49 @@ class NetworkTableModel(QAbstractTableModel): super().__init__(parent) self.community = community self.column_types = ( + 'pubkey', + 'is_member', + 'uid', 'address', - 'port' + 'port', + 'current_block' ) @property - def peers(self): - return self.community.peers + def nodes(self): + return self.community.nodes def rowCount(self, parent): - return len(self.peers) + return len(self.nodes) def columnCount(self, parent): return len(self.column_types) def headerData(self, section, orientation, role): + if role != Qt.DisplayRole: + return QVariant() + return self.column_types[section] - def data_peer(self, peer): - e = next((e for e in peer.endpoints if type(e) is BMAEndpoint)) - informations = bma.network.peering.Peers(e.conn_handler()).get() - if e.server: - address = e.server - elif e.ipv4: - address = e.port + def data_node(self, node): + try: + person = Person.lookup(node.pubkey, self.community) + uid = person.name + except PersonNotFoundError: + uid = "" + + is_member = node.pubkey in self.community.members_pubkeys() + + address = "" + if node.endpoint.server: + address = node.endpoint.server + elif node.endpoint.ipv4: + address = node.endpoint.ipv4 + elif node.endpoint.ipv6: + address = node.endpoint.ipv6 + port = node.endpoint.port + + return (node.pubkey, is_member, uid, address, port, node.block) def data(self, index, role): row = index.row() @@ -86,9 +114,9 @@ class NetworkTableModel(QAbstractTableModel): if not index.isValid(): return QVariant() - peer = self.peers[row] + node = self.nodes[row] if role == Qt.DisplayRole: - return self.data_peer(peer)[col] + return self.data_node(node)[col] def flags(self, index): return Qt.ItemIsSelectable | Qt.ItemIsEnabled