-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
97 lines (79 loc) · 2.79 KB
/
Copy pathinit.lua
File metadata and controls
97 lines (79 loc) · 2.79 KB
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
local setmetatable = setmetatable
local lgi = require 'lgi'
local GLib = lgi.GLib
local NM = lgi.NM
local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local devicewidget = require("awesome-networkmanager-widget.devicewidget")
local devicehelper = require("awesome-networkmanager-widget.devicehelper")
local network = { mt = {} }
local nm = nil
local function device_filter(device)
return device:get_device_type() == "WIFI" or device:get_device_type() == "ETHERNET"
end
local function foreach_device(fn)
local devices = nm:get_devices()
for _, d in ipairs(devices) do
if device_filter(d) then
fn(d)
end
end
end
function network:toggle_mode()
if self._private.mode == devicewidget.MODE_IP4 then self._private.mode = devicewidget.MODE_IP6
elseif self._private.mode == devicewidget.MODE_IP6 then self._private.mode = devicewidget.MODE_IP4
end
foreach_device(function(device)
local device_widget = self:get_or_create_device_widget(device)
device_widget:set_mode(self._private.mode)
device_widget:update(device)
end)
end
function network:get_or_create_device_widget(device)
-- cache widget by name
local device_name = devicehelper.get_device_name(device)
self._private[device_name] = self._private[device_name] or devicewidget({ mode = self._private.mode})
return self._private[device_name]
end
function network:update()
self:reset()
foreach_device(function(device)
local device_widget = self:get_or_create_device_widget(device)
self:add(device_widget)
device_widget:update(device)
end)
end
function network:init(nm_new_res)
-- finalize network-manager connexion
nm = NM.Client.new_finish(nm_new_res)
-- add listeners on network manager events
local update = function() self:update() end
for _, connection in ipairs(nm:get_active_connections()) do
connection.on_state_changed = update
end
nm.on_active_connection_added = function(_, connection)
connection.on_state_changed = update
end
nm.on_active_connection_removed = update
nm.on_device_added = update
nm.on_device_removed = update
-- add listeners on widget events
self:buttons(gears.table.join(
awful.button({ }, 1, function() self:toggle_mode() end)
))
-- update widget now
self:update()
end
local function new(args)
local container = wibox.layout.fixed.horizontal()
container.spacing = 2
container._private.mode = args and args.mode or devicewidget.MODE_IP4
setmetatable(container, {__index = network})
NM.Client.new_async(nil, function(src, res) container:init(res) end)
return container
end
function network.mt:__call(...)
return new(...)
end
return setmetatable(network, network.mt)