| 1263 | |
| 1264 | # play a sound |
| 1265 | def playSound(self, category, label): |
| 1266 | # filename of the sound to be played |
| 1267 | soundFilename = None |
| 1268 | |
| 1269 | def _choose_ext(basename): |
| 1270 | for ext in sound.extensions: |
| 1271 | if os.path.isfile(os.extsep.join([basename, ext])): |
| 1272 | return os.extsep + ext |
| 1273 | |
| 1274 | # if the address had a known label in the address book |
| 1275 | if label: |
| 1276 | # Does a sound file exist for this particular contact? |
| 1277 | soundFilename = state.appdata + 'sounds/' + label |
| 1278 | ext = _choose_ext(soundFilename) |
| 1279 | if not ext: |
| 1280 | category = sound.SOUND_KNOWN |
| 1281 | soundFilename = None |
| 1282 | |
| 1283 | if soundFilename is None: |
| 1284 | # Avoid making sounds more frequently than the threshold. |
| 1285 | # This suppresses playing sounds repeatedly when there |
| 1286 | # are many new messages |
| 1287 | if not sound.is_connection_sound(category): |
| 1288 | # elapsed time since the last sound was played |
| 1289 | dt = datetime.now() - self.lastSoundTime |
| 1290 | # suppress sounds which are more frequent than the threshold |
| 1291 | if dt.total_seconds() < self.maxSoundFrequencySec: |
| 1292 | return |
| 1293 | |
| 1294 | # the sound is for an address which exists in the address book |
| 1295 | if category is sound.SOUND_KNOWN: |
| 1296 | soundFilename = state.appdata + 'sounds/known' |
| 1297 | # the sound is for an unknown address |
| 1298 | elif category is sound.SOUND_UNKNOWN: |
| 1299 | soundFilename = state.appdata + 'sounds/unknown' |
| 1300 | # initial connection sound |
| 1301 | elif category is sound.SOUND_CONNECTED: |
| 1302 | soundFilename = state.appdata + 'sounds/connected' |
| 1303 | # disconnected sound |
| 1304 | elif category is sound.SOUND_DISCONNECTED: |
| 1305 | soundFilename = state.appdata + 'sounds/disconnected' |
| 1306 | # sound when the connection status becomes green |
| 1307 | elif category is sound.SOUND_CONNECTION_GREEN: |
| 1308 | soundFilename = state.appdata + 'sounds/green' |
| 1309 | |
| 1310 | if soundFilename is None: |
| 1311 | logger.warning("Probably wrong category number in playSound()") |
| 1312 | return |
| 1313 | |
| 1314 | if not sound.is_connection_sound(category): |
| 1315 | # record the last time that a received message sound was played |
| 1316 | self.lastSoundTime = datetime.now() |
| 1317 | |
| 1318 | try: # try already known format |
| 1319 | soundFilename += ext |
| 1320 | except (TypeError, NameError): |
| 1321 | ext = _choose_ext(soundFilename) |
| 1322 | if not ext: |