diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 1b1a78858..a5776efd0 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -56,6 +56,9 @@ import bitmessage_icons_rc # noqa:F401 pylint: disable=unused-import import helper_sent +from six.moves import iteritems, itervalues, range as xrange +from six import text_type + try: from plugins.plugin import get_plugin, get_plugins except ImportError: @@ -64,6 +67,11 @@ is_windows = sys.platform.startswith('win') +HOUR = 60 * 60 +SLIDER_SCALING_EXPONENT = 3.199 +DAY = 24 * HOUR +MONTH = 28 * DAY + # TODO: rewrite def powQueueSize(): @@ -471,7 +479,7 @@ def rerenderTabTreeSubscriptions(self): # add missing folders if len(db[toAddress]) > 0: j = 0 - for f, c in db[toAddress].iteritems(): + for f, c in iteritems(db[toAddress]): try: subwidget = Ui_FolderWidget(widget, j, toAddress, f, c['count']) except KeyError: @@ -601,7 +609,7 @@ def rerenderTabTree(self, tab): # add missing folders if len(db[toAddress]) > 0: j = 0 - for f, c in db[toAddress].iteritems(): + for f, c in iteritems(db[toAddress]): if toAddress is not None and tab == 'messages' and folder == "new": continue subwidget = Ui_FolderWidget(widget, j, toAddress, f, c) @@ -831,11 +839,11 @@ def __init__(self, parent=None): # Put the TTL slider in the correct spot TTL = config.getint('bitmessagesettings', 'ttl') - if TTL < 3600: # an hour - TTL = 3600 - elif TTL > 28*24*60*60: # 28 days - TTL = 28*24*60*60 - self.ui.horizontalSliderTTL.setSliderPosition((TTL - 3600) ** (1/3.199)) + if TTL < HOUR: + TTL = HOUR + elif TTL > MONTH: + TTL = MONTH + self.ui.horizontalSliderTTL.setSliderPosition((TTL - HOUR) ** (1 / SLIDER_SCALING_EXPONENT)) self.updateHumanFriendlyTTLDescription(TTL) QtCore.QObject.connect(self.ui.horizontalSliderTTL, QtCore.SIGNAL( @@ -888,34 +896,41 @@ def updateStartOnLogon(self): except (NameError, TypeError): self.desktop = False - def updateTTL(self, sliderPosition): - TTL = int(sliderPosition ** 3.199 + 3600) - self.updateHumanFriendlyTTLDescription(TTL) - config.set('bitmessagesettings', 'ttl', str(TTL)) + def updateTTL(self, slider_position): + ttl = int(slider_position ** SLIDER_SCALING_EXPONENT + HOUR) + self.updateHumanFriendlyTTLDescription(ttl) + config.set('bitmessagesettings', 'ttl', str(ttl)) config.save() - def updateHumanFriendlyTTLDescription(self, TTL): - numberOfHours = int(round(TTL / (60*60))) + def updateHumanFriendlyTTLDescription(self, ttl): font = QtGui.QFont() stylesheet = "" - if numberOfHours < 48: + if ttl < (2 * DAY): self.ui.labelHumanFriendlyTTLDescription.setText( - _translate("MainWindow", "%n hour(s)", None, QtCore.QCoreApplication.CodecForTr, numberOfHours) + - ", " + - _translate("MainWindow", "not recommended for chans", None, QtCore.QCoreApplication.CodecForTr) + ( + _translate( + "MainWindow", "%n hour(s)", None, + QtCore.QCoreApplication.CodecForTr, + int(round(ttl / HOUR)) + ) + + ", " + + _translate( + "MainWindow", "not recommended for chans", + None, QtCore.QCoreApplication.CodecForTr + ) ) + ) stylesheet = "QLabel { color : red; }" font.setBold(True) else: - numberOfDays = int(round(TTL / (24*60*60))) self.ui.labelHumanFriendlyTTLDescription.setText( _translate( "MainWindow", "%n day(s)", None, QtCore.QCoreApplication.CodecForTr, - numberOfDays)) + int(round(ttl / DAY)))) font.setBold(False) self.ui.labelHumanFriendlyTTLDescription.setStyleSheet(stylesheet) self.ui.labelHumanFriendlyTTLDescription.setFont(font) @@ -1078,15 +1093,15 @@ def propagateUnreadCount(self, folder=None, widget=None): for i in range(root.childCount()): addressItem = root.child(i) if addressItem.type == AccountMixin.ALL: - newCount = sum(totalUnread.itervalues()) + newCount = sum(itervalues(totalUnread)) self.drawTrayIcon(self.currentTrayIconFileName, newCount) else: try: - newCount = sum(( + newCount = sum(itervalues(( broadcastsUnread if addressItem.type == AccountMixin.SUBSCRIPTION else normalUnread - )[addressItem.address].itervalues()) + )[addressItem.address])) except KeyError: newCount = 0 if newCount != addressItem.unreadCount: @@ -1189,11 +1204,11 @@ def addMessageListItemSent( items = [ MessageList_AddressWidget( - toAddress, unicode(acct.toLabel, 'utf-8')), + toAddress, text_type(acct.toLabel, 'utf-8')), MessageList_AddressWidget( - fromAddress, unicode(acct.fromLabel, 'utf-8')), + fromAddress, text_type(acct.fromLabel, 'utf-8')), MessageList_SubjectWidget( - str(subject), unicode(acct.subject, 'utf-8', 'replace')), + str(subject), text_type(acct.subject, 'utf-8', 'replace')), MessageList_TimeWidget( statusText, False, lastactiontime, ackdata)] self.addMessageListItem(tableWidget, items) @@ -1214,11 +1229,11 @@ def addMessageListItemInbox( items = [ MessageList_AddressWidget( - toAddress, unicode(acct.toLabel, 'utf-8'), not read), + toAddress, text_type(acct.toLabel, 'utf-8'), not read), MessageList_AddressWidget( - fromAddress, unicode(acct.fromLabel, 'utf-8'), not read), + fromAddress, text_type(acct.fromLabel, 'utf-8'), not read), MessageList_SubjectWidget( - str(subject), unicode(acct.subject, 'utf-8', 'replace'), + str(subject), text_type(acct.subject, 'utf-8', 'replace'), not read), MessageList_TimeWidget( l10n.formatTimestamp(received), not read, received, msgid) @@ -1499,7 +1514,7 @@ def notifierShow( self, title, subtitle, category, label=None, icon=None): self.playSound(category, label) self._notifier( - unicode(title), unicode(subtitle), category, label, icon) + text_type(title), text_type(subtitle), category, label, icon) # tree def treeWidgetKeyPressEvent(self, event): @@ -1998,9 +2013,9 @@ def rerenderMessagelistToLabels(self): def rerenderAddressBook(self): def addRow (address, label, type): self.ui.tableWidgetAddressBook.insertRow(0) - newItem = Ui_AddressBookWidgetItemLabel(address, unicode(label, 'utf-8'), type) + newItem = Ui_AddressBookWidgetItemLabel(address, text_type(label, 'utf-8'), type) self.ui.tableWidgetAddressBook.setItem(0, 0, newItem) - newItem = Ui_AddressBookWidgetItemAddress(address, unicode(label, 'utf-8'), type) + newItem = Ui_AddressBookWidgetItemAddress(address, text_type(label, 'utf-8'), type) self.ui.tableWidgetAddressBook.setItem(0, 1, newItem) oldRows = {} @@ -2041,7 +2056,7 @@ def addRow (address, label, type): self.ui.tableWidgetAddressBook.removeRow(oldRows[address][2]) for address in newRows: addRow(address, newRows[address][0], newRows[address][1]) - completerList.append(unicode(newRows[address][0], encoding="UTF-8") + " <" + address + ">") + completerList.append(text_type(newRows[address][0], encoding="UTF-8") + " <" + address + ">") # sort self.ui.tableWidgetAddressBook.sortByColumn( @@ -2167,7 +2182,7 @@ def click_pushButtonSend(self): status, addressVersionNumber, streamNumber = decodeAddress(toAddress)[:3] if status != 'success': try: - toAddress = unicode(toAddress, 'utf-8', 'ignore') + toAddress = text_type(toAddress, 'utf-8', 'ignore') except: pass logger.error('Error: Could not decode recipient address ' + toAddress + ':' + status) @@ -2371,7 +2386,7 @@ def rerenderComboBoxSendFrom(self): addressInKeysFile, 'enabled') isMaillinglist = config.safeGetBoolean(addressInKeysFile, 'mailinglist') if isEnabled and not isMaillinglist: - label = unicode(config.get(addressInKeysFile, 'label'), 'utf-8', 'ignore').strip() + label = text_type(config.get(addressInKeysFile, 'label'), 'utf-8', 'ignore').strip() if label == "": label = addressInKeysFile self.ui.comboBoxSendFrom.addItem(avatarize(addressInKeysFile), label, addressInKeysFile) @@ -2395,7 +2410,7 @@ def rerenderComboBoxSendFromBroadcast(self): addressInKeysFile, 'enabled') isChan = config.safeGetBoolean(addressInKeysFile, 'chan') if isEnabled and not isChan: - label = unicode(config.get(addressInKeysFile, 'label'), 'utf-8', 'ignore').strip() + label = text_type(config.get(addressInKeysFile, 'label'), 'utf-8', 'ignore').strip() if label == "": label = addressInKeysFile self.ui.comboBoxSendFromBroadcast.addItem(avatarize(addressInKeysFile), label, addressInKeysFile) @@ -2501,7 +2516,7 @@ def displayNewInboxMessage( self.notifierShow( _translate("MainWindow", "New Message"), _translate("MainWindow", "From %1").arg( - unicode(acct.fromLabel, 'utf-8')), + text_type(acct.fromLabel, 'utf-8')), sound.SOUND_UNKNOWN ) if self.getCurrentAccount() is not None and ( @@ -2874,7 +2889,7 @@ def quit(self): QtCore.QEventLoop.AllEvents, 1000 ) self.saveSettings() - for attr, obj in self.ui.__dict__.iteritems(): + for attr, obj in iteritems(self.ui.__dict__): if hasattr(obj, "__class__") \ and isinstance(obj, settingsmixin.SettingsMixin): saveMethod = getattr(obj, "saveSettings", None) @@ -2937,7 +2952,7 @@ def on_action_InboxMessageForceHtml(self): lines[i] = '

' content = ' '.join(lines) # To keep the whitespace between lines content = shared.fixPotentiallyInvalidUTF8Data(content) - content = unicode(content, 'utf-8)') + content = text_type(content, 'utf-8)') textEdit.setHtml(QtCore.QString(content)) def on_action_InboxMarkUnread(self): @@ -3120,7 +3135,7 @@ def on_action_InboxReply(self, reply_type=None): self.setSendFromComboBox(toAddressAtCurrentInboxRow) quotedText = self.quoted_text( - unicode(messageAtCurrentInboxRow, 'utf-8', 'replace')) + text_type(messageAtCurrentInboxRow, 'utf-8', 'replace')) widget['message'].setPlainText(quotedText) if acct.subject[0:3] in ('Re:', 'RE:'): widget['subject'].setText( @@ -3371,7 +3386,7 @@ def on_action_AddressBookSend(self): return self.updateStatusBar(_translate( "MainWindow", "No addresses selected.")) - addresses_string = unicode( + addresses_string = text_type( self.ui.lineEditTo.text().toUtf8(), 'utf-8') for item in selected_items: address_string = item.accountString() @@ -3864,11 +3879,11 @@ def on_action_AddressBookSetSound(self): self.setAddressSound(widget.item(widget.currentRow(), 0).text()) def setAddressSound(self, addr): - filters = [unicode(_translate( + filters = [text_type(_translate( "MainWindow", "Sound files (%s)" % ' '.join(['*%s%s' % (os.extsep, ext) for ext in sound.extensions]) ))] - sourcefile = unicode(QtGui.QFileDialog.getOpenFileName( + sourcefile = text_type(QtGui.QFileDialog.getOpenFileName( self, _translate("MainWindow", "Set notification sound..."), filter=';;'.join(filters) )) @@ -3877,7 +3892,7 @@ def setAddressSound(self, addr): return destdir = os.path.join(state.appdata, 'sounds') - destfile = unicode(addr) + os.path.splitext(sourcefile)[-1] + destfile = text_type(addr) + os.path.splitext(sourcefile)[-1] destination = os.path.join(destdir, destfile) if sourcefile == destination: @@ -4087,7 +4102,7 @@ def treeWidgetItemChanged(self, item, column): if item.type == AccountMixin.ALL: return - newLabel = unicode(item.text(0), 'utf-8', 'ignore') + newLabel = text_type(item.text(0), 'utf-8', 'ignore') oldLabel = item.defaultLabel() # unchanged, do not do anything either @@ -4158,7 +4173,7 @@ def tableWidgetAddressBookItemChanged(self, item): self.rerenderMessagelistToLabels() completerList = self.ui.lineEditTo.completer().model().stringList() for i in range(len(completerList)): - if unicode(completerList[i]).endswith(" <" + item.address + ">"): + if text_type(completerList[i]).endswith(" <" + item.address + ">"): completerList[i] = item.label + " <" + item.address + ">" self.ui.lineEditTo.completer().model().setStringList(completerList) @@ -4212,7 +4227,7 @@ def resetNamecoinConnection(self): def initSettings(self): self.loadSettings() - for attr, obj in self.ui.__dict__.iteritems(): + for attr, obj in iteritems(self.ui.__dict__): if hasattr(obj, "__class__") and \ isinstance(obj, settingsmixin.SettingsMixin): loadMethod = getattr(obj, "loadSettings", None)