From 05ea62a8812447a4e39f1807a062f8669e4516fa Mon Sep 17 00:00:00 2001
From: vtexier <vit@free.fr>
Date: Thu, 2 Apr 2020 10:06:38 +0200
Subject: [PATCH] [enh] #798 source automatically checked when opening transfer
 dialog

---
 src/sakia/gui/sub/transfer/controller.py | 33 +++++++++++++++++++++---
 1 file changed, 29 insertions(+), 4 deletions(-)

diff --git a/src/sakia/gui/sub/transfer/controller.py b/src/sakia/gui/sub/transfer/controller.py
index 76d4f45a..a2809e7e 100644
--- a/src/sakia/gui/sub/transfer/controller.py
+++ b/src/sakia/gui/sub/transfer/controller.py
@@ -53,7 +53,7 @@ class TransferController(QObject):
         )
         self.view.spinbox_amount.valueChanged.connect(self.handle_amount_change)
         self.view.spinbox_relative.valueChanged.connect(self.handle_relative_change)
-        self.view.button_source_check.clicked.connect(self.check_source)
+        self.view.button_source_check.clicked.connect(self.check_source_dialog)
 
     @classmethod
     def create(cls, parent, app):
@@ -117,7 +117,11 @@ class TransferController(QObject):
             controller.set_amount_value(source.amount, source.base)
             controller.view.spinbox_amount.setDisabled(True)
             controller.view.spinbox_relative.setDisabled(True)
-            controller.view.button_source_check.setEnabled(True)
+            result, _ = controller.check_source(source)
+            # by default, source is unlocked, if not...
+            if not result:
+                # enabled the check button to open the errors dialog
+                controller.view.button_source_check.setEnabled(True)
         else:
             controller.view.widget_source.hide()
 
@@ -326,9 +330,19 @@ class TransferController(QObject):
         self.password_input.set_connection(self.model.connection)
         self.refresh()
 
-    def check_source(self):
+    def check_source(self, source):
+        """
+        Check source conditions lock status
+
+        Return a tupple with :
+            result: bool,
+            errors: list of tuples
+                [(condition: str, message: str, info: int|str),...]
+
+        :param source:
+        :return tuple:
+        """
         # evaluate condition
-        source = self.model.current_source
         condition = pypeg2.parse(source.conditions, Condition)
         result, _errors = self.model.app.sources_service.evaluate_condition(
             self.model.app.currency,
@@ -337,6 +351,17 @@ class TransferController(QObject):
             [],
             source.identifier,
         )
+        return result, _errors
+
+    def check_source_dialog(self):
+        """
+        Open check source result and errors dialog
+
+        :return:
+        """
+        source = self.model.current_source
+        result, _errors = self.check_source(source)
+
         # if success...
         if result:
             message = QCoreApplication.translate(
-- 
GitLab