-
Notifications
You must be signed in to change notification settings - Fork 2
/
KillSwitch
executable file
·197 lines (156 loc) · 6.64 KB
/
KillSwitch
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
#!/usr/bin/python3
"""
* Copyright 2021 Pong Loong Yeat (https://github.com/pongloongyeat/killswitch)
*
* 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, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
"""
import os
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gio", "2.0")
gi.require_version("Granite", "1.0")
from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import Granite
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="KillSwitch")
self.gtk_settings = Gtk.Settings.get_default()
self.granite_settings = Granite.Settings.get_default()
self.set_colour_scheme()
self.granite_settings.connect("notify::prefers-color-scheme", self.set_colour_scheme)
self.set_size_request(480, 320)
self.set_default_size(480, 320)
self.props.resizable = False
welcome = Granite.WidgetsWelcome.new("KillSwitch", "Kills running processes")
welcome.append("application-default-icon", "Applications", "Kills all running user-installed applications.")
welcome.append("preferences-desktop-wallpaper", "Desktop components", "Kills the dock and top bar.")
welcome.connect("activated", self.on_welcome_click)
self.add(welcome)
def set_colour_scheme(self, *args, **kwargs):
self.gtk_settings.props.gtk_application_prefer_dark_theme = (
self.granite_settings.props.prefers_color_scheme == Granite.SettingsColorScheme.DARK
)
"""
Most work happens here. This is where we gather the list of apps
we want to kill. This just checks for any apps in
/usr/share/applications and adds them to a list to be attempted
to be killed.
"""
def get_app_list(self):
desktop_files = os.listdir("/usr/share/applications")
desktop_files.sort()
app_list = []
exec_list = []
for desktop_file in desktop_files:
if ".desktop" in desktop_file:
try:
desktop_app_info = Gio.DesktopAppInfo.new(desktop_file)
# Exclude apps that launch in terminal.
if not desktop_app_info.get_boolean("Terminal"):
app_info = AppInfo(
desktop_app_info.get_executable()
)
# Check to make sure no duplicate entries
if app_info.exec_name not in exec_list:
exec_list.append(app_info.exec_name)
app_list.append(app_info)
except:
pass
return app_list
def show_message_dialog(self, on_response, primary_text="", secondary_text=""):
message_dialog = Granite.MessageDialog.with_image_from_icon_name(
primary_text=primary_text,
secondary_text=secondary_text,
image_icon_name="dialog-warning",
buttons=Gtk.ButtonsType.CANCEL
)
suggested_button = Gtk.Button.new_with_label("Proceed")
suggested_button.get_style_context().add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION)
message_dialog.add_action_widget(suggested_button, Gtk.ResponseType.ACCEPT)
message_dialog.props.transient_for = self
message_dialog.connect("response", on_response)
message_dialog.show_all()
def on_welcome_click(self, widget, index, *args):
if index == 0:
self.show_message_dialog(
self.on_app_response,
"Are you sure you want to proceed?",
"This will kill all running applications. Please save your work before proceeding."
)
elif index == 1:
self.show_message_dialog(
self.on_desktop_response,
"Are you sure you want to proceed",
"This will kill the dock and top bar. If they are killed again within the next 120s, they will not automatically relaunch."
)
else:
pass
def on_app_response(self, dialog, response_id):
if response_id == Gtk.ResponseType.ACCEPT:
for app_info in self.get_app_list():
if not app_info.is_desktop_component():
app_info.kill()
dialog.destroy()
def on_desktop_response(self, dialog, response_id):
if response_id == Gtk.ResponseType.ACCEPT:
for app_info in self.get_app_list():
if app_info.is_desktop_component():
app_info.kill()
dialog.destroy()
class AppInfo:
DEFAULT_NO_KILL = [
"evolution-alarm-notify",
"gala",
"mutter",
"pantheon-parental-controls-client",
]
def __init__(self, exec_raw):
self.exec_raw = exec_raw # Un-sanitised "Exec" parameter
self.exec_name = self.sanitise_exec(exec_raw)
def sanitise_exec(self, exec_raw):
# Remove args and path
return exec_raw.split(" ")[0].split("/")[-1]
def is_elementary(self):
return ("io.elementary" in self.exec_name or
"pantheon" in self.exec_name or
self.exec_name in ["gala", "plank"])
def is_lib(self):
return "lib" in self.exec_name
def is_agent(self):
return "agent" in self.exec_name
def is_daemon(self):
return "daemon" in self.exec_name
# Not sure if naming is right here
def is_service(self):
return "bus" in self.exec_name
def is_desktop_component(self):
return self.exec_name in ["io.elementary.wingpanel", "plank"]
def should_kill(self):
if self.exec_name in self.DEFAULT_NO_KILL:
return False
# Ignore libraries, agents and daemons
if self.is_lib() or self.is_agent() or self.is_daemon() or self.is_service():
return False
return True
def kill(self):
if self.should_kill() is True:
print("Killing {}".format(self.exec_name))
os.system("pkill {}".format(self.exec_name))
win = MainWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()