This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
openvpn-indicator.py
executable file
·100 lines (84 loc) · 2.81 KB
/
openvpn-indicator.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
#!/usr/bin/env python
# openvpn-indicator v1.0
# GTK3 indicator for Ubuntu Unity
from gi.repository import Gtk, GLib
try:
from gi.repository import AppIndicator3 as AppIndicator
except:
from gi.repository import AppIndicator
import os, sys
FREQUENCY = 60 # seconds
PATH = os.path.abspath(__file__).split("/")
DELIMITER = "/"
BASEPATH = DELIMITER.join(PATH[0:len(PATH)-1])+"/pics/"
class OpenVpnIndicator:
def __init__(self):
self.ind = AppIndicator.Indicator.new(
"openvpn-indicator",
"",
AppIndicator.IndicatorCategory.OTHER)
self.ind.set_status(AppIndicator.IndicatorStatus.ACTIVE)
self.icon_red()
self.ind.set_attention_icon(BASEPATH+'green.png')
self.setup_menu()
def icon_orange(self):
self.ind.set_icon(BASEPATH+'orange.png')
self.ind.set_status(AppIndicator.IndicatorStatus.ACTIVE)
def icon_red(self):
self.ind.set_icon(BASEPATH+'red.png')
self.ind.set_status(AppIndicator.IndicatorStatus.ACTIVE)
def icon_green(self):
self.ind.set_status(AppIndicator.IndicatorStatus.ATTENTION)
def setup_menu(self):
self.menu = Gtk.Menu()
vpnStatus = os.system("service openvpn status")
self.isRunning = vpnStatus == 0
# Start
if(not self.isRunning):
item = Gtk.MenuItem()
item.set_label("Start")
item.connect("activate", self.handler_menu_start)
item.show()
self.menu.append(item)
# Stop
if(self.isRunning):
item = Gtk.MenuItem()
item.set_label("Stop")
item.connect("activate", self.handler_menu_stop)
item.show()
self.menu.append(item)
# Restart
if(self.isRunning):
item = Gtk.MenuItem()
item.set_label("Restart")
item.connect("activate", self.handler_menu_restart)
item.show()
self.menu.append(item)
self.menu.show()
self.ind.set_menu(self.menu)
def handler_menu_start(self, evt):
self.icon_orange()
os.system("gksudo service openvpn start")
self.checkStatus()
def handler_menu_stop(self, evt):
self.icon_orange()
os.system("gksudo service openvpn stop")
self.checkStatus()
def handler_menu_restart(self, evt):
self.icon_orange()
os.system("gksudo service openvpn restart")
self.checkStatus()
def checkStatus(self):
self.setup_menu()
if self.isRunning:
self.icon_green()
else:
self.icon_red()
return 1
def main(self):
self.checkStatus()
GLib.timeout_add(FREQUENCY * 1000, self.checkStatus)
Gtk.main()
if __name__ == "__main__":
ind = OpenVpnIndicator()
ind.main()