Avoid strings in account types

This commit is contained in:
mailchuck 2015-11-26 19:41:20 +01:00 committed by Peter Surda
parent 399100e6d8
commit 1e89616c0f
2 changed files with 79 additions and 99 deletions

View File

@ -886,7 +886,7 @@ class MyForm(settingsmixin.SMainWindow):
def updateUnreadCount(item): def updateUnreadCount(item):
# if refreshing the account root, we need to rescan folders # if refreshing the account root, we need to rescan folders
if type == 0 or (folder is None and isinstance(item, Ui_FolderWidget)): if type == 0 or (folder is None and isinstance(item, Ui_FolderWidget)):
if addressItem.type == 'subscription' or addressItem.type == 'mailinglist': if addressItem.type in [AccountMixin.SUBSCRIPTION, AccountMixin.MAILINGLIST]:
xAddress = "fromaddress" xAddress = "fromaddress"
else: else:
xAddress = "toaddress" xAddress = "toaddress"
@ -2058,20 +2058,20 @@ class MyForm(settingsmixin.SMainWindow):
queryreturn = sqlQuery('SELECT label, address FROM subscriptions WHERE enabled = 1') queryreturn = sqlQuery('SELECT label, address FROM subscriptions WHERE enabled = 1')
for row in queryreturn: for row in queryreturn:
label, address = row label, address = row
addRow(address, label, 'subscription') addRow(address, label, AccountMixin.SUBSCRIPTION)
# chans # chans
addresses = getSortedAccounts() addresses = getSortedAccounts()
for address in addresses: for address in addresses:
account = accountClass(address) account = accountClass(address)
if (account.type == 'chan' and shared.safeConfigGetBoolean(address, 'enabled')): if (account.type == AccountMixin.CHAN and shared.safeConfigGetBoolean(address, 'enabled')):
addRow(address, account.getLabel(), 'chan') addRow(address, account.getLabel(), AccountMixin.CHAN)
# normal accounts # normal accounts
queryreturn = sqlQuery('SELECT * FROM addressbook') queryreturn = sqlQuery('SELECT * FROM addressbook')
for row in queryreturn: for row in queryreturn:
label, address = row label, address = row
addRow(address, label, 'normal') addRow(address, label, AccountMixin.NORMAL)
# sort # sort
self.ui.tableWidgetAddressBook.sortItems(0, QtCore.Qt.AscendingOrder) self.ui.tableWidgetAddressBook.sortItems(0, QtCore.Qt.AscendingOrder)
@ -2846,7 +2846,7 @@ class MyForm(settingsmixin.SMainWindow):
addressAtCurrentRow = self.getCurrentAccount() addressAtCurrentRow = self.getCurrentAccount()
acct = accountClass(addressAtCurrentRow) acct = accountClass(addressAtCurrentRow)
# no chans / mailinglists # no chans / mailinglists
if acct.type != 'normal': if acct.type != AccountMixin.NORMAL:
return return
if self.dialog.ui.radioButtonUnregister.isChecked() and isinstance(acct, GatewayAccount): if self.dialog.ui.radioButtonUnregister.isChecked() and isinstance(acct, GatewayAccount):
acct.unregister() acct.unregister()
@ -3370,7 +3370,7 @@ class MyForm(settingsmixin.SMainWindow):
currentRow = row.row() currentRow = row.row()
type = str(self.ui.tableWidgetAddressBook.item( type = str(self.ui.tableWidgetAddressBook.item(
currentRow, 0).data(Qt.UserRole).toPyObject()) currentRow, 0).data(Qt.UserRole).toPyObject())
if type != "normal": if type != AccountMixin.NORMAL:
normal = False normal = False
if normal: if normal:
# only if all selected addressbook items are normal, allow delete # only if all selected addressbook items are normal, allow delete
@ -3527,9 +3527,9 @@ class MyForm(settingsmixin.SMainWindow):
def getAccountTreeWidget(self, account): def getAccountTreeWidget(self, account):
try: try:
if account.type == 'chan': if account.type == AccountMixin.CHAN:
return self.ui.treeWidgetChans return self.ui.treeWidgetChans
elif account.type == 'subscription': elif account.type == AccountMixin.SUBSCRIPTION:
return self.ui.treeWidgetSubscriptions return self.ui.treeWidgetSubscriptions
else: else:
return self.ui.treeWidgetYourIdentities return self.ui.treeWidgetYourIdentities
@ -3551,9 +3551,9 @@ class MyForm(settingsmixin.SMainWindow):
def getAccountMessagelist(self, account): def getAccountMessagelist(self, account):
try: try:
if account.type == 'chan': if account.type == AccountMixin.CHAN:
return self.ui.tableWidgetInboxChans return self.ui.tableWidgetInboxChans
elif account.type == 'subscription': elif account.type == AccountMixin.SUBSCRIPTION:
return self.ui.tableWidgetInboxSubscriptions return self.ui.tableWidgetInboxSubscriptions
else: else:
return self.ui.tableWidgetInbox return self.ui.tableWidgetInbox
@ -3585,9 +3585,9 @@ class MyForm(settingsmixin.SMainWindow):
def getAccountTextedit(self, account): def getAccountTextedit(self, account):
try: try:
if account.type == 'chan': if account.type == AccountMixin.CHAN:
return self.ui.textEditInboxMessageChans return self.ui.textEditInboxMessageChans
elif account.type == 'subscription': elif account.type == AccountMixin.SUBSCRIPTION:
return self.ui.textEditInboxSubscriptions return self.ui.textEditInboxSubscriptions
else: else:
return self.ui.textEditInboxMessage return self.ui.textEditInboxMessage
@ -3664,9 +3664,9 @@ class MyForm(settingsmixin.SMainWindow):
def on_action_YourIdentitiesDelete(self): def on_action_YourIdentitiesDelete(self):
account = self.getCurrentItem() account = self.getCurrentItem()
if account.type == "normal": if account.type == AccountMixin.NORMAL:
return # maybe in the future return # maybe in the future
elif account.type == "chan": elif account.type == AccountMixin.CHAN:
if QtGui.QMessageBox.question(self, "Delete channel?", _translate("MainWindow", "If you delete the channel, messages that you already received will become inaccessible. Maybe you can consider disabling the channel instead. Disabled channels will not receive new messages, but you can still view messages you already received.\n\nAre you sure you want to delete the channel?"), QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes: if QtGui.QMessageBox.question(self, "Delete channel?", _translate("MainWindow", "If you delete the channel, messages that you already received will become inaccessible. Maybe you can consider disabling the channel instead. Disabled channels will not receive new messages, but you can still view messages you already received.\n\nAre you sure you want to delete the channel?"), QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes:
shared.config.remove_section(str(account.address)) shared.config.remove_section(str(account.address))
else: else:
@ -3676,9 +3676,9 @@ class MyForm(settingsmixin.SMainWindow):
shared.writeKeysFile() shared.writeKeysFile()
shared.reloadMyAddressHashes() shared.reloadMyAddressHashes()
self.rerenderAddressBook() self.rerenderAddressBook()
if account.type == "normal": if account.type == AccountMixin.NORMAL:
self.rerenderTabTreeMessages() self.rerenderTabTreeMessages()
elif account.type == "chan": elif account.type == AccountMixin.CHAN:
self.rerenderTabTreeChans() self.rerenderTabTreeChans()
def on_action_Enable(self): def on_action_Enable(self):
@ -3912,7 +3912,7 @@ class MyForm(settingsmixin.SMainWindow):
return return
newLabel = str(item.text(0)) newLabel = str(item.text(0))
if item.type == "subscription": if item.type == AccountMixin.SUBSCRIPTION:
oldLabel = item.label oldLabel = item.label
else: else:
oldLabel = shared.config.get(str(item.address), 'label') oldLabel = shared.config.get(str(item.address), 'label')
@ -3927,9 +3927,9 @@ class MyForm(settingsmixin.SMainWindow):
self.recurDepth += 1 self.recurDepth += 1
item.setData(0, QtCore.Qt.EditRole, newLabel) item.setData(0, QtCore.Qt.EditRole, newLabel)
item.updateText() item.updateText()
if item.type == 'mailinglist': if item.type == AccountMixin.MAILINGLIST:
self.rerenderComboBoxSendFromBroadcast() self.rerenderComboBoxSendFromBroadcast()
elif item.type != "subscription": elif item.type != AccountMixin.SUBSCRIPTION:
self.rerenderComboBoxSendFrom() self.rerenderComboBoxSendFrom()
self.recurDepth -= 1 self.recurDepth -= 1

View File

@ -6,12 +6,18 @@ import shared
from settingsmixin import SettingsMixin from settingsmixin import SettingsMixin
class AccountMixin (object): class AccountMixin (object):
ALL = 0
NORMAL = 1
CHAN = 2
MAILINGLIST = 3
SUBSCRIPTION = 4
def accountColor (self): def accountColor (self):
if not self.isEnabled: if not self.isEnabled:
return QtGui.QColor(128, 128, 128) return QtGui.QColor(128, 128, 128)
elif self.type == "chan": elif self.type == self.CHAN:
return QtGui.QColor(216, 119, 0) return QtGui.QColor(216, 119, 0)
elif self.type == "mailinglist" or self.type == "subscription": elif self.type in [self.MAILINGLIST, self.SUBSCRIPTION]:
return QtGui.QColor(137, 04, 177) return QtGui.QColor(137, 04, 177)
else: else:
return QtGui.QApplication.palette().text().color() return QtGui.QApplication.palette().text().color()
@ -51,12 +57,14 @@ class AccountMixin (object):
self.updateText() self.updateText()
def setType(self): def setType(self):
if shared.safeConfigGetBoolean(self.address, 'chan'): if self.address is None:
self.type = "chan" self.type = self.ALL
elif shared.safeConfigGetBoolean(self.address, 'chan'):
self.type = self.CHAN
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'): elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
self.type = "mailinglist" self.type = self.MAILINGLIST
else: else:
self.type = "normal" self.type = self.NORMAL
def updateText(self): def updateText(self):
pass pass
@ -119,7 +127,7 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin): class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
def __init__(self, parent, pos = 0, address = "", unreadCount = 0, enabled = True): def __init__(self, parent, pos = 0, address = None, unreadCount = 0, enabled = True):
super(QtGui.QTreeWidgetItem, self).__init__() super(QtGui.QTreeWidgetItem, self).__init__()
parent.insertTopLevelItem(pos, self) parent.insertTopLevelItem(pos, self)
# only set default when creating # only set default when creating
@ -131,19 +139,39 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
self.initialised = True self.initialised = True
self.setType() # does updateText self.setType() # does updateText
def _getLabel(self):
if self.address is None:
label = QtGui.QApplication.translate("MainWindow", "All accounts")
else:
try:
return unicode(shared.config.get(self.address, 'label'), 'utf-8)')
except:
return self.address
def _getAddressBracket(self, unreadCount = False):
ret = ""
if unreadCount:
ret += " (" + str(self.unreadCount) + ")"
if self.address is not None:
ret += " (" + self.address + ")"
return ret
def data(self, column, role): def data(self, column, role):
if column == 0: if column == 0:
if role == QtCore.Qt.DisplayRole: if role == QtCore.Qt.DisplayRole:
if self.unreadCount > 0 and not self.isExpanded(): if self.unreadCount > 0 and not self.isExpanded():
return unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + str(self.unreadCount) + ') (' + self.address + ')' return self._getLabel() + self._getAddressBracket(True)
else: else:
return unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + self.address + ')' return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.EditRole: elif role == QtCore.Qt.EditRole:
return unicode(shared.config.get(self.address, 'label'), 'utf-8') return self._getLabel()
elif role == QtCore.Qt.ToolTipRole: elif role == QtCore.Qt.ToolTipRole:
return unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + self.address + ')' return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.DecorationRole: elif role == QtCore.Qt.DecorationRole:
return avatarize(self.address) if self.address is None:
return avatarize(self._getLabel())
else:
return avatarize(self.address)
elif role == QtCore.Qt.FontRole: elif role == QtCore.Qt.FontRole:
font = QtGui.QFont() font = QtGui.QFont()
font.setBold(self.unreadCount > 0) font.setBold(self.unreadCount > 0)
@ -171,6 +199,12 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
def setExpanded(self, expand): def setExpanded(self, expand):
super(Ui_AddressWidget, self).setExpanded(expand) super(Ui_AddressWidget, self).setExpanded(expand)
self.updateText() self.updateText()
def _getSortRank(self):
ret = self.type
if not self.isEnabled:
ret += 5
return ret
# label (or address) alphabetically, disabled at the end # label (or address) alphabetically, disabled at the end
def __lt__(self, other): def __lt__(self, other):
@ -178,21 +212,11 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
reverse = False reverse = False
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if self.isEnabled == other.isEnabled: if self._getSortRank() == other._getSortRank():
if self.type == other.type: x = self._getLabel().decode('utf-8').lower()
if shared.config.get(self.address, 'label'): y = other._getLabel().decode('utf-8').lower()
x = shared.config.get(self.address, 'label').decode('utf-8').lower() return x < y
else: return (not reverse if self._getSortRank() < other._getSortRank() else reverse)
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
else:
return (reverse if self.type == "mailinglist" else not reverse)
# else:
return (not reverse if self.isEnabled else reverse)
return super(QtGui.QTreeWidgetItem, self).__lt__(other) return super(QtGui.QTreeWidgetItem, self).__lt__(other)
@ -213,30 +237,12 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
def setLabel(self, label): def setLabel(self, label):
self.label = label self.label = label
def _getLabel(self):
return unicode(self.label, 'utf-8)')
def setType(self): def setType(self):
self.type = "subscription" self.type = self.SUBSCRIPTION
def data(self, column, role):
if column == 0:
if role == QtCore.Qt.DisplayRole:
if self.unreadCount > 0 and not self.isExpanded():
return unicode(self.label, 'utf-8)') + ' (' + str(self.unreadCount) + ') (' + self.address + ')'
else:
return unicode(self.label, 'utf-8)') + ' (' + self.address + ')'
elif role == QtCore.Qt.EditRole:
return unicode(self.label, 'utf-8')
elif role == QtCore.Qt.ToolTipRole:
return unicode(self.label, 'utf-8)') + ' (' + self.address + ')'
elif role == QtCore.Qt.DecorationRole:
return avatarize(self.address)
elif role == QtCore.Qt.FontRole:
font = QtGui.QFont()
font.setBold(self.unreadCount > 0)
return font
elif role == QtCore.Qt.ForegroundRole:
return self.accountBrush()
return super(Ui_SubscriptionWidget, self).data(column, role)
def setData(self, column, role, value): def setData(self, column, role, value):
if role == QtCore.Qt.EditRole: if role == QtCore.Qt.EditRole:
@ -251,39 +257,13 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
if not self.initialised: if not self.initialised:
return return
self.emitDataChanged() self.emitDataChanged()
# 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)
return super(QtGui.QTreeWidgetItem, self).__lt__(other)
class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin): class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
_types = {'normal': 0, 'chan': 1, 'subscription': 2} def __init__ (self, text, type = AccountMixin.NORMAL):
def __init__ (self, text, type = 'normal'):
super(QtGui.QTableWidgetItem, self).__init__(text) super(QtGui.QTableWidgetItem, self).__init__(text)
self.label = text self.label = text
self.type = type self.type = type
try:
self.typeNum = self._types[self.type]
except:
self.type = 0
self.setEnabled(True) self.setEnabled(True)
self.setForeground(self.accountBrush()) self.setForeground(self.accountBrush())
@ -292,10 +272,10 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
reverse = False reverse = False
if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder: if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True reverse = True
if self.typeNum == other.typeNum: if self.type == other.type:
return self.label.decode('utf-8').lower() < other.label.decode('utf-8').lower() return self.label.decode('utf-8').lower() < other.label.decode('utf-8').lower()
else: else:
return (not reverse if self.typeNum < other.typeNum else reverse) return (not reverse if self.type < other.type else reverse)
return super(QtGui.QTableWidgetItem, self).__lt__(other) return super(QtGui.QTableWidgetItem, self).__lt__(other)