From dda530ca07788986d817e9130c206df267e250dc Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 31 Jul 2013 22:25:34 +0100 Subject: [PATCH 1/3] Set a maximum frequency for playing sounds --- src/bitmessageqt/__init__.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 96db1f92..033ce61f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -34,7 +34,7 @@ import platform import debug from debug import logger import subprocess - +import datetime try: from PyQt4 import QtCore, QtGui @@ -65,6 +65,12 @@ class MyForm(QtGui.QMainWindow): SOUND_DISCONNECTED = 4 SOUND_CONNECTION_GREEN = 5 + # the last time that a message arrival sound was played + lastSoundTime = datetime.datetime.now() - datetime.timedelta(days=1) + + # the maximum frequency of message sounds in seconds + maxSoundFrequencySec = 60 + str_broadcast_subscribers = '[Broadcast subscribers]' str_chan = '[chan]' @@ -429,7 +435,6 @@ class MyForm(QtGui.QMainWindow): self.rerenderComboBoxSendFrom() - # Show or hide the application window after clicking an item within the # tray icon or, on Windows, the try icon itself. def appIndicatorShowOrHideWindow(self): @@ -985,13 +990,26 @@ class MyForm(QtGui.QMainWindow): # play a sound def playSound(self, category, label): soundFilename = None + play = True if label is not None: - # does a sound file exist for this particular contact? + # Does a sound file exist for this particular contact? if (os.path.isfile(shared.appdata + 'sounds/' + label + '.wav') or os.path.isfile(shared.appdata + 'sounds/' + label + '.mp3')): soundFilename = shared.appdata + 'sounds/' + label + # Avoid making sounds more frequently than the threshold. + # This suppresses playing sounds repeatedly when there + # are many new messages + if (soundFilename is None and + category is not self.SOUND_CONNECTED and + category is not self.SOUND_DISCONNECTED and + category is not self.SOUND_CONNECTION_GREEN): + dt = datetime.datetime.now() - self.lastSoundTime + self.lastSoundTime = datetime.datetime.now() + if dt.total_seconds() < self.maxSoundFrequencySec: + play = False + if soundFilename is None: if category is self.SOUND_KNOWN: soundFilename = shared.appdata + 'sounds/known' @@ -1002,9 +1020,9 @@ class MyForm(QtGui.QMainWindow): elif category is self.SOUND_DISCONNECTED: soundFilename = shared.appdata + 'sounds/disconnected' elif category is self.SOUND_CONNECTION_GREEN: - soundFilename = shared.appdata + 'sounds/green' + soundFilename = shared.appdata + 'sounds/green' - if soundFilename is not None: + if soundFilename is not None and play is True: # if not wav then try mp3 format if not os.path.isfile(soundFilename + '.wav'): soundFilename = soundFilename + '.mp3' From b06ee336adfe53b77572c045fe3ebe730e843e96 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 1 Aug 2013 09:58:30 +0100 Subject: [PATCH 2/3] Time is reset only when a sound is played #355 --- src/bitmessageqt/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 033ce61f..86d48f0f 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -1006,7 +1006,6 @@ class MyForm(QtGui.QMainWindow): category is not self.SOUND_DISCONNECTED and category is not self.SOUND_CONNECTION_GREEN): dt = datetime.datetime.now() - self.lastSoundTime - self.lastSoundTime = datetime.datetime.now() if dt.total_seconds() < self.maxSoundFrequencySec: play = False @@ -1023,6 +1022,9 @@ class MyForm(QtGui.QMainWindow): soundFilename = shared.appdata + 'sounds/green' if soundFilename is not None and play is True: + # record the last time that a sound was played + self.lastSoundTime = datetime.datetime.now() + # if not wav then try mp3 format if not os.path.isfile(soundFilename + '.wav'): soundFilename = soundFilename + '.mp3' From 7606106096d21d95b36b688f94e7b1dbdd75b104 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Thu, 1 Aug 2013 14:48:01 +0100 Subject: [PATCH 3/3] Tidying --- src/bitmessageqt/__init__.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index 86d48f0f..1d115af1 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -987,11 +987,24 @@ class MyForm(QtGui.QMainWindow): # update the menu entries self.ubuntuMessagingMenuUnread(drawAttention) + # returns true if the given sound category is a connection sound + # rather than a received message sound + def isConnectionSound(self, category): + if (category is self.SOUND_CONNECTED or + category is self.SOUND_DISCONNECTED or + category is self.SOUND_CONNECTION_GREEN): + return True + return False + # play a sound def playSound(self, category, label): + # filename of the sound to be played soundFilename = None + + # whether to play a sound or not play = True + # if the address had a known label in the address book if label is not None: # Does a sound file exist for this particular contact? if (os.path.isfile(shared.appdata + 'sounds/' + label + '.wav') or @@ -1002,28 +1015,34 @@ class MyForm(QtGui.QMainWindow): # This suppresses playing sounds repeatedly when there # are many new messages if (soundFilename is None and - category is not self.SOUND_CONNECTED and - category is not self.SOUND_DISCONNECTED and - category is not self.SOUND_CONNECTION_GREEN): + not self.isConnectionSound(category)): + # elapsed time since the last sound was played dt = datetime.datetime.now() - self.lastSoundTime + # suppress sounds which are more frequent than the threshold if dt.total_seconds() < self.maxSoundFrequencySec: play = False if soundFilename is None: + # the sound is for an address which exists in the address book if category is self.SOUND_KNOWN: soundFilename = shared.appdata + 'sounds/known' + # the sound is for an unknown address elif category is self.SOUND_UNKNOWN: soundFilename = shared.appdata + 'sounds/unknown' + # initial connection sound elif category is self.SOUND_CONNECTED: soundFilename = shared.appdata + 'sounds/connected' + # disconnected sound elif category is self.SOUND_DISCONNECTED: soundFilename = shared.appdata + 'sounds/disconnected' + # sound when the connection status becomes green elif category is self.SOUND_CONNECTION_GREEN: soundFilename = shared.appdata + 'sounds/green' if soundFilename is not None and play is True: - # record the last time that a sound was played - self.lastSoundTime = datetime.datetime.now() + if not self.isConnectionSound(category): + # record the last time that a received message sound was played + self.lastSoundTime = datetime.datetime.now() # if not wav then try mp3 format if not os.path.isfile(soundFilename + '.wav'):