Update unread count more efficiently

Fixes #63. There are still some situations which can be improved but it
appears good enough.
This commit is contained in:
mailchuck 2015-10-31 15:27:07 +01:00
parent f356d90397
commit eb98192a0c
No known key found for this signature in database
GPG Key ID: B6311FA753FBF089
3 changed files with 73 additions and 21 deletions

View File

@ -675,6 +675,7 @@ class MyForm(QtGui.QMainWindow):
self.numberOfMessagesProcessed = 0 self.numberOfMessagesProcessed = 0
self.numberOfBroadcastsProcessed = 0 self.numberOfBroadcastsProcessed = 0
self.numberOfPubkeysProcessed = 0 self.numberOfPubkeysProcessed = 0
self.unreadCount = 0
# Set the icon sizes for the identicons # Set the icon sizes for the identicons
identicon_size = 3*7 identicon_size = 3*7
@ -874,6 +875,49 @@ class MyForm(QtGui.QMainWindow):
self.appIndicatorShow() self.appIndicatorShow()
self.ui.tabWidget.setCurrentIndex(3) self.ui.tabWidget.setCurrentIndex(3)
def propagateUnreadCount(self, address = None, folder = "inbox", widget = None, type = 1):
def updateUnreadCount(item, type = 1):
if type == 1:
item.setUnreadCount(item.unreadCount + 1)
if isinstance(item, Ui_AddressWidget):
self.drawTrayIcon(self.currentTrayIconFileName, self.findInboxUnreadCount(self.unreadCount + 1))
elif type == -1:
item.setUnreadCount(item.unreadCount - 1)
if isinstance(item, Ui_AddressWidget):
self.drawTrayIcon(self.currentTrayIconFileName, self.findInboxUnreadCount(self.unreadCount -1))
else:
if address and folder:
queryreturn = sqlQuery("SELECT COUNT(*) FROM inbox WHERE toaddress = ? AND folder = ? AND read = 0", address, folder)
elif address:
queryreturn = sqlQuery("SELECT COUNT(*) FROM inbox WHERE toaddress = ? AND read = 0", address)
elif folder:
queryreturn = sqlQuery("SELECT COUNT(*) FROM inbox WHERE folder = ? AND read = 0", folder)
else:
queryreturn = sqlQuery("SELECT COUNT(*) FROM inbox WHERE read = 0")
for row in queryreturn:
item.setUnreadCount(int(row[0]))
if isinstance(item, Ui_AddressWidget):
self.drawTrayIcon(self.currentTrayIconFileName, self.findInboxUnreadCount())
if widget == None:
widgets = [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans]
else:
widgets = [widget]
for treeWidget in widgets:
root = treeWidget.invisibleRootItem()
for i in range(root.childCount()):
addressItem = root.child(i)
if address is not None and addressItem.data(0, QtCore.Qt.UserRole) != address:
continue
updateUnreadCount(addressItem, type)
if addressItem.childCount == 0:
continue
for j in range(addressItem.childCount()):
folderItem = addressItem.child(j)
if folder is not None and folderItem.data(0, QtCore.Qt.UserRole) != folder:
continue
updateUnreadCount(folderItem, type)
# Load Sent items from database # Load Sent items from database
def loadSent(self, tableWidget, account, where="", what=""): def loadSent(self, tableWidget, account, where="", what=""):
what = "%" + what + "%" what = "%" + what + "%"
@ -1765,19 +1809,19 @@ class MyForm(QtGui.QMainWindow):
def changedInboxUnread(self, row = None): def changedInboxUnread(self, row = None):
self.drawTrayIcon(self.currentTrayIconFileName, self.findInboxUnreadCount()) self.drawTrayIcon(self.currentTrayIconFileName, self.findInboxUnreadCount())
self.rerenderTabTreeMessages() self.rerenderTabTreeMessages()
# if not row is None:
# row[1], row[6]
if self.ui.tabWidget.currentIndex() == 2:
self.rerenderTabTreeSubscriptions() self.rerenderTabTreeSubscriptions()
elif self.ui.tabWidget.currentIndex() == 3:
self.rerenderTabTreeChans() self.rerenderTabTreeChans()
def findInboxUnreadCount(self): def findInboxUnreadCount(self, count = None):
if count is None:
queryreturn = sqlQuery('''SELECT count(*) from inbox WHERE folder='inbox' and read=0''') queryreturn = sqlQuery('''SELECT count(*) from inbox WHERE folder='inbox' and read=0''')
cnt = 0 cnt = 0
for row in queryreturn: for row in queryreturn:
cnt, = row cnt, = row
return int(cnt) self.unreadCount = int(cnt)
else:
self.unreadCount = count
return self.unreadCount
def updateSentItemStatusByToAddress(self, toAddress, textToDisplay): def updateSentItemStatusByToAddress(self, toAddress, textToDisplay):
for i in range(self.ui.tableWidgetSent.rowCount()): for i in range(self.ui.tableWidgetSent.rowCount()):
@ -1816,14 +1860,15 @@ class MyForm(QtGui.QMainWindow):
self.ui.tableWidgetInbox.item(i, 3).setText(textToDisplay) self.ui.tableWidgetInbox.item(i, 3).setText(textToDisplay)
def removeInboxRowByMsgid(self, msgid): # msgid and inventoryHash are the same thing def removeInboxRowByMsgid(self, msgid): # msgid and inventoryHash are the same thing
inbox = self.getCurrentMessagelist inbox = self.getCurrentMessagelist()
for i in range(inbox.rowCount()): for i in range(inbox.rowCount()):
if msgid == str(inbox.item(i, 3).data(Qt.UserRole).toPyObject()): if msgid == str(inbox.item(i, 3).data(Qt.UserRole).toPyObject()):
self.statusBar().showMessage(_translate( self.statusBar().showMessage(_translate(
"MainWindow", "Message trashed")) "MainWindow", "Message trashed"))
inbox.removeRow(i) inbox.removeRow(i)
break break
self.changedInboxUnread() # this is a callback from core, not initiated by UI. We don't care about performance
self.propagateUnreadCount(None, None, None, 0)
def newVersionAvailable(self, version): def newVersionAvailable(self, version):
# if (not (self.windowState() & QtCore.Qt.WindowActive)) or (self.windowState() & QtCore.Qt.WindowMinimized): # if (not (self.windowState() & QtCore.Qt.WindowActive)) or (self.windowState() & QtCore.Qt.WindowMinimized):
@ -2310,6 +2355,8 @@ class MyForm(QtGui.QMainWindow):
acct.parseMessage(toAddress, fromAddress, subject, message) acct.parseMessage(toAddress, fromAddress, subject, message)
inbox = self.getAccountMessagelist(acct) inbox = self.getAccountMessagelist(acct)
treeWidget = self.getAccountTreeWidget(acct) treeWidget = self.getAccountTreeWidget(acct)
self.propagateUnreadCount(toAddress)
self.ubuntuMessagingMenuUpdate(True, newItem, acct.toLabel)
if (self.getCurrentFolder(treeWidget) != "inbox" and self.getCurrentFolder(treeWidget) != False) or self.getCurrentAccount(treeWidget) != toAddress: if (self.getCurrentFolder(treeWidget) != "inbox" and self.getCurrentFolder(treeWidget) != False) or self.getCurrentAccount(treeWidget) != toAddress:
return return
@ -2350,7 +2397,6 @@ class MyForm(QtGui.QMainWindow):
newItem.setFont(font) newItem.setFont(font)
inbox.setItem(0, 3, newItem) inbox.setItem(0, 3, newItem)
inbox.setSortingEnabled(True) inbox.setSortingEnabled(True)
self.ubuntuMessagingMenuUpdate(True, newItem, acct.toLabel)
def click_pushButtonAddAddressBook(self): def click_pushButtonAddAddressBook(self):
self.AddAddressDialogInstance = AddAddressDialog(self) self.AddAddressDialogInstance = AddAddressDialog(self)
@ -2885,7 +2931,7 @@ class MyForm(QtGui.QMainWindow):
#sqlite requires the exact number of ?s to prevent injection #sqlite requires the exact number of ?s to prevent injection
sqlExecute('''UPDATE inbox SET read=0 WHERE msgid IN (%s)''' % ( sqlExecute('''UPDATE inbox SET read=0 WHERE msgid IN (%s)''' % (
"?," * len(inventoryHashesToMarkUnread))[:-1], *inventoryHashesToMarkUnread) "?," * len(inventoryHashesToMarkUnread))[:-1], *inventoryHashesToMarkUnread)
self.changedInboxUnread() self.propagateUnreadCount(self.getCurrentAccount(), "inbox", self.getCurrentTreeWidget(), 0)
# tableWidget.selectRow(currentRow + 1) # tableWidget.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. # 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. # We could also select upwards, but then our problem would be with the topmost message.
@ -3038,7 +3084,7 @@ class MyForm(QtGui.QMainWindow):
else: else:
tableWidget.selectRow(currentRow - 1) tableWidget.selectRow(currentRow - 1)
if unread: if unread:
self.changedInboxUnread() self.propagateUnreadCount(self.getCurrentAccount(), self.getCurrentFolder(), self.getCurrentTreeWidget(), 0)
def on_action_InboxSaveMessageAs(self): def on_action_InboxSaveMessageAs(self):
tableWidget = self.getCurrentMessagelist() tableWidget = self.getCurrentMessagelist()
@ -3698,7 +3744,7 @@ class MyForm(QtGui.QMainWindow):
tableWidget.item(currentRow, 1).setFont(font) tableWidget.item(currentRow, 1).setFont(font)
tableWidget.item(currentRow, 2).setFont(font) tableWidget.item(currentRow, 2).setFont(font)
tableWidget.item(currentRow, 3).setFont(font) tableWidget.item(currentRow, 3).setFont(font)
self.changedInboxUnread() self.propagateUnreadCount(self.getCurrentAccount(), folder, self.getCurrentTreeWidget(), -1)
else: else:
data = self.getCurrentMessageId() data = self.getCurrentMessageId()

View File

@ -55,6 +55,7 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
def setFolderName(self, fname): def setFolderName(self, fname):
self.folderName = str(fname) self.folderName = str(fname)
self.setData(0, QtCore.Qt.UserRole, self.folderName)
self.updateText() self.updateText()
def updateText(self): def updateText(self):
@ -108,6 +109,10 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin):
self.initialised = True self.initialised = True
self.setExpanded(enabled) # does updateText self.setExpanded(enabled) # does updateText
def setAddress(self, address):
super(Ui_AddressWidget, self).setAddress(address)
self.setData(0, QtCore.Qt.UserRole, self.address)
def updateText(self): def updateText(self):
if not self.initialised: if not self.initialised:
return return

View File

@ -3,7 +3,8 @@ import shared
def insert(t): def insert(t):
sqlExecute('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?,?)''', *t) sqlExecute('''INSERT INTO inbox VALUES (?,?,?,?,?,?,?,?,?,?)''', *t)
shared.UISignalQueue.put(('changedInboxUnread', None)) #shouldn't emit changedInboxUnread and displayNewInboxMessage at the same time
#shared.UISignalQueue.put(('changedInboxUnread', None))
def trash(msgid): def trash(msgid):
sqlExecute('''UPDATE inbox SET folder='trash' WHERE msgid=?''', msgid) sqlExecute('''UPDATE inbox SET folder='trash' WHERE msgid=?''', msgid)