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
|
# -*- coding: utf-8 -*-
# systray.py
# Copyright (C) 2017 LEAP Encryption Acess Project
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
PyQt5 Systray Integration
"""
from leap.common.events import client as leap_events
from leap.common.events import catalog
from leap.bitmask.gui import app_rc
import platform
if platform.system() == 'Windows':
from PySide.QtGui import QDialog
else:
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import QMenu
from PyQt5.QtWidgets import QMessageBox
from PyQt5.QtWidgets import QSystemTrayIcon
from PyQt5.QtWidgets import QWidget
# TODO do switch based on theme
if platform.system() == 'Darwin':
TRAY_ICONS = (':/mac/wait.png',
':/mac/on.png',
':/mac/off.png')
else:
TRAY_ICONS = (':/black/22/wait.png',
':/black/22/on.png',
':/black/22/off.png')
# TODO I think this does not need QDialog
class WithTrayIcon(QWidget):
user_closed = False
def setupSysTray(self):
self._createIcons()
self._createActions()
self._createTrayIcon()
self.trayIcon.activated.connect(self.iconActivated)
self.setVPNStatus('off')
self.setUpEventListener()
self.trayIcon.show()
def setVPNStatus(self, status):
seticon = self.trayIcon.setIcon
settip = self.trayIcon.setToolTip
# XXX this is an oversimplification, see #9131
# the simple state for failure is off too, for now.
if status == 'off':
seticon(self.ICON_OFF)
settip('VPN: Off')
elif status == 'on':
seticon(self.ICON_ON)
settip('VPN: On')
elif status == 'starting':
seticon(self.ICON_WAIT)
settip('VPN: Starting')
elif status == 'stopping':
seticon(self.ICON_WAIT)
settip('VPN: Stopping')
def setUpEventListener(self):
leap_events.register(
catalog.VPN_STATUS_CHANGED,
self._handle_vpn_event)
def _handle_vpn_event(self, *args):
status = None
if len(args) > 1:
status = args[1]
self.setVPNStatus(status.lower())
def _createIcons(self):
self.ICON_WAIT = QIcon(QPixmap(TRAY_ICONS[0]))
self.ICON_ON = QIcon(QPixmap(TRAY_ICONS[1]))
self.ICON_OFF = QIcon(QPixmap(TRAY_ICONS[2]))
def _createActions(self):
self.quitAction = QAction(
"&Quit", self,
triggered=self.closeFromSystray)
def iconActivated(self, reason):
# can use .Trigger also for single click
if reason in (QSystemTrayIcon.DoubleClick, ):
self.showNormal()
def closeFromSystray(self):
self.user_closed = True
self.close()
def _createTrayIcon(self):
self.trayIconMenu = QMenu(self)
self.trayIconMenu.addAction(self.quitAction)
self.trayIcon = QSystemTrayIcon(self)
self.trayIcon.setContextMenu(self.trayIconMenu)
def closeEvent(self, event):
if self.trayIcon.isVisible() and not self.user_closed:
QMessageBox.information(
self, "Bitmask",
"Bitmask will minimize to the system tray. "
"You can choose 'Quit' from the menu with a "
"right click on the icon, and restore the window "
"with a double click.")
self.hide()
if not self.user_closed:
event.ignore()
|