diff --git a/buildscripts/winbuild.sh b/buildscripts/winbuild.sh index 415d91f2..da5997bd 100755 --- a/buildscripts/winbuild.sh +++ b/buildscripts/winbuild.sh @@ -124,9 +124,9 @@ function install_pyopencl() else wine python -m pip install pyopencl-2015.1-cp27-none-win32.whl fi + sed -Ei 's/_DEFAULT_INCLUDE_OPTIONS = .*/_DEFAULT_INCLUDE_OPTIONS = [] /' $WINEPREFIX/drive_c/Python27/Lib/site-packages/pyopencl/__init__.py } - function build_dll(){ cd ${BASE_DIR} cd src/bitmsghash diff --git a/src/addresses.py b/src/addresses.py index 0072a2f1..c3c61466 100644 --- a/src/addresses.py +++ b/src/addresses.py @@ -276,69 +276,3 @@ def addBMIfNotPresent(address): """Prepend BM- to an address if it doesn't already have it""" address = str(address).strip() return address if address[:3] == 'BM-' else 'BM-' + address - - -# TODO: make test case -if __name__ == "__main__": - from pyelliptic import arithmetic - - print( - '\nLet us make an address from scratch. Suppose we generate two' - ' random 32 byte values and call the first one the signing key' - ' and the second one the encryption key:' - ) - privateSigningKey = \ - '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665' - privateEncryptionKey = \ - '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a' - print( - '\nprivateSigningKey = %s\nprivateEncryptionKey = %s' % - (privateSigningKey, privateEncryptionKey) - ) - print( - '\nNow let us convert them to public keys by doing' - ' an elliptic curve point multiplication.' - ) - publicSigningKey = arithmetic.privtopub(privateSigningKey) - publicEncryptionKey = arithmetic.privtopub(privateEncryptionKey) - print( - '\npublicSigningKey = %s\npublicEncryptionKey = %s' % - (publicSigningKey, publicEncryptionKey) - ) - - print( - '\nNotice that they both begin with the \\x04 which specifies' - ' the encoding type. This prefix is not send over the wire.' - ' You must strip if off before you send your public key across' - ' the wire, and you must add it back when you receive a public key.' - ) - - publicSigningKeyBinary = \ - arithmetic.changebase(publicSigningKey, 16, 256, minlen=64) - publicEncryptionKeyBinary = \ - arithmetic.changebase(publicEncryptionKey, 16, 256, minlen=64) - - ripe = hashlib.new('ripemd160') - sha = hashlib.new('sha512') - sha.update(publicSigningKeyBinary + publicEncryptionKeyBinary) - - ripe.update(sha.digest()) - addressVersionNumber = 2 - streamNumber = 1 - print( - '\nRipe digest that we will encode in the address: %s' % - hexlify(ripe.digest()) - ) - returnedAddress = \ - encodeAddress(addressVersionNumber, streamNumber, ripe.digest()) - print('Encoded address: %s' % returnedAddress) - status, addressVersionNumber, streamNumber, data = \ - decodeAddress(returnedAddress) - print( - '\nAfter decoding address:\n\tStatus: %s' - '\n\taddressVersionNumber %s' - '\n\tstreamNumber %s' - '\n\tlength of data (the ripe hash): %s' - '\n\tripe data: %s' % - (status, addressVersionNumber, streamNumber, len(data), hexlify(data)) - ) diff --git a/src/api.py b/src/api.py index 6b1192c4..37b94603 100644 --- a/src/api.py +++ b/src/api.py @@ -27,6 +27,7 @@ import queues import shared import shutdown import state +import threads from addresses import ( addBMIfNotPresent, calculateInventoryHash, @@ -1209,7 +1210,7 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): len(encryptedPayload) + requiredPayloadLengthExtraBytes + 8 ) * requiredAverageProofOfWorkNonceTrialsPerByte ) - with shared.printLock: + with threads.printLock: print( '(For msg message via API) Doing proof of work.' 'Total required difficulty:', @@ -1224,8 +1225,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): powStartTime = time.time() initialHash = hashlib.sha512(encryptedPayload).digest() trialValue, nonce = proofofwork.run(target, initialHash) - with shared.printLock: - print('(For msg message via API) Found proof of work', trialValue, 'Nonce:', nonce) + with threads.printLock: + print '(For msg message via API) Found proof of work', trialValue, 'Nonce:', nonce try: print( 'POW took', int(time.time() - powStartTime), @@ -1243,8 +1244,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): objectType, toStreamNumber, encryptedPayload, int(time.time()) + TTL, '' ) - with shared.printLock: - print('Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', hexlify(inventoryHash)) + with threads.printLock: + print 'Broadcasting inv for msg(API disseminatePreEncryptedMsg command):', hexlify(inventoryHash) queues.invQueue.put((toStreamNumber, inventoryHash)) def HandleTrashSentMessageByAckDAta(self, params): @@ -1297,8 +1298,8 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): Inventory()[inventoryHash] = ( objectType, pubkeyStreamNumber, payload, int(time.time()) + TTL, '' ) - with shared.printLock: - print('broadcasting inv within API command disseminatePubkey with hash:', hexlify(inventoryHash)) + with threads.printLock: + print 'broadcasting inv within API command disseminatePubkey with hash:', hexlify(inventoryHash) queues.invQueue.put((pubkeyStreamNumber, inventoryHash)) def HandleGetMessageDataByDestinationHash(self, params): @@ -1350,15 +1351,15 @@ class MySimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): connections_num = len(network.stats.connectedHostsList()) if connections_num == 0: networkStatus = 'notConnected' - elif shared.clientHasReceivedIncomingConnections: + elif state.clientHasReceivedIncomingConnections: networkStatus = 'connectedAndReceivingIncomingConnections' else: networkStatus = 'connectedButHaveNotReceivedIncomingConnections' return json.dumps({ 'networkConnections': connections_num, - 'numberOfMessagesProcessed': shared.numberOfMessagesProcessed, - 'numberOfBroadcastsProcessed': shared.numberOfBroadcastsProcessed, - 'numberOfPubkeysProcessed': shared.numberOfPubkeysProcessed, + 'numberOfMessagesProcessed': state.numberOfMessagesProcessed, + 'numberOfBroadcastsProcessed': state.numberOfBroadcastsProcessed, + 'numberOfPubkeysProcessed': state.numberOfPubkeysProcessed, 'networkStatus': networkStatus, 'softwareName': 'PyBitmessage', 'softwareVersion': softwareVersion diff --git a/src/bitmessagecurses/__init__.py b/src/bitmessagecurses/__init__.py index d4b72f8c..b90a2ec9 100644 --- a/src/bitmessagecurses/__init__.py +++ b/src/bitmessagecurses/__init__.py @@ -24,6 +24,7 @@ import network.stats import queues import shared import shutdown +import state from addresses import addBMIfNotPresent, decodeAddress from bmconfigparser import BMConfigParser @@ -274,11 +275,11 @@ def drawtab(stdscr): # Uptime and processing data stdscr.addstr(6, 35, "Since startup on " + l10n.formatTimestamp(startuptime, False)) stdscr.addstr(7, 40, "Processed " + str( - shared.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.") + state.numberOfMessagesProcessed).ljust(4) + " person-to-person messages.") stdscr.addstr(8, 40, "Processed " + str( - shared.numberOfBroadcastsProcessed).ljust(4) + " broadcast messages.") + state.numberOfBroadcastsProcessed).ljust(4) + " broadcast messages.") stdscr.addstr(9, 40, "Processed " + str( - shared.numberOfPubkeysProcessed).ljust(4) + " public keys.") + state.numberOfPubkeysProcessed).ljust(4) + " public keys.") # Inventory data stdscr.addstr(11, 35, "Inventory lookups per second: " + str(inventorydata).ljust(3)) diff --git a/src/bitmessagekivy/kv/scanner.kv b/src/bitmessagekivy/kv/scanner.kv new file mode 100644 index 00000000..1c56f6c2 --- /dev/null +++ b/src/bitmessagekivy/kv/scanner.kv @@ -0,0 +1,37 @@ +#:import ZBarSymbol pyzbar.pyzbar.ZBarSymbol + +BoxLayout: + orientation: 'vertical' + ZBarCam: + id: zbarcam + # optional, by default checks all types + code_types: ZBarSymbol.QRCODE, ZBarSymbol.EAN13 + scan_callback: app._after_scan + scanner_line_y_initial: self.size[1]/2 +self.qrwidth/2 + scanner_line_y_final: self.size[1]/2-self.qrwidth/2 + + canvas: + Color: + rgba: 0,0,0,.25 + + #left rect + Rectangle: + pos: self.pos[0], self.pos[1] + size: self.size[0]/2-self.qrwidth/2, self.size[1] + + #right rect + Rectangle: + pos: self.size[0]/2+self.qrwidth/2, 0 + size: self.size[0]/2-self.qrwidth/2, self.size[1] + + #top rect + Rectangle: + pos: self.size[0]/2-self.qrwidth/2, self.size[1]/2+self.qrwidth/2 + size: self.qrwidth, self.size[1]/2-self.qrwidth/2 + + #bottom rect + Rectangle: + pos: self.size[0]/2-self.qrwidth/2, 0 + size: self.qrwidth, self.size[1]/2-self.qrwidth/2 + + \ No newline at end of file diff --git a/src/bitmessagekivy/mpybit.py b/src/bitmessagekivy/mpybit.py index f2a77f07..a1e22ae7 100644 --- a/src/bitmessagekivy/mpybit.py +++ b/src/bitmessagekivy/mpybit.py @@ -948,16 +948,15 @@ class NetworkStat(Screen): def init_ui(self, dt=0): """Clock Schdule for method networkstat screen""" import network.stats - import shared from network import objectracker self.text_variable_1 = '{0} :: {1}'.format( 'Total Connections', str(len(network.stats.connectedHostsList()))) self.text_variable_2 = 'Processed {0} per-to-per messages'.format( - str(shared.numberOfMessagesProcessed)) + str(state.numberOfMessagesProcessed)) self.text_variable_3 = 'Processed {0} brodcast messages'.format( - str(shared.numberOfBroadcastsProcessed)) + str(state.numberOfBroadcastsProcessed)) self.text_variable_4 = 'Processed {0} public keys'.format( - str(shared.numberOfPubkeysProcessed)) + str(state.numberOfPubkeysProcessed)) self.text_variable_5 = '{0} object to be synced'.format( len(objectracker.missingObjects)) diff --git a/src/bitmessagemain.py b/src/bitmessagemain.py index 1809ffd8..0145e26c 100755 --- a/src/bitmessagemain.py +++ b/src/bitmessagemain.py @@ -32,9 +32,7 @@ from bmconfigparser import BMConfigParser # this should go before any threads from debug import logger from helper_startup import ( - isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections, - start_proxyconfig -) + adjustHalfOpenConnectionsLimit, start_proxyconfig) from inventory import Inventory from knownnodes import readKnownNodes # Network objects and threads @@ -45,9 +43,8 @@ from network import ( from singleinstance import singleinstance # Synchronous threads from threads import ( - set_thread_name, addressGenerator, objectProcessor, singleCleaner, - singleWorker, sqlThread -) + set_thread_name, printLock, + addressGenerator, objectProcessor, singleCleaner, singleWorker, sqlThread) app_dir = os.path.dirname(os.path.abspath(__file__)) os.chdir(app_dir) @@ -56,26 +53,6 @@ sys.path.insert(0, app_dir) depends.check_dependencies() -def connectToStream(streamNumber): - """Connect to a stream""" - state.streamsInWhichIAmParticipating.append(streamNumber) - - if isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): - # Some XP and Vista systems can only have 10 outgoing connections - # at a time. - state.maximumNumberOfHalfOpenConnections = 9 - else: - state.maximumNumberOfHalfOpenConnections = 64 - try: - # don't overload Tor - if BMConfigParser().get( - 'bitmessagesettings', 'socksproxytype') != 'none': - state.maximumNumberOfHalfOpenConnections = 4 - except: - pass - - BMConnectionPool().connectToStream(streamNumber) - def _fixSocket(): if sys.platform.startswith('linux'): @@ -157,7 +134,7 @@ def signal_handler(signum, frame): logger.error("Got signal %i", signum) # there are possible non-UI variants to run bitmessage # which should shutdown especially test-mode - if shared.thisapp.daemon or not state.enableGUI: + if state.thisapp.daemon or not state.enableGUI: shutdown.doCleanShutdown() else: print('# Thread: {}({})'.format(thread.name, thread.ident)) @@ -175,6 +152,7 @@ class Main(object): """Start main application""" # pylint: disable=too-many-statements,too-many-branches,too-many-locals _fixSocket() + adjustHalfOpenConnectionsLimit() config = BMConfigParser() daemon = config.safeGetBoolean('bitmessagesettings', 'daemon') @@ -236,13 +214,10 @@ class Main(object): ' \'-c\' as a commandline argument.' ) # is the application already running? If yes then exit. - try: - shared.thisapp = singleinstance("", daemon) - except Exception: - pass + state.thisapp = singleinstance("", daemon) if daemon: - with shared.printLock: + with printLock: print('Running as a daemon. Send TERM signal to end.') self.daemonize() @@ -332,7 +307,7 @@ class Main(object): # start network components if networking is enabled if state.enableNetwork: start_proxyconfig() - BMConnectionPool() + BMConnectionPool().connectToStream(1) asyncoreThread = BMNetworkThread() asyncoreThread.daemon = True asyncoreThread.start() @@ -356,7 +331,6 @@ class Main(object): state.uploadThread.daemon = True state.uploadThread.start() - connectToStream(1) if config.safeGetBoolean('bitmessagesettings', 'upnp'): import upnp upnpThread = upnp.uPnPThread() @@ -413,7 +387,7 @@ class Main(object): try: if os.fork(): # unlock - shared.thisapp.cleanup() + state.thisapp.cleanup() # wait until grandchild ready while True: time.sleep(1) @@ -424,8 +398,7 @@ class Main(object): pass else: parentPid = os.getpid() - # relock - shared.thisapp.lock() + state.thisapp.lock() # relock os.umask(0) try: @@ -436,20 +409,17 @@ class Main(object): try: if os.fork(): # unlock - shared.thisapp.cleanup() + state.thisapp.cleanup() # wait until child ready while True: time.sleep(1) - # pylint: disable=protected-access - os._exit(0) + os._exit(0) # pylint: disable=protected-access except AttributeError: # fork not implemented pass else: - # relock - shared.thisapp.lock() - # indicate we're the final child - shared.thisapp.lockPid = None + state.thisapp.lock() # relock + state.thisapp.lockPid = None # indicate we're the final child sys.stdout.flush() sys.stderr.flush() if not sys.platform.startswith('win'): @@ -488,7 +458,7 @@ All parameters are optional. @staticmethod def stop(): """Stop main application""" - with shared.printLock: + with printLock: print('Stopping Bitmessage Deamon.') shutdown.doCleanShutdown() @@ -516,4 +486,4 @@ if __name__ == "__main__": # So far, the creation of and management of the Bitmessage protocol and this # client is a one-man operation. Bitcoin tips are quite appreciated. -# 1H5XaDA6fYENLbknwZyjiYXYPQaFjjLX2u +# 1Hl5XaDA6fYENLbknwZyjiYXYPQaFjjLX2u diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 440d36b2..2f1a6e7f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -7,6 +7,7 @@ import locale import os import random import string +import subprocess import sys import textwrap import threading @@ -17,10 +18,11 @@ from sqlite3 import register_adapter from PyQt4 import QtCore, QtGui from PyQt4.QtNetwork import QLocalSocket, QLocalServer +import shared +import state from debug import logger from tr import _translate from addresses import decodeAddress, addBMIfNotPresent -import shared from bitmessageui import Ui_MainWindow from bmconfigparser import BMConfigParser import namecoin @@ -29,7 +31,8 @@ from migrationwizard import Ui_MigrationWizard from foldertree import ( AccountMixin, Ui_FolderWidget, Ui_AddressWidget, Ui_SubscriptionWidget, MessageList_AddressWidget, MessageList_SubjectWidget, - Ui_AddressBookWidgetItemLabel, Ui_AddressBookWidgetItemAddress) + Ui_AddressBookWidgetItemLabel, Ui_AddressBookWidgetItemAddress, + MessageList_TimeWidget) import settingsmixin import support from helper_ackPayload import genAckPayload @@ -47,7 +50,6 @@ import paths from proofofwork import getPowType import queues import shutdown -import state from statusbar import BMStatusBar import sound # This is needed for tray icon @@ -72,6 +74,15 @@ def powQueueSize(): return queue_len +def openKeysFile(): + """Open keys file with an external editor""" + keysfile = os.path.join(state.appdata, 'keys.dat') + if 'linux' in sys.platform: + subprocess.call(["xdg-open", keysfile]) + elif sys.platform.startswith('win'): + os.startfile(keysfile) # pylint: disable=no-member + + class MyForm(settingsmixin.SMainWindow): # the maximum frequency of message sounds in seconds @@ -214,19 +225,19 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.tableWidgetInbox, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuInbox) + self.on_context_menuInbox) self.ui.tableWidgetInboxSubscriptions.setContextMenuPolicy( QtCore.Qt.CustomContextMenu) if connectSignal: self.connect(self.ui.tableWidgetInboxSubscriptions, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuInbox) + self.on_context_menuInbox) self.ui.tableWidgetInboxChans.setContextMenuPolicy( QtCore.Qt.CustomContextMenu) if connectSignal: self.connect(self.ui.tableWidgetInboxChans, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuInbox) + self.on_context_menuInbox) def init_identities_popup_menu(self, connectSignal=True): # Popup menu for the Your Identities tab @@ -266,7 +277,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.treeWidgetYourIdentities, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuYourIdentities) + self.on_context_menuYourIdentities) # load all gui.menu plugins with prefix 'address' self.menu_plugins = {'address': []} @@ -316,7 +327,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.treeWidgetChans, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuChan) + self.on_context_menuChan) def init_addressbook_popup_menu(self, connectSignal=True): # Popup menu for the Address Book page @@ -353,7 +364,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.tableWidgetAddressBook, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuAddressBook) + self.on_context_menuAddressBook) def init_subscriptions_popup_menu(self, connectSignal=True): # Actions @@ -382,7 +393,7 @@ class MyForm(settingsmixin.SMainWindow): if connectSignal: self.connect(self.ui.treeWidgetSubscriptions, QtCore.SIGNAL( 'customContextMenuRequested(const QPoint&)'), - self.on_context_menuSubscriptions) + self.on_context_menuSubscriptions) def init_sent_popup_menu(self, connectSignal=True): # Actions @@ -413,13 +424,13 @@ class MyForm(settingsmixin.SMainWindow): treeWidget.header().setSortIndicator( 0, QtCore.Qt.AscendingOrder) # init dictionary - + db = getSortedSubscriptions(True) for address in db: for folder in folders: - if not folder in db[address]: + if folder not in db[address]: db[address][folder] = {} - + if treeWidget.isSortingEnabled(): treeWidget.setSortingEnabled(False) @@ -431,8 +442,8 @@ class MyForm(settingsmixin.SMainWindow): toAddress = widget.address else: toAddress = None - - if not toAddress in db: + + if toAddress not in db: treeWidget.takeTopLevelItem(i) # no increment continue @@ -462,7 +473,7 @@ class MyForm(settingsmixin.SMainWindow): widget.setUnreadCount(unread) db.pop(toAddress, None) i += 1 - + i = 0 for toAddress in db: widget = Ui_SubscriptionWidget(treeWidget, i, toAddress, db[toAddress]["inbox"]['count'], db[toAddress]["inbox"]['label'], db[toAddress]["inbox"]['enabled']) @@ -477,23 +488,22 @@ class MyForm(settingsmixin.SMainWindow): j += 1 widget.setUnreadCount(unread) i += 1 - - treeWidget.setSortingEnabled(True) + treeWidget.setSortingEnabled(True) def rerenderTabTreeMessages(self): self.rerenderTabTree('messages') def rerenderTabTreeChans(self): self.rerenderTabTree('chan') - + def rerenderTabTree(self, tab): if tab == 'messages': treeWidget = self.ui.treeWidgetYourIdentities elif tab == 'chan': treeWidget = self.ui.treeWidgetChans folders = Ui_FolderWidget.folderWeight.keys() - + # sort ascending when creating if treeWidget.topLevelItemCount() == 0: treeWidget.header().setSortIndicator( @@ -501,7 +511,7 @@ class MyForm(settingsmixin.SMainWindow): # init dictionary db = {} enabled = {} - + for toAddress in getSortedAccounts(): isEnabled = BMConfigParser().getboolean( toAddress, 'enabled') @@ -520,7 +530,7 @@ class MyForm(settingsmixin.SMainWindow): db[toAddress] = {} for folder in folders: db[toAddress][folder] = 0 - + enabled[toAddress] = isEnabled # get number of (unread) messages @@ -538,10 +548,10 @@ class MyForm(settingsmixin.SMainWindow): db[None]["sent"] = 0 db[None]["trash"] = 0 enabled[None] = True - + if treeWidget.isSortingEnabled(): treeWidget.setSortingEnabled(False) - + widgets = {} i = 0 while i < treeWidget.topLevelItemCount(): @@ -550,8 +560,8 @@ class MyForm(settingsmixin.SMainWindow): toAddress = widget.address else: toAddress = None - - if not toAddress in db: + + if toAddress not in db: treeWidget.takeTopLevelItem(i) # no increment continue @@ -560,8 +570,9 @@ class MyForm(settingsmixin.SMainWindow): while j < widget.childCount(): subwidget = widget.child(j) try: - subwidget.setUnreadCount(db[toAddress][subwidget.folderName]) - if subwidget.folderName not in ["new", "trash", "sent"]: + subwidget.setUnreadCount( + db[toAddress][subwidget.folderName]) + if subwidget.folderName not in ("new", "trash", "sent"): unread += db[toAddress][subwidget.folderName] db[toAddress].pop(subwidget.folderName, None) except: @@ -577,13 +588,13 @@ class MyForm(settingsmixin.SMainWindow): if toAddress is not None and tab == 'messages' and folder == "new": continue subwidget = Ui_FolderWidget(widget, j, toAddress, f, c) - if subwidget.folderName not in ["new", "trash", "sent"]: + if subwidget.folderName not in ("new", "trash", "sent"): unread += c j += 1 widget.setUnreadCount(unread) db.pop(toAddress, None) i += 1 - + i = 0 for toAddress in db: widget = Ui_AddressWidget(treeWidget, i, toAddress, db[toAddress]["inbox"], enabled[toAddress]) @@ -593,12 +604,12 @@ class MyForm(settingsmixin.SMainWindow): if toAddress is not None and tab == 'messages' and folder == "new": continue subwidget = Ui_FolderWidget(widget, j, toAddress, folder, db[toAddress][folder]) - if subwidget.folderName not in ["new", "trash", "sent"]: + if subwidget.folderName not in ("new", "trash", "sent"): unread += db[toAddress][folder] j += 1 widget.setUnreadCount(unread) i += 1 - + treeWidget.setSortingEnabled(True) def __init__(self, parent=None): @@ -730,9 +741,6 @@ class MyForm(settingsmixin.SMainWindow): QtCore.QObject.connect(self.pushButtonStatusIcon, QtCore.SIGNAL( "clicked()"), self.click_pushButtonStatusIcon) - self.numberOfMessagesProcessed = 0 - self.numberOfBroadcastsProcessed = 0 - self.numberOfPubkeysProcessed = 0 self.unreadCount = 0 # Set the icon sizes for the identicons @@ -799,7 +807,7 @@ class MyForm(settingsmixin.SMainWindow): self.rerenderComboBoxSendFrom() self.rerenderComboBoxSendFromBroadcast() - + # Put the TTL slider in the correct spot TTL = BMConfigParser().getint('bitmessagesettings', 'ttl') if TTL < 3600: # an hour @@ -814,6 +822,14 @@ class MyForm(settingsmixin.SMainWindow): self.initSettings() self.resetNamecoinConnection() + self.sqlInit() + self.indicatorInit() + self.notifierInit() + + self.ui.updateNetworkSwitchMenuLabel() + + self._firstrun = BMConfigParser().safeGetBoolean( + 'bitmessagesettings', 'dontconnect') self._contact_selected = None @@ -963,40 +979,30 @@ class MyForm(settingsmixin.SMainWindow): Switch unread for item of msgid and related items in other STableWidgets "All Accounts" and "Chans" """ - related = [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans] + status = widget.item(row, 0).unread + if status != unread: + return + + widgets = [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans] + rrow = None try: - related.remove(widget) - related = related.pop() + widgets.remove(widget) + related = widgets.pop() except ValueError: - rrow = None - related = [] + pass else: # maybe use instead: # rrow = related.row(msgid), msgid should be QTableWidgetItem # related = related.findItems(msgid, QtCore.Qt.MatchExactly), # returns an empty list - for rrow in xrange(related.rowCount()): - if msgid == str(related.item(rrow, 3).data( - QtCore.Qt.UserRole).toPyObject()): + for rrow in range(related.rowCount()): + if related.item(rrow, 3).data() == msgid: break - else: - rrow = None - status = widget.item(row, 0).unread - if status == unread: - font = QtGui.QFont() - font.setBold(not status) - widget.item(row, 3).setFont(font) - for col in (0, 1, 2): - widget.item(row, col).setUnread(not status) - - try: - related.item(rrow, 3).setFont(font) - except (TypeError, AttributeError): - pass - else: - for col in (0, 1, 2): - related.item(rrow, col).setUnread(not status) + for col in range(widget.columnCount()): + widget.item(row, col).setUnread(not status) + if rrow: + related.item(rrow, col).setUnread(not status) # Here we need to update unread count for: # - all widgets if there is no args @@ -1081,43 +1087,46 @@ class MyForm(settingsmixin.SMainWindow): if sortingEnabled: tableWidget.setSortingEnabled(False) tableWidget.insertRow(0) - for i in range(len(items)): - tableWidget.setItem(0, i, items[i]) + for i, item in enumerate(items): + tableWidget.setItem(0, i, item) if sortingEnabled: tableWidget.setSortingEnabled(True) - def addMessageListItemSent(self, tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime): - acct = accountClass(fromAddress) - if acct is None: - acct = BMAccount(fromAddress) + def addMessageListItemSent( + self, tableWidget, toAddress, fromAddress, subject, + status, ackdata, lastactiontime + ): + acct = accountClass(fromAddress) or BMAccount(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, "") - items = [] - MessageList_AddressWidget(items, str(toAddress), unicode(acct.toLabel, 'utf-8')) - MessageList_AddressWidget(items, str(fromAddress), unicode(acct.fromLabel, 'utf-8')) - MessageList_SubjectWidget(items, str(subject), unicode(acct.subject, 'utf-8', 'replace')) - if status == 'awaitingpubkey': statusText = _translate( - "MainWindow", "Waiting for their encryption key. Will request it again soon.") + "MainWindow", + "Waiting for their encryption key. Will request it again soon." + ) elif status == 'doingpowforpubkey': statusText = _translate( - "MainWindow", "Doing work necessary to request encryption key.") + "MainWindow", "Doing work necessary to request encryption key." + ) elif status == 'msgqueued': - statusText = _translate( - "MainWindow", "Queued.") + statusText = _translate("MainWindow", "Queued.") elif status == 'msgsent': - statusText = _translate("MainWindow", "Message sent. Waiting for acknowledgement. Sent at %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Message sent. Waiting for acknowledgement. Sent at %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'msgsentnoackexpected': - statusText = _translate("MainWindow", "Message sent. Sent at %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", "Message sent. Sent at %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'doingmsgpow': statusText = _translate( "MainWindow", "Doing work necessary to send message.") elif status == 'ackreceived': - statusText = _translate("MainWindow", "Acknowledgement of the message received %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Acknowledgement of the message received %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'broadcastqueued': statusText = _translate( "MainWindow", "Broadcast queued.") @@ -1128,58 +1137,64 @@ class MyForm(settingsmixin.SMainWindow): statusText = _translate("MainWindow", "Broadcast on %1").arg( l10n.formatTimestamp(lastactiontime)) elif status == 'toodifficult': - statusText = _translate("MainWindow", "Problem: The work demanded by the recipient is more difficult than you are willing to do. %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Problem: The work demanded by the recipient is more" + " difficult than you are willing to do. %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'badkey': - statusText = _translate("MainWindow", "Problem: The recipient\'s encryption key is no good. Could not encrypt message. %1").arg( - l10n.formatTimestamp(lastactiontime)) + statusText = _translate( + "MainWindow", + "Problem: The recipient\'s encryption key is no good." + " Could not encrypt message. %1" + ).arg(l10n.formatTimestamp(lastactiontime)) elif status == 'forcepow': statusText = _translate( - "MainWindow", "Forced difficulty override. Send should start soon.") + "MainWindow", + "Forced difficulty override. Send should start soon.") else: - statusText = _translate("MainWindow", "Unknown status: %1 %2").arg(status).arg( + statusText = _translate( + "MainWindow", "Unknown status: %1 %2").arg(status).arg( l10n.formatTimestamp(lastactiontime)) - newItem = myTableWidgetItem(statusText) - newItem.setToolTip(statusText) - newItem.setData(QtCore.Qt.UserRole, QtCore.QByteArray(ackdata)) - newItem.setData(33, int(lastactiontime)) - newItem.setFlags( - QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - items.append(newItem) + + items = [ + MessageList_AddressWidget( + toAddress, unicode(acct.toLabel, 'utf-8')), + MessageList_AddressWidget( + fromAddress, unicode(acct.fromLabel, 'utf-8')), + MessageList_SubjectWidget( + str(subject), unicode(acct.subject, 'utf-8', 'replace')), + MessageList_TimeWidget( + statusText, False, lastactiontime, ackdata)] self.addMessageListItem(tableWidget, items) + return acct - def addMessageListItemInbox(self, tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read): - font = QtGui.QFont() - font.setBold(True) + def addMessageListItemInbox( + self, tableWidget, toAddress, fromAddress, subject, + msgid, received, read + ): if toAddress == str_broadcast_subscribers: acct = accountClass(fromAddress) else: - acct = accountClass(toAddress) - if acct is None: - acct = accountClass(fromAddress) + acct = accountClass(toAddress) or accountClass(fromAddress) if acct is None: acct = BMAccount(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, "") - - items = [] - #to - MessageList_AddressWidget(items, toAddress, unicode(acct.toLabel, 'utf-8'), not read) - # from - MessageList_AddressWidget(items, fromAddress, unicode(acct.fromLabel, 'utf-8'), not read) - # subject - MessageList_SubjectWidget(items, str(subject), unicode(acct.subject, 'utf-8', 'replace'), not read) - # time received - time_item = myTableWidgetItem(l10n.formatTimestamp(received)) - time_item.setToolTip(l10n.formatTimestamp(received)) - time_item.setData(QtCore.Qt.UserRole, QtCore.QByteArray(msgid)) - time_item.setData(33, int(received)) - time_item.setFlags( - QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - if not read: - time_item.setFont(font) - items.append(time_item) + + items = [ + MessageList_AddressWidget( + toAddress, unicode(acct.toLabel, 'utf-8'), not read), + MessageList_AddressWidget( + fromAddress, unicode(acct.fromLabel, 'utf-8'), not read), + MessageList_SubjectWidget( + str(subject), unicode(acct.subject, 'utf-8', 'replace'), + not read), + MessageList_TimeWidget( + l10n.formatTimestamp(received), not read, received, msgid) + ] self.addMessageListItem(tableWidget, items) + return acct # Load Sent items from database @@ -1194,35 +1209,40 @@ class MyForm(settingsmixin.SMainWindow): xAddress = 'both' else: tableWidget.setColumnHidden(0, False) - if account is None: - tableWidget.setColumnHidden(1, False) - else: - tableWidget.setColumnHidden(1, True) + tableWidget.setColumnHidden(1, bool(account)) xAddress = 'fromaddress' - tableWidget.setUpdatesEnabled(False) - tableWidget.setSortingEnabled(False) - tableWidget.setRowCount(0) - queryreturn = helper_search.search_sql(xAddress, account, "sent", where, what, False) + queryreturn = helper_search.search_sql( + xAddress, account, "sent", where, what, False) for row in queryreturn: - toAddress, fromAddress, subject, status, ackdata, lastactiontime = row - self.addMessageListItemSent(tableWidget, toAddress, fromAddress, subject, status, ackdata, lastactiontime) + self.addMessageListItemSent(tableWidget, *row) tableWidget.horizontalHeader().setSortIndicator( 3, QtCore.Qt.DescendingOrder) tableWidget.setSortingEnabled(True) - tableWidget.horizontalHeaderItem(3).setText(_translate("MainWindow", "Sent", None)) + tableWidget.horizontalHeaderItem(3).setText( + _translate("MainWindow", "Sent")) tableWidget.setUpdatesEnabled(True) # Load messages from database file - def loadMessagelist(self, tableWidget, account, folder="inbox", where="", what="", unreadOnly = False): + def loadMessagelist( + self, tableWidget, account, folder="inbox", where="", what="", + unreadOnly=False + ): + tableWidget.setUpdatesEnabled(False) + tableWidget.setSortingEnabled(False) + tableWidget.setRowCount(0) + if folder == 'sent': self.loadSent(tableWidget, account, where, what) return if tableWidget == self.ui.tableWidgetInboxSubscriptions: xAddress = "fromaddress" + if not what: + where = _translate("MainWindow", "To") + what = str_broadcast_subscribers else: xAddress = "toaddress" if account is not None: @@ -1232,21 +1252,21 @@ class MyForm(settingsmixin.SMainWindow): tableWidget.setColumnHidden(0, False) tableWidget.setColumnHidden(1, False) - tableWidget.setUpdatesEnabled(False) - tableWidget.setSortingEnabled(False) - tableWidget.setRowCount(0) + queryreturn = helper_search.search_sql( + xAddress, account, folder, where, what, unreadOnly) - queryreturn = helper_search.search_sql(xAddress, account, folder, where, what, unreadOnly) - for row in queryreturn: - msgfolder, msgid, toAddress, fromAddress, subject, received, read = row - self.addMessageListItemInbox(tableWidget, msgfolder, msgid, toAddress, fromAddress, subject, received, read) + toAddress, fromAddress, subject, _, msgid, received, read = row + self.addMessageListItemInbox( + tableWidget, toAddress, fromAddress, subject, + msgid, received, read) tableWidget.horizontalHeader().setSortIndicator( 3, QtCore.Qt.DescendingOrder) tableWidget.setSortingEnabled(True) tableWidget.selectRow(0) - tableWidget.horizontalHeaderItem(3).setText(_translate("MainWindow", "Received", None)) + tableWidget.horizontalHeaderItem(3).setText( + _translate("MainWindow", "Received")) tableWidget.setUpdatesEnabled(True) # create application indicator @@ -1473,9 +1493,9 @@ class MyForm(settingsmixin.SMainWindow): def handleKeyPress(self, event, focus=None): """This method handles keypress events for all widgets on MyForm""" messagelist = self.getCurrentMessagelist() - folder = self.getCurrentFolder() if event.key() == QtCore.Qt.Key_Delete: - if isinstance(focus, MessageView) or isinstance(focus, QtGui.QTableWidget): + if isinstance(focus, (MessageView, QtGui.QTableWidget)): + folder = self.getCurrentFolder() if folder == "sent": self.on_action_SentTrash() else: @@ -1511,17 +1531,18 @@ class MyForm(settingsmixin.SMainWindow): self.ui.lineEditTo.setFocus() event.ignore() elif event.key() == QtCore.Qt.Key_F: - searchline = self.getCurrentSearchLine(retObj=True) - if searchline: - searchline.setFocus() + try: + self.getCurrentSearchLine(retObj=True).setFocus() + except AttributeError: + pass event.ignore() if not event.isAccepted(): return if isinstance(focus, MessageView): return MessageView.keyPressEvent(focus, event) - elif isinstance(focus, QtGui.QTableWidget): + if isinstance(focus, QtGui.QTableWidget): return QtGui.QTableWidget.keyPressEvent(focus, event) - elif isinstance(focus, QtGui.QTreeWidget): + if isinstance(focus, QtGui.QTreeWidget): return QtGui.QTreeWidget.keyPressEvent(focus, event) # menu button 'manage keys' @@ -1546,7 +1567,7 @@ class MyForm(settingsmixin.SMainWindow): reply = QtGui.QMessageBox.question(self, _translate("MainWindow", "Open keys.dat?"), _translate( "MainWindow", "You may manage your keys by editing the keys.dat file stored in\n %1 \nIt is important that you back up this file. Would you like to open the file now? (Be sure to close Bitmessage before making any changes.)").arg(state.appdata), QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if reply == QtGui.QMessageBox.Yes: - shared.openKeysFile() + openKeysFile() # menu button 'delete all treshed messages' def click_actionDeleteAllTrashedMessages(self): @@ -1668,7 +1689,7 @@ class MyForm(settingsmixin.SMainWindow): if color == 'red': self.pushButtonStatusIcon.setIcon( QtGui.QIcon(":/newPrefix/images/redicon.png")) - shared.statusIconColor = 'red' + state.statusIconColor = 'red' # if the connection is lost then show a notification if self.connected and _notifications_enabled: self.notifierShow( @@ -1694,7 +1715,7 @@ class MyForm(settingsmixin.SMainWindow): self.statusbar.clearMessage() self.pushButtonStatusIcon.setIcon( QtGui.QIcon(":/newPrefix/images/yellowicon.png")) - shared.statusIconColor = 'yellow' + state.statusIconColor = 'yellow' # if a new connection has been established then show a notification if not self.connected and _notifications_enabled: self.notifierShow( @@ -1712,7 +1733,7 @@ class MyForm(settingsmixin.SMainWindow): self.statusbar.clearMessage() self.pushButtonStatusIcon.setIcon( QtGui.QIcon(":/newPrefix/images/greenicon.png")) - shared.statusIconColor = 'green' + state.statusIconColor = 'green' if not self.connected and _notifications_enabled: self.notifierShow( 'Bitmessage', @@ -1735,7 +1756,7 @@ class MyForm(settingsmixin.SMainWindow): self.drawTrayIcon(iconFileName, self.findInboxUnreadCount()) def calcTrayIcon(self, iconFileName, inboxUnreadCount): - pixmap = QtGui.QPixmap(":/newPrefix/images/"+iconFileName) + pixmap = QtGui.QPixmap(":/newPrefix/images/" + iconFileName) if inboxUnreadCount > 0: # choose font and calculate font parameters fontName = "Lucida" @@ -1747,7 +1768,8 @@ class MyForm(settingsmixin.SMainWindow): rect = fontMetrics.boundingRect(txt) # margins that we add in the top-right corner marginX = 2 - marginY = 0 # it looks like -2 is also ok due to the error of metric + # it looks like -2 is also ok due to the error of metric + marginY = 0 # if it renders too wide we need to change it to a plus symbol if rect.width() > 20: txt = "+" @@ -1787,11 +1809,18 @@ class MyForm(settingsmixin.SMainWindow): return self.unreadCount def updateSentItemStatusByToAddress(self, toAddress, textToDisplay): - for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: + for sent in ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxSubscriptions, + self.ui.tableWidgetInboxChans + ): treeWidget = self.widgetConvert(sent) if self.getCurrentFolder(treeWidget) != "sent": continue - if treeWidget in [self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] and self.getCurrentAccount(treeWidget) != toAddress: + if treeWidget in ( + self.ui.treeWidgetSubscriptions, + self.ui.treeWidgetChans + ) and self.getCurrentAccount(treeWidget) != toAddress: continue for i in range(sent.rowCount()): @@ -1811,15 +1840,17 @@ class MyForm(settingsmixin.SMainWindow): def updateSentItemStatusByAckdata(self, ackdata, textToDisplay): if type(ackdata) is str: ackdata = QtCore.QByteArray(ackdata) - for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: + for sent in ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxSubscriptions, + self.ui.tableWidgetInboxChans + ): treeWidget = self.widgetConvert(sent) if self.getCurrentFolder(treeWidget) != "sent": continue for i in range(sent.rowCount()): - toAddress = sent.item( - i, 0).data(QtCore.Qt.UserRole) - tableAckdata = sent.item( - i, 3).data(QtCore.Qt.UserRole).toPyObject() + toAddress = sent.item(i, 0).data(QtCore.Qt.UserRole) + tableAckdata = sent.item(i, 3).data() status, addressVersionNumber, streamNumber, ripe = decodeAddress( toAddress) if ackdata == tableAckdata: @@ -1843,8 +1874,7 @@ class MyForm(settingsmixin.SMainWindow): ): i = None for i in range(inbox.rowCount()): - if msgid == \ - inbox.item(i, 3).data(QtCore.Qt.UserRole).toPyObject(): + if msgid == inbox.item(i, 3).data(): break else: continue @@ -1919,11 +1949,13 @@ class MyForm(settingsmixin.SMainWindow): newRows[address] = [label, AccountMixin.NORMAL] completerList = [] - for address in sorted(oldRows, key = lambda x: oldRows[x][2], reverse = True): - if address in newRows: - completerList.append(unicode(newRows[address][0], encoding="UTF-8") + " <" + address + ">") - newRows.pop(address) - else: + for address in sorted( + oldRows, key=lambda x: oldRows[x][2], reverse=True + ): + try: + completerList.append( + newRows.pop(address)[0] + " <" + address + ">") + except KeyError: self.ui.tableWidgetAddressBook.removeRow(oldRows[address][2]) for address in newRows: addRow(address, newRows[address][0], newRows[address][1]) @@ -1996,11 +2028,14 @@ class MyForm(settingsmixin.SMainWindow): acct = accountClass(fromAddress) - if sendMessageToPeople: # To send a message to specific people (rather than broadcast) - toAddressesList = [s.strip() - for s in toAddresses.replace(',', ';').split(';')] - toAddressesList = list(set( - toAddressesList)) # remove duplicate addresses. If the user has one address with a BM- and the same address without the BM-, this will not catch it. They'll send the message to the person twice. + # To send a message to specific people (rather than broadcast) + if sendMessageToPeople: + toAddressesList = set([ + s.strip() for s in toAddresses.replace(',', ';').split(';') + ]) + # remove duplicate addresses. If the user has one address + # with a BM- and the same address without the BM-, this will + # not catch it. They'll send the message to the person twice. for toAddress in toAddressesList: if toAddress != '': # label plus address @@ -2119,7 +2154,7 @@ class MyForm(settingsmixin.SMainWindow): "MainWindow", "Concerning the address %1, Bitmessage cannot handle stream numbers of %2. Perhaps upgrade Bitmessage to the latest version.").arg(toAddress).arg(str(streamNumber))) continue self.statusbar.clearMessage() - if shared.statusIconColor == 'red': + if state.statusIconColor == 'red': self.updateStatusBar(_translate( "MainWindow", "Warning: You are currently not connected." @@ -2207,7 +2242,7 @@ class MyForm(settingsmixin.SMainWindow): '''INSERT INTO sent VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''', *t) toLabel = str_broadcast_subscribers - + self.displayNewSentMessage( toAddress, toLabel, fromAddress, subject, message, ackdata) @@ -2308,54 +2343,88 @@ class MyForm(settingsmixin.SMainWindow): # receives a message to an address that is acting as a # pseudo-mailing-list. The message will be broadcast out. This function # puts the message on the 'Sent' tab. - def displayNewSentMessage(self, toAddress, toLabel, fromAddress, subject, message, ackdata): + def displayNewSentMessage( + self, toAddress, toLabel, fromAddress, subject, + message, ackdata): acct = accountClass(fromAddress) acct.parseMessage(toAddress, fromAddress, subject, message) tab = -1 - for sent in [self.ui.tableWidgetInbox, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans]: + for sent in ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxSubscriptions, + self.ui.tableWidgetInboxChans + ): tab += 1 if tab == 1: tab = 2 treeWidget = self.widgetConvert(sent) if self.getCurrentFolder(treeWidget) != "sent": continue - if treeWidget == self.ui.treeWidgetYourIdentities and self.getCurrentAccount(treeWidget) not in (fromAddress, None, False): + if treeWidget == self.ui.treeWidgetYourIdentities \ + and self.getCurrentAccount(treeWidget) not in ( + fromAddress, None, False): continue - elif treeWidget in [self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans] and self.getCurrentAccount(treeWidget) != toAddress: + elif treeWidget in ( + self.ui.treeWidgetSubscriptions, + self.ui.treeWidgetChans + ) and self.getCurrentAccount(treeWidget) != toAddress: continue - elif not helper_search.check_match(toAddress, fromAddress, subject, message, self.getCurrentSearchOption(tab), self.getCurrentSearchLine(tab)): + elif not helper_search.check_match( + toAddress, fromAddress, subject, message, + self.getCurrentSearchOption(tab), + self.getCurrentSearchLine(tab) + ): continue - - self.addMessageListItemSent(sent, toAddress, fromAddress, subject, "msgqueued", ackdata, time.time()) - self.getAccountTextedit(acct).setPlainText(unicode(message, 'utf-8', 'replace')) + + self.addMessageListItemSent( + sent, toAddress, fromAddress, subject, + "msgqueued", ackdata, time.time()) + self.getAccountTextedit(acct).setPlainText(message) sent.setCurrentCell(0, 0) - def displayNewInboxMessage(self, inventoryHash, toAddress, fromAddress, subject, message): - if toAddress == str_broadcast_subscribers: - acct = accountClass(fromAddress) - else: - acct = accountClass(toAddress) + def displayNewInboxMessage( + self, inventoryHash, toAddress, fromAddress, subject, message): + acct = accountClass( + fromAddress if toAddress == str_broadcast_subscribers + else toAddress + ) inbox = self.getAccountMessagelist(acct) - ret = None + ret = treeWidget = None tab = -1 - for treeWidget in [self.ui.treeWidgetYourIdentities, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans]: + for treeWidget in ( + self.ui.treeWidgetYourIdentities, + self.ui.treeWidgetSubscriptions, + self.ui.treeWidgetChans + ): tab += 1 if tab == 1: tab = 2 - tableWidget = self.widgetConvert(treeWidget) - if not helper_search.check_match(toAddress, fromAddress, subject, message, self.getCurrentSearchOption(tab), self.getCurrentSearchLine(tab)): + if not helper_search.check_match( + toAddress, fromAddress, subject, message, + self.getCurrentSearchOption(tab), + self.getCurrentSearchLine(tab) + ): continue - if tableWidget == inbox and self.getCurrentAccount(treeWidget) == acct.address and self.getCurrentFolder(treeWidget) in ["inbox", None]: - ret = self.addMessageListItemInbox(inbox, "inbox", inventoryHash, toAddress, fromAddress, subject, time.time(), 0) - elif treeWidget == self.ui.treeWidgetYourIdentities and self.getCurrentAccount(treeWidget) is None and self.getCurrentFolder(treeWidget) in ["inbox", "new", None]: - ret = self.addMessageListItemInbox(tableWidget, "inbox", inventoryHash, toAddress, fromAddress, subject, time.time(), 0) + tableWidget = self.widgetConvert(treeWidget) + current_account = self.getCurrentAccount(treeWidget) + current_folder = self.getCurrentFolder(treeWidget) + # pylint: disable=too-many-boolean-expressions + if ((tableWidget == inbox + and current_account == acct.address + and current_folder in ("inbox", None)) + or (treeWidget == self.ui.treeWidgetYourIdentities + and current_account is None + and current_folder in ("inbox", "new", None))): + ret = self.addMessageListItemInbox( + tableWidget, toAddress, fromAddress, subject, + inventoryHash, time.time(), False) + if ret is None: acct.parseMessage(toAddress, fromAddress, subject, "") else: acct = ret - # pylint:disable=undefined-loop-variable self.propagateUnreadCount(widget=treeWidget if ret else None) - if BMConfigParser().getboolean( + if BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'showtraynotifications'): self.notifierShow( _translate("MainWindow", "New Message"), @@ -2363,16 +2432,22 @@ class MyForm(settingsmixin.SMainWindow): unicode(acct.fromLabel, 'utf-8')), sound.SOUND_UNKNOWN ) - if self.getCurrentAccount() is not None and ((self.getCurrentFolder(treeWidget) != "inbox" and self.getCurrentFolder(treeWidget) is not None) or self.getCurrentAccount(treeWidget) != acct.address): - # Ubuntu should notify of new message irespective of + if self.getCurrentAccount() is not None and ( + (self.getCurrentFolder(treeWidget) != "inbox" + and self.getCurrentFolder(treeWidget) is not None) + or self.getCurrentAccount(treeWidget) != acct.address): + # Ubuntu should notify of new message irrespective of # whether it's in current message list or not self.indicatorUpdate(True, to_label=acct.toLabel) - # cannot find item to pass here ): - if hasattr(acct, "feedback") \ - and acct.feedback != GatewayAccount.ALL_OK: - if acct.feedback == GatewayAccount.REGISTRATION_DENIED: - dialogs.EmailGatewayDialog( - self, BMConfigParser(), acct).exec_() + + try: + if acct.feedback != GatewayAccount.ALL_OK: + if acct.feedback == GatewayAccount.REGISTRATION_DENIED: + dialogs.EmailGatewayDialog( + self, BMConfigParser(), acct).exec_() + # possible other branches? + except AttributeError: + pass def click_pushButtonAddAddressBook(self, dialog=None): if not dialog: @@ -2525,17 +2600,11 @@ class MyForm(settingsmixin.SMainWindow): if idCount == 0: return - font = QtGui.QFont() - font.setBold(False) - msgids = [] for i in range(0, idCount): - msgids.append(str(tableWidget.item( - i, 3).data(QtCore.Qt.UserRole).toPyObject())) - tableWidget.item(i, 0).setUnread(False) - tableWidget.item(i, 1).setUnread(False) - tableWidget.item(i, 2).setUnread(False) - tableWidget.item(i, 3).setFont(font) + msgids.append(tableWidget.item(i, 3).data()) + for col in xrange(tableWidget.columnCount()): + tableWidget.item(i, col).setUnread(False) markread = sqlExecuteChunked( "UPDATE inbox SET read = 1 WHERE msgid IN({0}) AND read=0", @@ -2603,10 +2672,8 @@ class MyForm(settingsmixin.SMainWindow): ) + "\n\n" + _translate( "MainWindow", "Wait until these tasks finish?"), - QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | - QtGui.QMessageBox.Cancel, - QtGui.QMessageBox.Cancel - ) + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No + | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) if reply == QtGui.QMessageBox.No: waitForPow = False elif reply == QtGui.QMessageBox.Cancel: @@ -2623,16 +2690,14 @@ class MyForm(settingsmixin.SMainWindow): " synchronisation finishes?", None, QtCore.QCoreApplication.CodecForTr, pendingDownload() ), - QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | - QtGui.QMessageBox.Cancel, - QtGui.QMessageBox.Cancel - ) + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No + | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) if reply == QtGui.QMessageBox.Yes: self.wait = waitForSync = True elif reply == QtGui.QMessageBox.Cancel: return - if shared.statusIconColor == 'red' and not BMConfigParser().safeGetBoolean( + if state.statusIconColor == 'red' and not BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'dontconnect'): reply = QtGui.QMessageBox.question( self, _translate("MainWindow", "Not connected"), @@ -2642,10 +2707,8 @@ class MyForm(settingsmixin.SMainWindow): " quit now, it may cause delivery delays. Wait until" " connected and the synchronisation finishes?" ), - QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | - QtGui.QMessageBox.Cancel, - QtGui.QMessageBox.Cancel - ) + QtGui.QMessageBox.Yes | QtGui.QMessageBox.No + | QtGui.QMessageBox.Cancel, QtGui.QMessageBox.Cancel) if reply == QtGui.QMessageBox.Yes: waitForConnection = True self.wait = waitForSync = True @@ -2660,7 +2723,7 @@ class MyForm(settingsmixin.SMainWindow): if waitForConnection: self.updateStatusBar(_translate( "MainWindow", "Waiting for network connection...")) - while shared.statusIconColor == 'red': + while state.statusIconColor == 'red': time.sleep(0.5) QtCore.QCoreApplication.processEvents( QtCore.QEventLoop.AllEvents, 1000 @@ -2752,6 +2815,7 @@ class MyForm(settingsmixin.SMainWindow): QtCore.QEventLoop.AllEvents, 1000 ) shutdown.doCleanShutdown() + self.updateStatusBar(_translate( "MainWindow", "Stopping notifications... %1%").arg(90)) self.tray.hide() @@ -2760,20 +2824,21 @@ class MyForm(settingsmixin.SMainWindow): "MainWindow", "Shutdown imminent... %1%").arg(100)) logger.info("Shutdown complete") - super(MyForm, myapp).close() - # return - sys.exit() + self.close() + # FIXME: rewrite loops with timer instead + if self.wait: + self.destroy() + app.quit() - # window close event def closeEvent(self, event): - self.appIndicatorHide() - + """window close event""" + event.ignore() trayonclose = BMConfigParser().safeGetBoolean( 'bitmessagesettings', 'trayonclose') - - event.ignore() - if not trayonclose: - # quit the application + if trayonclose: + self.appIndicatorHide() + else: + # custom quit method self.quit() def on_action_InboxMessageForceHtml(self): @@ -2812,8 +2877,7 @@ class MyForm(settingsmixin.SMainWindow): # modified = 0 for row in tableWidget.selectedIndexes(): currentRow = row.row() - msgid = str(tableWidget.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + msgid = tableWidget.item(currentRow, 3).data() msgids.add(msgid) # if not tableWidget.item(currentRow, 0).unread: # modified += 1 @@ -2839,13 +2903,13 @@ class MyForm(settingsmixin.SMainWindow): # Format predefined text on message reply. def quoted_text(self, message): if not BMConfigParser().safeGetBoolean('bitmessagesettings', 'replybelow'): - return '\n\n------------------------------------------------------\n' + message + return '\n\n------------------------------------------------------\n' + message + + quoteWrapper = textwrap.TextWrapper( + replace_whitespace=False, initial_indent='> ', + subsequent_indent='> ', break_long_words=False, + break_on_hyphens=False) - quoteWrapper = textwrap.TextWrapper(replace_whitespace = False, - initial_indent = '> ', - subsequent_indent = '> ', - break_long_words = False, - break_on_hyphens = False) def quote_line(line): # Do quote empty lines. if line == '' or line.isspace(): @@ -2858,18 +2922,20 @@ class MyForm(settingsmixin.SMainWindow): return quoteWrapper.fill(line) return '\n'.join([quote_line(l) for l in message.splitlines()]) + '\n\n' - def setSendFromComboBox(self, address = None): + def setSendFromComboBox(self, address=None): if address is None: messagelist = self.getCurrentMessagelist() - if messagelist: - currentInboxRow = messagelist.currentRow() - address = messagelist.item( - currentInboxRow, 0).address - for box in [self.ui.comboBoxSendFrom, self.ui.comboBoxSendFromBroadcast]: - listOfAddressesInComboBoxSendFrom = [str(box.itemData(i).toPyObject()) for i in range(box.count())] - if address in listOfAddressesInComboBoxSendFrom: - currentIndex = listOfAddressesInComboBoxSendFrom.index(address) - box.setCurrentIndex(currentIndex) + if not messagelist: + return + currentInboxRow = messagelist.currentRow() + address = messagelist.item(currentInboxRow, 0).address + for box in ( + self.ui.comboBoxSendFrom, self.ui.comboBoxSendFromBroadcast + ): + for i in range(box.count()): + if str(box.itemData(i).toPyObject()) == address: + box.setCurrentIndex(i) + break else: box.setCurrentIndex(0) @@ -2901,8 +2967,7 @@ class MyForm(settingsmixin.SMainWindow): acct = accountClass(toAddressAtCurrentInboxRow) fromAddressAtCurrentInboxRow = tableWidget.item( currentInboxRow, column_from).address - msgid = str(tableWidget.item( - currentInboxRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + msgid = tableWidget.item(currentInboxRow, 3).data() queryreturn = sqlQuery( "SELECT message FROM inbox WHERE msgid=?", msgid ) or sqlQuery("SELECT message FROM sent WHERE ackdata=?", msgid) @@ -2985,7 +3050,7 @@ class MyForm(settingsmixin.SMainWindow): quotedText = self.quoted_text( unicode(messageAtCurrentInboxRow, 'utf-8', 'replace')) widget['message'].setPlainText(quotedText) - if acct.subject[0:3] in ['Re:', 'RE:']: + if acct.subject[0:3] in ('Re:', 'RE:'): widget['subject'].setText( tableWidget.item(currentInboxRow, 2).label) else: @@ -3001,7 +3066,6 @@ class MyForm(settingsmixin.SMainWindow): if not tableWidget: return currentInboxRow = tableWidget.currentRow() - # tableWidget.item(currentRow,1).data(Qt.UserRole).toPyObject() addressAtCurrentInboxRow = tableWidget.item( currentInboxRow, 1).data(QtCore.Qt.UserRole) self.ui.tabWidget.setCurrentIndex( @@ -3015,7 +3079,6 @@ class MyForm(settingsmixin.SMainWindow): if not tableWidget: return currentInboxRow = tableWidget.currentRow() - # tableWidget.item(currentRow,1).data(Qt.UserRole).toPyObject() addressAtCurrentInboxRow = tableWidget.item( currentInboxRow, 1).data(QtCore.Qt.UserRole) recipientAddress = tableWidget.item( @@ -3039,23 +3102,28 @@ class MyForm(settingsmixin.SMainWindow): "Error: You cannot add the same address to your blacklist" " twice. Try renaming the existing one if you want.")) - def deleteRowFromMessagelist(self, row = None, inventoryHash = None, ackData = None, messageLists = None): + def deleteRowFromMessagelist( + self, row=None, inventoryHash=None, ackData=None, messageLists=None + ): if messageLists is None: - messageLists = (self.ui.tableWidgetInbox, self.ui.tableWidgetInboxChans, self.ui.tableWidgetInboxSubscriptions) + messageLists = ( + self.ui.tableWidgetInbox, + self.ui.tableWidgetInboxChans, + self.ui.tableWidgetInboxSubscriptions + ) elif type(messageLists) not in (list, tuple): - messageLists = (messageLists) + messageLists = (messageLists,) for messageList in messageLists: if row is not None: - inventoryHash = str(messageList.item(row, 3).data( - QtCore.Qt.UserRole).toPyObject()) + inventoryHash = messageList.item(row, 3).data() messageList.removeRow(row) elif inventoryHash is not None: for i in range(messageList.rowCount() - 1, -1, -1): - if messageList.item(i, 3).data(QtCore.Qt.UserRole).toPyObject() == inventoryHash: + if messageList.item(i, 3).data() == inventoryHash: messageList.removeRow(i) elif ackData is not None: for i in range(messageList.rowCount() - 1, -1, -1): - if messageList.item(i, 3).data(QtCore.Qt.UserRole).toPyObject() == ackData: + if messageList.item(i, 3).data() == ackData: messageList.removeRow(i) # Send item on the Inbox tab to trash @@ -3065,20 +3133,21 @@ class MyForm(settingsmixin.SMainWindow): return currentRow = 0 folder = self.getCurrentFolder() - shifted = QtGui.QApplication.queryKeyboardModifiers() & QtCore.Qt.ShiftModifier - tableWidget.setUpdatesEnabled(False); - inventoryHashesToTrash = [] + shifted = QtGui.QApplication.queryKeyboardModifiers() \ + & QtCore.Qt.ShiftModifier + tableWidget.setUpdatesEnabled(False) + inventoryHashesToTrash = set() # ranges in reversed order - for r in sorted(tableWidget.selectedRanges(), key=lambda r: r.topRow())[::-1]: - for i in range(r.bottomRow()-r.topRow()+1): - inventoryHashToTrash = str(tableWidget.item( - r.topRow()+i, 3).data(QtCore.Qt.UserRole).toPyObject()) - if inventoryHashToTrash in inventoryHashesToTrash: - continue - inventoryHashesToTrash.append(inventoryHashToTrash) + for r in sorted( + tableWidget.selectedRanges(), key=lambda r: r.topRow() + )[::-1]: + for i in range(r.bottomRow() - r.topRow() + 1): + inventoryHashesToTrash.add( + tableWidget.item(r.topRow() + i, 3).data()) currentRow = r.topRow() self.getCurrentMessageTextedit().setText("") - tableWidget.model().removeRows(r.topRow(), r.bottomRow()-r.topRow()+1) + tableWidget.model().removeRows( + r.topRow(), r.bottomRow() - r.topRow() + 1) idCount = len(inventoryHashesToTrash) sqlExecuteChunked( ("DELETE FROM inbox" if folder == "trash" or shifted else @@ -3095,22 +3164,23 @@ class MyForm(settingsmixin.SMainWindow): return currentRow = 0 tableWidget.setUpdatesEnabled(False) - inventoryHashesToTrash = [] + inventoryHashesToTrash = set() # ranges in reversed order - for r in sorted(tableWidget.selectedRanges(), key=lambda r: r.topRow())[::-1]: - for i in range(r.bottomRow()-r.topRow()+1): - inventoryHashToTrash = str(tableWidget.item( - r.topRow()+i, 3).data(QtCore.Qt.UserRole).toPyObject()) - if inventoryHashToTrash in inventoryHashesToTrash: - continue - inventoryHashesToTrash.append(inventoryHashToTrash) + for r in sorted( + tableWidget.selectedRanges(), key=lambda r: r.topRow() + )[::-1]: + for i in range(r.bottomRow() - r.topRow() + 1): + inventoryHashesToTrash.add( + tableWidget.item(r.topRow() + i, 3).data()) currentRow = r.topRow() self.getCurrentMessageTextedit().setText("") - tableWidget.model().removeRows(r.topRow(), r.bottomRow()-r.topRow()+1) + tableWidget.model().removeRows( + r.topRow(), r.bottomRow() - r.topRow() + 1) tableWidget.selectRow(0 if currentRow == 0 else currentRow - 1) idCount = len(inventoryHashesToTrash) - sqlExecuteChunked('''UPDATE inbox SET folder='inbox' WHERE msgid IN({0})''', - idCount, *inventoryHashesToTrash) + sqlExecuteChunked( + "UPDATE inbox SET folder='inbox' WHERE msgid IN({0})", + idCount, *inventoryHashesToTrash) tableWidget.selectRow(0 if currentRow == 0 else currentRow - 1) tableWidget.setUpdatesEnabled(True) self.propagateUnreadCount() @@ -3128,8 +3198,7 @@ class MyForm(settingsmixin.SMainWindow): subjectAtCurrentInboxRow = '' # Retrieve the message data out of the SQL database - msgid = str(tableWidget.item( - currentInboxRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + msgid = tableWidget.item(currentInboxRow, 3).data() queryreturn = sqlQuery( '''select message from inbox where msgid=?''', msgid) if queryreturn != []: @@ -3157,8 +3226,7 @@ class MyForm(settingsmixin.SMainWindow): shifted = QtGui.QApplication.queryKeyboardModifiers() & QtCore.Qt.ShiftModifier while tableWidget.selectedIndexes() != []: currentRow = tableWidget.selectedIndexes()[0].row() - ackdataToTrash = str(tableWidget.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + ackdataToTrash = tableWidget.item(currentRow, 3).data() sqlExecute( "DELETE FROM sent" if folder == "trash" or shifted else "UPDATE sent SET folder='trash'" @@ -3381,13 +3449,13 @@ class MyForm(settingsmixin.SMainWindow): return None def getCurrentTreeWidget(self): - currentIndex = self.ui.tabWidget.currentIndex(); - treeWidgetList = [ + currentIndex = self.ui.tabWidget.currentIndex() + treeWidgetList = ( self.ui.treeWidgetYourIdentities, False, self.ui.treeWidgetSubscriptions, self.ui.treeWidgetChans - ] + ) if currentIndex >= 0 and currentIndex < len(treeWidgetList): return treeWidgetList[currentIndex] else: @@ -3405,18 +3473,16 @@ class MyForm(settingsmixin.SMainWindow): return self.ui.treeWidgetYourIdentities def getCurrentMessagelist(self): - currentIndex = self.ui.tabWidget.currentIndex(); - messagelistList = [ + currentIndex = self.ui.tabWidget.currentIndex() + messagelistList = ( self.ui.tableWidgetInbox, False, self.ui.tableWidgetInboxSubscriptions, self.ui.tableWidgetInboxChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): return messagelistList[currentIndex] - else: - return False - + def getAccountMessagelist(self, account): try: if account.type == AccountMixin.CHAN: @@ -3433,24 +3499,18 @@ class MyForm(settingsmixin.SMainWindow): if messagelist: currentRow = messagelist.currentRow() if currentRow >= 0: - msgid = str(messagelist.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) - # data is saved at the 4. column of the table... - return msgid - return False + return messagelist.item(currentRow, 3).data() def getCurrentMessageTextedit(self): currentIndex = self.ui.tabWidget.currentIndex() - messagelistList = [ + messagelistList = ( self.ui.textEditInboxMessage, False, self.ui.textEditInboxMessageSubscriptions, self.ui.textEditInboxMessageChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): return messagelistList[currentIndex] - else: - return False def getAccountTextedit(self, account): try: @@ -3466,33 +3526,28 @@ class MyForm(settingsmixin.SMainWindow): def getCurrentSearchLine(self, currentIndex=None, retObj=False): if currentIndex is None: currentIndex = self.ui.tabWidget.currentIndex() - messagelistList = [ + messagelistList = ( self.ui.inboxSearchLineEdit, False, self.ui.inboxSearchLineEditSubscriptions, self.ui.inboxSearchLineEditChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): - if retObj: - return messagelistList[currentIndex] - else: - return messagelistList[currentIndex].text().toUtf8().data() - else: - return None + return ( + messagelistList[currentIndex] if retObj + else messagelistList[currentIndex].text().toUtf8().data()) def getCurrentSearchOption(self, currentIndex=None): if currentIndex is None: currentIndex = self.ui.tabWidget.currentIndex() - messagelistList = [ + messagelistList = ( self.ui.inboxSearchOption, False, self.ui.inboxSearchOptionSubscriptions, self.ui.inboxSearchOptionChans, - ] + ) if currentIndex >= 0 and currentIndex < len(messagelistList): - return messagelistList[currentIndex].currentText().toUtf8().data() - else: - return None + return messagelistList[currentIndex].currentText() # Group of functions for the Your Identities dialog box def getCurrentItem(self, treeWidget=None): @@ -3596,12 +3651,11 @@ class MyForm(settingsmixin.SMainWindow): tableWidget = self.getCurrentMessagelist() currentColumn = tableWidget.currentColumn() currentRow = tableWidget.currentRow() - if currentColumn not in [0, 1, 2]: # to, from, subject - if self.getCurrentFolder() == "sent": - currentColumn = 0 - else: - currentColumn = 1 - if self.getCurrentFolder() == "sent": + currentFolder = self.getCurrentFolder() + if currentColumn not in (0, 1, 2): # to, from, subject + currentColumn = 0 if currentFolder == "sent" else 1 + + if currentFolder == "sent": myAddress = tableWidget.item(currentRow, 1).data(QtCore.Qt.UserRole) otherAddress = tableWidget.item(currentRow, 0).data(QtCore.Qt.UserRole) else: @@ -3614,18 +3668,18 @@ class MyForm(settingsmixin.SMainWindow): text = str(tableWidget.item(currentRow, currentColumn).label) else: text = tableWidget.item(currentRow, currentColumn).data(QtCore.Qt.UserRole) - text = unicode(str(text), 'utf-8', 'ignore') + clipboard = QtGui.QApplication.clipboard() clipboard.setText(text) - #set avatar functions + # set avatar functions def on_action_TreeWidgetSetAvatar(self): address = self.getCurrentAccount() self.setAvatar(address) def on_action_AddressBookSetAvatar(self): self.on_action_SetAvatar(self.ui.tableWidgetAddressBook) - + def on_action_SetAvatar(self, thisTableWidget): currentRow = thisTableWidget.currentRow() addressAtCurrentRow = thisTableWidget.item( @@ -3635,19 +3689,36 @@ class MyForm(settingsmixin.SMainWindow): thisTableWidget.item( currentRow, 0).setIcon(avatarize(addressAtCurrentRow)) + # TODO: reuse utils def setAvatar(self, addressAtCurrentRow): if not os.path.exists(state.appdata + 'avatars/'): os.makedirs(state.appdata + 'avatars/') hash = hashlib.md5(addBMIfNotPresent(addressAtCurrentRow)).hexdigest() - extensions = ['PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] - # http://pyqt.sourceforge.net/Docs/PyQt4/qimagereader.html#supportedImageFormats - names = {'BMP':'Windows Bitmap', 'GIF':'Graphic Interchange Format', 'JPG':'Joint Photographic Experts Group', 'JPEG':'Joint Photographic Experts Group', 'MNG':'Multiple-image Network Graphics', 'PNG':'Portable Network Graphics', 'PBM':'Portable Bitmap', 'PGM':'Portable Graymap', 'PPM':'Portable Pixmap', 'TIFF':'Tagged Image File Format', 'XBM':'X11 Bitmap', 'XPM':'X11 Pixmap', 'SVG':'Scalable Vector Graphics', 'TGA':'Targa Image Format'} + extensions = [ + 'PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', + 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] + + names = { + 'BMP': 'Windows Bitmap', + 'GIF': 'Graphic Interchange Format', + 'JPG': 'Joint Photographic Experts Group', + 'JPEG': 'Joint Photographic Experts Group', + 'MNG': 'Multiple-image Network Graphics', + 'PNG': 'Portable Network Graphics', + 'PBM': 'Portable Bitmap', + 'PGM': 'Portable Graymap', + 'PPM': 'Portable Pixmap', + 'TIFF': 'Tagged Image File Format', + 'XBM': 'X11 Bitmap', + 'XPM': 'X11 Pixmap', + 'SVG': 'Scalable Vector Graphics', + 'TGA': 'Targa Image Format'} filters = [] all_images_filter = [] current_files = [] for ext in extensions: - filters += [ names[ext] + ' (*.' + ext.lower() + ')' ] - all_images_filter += [ '*.' + ext.lower() ] + filters += [names[ext] + ' (*.' + ext.lower() + ')'] + all_images_filter += ['*.' + ext.lower()] upper = state.appdata + 'avatars/' + hash + '.' + ext.upper() lower = state.appdata + 'avatars/' + hash + '.' + ext.lower() if os.path.isfile(lower): @@ -3658,28 +3729,34 @@ class MyForm(settingsmixin.SMainWindow): filters[1:1] = ['All files (*.*)'] sourcefile = QtGui.QFileDialog.getOpenFileName( self, _translate("MainWindow", "Set avatar..."), - filter = ';;'.join(filters) + filter=';;'.join(filters) ) # determine the correct filename (note that avatars don't use the suffix) destination = state.appdata + 'avatars/' + hash + '.' + sourcefile.split('.')[-1] exists = QtCore.QFile.exists(destination) if sourcefile == '': # ask for removal of avatar - if exists | (len(current_files)>0): - displayMsg = _translate("MainWindow", "Do you really want to remove this avatar?") + if exists | (len(current_files) > 0): + displayMsg = _translate( + "MainWindow", "Do you really want to remove this avatar?") overwrite = QtGui.QMessageBox.question( - self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) + self, 'Message', displayMsg, + QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) else: overwrite = QtGui.QMessageBox.No else: # ask whether to overwrite old avatar - if exists | (len(current_files)>0): - displayMsg = _translate("MainWindow", "You have already set an avatar for this address. Do you really want to overwrite it?") + if exists | (len(current_files) > 0): + displayMsg = _translate( + "MainWindow", + "You have already set an avatar for this address." + " Do you really want to overwrite it?") overwrite = QtGui.QMessageBox.question( - self, 'Message', displayMsg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) + self, 'Message', displayMsg, + QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) else: overwrite = QtGui.QMessageBox.No - + # copy the image file to the appdata folder if (not exists) | (overwrite == QtGui.QMessageBox.Yes): if overwrite == QtGui.QMessageBox.Yes: @@ -3865,8 +3942,7 @@ class MyForm(settingsmixin.SMainWindow): # Check to see if this item is toodifficult and display an additional # menu option (Force Send) if it is. if currentRow >= 0: - ackData = str(self.ui.tableWidgetInbox.item( - currentRow, 3).data(QtCore.Qt.UserRole).toPyObject()) + ackData = self.ui.tableWidgetInbox.item(currentRow, 3).data() queryreturn = sqlQuery('''SELECT status FROM sent where ackdata=?''', ackData) for row in queryreturn: status, = row @@ -3877,25 +3953,27 @@ class MyForm(settingsmixin.SMainWindow): def inboxSearchLineEditUpdated(self, text): # dynamic search for too short text is slow - if len(str(text)) < 3: + text = text.toUtf8() + if 0 < len(text) < 3: return messagelist = self.getCurrentMessagelist() - searchOption = self.getCurrentSearchOption() if messagelist: + searchOption = self.getCurrentSearchOption() account = self.getCurrentAccount() folder = self.getCurrentFolder() - self.loadMessagelist(messagelist, account, folder, searchOption, str(text)) + self.loadMessagelist( + messagelist, account, folder, searchOption, text) def inboxSearchLineEditReturnPressed(self): logger.debug("Search return pressed") searchLine = self.getCurrentSearchLine() messagelist = self.getCurrentMessagelist() - if len(str(searchLine)) < 3: + if messagelist and len(str(searchLine)) < 3: searchOption = self.getCurrentSearchOption() account = self.getCurrentAccount() folder = self.getCurrentFolder() - self.loadMessagelist(messagelist, account, folder, searchOption, searchLine) - if messagelist: + self.loadMessagelist( + messagelist, account, folder, searchOption, searchLine) messagelist.setFocus() def treeWidgetItemClicked(self): @@ -3921,7 +3999,7 @@ class MyForm(settingsmixin.SMainWindow): if (not isinstance(item, Ui_AddressWidget)) or (not self.getCurrentTreeWidget()) or self.getCurrentTreeWidget().currentItem() is None: return # not visible - if (not self.getCurrentItem()) or (not isinstance (self.getCurrentItem(), Ui_AddressWidget)): + if (not self.getCurrentItem()) or (not isinstance(self.getCurrentItem(), Ui_AddressWidget)): return # only currently selected item if item.address != self.getCurrentAccount(): @@ -3929,7 +4007,7 @@ class MyForm(settingsmixin.SMainWindow): # "All accounts" can't be renamed if item.type == AccountMixin.ALL: return - + newLabel = unicode(item.text(0), 'utf-8', 'ignore') oldLabel = item.defaultLabel() @@ -3954,12 +4032,12 @@ class MyForm(settingsmixin.SMainWindow): self.recurDepth -= 1 def tableWidgetInboxItemClicked(self): - folder = self.getCurrentFolder() messageTextedit = self.getCurrentMessageTextedit() if not messageTextedit: return msgid = self.getCurrentMessageId() + folder = self.getCurrentFolder() if msgid: queryreturn = sqlQuery( '''SELECT message FROM %s WHERE %s=?''' % ( @@ -4020,12 +4098,15 @@ class MyForm(settingsmixin.SMainWindow): self.rerenderAddressBook() def updateStatusBar(self, data): - if type(data) is tuple or type(data) is list: - option = data[1] - message = data[0] - else: + try: + option, message = data + except ValueError: option = 0 message = data + except TypeError: + logger.debug( + 'Invalid argument for updateStatusBar!', exc_info=True) + if message != "": logger.info('Status bar: ' + message) @@ -4041,19 +4122,16 @@ class MyForm(settingsmixin.SMainWindow): # Check to see whether we can connect to namecoin. # Hide the 'Fetch Namecoin ID' button if we can't. if BMConfigParser().safeGetBoolean( - 'bitmessagesettings', 'dontconnect' + 'bitmessagesettings', 'dontconnect' ) or self.namecoin.test()[0] == 'failed': logger.warning( - 'There was a problem testing for a Namecoin daemon. Hiding the' - ' Fetch Namecoin ID button') + 'There was a problem testing for a Namecoin daemon.' + ' Hiding the Fetch Namecoin ID button') self.ui.pushButtonFetchNamecoinID.hide() else: self.ui.pushButtonFetchNamecoinID.show() def initSettings(self): - QtCore.QCoreApplication.setOrganizationName("PyBitmessage") - QtCore.QCoreApplication.setOrganizationDomain("bitmessage.org") - QtCore.QCoreApplication.setApplicationName("pybitmessageqt") self.loadSettings() for attr, obj in self.ui.__dict__.iteritems(): if hasattr(obj, "__class__") and \ @@ -4063,20 +4141,11 @@ class MyForm(settingsmixin.SMainWindow): obj.loadSettings() -# 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 myTableWidgetItem(QtGui.QTableWidgetItem): - - def __lt__(self, other): - return int(self.data(33).toPyObject()) < int(other.data(33).toPyObject()) - - app = None myapp = None -class MySingleApplication(QtGui.QApplication): +class BitmessageQtApplication(QtGui.QApplication): """ Listener to allow our Qt form to get focus when another instance of the application is open. @@ -4089,8 +4158,12 @@ class MySingleApplication(QtGui.QApplication): uuid = '6ec0149b-96e1-4be1-93ab-1465fb3ebf7c' def __init__(self, *argv): - super(MySingleApplication, self).__init__(*argv) - id = MySingleApplication.uuid + super(BitmessageQtApplication, self).__init__(*argv) + id = BitmessageQtApplication.uuid + + QtCore.QCoreApplication.setOrganizationName("PyBitmessage") + QtCore.QCoreApplication.setOrganizationDomain("bitmessage.org") + QtCore.QCoreApplication.setApplicationName("pybitmessageqt") self.server = None self.is_running = False @@ -4118,6 +4191,8 @@ class MySingleApplication(QtGui.QApplication): self.server.listen(id) self.server.newConnection.connect(self.on_new_connection) + self.setStyleSheet("QStatusBar::item { border: 0px solid black }") + def __del__(self): if self.server: self.server.close() @@ -4130,34 +4205,28 @@ class MySingleApplication(QtGui.QApplication): def init(): global app if not app: - app = MySingleApplication(sys.argv) + app = BitmessageQtApplication(sys.argv) return app def run(): global myapp app = init() - app.setStyleSheet("QStatusBar::item { border: 0px solid black }") myapp = MyForm() - myapp.sqlInit() myapp.appIndicatorInit(app) - myapp.indicatorInit() - myapp.notifierInit() - myapp._firstrun = BMConfigParser().safeGetBoolean( - 'bitmessagesettings', 'dontconnect') + if myapp._firstrun: myapp.showConnectDialog() # ask the user if we may connect - myapp.ui.updateNetworkSwitchMenuLabel() # try: # if BMConfigParser().get('bitmessagesettings', 'mailchuck') < 1: # myapp.showMigrationWizard(BMConfigParser().get('bitmessagesettings', 'mailchuck')) # except: # myapp.showMigrationWizard(0) - + # only show after wizards and connect dialogs have completed if not BMConfigParser().getboolean('bitmessagesettings', 'startintray'): myapp.show() - sys.exit(app.exec_()) + app.exec_() diff --git a/src/bitmessageqt/address_dialogs.py b/src/bitmessageqt/address_dialogs.py index 60c10369..56cf7cc5 100644 --- a/src/bitmessageqt/address_dialogs.py +++ b/src/bitmessageqt/address_dialogs.py @@ -1,9 +1,7 @@ """ -src/bitmessageqt/address_dialogs.py -=================================== - +Dialogs that work with BM address. """ -# pylint: disable=attribute-defined-outside-init +# pylint: disable=attribute-defined-outside-init,too-few-public-methods,relative-import import hashlib @@ -14,13 +12,11 @@ import widgets from account import AccountMixin, GatewayAccount, MailchuckAccount, accountClass, getSortedAccounts from addresses import addBMIfNotPresent, decodeAddress, encodeVarint from inventory import Inventory -from retranslateui import RetranslateMixin from tr import _translate class AddressCheckMixin(object): """Base address validation class for QT UI""" - # pylint: disable=too-few-public-methods def __init__(self): self.valid = False @@ -33,7 +29,9 @@ class AddressCheckMixin(object): pass def addressChanged(self, QString): - """Address validation callback, performs validation and gives feedback""" + """ + Address validation callback, performs validation and gives feedback + """ status, addressVersion, streamNumber, ripe = decodeAddress( str(QString)) self.valid = status == 'success' @@ -102,8 +100,8 @@ class AddressDataDialog(QtGui.QDialog, AddressCheckMixin): super(AddressDataDialog, self).accept() -class AddAddressDialog(AddressDataDialog, RetranslateMixin): - """QDialog for adding a new address, with validation and translation""" +class AddAddressDialog(AddressDataDialog): + """QDialog for adding a new address""" def __init__(self, parent=None, address=None): super(AddAddressDialog, self).__init__(parent) @@ -113,8 +111,8 @@ class AddAddressDialog(AddressDataDialog, RetranslateMixin): self.lineEditAddress.setText(address) -class NewAddressDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for generating a new address, with translation""" +class NewAddressDialog(QtGui.QDialog): + """QDialog for generating a new address""" def __init__(self, parent=None): super(NewAddressDialog, self).__init__(parent) @@ -175,8 +173,8 @@ class NewAddressDialog(QtGui.QDialog, RetranslateMixin): )) -class NewSubscriptionDialog(AddressDataDialog, RetranslateMixin): - """QDialog for subscribing to an address, with validation and translation""" +class NewSubscriptionDialog(AddressDataDialog): + """QDialog for subscribing to an address""" def __init__(self, parent=None): super(NewSubscriptionDialog, self).__init__(parent) @@ -193,8 +191,8 @@ class NewSubscriptionDialog(AddressDataDialog, RetranslateMixin): else: Inventory().flush() doubleHashOfAddressData = hashlib.sha512(hashlib.sha512( - encodeVarint(addressVersion) + - encodeVarint(streamNumber) + ripe + encodeVarint(addressVersion) + + encodeVarint(streamNumber) + ripe ).digest()).digest() tag = doubleHashOfAddressData[32:] self.recent = Inventory().by_type_and_tag(3, tag) @@ -218,8 +216,8 @@ class NewSubscriptionDialog(AddressDataDialog, RetranslateMixin): )) -class RegenerateAddressesDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for regenerating deterministic addresses, with translation""" +class RegenerateAddressesDialog(QtGui.QDialog): + """QDialog for regenerating deterministic addresses""" def __init__(self, parent=None): super(RegenerateAddressesDialog, self).__init__(parent) widgets.load('regenerateaddresses.ui', self) @@ -227,8 +225,10 @@ class RegenerateAddressesDialog(QtGui.QDialog, RetranslateMixin): QtGui.QWidget.resize(self, QtGui.QWidget.sizeHint(self)) -class SpecialAddressBehaviorDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for special address behaviour (e.g. mailing list functionality), with translation""" +class SpecialAddressBehaviorDialog(QtGui.QDialog): + """ + QDialog for special address behaviour (e.g. mailing list functionality) + """ def __init__(self, parent=None, config=None): super(SpecialAddressBehaviorDialog, self).__init__(parent) @@ -256,11 +256,7 @@ class SpecialAddressBehaviorDialog(QtGui.QDialog, RetranslateMixin): self.radioButtonBehaviorMailingList.click() else: self.radioButtonBehaveNormalAddress.click() - try: - mailingListName = config.get( - self.address, 'mailinglistname') - except: - mailingListName = '' + mailingListName = config.safeGet(self.address, 'mailinglistname', '') self.lineEditMailingListName.setText( unicode(mailingListName, 'utf-8') ) @@ -294,8 +290,8 @@ class SpecialAddressBehaviorDialog(QtGui.QDialog, RetranslateMixin): self.parent.rerenderMessagelistToLabels() -class EmailGatewayDialog(QtGui.QDialog, RetranslateMixin): - """QDialog for email gateway control, with translation""" +class EmailGatewayDialog(QtGui.QDialog): + """QDialog for email gateway control""" def __init__(self, parent, config=None, account=None): super(EmailGatewayDialog, self).__init__(parent) widgets.load('emailgateway.ui', self) diff --git a/src/bitmessageqt/bitmessageui.py b/src/bitmessageqt/bitmessageui.py index 30d054d0..7f2c8c91 100644 --- a/src/bitmessageqt/bitmessageui.py +++ b/src/bitmessageqt/bitmessageui.py @@ -104,6 +104,7 @@ class Ui_MainWindow(object): self.inboxSearchOption.addItem(_fromUtf8("")) self.inboxSearchOption.addItem(_fromUtf8("")) self.inboxSearchOption.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) + self.inboxSearchOption.setCurrentIndex(3) self.horizontalSplitterSearch.addWidget(self.inboxSearchOption) self.horizontalSplitterSearch.handle(1).setEnabled(False) self.horizontalSplitterSearch.setStretchFactor(0, 1) @@ -403,8 +404,8 @@ class Ui_MainWindow(object): self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) - self.inboxSearchOptionSubscriptions.addItem(_fromUtf8("")) self.inboxSearchOptionSubscriptions.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) + self.inboxSearchOptionSubscriptions.setCurrentIndex(2) self.horizontalSplitter_2.addWidget(self.inboxSearchOptionSubscriptions) self.horizontalSplitter_2.handle(1).setEnabled(False) self.horizontalSplitter_2.setStretchFactor(0, 1) @@ -504,6 +505,7 @@ class Ui_MainWindow(object): self.inboxSearchOptionChans.addItem(_fromUtf8("")) self.inboxSearchOptionChans.addItem(_fromUtf8("")) self.inboxSearchOptionChans.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents) + self.inboxSearchOptionChans.setCurrentIndex(3) self.horizontalSplitter_6.addWidget(self.inboxSearchOptionChans) self.horizontalSplitter_6.handle(1).setEnabled(False) self.horizontalSplitter_6.setStretchFactor(0, 1) @@ -719,10 +721,9 @@ class Ui_MainWindow(object): self.pushButtonAddSubscription.setText(_translate("MainWindow", "Add new Subscription", None)) self.inboxSearchLineEditSubscriptions.setPlaceholderText(_translate("MainWindow", "Search", None)) self.inboxSearchOptionSubscriptions.setItemText(0, _translate("MainWindow", "All", None)) - self.inboxSearchOptionSubscriptions.setItemText(1, _translate("MainWindow", "To", None)) - self.inboxSearchOptionSubscriptions.setItemText(2, _translate("MainWindow", "From", None)) - self.inboxSearchOptionSubscriptions.setItemText(3, _translate("MainWindow", "Subject", None)) - self.inboxSearchOptionSubscriptions.setItemText(4, _translate("MainWindow", "Message", None)) + self.inboxSearchOptionSubscriptions.setItemText(1, _translate("MainWindow", "From", None)) + self.inboxSearchOptionSubscriptions.setItemText(2, _translate("MainWindow", "Subject", None)) + self.inboxSearchOptionSubscriptions.setItemText(3, _translate("MainWindow", "Message", None)) self.tableWidgetInboxSubscriptions.setSortingEnabled(True) item = self.tableWidgetInboxSubscriptions.horizontalHeaderItem(0) item.setText(_translate("MainWindow", "To", None)) @@ -770,6 +771,8 @@ class Ui_MainWindow(object): self.actionRegenerateDeterministicAddresses.setText(_translate("MainWindow", "Regenerate deterministic addresses", None)) self.actionDeleteAllTrashedMessages.setText(_translate("MainWindow", "Delete all trashed messages", None)) self.actionJoinChan.setText(_translate("MainWindow", "Join / Create chan", None)) + self.updateNetworkSwitchMenuLabel() + import bitmessage_icons_rc diff --git a/src/bitmessageqt/dialogs.py b/src/bitmessageqt/dialogs.py index c667edb1..10701500 100644 --- a/src/bitmessageqt/dialogs.py +++ b/src/bitmessageqt/dialogs.py @@ -13,7 +13,6 @@ from address_dialogs import ( SpecialAddressBehaviorDialog ) from newchandialog import NewChanDialog -from retranslateui import RetranslateMixin from settings import SettingsDialog from tr import _translate from version import softwareVersion @@ -27,7 +26,7 @@ __all__ = [ ] -class AboutDialog(QtGui.QDialog, RetranslateMixin): +class AboutDialog(QtGui.QDialog): """The `About` dialog""" def __init__(self, parent=None): super(AboutDialog, self).__init__(parent) @@ -55,7 +54,7 @@ class AboutDialog(QtGui.QDialog, RetranslateMixin): self.setFixedSize(QtGui.QWidget.sizeHint(self)) -class IconGlossaryDialog(QtGui.QDialog, RetranslateMixin): +class IconGlossaryDialog(QtGui.QDialog): """The `Icon Glossary` dialog, explaining the status icon colors""" def __init__(self, parent=None, config=None): super(IconGlossaryDialog, self).__init__(parent) @@ -71,7 +70,7 @@ class IconGlossaryDialog(QtGui.QDialog, RetranslateMixin): self.setFixedSize(QtGui.QWidget.sizeHint(self)) -class HelpDialog(QtGui.QDialog, RetranslateMixin): +class HelpDialog(QtGui.QDialog): """The `Help` dialog""" def __init__(self, parent=None): super(HelpDialog, self).__init__(parent) @@ -79,7 +78,7 @@ class HelpDialog(QtGui.QDialog, RetranslateMixin): self.setFixedSize(QtGui.QWidget.sizeHint(self)) -class ConnectDialog(QtGui.QDialog, RetranslateMixin): +class ConnectDialog(QtGui.QDialog): """The `Connect` dialog""" def __init__(self, parent=None): super(ConnectDialog, self).__init__(parent) diff --git a/src/bitmessageqt/foldertree.py b/src/bitmessageqt/foldertree.py index 2e7e735f..e6d64427 100644 --- a/src/bitmessageqt/foldertree.py +++ b/src/bitmessageqt/foldertree.py @@ -1,8 +1,8 @@ """ -src/bitmessageqt/foldertree.py -============================== +Folder tree and messagelist widgets definitions. """ -# pylint: disable=too-many-arguments,bad-super-call,attribute-defined-outside-init +# pylint: disable=too-many-arguments,bad-super-call +# pylint: disable=attribute-defined-outside-init from cgi import escape @@ -20,6 +20,8 @@ _translate("MainWindow", "new") _translate("MainWindow", "sent") _translate("MainWindow", "trash") +TimestampRole = QtCore.Qt.UserRole + 1 + class AccountMixin(object): """UI-related functionality for accounts""" @@ -334,13 +336,14 @@ class Ui_SubscriptionWidget(Ui_AddressWidget): class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin): """A common abstract class for Table widget item""" - def __init__(self, parent=None, label=None, unread=False): + def __init__(self, label=None, unread=False): super(QtGui.QTableWidgetItem, self).__init__() self.setLabel(label) self.setUnread(unread) self._setup() - if parent is not None: - parent.append(self) + + def _setup(self): + self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) def setLabel(self, label): """Set object label""" @@ -353,7 +356,7 @@ class BMTableWidgetItem(QtGui.QTableWidgetItem, SettingsMixin): def data(self, role): """Return object data (QT UI)""" if role in ( - QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole + QtCore.Qt.DisplayRole, QtCore.Qt.EditRole, QtCore.Qt.ToolTipRole ): return self.label elif role == QtCore.Qt.FontRole: @@ -367,7 +370,9 @@ class BMAddressWidget(BMTableWidgetItem, AccountMixin): """A common class for Table widget item with account""" def _setup(self): + super(BMAddressWidget, self)._setup() self.setEnabled(True) + self.setType() def _getLabel(self): return self.label @@ -387,14 +392,9 @@ class BMAddressWidget(BMTableWidgetItem, AccountMixin): class MessageList_AddressWidget(BMAddressWidget): """Address item in a messagelist""" - def __init__(self, parent, address=None, label=None, unread=False): + def __init__(self, address=None, label=None, unread=False): self.setAddress(address) - super(MessageList_AddressWidget, self).__init__(parent, label, unread) - - def _setup(self): - self.isEnabled = True - self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) - self.setType() + super(MessageList_AddressWidget, self).__init__(label, unread) def setLabel(self, label=None): """Set label""" @@ -443,12 +443,9 @@ class MessageList_AddressWidget(BMAddressWidget): class MessageList_SubjectWidget(BMTableWidgetItem): """Message list subject item""" - def __init__(self, parent, subject=None, label=None, unread=False): + def __init__(self, subject=None, label=None, unread=False): self.setSubject(subject) - super(MessageList_SubjectWidget, self).__init__(parent, label, unread) - - def _setup(self): - self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) + super(MessageList_SubjectWidget, self).__init__(label, unread) def setSubject(self, subject): """Set subject""" @@ -469,6 +466,37 @@ class MessageList_SubjectWidget(BMTableWidgetItem): return super(QtGui.QTableWidgetItem, self).__lt__(other) +# 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 + + class Ui_AddressBookWidgetItem(BMAddressWidget): """Addressbook item""" # pylint: disable=unused-argument @@ -518,8 +546,8 @@ class Ui_AddressBookWidgetItem(BMAddressWidget): class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): """Addressbook label item""" def __init__(self, address, label, acc_type): - super(Ui_AddressBookWidgetItemLabel, self).__init__(label, acc_type) self.address = address + super(Ui_AddressBookWidgetItemLabel, self).__init__(label, acc_type) def data(self, role): """Return object data""" @@ -530,9 +558,8 @@ class Ui_AddressBookWidgetItemLabel(Ui_AddressBookWidgetItem): class Ui_AddressBookWidgetItemAddress(Ui_AddressBookWidgetItem): """Addressbook address item""" def __init__(self, address, label, acc_type): - super(Ui_AddressBookWidgetItemAddress, self).__init__(address, acc_type) self.address = address - self.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) + super(Ui_AddressBookWidgetItemAddress, self).__init__(address, acc_type) def data(self, role): """Return object data""" diff --git a/src/bitmessageqt/languagebox.py b/src/bitmessageqt/languagebox.py index 9032cc42..9ff78990 100644 --- a/src/bitmessageqt/languagebox.py +++ b/src/bitmessageqt/languagebox.py @@ -1,32 +1,45 @@ +"""Language Box Module for Locale Settings""" +# pylint: disable=too-few-public-methods,bad-continuation import glob import os + from PyQt4 import QtCore, QtGui -from bmconfigparser import BMConfigParser import paths +from bmconfigparser import BMConfigParser + class LanguageBox(QtGui.QComboBox): - languageName = {"system": "System Settings", "eo": "Esperanto", "en_pirate": "Pirate English"} - def __init__(self, parent = None): + """LanguageBox class for Qt UI""" + languageName = { + "system": "System Settings", "eo": "Esperanto", + "en_pirate": "Pirate English" + } + + def __init__(self, parent=None): super(QtGui.QComboBox, self).__init__(parent) self.populate() def populate(self): + """Populates drop down list with all available languages.""" self.clear() - localesPath = os.path.join (paths.codePath(), 'translations') - self.addItem(QtGui.QApplication.translate("settingsDialog", "System Settings", "system"), "system") + localesPath = os.path.join(paths.codePath(), 'translations') + self.addItem(QtGui.QApplication.translate( + "settingsDialog", "System Settings", "system"), "system") self.setCurrentIndex(0) self.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically) - for translationFile in sorted(glob.glob(os.path.join(localesPath, "bitmessage_*.qm"))): - localeShort = os.path.split(translationFile)[1].split("_", 1)[1][:-3] - locale = QtCore.QLocale(QtCore.QString(localeShort)) - + for translationFile in sorted( + glob.glob(os.path.join(localesPath, "bitmessage_*.qm")) + ): + localeShort = \ + os.path.split(translationFile)[1].split("_", 1)[1][:-3] if localeShort in LanguageBox.languageName: - self.addItem(LanguageBox.languageName[localeShort], localeShort) - elif locale.nativeLanguageName() == "": - self.addItem(localeShort, localeShort) + self.addItem( + LanguageBox.languageName[localeShort], localeShort) else: - self.addItem(locale.nativeLanguageName(), localeShort) + locale = QtCore.QLocale(localeShort) + self.addItem( + locale.nativeLanguageName() or localeShort, localeShort) configuredLocale = BMConfigParser().safeGet( 'bitmessagesettings', 'userlocale', "system") diff --git a/src/bitmessageqt/networkstatus.py b/src/bitmessageqt/networkstatus.py index 5f014563..a99ef451 100644 --- a/src/bitmessageqt/networkstatus.py +++ b/src/bitmessageqt/networkstatus.py @@ -1,7 +1,5 @@ """ -src/bitmessageqt/networkstatus.py -================================= - +Network status tab widget definition. """ import time @@ -11,7 +9,7 @@ from PyQt4 import QtCore, QtGui import knownnodes import l10n import network.stats -import shared +import state import widgets from inventory import Inventory from network.connectionpool import BMConnectionPool @@ -34,8 +32,6 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): header.setSortIndicator(0, QtCore.Qt.AscendingOrder) self.startup = time.localtime() - self.labelStartupTime.setText(_translate("networkstatus", "Since startup on %1").arg( - l10n.formatTimestamp(self.startup))) self.UISignalThread = UISignaler.get() # pylint: disable=no-member @@ -96,8 +92,8 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Object(s) to be synced: %n", None, QtCore.QCoreApplication.CodecForTr, - network.stats.pendingDownload() + - network.stats.pendingUpload())) + network.stats.pendingDownload() + + network.stats.pendingUpload())) def updateNumberOfMessagesProcessed(self): """Update the counter for number of processed messages""" @@ -108,7 +104,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Processed %n person-to-person message(s).", None, QtCore.QCoreApplication.CodecForTr, - shared.numberOfMessagesProcessed)) + state.numberOfMessagesProcessed)) def updateNumberOfBroadcastsProcessed(self): """Update the counter for the number of processed broadcasts""" @@ -119,7 +115,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Processed %n broadcast message(s).", None, QtCore.QCoreApplication.CodecForTr, - shared.numberOfBroadcastsProcessed)) + state.numberOfBroadcastsProcessed)) def updateNumberOfPubkeysProcessed(self): """Update the counter for the number of processed pubkeys""" @@ -130,7 +126,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): "Processed %n public key(s).", None, QtCore.QCoreApplication.CodecForTr, - shared.numberOfPubkeysProcessed)) + state.numberOfPubkeysProcessed)) def updateNumberOfBytes(self): """ @@ -207,7 +203,7 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): self.tableWidgetConnectionCount.item(0, 0).setData(QtCore.Qt.UserRole, destination) self.tableWidgetConnectionCount.item(0, 1).setData(QtCore.Qt.UserRole, outbound) else: - if len(BMConnectionPool().inboundConnections) == 0: + if not BMConnectionPool().inboundConnections: self.window().setStatusIcon('yellow') for i in range(self.tableWidgetConnectionCount.rowCount()): if self.tableWidgetConnectionCount.item(i, 0).data(QtCore.Qt.UserRole).toPyObject() != destination: @@ -225,9 +221,9 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): # FYI: The 'singlelistener' thread sets the icon color to green when it # receives an incoming connection, meaning that the user's firewall is # configured correctly. - if self.tableWidgetConnectionCount.rowCount() and shared.statusIconColor == 'red': + if self.tableWidgetConnectionCount.rowCount() and state.statusIconColor == 'red': self.window().setStatusIcon('yellow') - elif self.tableWidgetConnectionCount.rowCount() == 0 and shared.statusIconColor != "red": + elif self.tableWidgetConnectionCount.rowCount() == 0 and state.statusIconColor != "red": self.window().setStatusIcon('red') # timer driven @@ -240,6 +236,15 @@ class NetworkStatus(QtGui.QWidget, RetranslateMixin): self.updateNumberOfObjectsToBeSynced() def retranslateUi(self): + """Conventional Qt Designer method for dynamic l10n""" super(NetworkStatus, self).retranslateUi() - self.labelStartupTime.setText(_translate("networkstatus", "Since startup on %1").arg( - l10n.formatTimestamp(self.startup))) + self.labelTotalConnections.setText( + _translate( + "networkstatus", "Total Connections: %1").arg( + str(self.tableWidgetConnectionCount.rowCount()))) + self.labelStartupTime.setText(_translate( + "networkstatus", "Since startup on %1" + ).arg(l10n.formatTimestamp(self.startup))) + self.updateNumberOfMessagesProcessed() + self.updateNumberOfBroadcastsProcessed() + self.updateNumberOfPubkeysProcessed() diff --git a/src/bitmessageqt/newchandialog.py b/src/bitmessageqt/newchandialog.py index 8db486c1..c0629cd7 100644 --- a/src/bitmessageqt/newchandialog.py +++ b/src/bitmessageqt/newchandialog.py @@ -9,13 +9,13 @@ from PyQt4 import QtCore, QtGui import widgets from addresses import addBMIfNotPresent from addressvalidator import AddressValidator, PassPhraseValidator -from queues import UISignalQueue, addressGeneratorQueue, apiAddressGeneratorReturnQueue -from retranslateui import RetranslateMixin +from queues import ( + addressGeneratorQueue, apiAddressGeneratorReturnQueue, UISignalQueue) from tr import _translate from utils import str_chan -class NewChanDialog(QtGui.QDialog, RetranslateMixin): +class NewChanDialog(QtGui.QDialog): """The `New Chan` dialog""" def __init__(self, parent=None): super(NewChanDialog, self).__init__(parent) diff --git a/src/bitmessageqt/settings.py b/src/bitmessageqt/settings.py index 011d38ed..8e4bf198 100644 --- a/src/bitmessageqt/settings.py +++ b/src/bitmessageqt/settings.py @@ -1,3 +1,6 @@ +""" +This module setting file is for settings +""" import ConfigParser import os import sys @@ -11,7 +14,6 @@ import namecoin import openclpow import paths import queues -import shared import state import tempfile import widgets @@ -111,7 +113,7 @@ class SettingsDialog(QtGui.QDialog): tempfile.NamedTemporaryFile( dir=paths.lookupExeFolder(), delete=True ).close() # should autodelete - except: + except Exception: self.checkBoxPortableMode.setDisabled(True) if 'darwin' in sys.platform: @@ -338,7 +340,7 @@ class SettingsDialog(QtGui.QDialog): proxytype_index = self.comboBoxProxyType.currentIndex() if proxytype_index == 0: - if self._proxy_type and shared.statusIconColor != 'red': + if self._proxy_type and state.statusIconColor != 'red': self.net_restart_needed = True elif self.comboBoxProxyType.currentText() != self._proxy_type: self.net_restart_needed = True @@ -408,14 +410,14 @@ class SettingsDialog(QtGui.QDialog): self.config.set( 'bitmessagesettings', 'defaultnoncetrialsperbyte', str(int( - float(self.lineEditTotalDifficulty.text()) * - defaults.networkDefaultProofOfWorkNonceTrialsPerByte))) + float(self.lineEditTotalDifficulty.text()) + * defaults.networkDefaultProofOfWorkNonceTrialsPerByte))) if float(self.lineEditSmallMessageDifficulty.text()) >= 1: self.config.set( 'bitmessagesettings', 'defaultpayloadlengthextrabytes', str(int( - float(self.lineEditSmallMessageDifficulty.text()) * - defaults.networkDefaultPayloadLengthExtraBytes))) + float(self.lineEditSmallMessageDifficulty.text()) + * defaults.networkDefaultPayloadLengthExtraBytes))) if self.comboBoxOpenCL.currentText().toUtf8() != self.config.safeGet( 'bitmessagesettings', 'opencl'): @@ -427,40 +429,40 @@ class SettingsDialog(QtGui.QDialog): acceptableDifficultyChanged = False if ( - float(self.lineEditMaxAcceptableTotalDifficulty.text()) >= 1 or - float(self.lineEditMaxAcceptableTotalDifficulty.text()) == 0 + float(self.lineEditMaxAcceptableTotalDifficulty.text()) >= 1 + or float(self.lineEditMaxAcceptableTotalDifficulty.text()) == 0 ): if self.config.get( 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte' ) != str(int( - float(self.lineEditMaxAcceptableTotalDifficulty.text()) * - defaults.networkDefaultProofOfWorkNonceTrialsPerByte) + float(self.lineEditMaxAcceptableTotalDifficulty.text()) + * defaults.networkDefaultProofOfWorkNonceTrialsPerByte) ): # the user changed the max acceptable total difficulty acceptableDifficultyChanged = True self.config.set( 'bitmessagesettings', 'maxacceptablenoncetrialsperbyte', str(int( - float(self.lineEditMaxAcceptableTotalDifficulty.text()) * - defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) + float(self.lineEditMaxAcceptableTotalDifficulty.text()) + * defaults.networkDefaultProofOfWorkNonceTrialsPerByte)) ) if ( - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) >= 1 or - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) == 0 + float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) >= 1 + or float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) == 0 ): if self.config.get( 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes' ) != str(int( - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) * - defaults.networkDefaultPayloadLengthExtraBytes) + float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) + * defaults.networkDefaultPayloadLengthExtraBytes) ): # the user changed the max acceptable small message difficulty acceptableDifficultyChanged = True self.config.set( 'bitmessagesettings', 'maxacceptablepayloadlengthextrabytes', str(int( - float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) * - defaults.networkDefaultPayloadLengthExtraBytes)) + float(self.lineEditMaxAcceptableSmallMessageDifficulty.text()) + * defaults.networkDefaultPayloadLengthExtraBytes)) ) if acceptableDifficultyChanged: # It might now be possible to send msgs which were previously @@ -473,6 +475,8 @@ class SettingsDialog(QtGui.QDialog): " WHERE status='toodifficult'") queues.workerQueue.put(('sendmessage', '')) + stopResendingDefaults = False + # UI setting to stop trying to send messages after X days/months # I'm open to changing this UI to something else if someone has a better idea. if self.lineEditDays.text() == '' and self.lineEditMonths.text() == '': @@ -480,7 +484,8 @@ class SettingsDialog(QtGui.QDialog): # default behavior. The input is blank/blank self.config.set('bitmessagesettings', 'stopresendingafterxdays', '') self.config.set('bitmessagesettings', 'stopresendingafterxmonths', '') - shared.maximumLengthOfTimeToBotherResendingMessages = float('inf') + state.maximumLengthOfTimeToBotherResendingMessages = float('inf') + stopResendingDefaults = True try: days = float(self.lineEditDays.text()) @@ -493,10 +498,10 @@ class SettingsDialog(QtGui.QDialog): self.lineEditMonths.setText("0") months = 0.0 - if days >= 0 and months >= 0: - shared.maximumLengthOfTimeToBotherResendingMessages = \ + if days >= 0 and months >= 0 and not stopResendingDefaults: + state.maximumLengthOfTimeToBotherResendingMessages = \ days * 24 * 60 * 60 + months * 60 * 60 * 24 * 365 / 12 - if shared.maximumLengthOfTimeToBotherResendingMessages < 432000: + if state.maximumLengthOfTimeToBotherResendingMessages < 432000: # If the time period is less than 5 hours, we give # zero values to all fields. No message will be sent again. QtGui.QMessageBox.about( @@ -513,7 +518,7 @@ class SettingsDialog(QtGui.QDialog): 'bitmessagesettings', 'stopresendingafterxdays', '0') self.config.set( 'bitmessagesettings', 'stopresendingafterxmonths', '0') - shared.maximumLengthOfTimeToBotherResendingMessages = 0.0 + state.maximumLengthOfTimeToBotherResendingMessages = 0.0 else: self.config.set( 'bitmessagesettings', 'stopresendingafterxdays', str(days)) @@ -535,8 +540,8 @@ class SettingsDialog(QtGui.QDialog): self.parent.updateStartOnLogon() if ( - state.appdata != paths.lookupExeFolder() and - self.checkBoxPortableMode.isChecked() + state.appdata != paths.lookupExeFolder() + and self.checkBoxPortableMode.isChecked() ): # If we are NOT using portable mode now but the user selected # that we should... @@ -554,12 +559,12 @@ class SettingsDialog(QtGui.QDialog): try: os.remove(previousAppdataLocation + 'debug.log') os.remove(previousAppdataLocation + 'debug.log.1') - except: + except Exception: pass if ( - state.appdata == paths.lookupExeFolder() and - not self.checkBoxPortableMode.isChecked() + state.appdata == paths.lookupExeFolder() + and not self.checkBoxPortableMode.isChecked() ): # If we ARE using portable mode now but the user selected # that we shouldn't... @@ -577,5 +582,5 @@ class SettingsDialog(QtGui.QDialog): try: os.remove(paths.lookupExeFolder() + 'debug.log') os.remove(paths.lookupExeFolder() + 'debug.log.1') - except: + except Exception: pass diff --git a/src/bitmessageqt/support.py b/src/bitmessageqt/support.py index d6d4543d..4970beba 100644 --- a/src/bitmessageqt/support.py +++ b/src/bitmessageqt/support.py @@ -1,33 +1,43 @@ +"""Composing support request message functions.""" +# pylint: disable=no-member + import ctypes -from PyQt4 import QtCore, QtGui import ssl import sys import time +from PyQt4 import QtCore + import account -from bmconfigparser import BMConfigParser -from debug import logger import defaults -from foldertree import AccountMixin -from helper_sql import * -from l10n import getTranslationLanguage -from openclpow import openclAvailable, openclEnabled +import network.stats import paths import proofofwork +import queues +import state +from bmconfigparser import BMConfigParser +from foldertree import AccountMixin +from helper_sql import sqlExecute, sqlQuery +from l10n import getTranslationLanguage +from openclpow import openclEnabled from pyelliptic.openssl import OpenSSL from settings import getSOCKSProxyType -import queues -import network.stats -import state from version import softwareVersion +from tr import _translate + # this is BM support address going to Peter Surda OLD_SUPPORT_ADDRESS = 'BM-2cTkCtMYkrSPwFTpgcBrMrf5d8oZwvMZWK' SUPPORT_ADDRESS = 'BM-2cUdgkDDAahwPAU6oD2A7DnjqZz3hgY832' -SUPPORT_LABEL = 'PyBitmessage support' -SUPPORT_MY_LABEL = 'My new address' +SUPPORT_LABEL = _translate("Support", "PyBitmessage support") +SUPPORT_MY_LABEL = _translate("Support", "My new address") SUPPORT_SUBJECT = 'Support request' -SUPPORT_MESSAGE = '''You can use this message to send a report to one of the PyBitmessage core developers regarding PyBitmessage or the mailchuck.com email service. If you are using PyBitmessage involuntarily, for example because your computer was infected with ransomware, this is not an appropriate venue for resolving such issues. +SUPPORT_MESSAGE = _translate("Support", ''' +You can use this message to send a report to one of the PyBitmessage core \ +developers regarding PyBitmessage or the mailchuck.com email service. \ +If you are using PyBitmessage involuntarily, for example because \ +your computer was infected with ransomware, this is not an appropriate venue \ +for resolving such issues. Please describe what you are trying to do: @@ -37,7 +47,8 @@ Please describe what happens instead: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Please write above this line and if possible, keep the information about your environment below intact. +Please write above this line and if possible, keep the information about your \ +environment below intact. PyBitmessage version: {} Operating system: {} @@ -52,15 +63,19 @@ Locale: {} SOCKS: {} UPnP: {} Connected hosts: {} -''' +''') + def checkAddressBook(myapp): - sqlExecute('''DELETE from addressbook WHERE address=?''', OLD_SUPPORT_ADDRESS) - queryreturn = sqlQuery('''SELECT * FROM addressbook WHERE address=?''', SUPPORT_ADDRESS) + sqlExecute('DELETE from addressbook WHERE address=?', OLD_SUPPORT_ADDRESS) + queryreturn = sqlQuery('SELECT * FROM addressbook WHERE address=?', SUPPORT_ADDRESS) if queryreturn == []: - sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', str(QtGui.QApplication.translate("Support", SUPPORT_LABEL)), SUPPORT_ADDRESS) + sqlExecute( + 'INSERT INTO addressbook VALUES (?,?)', + SUPPORT_LABEL.toUtf8(), SUPPORT_ADDRESS) myapp.rerenderAddressBook() + def checkHasNormalAddress(): for address in account.getSortedAccounts(): acct = account.accountClass(address) @@ -68,23 +83,33 @@ def checkHasNormalAddress(): return address return False + def createAddressIfNeeded(myapp): if not checkHasNormalAddress(): - queues.addressGeneratorQueue.put(('createRandomAddress', 4, 1, str(QtGui.QApplication.translate("Support", SUPPORT_MY_LABEL)), 1, "", False, defaults.networkDefaultProofOfWorkNonceTrialsPerByte, defaults.networkDefaultPayloadLengthExtraBytes)) + queues.addressGeneratorQueue.put(( + 'createRandomAddress', 4, 1, + SUPPORT_MY_LABEL.toUtf8(), + 1, "", False, + defaults.networkDefaultProofOfWorkNonceTrialsPerByte, + defaults.networkDefaultPayloadLengthExtraBytes + )) while state.shutdown == 0 and not checkHasNormalAddress(): time.sleep(.2) myapp.rerenderComboBoxSendFrom() return checkHasNormalAddress() + def createSupportMessage(myapp): checkAddressBook(myapp) address = createAddressIfNeeded(myapp) if state.shutdown: return - myapp.ui.lineEditSubject.setText(str(QtGui.QApplication.translate("Support", SUPPORT_SUBJECT))) - addrIndex = myapp.ui.comboBoxSendFrom.findData(address, QtCore.Qt.UserRole, QtCore.Qt.MatchFixedString | QtCore.Qt.MatchCaseSensitive) - if addrIndex == -1: # something is very wrong + myapp.ui.lineEditSubject.setText(SUPPORT_SUBJECT) + addrIndex = myapp.ui.comboBoxSendFrom.findData( + address, QtCore.Qt.UserRole, + QtCore.Qt.MatchFixedString | QtCore.Qt.MatchCaseSensitive) + if addrIndex == -1: # something is very wrong return myapp.ui.comboBoxSendFrom.setCurrentIndex(addrIndex) myapp.ui.lineEditTo.setText(SUPPORT_ADDRESS) @@ -107,8 +132,9 @@ def createSupportMessage(myapp): pass architecture = "32" if ctypes.sizeof(ctypes.c_voidp) == 4 else "64" pythonversion = sys.version - - opensslversion = "%s (Python internal), %s (external for PyElliptic)" % (ssl.OPENSSL_VERSION, OpenSSL._version) + + opensslversion = "%s (Python internal), %s (external for PyElliptic)" % ( + ssl.OPENSSL_VERSION, OpenSSL._version) frozen = "N/A" if paths.frozen: @@ -123,7 +149,9 @@ def createSupportMessage(myapp): upnp = BMConfigParser().safeGet('bitmessagesettings', 'upnp', "N/A") connectedhosts = len(network.stats.connectedHostsList()) - myapp.ui.textEditMessage.setText(str(QtGui.QApplication.translate("Support", SUPPORT_MESSAGE)).format(version, os, architecture, pythonversion, opensslversion, frozen, portablemode, cpow, openclpow, locale, socks, upnp, connectedhosts)) + myapp.ui.textEditMessage.setText(unicode(SUPPORT_MESSAGE, 'utf-8').format( + version, os, architecture, pythonversion, opensslversion, frozen, + portablemode, cpow, openclpow, locale, socks, upnp, connectedhosts)) # single msg tab myapp.ui.tabWidgetSend.setCurrentIndex( diff --git a/src/bitmessageqt/utils.py b/src/bitmessageqt/utils.py index 564dbc8b..e118f487 100644 --- a/src/bitmessageqt/utils.py +++ b/src/bitmessageqt/utils.py @@ -1,13 +1,16 @@ -from PyQt4 import QtGui import hashlib import os + +from PyQt4 import QtGui + +import state from addresses import addBMIfNotPresent from bmconfigparser import BMConfigParser -import state str_broadcast_subscribers = '[Broadcast subscribers]' str_chan = '[chan]' + def identiconize(address): size = 48 @@ -28,32 +31,40 @@ def identiconize(address): # the identicons to decrease the risk of attacks where someone creates # an address to mimic someone else's identicon. identiconsuffix = BMConfigParser().get('bitmessagesettings', 'identiconsuffix') - if (identicon_lib[:len('qidenticon')] == 'qidenticon'): - # print identicon_lib + if identicon_lib[:len('qidenticon')] == 'qidenticon': # originally by: # :Author:Shin Adachi # Licesensed under FreeBSD License. # stripped from PIL and uses QT instead (by sendiulo, same license) import qidenticon - hash = hashlib.md5(addBMIfNotPresent(address)+identiconsuffix).hexdigest() - use_two_colors = (identicon_lib[:len('qidenticon_two')] == 'qidenticon_two') - opacity = int(not((identicon_lib == 'qidenticon_x') | (identicon_lib == 'qidenticon_two_x') | (identicon_lib == 'qidenticon_b') | (identicon_lib == 'qidenticon_two_b')))*255 + icon_hash = hashlib.md5( + addBMIfNotPresent(address) + identiconsuffix).hexdigest() + use_two_colors = identicon_lib[:len('qidenticon_two')] == 'qidenticon_two' + opacity = int( + identicon_lib not in ( + 'qidenticon_x', 'qidenticon_two_x', + 'qidenticon_b', 'qidenticon_two_b' + )) * 255 penwidth = 0 - image = qidenticon.render_identicon(int(hash, 16), size, use_two_colors, opacity, penwidth) + image = qidenticon.render_identicon( + int(icon_hash, 16), size, use_two_colors, opacity, penwidth) # filename = './images/identicons/'+hash+'.png' # image.save(filename) idcon = QtGui.QIcon() idcon.addPixmap(image, QtGui.QIcon.Normal, QtGui.QIcon.Off) return idcon elif identicon_lib == 'pydenticon': - # print identicon_lib - # Here you could load pydenticon.py (just put it in the "src" folder of your Bitmessage source) + # Here you could load pydenticon.py + # (just put it in the "src" folder of your Bitmessage source) from pydenticon import Pydenticon # It is not included in the source, because it is licensed under GPLv3 # GPLv3 is a copyleft license that would influence our licensing - # Find the source here: http://boottunes.googlecode.com/svn-history/r302/trunk/src/pydenticon.py - # note that it requires PIL to be installed: http://www.pythonware.com/products/pil/ - idcon_render = Pydenticon(addBMIfNotPresent(address)+identiconsuffix, size*3) + # Find the source here: + # https://github.com/azaghal/pydenticon + # note that it requires pillow (or PIL) to be installed: + # https://python-pillow.org/ + idcon_render = Pydenticon( + addBMIfNotPresent(address) + identiconsuffix, size * 3) rendering = idcon_render._render() data = rendering.convert("RGBA").tostring("raw", "RGBA") qim = QtGui.QImage(data, size, size, QtGui.QImage.Format_ARGB32) @@ -62,32 +73,31 @@ def identiconize(address): idcon.addPixmap(pix, QtGui.QIcon.Normal, QtGui.QIcon.Off) return idcon + def avatarize(address): """ - loads a supported image for the given address' hash form 'avatars' folder - falls back to default avatar if 'default.*' file exists - falls back to identiconize(address) + Loads a supported image for the given address' hash form 'avatars' folder + falls back to default avatar if 'default.*' file exists + falls back to identiconize(address) """ idcon = QtGui.QIcon() - hash = hashlib.md5(addBMIfNotPresent(address)).hexdigest() - str_broadcast_subscribers = '[Broadcast subscribers]' + icon_hash = hashlib.md5(addBMIfNotPresent(address)).hexdigest() if address == str_broadcast_subscribers: # don't hash [Broadcast subscribers] - hash = address - # http://pyqt.sourceforge.net/Docs/PyQt4/qimagereader.html#supportedImageFormats - # print QImageReader.supportedImageFormats () + icon_hash = address + # https://www.riverbankcomputing.com/static/Docs/PyQt4/qimagereader.html#supportedImageFormats # QImageReader.supportedImageFormats () - extensions = ['PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', 'TIFF', 'XBM', 'XPM', 'TGA'] + extensions = [ + 'PNG', 'GIF', 'JPG', 'JPEG', 'SVG', 'BMP', 'MNG', 'PBM', 'PGM', 'PPM', + 'TIFF', 'XBM', 'XPM', 'TGA'] # try to find a specific avatar for ext in extensions: - lower_hash = state.appdata + 'avatars/' + hash + '.' + ext.lower() - upper_hash = state.appdata + 'avatars/' + hash + '.' + ext.upper() + lower_hash = state.appdata + 'avatars/' + icon_hash + '.' + ext.lower() + upper_hash = state.appdata + 'avatars/' + icon_hash + '.' + ext.upper() if os.path.isfile(lower_hash): - # print 'found avatar of ', address idcon.addFile(lower_hash) return idcon elif os.path.isfile(upper_hash): - # print 'found avatar of ', address idcon.addFile(upper_hash) return idcon # if we haven't found any, try to find a default avatar diff --git a/src/class_objectProcessor.py b/src/class_objectProcessor.py index e91add6e..ea98d457 100644 --- a/src/class_objectProcessor.py +++ b/src/class_objectProcessor.py @@ -138,9 +138,9 @@ class objectProcessor(threading.Thread): # bypass nonce and time, retain object type/version/stream + body readPosition = 16 - if bytes(data[readPosition:]) in shared.ackdataForWhichImWatching: + if bytes(data[readPosition:]) in state.ackdataForWhichImWatching: logger.info('This object is an acknowledgement bound for me.') - del shared.ackdataForWhichImWatching[bytes(data[readPosition:])] + del state.ackdataForWhichImWatching[bytes(data[readPosition:])] sqlExecute( 'UPDATE sent SET status=?, lastactiontime=?' ' WHERE ackdata=?', @@ -284,7 +284,7 @@ class objectProcessor(threading.Thread): def processpubkey(self, data): """Process a pubkey object""" pubkeyProcessingStartTime = time.time() - shared.numberOfPubkeysProcessed += 1 + state.numberOfPubkeysProcessed += 1 queues.UISignalQueue.put(( 'updateNumberOfPubkeysProcessed', 'no data')) readPosition = 20 # bypass the nonce, time, and object type @@ -457,7 +457,7 @@ class objectProcessor(threading.Thread): def processmsg(self, data): """Process a message object""" messageProcessingStartTime = time.time() - shared.numberOfMessagesProcessed += 1 + state.numberOfMessagesProcessed += 1 queues.UISignalQueue.put(( 'updateNumberOfMessagesProcessed', 'no data')) readPosition = 20 # bypass the nonce, time, and object type @@ -803,7 +803,7 @@ class objectProcessor(threading.Thread): def processbroadcast(self, data): """Process a broadcast object""" messageProcessingStartTime = time.time() - shared.numberOfBroadcastsProcessed += 1 + state.numberOfBroadcastsProcessed += 1 queues.UISignalQueue.put(( 'updateNumberOfBroadcastsProcessed', 'no data')) inventoryHash = calculateInventoryHash(data) diff --git a/src/class_singleCleaner.py b/src/class_singleCleaner.py index 711d8de2..d162c2c8 100644 --- a/src/class_singleCleaner.py +++ b/src/class_singleCleaner.py @@ -36,6 +36,13 @@ from network.connectionpool import BMConnectionPool from network.threads import StoppableThread +#: Equals 4 weeks. You could make this longer if you want +#: but making it shorter would not be advisable because +#: there is a very small possibility that it could keep you +#: from obtaining a needed pubkey for a period of time. +lengthOfTimeToHoldOnToAllPubkeys = 2419200 + + class singleCleaner(StoppableThread): """The singleCleaner thread class""" name = "singleCleaner" @@ -46,7 +53,7 @@ class singleCleaner(StoppableThread): gc.disable() timeWeLastClearedInventoryAndPubkeysTables = 0 try: - shared.maximumLengthOfTimeToBotherResendingMessages = ( + state.maximumLengthOfTimeToBotherResendingMessages = ( float(BMConfigParser().get( 'bitmessagesettings', 'stopresendingafterxdays')) * 24 * 60 * 60 @@ -58,7 +65,7 @@ class singleCleaner(StoppableThread): # Either the user hasn't set stopresendingafterxdays and # stopresendingafterxmonths yet or the options are missing # from the config file. - shared.maximumLengthOfTimeToBotherResendingMessages = float('inf') + state.maximumLengthOfTimeToBotherResendingMessages = float('inf') # initial wait if state.shutdown == 0: @@ -75,8 +82,8 @@ class singleCleaner(StoppableThread): # If we are running as a daemon then we are going to fill up the UI # queue which will never be handled by a UI. We should clear it to # save memory. - # ..FIXME redundant? - if shared.thisapp.daemon or not state.enableGUI: + # FIXME redundant? + if state.thisapp.daemon or not state.enableGUI: queues.UISignalQueue.queue.clear() if timeWeLastClearedInventoryAndPubkeysTables < \ int(time.time()) - 7380: @@ -86,7 +93,7 @@ class singleCleaner(StoppableThread): # pubkeys sqlExecute( "DELETE FROM pubkeys WHERE time?)", int(time.time()), int(time.time()) - - shared.maximumLengthOfTimeToBotherResendingMessages + - state.maximumLengthOfTimeToBotherResendingMessages ) for row in queryreturn: if len(row) < 2: @@ -133,7 +140,8 @@ class singleCleaner(StoppableThread): ' is full. Bitmessage will now exit.'), True) )) - if shared.thisapp.daemon or not state.enableGUI: + # FIXME redundant? + if state.thisapp.daemon or not state.enableGUI: os._exit(1) # inv/object tracking diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 2c28cf9b..93372629 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -99,25 +99,25 @@ class singleWorker(StoppableThread): hexlify(privEncryptionKey)) ) - # Initialize the shared.ackdataForWhichImWatching data structure + # Initialize the state.ackdataForWhichImWatching data structure queryreturn = sqlQuery( '''SELECT ackdata FROM sent WHERE status = 'msgsent' ''') for row in queryreturn: ackdata, = row self.logger.info('Watching for ackdata %s', hexlify(ackdata)) - shared.ackdataForWhichImWatching[ackdata] = 0 + state.ackdataForWhichImWatching[ackdata] = 0 # Fix legacy (headerless) watched ackdata to include header - for oldack in shared.ackdataForWhichImWatching: + for oldack in state.ackdataForWhichImWatching: if len(oldack) == 32: # attach legacy header, always constant (msg/1/1) newack = '\x00\x00\x00\x02\x01\x01' + oldack - shared.ackdataForWhichImWatching[newack] = 0 + state.ackdataForWhichImWatching[newack] = 0 sqlExecute( 'UPDATE sent SET ackdata=? WHERE ackdata=?', newack, oldack ) - del shared.ackdataForWhichImWatching[oldack] + del state.ackdataForWhichImWatching[oldack] # give some time for the GUI to start # before we start on existing POW tasks. @@ -867,7 +867,7 @@ class singleWorker(StoppableThread): embeddedTime = int(time.time() + TTL) # if we aren't sending this to ourselves or a chan if not BMConfigParser().has_section(toaddress): - shared.ackdataForWhichImWatching[ackdata] = 0 + state.ackdataForWhichImWatching[ackdata] = 0 queues.UISignalQueue.put(( 'updateSentItemStatusByAckdata', ( ackdata, diff --git a/src/download.wget b/src/download.wget new file mode 100644 index 00000000..472b6db3 --- /dev/null +++ b/src/download.wget @@ -0,0 +1,11 @@ +Google



 

Advanced search

© 2020 - Privacy - Terms

\ No newline at end of file diff --git a/src/helper_search.py b/src/helper_search.py index 2384ff01..c572c5b1 100644 --- a/src/helper_search.py +++ b/src/helper_search.py @@ -1,92 +1,115 @@ -"""Additional SQL helper for searching messages""" +""" +Additional SQL helper for searching messages. +Used by :mod:`.bitmessageqt`. +""" -from helper_sql import sqlQuery - -try: - from PyQt4 import QtGui - haveQt = True -except ImportError: - haveQt = False # pylint: disable=too-many-arguments +from helper_sql import sqlQuery +from tr import _translate -def search_translate(context, text): - """Translation wrapper""" - if haveQt: - return QtGui.QApplication.translate(context, text) - return text.lower() +def search_sql( + xAddress='toaddress', account=None, folder='inbox', where=None, + what=None, unreadOnly=False +): + """ + Search for messages from given account and folder having search term + in one of it's fields. - -def search_sql(xAddress="toaddress", account=None, folder="inbox", where=None, what=None, unreadOnly=False): - """Perform a search in mailbox tables""" - # pylint: disable=too-many-branches - if what is not None and what != "": - what = "%" + what + "%" - if where == search_translate("MainWindow", "To"): - where = "toaddress" - elif where == search_translate("MainWindow", "From"): - where = "fromaddress" - elif where == search_translate("MainWindow", "Subject"): - where = "subject" - elif where == search_translate("MainWindow", "Message"): - where = "message" + :param str xAddress: address field checked + ('fromaddress', 'toaddress' or 'both') + :param account: the account which is checked + :type account: :class:`.bitmessageqt.account.BMAccount` + instance + :param str folder: the folder which is checked + :param str where: message field which is checked ('toaddress', + 'fromaddress', 'subject' or 'message'), by default check any field + :param str what: the search term + :param bool unreadOnly: if True, search only for unread messages + :return: all messages where field contains + :rtype: list[list] + """ + # pylint: disable=too-many-arguments, too-many-branches + if what: + what = '%' + what + '%' + if where == _translate("MainWindow", "To"): + where = 'toaddress' + elif where == _translate("MainWindow", "From"): + where = 'fromaddress' + elif where == _translate("MainWindow", "Subject"): + where = 'subject' + elif where == _translate("MainWindow", "Message"): + where = 'message' else: - where = "toaddress || fromaddress || subject || message" - else: - what = None + where = 'toaddress || fromaddress || subject || message' - if folder == "sent": - sqlStatementBase = ''' - SELECT toaddress, fromaddress, subject, status, ackdata, lastactiontime - FROM sent ''' - else: - sqlStatementBase = '''SELECT folder, msgid, toaddress, fromaddress, subject, received, read - FROM inbox ''' + sqlStatementBase = 'SELECT toaddress, fromaddress, subject, ' + ( + 'status, ackdata, lastactiontime FROM sent ' if folder == 'sent' + else 'folder, msgid, received, read FROM inbox ' + ) sqlStatementParts = [] sqlArguments = [] if account is not None: if xAddress == 'both': - sqlStatementParts.append("(fromaddress = ? OR toaddress = ?)") + sqlStatementParts.append('(fromaddress = ? OR toaddress = ?)') sqlArguments.append(account) sqlArguments.append(account) else: - sqlStatementParts.append(xAddress + " = ? ") + sqlStatementParts.append(xAddress + ' = ? ') sqlArguments.append(account) if folder is not None: - if folder == "new": - folder = "inbox" + if folder == 'new': + folder = 'inbox' unreadOnly = True - sqlStatementParts.append("folder = ? ") + sqlStatementParts.append('folder = ? ') sqlArguments.append(folder) else: - sqlStatementParts.append("folder != ?") - sqlArguments.append("trash") - if what is not None: - sqlStatementParts.append("%s LIKE ?" % (where)) + sqlStatementParts.append('folder != ?') + sqlArguments.append('trash') + if what: + sqlStatementParts.append('%s LIKE ?' % (where)) sqlArguments.append(what) if unreadOnly: - sqlStatementParts.append("read = 0") + sqlStatementParts.append('read = 0') if sqlStatementParts: - sqlStatementBase += "WHERE " + " AND ".join(sqlStatementParts) - if folder == "sent": - sqlStatementBase += " ORDER BY lastactiontime" + sqlStatementBase += 'WHERE ' + ' AND '.join(sqlStatementParts) + if folder == 'sent': + sqlStatementBase += ' ORDER BY lastactiontime' return sqlQuery(sqlStatementBase, sqlArguments) -def check_match(toAddress, fromAddress, subject, message, where=None, what=None): - """Check if a single message matches a filter (used when new messages are added to messagelists)""" - if what is not None and what != "": - if where in (search_translate("MainWindow", "To"), search_translate("MainWindow", "All")): - if what.lower() not in toAddress.lower(): - return False - elif where in (search_translate("MainWindow", "From"), search_translate("MainWindow", "All")): - if what.lower() not in fromAddress.lower(): - return False - elif where in (search_translate("MainWindow", "Subject"), search_translate("MainWindow", "All")): - if what.lower() not in subject.lower(): - return False - elif where in (search_translate("MainWindow", "Message"), search_translate("MainWindow", "All")): - if what.lower() not in message.lower(): - return False + +def check_match( + toAddress, fromAddress, subject, message, where=None, what=None): + """ + Check if a single message matches a filter (used when new messages + are added to messagelists) + """ + # pylint: disable=too-many-arguments + if not what: + return True + + if where in ( + _translate("MainWindow", "To"), _translate("MainWindow", "All") + ): + if what.lower() not in toAddress.lower(): + return False + elif where in ( + _translate("MainWindow", "From"), _translate("MainWindow", "All") + ): + if what.lower() not in fromAddress.lower(): + return False + elif where in ( + _translate("MainWindow", "Subject"), + _translate("MainWindow", "All") + ): + if what.lower() not in subject.lower(): + return False + elif where in ( + _translate("MainWindow", "Message"), + _translate("MainWindow", "All") + ): + if what.lower() not in message.lower(): + return False return True diff --git a/src/helper_startup.py b/src/helper_startup.py index e4371400..96e96a5a 100644 --- a/src/helper_startup.py +++ b/src/helper_startup.py @@ -279,19 +279,28 @@ def updateConfig(): config.save() -def isOurOperatingSystemLimitedToHavingVeryFewHalfOpenConnections(): - """Check for (mainly XP and Vista) limitations""" +def adjustHalfOpenConnectionsLimit(): + """Check and satisfy half-open connections limit (mainly XP and Vista)""" + if BMConfigParser().safeGet( + 'bitmessagesettings', 'socksproxytype', 'none') != 'none': + state.maximumNumberOfHalfOpenConnections = 4 + return + + is_limited = False try: if sys.platform[0:3] == "win": + # Some XP and Vista systems can only have 10 outgoing + # connections at a time. VER_THIS = StrictVersion(platform.version()) - return ( + is_limited = ( StrictVersion("5.1.2600") <= VER_THIS and StrictVersion("6.0.6000") >= VER_THIS ) - return False - except Exception: + except ValueError: pass + state.maximumNumberOfHalfOpenConnections = 9 if is_limited else 64 + def start_proxyconfig(): """Check socksproxytype and start any proxy configuration plugin""" diff --git a/src/highlevelcrypto.py b/src/highlevelcrypto.py index 03e2d1d3..028f6299 100644 --- a/src/highlevelcrypto.py +++ b/src/highlevelcrypto.py @@ -2,8 +2,8 @@ High level cryptographic functions based on `.pyelliptic` OpenSSL bindings. .. note:: - Upstream pyelliptic was upgraded from SHA1 to SHA256 for signing. - We must upgrade PyBitmessage gracefully. + Upstream pyelliptic was upgraded from SHA1 to SHA256 for signing. We must + `upgrade PyBitmessage gracefully. `_ `More discussion. `_ """ @@ -69,7 +69,7 @@ def sign(msg, hexPrivkey): "digestalg" setting """ digestAlg = BMConfigParser().safeGet( - 'bitmessagesettings', 'digestalg', 'sha1') + 'bitmessagesettings', 'digestalg', 'sha256') if digestAlg == "sha1": # SHA1, this will eventually be deprecated return makeCryptor(hexPrivkey).sign( diff --git a/src/images/btc.png b/src/images/btc.png new file mode 100644 index 00000000..33302ff8 Binary files /dev/null and b/src/images/btc.png differ diff --git a/src/images/buy.png b/src/images/buy.png new file mode 100644 index 00000000..3a63af11 Binary files /dev/null and b/src/images/buy.png differ diff --git a/src/images/buynew.png b/src/images/buynew.png new file mode 100644 index 00000000..852e57b5 Binary files /dev/null and b/src/images/buynew.png differ diff --git a/src/images/buynew1.png b/src/images/buynew1.png new file mode 100644 index 00000000..f02090f8 Binary files /dev/null and b/src/images/buynew1.png differ diff --git a/src/images/credits.jpeg b/src/images/credits.jpeg new file mode 100644 index 00000000..6b75132a Binary files /dev/null and b/src/images/credits.jpeg differ diff --git a/src/images/creditss.png b/src/images/creditss.png new file mode 100644 index 00000000..b45cd997 Binary files /dev/null and b/src/images/creditss.png differ diff --git a/src/images/gpay.png b/src/images/gpay.png new file mode 100644 index 00000000..62aa55cd Binary files /dev/null and b/src/images/gpay.png differ diff --git a/src/images/gplay.png b/src/images/gplay.png new file mode 100644 index 00000000..69550edd Binary files /dev/null and b/src/images/gplay.png differ diff --git a/src/images/gplayfinal.png b/src/images/gplayfinal.png new file mode 100644 index 00000000..16d0d538 Binary files /dev/null and b/src/images/gplayfinal.png differ diff --git a/src/images/gplayss.png b/src/images/gplayss.png new file mode 100644 index 00000000..67f293fd Binary files /dev/null and b/src/images/gplayss.png differ diff --git a/src/images/images.png b/src/images/images.png new file mode 100644 index 00000000..7bb33861 Binary files /dev/null and b/src/images/images.png differ diff --git a/src/images/paypal.png b/src/images/paypal.png new file mode 100644 index 00000000..f994130d Binary files /dev/null and b/src/images/paypal.png differ diff --git a/src/kivy_garden/__init__.py b/src/kivy_garden/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/kivy_garden/xcamera/__init__.py b/src/kivy_garden/xcamera/__init__.py new file mode 100644 index 00000000..e2c59314 --- /dev/null +++ b/src/kivy_garden/xcamera/__init__.py @@ -0,0 +1,15 @@ +""" +Exposes `XCamera` directly in `xcamera` rather than `xcamera.xcamera`. +Also note this may break `pip` since all imports within `xcamera.py` would be +required at setup time. This is because `version.py` (same directory) is used +by the `setup.py` file. +Hence we're not exposing `XCamera` if `pip` is detected. +""" +import os + +project_dir = os.path.abspath( + os.path.join(__file__, os.pardir, os.pardir, os.pardir, os.pardir)) +using_pip = os.path.basename(project_dir).startswith('pip-') +# only exposes `XCamera` if not within `pip` ongoing install +if not using_pip: + from .xcamera import XCamera # noqa diff --git a/src/kivy_garden/xcamera/android_api.py b/src/kivy_garden/xcamera/android_api.py new file mode 100644 index 00000000..1beb5b8f --- /dev/null +++ b/src/kivy_garden/xcamera/android_api.py @@ -0,0 +1,85 @@ +from kivy.logger import Logger + +from jnius import JavaException, PythonJavaClass, autoclass, java_method + +Camera = autoclass('android.hardware.Camera') +AndroidActivityInfo = autoclass('android.content.pm.ActivityInfo') +AndroidPythonActivity = autoclass('org.kivy.android.PythonActivity') +PORTRAIT = AndroidActivityInfo.SCREEN_ORIENTATION_PORTRAIT +LANDSCAPE = AndroidActivityInfo.SCREEN_ORIENTATION_LANDSCAPE + + +class ShutterCallback(PythonJavaClass): + __javainterfaces__ = ('android.hardware.Camera$ShutterCallback', ) + + @java_method('()V') + def onShutter(self): + # apparently, it is enough to have an empty shutter callback to play + # the standard shutter sound. If you pass None instead of shutter_cb + # below, the standard sound doesn't play O_o + pass + + +class PictureCallback(PythonJavaClass): + __javainterfaces__ = ('android.hardware.Camera$PictureCallback', ) + + def __init__(self, filename, on_success): + super(PictureCallback, self).__init__() + self.filename = filename + self.on_success = on_success + + @java_method('([BLandroid/hardware/Camera;)V') + def onPictureTaken(self, data, camera): + s = data.tostring() + with open(self.filename, 'wb') as f: + f.write(s) + Logger.info('xcamera: picture saved to %s', self.filename) + camera.startPreview() + self.on_success(self.filename) + + +class AutoFocusCallback(PythonJavaClass): + __javainterfaces__ = ('android.hardware.Camera$AutoFocusCallback', ) + + def __init__(self, filename, on_success): + super(AutoFocusCallback, self).__init__() + self.filename = filename + self.on_success = on_success + + @java_method('(ZLandroid/hardware/Camera;)V') + def onAutoFocus(self, success, camera): + if success: + Logger.info('xcamera: autofocus succeeded, taking picture...') + shutter_cb = ShutterCallback() + picture_cb = PictureCallback(self.filename, self.on_success) + camera.takePicture(shutter_cb, None, picture_cb) + else: + Logger.info('xcamera: autofocus failed') + + +def take_picture(camera_widget, filename, on_success): + # to call the android API, we need access to the underlying + # android.hardware.Camera instance. However, there is no official way to + # retrieve it from the camera widget, so we need to dig into internal + # attributes :-( This works at least on kivy 1.9.1, but it might break any + # time soon. + camera = camera_widget._camera._android_camera + params = camera.getParameters() + params.setFocusMode("auto") + camera.setParameters(params) + cb = AutoFocusCallback(filename, on_success) + Logger.info('xcamera: starting autofocus...') + try: + camera.autoFocus(cb) + except JavaException as e: + Logger.info('Error when calling autofocus: {}'.format(e)) + + +def set_orientation(value): + previous = get_orientation() + AndroidPythonActivity.mActivity.setRequestedOrientation(value) + return previous + + +def get_orientation(): + return AndroidPythonActivity.mActivity.getRequestedOrientation() diff --git a/src/kivy_garden/xcamera/data/icons.ttf b/src/kivy_garden/xcamera/data/icons.ttf new file mode 100644 index 00000000..1eae4aa1 Binary files /dev/null and b/src/kivy_garden/xcamera/data/icons.ttf differ diff --git a/src/kivy_garden/xcamera/data/shutter.wav b/src/kivy_garden/xcamera/data/shutter.wav new file mode 100644 index 00000000..66d4380e Binary files /dev/null and b/src/kivy_garden/xcamera/data/shutter.wav differ diff --git a/src/kivy_garden/xcamera/main.py b/src/kivy_garden/xcamera/main.py new file mode 100644 index 00000000..c5226627 --- /dev/null +++ b/src/kivy_garden/xcamera/main.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +from kivy.app import App +from kivy.lang import Builder + +kv = """ +#:import XCamera kivy_garden.xcamera.XCamera + +FloatLayout: + orientation: 'vertical' + + XCamera: + id: xcamera + on_picture_taken: app.picture_taken(*args) + + BoxLayout: + orientation: 'horizontal' + size_hint: 1, None + height: sp(50) + + Button: + text: 'Set landscape' + on_release: xcamera.force_landscape() + + Button: + text: 'Restore orientation' + on_release: xcamera.restore_orientation() +""" + + +class CameraApp(App): + def build(self): + return Builder.load_string(kv) + + def picture_taken(self, obj, filename): + print('Picture taken and saved to {}'.format(filename)) + + +def main(): + CameraApp().run() + + +if __name__ == '__main__': + main() diff --git a/src/kivy_garden/xcamera/platform_api.py b/src/kivy_garden/xcamera/platform_api.py new file mode 100644 index 00000000..ea0236db --- /dev/null +++ b/src/kivy_garden/xcamera/platform_api.py @@ -0,0 +1,37 @@ +from kivy.utils import platform + + +def play_shutter(): + # bah, apparently we need to delay the import of kivy.core.audio, lese + # kivy cannot find a camera provider, at lease on linux. Maybe a + # gstreamer/pygame issue? + from kivy.core.audio import SoundLoader + sound = SoundLoader.load("data/shutter.wav") + sound.play() + + +if platform == 'android': + from .android_api import ( + LANDSCAPE, PORTRAIT, take_picture, set_orientation, get_orientation) + +else: + + # generic fallback for taking pictures. Probably not the best quality, + # they are meant mostly for testing + LANDSCAPE = 'landscape' + PORTRAIT = 'portrait' + + def take_picture(camera_widget, filename, on_success): + camera_widget.texture.save(filename, flipped=False) + play_shutter() + on_success(filename) + + def set_orientation(value): + previous = get_orientation() + print('FAKE orientation set to {}'.format(value)) + get_orientation.value = value + return previous + + def get_orientation(): + return get_orientation.value + get_orientation.value = PORTRAIT diff --git a/src/kivy_garden/xcamera/version.py b/src/kivy_garden/xcamera/version.py new file mode 100644 index 00000000..c33baebf --- /dev/null +++ b/src/kivy_garden/xcamera/version.py @@ -0,0 +1 @@ +__version__ = '2019.0928' diff --git a/src/kivy_garden/xcamera/xcamera.kv b/src/kivy_garden/xcamera/xcamera.kv new file mode 100644 index 00000000..966e702c --- /dev/null +++ b/src/kivy_garden/xcamera/xcamera.kv @@ -0,0 +1,41 @@ +#:import xcamera kivy_garden.xcamera.xcamera + + + icon_color: (0, 0, 0, 1) + _down_color: xcamera.darker(self.icon_color) + icon_size: dp(50) + + canvas.before: + Color: + rgba: self.icon_color if self.state == 'normal' else self._down_color + Ellipse: + pos: self.pos + size: self.size + + size_hint: None, None + size: self.icon_size, self.icon_size + font_size: self.icon_size/2 + + +: + # \ue800 corresponds to the camera icon in the font + icon: u"[font=data/icons.ttf]\ue800[/font]" + icon_color: (0.13, 0.58, 0.95, 0.8) + icon_size: dp(70) + + id: camera + resolution: 640, 480 #1920, 1080 # # + allow_stretch: True + + # Shoot button + XCameraIconButton: + id: shoot_button + markup: True + text: root.icon + icon_color: root.icon_color + icon_size: root.icon_size + on_release: root.shoot() + + # position + right: root.width - dp(10) + center_y: root.center_y diff --git a/src/kivy_garden/xcamera/xcamera.py b/src/kivy_garden/xcamera/xcamera.py new file mode 100644 index 00000000..f08871d5 --- /dev/null +++ b/src/kivy_garden/xcamera/xcamera.py @@ -0,0 +1,116 @@ +import datetime +import os + +from kivy.clock import mainthread +from kivy.lang import Builder +from kivy.properties import ObjectProperty +from kivy.resources import resource_add_path +from kivy.uix.behaviors import ButtonBehavior +from kivy.uix.camera import Camera +from kivy.uix.label import Label +from kivy.utils import platform + +from .platform_api import LANDSCAPE, set_orientation, take_picture + +ROOT = os.path.dirname(os.path.abspath(__file__)) +resource_add_path(ROOT) + + +def darker(color, factor=0.5): + r, g, b, a = color + r *= factor + g *= factor + b *= factor + return r, g, b, a + + +def get_filename(): + return datetime.datetime.now().strftime('%Y-%m-%d %H.%M.%S.jpg') + + +def is_android(): + return platform == 'android' + + +def check_camera_permission(): + """ + Android runtime `CAMERA` permission check. + """ + if not is_android(): + return True + from android.permissions import Permission, check_permission + permission = Permission.CAMERA + return check_permission(permission) + + +def check_request_camera_permission(callback=None): + """ + Android runtime `CAMERA` permission check & request. + """ + had_permission = check_camera_permission() + if not had_permission: + from android.permissions import Permission, request_permissions + permissions = [Permission.CAMERA] + request_permissions(permissions, callback) + return had_permission + + +class XCameraIconButton(ButtonBehavior, Label): + pass + + +class XCamera(Camera): + directory = ObjectProperty(None) + _previous_orientation = None + __events__ = ('on_picture_taken', 'on_camera_ready') + + def __init__(self, **kwargs): + Builder.load_file(os.path.join(ROOT, "xcamera.kv")) + super().__init__(**kwargs) + + def _on_index(self, *largs): + """ + Overrides `kivy.uix.camera.Camera._on_index()` to make sure + `camera.open()` is not called unless Android `CAMERA` permission is + granted, refs #5. + """ + @mainthread + def on_permissions_callback(permissions, grant_results): + """ + On camera permission callback calls parent `_on_index()` method. + """ + if all(grant_results): + self._on_index_dispatch(*largs) + if check_request_camera_permission(callback=on_permissions_callback): + self._on_index_dispatch(*largs) + + def _on_index_dispatch(self, *largs): + super()._on_index(*largs) + self.dispatch('on_camera_ready') + + def on_picture_taken(self, filename): + """ + This event is fired every time a picture has been taken. + """ + pass + + def on_camera_ready(self): + """ + Fired when the camera is ready. + """ + pass + + def shoot(self): + def on_success(filename): + self.dispatch('on_picture_taken', filename) + filename = get_filename() + if self.directory: + filename = os.path.join(self.directory, filename) + take_picture(self, filename, on_success) + + def force_landscape(self): + self._previous_orientation = set_orientation(LANDSCAPE) + + def restore_orientation(self): + if self._previous_orientation is not None: + set_orientation(self._previous_orientation) diff --git a/src/kivy_garden/zbarcam/__init__.py b/src/kivy_garden/zbarcam/__init__.py new file mode 100644 index 00000000..7fe813a8 --- /dev/null +++ b/src/kivy_garden/zbarcam/__init__.py @@ -0,0 +1,15 @@ +""" +Exposes `ZBarCam` directly in `zbarcam` rather than `zbarcam.zbarcam`. +Also note this may break `pip` since all imports within `zbarcam.py` would be +required at setup time. This is because `version.py` (same directory) is used +by the `setup.py` file. +Hence we're not exposing `ZBarCam` if `pip` is detected. +""" +import os + +project_dir = os.path.abspath( + os.path.join(__file__, os.pardir, os.pardir, os.pardir, os.pardir)) +using_pip = os.path.basename(project_dir).startswith('pip-') +# only exposes `ZBarCam` if not within `pip` ongoing install +if not using_pip: + from .zbarcam import ZBarCam # noqa diff --git a/src/kivy_garden/zbarcam/utils.py b/src/kivy_garden/zbarcam/utils.py new file mode 100644 index 00000000..f1f6a53c --- /dev/null +++ b/src/kivy_garden/zbarcam/utils.py @@ -0,0 +1,21 @@ +from kivy.utils import platform +from PIL import ImageOps + + +def is_android(): + return platform == 'android' + + +def is_ios(): + return platform == 'ios' + + +def fix_android_image(pil_image): + """ + On Android, the image seems mirrored and rotated somehow, refs #32. + """ + if not is_android(): + return pil_image + pil_image = pil_image.rotate(90) + pil_image = ImageOps.mirror(pil_image) + return pil_image diff --git a/src/kivy_garden/zbarcam/version.py b/src/kivy_garden/zbarcam/version.py new file mode 100644 index 00000000..ec343874 --- /dev/null +++ b/src/kivy_garden/zbarcam/version.py @@ -0,0 +1,7 @@ +__version__ = '2019.1020' +# The `__version_code__` is used for the F-Droid auto update and should match +# the `versionCode` from the `build.gradle` file located in: +# `.buildozer/android/platform/build-*/dists/zbarcamdemo__*/build.gradle` +# The auto update method used is the `HTTP`, see: +# https://f-droid.org/en/docs/Build_Metadata_Reference/#UpdateCheckMode +__version_code__ = 721202920 diff --git a/src/kivy_garden/zbarcam/zbarcam.kv b/src/kivy_garden/zbarcam/zbarcam.kv new file mode 100644 index 00000000..2a077ffe --- /dev/null +++ b/src/kivy_garden/zbarcam/zbarcam.kv @@ -0,0 +1,26 @@ +#:import XCamera kivy_garden.xcamera.XCamera +#:import is_android kivy_garden.zbarcam.utils.is_android +: + Widget: + # invert width/height on rotated Android + # https://stackoverflow.com/a/45192295/185510 + id: proxy + XCamera: + id: xcamera + play: True + resolution: root.resolution + allow_stretch: True + keep_ratio: True + center: self.size and proxy.center + size: + (proxy.height, proxy.width) if is_android() \ + else (proxy.width, proxy.height) + # Android camera rotation workaround, refs: + # https://github.com/AndreMiras/garden.zbarcam/issues/3 + canvas.before: + PushMatrix + Rotate: + angle: -90 if is_android() else 0 + origin: self.center + canvas.after: + PopMatrix diff --git a/src/kivy_garden/zbarcam/zbarcam.py b/src/kivy_garden/zbarcam/zbarcam.py new file mode 100644 index 00000000..91135417 --- /dev/null +++ b/src/kivy_garden/zbarcam/zbarcam.py @@ -0,0 +1,214 @@ +import os +from collections import namedtuple + +import PIL +from kivy.clock import Clock +from kivy.lang import Builder +from kivy.properties import ListProperty, ObjectProperty, NumericProperty +from kivy.uix.anchorlayout import AnchorLayout +from kivy.uix.boxlayout import BoxLayout +from kivy.animation import Animation +from kivy.graphics import Color, Line +from pyzbar import pyzbar +from kivy.utils import platform +# from android.runnable import run_on_ui_thread + +from .utils import fix_android_image + +MODULE_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) + +from kivy.metrics import dp + +class ZBarCam(BoxLayout): + """ + Widget that use the Camera and zbar to detect qrcode. + When found, the `codes` will be updated. + """ + resolution = ListProperty([640, 480]) + qrwidth = dp(250) if platform=='android' else dp(600) + # qrwidth= dp(500) #width of QR code scanner + line_length= qrwidth/4 #width of boundary line (Focus Box) + scanner_line_y_initial= NumericProperty(0) + scanner_line_y_final= NumericProperty(0) + scanner_line_y= NumericProperty(0) + scanner_line_width= dp(2) + sscanner_line_alpha=1 + border_line_alpha=0.4 + + symbols = ListProperty([]) + Symbol = namedtuple('Symbol', ['type', 'data']) + scan_callback= ObjectProperty(None) + # checking all possible types by default + code_types = ListProperty(set(pyzbar.ZBarSymbol)) + + def __init__(self, **kwargs): + # lazy loading the kv file rather than loading at module level, + # that way the `XCamera` import doesn't happen too early + Builder.load_file(os.path.join(MODULE_DIRECTORY, "zbarcam.kv")) + super().__init__(**kwargs) + Clock.schedule_once(lambda dt: self._setup()) + self.register_event_type("on_scan") + + def _setup(self): + """ + Postpones some setup tasks that require self.ids dictionary. + """ + self._remove_shoot_button() + # `self.xcamera._camera` instance may not be available if e.g. + # the `CAMERA` permission is not granted + self.xcamera.bind(on_camera_ready=self._on_camera_ready) + # camera may still be ready before we bind the event + if self.xcamera._camera is not None: + self._on_camera_ready(self.xcamera) + + def _on_camera_ready(self, xcamera): + """ + Starts binding when the `xcamera._camera` instance is ready. + """ + xcamera._camera.bind(on_texture=self._on_texture) + #print((self.scanner_line_y_initial, self.scanner_line_y_final)) + #print(self.size[0]) + Clock.schedule_once(lambda dt: self.start_animation(1), 0) + #start scanning animation + #self.init_scanner_line(self.scanner_line_y_final) + + def _remove_shoot_button(self): + """ + Removes the "shoot button", see: + https://github.com/kivy-garden/garden.xcamera/pull/3 + """ + xcamera = self.xcamera + shoot_button = xcamera.children[0] + xcamera.remove_widget(shoot_button) + + def _on_texture(self, instance): + #print((self.scanner_line_y_initial, self.scanner_line_y_final)) + #print(self.size[0]) + self.symbols = self._detect_qrcode_frame( + texture=instance.texture, code_types=self.code_types) + + txt= ', '.join([symbol.data.decode("utf-8") for symbol in self.symbols]) + if txt: + self.scanner_line_alpha=0 + self.dispatch("on_scan", txt) + else: self.scanner_line_alpha=1 + + def on_scan(self, text, *args): + if self.scan_callback: + self.scan_callback(text) + + def init_scanner_line(self, *args): + self.scanner_line_y= self.scanner_line_y_initial + Clock.schedule_once(lambda dt: self._update_canvas()) + self.update_scanner_line(self.scanner_line_y_final+self.scanner_line_width, self.scanner_line_y_initial-self.scanner_line_width) + + def update_scanner_line(self, val1, val2, *args): + anim= Animation( + d=1.5, + scanner_line_y= val1, + ) + anim += Animation( + d=1.5, + scanner_line_y= val2, + ) + #anim.stop_all(self) + anim.bind(on_complete=self._repeat_anim) + anim.bind(on_progress=self._update_canvas) + anim.start(self) + + def start_animation(self, val, *args): + anim= Animation( + d=.7, + border_line_alpha=val + ) + anim.bind(on_complete=self._repeat_anim) + anim.bind(on_progress=self.update_border_line) + anim.start(self) + + def _repeat_anim(self, inst, widget): + inst.unbind(on_complete=self._repeat_anim) + border_line_alpha = 0.4 if self.border_line_alpha==1 else 1 + self.start_animation(border_line_alpha) + # self.update_scanner_line(self.scanner_line_y_final+self.scanner_line_width, self.scanner_line_y_initial-self.scanner_line_width) + + def _update_canvas(self, *args): + self.canvas.remove_group("scanner_line") + with self.canvas: + Color(rgba=(0,1,0,self.scanner_line_alpha), group="scanner_line") + Line( + group="scanner_line", + points= [ + self.size[0]/2-self.qrwidth/2+1, self.scanner_line_y, + self.size[0]/2+self.qrwidth/2-1, self.scanner_line_y + ], + width=self.scanner_line_width, + cap="none" + ) + + def update_border_line(self, *args): + self.canvas.remove_group("qr_line") + with self.canvas: + Color(rgba=(1,1,1,self.border_line_alpha), group="qr_line") + #top left + Line(points=[\ + self.size[0]/2-self.qrwidth/2, self.size[1]/2+self.qrwidth/2-self.line_length,\ + self.size[0]/2-self.qrwidth/2, self.size[1]/2+self.qrwidth/2,\ + self.size[0]/2-self.qrwidth/2+self.line_length, self.size[1]/2+self.qrwidth/2], width=dp(2),cap= "none", group="qr_line") + + #top right + Line(points=[\ + self.size[0]/2+self.qrwidth/2, self.size[1]/2+self.qrwidth/2-self.line_length,\ + self.size[0]/2+self.qrwidth/2, self.size[1]/2+self.qrwidth/2,\ + self.size[0]/2+self.qrwidth/2-self.line_length, self.size[1]/2+self.qrwidth/2], width=dp(2),cap= "none", group="qr_line") + + #bottom right + Line(points=[\ + self.size[0]/2+self.qrwidth/2, self.size[1]/2-self.qrwidth/2+self.line_length,\ + self.size[0]/2+self.qrwidth/2, self.size[1]/2-self.qrwidth/2,\ + self.size[0]/2+self.qrwidth/2-self.line_length, self.size[1]/2-self.qrwidth/2], width= dp(2), cap="none", group="qr_line") + + #bottom left + Line(points=[\ + self.size[0]/2-self.qrwidth/2, self.size[1]/2-self.qrwidth/2+self.line_length,\ + self.size[0]/2-self.qrwidth/2, self.size[1]/2-self.qrwidth/2,\ + self.size[0]/2-self.qrwidth/2+self.line_length, self.size[1]/2-self.qrwidth/2],width= dp(2),cap= "none", group="qr_line") + + + @classmethod + def _detect_qrcode_frame(cls, texture, code_types): + image_data = texture.pixels + size = texture.size + #print(cls.height.value, cls.width) + #print(image_data) + # Fix for mode mismatch between texture.colorfmt and data returned by + # texture.pixels. texture.pixels always returns RGBA, so that should + # be passed to PIL no matter what texture.colorfmt returns. refs: + # https://github.com/AndreMiras/garden.zbarcam/issues/41 + pil_image = PIL.Image.frombytes(mode='RGBA', size=size, + data=image_data) + pil_image = fix_android_image(pil_image) + pil_image.thumbnail(size, PIL.Image.ANTIALIAS) + + qrwidth= cls.qrwidth + cropped_image = pil_image.crop((size[0]/2-qrwidth/4.5,size[1]/2-qrwidth/4.5,size[0]/2+qrwidth/4.5,size[1]/2+qrwidth/4.5)) + + #print(pil_image.size) + symbols = [] + codes = pyzbar.decode(cropped_image, symbols=code_types) + for code in codes: + symbol = ZBarCam.Symbol(type=code.type, data=code.data) + symbols.append(symbol) + + return symbols + + + + @property + def xcamera(self): + return self.ids['xcamera'] + + def start(self): + self.xcamera.play = True + + def stop(self): + self.xcamera.play = False diff --git a/src/network/bmproto.py b/src/network/bmproto.py index 9530ef8d..9133add8 100644 --- a/src/network/bmproto.py +++ b/src/network/bmproto.py @@ -444,9 +444,10 @@ class BMProto(AdvancedDispatcher, ObjectTracker): if stream not in state.streamsInWhichIAmParticipating: continue if ( - decodedIP and time.time() - seenTime > 0 and - seenTime > time.time() - ADDRESS_ALIVE and - port > 0 + decodedIP + and time.time() - seenTime > 0 + and seenTime > time.time() - ADDRESS_ALIVE + and port > 0 ): peer = Peer(decodedIP, port) try: @@ -514,9 +515,11 @@ class BMProto(AdvancedDispatcher, ObjectTracker): Incoming version. Parse and log, remember important things, like streams, bitfields, etc. """ + decoded = self.decode_payload_content("IQQiiQlslv") (self.remoteProtocolVersion, self.services, self.timestamp, - self.sockNode, self.peerNode, self.nonce, self.userAgent, - self.streams) = self.decode_payload_content("IQQiiQlsLv") + self.sockNode, self.peerNode, self.nonce, self.userAgent + ) = decoded[:7] + self.streams = decoded[7:] self.nonce = struct.pack('>Q', self.nonce) self.timeOffset = self.timestamp - int(time.time()) logger.debug('remoteProtocolVersion: %i', self.remoteProtocolVersion) @@ -541,7 +544,8 @@ class BMProto(AdvancedDispatcher, ObjectTracker): logger.debug( '%(host)s:%(port)i sending version', self.destination._asdict()) - if self.services & protocol.NODE_SSL == protocol.NODE_SSL: + if ((self.services & protocol.NODE_SSL == protocol.NODE_SSL) + and protocol.haveSSL(not self.isOutbound)): self.isSSL = True if not self.verackReceived: return True @@ -599,7 +603,7 @@ class BMProto(AdvancedDispatcher, ObjectTracker): 'Closed connection to {} because we are already connected' ' to that IP.'.format(self.destination)) return False - except: + except Exception: pass if not self.isOutbound: # incoming from a peer we're connected to as outbound, diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 159f66f3..cf281ede 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -89,6 +89,7 @@ class BMConnectionPool(object): def connectToStream(self, streamNumber): """Connect to a bitmessage stream""" self.streams.append(streamNumber) + state.streamsInWhichIAmParticipating.append(streamNumber) def getConnectionByAddr(self, addr): """ diff --git a/src/network/tcp.py b/src/network/tcp.py index 3e566ad1..5798ed28 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -14,7 +14,6 @@ from network import connectionpool import helper_random import knownnodes import protocol -import shared import state from bmconfigparser import BMConfigParser from helper_random import randomBytes @@ -35,6 +34,9 @@ from queues import UISignalQueue, invQueue, receiveDataQueue logger = logging.getLogger('default') +maximumAgeOfNodesThatIAdvertiseToOthers = 10800 #: Equals three hours + + class TCPConnection(BMProto, TLSDispatcher): # pylint: disable=too-many-instance-attributes """ @@ -142,7 +144,7 @@ class TCPConnection(BMProto, TLSDispatcher): def set_connection_fully_established(self): """Initiate inventory synchronisation.""" if not self.isOutbound and not self.local: - shared.clientHasReceivedIncomingConnections = True + state.clientHasReceivedIncomingConnections = True UISignalQueue.put(('setStatusIcon', 'green')) UISignalQueue.put( ('updateNetworkStatusTab', ( @@ -176,7 +178,7 @@ class TCPConnection(BMProto, TLSDispatcher): filtered = [ (k, v) for k, v in iter(nodes.items()) if v["lastseen"] > int(time.time()) - - shared.maximumAgeOfNodesThatIAdvertiseToOthers and + maximumAgeOfNodesThatIAdvertiseToOthers and v["rating"] >= 0 and len(k.host) <= 22 ] # sent 250 only if the remote isn't interested in it diff --git a/src/pyelliptic/__init__.py b/src/pyelliptic/__init__.py index dbc1b2af..cafa89c9 100644 --- a/src/pyelliptic/__init__.py +++ b/src/pyelliptic/__init__.py @@ -12,6 +12,7 @@ This is an abandoned package maintained inside of the PyBitmessage. from .cipher import Cipher from .ecc import ECC from .eccblind import ECCBlind +from .eccblindchain import ECCBlindChain from .hash import hmac_sha256, hmac_sha512, pbkdf2 from .openssl import OpenSSL @@ -21,6 +22,7 @@ __all__ = [ 'OpenSSL', 'ECC', 'ECCBlind', + 'ECCBlindChain', 'Cipher', 'hmac_sha256', 'hmac_sha512', diff --git a/src/pyelliptic/eccblind.py b/src/pyelliptic/eccblind.py index 5b9b045e..a417451e 100644 --- a/src/pyelliptic/eccblind.py +++ b/src/pyelliptic/eccblind.py @@ -10,8 +10,70 @@ http://www.isecure-journal.com/article_39171_47f9ec605dd3918c2793565ec21fcd7a.pd # variable names are based on the math in the paper, so they don't conform # to PEP8 +import time +from hashlib import sha256 +from struct import pack, unpack + from .openssl import OpenSSL +# first byte in serialisation can contain data +Y_BIT = 0x01 +COMPRESSED_BIT = 0x02 + +# formats +BIGNUM = '!32s' +EC = '!B32s' +PUBKEY = '!BB33s' + + +class Expiration(object): + """Expiration of pubkey""" + @staticmethod + def deserialize(val): + """Create an object out of int""" + year = ((val & 0xF0) >> 4) + 2020 + month = val & 0x0F + assert month < 12 + return Expiration(year, month) + + def __init__(self, year, month): + assert isinstance(year, int) + assert year > 2019 and year < 2036 + assert isinstance(month, int) + assert month < 12 + self.year = year + self.month = month + self.exp = year + month / 12.0 + + def serialize(self): + """Make int out of object""" + return ((self.year - 2020) << 4) + self.month + + def verify(self): + """Check if the pubkey has expired""" + now = time.gmtime() + return self.exp >= now.tm_year + (now.tm_mon - 1) / 12.0 + + +class Value(object): + """Value of a pubkey""" + @staticmethod + def deserialize(val): + """Make object out of int""" + return Value(val) + + def __init__(self, value=0xFF): + assert isinstance(value, int) + self.value = value + + def serialize(self): + """Make int out of object""" + return self.value & 0xFF + + def verify(self, value): + """Verify against supplied value""" + return value <= self.value + class ECCBlind(object): # pylint: disable=too-many-instance-attributes """ @@ -21,8 +83,8 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes # init k = None R = None - keypair = None F = None + d = None Q = None a = None b = None @@ -33,92 +95,183 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes m_ = None s_ = None signature = None + exp = None + val = None - @staticmethod - def ec_get_random(group, ctx): + def ec_get_random(self): """ - Random point from finite field + Random integer within the EC order """ - order = OpenSSL.BN_new() - OpenSSL.EC_GROUP_get_order(group, order, ctx) - OpenSSL.BN_rand(order, OpenSSL.BN_num_bits(order), 0, 0) - return order + randomnum = OpenSSL.BN_new() + OpenSSL.BN_rand(randomnum, OpenSSL.BN_num_bits(self.n), 0, 0) + return randomnum - @staticmethod - def ec_invert(group, a, ctx): + def ec_invert(self, a): """ ECC inversion """ - order = OpenSSL.BN_new() - OpenSSL.EC_GROUP_get_order(group, order, ctx) - inverse = OpenSSL.BN_mod_inverse(0, a, order, ctx) + inverse = OpenSSL.BN_mod_inverse(0, a, self.n, self.ctx) return inverse - @staticmethod - def ec_gen_keypair(group, ctx): + def ec_gen_keypair(self): """ Generate an ECC keypair + We're using compressed keys """ - d = ECCBlind.ec_get_random(group, ctx) - Q = OpenSSL.EC_POINT_new(group) - OpenSSL.EC_POINT_mul(group, Q, d, 0, 0, 0) + d = self.ec_get_random() + Q = OpenSSL.EC_POINT_new(self.group) + OpenSSL.EC_POINT_mul(self.group, Q, d, 0, 0, 0) return (d, Q) - @staticmethod - def ec_Ftor(F, group, ctx): + def ec_Ftor(self, F): """ x0 coordinate of F """ # F = (x0, y0) x0 = OpenSSL.BN_new() y0 = OpenSSL.BN_new() - OpenSSL.EC_POINT_get_affine_coordinates_GFp(group, F, x0, y0, ctx) + OpenSSL.EC_POINT_get_affine_coordinates(self.group, F, x0, y0, self.ctx) + OpenSSL.BN_free(y0) return x0 - def __init__(self, curve="secp256k1", pubkey=None): + def _ec_point_serialize(self, point): + """Make an EC point into a string""" + try: + x = OpenSSL.BN_new() + y = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates( + self.group, point, x, y, 0) + y_byte = (OpenSSL.BN_is_odd(y) & Y_BIT) | COMPRESSED_BIT + l_ = OpenSSL.BN_num_bytes(self.n) + try: + bx = OpenSSL.malloc(0, l_) + OpenSSL.BN_bn2binpad(x, bx, l_) + out = bx.raw + except AttributeError: + # padding manually + bx = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(x)) + OpenSSL.BN_bn2bin(x, bx) + out = bx.raw.rjust(l_, chr(0)) + return pack(EC, y_byte, out) + + finally: + OpenSSL.BN_clear_free(x) + OpenSSL.BN_clear_free(y) + + def _ec_point_deserialize(self, data): + """Make a string into an EC point""" + y_bit, x_raw = unpack(EC, data) + x = OpenSSL.BN_bin2bn(x_raw, OpenSSL.BN_num_bytes(self.n), 0) + y_bit &= Y_BIT + retval = OpenSSL.EC_POINT_new(self.group) + OpenSSL.EC_POINT_set_compressed_coordinates(self.group, + retval, + x, + y_bit, + self.ctx) + return retval + + def _bn_serialize(self, bn): + """Make a string out of BigNum""" + l_ = OpenSSL.BN_num_bytes(self.n) + try: + o = OpenSSL.malloc(0, l_) + OpenSSL.BN_bn2binpad(bn, o, l_) + return o.raw + except AttributeError: + o = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(bn)) + OpenSSL.BN_bn2bin(bn, o) + return o.raw.rjust(l_, chr(0)) + + def _bn_deserialize(self, data): + """Make a BigNum out of string""" + x = OpenSSL.BN_bin2bn(data, OpenSSL.BN_num_bytes(self.n), 0) + return x + + def _init_privkey(self, privkey): + """Initialise private key out of string/bytes""" + self.d = self._bn_deserialize(privkey) + + def privkey(self): + """Make a private key into a string""" + return pack(BIGNUM, self.d) + + def _init_pubkey(self, pubkey): + """Initialise pubkey out of string/bytes""" + unpacked = unpack(PUBKEY, pubkey) + self.expiration = Expiration.deserialize(unpacked[0]) + self.value = Value.deserialize(unpacked[1]) + self.Q = self._ec_point_deserialize(unpacked[2]) + + def pubkey(self): + """Make a pubkey into a string""" + return pack(PUBKEY, self.expiration.serialize(), + self.value.serialize(), + self._ec_point_serialize(self.Q)) + + def __init__(self, curve="secp256k1", pubkey=None, privkey=None, # pylint: disable=too-many-arguments + year=2025, month=11, value=0xFF): self.ctx = OpenSSL.BN_CTX_new() - if pubkey: - self.group, self.G, self.n, self.Q = pubkey - else: - self.group = OpenSSL.EC_GROUP_new_by_curve_name( - OpenSSL.get_curve(curve)) - # Order n - self.n = OpenSSL.BN_new() - OpenSSL.EC_GROUP_get_order(self.group, self.n, self.ctx) + # ECC group + self.group = OpenSSL.EC_GROUP_new_by_curve_name( + OpenSSL.get_curve(curve)) - # Generator G - self.G = OpenSSL.EC_GROUP_get0_generator(self.group) + # Order n + self.n = OpenSSL.BN_new() + OpenSSL.EC_GROUP_get_order(self.group, self.n, self.ctx) - # new keypair - self.keypair = ECCBlind.ec_gen_keypair(self.group, self.ctx) - - self.Q = self.keypair[1] - - self.pubkey = (self.group, self.G, self.n, self.Q) + # Generator G + self.G = OpenSSL.EC_GROUP_get0_generator(self.group) # Identity O (infinity) self.iO = OpenSSL.EC_POINT_new(self.group) OpenSSL.EC_POINT_set_to_infinity(self.group, self.iO) + if privkey: + assert pubkey + # load both pubkey and privkey from bytes + self._init_privkey(privkey) + self._init_pubkey(pubkey) + elif pubkey: + # load pubkey from bytes + self._init_pubkey(pubkey) + else: + # new keypair + self.d, self.Q = self.ec_gen_keypair() + if not year or not month: + now = time.gmtime() + if now.tm_mon == 12: + self.expiration = Expiration(now.tm_year + 1, 1) + else: + self.expiration = Expiration(now.tm_year, now.tm_mon + 1) + else: + self.expiration = Expiration(year, month) + self.value = Value(value) + + def __del__(self): + OpenSSL.BN_free(self.n) + OpenSSL.BN_CTX_free(self.ctx) + def signer_init(self): """ Init signer """ # Signer: Random integer k - self.k = ECCBlind.ec_get_random(self.group, self.ctx) + self.k = self.ec_get_random() # R = kG self.R = OpenSSL.EC_POINT_new(self.group) OpenSSL.EC_POINT_mul(self.group, self.R, self.k, 0, 0, 0) - return self.R + return self._ec_point_serialize(self.R) def create_signing_request(self, R, msg): """ Requester creates a new signing request """ - self.R = R + self.R = self._ec_point_deserialize(R) + msghash = sha256(msg).digest() # Requester: 3 random blinding factors self.F = OpenSSL.EC_POINT_new(self.group) @@ -128,12 +281,12 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes # F != O while OpenSSL.EC_POINT_cmp(self.group, self.F, self.iO, self.ctx) == 0: - self.a = ECCBlind.ec_get_random(self.group, self.ctx) - self.b = ECCBlind.ec_get_random(self.group, self.ctx) - self.c = ECCBlind.ec_get_random(self.group, self.ctx) + self.a = self.ec_get_random() + self.b = self.ec_get_random() + self.c = self.ec_get_random() # F = b^-1 * R... - self.binv = ECCBlind.ec_invert(self.group, self.b, self.ctx) + self.binv = self.ec_invert(self.b) OpenSSL.EC_POINT_mul(self.group, temp, 0, self.R, self.binv, 0) OpenSSL.EC_POINT_copy(self.F, temp) @@ -147,52 +300,58 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes OpenSSL.EC_POINT_add(self.group, self.F, self.F, temp, 0) # F = (x0, y0) - self.r = ECCBlind.ec_Ftor(self.F, self.group, self.ctx) + self.r = self.ec_Ftor(self.F) # Requester: Blinding (m' = br(m) + a) self.m = OpenSSL.BN_new() - OpenSSL.BN_bin2bn(msg, len(msg), self.m) + OpenSSL.BN_bin2bn(msghash, len(msghash), self.m) self.m_ = OpenSSL.BN_new() OpenSSL.BN_mod_mul(self.m_, self.b, self.r, self.n, self.ctx) OpenSSL.BN_mod_mul(self.m_, self.m_, self.m, self.n, self.ctx) OpenSSL.BN_mod_add(self.m_, self.m_, self.a, self.n, self.ctx) - return self.m_ + return self._bn_serialize(self.m_) def blind_sign(self, m_): """ Signer blind-signs the request """ - self.m_ = m_ + self.m_ = self._bn_deserialize(m_) self.s_ = OpenSSL.BN_new() - OpenSSL.BN_mod_mul(self.s_, self.keypair[0], self.m_, self.n, self.ctx) + OpenSSL.BN_mod_mul(self.s_, self.d, self.m_, self.n, self.ctx) OpenSSL.BN_mod_add(self.s_, self.s_, self.k, self.n, self.ctx) - return self.s_ + OpenSSL.BN_free(self.k) + return self._bn_serialize(self.s_) def unblind(self, s_): """ Requester unblinds the signature """ - self.s_ = s_ + self.s_ = self._bn_deserialize(s_) s = OpenSSL.BN_new() OpenSSL.BN_mod_mul(s, self.binv, self.s_, self.n, self.ctx) OpenSSL.BN_mod_add(s, s, self.c, self.n, self.ctx) + OpenSSL.BN_free(self.a) + OpenSSL.BN_free(self.b) + OpenSSL.BN_free(self.c) self.signature = (s, self.F) - return self.signature + return self._bn_serialize(s) + self._ec_point_serialize(self.F) - def verify(self, msg, signature): + def verify(self, msg, signature, value=1): """ Verify signature with certifier's pubkey """ # convert msg to BIGNUM self.m = OpenSSL.BN_new() - OpenSSL.BN_bin2bn(msg, len(msg), self.m) + msghash = sha256(msg).digest() + OpenSSL.BN_bin2bn(msghash, len(msghash), self.m) # init - s, self.F = signature + s, self.F = (self._bn_deserialize(signature[0:32]), + self._ec_point_deserialize(signature[32:])) if self.r is None: - self.r = ECCBlind.ec_Ftor(self.F, self.group, self.ctx) + self.r = self.ec_Ftor(self.F) lhs = OpenSSL.EC_POINT_new(self.group) rhs = OpenSSL.EC_POINT_new(self.group) @@ -206,5 +365,10 @@ class ECCBlind(object): # pylint: disable=too-many-instance-attributes retval = OpenSSL.EC_POINT_cmp(self.group, lhs, rhs, self.ctx) if retval == -1: raise RuntimeError("EC_POINT_cmp returned an error") - else: - return retval == 0 + elif not self.value.verify(value): + return False + elif not self.expiration.verify(): + return False + elif retval != 0: + return False + return True diff --git a/src/pyelliptic/eccblindchain.py b/src/pyelliptic/eccblindchain.py new file mode 100644 index 00000000..56e8ce2a --- /dev/null +++ b/src/pyelliptic/eccblindchain.py @@ -0,0 +1,52 @@ +""" +Blind signature chain with a top level CA +""" + +from .eccblind import ECCBlind + + +class ECCBlindChain(object): # pylint: disable=too-few-public-methods + """ + # Class for ECC Blind Chain signature functionality + """ + + def __init__(self, ca=None, chain=None): + self.chain = [] + self.ca = [] + if ca: + for i in range(0, len(ca), 35): + self.ca.append(ca[i:i + 35]) + if chain: + self.chain.append(chain[0:35]) + for i in range(35, len(chain), 100): + if len(chain[i:]) == 65: + self.chain.append(chain[i:i + 65]) + else: + self.chain.append(chain[i:i + 100]) + + def verify(self, msg, value): + """Verify a chain provides supplied message and value""" + parent = None + l_ = 0 + for level in self.chain: + l_ += 1 + pubkey = None + signature = None + if len(level) == 100: + pubkey, signature = (level[0:35], level[35:]) + elif len(level) == 35: + if level not in self.ca: + return False + parent = level + continue + else: + signature = level + verifier_obj = ECCBlind(pubkey=parent) + if pubkey: + if not verifier_obj.verify(pubkey, signature, value): + return False + parent = pubkey + else: + return verifier_obj.verify(msg=msg, signature=signature, + value=value) + return None diff --git a/src/pyelliptic/openssl.py b/src/pyelliptic/openssl.py index 68364504..c8c888ed 100644 --- a/src/pyelliptic/openssl.py +++ b/src/pyelliptic/openssl.py @@ -10,6 +10,7 @@ needed openssl functionality in class _OpenSSL. import ctypes from kivy.utils import platform import sys + # pylint: disable=protected-access OpenSSL = None @@ -97,6 +98,10 @@ class _OpenSSL(object): self.BN_free.restype = None self.BN_free.argtypes = [ctypes.c_void_p] + self.BN_clear_free = self._lib.BN_clear_free + self.BN_clear_free.restype = None + self.BN_clear_free.argtypes = [ctypes.c_void_p] + self.BN_num_bits = self._lib.BN_num_bits self.BN_num_bits.restype = ctypes.c_int self.BN_num_bits.argtypes = [ctypes.c_void_p] @@ -105,6 +110,15 @@ class _OpenSSL(object): self.BN_bn2bin.restype = ctypes.c_int self.BN_bn2bin.argtypes = [ctypes.c_void_p, ctypes.c_void_p] + try: + self.BN_bn2binpad = self._lib.BN_bn2binpad + self.BN_bn2binpad.restype = ctypes.c_int + self.BN_bn2binpad.argtypes = [ctypes.c_void_p, ctypes.c_void_p, + ctypes.c_int] + except AttributeError: + # optional, we have a workaround + pass + self.BN_bin2bn = self._lib.BN_bin2bn self.BN_bin2bn.restype = ctypes.c_void_p self.BN_bin2bn.argtypes = [ctypes.c_void_p, ctypes.c_int, @@ -147,6 +161,20 @@ class _OpenSSL(object): ctypes.c_void_p, ctypes.c_void_p] + try: + self.EC_POINT_get_affine_coordinates = \ + self._lib.EC_POINT_get_affine_coordinates + except AttributeError: + # OpenSSL docs say only use this for backwards compatibility + self.EC_POINT_get_affine_coordinates = \ + self._lib.EC_POINT_get_affine_coordinates_GF2m + self.EC_POINT_get_affine_coordinates.restype = ctypes.c_int + self.EC_POINT_get_affine_coordinates.argtypes = [ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p] + self.EC_KEY_set_private_key = self._lib.EC_KEY_set_private_key self.EC_KEY_set_private_key.restype = ctypes.c_int self.EC_KEY_set_private_key.argtypes = [ctypes.c_void_p, @@ -171,6 +199,34 @@ class _OpenSSL(object): ctypes.c_void_p, ctypes.c_void_p] + try: + self.EC_POINT_set_affine_coordinates = \ + self._lib.EC_POINT_set_affine_coordinates + except AttributeError: + # OpenSSL docs say only use this for backwards compatibility + self.EC_POINT_set_affine_coordinates = \ + self._lib.EC_POINT_set_affine_coordinates_GF2m + self.EC_POINT_set_affine_coordinates.restype = ctypes.c_int + self.EC_POINT_set_affine_coordinates.argtypes = [ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p] + + try: + self.EC_POINT_set_compressed_coordinates = \ + self._lib.EC_POINT_set_compressed_coordinates + except AttributeError: + # OpenSSL docs say only use this for backwards compatibility + self.EC_POINT_set_compressed_coordinates = \ + self._lib.EC_POINT_set_compressed_coordinates_GF2m + self.EC_POINT_set_compressed_coordinates.restype = ctypes.c_int + self.EC_POINT_set_compressed_coordinates.argtypes = [ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_void_p, + ctypes.c_int, + ctypes.c_void_p] + self.EC_POINT_new = self._lib.EC_POINT_new self.EC_POINT_new.restype = ctypes.c_void_p self.EC_POINT_new.argtypes = [ctypes.c_void_p] @@ -215,10 +271,6 @@ class _OpenSSL(object): self._lib.ECDH_set_method.argtypes = [ctypes.c_void_p, ctypes.c_void_p] - self.BN_CTX_new = self._lib.BN_CTX_new - self._lib.BN_CTX_new.restype = ctypes.c_void_p - self._lib.BN_CTX_new.argtypes = [] - self.ECDH_compute_key = self._lib.ECDH_compute_key self.ECDH_compute_key.restype = ctypes.c_int self.ECDH_compute_key.argtypes = [ctypes.c_void_p, @@ -401,7 +453,7 @@ class _OpenSSL(object): try: self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC - except: + except Exception: # The above is not compatible with all versions of OSX. self.PKCS5_PBKDF2_HMAC = self._lib.PKCS5_PBKDF2_HMAC_SHA1 @@ -477,13 +529,19 @@ class _OpenSSL(object): self.BN_cmp.argtypes = [ctypes.c_void_p, ctypes.c_void_p] + try: + self.BN_is_odd = self._lib.BN_is_odd + self.BN_is_odd.restype = ctypes.c_int + self.BN_is_odd.argtypes = [ctypes.c_void_p] + except AttributeError: + # OpenSSL 1.1.0 implements this as a function, but earlier + # versions as macro, so we need to workaround + self.BN_is_odd = self.BN_is_odd_compatible + self.BN_bn2dec = self._lib.BN_bn2dec self.BN_bn2dec.restype = ctypes.c_char_p self.BN_bn2dec.argtypes = [ctypes.c_void_p] - self.BN_CTX_free = self._lib.BN_CTX_free - self.BN_CTX_free.argtypes = [ctypes.c_void_p] - self.EC_GROUP_new_by_curve_name = self._lib.EC_GROUP_new_by_curve_name self.EC_GROUP_new_by_curve_name.restype = ctypes.c_void_p self.EC_GROUP_new_by_curve_name.argtypes = [ctypes.c_int] @@ -600,6 +658,16 @@ class _OpenSSL(object): """ return int((self.BN_num_bits(x) + 7) / 8) + def BN_is_odd_compatible(self, x): + """ + returns if BN is odd + we assume big endianness, and that BN is initialised + """ + length = self.BN_num_bytes(x) + data = self.malloc(0, length) + OpenSSL.BN_bn2bin(x, data) + return ord(data[length - 1]) & 1 + def get_cipher(self, name): """ returns the OpenSSL cipher instance @@ -735,7 +803,7 @@ def loadOpenSSL(): try: OpenSSL = _OpenSSL(library) return - except: + except Exception: pass raise Exception( "Couldn't find and load the OpenSSL library. You must install it.") diff --git a/src/shared.py b/src/shared.py index 469af38d..232687cb 100644 --- a/src/shared.py +++ b/src/shared.py @@ -13,7 +13,6 @@ import os import stat import subprocess import sys -import threading from binascii import hexlify from pyelliptic import arithmetic from kivy.utils import platform @@ -27,19 +26,6 @@ from debug import logger from helper_sql import sqlQuery # pylint: disable=logging-format-interpolation -verbose = 1 -# This is obsolete with the change to protocol v3 -# but the singleCleaner thread still hasn't been updated -# so we need this a little longer. -maximumAgeOfAnObjectThatIAmWillingToAccept = 216000 -# Equals 4 weeks. You could make this longer if you want -# but making it shorter would not be advisable because -# there is a very small possibility that it could keep you -# from obtaining a needed pubkey for a period of time. -lengthOfTimeToHoldOnToAllPubkeys = 2419200 -maximumAgeOfNodesThatIAdvertiseToOthers = 10800 # Equals three hours - - myECCryptorObjects = {} MyECSubscriptionCryptorObjects = {} # The key in this dictionary is the RIPE hash which is encoded @@ -48,19 +34,6 @@ myAddressesByHash = {} # The key in this dictionary is the tag generated from the address. myAddressesByTag = {} broadcastSendersForWhichImWatching = {} -printLock = threading.Lock() -statusIconColor = 'red' - -thisapp = None # singleton lock instance - -ackdataForWhichImWatching = {} -# used by API command clientStatus -clientHasReceivedIncomingConnections = False -numberOfMessagesProcessed = 0 -numberOfBroadcastsProcessed = 0 -numberOfPubkeysProcessed = 0 - -maximumLengthOfTimeToBotherResendingMessages = 0 def isAddressInMyAddressBook(address): @@ -279,11 +252,3 @@ def fixSensitiveFilePermissions(filename, hasEnabledKeys): except Exception: logger.exception('Keyfile permissions could not be fixed.') raise - - -def openKeysFile(): - """Open keys file with an external editor""" - if 'linux' in sys.platform: - subprocess.call(["xdg-open", state.appdata + 'keys.dat']) - else: - os.startfile(state.appdata + 'keys.dat') diff --git a/src/shutdown.py b/src/shutdown.py index bba7c870..618dc3aa 100644 --- a/src/shutdown.py +++ b/src/shutdown.py @@ -4,7 +4,6 @@ import queue as Queue import threading import time -import shared import state from debug import logger from helper_sql import sqlQuery, sqlStoredProcedure @@ -80,9 +79,9 @@ def doCleanShutdown(): except Queue.Empty: break - if shared.thisapp.daemon or not state.enableGUI: # ..fixme:: redundant? + if state.thisapp.daemon or not state.enableGUI: logger.info('Clean shutdown complete.') - shared.thisapp.cleanup() + state.thisapp.cleanup() os._exit(0) # pylint: disable=protected-access else: logger.info('Core shutdown complete.') diff --git a/src/state.py b/src/state.py index c8ab8bf4..eeda27c1 100644 --- a/src/state.py +++ b/src/state.py @@ -26,6 +26,9 @@ enableSTDIO = False # enable STDIO threads curses = False sqlReady = False # set to true by sqlTread when ready for processing maximumNumberOfHalfOpenConnections = 0 + +maximumLengthOfTimeToBotherResendingMessages = 0 + invThread = None addrThread = None downloadThread = None @@ -108,3 +111,21 @@ availabe_credit = 0 in_sent_method = False in_search_mode = False + +clientHasReceivedIncomingConnections = False +"""used by API command clientStatus""" + +numberOfMessagesProcessed = 0 +numberOfBroadcastsProcessed = 0 +numberOfPubkeysProcessed = 0 + +statusIconColor = 'red' +""" +GUI status icon color +.. note:: bad style, refactor it +""" + +ackdataForWhichImWatching = {} + +thisapp = None +"""Singleton instance""" diff --git a/src/tests/core.py b/src/tests/core.py index 7b89e5b0..37bba824 100644 --- a/src/tests/core.py +++ b/src/tests/core.py @@ -12,15 +12,18 @@ import time import unittest import knownnodes +import protocol import state from bmconfigparser import BMConfigParser from helper_msgcoding import MsgEncode, MsgDecode from helper_startup import start_proxyconfig from network import asyncore_pollchoose as asyncore +from network.bmproto import BMProto from network.connectionpool import BMConnectionPool -from network.node import Peer +from network.node import Node, Peer from network.tcp import Socks4aBMConnection, Socks5BMConnection, TCPConnection from queues import excQueue +from version import softwareVersion try: import stem.version as stem_version @@ -216,6 +219,29 @@ class TestCore(unittest.TestCase): % peer.host) self.fail('Failed to connect to at least 3 nodes within 360 sec') + @staticmethod + def _decode_msg(data, pattern): + proto = BMProto() + proto.bm_proto_reset() + proto.payload = data[protocol.Header.size:] + return proto.decode_payload_content(pattern) + + def test_version(self): + """check encoding/decoding of the version message""" + # with single stream + msg = protocol.assembleVersionMessage('127.0.0.1', 8444, [1]) + decoded = self._decode_msg(msg, "IQQiiQlsLv") + peer, _, ua, streams = self._decode_msg(msg, "IQQiiQlsLv")[4:] + self.assertEqual(peer, Node(3, '127.0.0.1', 8444)) + self.assertEqual(ua, '/PyBitmessage:' + softwareVersion + '/') + self.assertEqual(streams, [1]) + # with multiple streams + msg = protocol.assembleVersionMessage('127.0.0.1', 8444, [1, 2, 3]) + decoded = self._decode_msg(msg, "IQQiiQlslv") + peer, _, ua = decoded[4:7] + streams = decoded[7:] + self.assertEqual(streams, [1, 2, 3]) + def run(): """Starts all tests defined in this module""" diff --git a/src/tests/test_blindsig.py b/src/tests/test_blindsig.py index b8d0248a..cae16191 100644 --- a/src/tests/test_blindsig.py +++ b/src/tests/test_blindsig.py @@ -3,11 +3,14 @@ Test for ECC blind signatures """ import os import unittest -from ctypes import cast, c_char_p +from hashlib import sha256 from pybitmessage.pyelliptic.eccblind import ECCBlind +from pybitmessage.pyelliptic.eccblindchain import ECCBlindChain from pybitmessage.pyelliptic.openssl import OpenSSL +# pylint: disable=protected-access + class TestBlindSig(unittest.TestCase): """ @@ -19,34 +22,255 @@ class TestBlindSig(unittest.TestCase): # (1) Initialization signer_obj = ECCBlind() point_r = signer_obj.signer_init() + self.assertEqual(len(signer_obj.pubkey()), 35) # (2) Request - requester_obj = ECCBlind(pubkey=signer_obj.pubkey) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) # only 64 byte messages are planned to be used in Bitmessage msg = os.urandom(64) msg_blinded = requester_obj.create_signing_request(point_r, msg) + self.assertEqual(len(msg_blinded), 32) # check - msg_blinded_str = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(msg_blinded)) - OpenSSL.BN_bn2bin(msg_blinded, msg_blinded_str) - self.assertNotEqual(msg, cast(msg_blinded_str, c_char_p).value) + self.assertNotEqual(sha256(msg).digest(), msg_blinded) # (3) Signature Generation signature_blinded = signer_obj.blind_sign(msg_blinded) + assert isinstance(signature_blinded, str) + self.assertEqual(len(signature_blinded), 32) # (4) Extraction signature = requester_obj.unblind(signature_blinded) + assert isinstance(signature, str) + self.assertEqual(len(signature), 65) - # check - signature_blinded_str = OpenSSL.malloc(0, - OpenSSL.BN_num_bytes( - signature_blinded)) - signature_str = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(signature[0])) - OpenSSL.BN_bn2bin(signature_blinded, signature_blinded_str) - OpenSSL.BN_bn2bin(signature[0], signature_str) - self.assertNotEqual(cast(signature_str, c_char_p).value, - cast(signature_blinded_str, c_char_p).value) + self.assertNotEqual(signature, signature_blinded) # (5) Verification - verifier_obj = ECCBlind(pubkey=signer_obj.pubkey) + verifier_obj = ECCBlind(pubkey=signer_obj.pubkey()) self.assertTrue(verifier_obj.verify(msg, signature)) + + def test_is_odd(self): + """Test our implementation of BN_is_odd""" + for _ in range(1024): + obj = ECCBlind() + x = OpenSSL.BN_new() + y = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates( + obj.group, obj.Q, x, y, 0) + self.assertEqual(OpenSSL.BN_is_odd(y), + OpenSSL.BN_is_odd_compatible(y)) + + def test_serialize_ec_point(self): + """Test EC point serialization/deserialization""" + for _ in range(1024): + try: + obj = ECCBlind() + obj2 = ECCBlind() + randompoint = obj.Q + serialized = obj._ec_point_serialize(randompoint) + secondpoint = obj2._ec_point_deserialize(serialized) + x0 = OpenSSL.BN_new() + y0 = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates(obj.group, + randompoint, x0, + y0, obj.ctx) + x1 = OpenSSL.BN_new() + y1 = OpenSSL.BN_new() + OpenSSL.EC_POINT_get_affine_coordinates(obj2.group, + secondpoint, x1, + y1, obj2.ctx) + + self.assertEqual(OpenSSL.BN_cmp(y0, y1), 0) + self.assertEqual(OpenSSL.BN_cmp(x0, x1), 0) + self.assertEqual(OpenSSL.EC_POINT_cmp(obj.group, randompoint, + secondpoint, 0), 0) + finally: + OpenSSL.BN_free(x0) + OpenSSL.BN_free(x1) + OpenSSL.BN_free(y0) + OpenSSL.BN_free(y1) + del obj + del obj2 + + def test_serialize_bn(self): + """Test Bignum serialization/deserialization""" + for _ in range(1024): + obj = ECCBlind() + obj2 = ECCBlind() + randomnum = obj.d + serialized = obj._bn_serialize(randomnum) + secondnum = obj2._bn_deserialize(serialized) + self.assertEqual(OpenSSL.BN_cmp(randomnum, secondnum), 0) + + def test_blind_sig_many(self): + """Test a lot of blind signatures""" + for _ in range(1024): + self.test_blind_sig() + + def test_blind_sig_value(self): + """Test blind signature value checking""" + signer_obj = ECCBlind(value=5) + point_r = signer_obj.signer_init() + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + msg = os.urandom(64) + msg_blinded = requester_obj.create_signing_request(point_r, msg) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + verifier_obj = ECCBlind(pubkey=signer_obj.pubkey()) + self.assertFalse(verifier_obj.verify(msg, signature, value=8)) + + def test_blind_sig_expiration(self): + """Test blind signature expiration checking""" + signer_obj = ECCBlind(year=2020, month=1) + point_r = signer_obj.signer_init() + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + msg = os.urandom(64) + msg_blinded = requester_obj.create_signing_request(point_r, msg) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + verifier_obj = ECCBlind(pubkey=signer_obj.pubkey()) + self.assertFalse(verifier_obj.verify(msg, signature)) + + def test_blind_sig_chain(self): # pylint: disable=too-many-locals + """Test blind signature chain using a random certifier key and a random message""" + + test_levels = 4 + msg = os.urandom(1024) + + ca = ECCBlind() + signer_obj = ca + + output = bytearray() + + for level in range(test_levels): + if not level: + output.extend(ca.pubkey()) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertTrue(verifychain.verify(msg=msg, value=1)) + + def test_blind_sig_chain_wrong_ca(self): # pylint: disable=too-many-locals + """Test blind signature chain with an unlisted ca""" + + test_levels = 4 + msg = os.urandom(1024) + + ca = ECCBlind() + fake_ca = ECCBlind() + signer_obj = fake_ca + + output = bytearray() + + for level in range(test_levels): + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + if not level: + # unlisted CA, but a syntactically valid pubkey + output.extend(fake_ca.pubkey()) + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertFalse(verifychain.verify(msg, 1)) + + def test_blind_sig_chain_wrong_msg(self): # pylint: disable=too-many-locals + """Test blind signature chain with a fake message""" + + test_levels = 4 + msg = os.urandom(1024) + fake_msg = os.urandom(1024) + + ca = ECCBlind() + signer_obj = ca + + output = bytearray() + + for level in range(test_levels): + if not level: + output.extend(ca.pubkey()) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertFalse(verifychain.verify(fake_msg, 1)) + + def test_blind_sig_chain_wrong_intermediary(self): # pylint: disable=too-many-locals + """Test blind signature chain using a fake intermediary pubkey""" + + test_levels = 4 + msg = os.urandom(1024) + wrong_level = 2 + + ca = ECCBlind() + signer_obj = ca + fake_intermediary = ECCBlind() + + output = bytearray() + + for level in range(test_levels): + if not level: + output.extend(ca.pubkey()) + requester_obj = ECCBlind(pubkey=signer_obj.pubkey()) + child_obj = ECCBlind() + point_r = signer_obj.signer_init() + pubkey = child_obj.pubkey() + + if level == test_levels - 1: + msg_blinded = requester_obj.create_signing_request(point_r, + msg) + else: + msg_blinded = requester_obj.create_signing_request(point_r, + pubkey) + signature_blinded = signer_obj.blind_sign(msg_blinded) + signature = requester_obj.unblind(signature_blinded) + if level == wrong_level: + output.extend(fake_intermediary.pubkey()) + elif level != test_levels - 1: + output.extend(pubkey) + output.extend(signature) + signer_obj = child_obj + verifychain = ECCBlindChain(ca=ca.pubkey(), chain=str(output)) + self.assertFalse(verifychain.verify(msg, 1)) diff --git a/src/tests/test_crypto.py b/src/tests/test_crypto.py index b7eb7177..b53105cb 100644 --- a/src/tests/test_crypto.py +++ b/src/tests/test_crypto.py @@ -6,6 +6,7 @@ import hashlib import unittest from abc import ABCMeta, abstractmethod from binascii import hexlify, unhexlify +from pybitmessage.pyelliptic import arithmetic try: from Crypto.Hash import RIPEMD @@ -20,8 +21,13 @@ sample_pubsigningkey = unhexlify( sample_pubencryptionkey = unhexlify( '044597d59177fc1d89555d38915f581b5ff2286b39d022ca0283d2bdd5c36be5d3c' 'e7b9b97792327851a562752e4b79475d1f51f5a71352482b241227f45ed36a9') - +sample_privatesigningkey = \ + '93d0b61371a54b53df143b954035d612f8efa8a3ed1cf842c2186bfd8f876665' +sample_privateencryptionkey = \ + '4b0b73a54e19b059dc274ab69df095fe699f43b17397bca26fdf40f4d7400a3a' sample_ripe = '003cd097eb7f35c87b5dc8b4538c22cb55312a9f' +# stream: 1, version: 2 +sample_address = 'BM-onkVu1KKL2UaUss5Upg9vXmqd3esTmV79' _sha = hashlib.new('sha512') _sha.update(sample_pubsigningkey + sample_pubencryptionkey) @@ -59,3 +65,34 @@ class TestCrypto(RIPEMD160TestCase, unittest.TestCase): @staticmethod def _hashdigest(data): return RIPEMD.RIPEMD160Hash(data).digest() + + +class TestAddresses(unittest.TestCase): + """Test addresses manipulations""" + def test_privtopub(self): + """Generate public keys and check the result""" + self.assertEqual( + arithmetic.privtopub(sample_privatesigningkey), + hexlify(sample_pubsigningkey) + ) + self.assertEqual( + arithmetic.privtopub(sample_privateencryptionkey), + hexlify(sample_pubencryptionkey) + ) + + def test_address(self): + """Create address and check the result""" + from pybitmessage import addresses + from pybitmessage.fallback import RIPEMD160Hash + + sha = hashlib.new('sha512') + sha.update(sample_pubsigningkey + sample_pubencryptionkey) + ripe_hash = RIPEMD160Hash(sha.digest()).digest() + self.assertEqual(ripe_hash, unhexlify(sample_ripe)) + + self.assertEqual( + addresses.encodeAddress(2, 1, ripe_hash), sample_address) + + self.assertEqual( + addresses.decodeAddress(sample_address), + ('success', 2, 1, ripe_hash)) diff --git a/src/tests/test_openssl.py b/src/tests/test_openssl.py new file mode 100644 index 00000000..e947fff3 --- /dev/null +++ b/src/tests/test_openssl.py @@ -0,0 +1,54 @@ +""" +Test if OpenSSL is working correctly +""" +import unittest + +from pybitmessage.pyelliptic.openssl import OpenSSL + +try: + OpenSSL.BN_bn2binpad + have_pad = True +except AttributeError: + have_pad = None + + +class TestOpenSSL(unittest.TestCase): + """ + Test cases for OpenSSL + """ + def test_is_odd(self): + """Test BN_is_odd implementation""" + ctx = OpenSSL.BN_CTX_new() + a = OpenSSL.BN_new() + group = OpenSSL.EC_GROUP_new_by_curve_name( + OpenSSL.get_curve("secp256k1")) + OpenSSL.EC_GROUP_get_order(group, a, ctx) + + bad = 0 + for _ in range(1024): + OpenSSL.BN_rand(a, OpenSSL.BN_num_bits(a), 0, 0) + if not OpenSSL.BN_is_odd(a) == OpenSSL.BN_is_odd_compatible(a): + bad += 1 + self.assertEqual(bad, 0) + + @unittest.skipUnless(have_pad, 'Skipping OpenSSL pad test') + def test_padding(self): + """Test an alternatie implementation of bn2binpad""" + + ctx = OpenSSL.BN_CTX_new() + a = OpenSSL.BN_new() + n = OpenSSL.BN_new() + group = OpenSSL.EC_GROUP_new_by_curve_name( + OpenSSL.get_curve("secp256k1")) + OpenSSL.EC_GROUP_get_order(group, n, ctx) + + bad = 0 + for _ in range(1024): + OpenSSL.BN_rand(a, OpenSSL.BN_num_bits(n), 0, 0) + b = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(n)) + c = OpenSSL.malloc(0, OpenSSL.BN_num_bytes(a)) + OpenSSL.BN_bn2binpad(a, b, OpenSSL.BN_num_bytes(n)) + OpenSSL.BN_bn2bin(a, c) + if b.raw != c.raw.rjust(OpenSSL.BN_num_bytes(n), chr(0)): + bad += 1 + self.assertEqual(bad, 0) diff --git a/src/threads.py b/src/threads.py index b7471508..ac8bf7a6 100644 --- a/src/threads.py +++ b/src/threads.py @@ -40,7 +40,9 @@ else: threading.Thread._Thread__bootstrap = _thread_name_hack +printLock = threading.Lock() + __all__ = [ "addressGenerator", "objectProcessor", "singleCleaner", "singleWorker", - "sqlThread" + "sqlThread", "printLock" ]