forked from cedlemo/blingbling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
udisks_glue.lua
291 lines (276 loc) · 9.82 KB
/
udisks_glue.lua
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
--@author cedlemo
local helpers = require("blingbling.helpers")
local awful = require("awful")
local naughty = require("naughty")
local string = string
local math = math
local ipairs = ipairs
local next = next
local pairs = pairs
local type = type
local setmetatable = setmetatable
local table = table
local wibox = require("wibox")
local debug = debug
---A menu for udisks-glue informations and actions
--@module blingbling.udisks_glue
local udisks_glue = { mt = {} }
local data = setmetatable( {}, { __mode = "k"})
local function udisks_send(ud_menu,command,a_device)
local s=""
data[ud_menu].menu_visible = "false"
data[ud_menu].menu:hide()
s=s .. "udisks --"..command.." "..a_device
return s
end
local function mounted_submenu(ud_menu, a_device)
local my_submenu= {
{ "umount",udisks_send(ud_menu,"unmount", a_device),data[ud_menu].umount_icon}--,
}
return my_submenu
end
local function unmounted_submenu(ud_menu,a_device)
local my_submenu= {
{ "mount",udisks_send(ud_menu,"mount", a_device),data[ud_menu].mount_icon}--,
}
return my_submenu
end
local function unmount_multiple_partitions(ud_menu, a_device, mount_points)
local command = "bash -c \""
for _,m in ipairs(mount_points) do
command = command .. udisks_send(ud_menu, "unmount", m)..";"
end
command = command .. udisks_send(ud_menu, "detach", a_device) .. "\""
return command
end
local function generate_menu(ud_menu)
--all_devices={device_name={partition_1,partition_2}
--devices_type={device_name=Usb or Cdrom}
--partition_state={partition_name = mounted or unmounted}
local my_menu={}
if next(data[ud_menu].all_devices) ~= nil then
for k,v in pairs(data[ud_menu].all_devices) do
local device_type=data[ud_menu].devices_type[k]
local action=""
if device_type == "Usb" then
action="detach"
else
action="eject"
end
local check_remain_mounted_partition =0
my_submenu={}
local mounted_partitions = {}
for j,x in ipairs(v) do
if data[ud_menu].partition_state[x] == "mounted" then
check_remain_mounted_partition = 1
table.insert(my_submenu,{x, mounted_submenu(ud_menu, x), data[ud_menu][device_type.."_icon"]})
table.insert(mounted_partitions,x)
else
table.insert(my_submenu,{x, unmounted_submenu(ud_menu, x), data[ud_menu][device_type.."_icon"]})
end
end
if check_remain_mounted_partition == 1 then
table.insert(my_submenu,{"unmount all", unmount_multiple_partitions(ud_menu, k, mounted_partitions ), data[ud_menu]["umount_icon"]})
--table.insert(my_submenu,{"Can\'t "..action, {{k .." busy"}}, data[ud_menu][action.."_icon"]})
else
table.insert(my_submenu,{action, udisks_send(ud_menu, action, k), data[ud_menu][action.."_icon"]})
end
table.insert(my_menu, {k, my_submenu, data[ud_menu][device_type.."_icon"]})
end
else
my_menu={{"No media",""}}
end
data[ud_menu].menu= awful.menu({ items = my_menu,
})
return ud_menu
end
local function display_menu(ud_menu)
ud_menu:buttons(awful.util.table.join(
awful.button({ }, 1, function()
if data[ud_menu].menu_visible == "false" then
data[ud_menu].menu_visible = "true"
generate_menu(ud_menu )
data[ud_menu].menu:show()
else
data[ud_menu].menu:hide()
data[ud_menu].menu_visible = "false"
end
end),
awful.button({ }, 3, function()
data[ud_menu].menu:hide()
data[ud_menu].menu_visible = "false"
end)
))
end
function udisks_glue.insert_device(ud_menu,device, mount_point, device_type)
-- generate the device_name
if device_type == "Usb" then
device_name = string.gsub(device,"%d*","")
else
device_name=device
end
-- add all_devices entry:
-- check if device is already registred
if data[ud_menu].all_devices[device_name] == nil then
data[ud_menu].all_devices[device_name]={device}
data[ud_menu].devices_type[device_name] = device_type
else
partition_already_registred = 0
for i, v in ipairs(data[ud_menu].all_devices[device_name]) do
if v == device then
partition_already_registred = 1
end
end
if partition_already_registred == 0 then
table.insert(data[ud_menu].all_devices[device_name],device)
end
end
data[ud_menu].partition_state[device]="unmounted"
data[ud_menu].menu:hide()
data[ud_menu].menu_visible = "false"
naughty.notify({title = device_type..":", text = device .." inserted", timeout = 10})
end
function udisks_glue.mount_device(ud_menu,device, mount_point, device_type)
-- generate the device_name
if device_type == "Usb" then
device_name = string.gsub(device,"%d*","")
else
device_name=device
end
-- add all_devices entry:
-- check if device is already registred
if data[ud_menu].all_devices[device_name] == nil then
data[ud_menu].all_devices[device_name]={device}
data[ud_menu].devices_type[device_name] = device_type
else
partition_already_registred = 0
for i, v in ipairs(data[ud_menu].all_devices[device_name]) do
if v == device then
partition_already_registred = 1
end
end
if partition_already_registred == 0 then
table.insert(data[ud_menu].all_devices[device_name],device)
end
end
data[ud_menu].partition_state[device]="mounted"
data[ud_menu].menu:hide()
data[ud_menu].menu_visible = "false"
naughty.notify({title = device_type..":", text =device .. " mounted on" .. mount_point, timeout = 10})
return ud_menu
end
function unmount_device(ud_menu, device, mount_point, device_type)
data[ud_menu].partition_state[device]="unmounted"
data[ud_menu].menu:hide()
data[ud_menu].menu_visible = "false"
naughty.notify({title = device_type..":", text = device .." unmounted", timeout = 10})
end
function remove_device(ud_menu, device, mount_point, device_type )
local device_name=""
if device_type == "Usb" then
device_name=string.gsub(device,"%d*","")
else
device_name = device
end
--Remove the partitions
if data[ud_menu].all_devices[device_name] ~= nil then
for i,v in ipairs(data[ud_menu].all_devices[device_name]) do
if v == device then
table.remove(data[ud_menu].all_devices[device_name],i)
helpers.hash_remove(data[ud_menu].partition_state, device)
end
end
end
--Remove the device if no remaining partition
if data[ud_menu].all_devices[device_name] ~= nil and #data[ud_menu].all_devices[device_name] == 0 then
helpers.hash_remove(data[ud_menu].all_devices, device_name)
helpers.hash_remove(data[ud_menu].devices_type, device_name)
end
data[ud_menu].menu:hide()
data[ud_menu].menu_visible = "false"
naughty.notify({title = device_type ..":", text = device .." removed", timeout = 10})
end
---Define the icon for the mount action in the menu.
--@usage ud_widget:set_mount_icon(icon)
--@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_mount_icon
--@param an_image an image file name
function set_mount_icon(ud_menu,an_image)
data[ud_menu].mount_icon=an_image
return ud_menu
end
---Define the icon for the umount action in the menu.
--@usage ud_widget:set_umount_icon(icon)
--@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_umount_icon
--@param an_image an image file name
function set_umount_icon(ud_menu,an_image)
data[ud_menu].umount_icon=an_image
return ud_menu
end
---Define the icon for the detach action in the menu.
--@usage ud_widget:set_detach_icon(icon)
--@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_detach_icon
--@param an_image an image file name
function set_detach_icon(ud_menu,an_image)
data[ud_menu].detach_icon=an_image
return ud_menu
end
---Define the icon for eject action in the menu.
--@usage ud_widget:set_eject_icon(icon)
--@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_eject_icon
--@param an_image an image file name
function set_eject_icon(ud_menu,an_image)
data[ud_menu].eject_icon=an_image
return ud_menu
end
---Define the icon for usb devices in the menu.
--@usage ud_widget:set_Usb_icon(icon)
--@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_Usb_icon
--@param an_image an image file name
function set_Usb_icon(ud_menu,an_image)
data[ud_menu].Usb_icon=an_image
return ud_menu
end
---Define the icon for Cdrom devices in the menu.
--@usage ud_widget:set_Cdrom_icon(icon)
--@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_Cdrom_icon
--@param an_image an image file name
function set_Cdrom_icon(ud_menu,an_image)
data[ud_menu].Cdrom_icon=an_image
return ud_menu
end
function udisks_glue.new(args)
local args = args or {}
local ud_menu
ud_menu = wibox.widget.imagebox()
ud_menu:set_image(args.menu_icon)
data[ud_menu]={ image = menu_icon,
all_devices= {},
devices_type={},
partition_state={},
menu_visible = "false",
menu={},
Cdrom_icon=args.Cdrom_icon,
Usb_icon=args.Usb_icon,
mount_icon=args.mount_icon,
umount_icon=args.umount_icon,
detach_icon=args.detach_icon,
eject_icon=args.eject_icon,
}
ud_menu.insert_device = udisks_glue.insert_device
ud_menu.mount_device = udisks_glue.mount_device
ud_menu.unmount_device = unmount_device
ud_menu.remove_device = remove_device
ud_menu.set_mount_icon = set_mount_icon
ud_menu.set_umount_icon = set_umount_icon
ud_menu.set_detach_icon = set_detach_icon
ud_menu.set_eject_icon = set_eject_icon
ud_menu.set_Usb_icon = set_Usb_icon
ud_menu.set_Cdrom_icon = set_Cdrom_icon
generate_menu(ud_menu)
display_menu(ud_menu)
return ud_menu
end
function udisks_glue.mt:__call(...)
return udisks_glue.new(...)
end
return setmetatable(udisks_glue, udisks_glue.mt)