2015-10-02 22:24:46 +02:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
|
|
|
from utils import *
|
|
|
|
import shared
|
2015-11-12 00:33:57 +01:00
|
|
|
from settingsmixin import SettingsMixin
|
2015-10-02 22:24:46 +02:00
|
|
|
|
2015-10-27 19:24:29 +01:00
|
|
|
class AccountMixin (object):
|
|
|
|
def accountColor (self):
|
|
|
|
if not self.isEnabled:
|
2015-10-31 10:12:12 +01:00
|
|
|
return QtGui.QColor(128, 128, 128)
|
2015-10-27 19:24:29 +01:00
|
|
|
elif self.type == "chan":
|
2015-10-31 10:12:12 +01:00
|
|
|
return QtGui.QColor(216, 119, 0)
|
2015-10-27 19:24:29 +01:00
|
|
|
elif self.type == "mailinglist" or self.type == "subscription":
|
2015-10-31 10:12:12 +01:00
|
|
|
return QtGui.QColor(137, 04, 177)
|
2015-10-27 19:24:29 +01:00
|
|
|
else:
|
2015-10-31 10:12:12 +01:00
|
|
|
return QtGui.QApplication.palette().text().color()
|
|
|
|
|
2015-11-09 18:58:27 +01:00
|
|
|
def folderColor (self):
|
|
|
|
if not self.parent.isEnabled:
|
|
|
|
return QtGui.QColor(128, 128, 128)
|
|
|
|
else:
|
|
|
|
return QtGui.QApplication.palette().text().color()
|
|
|
|
|
2015-10-31 10:12:12 +01:00
|
|
|
def accountBrush(self):
|
2015-11-01 08:29:13 +01:00
|
|
|
brush = QtGui.QBrush(self.accountColor())
|
|
|
|
brush.setStyle(QtCore.Qt.NoBrush)
|
|
|
|
return brush
|
2015-11-09 18:58:27 +01:00
|
|
|
|
|
|
|
def folderBrush(self):
|
|
|
|
brush = QtGui.QBrush(self.folderColor())
|
|
|
|
brush.setStyle(QtCore.Qt.NoBrush)
|
|
|
|
return brush
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
def setAddress(self, address):
|
|
|
|
self.address = str(address)
|
|
|
|
self.updateText()
|
|
|
|
|
|
|
|
def setUnreadCount(self, cnt):
|
|
|
|
self.unreadCount = int(cnt)
|
|
|
|
self.updateText()
|
|
|
|
|
2015-10-27 19:24:29 +01:00
|
|
|
def setEnabled(self, enabled):
|
|
|
|
self.isEnabled = enabled
|
2015-11-09 18:45:35 +01:00
|
|
|
if hasattr(self, "setExpanded"):
|
|
|
|
self.setExpanded(enabled)
|
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)
|
2015-10-27 19:24:29 +01:00
|
|
|
self.updateText()
|
|
|
|
|
|
|
|
def setType(self):
|
|
|
|
if shared.safeConfigGetBoolean(self.address, 'chan'):
|
|
|
|
self.type = "chan"
|
|
|
|
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
|
|
|
|
self.type = "mailinglist"
|
|
|
|
else:
|
|
|
|
self.type = "normal"
|
|
|
|
|
|
|
|
def updateText(self):
|
|
|
|
pass
|
|
|
|
|
2015-10-31 15:27:07 +01:00
|
|
|
|
2015-10-27 19:24:29 +01:00
|
|
|
class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
|
|
|
|
folderWeight = {"inbox": 1, "sent": 2, "trash": 3}
|
|
|
|
def __init__(self, parent, pos = 0, address = "", folderName = "", unreadCount = 0):
|
|
|
|
super(QtGui.QTreeWidgetItem, self).__init__()
|
|
|
|
self.initialised = False
|
|
|
|
self.setAddress(address)
|
|
|
|
self.setFolderName(folderName)
|
|
|
|
self.setUnreadCount(unreadCount)
|
2015-11-09 18:58:27 +01:00
|
|
|
self.parent = parent
|
2015-10-27 19:24:29 +01:00
|
|
|
self.initialised = True
|
|
|
|
self.updateText()
|
|
|
|
parent.insertChild(pos, self)
|
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
def setFolderName(self, fname):
|
|
|
|
self.folderName = str(fname)
|
2015-10-31 15:27:07 +01:00
|
|
|
self.setData(0, QtCore.Qt.UserRole, self.folderName)
|
2015-10-02 22:24:46 +02:00
|
|
|
self.updateText()
|
|
|
|
|
|
|
|
def updateText(self):
|
2015-10-27 19:24:29 +01:00
|
|
|
if not self.initialised:
|
|
|
|
return
|
2015-10-02 22:24:46 +02:00
|
|
|
text = QtGui.QApplication.translate("MainWindow", self.folderName)
|
|
|
|
font = QtGui.QFont()
|
|
|
|
if self.unreadCount > 0:
|
|
|
|
text += " (" + str(self.unreadCount) + ")"
|
|
|
|
font.setBold(True)
|
|
|
|
else:
|
|
|
|
font.setBold(False)
|
|
|
|
self.setFont(0, font)
|
2015-11-09 18:58:27 +01:00
|
|
|
self.setForeground(0, self.folderBrush())
|
2015-10-02 22:24:46 +02:00
|
|
|
self.setText(0, text)
|
|
|
|
self.setToolTip(0, text)
|
2015-10-03 01:17:47 +02:00
|
|
|
# self.setData(0, QtCore.Qt.UserRole, [self.address, self.folderName])
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
# inbox, sent, thrash first, rest alphabetically
|
|
|
|
def __lt__(self, other):
|
|
|
|
if (isinstance(other, Ui_FolderWidget)):
|
|
|
|
if self.folderName in self.folderWeight:
|
|
|
|
x = self.folderWeight[self.folderName]
|
|
|
|
else:
|
|
|
|
x = 4
|
|
|
|
if other.folderName in self.folderWeight:
|
|
|
|
y = self.folderWeight[other.folderName]
|
|
|
|
else:
|
|
|
|
y = 4
|
2015-10-03 12:12:18 +02:00
|
|
|
reverse = False
|
|
|
|
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
|
|
|
|
reverse = True
|
2015-10-02 22:24:46 +02:00
|
|
|
if x == y:
|
2015-10-03 12:12:18 +02:00
|
|
|
return self.folderName < other.folderName
|
2015-10-02 22:24:46 +02:00
|
|
|
else:
|
2015-10-03 12:12:18 +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)
|
|
|
|
|
|
|
|
|
2015-11-12 00:33:57 +01:00
|
|
|
class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
|
2015-10-27 19:24:29 +01:00
|
|
|
def __init__(self, parent, pos = 0, address = "", unreadCount = 0, enabled = True):
|
2015-10-02 22:24:46 +02:00
|
|
|
super(QtGui.QTreeWidgetItem, self).__init__()
|
|
|
|
parent.insertTopLevelItem(pos, self)
|
|
|
|
# only set default when creating
|
|
|
|
#super(QtGui.QTreeWidgetItem, self).setExpanded(shared.config.getboolean(self.address, 'enabled'))
|
2015-10-27 19:24:29 +01:00
|
|
|
self.initialised = False
|
2015-10-05 10:32:56 +02:00
|
|
|
self.setAddress(address)
|
2015-10-27 19:24:29 +01:00
|
|
|
self.setEnabled(enabled)
|
|
|
|
self.setUnreadCount(unreadCount)
|
|
|
|
self.initialised = True
|
2015-11-09 18:45:35 +01:00
|
|
|
self.setType() # does updateText
|
2015-10-31 15:27:07 +01:00
|
|
|
|
|
|
|
def setAddress(self, address):
|
|
|
|
super(Ui_AddressWidget, self).setAddress(address)
|
|
|
|
self.setData(0, QtCore.Qt.UserRole, self.address)
|
2015-10-05 10:32:56 +02:00
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
def updateText(self):
|
2015-10-27 19:24:29 +01:00
|
|
|
if not self.initialised:
|
|
|
|
return
|
2015-10-04 10:47:51 +02:00
|
|
|
text = unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + self.address + ')'
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
font = QtGui.QFont()
|
|
|
|
if self.unreadCount > 0:
|
|
|
|
# only show message count if the child doesn't show
|
|
|
|
if not self.isExpanded():
|
|
|
|
text += " (" + str(self.unreadCount) + ")"
|
|
|
|
font.setBold(True)
|
|
|
|
else:
|
|
|
|
font.setBold(False)
|
|
|
|
self.setFont(0, font)
|
|
|
|
|
|
|
|
#set text color
|
2015-11-01 08:29:13 +01:00
|
|
|
self.setForeground(0, self.accountBrush())
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
self.setIcon(0, avatarize(self.address))
|
|
|
|
self.setText(0, text)
|
|
|
|
self.setToolTip(0, text)
|
2015-10-03 01:17:47 +02:00
|
|
|
# self.setData(0, QtCore.Qt.UserRole, [self.address, "inbox"])
|
2015-10-02 22:24:46 +02:00
|
|
|
|
|
|
|
def setExpanded(self, expand):
|
|
|
|
super(Ui_AddressWidget, self).setExpanded(expand)
|
|
|
|
self.updateText()
|
|
|
|
|
2015-10-04 10:47:51 +02:00
|
|
|
def edit(self):
|
|
|
|
self.setText(0, shared.config.get(self.address, 'label'))
|
|
|
|
super(QtGui.QAbstractItemView, self).edit()
|
|
|
|
|
2015-10-02 22:24:46 +02:00
|
|
|
# label (or address) alphabetically, disabled at the end
|
|
|
|
def __lt__(self, other):
|
|
|
|
if (isinstance(other, Ui_AddressWidget)):
|
2015-10-03 12:12:18 +02:00
|
|
|
reverse = False
|
|
|
|
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
|
|
|
|
reverse = True
|
2015-10-27 19:24:29 +01:00
|
|
|
if self.isEnabled == other.isEnabled:
|
|
|
|
if self.type == other.type:
|
2015-10-23 07:07:38 +02:00
|
|
|
if shared.config.get(self.address, 'label'):
|
|
|
|
x = shared.config.get(self.address, 'label').decode('utf-8').lower()
|
|
|
|
else:
|
|
|
|
x = self.address.decode('utf-8').lower()
|
|
|
|
if shared.config.get(other.address, 'label'):
|
|
|
|
y = shared.config.get(other.address, 'label').decode('utf-8').lower()
|
|
|
|
else:
|
|
|
|
y = other.address.decode('utf-8').lower()
|
|
|
|
return x < y
|
2015-10-02 22:24:46 +02:00
|
|
|
else:
|
2015-10-27 19:24:29 +01:00
|
|
|
return (reverse if self.type == "mailinglist" else not reverse)
|
2015-10-02 22:24:46 +02:00
|
|
|
# else:
|
2015-10-27 19:24:29 +01:00
|
|
|
return (not reverse if self.isEnabled 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
|
|
|
|
|
|
|
|
2015-10-27 19:24:29 +01:00
|
|
|
class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
|
|
|
|
def __init__(self, parent, pos = 0, address = "", unreadCount = 0, label = "", enabled = True):
|
2015-10-10 19:58:01 +02:00
|
|
|
super(QtGui.QTreeWidgetItem, self).__init__()
|
|
|
|
parent.insertTopLevelItem(pos, self)
|
|
|
|
# only set default when creating
|
|
|
|
#super(QtGui.QTreeWidgetItem, self).setExpanded(shared.config.getboolean(self.address, 'enabled'))
|
2015-10-27 19:24:29 +01:00
|
|
|
self.initialised = False
|
|
|
|
self.setAddress(address)
|
2015-10-10 19:58:01 +02:00
|
|
|
self.setEnabled(enabled)
|
2015-10-27 19:24:29 +01:00
|
|
|
self.setType()
|
2015-10-10 19:58:01 +02:00
|
|
|
self.setLabel(label)
|
2015-10-27 19:24:29 +01:00
|
|
|
self.initialised = True
|
2015-11-09 18:45:35 +01:00
|
|
|
self.setUnreadCount (unreadCount) # does updateText
|
2015-10-10 19:58:01 +02:00
|
|
|
|
|
|
|
def setLabel(self, label):
|
|
|
|
self.label = label
|
|
|
|
|
|
|
|
def setType(self):
|
|
|
|
self.type = "subscription"
|
2015-10-27 19:50:52 +01:00
|
|
|
|
|
|
|
def edit(self):
|
|
|
|
self.setText(0, self.label)
|
|
|
|
super(QtGui.QAbstractItemView, self).edit()
|
2015-10-10 19:58:01 +02:00
|
|
|
|
|
|
|
def updateText(self):
|
2015-10-27 19:24:29 +01:00
|
|
|
if not self.initialised:
|
|
|
|
return
|
2015-10-10 19:58:01 +02:00
|
|
|
text = unicode(self.label, 'utf-8)') + ' (' + self.address + ')'
|
|
|
|
|
|
|
|
font = QtGui.QFont()
|
|
|
|
if self.unreadCount > 0:
|
|
|
|
# only show message count if the child doesn't show
|
|
|
|
if not self.isExpanded():
|
|
|
|
text += " (" + str(self.unreadCount) + ")"
|
|
|
|
font.setBold(True)
|
|
|
|
else:
|
|
|
|
font.setBold(False)
|
|
|
|
self.setFont(0, font)
|
|
|
|
|
|
|
|
#set text color
|
2015-11-01 08:29:13 +01:00
|
|
|
self.setForeground(0, self.accountBrush())
|
2015-10-10 19:58:01 +02:00
|
|
|
|
|
|
|
self.setIcon(0, avatarize(self.address))
|
|
|
|
self.setText(0, text)
|
|
|
|
self.setToolTip(0, text)
|
|
|
|
# self.setData(0, QtCore.Qt.UserRole, [self.address, "inbox"])
|
|
|
|
|
|
|
|
# label (or address) alphabetically, disabled at the end
|
|
|
|
def __lt__(self, other):
|
|
|
|
if (isinstance(other, Ui_SubscriptionWidget)):
|
|
|
|
reverse = False
|
|
|
|
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
|
|
|
|
reverse = True
|
|
|
|
if self.isEnabled == other.isEnabled:
|
|
|
|
if self.label:
|
|
|
|
x = self.label.decode('utf-8').lower()
|
|
|
|
else:
|
|
|
|
x = self.address.decode('utf-8').lower()
|
|
|
|
if other.label:
|
|
|
|
y = other.label.decode('utf-8').lower()
|
|
|
|
else:
|
|
|
|
y = other.address.decode('utf-8').lower()
|
|
|
|
return x < y
|
|
|
|
# else:
|
|
|
|
return (not reverse if self.isEnabled else reverse)
|
|
|
|
|
2015-10-23 19:14:01 +02:00
|
|
|
return super(QtGui.QTreeWidgetItem, self).__lt__(other)
|
|
|
|
|
2015-10-27 19:24:29 +01:00
|
|
|
class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
|
2015-10-23 19:14:01 +02:00
|
|
|
_types = {'normal': 0, 'chan': 1, 'subscription': 2}
|
|
|
|
|
|
|
|
def __init__ (self, text, type = 'normal'):
|
|
|
|
super(QtGui.QTableWidgetItem, self).__init__(text)
|
|
|
|
self.label = text
|
2015-10-27 19:24:29 +01:00
|
|
|
self.type = type
|
2015-10-23 19:14:01 +02:00
|
|
|
try:
|
2015-10-27 19:24:29 +01:00
|
|
|
self.typeNum = self._types[self.type]
|
2015-10-23 19:14:01 +02:00
|
|
|
except:
|
|
|
|
self.type = 0
|
2015-10-27 19:24:29 +01:00
|
|
|
self.setEnabled(True)
|
2015-11-01 08:29:13 +01:00
|
|
|
self.setForeground(self.accountBrush())
|
2015-10-23 19:14:01 +02:00
|
|
|
|
|
|
|
def __lt__ (self, other):
|
|
|
|
if (isinstance(other, Ui_AddressBookWidgetItem)):
|
|
|
|
reverse = False
|
|
|
|
if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
|
|
|
|
reverse = True
|
2015-10-27 19:24:29 +01:00
|
|
|
if self.typeNum == other.typeNum:
|
2015-10-23 19:14:01 +02:00
|
|
|
return self.label.decode('utf-8').lower() < other.label.decode('utf-8').lower()
|
|
|
|
else:
|
2015-10-27 19:24:29 +01:00
|
|
|
return (not reverse if self.typeNum < other.typeNum else reverse)
|
2015-10-23 19:14:01 +02:00
|
|
|
return super(QtGui.QTableWidgetItem, self).__lt__(other)
|
|
|
|
|
|
|
|
|
|
|
|
class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem):
|
|
|
|
def __init__ (self, address, label, type):
|
|
|
|
Ui_AddressBookWidgetItem.__init__(self, label, type)
|
|
|
|
self.setIcon(avatarize(address))
|
2015-10-27 20:03:46 +01:00
|
|
|
self.setToolTip(label + " (" + address + ")")
|
2015-10-23 19:14:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem):
|
|
|
|
def __init__ (self, address, label, type):
|
|
|
|
Ui_AddressBookWidgetItem.__init__(self, address, type)
|
2015-10-27 20:03:46 +01:00
|
|
|
self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
|
|
|
|
self.setToolTip(label + " (" + address + ")")
|