forked from openairplay/openairplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·306 lines (245 loc) · 13.1 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env python3
# Copyright (C) 2015-2016 Ben Klein. All rights reserved.
#
# This application is licensed under the GNU GPLv3 License, included with
# this application source.
import sys
global DEBUG
DEBUG = True
if DEBUG:
print("Debugging enabled.")
print("Called with system args: " + str(sys.argv))
print("Python version: " + sys.version)
# Qt GUI stuff
try:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QSettings
except ImportError:
print("There was an error importing the Qt python3 libraries,")
print("These are required by to operate this program.")
print("If you are on Ubuntu/Debian, they should be available via APT.")
sys.exit("Could not import Python3 Qt Libraries.")
# Airplay Things:
try:
import discovery
import airplay
except:
sys.exit("Could not import own classes.")
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.settings = QSettings('open-airplay')
# Establishes a hook on our system settings.
# http://pyqt.sourceforge.net/Docs/PyQt4/pyqt_qsettings.html
# Place items in our window.
self.createIconGroupBox() # Tray Icon Settings
self.createMessageGroupBox() # Test notification group
self.createDeviceListGroupBox() # Airplay server selection
# Set the iconlabel to it's minimum width without scollbaring.
self.iconLabel.setMinimumWidth(self.durationLabel.sizeHint().width())
# Create action groups to put actionable items into.
self.createActions()
self.createTrayIcon()
# Attach clicks on things to actual functions
self.showMessageButton.clicked.connect(self.showMessage)
self.showIconCheckBox.toggled.connect(self.trayIconVisible)
self.systrayClosePromptCheckBox.toggled.connect(self.setSystrayClosePrompt)
self.iconComboBox.currentIndexChanged.connect(self.setIcon)
self.trayIcon.messageClicked.connect(self.messageClicked)
self.trayIcon.activated.connect(self.iconActivated)
# Finally add the GUI item groupings we made to the layout and init it.
mainLayout = QtWidgets.QVBoxLayout()
mainLayout.addWidget(self.iconGroupBox)
mainLayout.addWidget(self.deviceListGroupBox)
mainLayout.addWidget(self.messageGroupBox)
self.setLayout(mainLayout)
# Set our System Tray Presence
self.iconComboBox.setCurrentIndex(1)
self.trayIcon.show()
self.trayIcon.setToolTip("OpenAirplay")
# Set our basic window things.
self.setWindowTitle("OpenAirplay Settings")
self.resize(400, 300)
# If the user chose not to show the system tray icon:
if self.settings.value('systrayicon', type=bool) is False:
print("The user chose not to show the system tray icon.")
self.trayIconVisible(False)
# Setup stuff to poll available receivers every 3 seconds.
self.oldReceiverList = []
self.timer=QtCore.QTimer()
self.timer.start(3000)
self.timer.timeout.connect(self.updateReceivers)
# Start discovery of airplay receivers:
if DEBUG:
print("Starting discovery service...")
discovery.start()
def setVisible(self, visible):
# When we want to 'disappear' into the system tray.
self.minimizeAction.setEnabled(visible)
#self.maximizeAction.setEnabled(not self.isMaximized())
self.restoreAction.setEnabled(self.isMaximized() or not visible)
super(Window, self).setVisible(visible)
def closeEvent(self, event):
# When someone clicks to close the window, not the tray icon.
if self.trayIcon.isVisible():
if self.settings.value('promptOnClose_systray', type=bool):
print("The program is returning to the system tray, user notified.")
QtWidgets.QMessageBox.information(self, "Systray",
"The program will keep running in the system tray. \
To terminate the program, choose <b>Quit</b> in \
the menu of the system tray airplay icon.")
else:
print("Program returned to system tray, user chose not to be notified.")
self.hide()
event.ignore()
print("Closing to System Tray")
else:
print("Tray Icon not visible, quitting.")
self.quit("Exit: No system tray instance to close to.")
def setIcon(self, index):
# Sets the selected icon in the tray and taskbar.
icon = self.iconComboBox.itemIcon(index)
self.trayIcon.setIcon(icon)
self.setWindowIcon(icon)
def setSystrayClosePrompt(self, preference):
print("Prompt on close is now " + str(preference))
self.settings.setValue('promptOnClose_systray', preference)
def trayIconVisible(self, preference):
self.trayIcon.setVisible(preference)
self.settings.setValue('systrayicon', preference)
def iconActivated(self, reason):
if reason in (QtWidgets.QSystemTrayIcon.Trigger, QtWidgets.QSystemTrayIcon.DoubleClick):
self.iconComboBox.setCurrentIndex(
(self.iconComboBox.currentIndex() + 1)
% self.iconComboBox.count())
elif reason == QtWidgets.QSystemTrayIcon.MiddleClick:
self.showMessage()
def showMessage(self):
# Show the message that was typed in the boxes
icon = QtWidgets.QSystemTrayIcon.MessageIcon(
self.typeComboBox.itemData(self.typeComboBox.currentIndex()))
self.trayIcon.showMessage(self.titleEdit.text(),
self.bodyEdit.toPlainText(), icon,
self.durationSpinBox.value() * 1000)
def messageClicked(self):
# In the case that someone clicks on the notification popup (impossible on Ubuntu Unity)
QtWidgets.QMessageBox.information(None, "OpenAirplay Help", "If you need help with OpenAirplay, "
"see the Github page to file bug reports or see further documentation and help.")
def updateReceivers(self):
if list(set(discovery.airplayReceivers) - set(self.oldReceiverList)) != []:
# The new list has items oldReceiverList doesn't!
for item in list(set(discovery.airplayReceivers) - set(self.oldReceiverList)):
self.oldReceiverList.append(item)
print("Adding device: " + item)
# Convert item to string to remove the excess info
item = QtWidgets.QListWidgetItem(str(item).replace("._airplay._tcp.local.", ""))
self.deviceSelectList.addItem(item)
if list(set(self.oldReceiverList) - set(discovery.airplayReceivers)) != []:
# Items have been removed from the list!
for item in list(set(self.oldReceiverList) - set(discovery.airplayReceivers)):
self.oldReceiverList.remove(item)
print("Removed device: " + item)
items = self.deviceSelectList.findItems(item, QtCore.Qt.MatchExactly)
for x in items:
self.deviceSelectList.takeItem(self.deviceSelectList.row(x))
def createIconGroupBox(self): # Add the SysTray preferences window grouping
self.iconGroupBox = QtWidgets.QGroupBox("Tray Icon")
self.iconLabel = QtWidgets.QLabel("Icon:")
self.iconComboBox = QtWidgets.QComboBox()
self.iconComboBox.addItem(QtGui.QIcon('images/Airplay-Light'), "Black Icon")
self.iconComboBox.addItem(QtGui.QIcon('images/Airplay-Dark'), "White Icon")
self.showIconCheckBox = QtWidgets.QCheckBox("Show tray icon")
self.showIconCheckBox.setChecked(self.settings.value('systrayicon', type=bool))
print("Got systrayicon from settings:" + str(self.settings.value('systrayicon', type=bool)))
self.systrayClosePromptCheckBox = QtWidgets.QCheckBox("Systray Close warning")
self.systrayClosePromptCheckBox.setChecked(self.settings.value('promptOnClose_systray', type=bool))
print("Got promptOnClose_systray from settings:" + str(self.settings.value('promptOnClose_systray', type=bool)))
iconLayout = QtWidgets.QHBoxLayout()
iconLayout.addWidget(self.iconLabel)
iconLayout.addWidget(self.iconComboBox)
iconLayout.addStretch()
iconLayout.addWidget(self.showIconCheckBox)
iconLayout.addWidget(self.systrayClosePromptCheckBox)
self.iconGroupBox.setLayout(iconLayout)
# Creates the device selection list.
def createDeviceListGroupBox(self):
self.deviceListGroupBox = QtWidgets.QGroupBox("Airplay to")
self.deviceSelectList = QtWidgets.QListWidget()
deviceSelectListNoDisplayItem = QtWidgets.QListWidgetItem("No display.")
self.deviceSelectList.addItem(deviceSelectListNoDisplayItem)
# layout
deviceListLayout = QtWidgets.QHBoxLayout()
deviceListLayout.addWidget(self.deviceSelectList)
self.deviceListGroupBox.setLayout(deviceListLayout)
def createMessageGroupBox(self): # Add the message test GUI window grouping.
self.messageGroupBox = QtWidgets.QGroupBox("Balloon Message Test:")
typeLabel = QtWidgets.QLabel("Type:")
self.typeComboBox = QtWidgets.QComboBox()
self.typeComboBox.addItem("None", QtWidgets.QSystemTrayIcon.NoIcon)
#self.typeComboBox.addItem(self.style().standardIcon(
# QtWidgets.QStyle.SP_MessageBoxInformation), "Information", #QtWidgets.QSystemTrayIcon.Information)
#self.typeComboBox.addItem(self.style().standardIcon(
# QtWidgets.QStyle.SP_MessageBoxWarning), "Warning", #QtWidgets.QSystemTrayIcon.Warning)
#self.typeComboBox.addItem(self.style().standardIcon(
# QtWidgets.QStyle.SP_MessageBoxCritical), "Critical", #QtWidgets.QSystemTrayIcon.Critical)
self.typeComboBox.addItem("Information", QtWidgets.QSystemTrayIcon.Information)
self.typeComboBox.addItem("Warning", QtWidgets.QSystemTrayIcon.Information)
self.typeComboBox.addItem("Critical", QtWidgets.QSystemTrayIcon.Information)
self.typeComboBox.setCurrentIndex(1)
self.durationLabel = QtWidgets.QLabel("Duration:")
self.durationSpinBox = QtWidgets.QSpinBox()
self.durationSpinBox.setRange(2, 15)
self.durationSpinBox.setSuffix("s")
self.durationSpinBox.setValue(5)
durationWarningLabel = QtWidgets.QLabel("(some systems might ignore this hint)")
durationWarningLabel.setIndent(10)
titleLabel = QtWidgets.QLabel("Title:")
self.titleEdit = QtWidgets.QLineEdit("Cannot connect to network")
bodyLabel = QtWidgets.QLabel("Body:")
self.bodyEdit = QtWidgets.QTextEdit()
self.bodyEdit.setPlainText("Don't believe me. Honestly, I don't have a clue.")
self.showMessageButton = QtWidgets.QPushButton("Show Message")
self.showMessageButton.setDefault(True)
messageLayout = QtWidgets.QGridLayout()
messageLayout.addWidget(typeLabel, 0, 0)
messageLayout.addWidget(self.typeComboBox, 0, 1, 1, 2)
messageLayout.addWidget(self.durationLabel, 1, 0)
messageLayout.addWidget(self.durationSpinBox, 1, 1)
messageLayout.addWidget(durationWarningLabel, 1, 2, 1, 3)
messageLayout.addWidget(titleLabel, 2, 0)
messageLayout.addWidget(self.titleEdit, 2, 1, 1, 4)
messageLayout.addWidget(bodyLabel, 3, 0)
messageLayout.addWidget(self.bodyEdit, 3, 1, 2, 4)
messageLayout.addWidget(self.showMessageButton, 5, 4)
messageLayout.setColumnStretch(3, 1)
messageLayout.setRowStretch(4, 1)
self.messageGroupBox.setLayout(messageLayout)
def createActions(self): # Create Actions that can be taken from the System Tray Icon
self.minimizeAction = QtWidgets.QAction("Mi&nimize", self, triggered=self.hide)
# Application is not the kind to be maximized
#self.maximizeAction = QtWidgets.QAction("Ma&ximize", self, triggered=self.showMaximized)
self.restoreAction = QtWidgets.QAction("&Restore", self, triggered=self.showNormal)
self.quitAction = QtWidgets.QAction("&Quit", self, triggered=QtWidgets.qApp.quit)
def createTrayIcon(self):
self.trayIconMenu = QtWidgets.QMenu()
self.trayIconMenu.addAction(self.minimizeAction)
#self.trayIconMenu.addAction(self.maximizeAction)
self.trayIconMenu.addAction(self.restoreAction)
self.trayIconMenu.addSeparator()
self.trayIconMenu.addAction(self.quitAction)
self.trayIcon = QtWidgets.QSystemTrayIcon(self)
self.trayIcon.setContextMenu(self.trayIconMenu)
def quit(self, reason):
del self.settings
#discovery.stop()
sys.exit(reason)
if __name__ == '__main__':
app = QtWidgets.QApplication(['Open Airplay'])
if not QtWidgets.QSystemTrayIcon.isSystemTrayAvailable():
QtWidgets.QMessageBox.critical(None, "Systray", "I couldn't detect any system tray on this system.")
sys.exit(1)
QtWidgets.QApplication.setQuitOnLastWindowClosed(False)
window = Window()
window.show()
# After teh progreem endz:
sys.exit(app.exec_()) # Goodbye World