-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdroidian-restart-services-gtk3-bin-main.py
executable file
·187 lines (175 loc) · 8.25 KB
/
droidian-restart-services-gtk3-bin-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
#!/usr/bin/env python3
#[HEADER_SECTION]
#[HEADER_END]
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
import subprocess
import os
import time
import threading
class AboutDialog(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "About", parent, 0)
self.set_default_size(400, 300)
#
## Config About dialog box
box = self.get_content_area()
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
vbox.set_border_width(10)
box.add(vbox)
#
## TODO: Add a logo
#image_path = "/usr/share/service_manager/logo.png" # Assegura't que tens un logo en aquest path
#if os.path.exists(image_path):
# pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(image_path, 100, 100, True)
# logo = Gtk.Image.new_from_pixbuf(pixbuf)
# vbox.pack_start(logo, False, False, 0)
#
## Title
title = Gtk.Label()
title.set_markup("<big><b>Droidian Restart Services GTK3</b></big>")
vbox.pack_start(title, False, False, 0)
#
## Description
label_about = Gtk.Label()
label_about.set_justify(Gtk.Justification.CENTER)
label_about.set_markup("<i>GTK GUI to restart systemd services</i>")
vbox.pack_start(label_about, False, False, 0)
#
## Add a separator
separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
vbox.pack_start(separator, False, False, 10)
#
## Create label for about info
label_about_info = Gtk.Label()
label_about_info.set_line_wrap(True)
label_about_info.set_justify(Gtk.Justification.LEFT)
label_about_info.set_selectable(True)
self.get_content_area().add(label_about_info)
#
## Load about info
about_file_path = "/usr/share/droidian-restart-services-gtk3/about.txt"
formatted_text = "Information not available."
if os.path.exists(about_file_path):
with open(about_file_path, "r") as f:
lines = f.readlines()
formatted_text = ""
for line in lines:
if not line:
continue
key, _, value = line.partition(": ")
if key and value:
# Escapar els caràcters especials en el valor abans d'afegir al markup
escaped_value = GLib.markup_escape_text(value)
formatted_text += f"<b>{key}:</b> {escaped_value}\n"
else:
# Afegir el text sense formatar si no compleix el format clau: valor
formatted_text += line + "\n"
#
# Configurar el text amb Pango markup
label_about_info.set_markup(formatted_text)
#
self.show_all()
class RestartServicesWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Restart Systemd Services GTK3")
self.set_default_size(800, 600)
#
## Create the main vbox
main_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
self.add(main_vbox)
## Box1 top 10%
box_top = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box_top.set_size_request(-1, 60) ## In pixels
main_vbox.pack_start(box_top, False, False, 0)
## Box2 services 20%
box_services = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box_services.set_size_request(-1, 120) ## In pixels
main_vbox.pack_start(box_services, False, False, 0)
## Box3 info 80%
box_info = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
box_info.set_size_request(-1, 450) ## In pixels
main_vbox.pack_start(box_info, False, False, 0)
box_info_label = Gtk.Label(label="Information:\n\nRestart the service that you want!")
box_info.pack_start(box_info_label, True, True, 0)
## Box4 misc remaining
box_misc = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
main_vbox.pack_start(box_misc, True, True, 0)
#
## Create restart services buttons
# Section Network services
separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
box_services.pack_start(separator, False, True, 10)
label_net_dev_services = Gtk.Label(label="Network device services")
box_services.pack_start(label_net_dev_services, True, True, 5)
## ofono
button_ofono = Gtk.Button(label="Restart ofono Service")
button_ofono.connect("clicked", self.on_restart_clicked, "ofono", False)
box_services.pack_start(button_ofono, True, True, 0)
## ModemManager
button_MM = Gtk.Button(label="Restart ModemManager Service")
button_MM.connect("clicked", self.on_restart_clicked, "ModemManager", False)
box_services.pack_start(button_MM, True, True, 0)
## MetworkManager
button_NM = Gtk.Button(label="Restart NetworkManager Service")
button_NM.connect("clicked", self.on_restart_clicked, "NetworkManager", False)
box_services.pack_start(button_NM, True, True, 0)
# Section Multiple net dev services
box_services.pack_start(separator, False, True, 10)
label_net_dev_multi_services = Gtk.Label(label="Network device Multiple services")
box_services.pack_start(label_net_dev_multi_services, True, True, 5)
## ofono+ModemManager
button_ofono_MM = Gtk.Button(label="Restart ofono and ModemManager Services")
button_ofono_MM.connect("clicked", lambda w: threading.Thread(target=self.restart_services, args=("ofono", "ModemManager", False)).start())
box_services.pack_start(button_ofono_MM, True, True, 0)
# Section VPN services
#separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
box_services.pack_start(separator, False, True, 10)
label_vpn_services = Gtk.Label(label="VPN services")
box_services.pack_start(label_vpn_services, True, True, 10)
## ProtonVPN
button_protonvpn = Gtk.Button(label="Restart ProtonVPN Service")
button_protonvpn.connect("clicked", self.on_restart_clicked, "protonvpn-phosh-monitor", True)
box_services.pack_start(button_protonvpn, True, True, 0)
# Section Other services
#separator = Gtk.Separator(orientation=Gtk.Orientation.HORIZONTAL)
box_services.pack_start(separator, False, True, 5)
label_other_services = Gtk.Label(label="Other services")
box_services.pack_start(label_other_services, True, True, 5)
## Bluetooth
button_bluetooth = Gtk.Button(label="Restart Bluetooth Service")
button_bluetooth.connect("clicked", self.on_restart_clicked, "bluetooth", False)
box_services.pack_start(button_bluetooth, True, True, 0)
#
## Create misc buttons
## About
button_about = Gtk.Button(label="About")
#button_about.connect("clicked", lambda btn: AboutDialog(win).run())
button_about.connect("clicked", self.on_about_clicked)
box_misc.pack_start(button_about, True, True, 0)
## Exit
exit_button = Gtk.Button(label="Exit")
exit_button.connect("clicked", Gtk.main_quit)
box_misc.pack_start(exit_button, True, True, 0)
def on_restart_clicked(self, widget, service_name, user_service):
command = f"pkexec systemctl restart {service_name}"
if user_service:
command = f"systemctl --user restart {service_name}"
try:
result = subprocess.run(command, shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(f"Service '{service_name}' restarted successfully.")
except subprocess.CalledProcessError as e:
print(f"Error restarting service '{service_name}': {e.stderr.decode()}")
def restart_services(self, service1, service2, user_service):
self.on_restart_clicked(None, service1, user_service)
time.sleep(10) # Espera 10 segons
self.on_restart_clicked(None, service2, user_service)
def on_about_clicked(self, widget):
about_dialog = AboutDialog(self)
about_dialog.run()
about_dialog.destroy()
win = RestartServicesWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()