From c9cf1ded0617b1f5853e49930f69117968fa4ede Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Sun, 3 May 2015 17:06:41 +0200 Subject: [PATCH] Working on notifiations (Request #100) --- res/ui/toast.ui | 8 +++++- src/cutecoin/core/app.py | 2 +- src/cutecoin/gui/community_tab.py | 7 +++-- src/cutecoin/gui/currency_tab.py | 12 +++------ src/cutecoin/gui/toast.py | 45 +++++++++++++++++++++++++++---- 5 files changed, 55 insertions(+), 19 deletions(-) diff --git a/res/ui/toast.ui b/res/ui/toast.ui index 73ebaaed..61c442e0 100644 --- a/res/ui/toast.ui +++ b/res/ui/toast.ui @@ -13,6 +13,10 @@ <property name="windowTitle"> <string>MainWindow</string> </property> + <property name="windowIcon"> + <iconset resource="../icons/icons.qrc"> + <normaloff>:/icons/cutecoin_logo</normaloff>:/icons/cutecoin_logo</iconset> + </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> @@ -35,6 +39,8 @@ </widget> <widget class="QStatusBar" name="statusbar"/> </widget> - <resources/> + <resources> + <include location="../icons/icons.qrc"/> + </resources> <connections/> </ui> diff --git a/src/cutecoin/core/app.py b/src/cutecoin/core/app.py index 3893d143..199e2823 100644 --- a/src/cutecoin/core/app.py +++ b/src/cutecoin/core/app.py @@ -6,11 +6,11 @@ Created on 1 févr. 2014 import os import logging -import json import tarfile import shutil import json import datetime +import sys from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot, QUrl from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkReply, QNetworkRequest diff --git a/src/cutecoin/gui/community_tab.py b/src/cutecoin/gui/community_tab.py index c26b2bee..ea57c499 100644 --- a/src/cutecoin/gui/community_tab.py +++ b/src/cutecoin/gui/community_tab.py @@ -17,6 +17,7 @@ from .wot_tab import WotTabWidget from .transfer import TransferMoneyDialog from .password_asker import PasswordAskerDialog from .certification import CertificationDialog +from . import toast from ..tools.exceptions import PersonNotFoundError, NoPeerAvailable from ..core.person import Person from ucoinpy.api import bma @@ -172,8 +173,7 @@ class CommunityTabWidget(QWidget, Ui_CommunityTabWidget): try: self.account.send_membership(password, self.community, 'IN') - QMessageBox.information(self, self.tr("Membership"), - self.tr("Success sending membership demand")) + toast.display(self.tr("Membership"), self.tr("Success sending membership demand")) except ValueError as e: QMessageBox.critical(self, self.tr("Join demand error"), str(e)) @@ -204,8 +204,7 @@ The process to join back the community later will have to be done again.""") try: self.account.send_membership(password, self.community, 'OUT') - QMessageBox.information(self, self.tr("Membership"), - self.tr("Success sending leaving demand")) + toast.display(self.tr("Membership"), self.tr("Success sending leaving demand")) except ValueError as e: QMessageBox.critical(self, self.tr("Leaving demand error"), e.message) diff --git a/src/cutecoin/gui/currency_tab.py b/src/cutecoin/gui/currency_tab.py index 1cc3f3f8..966a3d24 100644 --- a/src/cutecoin/gui/currency_tab.py +++ b/src/cutecoin/gui/currency_tab.py @@ -16,7 +16,7 @@ from .wallets_tab import WalletsTabWidget from .transactions_tab import TransactionsTabWidget from .network_tab import NetworkTabWidget from .informations_tab import InformationsTabWidget -from .toast import Toast +from . import toast from ..tools.exceptions import MembershipNotFoundError from ..core.person import Person @@ -74,12 +74,8 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): if will_expire_soon: days = QDateTime().currentDateTime().daysTo(QDateTime.fromTime_t(expiration_date)) if days > 0: - QMessageBox.warning( - self, - self.tr("Membership expiration"), - self.tr("Warning : Membership expiration in {0} days").format(days), - QMessageBox.Ok - ) + toast.display(self.tr("Membership expiration" +"<b>Warning : Membership expiration in {0} days</b>").format(days)) except MembershipNotFoundError as e: pass @@ -188,7 +184,7 @@ class CurrencyTabWidget(QWidget, Ui_CurrencyTabWidget): text += """{0} """.format(t.metadata['uid']) text.format(amount, currency) - Toast(text) + toast.display(self.tr("New transactions received"), text) def refresh_wallets(self): if self.app.current_account: diff --git a/src/cutecoin/gui/toast.py b/src/cutecoin/gui/toast.py index f3903a69..f96cb04a 100644 --- a/src/cutecoin/gui/toast.py +++ b/src/cutecoin/gui/toast.py @@ -4,23 +4,58 @@ Created on 1 mai 2015 @author: inso ''' import sys, time +import logging from PyQt5.QtCore import Qt, QThread from PyQt5.QtWidgets import QMainWindow +from PyQt5.QtGui import QImage, QPixmap from ..gen_resources.toast_uic import Ui_Toast window = None # global -class Toast(QMainWindow, Ui_Toast): - def __init__(self, msg): +def display(title, msg): + if sys.platform == "linux": + import notify2 + import dbus + if not notify2.is_initted(): + logging.debug("Initialising notify2") + notify2.init("cutecoin") + n = notify2.Notification(title, + msg) + + # Not working... Empty icon at the moment. + icon = QPixmap(":/icons/cutecoin_logo/").toImage() + if icon.isNull(): + logging.debug("Error converting logo") + else: + icon.convertToFormat(QImage.Format_ARGB32) + icon_bytes = icon.bits().asstring(icon.byteCount()) + icon_struct = ( + icon.width(), + icon.height(), + icon.bytesPerLine(), + icon.hasAlphaChannel(), + 32, + 4, + dbus.ByteArray(icon_bytes) + ) + n.set_hint('icon_data', icon_struct) + n.set_timeout(5000) + n.show() + else: + _Toast(title, msg) + +class _Toast(QMainWindow, Ui_Toast): + def __init__(self, title, msg): global window # some space outside the local stack window = self # save pointer till killed to avoid GC super().__init__() self.setWindowFlags(Qt.FramelessWindowHint) self.setupUi(self) - self.display.setText(msg) + self.display.setText("""<h1>{0}</h1> +<p>{1}</p>""".format(title, msg)) - self.toastThread = ToastThread() # start thread to remove display + self.toastThread = _ToastThread() # start thread to remove display self.toastThread.finished.connect(self.toastDone) self.toastThread.start() self.show() @@ -29,7 +64,7 @@ class Toast(QMainWindow, Ui_Toast): global window window = None # kill pointer to window object to close it and GC -class ToastThread(QThread): +class _ToastThread(QThread): def __init__(self): QThread.__init__(self) -- GitLab