Change unread status of message in "All Accounts"

when it changed in "Chans" and vice versa (#1044, #884)
This commit is contained in:
Dmitri Bogomolov 2018-02-06 15:48:56 +02:00
parent 85cce42de0
commit 4a3f8f4806
Signed by untrusted user: g1itch
GPG Key ID: 720A756F18DEED13

View File

@ -911,6 +911,46 @@ class MyForm(settingsmixin.SMainWindow):
self.ui.tabWidget.indexOf(self.ui.chans) self.ui.tabWidget.indexOf(self.ui.chans)
) )
def updateUnreadStatus(self, widget, row, msgid, unread=True):
"""
Switch unread for item of msgid and related items in
other STableWidgets "All Accounts" and "Chans"
"""
related = [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans]
try:
related.remove(widget)
related = related.pop()
except ValueError:
rrow = None
related = []
else:
# maybe use instead:
# rrow = related.row(msgid), msgid should be QTableWidgetItem
# related = related.findItems(msgid, QtCore.Qt.MatchExactly),
# returns an empty list
for rrow in xrange(related.rowCount()):
if msgid == str(related.item(rrow, 3).data(
QtCore.Qt.UserRole).toPyObject()):
break
else:
rrow = None
status = widget.item(row, 0).unread
if status == unread:
font = QtGui.QFont()
font.setBold(not status)
widget.item(row, 3).setFont(font)
for col in (0, 1, 2):
widget.item(row, col).setUnread(not status)
try:
related.item(rrow, 3).setFont(font)
except (TypeError, AttributeError):
pass
else:
for col in (0, 1, 2):
related.item(rrow, col).setUnread(not status)
def propagateUnreadCount(self, address = None, folder = "inbox", widget = None, type = 1): def propagateUnreadCount(self, address = None, folder = "inbox", widget = None, type = 1):
widgets = [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] widgets = [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans]
queryReturn = sqlQuery("SELECT toaddress, folder, COUNT(msgid) AS cnt FROM inbox WHERE read = 0 GROUP BY toaddress, folder") queryReturn = sqlQuery("SELECT toaddress, folder, COUNT(msgid) AS cnt FROM inbox WHERE read = 0 GROUP BY toaddress, folder")
@ -2583,7 +2623,7 @@ class MyForm(settingsmixin.SMainWindow):
), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No ), QtGui.QMessageBox.Yes | QtGui.QMessageBox.No
) != QtGui.QMessageBox.Yes: ) != QtGui.QMessageBox.Yes:
return return
addressAtCurrentRow = self.getCurrentAccount() # addressAtCurrentRow = self.getCurrentAccount()
tableWidget = self.getCurrentMessagelist() tableWidget = self.getCurrentMessagelist()
idCount = tableWidget.rowCount() idCount = tableWidget.rowCount()
@ -2610,8 +2650,8 @@ class MyForm(settingsmixin.SMainWindow):
) )
if markread > 0: if markread > 0:
self.propagateUnreadCount( self.propagateUnreadCount()
addressAtCurrentRow, self.getCurrentFolder(), None, 0) # addressAtCurrentRow, self.getCurrentFolder(), None, 0)
def click_NewAddressDialog(self): def click_NewAddressDialog(self):
dialogs.NewAddressDialog(self) dialogs.NewAddressDialog(self)
@ -2844,36 +2884,32 @@ class MyForm(settingsmixin.SMainWindow):
tableWidget = self.getCurrentMessagelist() tableWidget = self.getCurrentMessagelist()
if not tableWidget: if not tableWidget:
return return
font = QtGui.QFont()
font.setBold(True) msgids = set()
inventoryHashesToMarkUnread = [] # modified = 0
modified = 0
for row in tableWidget.selectedIndexes(): for row in tableWidget.selectedIndexes():
currentRow = row.row() currentRow = row.row()
inventoryHashToMarkUnread = str(tableWidget.item( msgid = str(tableWidget.item(
currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) currentRow, 3).data(QtCore.Qt.UserRole).toPyObject())
if inventoryHashToMarkUnread in inventoryHashesToMarkUnread: msgids.add(msgid)
# it returns columns as separate items, so we skip dupes # if not tableWidget.item(currentRow, 0).unread:
continue # modified += 1
if not tableWidget.item(currentRow, 0).unread: self.updateUnreadStatus(tableWidget, currentRow, msgid, False)
modified += 1
inventoryHashesToMarkUnread.append(inventoryHashToMarkUnread)
tableWidget.item(currentRow, 0).setUnread(True)
tableWidget.item(currentRow, 1).setUnread(True)
tableWidget.item(currentRow, 2).setUnread(True)
tableWidget.item(currentRow, 3).setFont(font)
# for 1081 # for 1081
idCount = len(inventoryHashesToMarkUnread) idCount = len(msgids)
rowcount = sqlExecuteChunked( # rowcount =
sqlExecuteChunked(
'''UPDATE inbox SET read=0 WHERE msgid IN ({0}) AND read=1''', '''UPDATE inbox SET read=0 WHERE msgid IN ({0}) AND read=1''',
idCount, *inventoryHashesToMarkUnread idCount, *msgids
) )
if rowcount == 1: self.propagateUnreadCount()
# performance optimisation # if rowcount == 1:
self.propagateUnreadCount(tableWidget.item(currentRow, 1 if tableWidget.item(currentRow, 1).type == AccountMixin.SUBSCRIPTION else 0).data(QtCore.Qt.UserRole), self.getCurrentFolder()) # # performance optimisation
else: # self.propagateUnreadCount(tableWidget.item(currentRow, 1 if tableWidget.item(currentRow, 1).type == AccountMixin.SUBSCRIPTION else 0).data(QtCore.Qt.UserRole), self.getCurrentFolder())
self.propagateUnreadCount(tableWidget.item(currentRow, 1 if tableWidget.item(currentRow, 1).type == AccountMixin.SUBSCRIPTION else 0).data(QtCore.Qt.UserRole), self.getCurrentFolder(), self.getCurrentTreeWidget(), 0) # else:
# self.propagateUnreadCount(tableWidget.item(currentRow, 1 if tableWidget.item(currentRow, 1).type == AccountMixin.SUBSCRIPTION else 0).data(QtCore.Qt.UserRole), self.getCurrentFolder(), 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.
@ -3953,54 +3989,38 @@ class MyForm(settingsmixin.SMainWindow):
messageTextedit = self.getCurrentMessageTextedit() messageTextedit = self.getCurrentMessageTextedit()
if not messageTextedit: if not messageTextedit:
return return
queryreturn = []
message = ""
if folder == 'sent': msgid = self.getCurrentMessageId()
ackdata = self.getCurrentMessageId() if msgid:
if ackdata and messageTextedit: queryreturn = sqlQuery(
queryreturn = sqlQuery( '''SELECT message FROM %s WHERE %s=?''' % (
'''select message, 1 from sent where ackdata=?''', ackdata) ('sent', 'ackdata') if folder == 'sent'
else ('inbox', 'msgid')
), msgid
)
try:
message = queryreturn[-1][0]
except NameError:
message = ""
except IndexError:
message = _translate(
"MainWindow",
"Error occurred: could not load message from disk."
)
else: else:
msgid = self.getCurrentMessageId()
if msgid and messageTextedit:
queryreturn = sqlQuery(
'''select message, read from inbox where msgid=?''', msgid)
if queryreturn != []:
refresh = False
propagate = False
tableWidget = self.getCurrentMessagelist() tableWidget = self.getCurrentMessagelist()
currentRow = tableWidget.currentRow() currentRow = tableWidget.currentRow()
for row in queryreturn: # refresh
message, read = row if tableWidget.item(currentRow, 0).unread is True:
if tableWidget.item(currentRow, 0).unread == True: self.updateUnreadStatus(tableWidget, currentRow, msgid)
refresh = True # propagate
if folder != 'sent': if folder != 'sent' and sqlExecute(
markread = sqlExecute( '''UPDATE inbox SET read=1 WHERE msgid=? AND read=0''',
'''UPDATE inbox SET read = 1 WHERE msgid = ? AND read=0''', msgid) msgid
if markread > 0: ) > 0:
propagate = True self.propagateUnreadCount()
if refresh:
if not tableWidget:
return
font = QtGui.QFont()
font.setBold(False)
# inventoryHashesToMarkRead = []
# inventoryHashToMarkRead = str(tableWidget.item(
# currentRow, 3).data(Qt.UserRole).toPyObject())
# inventoryHashesToMarkRead.append(inventoryHashToMarkRead)
tableWidget.item(currentRow, 0).setUnread(False)
tableWidget.item(currentRow, 1).setUnread(False)
tableWidget.item(currentRow, 2).setUnread(False)
tableWidget.item(currentRow, 3).setFont(font)
if propagate:
self.propagateUnreadCount(tableWidget.item(currentRow, 1 if tableWidget.item(currentRow, 1).type == AccountMixin.SUBSCRIPTION else 0).data(QtCore.Qt.UserRole), folder, self.getCurrentTreeWidget(), -1)
else:
data = self.getCurrentMessageId()
if data != False:
message = "Error occurred: could not load message from disk."
messageTextedit.setCurrentFont(QtGui.QFont()) messageTextedit.setCurrentFont(QtGui.QFont())
messageTextedit.setTextColor(QtGui.QColor()) messageTextedit.setTextColor(QtGui.QColor())
messageTextedit.setContent(message) messageTextedit.setContent(message)