toxygen/toxygen/notifications.py

72 lines
2.0 KiB
Python
Raw Normal View History

2017-04-13 18:22:46 +02:00
from PyQt5 import QtCore, QtWidgets
2016-02-25 09:32:40 +01:00
from util import curr_directory
2016-04-10 14:27:35 +02:00
import wave
import pyaudio
2016-02-24 19:38:36 +01:00
SOUND_NOTIFICATION = {
'MESSAGE': 0,
'FRIEND_CONNECTION_STATUS': 1,
'FILE_TRANSFER': 2
}
2016-04-01 18:13:01 +02:00
def tray_notification(title, text, tray, window):
"""
2016-04-02 20:31:59 +02:00
Show tray notification and activate window icon
NOTE: different behaviour on different OS
2016-04-01 18:13:01 +02:00
:param title: Name of user who sent message or file
:param text: text of message or file info
:param tray: ref to tray icon
:param window: main window
"""
2017-04-13 18:22:46 +02:00
if QtWidgets.QSystemTrayIcon.isSystemTrayAvailable():
2016-02-25 09:32:40 +01:00
if len(text) > 30:
2016-02-25 21:40:00 +01:00
text = text[:27] + '...'
2017-04-13 18:22:46 +02:00
tray.showMessage(title, text, QtWidgets.QSystemTrayIcon.NoIcon, 3000)
2017-04-11 20:10:03 +02:00
QtWidgets.QApplication.alert(window, 0)
2016-04-01 18:13:01 +02:00
def message_clicked():
window.setWindowState(window.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
window.activateWindow()
2017-04-13 18:22:46 +02:00
tray.messageClicked.connect(message_clicked)
2016-04-01 18:13:01 +02:00
2016-06-22 13:35:22 +02:00
class AudioFile:
2016-04-10 14:27:35 +02:00
chunk = 1024
def __init__(self, fl):
self.wf = wave.open(fl, 'rb')
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format=self.p.get_format_from_width(self.wf.getsampwidth()),
channels=self.wf.getnchannels(),
rate=self.wf.getframerate(),
2016-10-27 23:55:34 +02:00
output=True)
2016-04-10 14:27:35 +02:00
def play(self):
data = self.wf.readframes(self.chunk)
while data:
self.stream.write(data)
data = self.wf.readframes(self.chunk)
def close(self):
self.stream.close()
self.p.terminate()
def sound_notification(t):
2016-04-02 20:31:59 +02:00
"""
Plays sound notification
:param t: type of notification
"""
if t == SOUND_NOTIFICATION['MESSAGE']:
f = curr_directory() + '/sounds/message.wav'
2016-03-18 14:50:32 +01:00
elif t == SOUND_NOTIFICATION['FILE_TRANSFER']:
f = curr_directory() + '/sounds/file.wav'
else:
f = curr_directory() + '/sounds/contact.wav'
2016-04-10 14:27:35 +02:00
a = AudioFile(f)
a.play()
a.close()