9
9
gi .require_version ('Gtk' , '3.0' )
10
10
gi .require_version ('AppIndicator3' , '0.1' )
11
11
from gi .repository import Gtk , GLib , AppIndicator3 as AI
12
- import os , sys , subprocess
12
+ import os , subprocess
13
13
14
14
class ServiceStatus :
15
15
On , Off , Disabled = ("RUNNING" , "NOTRUNNING" , "DEACTIVATED" )
@@ -19,49 +19,68 @@ def __init__(self):
19
19
# Setup Indicator Applet
20
20
self .indicator = AI .Indicator .new ("xampp-indicator" , "xampp" , AI .IndicatorCategory .APPLICATION_STATUS )
21
21
self .indicator .set_status (AI .IndicatorStatus .ACTIVE )
22
- self .indicator .set_icon (self .get_icon ())
23
22
24
23
# Set Attributes
25
24
xampp_path = "/opt/lampp"
26
25
self .xampp_bin = os .path .join (xampp_path , "lampp" )
27
26
self .control_panel_bin = os .path .join (xampp_path , "manager-linux-x64.run" )
28
27
if not os .path .exists (self .control_panel_bin ):
29
28
self .control_panel_bin = os .path .join (xampp_path , "manager-linux.run" )
29
+ self .serviceMenuItems = {}
30
30
self .services = {
31
- "APACHE" : "apache" ,
32
- "MYSQL" : "mysql" ,
33
- "PROFTPD" : "ftp"
31
+ "APACHE" : {
32
+ "label" : "Apache" ,
33
+ "name" : "apache" ,
34
+ "status_key" : "APACHE"
35
+ },
36
+ "MYSQL" : {
37
+ "label" : "MySQL" ,
38
+ "name" : "mysql" ,
39
+ "status_key" : "APACHE" # i used APACHE status here because MYSQL status returns Off when get status command is not executed as administrator (using pkexec or sudo)
40
+ }
41
+ ,
42
+ "PROFTPD" : {
43
+ "label" : "FTP" ,
44
+ "name" : "ftp" ,
45
+ #"enabled": True,
46
+ "status_key" : "PROFTPD"
47
+ }
34
48
}
35
49
36
50
# Get Xampp Status
37
51
self .status = self .get_xampp_status ()
38
52
53
+ # Set Indicator Icon
54
+ self .set_icon ()
55
+
39
56
# Setup The Menu
40
57
self .menu = Gtk .Menu ()
41
58
42
- # Menu Item: Apache
43
- service_name = "APACHE"
44
- self .apacheItem = Gtk .CheckMenuItem ("Apache" )
45
- if service_name in self .status and self .status [service_name ] == ServiceStatus .On :
46
- self .apacheItem .set_active (True )
47
- self .apacheItem .connect ("activate" , self .toggle_service , service_name )
48
- self .menu .append (self .apacheItem )
49
-
50
- # Menu Item: MySQL
51
- service_name = "MYSQL"
52
- self .mySqlItem = Gtk .CheckMenuItem ("MySQL" )
53
- if service_name in self .status and self .status [service_name ] == ServiceStatus .On :
54
- self .mySqlItem .set_active (True )
55
- self .mySqlItem .connect ("activate" , self .toggle_service , service_name )
56
- self .menu .append (self .mySqlItem )
57
-
58
- # Menu Item: FTP
59
- service_name = "PROFTPD"
60
- self .mySqlItem = Gtk .CheckMenuItem ("FTP" )
61
- if service_name in self .status and self .status [service_name ] == ServiceStatus .On :
62
- self .mySqlItem .set_active (True )
63
- self .mySqlItem .connect ("activate" , self .toggle_service , service_name )
64
- self .menu .append (self .mySqlItem )
59
+ # Menu Item: Start
60
+ status_key = "APACHE"
61
+ if status_key in self .status and self .status [status_key ] == ServiceStatus .On :
62
+ label = "Stop"
63
+ else :
64
+ label = "Start"
65
+ startStopItem = Gtk .MenuItem (label )
66
+ startStopItem .connect ("activate" , self .start_stop_xampp )
67
+ self .menu .append (startStopItem )
68
+
69
+ # Menu Item: Restart
70
+ restartItem = Gtk .MenuItem ("Restart" )
71
+ restartItem .connect ("activate" , self .restart_xampp )
72
+ self .menu .append (restartItem )
73
+ self .menu .append (Gtk .SeparatorMenuItem ())
74
+
75
+ # Service Menu Items
76
+ for service in self .services :
77
+ self .serviceMenuItems [service ] = Gtk .CheckMenuItem (self .services [service ]["label" ])
78
+ if "enabled" not in self .services [service ] or not self .services [service ]["enabled" ]:
79
+ self .serviceMenuItems [service ].set_sensitive (False ) # Disable service toggle due to MYSQL get status issue
80
+ if self .services [service ]["status_key" ] in self .status and self .status [self .services [service ]["status_key" ]] == ServiceStatus .On :
81
+ self .serviceMenuItems [service ].set_active (True )
82
+ self .serviceMenuItems [service ].connect ("activate" , self .toggle_service , service )
83
+ self .menu .append (self .serviceMenuItems [service ])
65
84
self .menu .append (Gtk .SeparatorMenuItem ())
66
85
67
86
# Menu Item: Control Panel
@@ -101,50 +120,103 @@ def get_xampp_status(self):
101
120
def launch_control_panel (self , widget ):
102
121
subprocess .Popen (["pkexec" , self .control_panel_bin ])
103
122
104
- def toggle_service (self , widget , service_name ):
123
+ def start_stop_xampp (self , widget ):
124
+ # Disable Widget
125
+ widget .set_sensitive (False )
126
+ # Change Widget Label
127
+ label = widget .get_label ()
128
+ widget .set_label (label + "..." )
129
+ # Start or Stop Xampp
130
+ status_key = "APACHE"
131
+ if status_key in self .status and self .status [status_key ] == ServiceStatus .On :
132
+ self .stop_service ()
133
+ new_label = "Start"
134
+ else :
135
+ self .start_service ()
136
+ new_label = "Stop"
137
+ # Update Status after 10 seconds
138
+ GLib .timeout_add_seconds (10 , self .update_status , widget , new_label )
139
+
140
+ def restart_xampp (self , widget ):
141
+ # Disable Widget
142
+ widget .set_sensitive (False )
143
+ # Set Indicator Icon "Off"
144
+ self .set_icon ("xampp-dark.svg" )
145
+ # Change Widget Label
146
+ label = widget .get_label ()
147
+ widget .set_label (label + "..." )
148
+ # Restart Xampp
149
+ self .restart_service ()
150
+ # Update Status after 10 seconds
151
+ GLib .timeout_add_seconds (10 , self .update_status , widget , label )
152
+
153
+ def toggle_service (self , widget , service ):
105
154
if widget .get_sensitive ():
106
155
# Change Widget Label
107
156
label = widget .get_label ()
108
157
widget .set_label (label + "..." )
109
158
# Disable Widget
110
159
widget .set_sensitive (False )
111
160
# Toggle Service
112
- if self .status [service_name ] == ServiceStatus .On :
113
- self .stop_service (self .services [service_name ])
161
+ if self .status [service ] == ServiceStatus .On :
162
+ self .stop_service (self .services [service ][ "name" ])
114
163
else :
115
- self .start_service (self .services [service_name ])
164
+ self .start_service (self .services [service ][ "name" ])
116
165
# Update Status after 10 seconds
117
- GLib .timeout_add_seconds (10 , self .update_status , widget , service_name )
166
+ GLib .timeout_add_seconds (10 , self .update_status , widget , label , service )
118
167
119
- def update_status (self , widget , service_name ):
168
+ def update_status (self , widget , widget_label = None , service = None ):
120
169
# Update Xampp Status
121
170
self .status = self .get_xampp_status ()
122
- if service_name in self .status and self .status [service_name ] == ServiceStatus .On :
123
- widget .set_active (True )
171
+ if service is not None :
172
+ # Update Widget
173
+ if self .services [service ]["status_key" ] in self .status and self .status [self .services [service ]["status_key" ]] == ServiceStatus .On :
174
+ widget .set_active (True )
175
+ else :
176
+ widget .set_active (False )
124
177
else :
125
- widget .set_active (False )
178
+ # Update All Service Menu Items
179
+ for service in self .services :
180
+ service_menu_item = self .serviceMenuItems [service ]
181
+ was_sensitive = service_menu_item .get_sensitive ()
182
+ if was_sensitive :
183
+ service_menu_item .set_sensitive (False )
184
+ if self .services [service ]["status_key" ] in self .status and self .status [self .services [service ]["status_key" ]] == ServiceStatus .On :
185
+ service_menu_item .set_active (True )
186
+ else :
187
+ service_menu_item .set_active (False )
188
+ if was_sensitive :
189
+ service_menu_item .set_sensitive (True )
126
190
# Change Widget Label
127
- label = widget . get_label (). replace ( "..." , "" )
128
- widget .set_label (label )
191
+ if widget_label is not None :
192
+ widget .set_label (widget_label )
129
193
# Enable Widget
130
194
widget .set_sensitive (True )
195
+ # Set Indicator Icon
196
+ self .set_icon ()
131
197
132
198
return False # Do not loop
133
199
134
- def start_service (self , service ):
135
- p = subprocess .Popen (["pkexec" , self .xampp_bin , "start" + service ])
200
+ def start_service (self , service_name = "" ):
201
+ subprocess .Popen (["pkexec" , self .xampp_bin , "start" + service_name ])
202
+
203
+ def stop_service (self , service_name = "" ):
204
+ subprocess .Popen (["pkexec" , self .xampp_bin , "stop" + service_name ])
136
205
137
- def stop_service (self , service ):
138
- p = subprocess .Popen (["pkexec" , self .xampp_bin , "stop " + service ])
206
+ def restart_service (self , service_name = "" ):
207
+ subprocess .Popen (["pkexec" , self .xampp_bin , "reload " + service_name ])
139
208
140
- def get_icon (self ):
141
- # Get Icon
142
- icon = os .path .dirname (os .path .realpath (__file__ )) + "/xampp.svg"
209
+ def set_icon (self , icon_name = None ):
210
+ if icon_name is None :
211
+ icon_name = "xampp-dark.svg"
212
+ status_key = "APACHE"
213
+ if status_key in self .status and self .status [status_key ] == ServiceStatus .On :
214
+ icon_name = "xampp.svg"
215
+ icon = os .path .dirname (os .path .realpath (__file__ )) + "/icons/" + icon_name
143
216
if os .path .exists (icon ):
144
- return icon
217
+ self . indicator . set_icon ( icon )
145
218
else :
146
219
print ("ERROR: Cannot find icon : %s" % icon )
147
- sys .exit (1 )
148
220
149
221
def quit (self , widget ):
150
222
Gtk .main_quit ()
0 commit comments