From 892ccedd352c7b63b29a9247bb872f104b3c242e Mon Sep 17 00:00:00 2001
From: Inso <insomniak.fr@gmail.com>
Date: Sun, 5 Jul 2015 19:34:54 +0200
Subject: [PATCH] Parameter to disable system notifications (#162)

---
 res/ui/preferences.ui                     | 27 ++++++++++++++++++++
 src/cutecoin/core/account.py              | 16 +++++++++---
 src/cutecoin/core/app.py                  |  3 ++-
 src/cutecoin/gui/certification.py         |  5 +++-
 src/cutecoin/gui/community_tab.py         | 30 ++++++++++++++++-------
 src/cutecoin/gui/currency_tab.py          |  8 ++++--
 src/cutecoin/gui/mainwindow.py            |  3 ++-
 src/cutecoin/gui/preferences.py           |  4 ++-
 src/cutecoin/gui/process_cfg_community.py | 19 +++++++++-----
 src/cutecoin/gui/toast.py                 |  1 -
 src/cutecoin/gui/transactions_tab.py      |  3 ++-
 src/cutecoin/gui/transfer.py              | 17 ++++++++++---
 12 files changed, 106 insertions(+), 30 deletions(-)

diff --git a/res/ui/preferences.ui b/res/ui/preferences.ui
index a00cfadf..f4af53d0 100644
--- a/res/ui/preferences.ui
+++ b/res/ui/preferences.ui
@@ -242,6 +242,33 @@
            </item>
           </layout>
          </item>
+         <item>
+          <layout class="QHBoxLayout" name="horizontalLayout_8">
+           <property name="topMargin">
+            <number>6</number>
+           </property>
+           <item>
+            <spacer name="horizontalSpacer_3">
+             <property name="orientation">
+              <enum>Qt::Horizontal</enum>
+             </property>
+             <property name="sizeHint" stdset="0">
+              <size>
+               <width>40</width>
+               <height>20</height>
+              </size>
+             </property>
+            </spacer>
+           </item>
+           <item>
+            <widget class="QCheckBox" name="checkbox_notifications">
+             <property name="text">
+              <string>Enable notifications</string>
+             </property>
+            </widget>
+           </item>
+          </layout>
+         </item>
          <item>
           <spacer name="verticalSpacer_2">
            <property name="orientation">
diff --git a/src/cutecoin/core/account.py b/src/cutecoin/core/account.py
index d7111fa8..7ba205ac 100644
--- a/src/cutecoin/core/account.py
+++ b/src/cutecoin/core/account.py
@@ -427,13 +427,15 @@ class Account(QObject):
         strdata = bytes(reply.readAll()).decode('utf-8')
         logging.debug("Received reply : {0} : {1}".format(reply.error(), strdata))
         if reply.error() == QNetworkReply.NoError:
-            self.selfcert_broadcasted.emit()
             for r in replies:
                 try:
                     r.disconnect()
                 except TypeError as e:
                     if "disconnect()" in str(e):
                         logging.debug("Could not disconnect a reply")
+                    else:
+                        raise
+            self.selfcert_broadcasted.emit()
         else:
             for r in replies:
                 if not r.isFinished() or r.error() == QNetworkReply.NoError:
@@ -480,13 +482,15 @@ class Account(QObject):
         strdata = bytes(reply.readAll()).decode('utf-8')
         logging.debug("Received reply : {0} : {1}".format(reply.error(), strdata))
         if reply.error() == QNetworkReply.NoError:
-            self.membership_broadcasted.emit()
             for r in replies:
                 try:
                     r.disconnect()
                 except TypeError as e:
                     if "disconnect()" in str(e):
                         logging.debug("Could not disconnect a reply")
+                    else:
+                        raise
+            self.membership_broadcasted.emit()
         else:
             for r in replies:
                 if not r.isFinished() or r.error() == QNetworkReply.NoError:
@@ -535,13 +539,15 @@ class Account(QObject):
         strdata = bytes(reply.readAll()).decode('utf-8')
         logging.debug("Received reply : {0} : {1}".format(reply.error(), strdata))
         if reply.error() == QNetworkReply.NoError:
-            self.certification_broadcasted.emit()
             for r in replies:
                 try:
                     r.disconnect()
                 except TypeError as e:
                     if "disconnect()" in str(e):
                         logging.debug("Could not disconnect a reply")
+                    else:
+                        raise
+            self.certification_broadcasted.emit()
         else:
             for r in replies:
                 if not r.isFinished() or r.error() == QNetworkReply.NoError:
@@ -589,13 +595,15 @@ class Account(QObject):
         strdata = bytes(reply.readAll()).decode('utf-8')
         logging.debug("Received reply : {0} : {1}".format(reply.error(), strdata))
         if reply.error() == QNetworkReply.NoError:
-            self.revoke_broadcasted.emit()
             for r in replies:
                 try:
                     r.disconnect()
                 except TypeError as e:
                     if "disconnect()" in str(e):
                         logging.debug("Could not disconnect a reply")
+                    else:
+                        raise
+            self.revoke_broadcasted.emit()
         else:
             for r in replies:
                 if not r.isFinished() or r.error() == QNetworkReply.NoError:
diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py
index dae6046b..7b819537 100644
--- a/src/cutecoin/core/app.py
+++ b/src/cutecoin/core/app.py
@@ -54,7 +54,8 @@ class Application(QObject):
                             'ref': 0,
                             'expert_mode': False,
                             'digits_after_comma': 6,
-                            'maximized': False
+                            'maximized': False,
+                            'notifications': True
                             }
 
     @classmethod
diff --git a/src/cutecoin/gui/certification.py b/src/cutecoin/gui/certification.py
index 25f86071..1ef4d400 100644
--- a/src/cutecoin/gui/certification.py
+++ b/src/cutecoin/gui/certification.py
@@ -62,7 +62,10 @@ class CertificationDialog(QDialog, Ui_CertificationDialog):
 
     @pyqtSlot(int, str)
     def handle_error(self, error_code, text):
-        toast.display(self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
+        else:
+            QMessageBox.Critical(self, self.tr("Error", self.tr("{0} : {1}".format(error_code, text))))
         self.account.certification_broadcasted.disconnect()
         self.account.broadcast_error.disconnect(self.handle_error)
         QApplication.restoreOverrideCursor()
diff --git a/src/cutecoin/gui/community_tab.py b/src/cutecoin/gui/community_tab.py
index 89717f7d..ae672340 100644
--- a/src/cutecoin/gui/community_tab.py
+++ b/src/cutecoin/gui/community_tab.py
@@ -66,17 +66,29 @@ class CommunityTabWidget(QWidget, Ui_CommunityTabWidget):
 
         self.account.identity(self.community).inner_data_changed.connect(self.handle_account_identity_change)
         self.search_direct_connections()
-        self.account.membership_broadcasted.connect(lambda:
-                                                    toast.display(self.tr("Membership"),
-                                                                self.tr("Success sending Membership demand")))
-        self.account.revoke_broadcasted.connect(lambda:
-                                                    toast.display(self.tr("Revoke"),
-                                                                self.tr("Success sending Revoke demand")))
-        self.account.selfcert_broadcasted.connect(lambda:
-                                                    toast.display(self.tr("Self Certification"),
-                                                                self.tr("Success sending Self Certification document")))
+        self.account.membership_broadcasted.connect(self.handle_membership_broadcasted)
+        self.account.revoke_broadcasted.connect(self.handle_revoke_broadcasted)
+        self.account.selfcert_broadcasted.connect(self.handle_selfcert_broadcasted)
         self.refresh_quality_buttons()
 
+    def handle_membership_broadcasted(self):
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Membership"), self.tr("Success sending Membership demand"))
+        else:
+            QMessageBox.information(self, self.tr("Membership"), self.tr("Success sending Membership demand"))
+
+    def handle_revoke_broadcasted(self):
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Revoke"), self.tr("Success sending Revoke demand"))
+        else:
+            QMessageBox.information(self, self.tr("Revoke"), self.tr("Success sending Revoke demand"))
+
+    def handle_selfcert_broadcasted(self):
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Self Certification"), self.tr("Success sending Self Certification document"))
+        else:
+            QMessageBox.information(self.tr("Self Certification"), self.tr("Success sending Self Certification document"))
+
     def identity_context_menu(self, point):
         index = self.table_identities.indexAt(point)
         model = self.table_identities.model()
diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py
index 8f62ea38..e5904962 100644
--- a/src/cutecoin/gui/currency_tab.py
+++ b/src/cutecoin/gui/currency_tab.py
@@ -118,12 +118,16 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget):
                 days = int(expiration_time / 3600 / 24)
                 if days > 0:
                     self.status_info.append('membership_expire_soon')
-                    toast.display(self.tr("Membership expiration"),
+
+                    if self.app.preferences['notifications']:
+                        toast.display(self.tr("Membership expiration"),
                                   self.tr("<b>Warning : Membership expiration in {0} days</b>").format(days))
+
             certifiers_of = person.unique_valid_certifiers_of(self.community)
             if len(certifiers_of) < self.community.parameters['sigQty']:
                 self.status_info.append('warning_certifications')
-                toast.display(self.tr("Certifications number"),
+                if self.app.preferences['notifications']:
+                    toast.display(self.tr("Certifications number"),
                               self.tr("<b>Warning : You are certified by only {0} persons, need {1}</b>")
                               .format(len(certifiers_of),
                                      self.community.parameters['sigQty']))
diff --git a/src/cutecoin/gui/mainwindow.py b/src/cutecoin/gui/mainwindow.py
index a9c5f5e2..d991775e 100644
--- a/src/cutecoin/gui/mainwindow.py
+++ b/src/cutecoin/gui/mainwindow.py
@@ -227,7 +227,8 @@ class MainWindow(QMainWindow, Ui_MainWindow):
                 .format(version=latest[1])
             version_url = latest[2]
 
-            toast.display("Cutecoin", """<p>{version_info}</br>
+            if self.app.preferences['notifications']:
+                toast.display("Cutecoin", """<p>{version_info}</br>
 <a href={version_url}>Download link</a></p>""".format(
                 version_info=version_info,
                 version_url=version_url))
diff --git a/src/cutecoin/gui/preferences.py b/src/cutecoin/gui/preferences.py
index 6a1e5320..902b5d62 100644
--- a/src/cutecoin/gui/preferences.py
+++ b/src/cutecoin/gui/preferences.py
@@ -40,6 +40,7 @@ class PreferencesDialog(QDialog, Ui_PreferencesDialog):
         self.combo_language.setCurrentText(self.app.preferences['lang'])
         self.checkbox_expertmode.setChecked(self.app.preferences['expert_mode'])
         self.checkbox_maximize.setChecked(self.app.preferences['maximized'])
+        self.checkbox_notifications.setChecked(self.app.preferences['notifications'])
         self.spinbox_digits_comma.setValue(self.app.preferences['digits_after_comma'])
         self.spinbox_digits_comma.setMaximum(12)
         self.spinbox_digits_comma.setMinimum(1)
@@ -53,7 +54,8 @@ class PreferencesDialog(QDialog, Ui_PreferencesDialog):
                 'ref': self.combo_referential.currentIndex(),
                 'expert_mode': self.checkbox_expertmode.isChecked(),
                 'maximized': self.checkbox_maximize.isChecked(),
-                'digits_after_comma': self.spinbox_digits_comma.value()}
+                'digits_after_comma': self.spinbox_digits_comma.value(),
+                'notifications': self.checkbox_notifications.isChecked()}
         self.app.save_preferences(pref)
         toast.display(self.tr("Preferences"),
                       self.tr("A restart is needed to apply your new preferences."))
diff --git a/src/cutecoin/gui/process_cfg_community.py b/src/cutecoin/gui/process_cfg_community.py
index 6ebd82b5..993b6e57 100644
--- a/src/cutecoin/gui/process_cfg_community.py
+++ b/src/cutecoin/gui/process_cfg_community.py
@@ -101,10 +101,10 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
         """
         Constructor
 
-        :type app: cutecoin.core.App
-        :type account: cutecoin.core.Account
-        :type community: cutecoin.core.Community
-        :type password_asker: cutecoin.gui.password_asker.Password_Asker
+        :param cutecoin.core.Application app: The application
+        :param cutecoin.core.Account account: The configured account
+        :param cutecoin.core.Community community: The configured community
+        :param cutecoin.gui.password_asker.Password_Asker password_asker: The password asker
         """
         super().__init__()
         self.setupUi(self)
@@ -197,7 +197,11 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
             menu.exec_(QCursor.pos())
 
     def selfcert_sent(self, pubkey, currency):
-        toast.display(self.tr("UID Publishing"),
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("UID Publishing"),
+                      self.tr("Success publishing  your UID").format(pubkey, currency))
+        else:
+            QMessageBox.information(self, self.tr("UID Publishing"),
                       self.tr("Success publishing  your UID").format(pubkey, currency))
         self.account.certification_broadcasted.disconnect()
         self.account.broadcast_error.disconnect(self.handle_error)
@@ -206,7 +210,10 @@ class ProcessConfigureCommunity(QDialog, Ui_CommunityConfigurationDialog):
 
     @pyqtSlot(int, str)
     def handle_error(self, error_code, text):
-        toast.display(self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
+        else:
+            QMessageBox.critical(self, self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
         self.account.certification_broadcasted.disconnect()
         self.account.broadcast_error.disconnect(self.handle_error)
         QApplication.restoreOverrideCursor()
diff --git a/src/cutecoin/gui/toast.py b/src/cutecoin/gui/toast.py
index a259e9e9..afd30d77 100644
--- a/src/cutecoin/gui/toast.py
+++ b/src/cutecoin/gui/toast.py
@@ -17,7 +17,6 @@ def display(title, msg):
     logging.debug("NOTIFY DISPLAY")
     if sys.platform == "linux":
         import notify2
-        import dbus
         if not notify2.is_initted():
             logging.debug("Initialising notify2")
             notify2.init("cutecoin")
diff --git a/src/cutecoin/gui/transactions_tab.py b/src/cutecoin/gui/transactions_tab.py
index 777274f9..302c8fbc 100644
--- a/src/cutecoin/gui/transactions_tab.py
+++ b/src/cutecoin/gui/transactions_tab.py
@@ -93,7 +93,8 @@ class TransactionsTabWidget(QWidget, Ui_transactionsTabWidget):
             text = self.tr("Received {0} {1} from {2} transfers").format(amount,
                                                                self.community.currency,
                                                                len(received_list))
-            toast.display(self.tr("New transactions received"), text)
+            if self.app.preferences['notifications']:
+                toast.display(self.tr("New transactions received"), text)
 
             self.table_history.model().sourceModel().refresh_transfers()
             self.table_history.resizeColumnsToContents()
diff --git a/src/cutecoin/gui/transfer.py b/src/cutecoin/gui/transfer.py
index b62f1e7a..d514c900 100644
--- a/src/cutecoin/gui/transfer.py
+++ b/src/cutecoin/gui/transfer.py
@@ -18,9 +18,13 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
     classdocs
     """
 
-    def __init__(self, sender, password_asker):
+    def __init__(self, app, sender, password_asker):
         """
         Constructor
+        :param cutecoin.core.Application app: The application
+        :param cutecoin.core.Account sender: The sender
+        :param cutecoin.gui.password_asker.Password_Asker password_asker: The password asker
+        :return:
         """
         super().__init__()
         self.setupUi(self)
@@ -79,7 +83,11 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
 
     @pyqtSlot(str)
     def money_sent(self, receiver_uid):
-        toast.display(self.tr("Transfer"),
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Transfer"),
+                      self.tr("Success sending money to {0}").format(receiver_uid))
+        else:
+            QMessageBox.information(self, self.tr("Transfer"),
                       self.tr("Success sending money to {0}").format(receiver_uid))
         self.wallet.transfer_broadcasted.disconnect()
         self.wallet.broadcast_error.disconnect(self.handle_error)
@@ -88,7 +96,10 @@ class TransferMoneyDialog(QDialog, Ui_TransferMoneyDialog):
 
     @pyqtSlot(int, str)
     def handle_error(self, error_code, text):
-        toast.display(self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
+        if self.app.preferences['notifications']:
+            toast.display(self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
+        else:
+            QMessageBox.critical(self, self.tr("Error"), self.tr("{0} : {1}".format(error_code, text)))
         self.wallet.transfer_broadcasted.disconnect()
         self.wallet.broadcast_error.disconnect(self.handle_error)
         QApplication.restoreOverrideCursor()
-- 
GitLab