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):
# if refreshing the account root, we need to rescan folders
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"
else:
xAddress = "toaddress"
@ -2058,20 +2058,20 @@ class MyForm(settingsmixin.SMainWindow):
queryreturn = sqlQuery('SELECT label, address FROM subscriptions WHERE enabled = 1')
for row in queryreturn:
label, address = row
addRow(address, label, 'subscription')
addRow(address, label, AccountMixin.SUBSCRIPTION)
# chans
addresses = getSortedAccounts()
for address in addresses:
account = accountClass(address)
if (account.type == 'chan' and shared.safeConfigGetBoolean(address, 'enabled')):
addRow(address, account.getLabel(), 'chan')
if (account.type == AccountMixin.CHAN and shared.safeConfigGetBoolean(address, 'enabled')):
addRow(address, account.getLabel(), AccountMixin.CHAN)
# normal accounts
queryreturn = sqlQuery('SELECT * FROM addressbook')
for row in queryreturn:
label, address = row
addRow(address, label, 'normal')
addRow(address, label, AccountMixin.NORMAL)
# sort
self.ui.tableWidgetAddressBook.sortItems(0, QtCore.Qt.AscendingOrder)
@ -2846,7 +2846,7 @@ class MyForm(settingsmixin.SMainWindow):
addressAtCurrentRow = self.getCurrentAccount()
acct = accountClass(addressAtCurrentRow)
# no chans / mailinglists
if acct.type != 'normal':
if acct.type != AccountMixin.NORMAL:
return
if self.dialog.ui.radioButtonUnregister.isChecked() and isinstance(acct, GatewayAccount):
acct.unregister()
@ -3370,7 +3370,7 @@ class MyForm(settingsmixin.SMainWindow):
currentRow = row.row()
type = str(self.ui.tableWidgetAddressBook.item(
currentRow, 0).data(Qt.UserRole).toPyObject())
if type != "normal":
if type != AccountMixin.NORMAL:
normal = False
if normal:
# only if all selected addressbook items are normal, allow delete
@ -3527,9 +3527,9 @@ class MyForm(settingsmixin.SMainWindow):
def getAccountTreeWidget(self, account):
try:
if account.type == 'chan':
if account.type == AccountMixin.CHAN:
return self.ui.treeWidgetChans
elif account.type == 'subscription':
elif account.type == AccountMixin.SUBSCRIPTION:
return self.ui.treeWidgetSubscriptions
else:
return self.ui.treeWidgetYourIdentities
@ -3551,9 +3551,9 @@ class MyForm(settingsmixin.SMainWindow):
def getAccountMessagelist(self, account):
try:
if account.type == 'chan':
if account.type == AccountMixin.CHAN:
return self.ui.tableWidgetInboxChans
elif account.type == 'subscription':
elif account.type == AccountMixin.SUBSCRIPTION:
return self.ui.tableWidgetInboxSubscriptions
else:
return self.ui.tableWidgetInbox
@ -3585,9 +3585,9 @@ class MyForm(settingsmixin.SMainWindow):
def getAccountTextedit(self, account):
try:
if account.type == 'chan':
if account.type == AccountMixin.CHAN:
return self.ui.textEditInboxMessageChans
elif account.type == 'subscription':
elif account.type == AccountMixin.SUBSCRIPTION:
return self.ui.textEditInboxSubscriptions
else:
return self.ui.textEditInboxMessage
@ -3664,9 +3664,9 @@ class MyForm(settingsmixin.SMainWindow):
def on_action_YourIdentitiesDelete(self):
account = self.getCurrentItem()
if account.type == "normal":
if account.type == AccountMixin.NORMAL:
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:
shared.config.remove_section(str(account.address))
else:
@ -3676,9 +3676,9 @@ class MyForm(settingsmixin.SMainWindow):
shared.writeKeysFile()
shared.reloadMyAddressHashes()
self.rerenderAddressBook()
if account.type == "normal":
if account.type == AccountMixin.NORMAL:
self.rerenderTabTreeMessages()
elif account.type == "chan":
elif account.type == AccountMixin.CHAN:
self.rerenderTabTreeChans()
def on_action_Enable(self):
@ -3912,7 +3912,7 @@ class MyForm(settingsmixin.SMainWindow):
return
newLabel = str(item.text(0))
if item.type == "subscription":
if item.type == AccountMixin.SUBSCRIPTION:
oldLabel = item.label
else:
oldLabel = shared.config.get(str(item.address), 'label')
@ -3927,9 +3927,9 @@ class MyForm(settingsmixin.SMainWindow):
self.recurDepth += 1
item.setData(0, QtCore.Qt.EditRole, newLabel)
item.updateText()
if item.type == 'mailinglist':
if item.type == AccountMixin.MAILINGLIST:
self.rerenderComboBoxSendFromBroadcast()
elif item.type != "subscription":
elif item.type != AccountMixin.SUBSCRIPTION:
self.rerenderComboBoxSendFrom()
self.recurDepth -= 1

View File

@ -6,12 +6,18 @@ import shared
from settingsmixin import SettingsMixin
class AccountMixin (object):
ALL = 0
NORMAL = 1
CHAN = 2
MAILINGLIST = 3
SUBSCRIPTION = 4
def accountColor (self):
if not self.isEnabled:
return QtGui.QColor(128, 128, 128)
elif self.type == "chan":
elif self.type == self.CHAN:
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)
else:
return QtGui.QApplication.palette().text().color()
@ -51,12 +57,14 @@ class AccountMixin (object):
self.updateText()
def setType(self):
if shared.safeConfigGetBoolean(self.address, 'chan'):
self.type = "chan"
if self.address is None:
self.type = self.ALL
elif shared.safeConfigGetBoolean(self.address, 'chan'):
self.type = self.CHAN
elif shared.safeConfigGetBoolean(self.address, 'mailinglist'):
self.type = "mailinglist"
self.type = self.MAILINGLIST
else:
self.type = "normal"
self.type = self.NORMAL
def updateText(self):
pass
@ -119,7 +127,7 @@ class Ui_FolderWidget(QtGui.QTreeWidgetItem, AccountMixin):
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__()
parent.insertTopLevelItem(pos, self)
# only set default when creating
@ -131,18 +139,38 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
self.initialised = True
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):
if column == 0:
if role == QtCore.Qt.DisplayRole:
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:
return unicode(shared.config.get(self.address, 'label'), 'utf-8)') + ' (' + self.address + ')'
return self._getLabel() + self._getAddressBracket(False)
elif role == QtCore.Qt.EditRole:
return unicode(shared.config.get(self.address, 'label'), 'utf-8')
return self._getLabel()
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:
if self.address is None:
return avatarize(self._getLabel())
else:
return avatarize(self.address)
elif role == QtCore.Qt.FontRole:
font = QtGui.QFont()
@ -172,27 +200,23 @@ class Ui_AddressWidget(QtGui.QTreeWidgetItem, AccountMixin, SettingsMixin):
super(Ui_AddressWidget, self).setExpanded(expand)
self.updateText()
def _getSortRank(self):
ret = self.type
if not self.isEnabled:
ret += 5
return ret
# label (or address) alphabetically, disabled at the end
def __lt__(self, other):
if (isinstance(other, Ui_AddressWidget)):
reverse = False
if self.treeWidget().header().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
reverse = True
if self.isEnabled == other.isEnabled:
if self.type == other.type:
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()
if self._getSortRank() == other._getSortRank():
x = self._getLabel().decode('utf-8').lower()
y = other._getLabel().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 (not reverse if self._getSortRank() < other._getSortRank() else reverse)
return super(QtGui.QTreeWidgetItem, self).__lt__(other)
@ -214,29 +238,11 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
def setLabel(self, label):
self.label = label
def setType(self):
self.type = "subscription"
def _getLabel(self):
return unicode(self.label, 'utf-8)')
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 setType(self):
self.type = self.SUBSCRIPTION
def setData(self, column, role, value):
if role == QtCore.Qt.EditRole:
@ -252,38 +258,12 @@ class Ui_SubscriptionWidget(Ui_AddressWidget, AccountMixin):
return
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):
_types = {'normal': 0, 'chan': 1, 'subscription': 2}
def __init__ (self, text, type = 'normal'):
def __init__ (self, text, type = AccountMixin.NORMAL):
super(QtGui.QTableWidgetItem, self).__init__(text)
self.label = text
self.type = type
try:
self.typeNum = self._types[self.type]
except:
self.type = 0
self.setEnabled(True)
self.setForeground(self.accountBrush())
@ -292,10 +272,10 @@ class Ui_AddressBookWidgetItem(QtGui.QTableWidgetItem, AccountMixin):
reverse = False
if self.tableWidget().horizontalHeader().sortIndicatorOrder() == QtCore.Qt.DescendingOrder:
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()
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)