-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
98 lines (73 loc) · 3.46 KB
/
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
import logging
import os.path
import subprocess
import gi
import nm_tools
gi.require_version('Gdk', '3.0')
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
logger = logging.getLogger(__name__)
description_active = "{} | ACTIVE | Select to disconnect"
description_inactive = "{} | Select to connect"
class NetworkManagerExtension(Extension):
def __init__(self):
super(NetworkManagerExtension, self).__init__()
# Subscribe plugin listeners to launcher
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
search_query = event.get_argument()
hidden_types = extension.preferences.get("hidden_type_list").split(",")
connections = nm_tools.get_connections()
connections = sorted(connections, key=lambda d: d["name"].lower())
items = []
for a in connections:
name = a["name"]
if search_query is not None and search_query not in name.lower():
continue
description = description_active if a["active"] else description_inactive
description = description.format(a["type"])
icon_name = "{}_{}".format(a["type"], a["active"])
icon_path = 'images/{}.png'.format(icon_name)
if not os.path.isfile(icon_path):
logger.warning("Icon not found: " + icon_path)
if a["type"] in hidden_types:
continue
on_click_event = ExtensionCustomAction(a, keep_app_open=False)
item_row = ExtensionResultItem(icon=icon_path,
name=name,
description=description,
on_enter=on_click_event)
items.append(item_row)
return RenderResultListAction(items)
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
con = event.get_data()
uuid = con["uuid"]
if con["active"]:
log, result = nm_tools.disconnect(uuid)
else:
log, result = nm_tools.connect(uuid)
logging.debug(log)
# Notification
if extension.preferences.get("enable_notifications") == "true":
if not result:
# Operation failed
nm_tools.send_notification("Operation failed: " + log)
elif con["active"]:
# Success, disconnected
nm_tools.send_notification("Now disconnected: " + con["name"])
else:
# Success, connected
nm_tools.send_notification("Now connected: " + con["name"])
# Run script if successfully connected and script isn't empty
script = extension.preferences.get("script_on_connect")
if not con["active"] and script != "" and result:
subprocess.run([script, con["name"], con["uuid"]], stdout=subprocess.PIPE)
if __name__ == '__main__':
NetworkManagerExtension().run()