From 8f5dfb02aebb76b5244da3317631849016700a77 Mon Sep 17 00:00:00 2001
From: inso <insomniak.fr@gmaiL.com>
Date: Sat, 20 Aug 2016 20:59:18 +0200
Subject: [PATCH] New account_cfg component

---
 src/sakia/gui/dialogs/account_cfg/__init__.py |   0
 .../account_cfg/account.py}                   |   0
 .../gui/dialogs/account_cfg}/account_cfg.ui   |   4 +-
 .../gui/dialogs/account_cfg/controller.py     | 113 ++++++++++++++++++
 src/sakia/gui/dialogs/account_cfg/model.py    |  32 +++++
 src/sakia/gui/dialogs/account_cfg/view.py     |  36 ++++++
 .../gui/dialogs/revocation/controller.py      |  10 +-
 7 files changed, 188 insertions(+), 7 deletions(-)
 create mode 100644 src/sakia/gui/dialogs/account_cfg/__init__.py
 rename src/sakia/gui/{process_cfg_account.py => dialogs/account_cfg/account.py} (100%)
 rename {res/ui => src/sakia/gui/dialogs/account_cfg}/account_cfg.ui (99%)
 create mode 100644 src/sakia/gui/dialogs/account_cfg/controller.py
 create mode 100644 src/sakia/gui/dialogs/account_cfg/model.py
 create mode 100644 src/sakia/gui/dialogs/account_cfg/view.py

diff --git a/src/sakia/gui/dialogs/account_cfg/__init__.py b/src/sakia/gui/dialogs/account_cfg/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/sakia/gui/process_cfg_account.py b/src/sakia/gui/dialogs/account_cfg/account.py
similarity index 100%
rename from src/sakia/gui/process_cfg_account.py
rename to src/sakia/gui/dialogs/account_cfg/account.py
diff --git a/res/ui/account_cfg.ui b/src/sakia/gui/dialogs/account_cfg/account_cfg.ui
similarity index 99%
rename from res/ui/account_cfg.ui
rename to src/sakia/gui/dialogs/account_cfg/account_cfg.ui
index cd47bb57..b1d3c8f3 100644
--- a/res/ui/account_cfg.ui
+++ b/src/sakia/gui/dialogs/account_cfg/account_cfg.ui
@@ -20,7 +20,7 @@
    <item>
     <widget class="QStackedWidget" name="stacked_pages">
      <property name="currentIndex">
-      <number>0</number>
+      <number>1</number>
      </property>
      <widget class="QWidget" name="page_init">
       <layout class="QVBoxLayout" name="verticalLayout_4">
@@ -105,7 +105,7 @@
        </item>
       </layout>
      </widget>
-     <widget class="QWidget" name="page_gpg">
+     <widget class="QWidget" name="page_brainwallet">
       <layout class="QVBoxLayout" name="verticalLayout_7">
        <item>
         <widget class="QLabel" name="label_3">
diff --git a/src/sakia/gui/dialogs/account_cfg/controller.py b/src/sakia/gui/dialogs/account_cfg/controller.py
new file mode 100644
index 00000000..a5861378
--- /dev/null
+++ b/src/sakia/gui/dialogs/account_cfg/controller.py
@@ -0,0 +1,113 @@
+from sakia.gui.component.controller import ComponentController
+from .view import AccountConfigView
+from .model import AccountConfigModel
+
+
+class AccountConfigController(ComponentController):
+    """
+    The AccountConfigController view
+    """
+
+    def __init__(self, parent, view, model):
+        """
+        Constructor of the AccountConfigController component
+
+        :param sakia.gui.AccountConfigController.view.AccountConfigControllerView: the view
+        :param sakia.gui.AccountConfigController.model.AccountConfigControllerModel model: the model
+        """
+        super().__init__(parent, view, model)
+
+        self.handle_next_step(init=True)
+        self.view.button_next.clicked.connect(lambda checked: self.handle_next_step(False))
+        self._steps = (
+            {
+                'page': self.view.page_init,
+                'init': self.init_name_page,
+                'next': self.account_name_selected
+            },
+            {
+                'page': self.view.page_brainwallet,
+                'init': self.init_key_page,
+                'next': self.account_key_selected
+            },
+            {
+                'page': self.view.page__communities,
+                'init': self.inir_communities,
+                'next': self.accept
+            }
+        )
+        self._current_step = 0
+
+    @classmethod
+    def create(cls, parent, app, **kwargs):
+        """
+        Instanciate a AccountConfigController component
+        :param sakia.gui.component.controller.ComponentController parent:
+        :param sakia.core.Application app:
+        :return: a new AccountConfigController controller
+        :rtype: AccountConfigControllerController
+        """
+        view = AccountConfigView(parent.view)
+        model = AccountConfigModel(None, app)
+        account_cfg = cls(parent, view, model)
+        model.setParent(account_cfg)
+        return account_cfg
+
+    @classmethod
+    def create_account(cls, parent, app):
+        """
+        Open a dialog to create a new account
+        :param parent:
+        :param app:
+        :param account:
+        :return:
+        """
+        account_cfg = cls.create(parent, app, account=None)
+        account_cfg.view.set_creation_layout()
+
+    @classmethod
+    def modify_account(cls, parent, app, account):
+        """
+        Open a dialog to modify an existing account
+        :param parent:
+        :param app:
+        :param account:
+        :return:
+        """
+        account_cfg = cls.create(parent, app, account=account)
+        account_cfg.view.set_modification_layout(account.name)
+        account_cfg._current_step = 1
+
+    def init_name_page(self):
+        """
+        Initialize an account name page
+        """
+        if self.model.account:
+            self.view.set_account_name(self.model.account.name)
+
+        self.view.button_previous.setEnabled(False)
+        self.view.button_next.setEnabled(False)
+
+    def account_name_selected(self):
+        name = self.view.account_name()
+        if self.model.account is None:
+            self.model.instanciate_account(name)
+        else:
+            self.model.rename_account(name)
+
+    def handle_next_step(self, init=False):
+        if self._current_step < len(self._steps) - 1:
+            if not init:
+                self.view.button_next.clicked.disconnect(self._steps[self._current_step]['next'])
+                self._current_step += 1
+            self._steps[self._current_step]['init']()
+            self.view.stackedWidget.setCurrentWidget(self._steps[self._current_step]['page'])
+            self.view.button_next.clicked.connect(self._steps[self._current_step]['next'])
+
+    @property
+    def view(self) -> AccountConfigView:
+        return self._view
+
+    @property
+    def model(self) -> AccountConfigModel:
+        return self._model
\ No newline at end of file
diff --git a/src/sakia/gui/dialogs/account_cfg/model.py b/src/sakia/gui/dialogs/account_cfg/model.py
new file mode 100644
index 00000000..e569382d
--- /dev/null
+++ b/src/sakia/gui/dialogs/account_cfg/model.py
@@ -0,0 +1,32 @@
+from sakia.gui.component.model import ComponentModel
+
+
+class AccountConfigModel(ComponentModel):
+    """
+    The model of AccountConfig component
+    """
+
+    def __init__(self, parent, app, account):
+        """
+
+        :param sakia.gui.dialogs.account_cfg.controller.AccountConfigController parent:
+        :param sakia.core.Application app:
+        :param sakia.core.Account account:
+        """
+        super().__init__(parent)
+        self.app = app
+        self.account = account
+
+    def instanciate_account(self, name):
+        """
+        Creates an account with a given name
+        :param str name: the name of the new account
+        """
+        self.account = self.app.create_account(name)
+
+    def rename_account(self, name):
+        """
+        Renames current account
+        :param str name: the new name
+        """
+        self.account.name = name
\ No newline at end of file
diff --git a/src/sakia/gui/dialogs/account_cfg/view.py b/src/sakia/gui/dialogs/account_cfg/view.py
new file mode 100644
index 00000000..df8b1526
--- /dev/null
+++ b/src/sakia/gui/dialogs/account_cfg/view.py
@@ -0,0 +1,36 @@
+from PyQt5.QtWidgets import QWidget
+from .account_cfg_uic import Ui_AccountConfigurationDialog
+
+
+class AccountConfigView(QWidget, Ui_AccountConfigurationDialog):
+    """
+    Home screen view
+    """
+
+    def __init__(self, parent):
+        """
+        Constructor
+        """
+        super().__init__(parent)
+        self.setupUi(self)
+
+    def set_creation_layout(self):
+        """
+        Hide unecessary buttons and display correct title
+        """
+        self.setWindowTitle(self.tr("New account"))
+        self.button_delete.hide()
+
+    def set_modification_layout(self, account_name):
+        """
+        Hide unecessary widgets for account modification
+        and display correct title
+        :return:
+        """
+        self.label_action.setText("Edit account uid")
+        self.edit_account_name.setPlaceholderText(account_name)
+        self.button_next.setEnabled(True)
+        self.setWindowTitle(self.tr("Configure {0}".format(account_name)))
+
+    def account_name(self):
+        return self.edit_account_name.text()
diff --git a/src/sakia/gui/dialogs/revocation/controller.py b/src/sakia/gui/dialogs/revocation/controller.py
index 37a25b2f..e8294ed6 100644
--- a/src/sakia/gui/dialogs/revocation/controller.py
+++ b/src/sakia/gui/dialogs/revocation/controller.py
@@ -24,12 +24,12 @@ class RevocationController(ComponentController):
         self.view.button_next.clicked.connect(lambda checked: self.handle_next_step(False))
         self._steps = (
             {
-                'page': self.ui.page_load_file,
+                'page': self.view.page_load_file,
                 'init': self.init_dialog,
                 'next': self.revocation_selected
             },
             {
-                'page': self.ui.page_destination,
+                'page': self.view.page_destination,
                 'init': self.init_publication_page,
                 'next': self.publish
             }
@@ -108,12 +108,12 @@ class RevocationController(ComponentController):
 
     @asyncify
     async def accept(self):
-        if self.ui.radio_community.isChecked():
+        if self.view.radio_community.isChecked():
             index = self.view.combo_community.currentIndex()
             result, error = await self.model.send_to_community(index)
         else:
-            server = self.ui.edit_address.text()
-            port = self.ui.spinbox_port.value()
+            server = self.view.edit_address.text()
+            port = self.view.spinbox_port.value()
             result, error = await self.model.send_to_node(server, port)
 
         if result:
-- 
GitLab