From 42faf2aaa0a2ab21d9f64e10531000337bba24d1 Mon Sep 17 00:00:00 2001 From: Yuri Date: Thu, 28 Nov 2013 17:05:53 -0800 Subject: [PATCH] [NEW FEATURE] Implemented the indicator in tray icon that there are unread messages. --- src/bitmessagemain.py | 1 + src/bitmessageqt/__init__.py | 72 +++++++++++++++++++++++++++++++----- src/helper_inbox.py | 1 + src/message_data_reader.py | 1 + 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index c05b724f..ab63664d 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -516,6 +516,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): # UPDATE is slow, only update if status is different if queryreturn != [] and (queryreturn[0][0] == 1) != readStatus: sqlExecute('''UPDATE inbox set read = ? WHERE msgid=?''', readStatus, msgid) + shared.UISignalQueue.put(('changedInboxUnread', None)) queryreturn = sqlQuery('''SELECT msgid, toaddress, fromaddress, subject, received, message, encodingtype, read FROM inbox WHERE msgid=?''', msgid) data = '{"inboxMessage":[' for row in queryreturn: diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index a3bf7ec3..7108ae3d 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -609,6 +609,8 @@ class MyForm(QtGui.QMainWindow): "updateNumberOfBroadcastsProcessed()"), self.updateNumberOfBroadcastsProcessed) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( "setStatusIcon(PyQt_PyObject)"), self.setStatusIcon) + QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( + "changedInboxUnread(PyQt_PyObject)"), self.changedInboxUnread) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( "rerenderInboxFromLabels()"), self.rerenderInboxFromLabels) QtCore.QObject.connect(self.UISignalThread, QtCore.SIGNAL( @@ -980,8 +982,7 @@ class MyForm(QtGui.QMainWindow): # create application indicator def appIndicatorInit(self, app): - self.tray = QSystemTrayIcon(QtGui.QIcon( - ":/newPrefix/images/can-icon-24px-red.png"), app) + self.initTrayIcon("can-icon-24px-red.png", app) if sys.platform[0:3] == 'win': traySignal = "activated(QSystemTrayIcon::ActivationReason)" QtCore.QObject.connect(self.tray, QtCore.SIGNAL( @@ -1539,8 +1540,7 @@ class MyForm(QtGui.QMainWindow): if self.actionStatus is not None: self.actionStatus.setText(_translate( "MainWindow", "Not Connected")) - self.tray.setIcon(QtGui.QIcon( - ":/newPrefix/images/can-icon-24px-red.png")) + self.setTrayIconFile("can-icon-24px-red.png") if color == 'yellow': if self.statusBar().currentMessage() == 'Warning: You are currently not connected. Bitmessage will do the work necessary to send the message but it won\'t send until you connect.': self.statusBar().showMessage('') @@ -1557,8 +1557,7 @@ class MyForm(QtGui.QMainWindow): if self.actionStatus is not None: self.actionStatus.setText(_translate( "MainWindow", "Connected")) - self.tray.setIcon(QtGui.QIcon( - ":/newPrefix/images/can-icon-24px-yellow.png")) + self.setTrayIconFile("can-icon-24px-yellow.png") if color == 'green': if self.statusBar().currentMessage() == 'Warning: You are currently not connected. Bitmessage will do the work necessary to send the message but it won\'t send until you connect.': self.statusBar().showMessage('') @@ -1574,8 +1573,59 @@ class MyForm(QtGui.QMainWindow): if self.actionStatus is not None: self.actionStatus.setText(_translate( "MainWindow", "Connected")) - self.tray.setIcon(QtGui.QIcon( - ":/newPrefix/images/can-icon-24px-green.png")) + self.setTrayIconFile("can-icon-24px-green.png") + + def initTrayIcon(self, iconFileName, app): + self.currentTrayIconFileName = iconFileName + self.tray = QSystemTrayIcon( + self.calcTrayIcon(iconFileName, self.findInboxUnreadCount()), app) + + def setTrayIconFile(self, iconFileName): + self.currentTrayIconFileName = iconFileName + self.drawTrayIcon(iconFileName, self.findInboxUnreadCount()) + + def calcTrayIcon(self, iconFileName, inboxUnreadCount): + pixmap = QtGui.QPixmap(":/newPrefix/images/"+iconFileName) + if inboxUnreadCount > 0: + # choose font and calculate font parameters + fontName = "Lucida" + fontSize = 10 + font = QtGui.QFont(fontName, fontSize, QtGui.QFont.Bold) + fontMetrics = QtGui.QFontMetrics(font) + # text + txt = str(inboxUnreadCount) + rect = fontMetrics.boundingRect(txt) + # margins that we add in the top-right corner + marginX = 2 + marginY = 0 # it looks like -2 is also ok due to the error of metric + # if it renders too wide we need to change it to a plus symbol + if rect.width() > 20: + txt = "+" + fontSize = 15 + font = QtGui.QFont(fontName, fontSize, QtGui.QFont.Bold) + fontMetrics = QtGui.QFontMetrics(font) + rect = fontMetrics.boundingRect(txt) + # draw text + painter = QPainter() + painter.begin(pixmap) + painter.setPen(QtGui.QPen(QtGui.QColor(255, 0, 0), Qt.SolidPattern)) + painter.setFont(font) + painter.drawText(24-rect.right()-marginX, -rect.top()+marginY, txt) + painter.end() + return QtGui.QIcon(pixmap) + + def drawTrayIcon(self, iconFileName, inboxUnreadCount): + self.tray.setIcon(self.calcTrayIcon(iconFileName, inboxUnreadCount)) + + def changedInboxUnread(self): + self.drawTrayIcon(self.currentTrayIconFileName, self.findInboxUnreadCount()) + + def findInboxUnreadCount(self): + queryreturn = sqlQuery('''SELECT count(*) from inbox WHERE folder='inbox' and read=0''') + cnt = 0 + for row in queryreturn: + cnt, = row + return int(cnt) def updateSentItemStatusByHash(self, toRipe, textToDisplay): for i in range(self.ui.tableWidgetSent.rowCount()): @@ -1622,6 +1672,7 @@ class MyForm(QtGui.QMainWindow): "MainWindow", "Message trashed")) self.ui.tableWidgetInbox.removeRow(i) break + self.changedInboxUnread() def displayAlert(self, title, text, exitAfterUserClicksOk): self.statusBar().showMessage(text) @@ -2539,6 +2590,7 @@ class MyForm(QtGui.QMainWindow): self.ui.tableWidgetInbox.item(currentRow, 1).setFont(font) self.ui.tableWidgetInbox.item(currentRow, 2).setFont(font) self.ui.tableWidgetInbox.item(currentRow, 3).setFont(font) + self.changedInboxUnread() # self.ui.tableWidgetInbox.selectRow(currentRow + 1) # This doesn't de-select the last message if you try to mark it unread, but that doesn't interfere. Might not be necessary. # We could also select upwards, but then our problem would be with the topmost message. @@ -3062,7 +3114,6 @@ class MyForm(QtGui.QMainWindow): def tableWidgetInboxItemClicked(self): currentRow = self.ui.tableWidgetInbox.currentRow() if currentRow >= 0: - font = QFont() font.setBold(False) self.ui.textEditInboxMessage.setCurrentFont(font) @@ -3105,6 +3156,7 @@ class MyForm(QtGui.QMainWindow): currentRow, 3).data(Qt.UserRole).toPyObject()) self.ubuntuMessagingMenuClear(inventoryHash) sqlExecute('''update inbox set read=1 WHERE msgid=?''', inventoryHash) + self.changedInboxUnread() def tableWidgetSentItemClicked(self): currentRow = self.ui.tableWidgetSent.currentRow() @@ -3593,6 +3645,8 @@ class UISignaler(QThread): self.emit(SIGNAL("updateNumberOfBroadcastsProcessed()")) elif command == 'setStatusIcon': self.emit(SIGNAL("setStatusIcon(PyQt_PyObject)"), data) + elif command == 'changedInboxUnread': + self.emit(SIGNAL("changedInboxUnread(PyQt_PyObject)"), data) elif command == 'rerenderInboxFromLabels': self.emit(SIGNAL("rerenderInboxFromLabels()")) elif command == 'rerenderSentToLabels': diff --git a/src/helper_inbox.py b/src/helper_inbox.py index 20fff505..1ba0e34b 100644 --- a/src/helper_inbox.py +++ b/src/helper_inbox.py @@ -3,6 +3,7 @@ import shared def insert(t): sqlExecute('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?)''', *t) + shared.UISignalQueue.put(('changedInboxUnread', None)) def trash(msgid): sqlExecute('''UPDATE inbox SET folder='trash' WHERE msgid=?''', msgid) diff --git a/src/message_data_reader.py b/src/message_data_reader.py index 084ef553..d13e1493 100644 --- a/src/message_data_reader.py +++ b/src/message_data_reader.py @@ -85,6 +85,7 @@ def markAllInboxMessagesAsUnread(): cur.execute(item, parameters) output = cur.fetchall() conn.commit() + shared.UISignalQueue.put(('changedInboxUnread', None)) print 'done' def vacuum(): -- 2.45.1