2018-10-04 16:01:38 +02:00
|
|
|
"""
|
2019-02-08 17:50:07 +01:00
|
|
|
Folder tree and messagelist widgets definitions.
|
2018-10-04 16:01:38 +02:00
|
|
|
"""
|
2019-02-08 17:50:07 +01:00
|
|
|
# pylint: disable=too-many-arguments,bad-super-call
|
|
|
|
# pylint: disable=attribute-defined-outside-init
|
2018-10-04 16:01:38 +02:00
|
|
|
|
2019-04-26 10:05:02 +02:00
|
|
|
from cgi import escape
|
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
2017-02-22 09:34:54 +01:00
|
|
|
from bmconfigparser import BMConfigParser
|
2018-10-04 16:01:38 +02:00
|
|
|
from helper_sql import sqlExecute, sqlQuery
|
2015-11-12 00:33:57 +01:00
|
|
|
from settingsmixin import SettingsMixin
|
2018-10-04 16:01:38 +02:00
|
|
|
from tr import _translate
|
|
|
|
from utils import avatarize
|
2015-10-02 22:24:46 +02:00
|
|
|
|
2018-02-12 12:21:31 +01:00
|
|
|
# for pylupdate
|
|
|
|
_translate("MainWindow", "inbox")
|
|
|
|
_translate("MainWindow", "new")
|
|
|
|
_translate("MainWindow", "sent")
|
|
|
|
_translate("MainWindow", "trash")
|
|
|
|
|
2019-02-08 17:50:07 +01:00
|
|
|
TimestampRole = QtCore.Qt.UserRole + 1
|
|
|
|
|
2018-02-12 12:21:31 +01:00
|
|
|
|
|
|
|
class AccountMixin(object):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""UI-related functionality for accounts"""
|
2015-11-26 19:41:20 +01:00
|
|
|
ALL = 0
|
|
|
|
NORMAL = 1
|
|
|
|
CHAN = 2
|
|
|
|
MAILINGLIST = 3
|
|
|
|
SUBSCRIPTION = 4
|
2015-11-27 02:10:27 +01:00
|
|
|
BROADCAST = 5
|
2015-11-26 19:41:20 +01:00
|
|
|
|
2018-03-06 12:15:41 +01:00
|
|
|
def accountColor(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""QT UI color for an account"""
|
2015-10-27 19:24:29 +01:00
|
|
|
if not self.isEnabled:
|
2015-10-31 10:12:12 +01:00
|
|
|
return QtGui.QColor(128, 128, 128)
|
2015-11-26 19:41:20 +01:00
|
|
|
elif self.type == self.CHAN:
|
2015-10-31 10:12:12 +01:00
|
|
|
return QtGui.QColor(216, 119, 0)
|
2015-11-26 19:41:20 +01:00
|
|
|
elif self.type in [self.MAILINGLIST, self.SUBSCRIPTION]:
|
2018-10-04 16:01:38 +02:00
|
|
|
return QtGui.QColor(137, 4, 177)
|
|
|
|
return QtGui.QApplication.palette().text().color()
|
2018-03-06 12:15:41 +01:00
|
|
|
|
|
|
|
def folderColor(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""QT UI color for a folder"""
|
2016-01-24 18:21:15 +01:00
|
|
|
if not self.parent().isEnabled:
|
2015-11-09 18:58:27 +01:00
|
|
|
return QtGui.QColor(128, 128, 128)
|
2018-10-04 16:01:38 +02:00
|
|
|
return QtGui.QApplication.palette().text().color()
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-10-31 10:12:12 +01:00
|
|
|
def accountBrush(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Account brush (for QT UI)"""
|
2015-11-01 08:29:13 +01:00
|
|
|
brush = QtGui.QBrush(self.accountColor())
|
|
|
|
brush.setStyle(QtCore.Qt.NoBrush)
|
|
|
|
return brush
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-11-09 18:58:27 +01:00
|
|
|
def folderBrush(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Folder brush (for QT UI)"""
|
2015-11-09 18:58:27 +01:00
|
|
|
brush = QtGui.QBrush(self.folderColor())
|
|
|
|
brush.setStyle(QtCore.Qt.NoBrush)
|
|
|
|
return brush
|
2015-10-02 22:24:46 +02:00
|
|
|
|
2019-03-25 17:35:33 +01:00
|
|
|
def accountString(self):
|
|
|
|
"""Account string suitable for use in To: field: label <address>"""
|
|
|
|
label = self._getLabel()
|
|
|
|
return (
|
|
|
|
self.address if label == self.address
|
|
|
|
else '%s <%s>' % (label, self.address)
|
|
|
|
)
|
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
def setAddress(self, address):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set bitmessage address of the object"""
|
2015-11-27 00:56:25 +01:00
|
|
|
if address is None:
|
|
|
|
self.address = None
|
|
|
|
else:
|
|
|
|
self.address = str(address)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
def setUnreadCount(self, cnt):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set number of unread messages"""
|
2018-03-06 12:15:41 +01:00
|
|
|
try:
|
|
|
|
if self.unreadCount == int(cnt):
|
|
|
|
return
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-10-02 22:24:46 +02:00
|
|
|
self.unreadCount = int(cnt)
|
2016-01-24 21:26:42 +01:00
|
|
|
if isinstance(self, QtGui.QTreeWidgetItem):
|
|
|
|
self.emitDataChanged()
|
2015-10-02 22:24:46 +02:00
|
|
|
|
2015-10-27 19:24:29 +01:00
|
|
|
def setEnabled(self, enabled):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set account enabled (QT UI)"""
|
2015-10-27 19:24:29 +01:00
|
|
|
self.isEnabled = enabled
|
2018-03-06 12:15:41 +01:00
|
|
|
try:
|
2015-11-09 18:45:35 +01:00
|
|
|
self.setExpanded(enabled)
|
2018-03-06 12:15:41 +01:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
2015-11-11 01:02:23 +01:00
|
|
|
if isinstance(self, Ui_AddressWidget):
|
|
|
|
for i in range(self.childCount()):
|
|
|
|
if isinstance(self.child(i), Ui_FolderWidget):
|
|
|
|
self.child(i).setEnabled(enabled)
|
2016-01-24 18:50:38 +01:00
|
|
|
if isinstance(self, QtGui.QTreeWidgetItem):
|
2016-01-24 18:21:15 +01:00
|
|
|
self.emitDataChanged()
|
2015-10-27 19:24:29 +01:00
|
|
|
|
|
|
|
def setType(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set account type (QT UI)"""
|
2016-01-10 17:45:49 +01:00
|
|
|
self.setFlags(self.flags() | QtCore.Qt.ItemIsEditable)
|
2015-11-26 19:41:20 +01:00
|
|
|
if self.address is None:
|
|
|
|
self.type = self.ALL
|
2016-01-10 17:45:49 +01:00
|
|
|
self.setFlags(self.flags() & ~QtCore.Qt.ItemIsEditable)
|
2017-01-11 14:27:19 +01:00
|
|
|
elif BMConfigParser().safeGetBoolean(self.address, 'chan'):
|
2015-11-26 19:41:20 +01:00
|
|
|
self.type = self.CHAN
|
2017-01-11 14:27:19 +01:00
|
|
|
elif BMConfigParser().safeGetBoolean(self.address, 'mailinglist'):
|
2015-11-26 19:41:20 +01:00
|
|
|
self.type = self.MAILINGLIST
|
2016-01-23 10:14:12 +01:00
|
|
|
elif sqlQuery(
|
2018-10-04 16:01:38 +02:00
|
|
|
'''select label from subscriptions where address=?''', self.address):
|
2016-01-23 10:14:12 +01:00
|
|
|
self.type = AccountMixin.SUBSCRIPTION
|
2015-10-27 19:24:29 +01:00
|
|
|
else:
|
2015-11-26 19:41:20 +01:00
|
|
|
self.type = self.NORMAL
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2016-01-23 22:18:07 +01:00
|
|
|
def defaultLabel(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Default label (in case no label is set manually)"""
|
2016-01-23 22:18:07 +01:00
|
|
|
queryreturn = None
|
|
|
|
retval = None
|
2018-03-06 12:15:41 +01:00
|
|
|
if self.type in (
|
|
|
|
AccountMixin.NORMAL,
|
|
|
|
AccountMixin.CHAN, AccountMixin.MAILINGLIST):
|
2016-01-23 22:18:07 +01:00
|
|
|
try:
|
2018-03-06 12:15:41 +01:00
|
|
|
retval = unicode(
|
|
|
|
BMConfigParser().get(self.address, 'label'), 'utf-8')
|
2018-10-04 16:01:38 +02:00
|
|
|
except Exception:
|
2016-01-23 22:18:07 +01:00
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from addressbook where address=?''', self.address)
|
|
|
|
elif self.type == AccountMixin.SUBSCRIPTION:
|
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from subscriptions where address=?''', self.address)
|
|
|
|
if queryreturn is not None:
|
|
|
|
if queryreturn != []:
|
|
|
|
for row in queryreturn:
|
|
|
|
retval, = row
|
|
|
|
retval = unicode(retval, 'utf-8')
|
|
|
|
elif self.address is None or self.type == AccountMixin.ALL:
|
2018-02-12 12:21:31 +01:00
|
|
|
return unicode(
|
|
|
|
str(_translate("MainWindow", "All accounts")), 'utf-8')
|
2018-03-06 12:15:41 +01:00
|
|
|
|
|
|
|
return retval or unicode(self.address, 'utf-8')
|
2015-10-27 19:24:29 +01:00
|
|
|
|
2015-10-31 15:27:07 +01:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
class BMTreeWidgetItem(QtGui.QTreeWidgetItem, AccountMixin):
|
|
|
|
"""A common abstract class for Tree widget item"""
|
2018-10-04 16:01:38 +02:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
def __init__(self, parent, pos, address, unreadCount):
|
2015-10-27 19:24:29 +01:00
|
|
|
super(QtGui.QTreeWidgetItem, self).__init__()
|
|
|
|
self.setAddress(address)
|
|
|
|
self.setUnreadCount(unreadCount)
|
2018-03-12 10:40:50 +01:00
|
|
|
self._setup(parent, pos)
|
2015-10-27 19:24:29 +01:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
def _getAddressBracket(self, unreadCount=False):
|
2018-10-04 16:01:38 +02:00
|
|
|
return " (" + str(self.unreadCount) + ")" if unreadCount else ""
|
2018-02-12 12:21:31 +01:00
|
|
|
|
2016-01-24 18:21:15 +01:00
|
|
|
def data(self, column, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Override internal QT method for returning object data"""
|
2016-01-24 18:21:15 +01:00
|
|
|
if column == 0:
|
|
|
|
if role == QtCore.Qt.DisplayRole:
|
2018-03-12 10:40:50 +01:00
|
|
|
return self._getLabel() + self._getAddressBracket(
|
|
|
|
self.unreadCount > 0)
|
|
|
|
elif role == QtCore.Qt.EditRole:
|
|
|
|
return self._getLabel()
|
|
|
|
elif role == QtCore.Qt.ToolTipRole:
|
|
|
|
return self._getLabel() + self._getAddressBracket(False)
|
2016-01-24 18:21:15 +01:00
|
|
|
elif role == QtCore.Qt.FontRole:
|
|
|
|
font = QtGui.QFont()
|
|
|
|
font.setBold(self.unreadCount > 0)
|
|
|
|
return font
|
2018-03-12 10:40:50 +01:00
|
|
|
return super(BMTreeWidgetItem, self).data(column, role)
|
|
|
|
|
|
|
|
|
|
|
|
class Ui_FolderWidget(BMTreeWidgetItem):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Item in the account/folder tree representing a folder"""
|
2018-03-12 10:40:50 +01:00
|
|
|
folderWeight = {"inbox": 1, "new": 2, "sent": 3, "trash": 4}
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, parent, pos=0, address="", folderName="", unreadCount=0):
|
|
|
|
self.setFolderName(folderName)
|
|
|
|
super(Ui_FolderWidget, self).__init__(
|
|
|
|
parent, pos, address, unreadCount)
|
|
|
|
|
|
|
|
def _setup(self, parent, pos):
|
|
|
|
parent.insertChild(pos, self)
|
|
|
|
|
|
|
|
def _getLabel(self):
|
|
|
|
return _translate("MainWindow", self.folderName)
|
|
|
|
|
|
|
|
def setFolderName(self, fname):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set folder name (for QT UI)"""
|
2018-03-12 10:40:50 +01:00
|
|
|
self.folderName = str(fname)
|
|
|
|
|
|
|
|
def data(self, column, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Override internal QT method for returning object data"""
|
2018-03-12 10:40:50 +01:00
|
|
|
if column == 0 and role == QtCore.Qt.ForegroundRole:
|
|
|
|
return self.folderBrush()
|
2016-01-24 18:21:15 +01:00
|
|
|
return super(Ui_FolderWidget, self).data(column, role)
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
# inbox, sent, thrash first, rest alphabetically
|
|
|
|
def __lt__(self, other):
|
2018-03-06 12:15:41 +01:00
|
|
|
if isinstance(other, Ui_FolderWidget):
|
2015-10-02 22:24:46 +02:00
|
|
|
if self.folderName in self.folderWeight:
|
|
|
|
x = self.folderWeight[self.folderName]
|
|
|
|
else:
|
2015-11-27 00:56:25 +01:00
|
|
|
x = 99
|
2015-10-02 22:24:46 +02:00
|
|
|
if other.folderName in self.folderWeight:
|
|
|
|
y = self.folderWeight[other.folderName]
|
|
|
|
else:
|
2015-11-27 00:56:25 +01:00
|
|
|
y = 99
|
2018-03-06 12:15:41 +01:00
|
|
|
reverse = QtCore.Qt.DescendingOrder == \
|
|
|
|
self.treeWidget().header().sortIndicatorOrder()
|
2015-10-02 22:24:46 +02:00
|
|
|
if x == y:
|
2015-10-03 12:12:18 +02:00
|
|
|
return self.folderName < other.folderName
|
2018-10-04 16:01:38 +02:00
|
|
|
return x >= y if reverse else x < y
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
return super(QtGui.QTreeWidgetItem, self).__lt__(other)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
class Ui_AddressWidget(BMTreeWidgetItem, SettingsMixin):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Item in the account/folder tree representing an account"""
|
|
|
|
def __init__(self, parent, pos=0, address=None, unreadCount=0, enabled=True):
|
2018-03-12 10:40:50 +01:00
|
|
|
super(Ui_AddressWidget, self).__init__(
|
|
|
|
parent, pos, address, unreadCount)
|
2015-10-27 19:24:29 +01:00
|
|
|
self.setEnabled(enabled)
|
2018-03-12 10:40:50 +01:00
|
|
|
|
|
|
|
def _setup(self, parent, pos):
|
2016-01-24 18:21:15 +01:00
|
|
|
self.setType()
|
2018-03-12 10:40:50 +01:00
|
|
|
parent.insertTopLevelItem(pos, self)
|
2018-02-12 12:21:31 +01:00
|
|
|
|
2015-11-26 19:41:20 +01:00
|
|
|
def _getLabel(self):
|
|
|
|
if self.address is None:
|
2018-02-12 12:21:31 +01:00
|
|
|
return unicode(_translate(
|
|
|
|
"MainWindow", "All accounts").toUtf8(), 'utf-8', 'ignore')
|
2015-11-26 19:41:20 +01:00
|
|
|
else:
|
|
|
|
try:
|
2018-02-12 12:21:31 +01:00
|
|
|
return unicode(
|
|
|
|
BMConfigParser().get(self.address, 'label'),
|
|
|
|
'utf-8', 'ignore')
|
2015-11-26 19:41:20 +01:00
|
|
|
except:
|
2015-11-27 00:56:25 +01:00
|
|
|
return unicode(self.address, 'utf-8')
|
2018-02-12 12:21:31 +01:00
|
|
|
|
2018-03-06 12:15:41 +01:00
|
|
|
def _getAddressBracket(self, unreadCount=False):
|
2018-03-12 10:40:50 +01:00
|
|
|
ret = "" if self.isExpanded() \
|
|
|
|
else super(Ui_AddressWidget, self)._getAddressBracket(unreadCount)
|
2015-11-26 19:41:20 +01:00
|
|
|
if self.address is not None:
|
|
|
|
ret += " (" + self.address + ")"
|
|
|
|
return ret
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-11-25 00:18:31 +01:00
|
|
|
def data(self, column, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Override internal QT method for returning object data"""
|
2015-11-25 00:18:31 +01:00
|
|
|
if column == 0:
|
2018-03-12 10:40:50 +01:00
|
|
|
if role == QtCore.Qt.DecorationRole:
|
|
|
|
return avatarize(
|
|
|
|
self.address or self._getLabel().encode('utf8'))
|
2015-11-25 00:18:31 +01:00
|
|
|
elif role == QtCore.Qt.ForegroundRole:
|
|
|
|
return self.accountBrush()
|
|
|
|
return super(Ui_AddressWidget, self).data(column, role)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-11-25 00:18:31 +01:00
|
|
|
def setData(self, column, role, value):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Save account label (if you edit in the the UI, this will be triggered and will save it to keys.dat)"""
|
2018-03-06 12:15:41 +01:00
|
|
|
if role == QtCore.Qt.EditRole \
|
|
|
|
and self.type != AccountMixin.SUBSCRIPTION:
|
|
|
|
BMConfigParser().set(
|
|
|
|
str(self.address), 'label',
|
2018-03-30 15:15:50 +02:00
|
|
|
str(value.toString().toUtf8())
|
|
|
|
if isinstance(value, QtCore.QVariant)
|
|
|
|
else value.encode('utf-8')
|
2018-03-06 12:15:41 +01:00
|
|
|
)
|
2017-01-15 10:50:02 +01:00
|
|
|
BMConfigParser().save()
|
2015-11-25 00:18:31 +01:00
|
|
|
return super(Ui_AddressWidget, self).setData(column, role, value)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-10-31 15:27:07 +01:00
|
|
|
def setAddress(self, address):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set address to object (for QT UI)"""
|
2015-10-31 15:27:07 +01:00
|
|
|
super(Ui_AddressWidget, self).setAddress(address)
|
|
|
|
self.setData(0, QtCore.Qt.UserRole, self.address)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-11-26 19:41:20 +01:00
|
|
|
def _getSortRank(self):
|
2018-03-12 10:40:50 +01:00
|
|
|
return self.type if self.isEnabled else (self.type + 100)
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
# label (or address) alphabetically, disabled at the end
|
|
|
|
def __lt__(self, other):
|
2018-10-04 16:01:38 +02:00
|
|
|
# pylint: disable=protected-access
|
2018-03-06 12:15:41 +01:00
|
|
|
if isinstance(other, Ui_AddressWidget):
|
|
|
|
reverse = QtCore.Qt.DescendingOrder == \
|
|
|
|
self.treeWidget().header().sortIndicatorOrder()
|
2015-11-26 19:41:20 +01:00
|
|
|
if self._getSortRank() == other._getSortRank():
|
2016-03-12 10:58:48 +01:00
|
|
|
x = self._getLabel().lower()
|
|
|
|
y = other._getLabel().lower()
|
2015-11-26 19:41:20 +01:00
|
|
|
return x < y
|
2018-03-06 12:15:41 +01:00
|
|
|
return (
|
|
|
|
not reverse
|
|
|
|
if self._getSortRank() < other._getSortRank() else reverse
|
|
|
|
)
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
return super(QtGui.QTreeWidgetItem, self).__lt__(other)
|
2015-10-10 19:58:01 +02:00
|
|
|
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
class Ui_SubscriptionWidget(Ui_AddressWidget):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Special treating of subscription addresses"""
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def __init__(self, parent, pos=0, address="", unreadCount=0, label="", enabled=True):
|
2018-03-12 10:40:50 +01:00
|
|
|
super(Ui_SubscriptionWidget, self).__init__(
|
|
|
|
parent, pos, address, unreadCount, enabled)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-11-26 19:41:20 +01:00
|
|
|
def _getLabel(self):
|
2016-01-23 22:18:07 +01:00
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from subscriptions where address=?''', self.address)
|
|
|
|
if queryreturn != []:
|
|
|
|
for row in queryreturn:
|
|
|
|
retval, = row
|
2016-03-12 10:58:48 +01:00
|
|
|
return unicode(retval, 'utf-8', 'ignore')
|
2016-01-23 22:18:07 +01:00
|
|
|
return unicode(self.address, 'utf-8')
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-10-10 19:58:01 +02:00
|
|
|
def setType(self):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set account type"""
|
2018-03-06 12:15:41 +01:00
|
|
|
super(Ui_SubscriptionWidget, self).setType() # sets it editable
|
|
|
|
self.type = AccountMixin.SUBSCRIPTION # overrides type
|
|
|
|
|
2015-11-25 00:18:31 +01:00
|
|
|
def setData(self, column, role, value):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Save subscription label to database"""
|
2015-11-25 00:18:31 +01:00
|
|
|
if role == QtCore.Qt.EditRole:
|
2016-01-23 20:24:50 +01:00
|
|
|
if isinstance(value, QtCore.QVariant):
|
2018-03-06 12:15:41 +01:00
|
|
|
label = str(
|
|
|
|
value.toString().toUtf8()).decode('utf-8', 'ignore')
|
2016-01-23 20:24:50 +01:00
|
|
|
else:
|
2016-03-12 10:58:48 +01:00
|
|
|
label = unicode(value, 'utf-8', 'ignore')
|
2015-11-25 00:18:31 +01:00
|
|
|
sqlExecute(
|
|
|
|
'''UPDATE subscriptions SET label=? WHERE address=?''',
|
2016-01-23 22:18:07 +01:00
|
|
|
label, self.address)
|
2015-11-25 00:18:31 +01:00
|
|
|
return super(Ui_SubscriptionWidget, self).setData(column, role, value)
|
2015-12-20 01:01:11 +01:00
|
|
|
|
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin):
|
|
|
|
"""A common abstract class for Table widget item"""
|
2018-10-04 16:01:38 +02:00
|
|
|
|
2019-02-08 17:50:07 +01:00
|
|
|
def __init__(self, label=None, unread=False):
|
2015-12-20 01:01:11 +01:00
|
|
|
super(QtGui.QTableWidgetItem, self).__init__()
|
|
|
|
self.setLabel(label)
|
|
|
|
self.setUnread(unread)
|
2018-03-12 10:40:50 +01:00
|
|
|
self._setup()
|
2019-02-08 17:50:07 +01:00
|
|
|
|
|
|
|
def _setup(self):
|
|
|
|
self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
|
2015-12-20 01:01:11 +01:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
def setLabel(self, label):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set object label"""
|
2018-03-12 10:40:50 +01:00
|
|
|
self.label = label
|
2015-12-20 01:21:54 +01:00
|
|
|
|
2015-12-20 01:01:11 +01:00
|
|
|
def setUnread(self, unread):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set/unset read state of an item"""
|
2015-12-20 01:01:11 +01:00
|
|
|
self.unread = unread
|
|
|
|
|
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data (QT UI)"""
|
2018-03-12 10:40:50 +01:00
|
|
|
if role in (
|
2019-02-08 17:50:07 +01:00
|
|
|
QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole
|
2018-03-12 10:40:50 +01:00
|
|
|
):
|
2015-12-20 01:01:11 +01:00
|
|
|
return self.label
|
|
|
|
elif role == QtCore.Qt.FontRole:
|
|
|
|
font = QtGui.QFont()
|
|
|
|
font.setBold(self.unread)
|
|
|
|
return font
|
2018-03-12 10:40:50 +01:00
|
|
|
return super(BMTableWidgetItem, self).data(role)
|
|
|
|
|
|
|
|
|
|
|
|
class BMAddressWidget(BMTableWidgetItem, AccountMixin):
|
|
|
|
"""A common class for Table widget item with account"""
|
2018-10-04 16:01:38 +02:00
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
def _setup(self):
|
2019-02-08 17:50:07 +01:00
|
|
|
super(BMAddressWidget, self)._setup()
|
2018-03-12 10:40:50 +01:00
|
|
|
self.setEnabled(True)
|
2019-02-08 17:50:07 +01:00
|
|
|
self.setType()
|
2018-03-12 10:40:50 +01:00
|
|
|
|
2019-03-25 17:35:33 +01:00
|
|
|
def _getLabel(self):
|
|
|
|
return self.label
|
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data (QT UI)"""
|
2018-03-12 10:40:50 +01:00
|
|
|
if role == QtCore.Qt.ToolTipRole:
|
|
|
|
return self.label + " (" + self.address + ")"
|
|
|
|
elif role == QtCore.Qt.DecorationRole:
|
|
|
|
if BMConfigParser().safeGetBoolean(
|
|
|
|
'bitmessagesettings', 'useidenticons'):
|
|
|
|
return avatarize(self.address or self.label)
|
2015-12-20 01:01:11 +01:00
|
|
|
elif role == QtCore.Qt.ForegroundRole:
|
|
|
|
return self.accountBrush()
|
2018-03-12 10:40:50 +01:00
|
|
|
return super(BMAddressWidget, self).data(role)
|
|
|
|
|
|
|
|
|
|
|
|
class MessageList_AddressWidget(BMAddressWidget):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Address item in a messagelist"""
|
2019-02-08 17:50:07 +01:00
|
|
|
def __init__(self, address=None, label=None, unread=False):
|
2018-03-12 10:40:50 +01:00
|
|
|
self.setAddress(address)
|
2019-02-08 17:50:07 +01:00
|
|
|
super(MessageList_AddressWidget, self).__init__(label, unread)
|
2018-03-12 10:40:50 +01:00
|
|
|
|
|
|
|
def setLabel(self, label=None):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set label"""
|
2018-03-12 10:40:50 +01:00
|
|
|
super(MessageList_AddressWidget, self).setLabel(label)
|
|
|
|
if label is not None:
|
|
|
|
return
|
|
|
|
newLabel = self.address
|
|
|
|
queryreturn = None
|
|
|
|
if self.type in (
|
|
|
|
AccountMixin.NORMAL,
|
|
|
|
AccountMixin.CHAN, AccountMixin.MAILINGLIST):
|
|
|
|
try:
|
|
|
|
newLabel = unicode(
|
|
|
|
BMConfigParser().get(self.address, 'label'),
|
|
|
|
'utf-8', 'ignore')
|
|
|
|
except:
|
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from addressbook where address=?''', self.address)
|
|
|
|
elif self.type == AccountMixin.SUBSCRIPTION:
|
|
|
|
queryreturn = sqlQuery(
|
|
|
|
'''select label from subscriptions where address=?''', self.address)
|
|
|
|
if queryreturn:
|
|
|
|
for row in queryreturn:
|
|
|
|
newLabel = unicode(row[0], 'utf-8', 'ignore')
|
|
|
|
|
|
|
|
self.label = newLabel
|
|
|
|
|
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data (QT UI)"""
|
2018-03-12 10:40:50 +01:00
|
|
|
if role == QtCore.Qt.UserRole:
|
2015-12-20 01:01:11 +01:00
|
|
|
return self.address
|
|
|
|
return super(MessageList_AddressWidget, self).data(role)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-12-20 01:01:11 +01:00
|
|
|
def setData(self, role, value):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set object data"""
|
2015-12-20 01:01:11 +01:00
|
|
|
if role == QtCore.Qt.EditRole:
|
|
|
|
self.setLabel()
|
|
|
|
return super(MessageList_AddressWidget, self).setData(role, value)
|
|
|
|
|
|
|
|
# label (or address) alphabetically, disabled at the end
|
|
|
|
def __lt__(self, other):
|
2018-03-06 12:15:41 +01:00
|
|
|
if isinstance(other, MessageList_AddressWidget):
|
2015-12-20 01:01:11 +01:00
|
|
|
return self.label.lower() < other.label.lower()
|
|
|
|
return super(QtGui.QTableWidgetItem, self).__lt__(other)
|
|
|
|
|
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
class MessageList_SubjectWidget(BMTableWidgetItem):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Message list subject item"""
|
2019-02-08 17:50:07 +01:00
|
|
|
def __init__(self, subject=None, label=None, unread=False):
|
2015-12-21 21:08:28 +01:00
|
|
|
self.setSubject(subject)
|
2019-02-08 17:50:07 +01:00
|
|
|
super(MessageList_SubjectWidget, self).__init__(label, unread)
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2015-12-21 21:08:28 +01:00
|
|
|
def setSubject(self, subject):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set subject"""
|
2015-12-21 21:08:28 +01:00
|
|
|
self.subject = subject
|
|
|
|
|
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data (QT UI)"""
|
2018-03-12 10:40:50 +01:00
|
|
|
if role == QtCore.Qt.UserRole:
|
2015-12-21 21:08:28 +01:00
|
|
|
return self.subject
|
2019-04-26 10:05:02 +02:00
|
|
|
if role == QtCore.Qt.ToolTipRole:
|
2019-05-22 10:58:45 +02:00
|
|
|
return escape(unicode(self.subject, 'utf-8'))
|
2015-12-21 21:08:28 +01:00
|
|
|
return super(MessageList_SubjectWidget, self).data(role)
|
|
|
|
|
|
|
|
# label (or address) alphabetically, disabled at the end
|
|
|
|
def __lt__(self, other):
|
2018-03-06 12:15:41 +01:00
|
|
|
if isinstance(other, MessageList_SubjectWidget):
|
2015-12-21 21:08:28 +01:00
|
|
|
return self.label.lower() < other.label.lower()
|
|
|
|
return super(QtGui.QTableWidgetItem, self).__lt__(other)
|
|
|
|
|
|
|
|
|
2019-02-08 17:50:07 +01:00
|
|
|
# In order for the time columns on the Inbox and Sent tabs to be sorted
|
|
|
|
# correctly (rather than alphabetically), we need to overload the <
|
|
|
|
# operator and use this class instead of QTableWidgetItem.
|
|
|
|
class MessageList_TimeWidget(BMTableWidgetItem):
|
|
|
|
"""
|
|
|
|
A subclass of QTableWidgetItem for received (lastactiontime) field.
|
|
|
|
'<' operator is overloaded to sort by TimestampRole == 33
|
|
|
|
msgid is available by QtCore.Qt.UserRole
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, label=None, unread=False, timestamp=None, msgid=''):
|
|
|
|
super(MessageList_TimeWidget, self).__init__(label, unread)
|
|
|
|
self.setData(QtCore.Qt.UserRole, QtCore.QByteArray(msgid))
|
|
|
|
self.setData(TimestampRole, int(timestamp))
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.data(TimestampRole) < other.data(TimestampRole)
|
|
|
|
|
|
|
|
def data(self, role=QtCore.Qt.UserRole):
|
|
|
|
"""
|
|
|
|
Returns expected python types for QtCore.Qt.UserRole and TimestampRole
|
|
|
|
custom roles and super for any Qt role
|
|
|
|
"""
|
|
|
|
data = super(MessageList_TimeWidget, self).data(role)
|
|
|
|
if role == TimestampRole:
|
|
|
|
return int(data.toPyObject())
|
|
|
|
if role == QtCore.Qt.UserRole:
|
|
|
|
return str(data.toPyObject())
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2018-03-12 10:40:50 +01:00
|
|
|
class Ui_AddressBookWidgetItem(BMAddressWidget):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Addressbook item"""
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def __init__(self, label=None, acc_type=AccountMixin.NORMAL):
|
|
|
|
self.type = acc_type
|
2018-03-12 10:40:50 +01:00
|
|
|
super(Ui_AddressBookWidgetItem, self).__init__(label=label)
|
2016-01-23 20:24:50 +01:00
|
|
|
|
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data"""
|
2018-03-12 10:40:50 +01:00
|
|
|
if role == QtCore.Qt.UserRole:
|
2016-01-23 20:24:50 +01:00
|
|
|
return self.type
|
|
|
|
return super(Ui_AddressBookWidgetItem, self).data(role)
|
|
|
|
|
|
|
|
def setData(self, role, value):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Set data"""
|
2016-01-23 22:18:07 +01:00
|
|
|
if role == QtCore.Qt.EditRole:
|
2018-03-12 10:40:50 +01:00
|
|
|
self.label = str(
|
|
|
|
value.toString().toUtf8()
|
|
|
|
if isinstance(value, QtCore.QVariant) else value
|
|
|
|
)
|
2018-03-06 12:15:41 +01:00
|
|
|
if self.type in (
|
|
|
|
AccountMixin.NORMAL,
|
|
|
|
AccountMixin.MAILINGLIST, AccountMixin.CHAN):
|
2016-01-23 22:18:07 +01:00
|
|
|
try:
|
2018-03-06 12:15:41 +01:00
|
|
|
BMConfigParser().get(self.address, 'label')
|
2017-01-11 14:27:19 +01:00
|
|
|
BMConfigParser().set(self.address, 'label', self.label)
|
2017-02-14 01:35:32 +01:00
|
|
|
BMConfigParser().save()
|
2016-01-23 22:18:07 +01:00
|
|
|
except:
|
|
|
|
sqlExecute('''UPDATE addressbook set label=? WHERE address=?''', self.label, self.address)
|
2016-01-23 20:24:50 +01:00
|
|
|
elif self.type == AccountMixin.SUBSCRIPTION:
|
2016-01-23 22:18:07 +01:00
|
|
|
sqlExecute('''UPDATE subscriptions set label=? WHERE address=?''', self.label, self.address)
|
2016-01-23 20:24:50 +01:00
|
|
|
else:
|
|
|
|
pass
|
2018-03-06 12:15:41 +01:00
|
|
|
return super(Ui_AddressBookWidgetItem, self).setData(role, value)
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, Ui_AddressBookWidgetItem):
|
|
|
|
reverse = QtCore.Qt.DescendingOrder == \
|
|
|
|
self.tableWidget().horizontalHeader().sortIndicatorOrder()
|
2015-10-23 19:14:01 +02:00
|
|
|
|
2015-11-26 19:41:20 +01:00
|
|
|
if self.type == other.type:
|
2016-03-12 10:58:48 +01:00
|
|
|
return self.label.lower() < other.label.lower()
|
2018-10-04 16:01:38 +02:00
|
|
|
return not reverse if self.type < other.type else reverse
|
2015-10-23 19:14:01 +02:00
|
|
|
return super(QtGui.QTableWidgetItem, self).__lt__(other)
|
|
|
|
|
|
|
|
|
|
|
|
class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Addressbook label item"""
|
|
|
|
def __init__(self, address, label, acc_type):
|
2015-11-13 14:31:22 +01:00
|
|
|
self.address = address
|
2019-02-08 17:50:07 +01:00
|
|
|
super(Ui_AddressBookWidgetItemLabel, self).__init__(label, acc_type)
|
2015-10-23 19:14:01 +02:00
|
|
|
|
2016-01-23 22:18:07 +01:00
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data"""
|
2016-01-23 22:18:07 +01:00
|
|
|
self.label = self.defaultLabel()
|
|
|
|
return super(Ui_AddressBookWidgetItemLabel, self).data(role)
|
2015-11-13 14:31:22 +01:00
|
|
|
|
2015-10-23 19:14:01 +02:00
|
|
|
|
|
|
|
class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Addressbook address item"""
|
|
|
|
def __init__(self, address, label, acc_type):
|
2016-04-28 23:05:07 +02:00
|
|
|
self.address = address
|
2019-02-08 17:50:07 +01:00
|
|
|
super(Ui_AddressBookWidgetItemAddress, self).__init__(address, acc_type)
|
2018-03-02 13:56:25 +01:00
|
|
|
|
|
|
|
def data(self, role):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Return object data"""
|
2018-03-02 13:56:25 +01:00
|
|
|
if role == QtCore.Qt.ToolTipRole:
|
|
|
|
return self.address
|
|
|
|
if role == QtCore.Qt.DecorationRole:
|
2018-10-04 16:01:38 +02:00
|
|
|
return None
|
2018-03-02 13:56:25 +01:00
|
|
|
return super(Ui_AddressBookWidgetItemAddress, self).data(role)
|
|
|
|
|
|
|
|
|
2016-04-29 02:03:48 +02:00
|
|
|
class AddressBookCompleter(QtGui.QCompleter):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Addressbook completer"""
|
|
|
|
|
2016-04-29 02:03:48 +02:00
|
|
|
def __init__(self):
|
2018-03-06 12:15:41 +01:00
|
|
|
super(AddressBookCompleter, self).__init__()
|
2016-04-29 02:03:48 +02:00
|
|
|
self.cursorPos = -1
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2018-10-04 16:01:38 +02:00
|
|
|
def onCursorPositionChanged(self, oldPos, newPos): # pylint: disable=unused-argument
|
|
|
|
"""Callback for cursor position change"""
|
2016-04-29 02:03:48 +02:00
|
|
|
if oldPos != self.cursorPos:
|
|
|
|
self.cursorPos = -1
|
2018-03-06 12:15:41 +01:00
|
|
|
|
2016-04-29 02:03:48 +02:00
|
|
|
def splitPath(self, path):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Split on semicolon"""
|
2018-03-06 12:15:41 +01:00
|
|
|
text = unicode(path.toUtf8(), 'utf-8')
|
|
|
|
return [text[:self.widget().cursorPosition()].split(';')[-1].strip()]
|
|
|
|
|
2016-04-29 02:03:48 +02:00
|
|
|
def pathFromIndex(self, index):
|
2018-10-04 16:01:38 +02:00
|
|
|
"""Perform autocompletion (reimplemented QCompleter method)"""
|
2018-03-06 12:15:41 +01:00
|
|
|
autoString = unicode(
|
|
|
|
index.data(QtCore.Qt.EditRole).toString().toUtf8(), 'utf-8')
|
|
|
|
text = unicode(self.widget().text().toUtf8(), 'utf-8')
|
|
|
|
|
2016-04-29 02:03:48 +02:00
|
|
|
# If cursor position was saved, restore it, else save it
|
|
|
|
if self.cursorPos != -1:
|
|
|
|
self.widget().setCursorPosition(self.cursorPos)
|
|
|
|
else:
|
|
|
|
self.cursorPos = self.widget().cursorPosition()
|
|
|
|
|
|
|
|
# Get current prosition
|
|
|
|
curIndex = self.widget().cursorPosition()
|
2018-03-06 12:15:41 +01:00
|
|
|
|
|
|
|
# prev_delimiter_index should actually point at final white space
|
|
|
|
# AFTER the delimiter
|
2016-04-29 02:03:48 +02:00
|
|
|
# Get index of last delimiter before current position
|
2018-03-06 12:15:41 +01:00
|
|
|
prevDelimiterIndex = text[0:curIndex].rfind(";")
|
2016-04-29 02:03:48 +02:00
|
|
|
while text[prevDelimiterIndex + 1] == " ":
|
|
|
|
prevDelimiterIndex += 1
|
2018-03-06 12:15:41 +01:00
|
|
|
|
|
|
|
# Get index of first delimiter after current position
|
|
|
|
# (or EOL if no delimiter after cursor)
|
|
|
|
nextDelimiterIndex = text.find(";", curIndex)
|
2016-04-29 02:03:48 +02:00
|
|
|
if nextDelimiterIndex == -1:
|
|
|
|
nextDelimiterIndex = len(text)
|
|
|
|
|
|
|
|
# Get part of string that occurs before cursor
|
|
|
|
part1 = text[0:prevDelimiterIndex + 1]
|
|
|
|
|
|
|
|
# Get string value from before auto finished string is selected
|
2018-03-06 12:15:41 +01:00
|
|
|
# pre = text[prevDelimiterIndex + 1:curIndex - 1]
|
2016-04-29 02:03:48 +02:00
|
|
|
|
|
|
|
# Get part of string that occurs AFTER cursor
|
|
|
|
part2 = text[nextDelimiterIndex:]
|
|
|
|
|
2018-03-06 12:15:41 +01:00
|
|
|
return part1 + autoString + part2
|