udisks_glue.lua 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. --@author cedlemo
  2. local helpers = require("blingbling.helpers")
  3. local awful = require("awful")
  4. local naughty = require("naughty")
  5. local string = string
  6. local math = math
  7. local ipairs = ipairs
  8. local next = next
  9. local pairs = pairs
  10. local type = type
  11. local setmetatable = setmetatable
  12. local table = table
  13. local wibox = require("wibox")
  14. local debug = debug
  15. ---A menu for udisks-glue informations and actions
  16. --@module blingbling.udisks_glue
  17. local udisks_glue = { mt = {} }
  18. local data = setmetatable( {}, { __mode = "k"})
  19. local function udisks_send(ud_menu,command,a_device)
  20. local s=""
  21. data[ud_menu].menu_visible = "false"
  22. data[ud_menu].menu:hide()
  23. s=s .. "udisks --"..command.." "..a_device
  24. return s
  25. end
  26. local function mounted_submenu(ud_menu, a_device)
  27. local my_submenu= {
  28. { "umount",udisks_send(ud_menu,"unmount", a_device),data[ud_menu].umount_icon}--,
  29. }
  30. return my_submenu
  31. end
  32. local function unmounted_submenu(ud_menu,a_device)
  33. local my_submenu= {
  34. { "mount",udisks_send(ud_menu,"mount", a_device),data[ud_menu].mount_icon}--,
  35. }
  36. return my_submenu
  37. end
  38. local function unmount_multiple_partitions(ud_menu, a_device, mount_points)
  39. local command = "bash -c \""
  40. for _,m in ipairs(mount_points) do
  41. command = command .. udisks_send(ud_menu, "unmount", m)..";"
  42. end
  43. command = command .. udisks_send(ud_menu, "detach", a_device) .. "\""
  44. return command
  45. end
  46. local function generate_menu(ud_menu)
  47. --all_devices={device_name={partition_1,partition_2}
  48. --devices_type={device_name=Usb or Cdrom}
  49. --partition_state={partition_name = mounted or unmounted}
  50. local my_menu={}
  51. if next(data[ud_menu].all_devices) ~= nil then
  52. for k,v in pairs(data[ud_menu].all_devices) do
  53. local device_type=data[ud_menu].devices_type[k]
  54. local action=""
  55. if device_type == "Usb" then
  56. action="detach"
  57. else
  58. action="eject"
  59. end
  60. local check_remain_mounted_partition =0
  61. my_submenu={}
  62. local mounted_partitions = {}
  63. for j,x in ipairs(v) do
  64. if data[ud_menu].partition_state[x] == "mounted" then
  65. check_remain_mounted_partition = 1
  66. table.insert(my_submenu,{x, mounted_submenu(ud_menu, x), data[ud_menu][device_type.."_icon"]})
  67. table.insert(mounted_partitions,x)
  68. else
  69. table.insert(my_submenu,{x, unmounted_submenu(ud_menu, x), data[ud_menu][device_type.."_icon"]})
  70. end
  71. end
  72. if check_remain_mounted_partition == 1 then
  73. table.insert(my_submenu,{"unmount all", unmount_multiple_partitions(ud_menu, k, mounted_partitions ), data[ud_menu]["umount_icon"]})
  74. --table.insert(my_submenu,{"Can\'t "..action, {{k .." busy"}}, data[ud_menu][action.."_icon"]})
  75. else
  76. table.insert(my_submenu,{action, udisks_send(ud_menu, action, k), data[ud_menu][action.."_icon"]})
  77. end
  78. table.insert(my_menu, {k, my_submenu, data[ud_menu][device_type.."_icon"]})
  79. end
  80. else
  81. my_menu={{"No media",""}}
  82. end
  83. data[ud_menu].menu= awful.menu({ items = my_menu,
  84. })
  85. return ud_menu
  86. end
  87. local function display_menu(ud_menu)
  88. ud_menu:buttons(awful.util.table.join(
  89. awful.button({ }, 1, function()
  90. if data[ud_menu].menu_visible == "false" then
  91. data[ud_menu].menu_visible = "true"
  92. generate_menu(ud_menu )
  93. data[ud_menu].menu:show()
  94. else
  95. data[ud_menu].menu:hide()
  96. data[ud_menu].menu_visible = "false"
  97. end
  98. end),
  99. awful.button({ }, 3, function()
  100. data[ud_menu].menu:hide()
  101. data[ud_menu].menu_visible = "false"
  102. end)
  103. ))
  104. end
  105. function udisks_glue.insert_device(ud_menu,device, mount_point, device_type)
  106. -- generate the device_name
  107. if device_type == "Usb" then
  108. device_name = string.gsub(device,"%d*","")
  109. else
  110. device_name=device
  111. end
  112. -- add all_devices entry:
  113. -- check if device is already registred
  114. if data[ud_menu].all_devices[device_name] == nil then
  115. data[ud_menu].all_devices[device_name]={device}
  116. data[ud_menu].devices_type[device_name] = device_type
  117. else
  118. partition_already_registred = 0
  119. for i, v in ipairs(data[ud_menu].all_devices[device_name]) do
  120. if v == device then
  121. partition_already_registred = 1
  122. end
  123. end
  124. if partition_already_registred == 0 then
  125. table.insert(data[ud_menu].all_devices[device_name],device)
  126. end
  127. end
  128. data[ud_menu].partition_state[device]="unmounted"
  129. data[ud_menu].menu:hide()
  130. data[ud_menu].menu_visible = "false"
  131. naughty.notify({title = device_type..":", text = device .." inserted", timeout = 10})
  132. end
  133. function udisks_glue.mount_device(ud_menu,device, mount_point, device_type)
  134. -- generate the device_name
  135. if device_type == "Usb" then
  136. device_name = string.gsub(device,"%d*","")
  137. else
  138. device_name=device
  139. end
  140. -- add all_devices entry:
  141. -- check if device is already registred
  142. if data[ud_menu].all_devices[device_name] == nil then
  143. data[ud_menu].all_devices[device_name]={device}
  144. data[ud_menu].devices_type[device_name] = device_type
  145. else
  146. partition_already_registred = 0
  147. for i, v in ipairs(data[ud_menu].all_devices[device_name]) do
  148. if v == device then
  149. partition_already_registred = 1
  150. end
  151. end
  152. if partition_already_registred == 0 then
  153. table.insert(data[ud_menu].all_devices[device_name],device)
  154. end
  155. end
  156. data[ud_menu].partition_state[device]="mounted"
  157. data[ud_menu].menu:hide()
  158. data[ud_menu].menu_visible = "false"
  159. naughty.notify({title = device_type..":", text =device .. " mounted on" .. mount_point, timeout = 10})
  160. return ud_menu
  161. end
  162. function unmount_device(ud_menu, device, mount_point, device_type)
  163. data[ud_menu].partition_state[device]="unmounted"
  164. data[ud_menu].menu:hide()
  165. data[ud_menu].menu_visible = "false"
  166. naughty.notify({title = device_type..":", text = device .." unmounted", timeout = 10})
  167. end
  168. function remove_device(ud_menu, device, mount_point, device_type )
  169. local device_name=""
  170. if device_type == "Usb" then
  171. device_name=string.gsub(device,"%d*","")
  172. else
  173. device_name = device
  174. end
  175. --Remove the partitions
  176. if data[ud_menu].all_devices[device_name] ~= nil then
  177. for i,v in ipairs(data[ud_menu].all_devices[device_name]) do
  178. if v == device then
  179. table.remove(data[ud_menu].all_devices[device_name],i)
  180. helpers.hash_remove(data[ud_menu].partition_state, device)
  181. end
  182. end
  183. end
  184. --Remove the device if no remaining partition
  185. if data[ud_menu].all_devices[device_name] ~= nil and #data[ud_menu].all_devices[device_name] == 0 then
  186. helpers.hash_remove(data[ud_menu].all_devices, device_name)
  187. helpers.hash_remove(data[ud_menu].devices_type, device_name)
  188. end
  189. data[ud_menu].menu:hide()
  190. data[ud_menu].menu_visible = "false"
  191. naughty.notify({title = device_type ..":", text = device .." removed", timeout = 10})
  192. end
  193. ---Define the icon for the mount action in the menu.
  194. --@usage ud_widget:set_mount_icon(icon)
  195. --@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_mount_icon
  196. --@param an_image an image file name
  197. function set_mount_icon(ud_menu,an_image)
  198. data[ud_menu].mount_icon=an_image
  199. return ud_menu
  200. end
  201. ---Define the icon for the umount action in the menu.
  202. --@usage ud_widget:set_umount_icon(icon)
  203. --@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_umount_icon
  204. --@param an_image an image file name
  205. function set_umount_icon(ud_menu,an_image)
  206. data[ud_menu].umount_icon=an_image
  207. return ud_menu
  208. end
  209. ---Define the icon for the detach action in the menu.
  210. --@usage ud_widget:set_detach_icon(icon)
  211. --@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_detach_icon
  212. --@param an_image an image file name
  213. function set_detach_icon(ud_menu,an_image)
  214. data[ud_menu].detach_icon=an_image
  215. return ud_menu
  216. end
  217. ---Define the icon for eject action in the menu.
  218. --@usage ud_widget:set_eject_icon(icon)
  219. --@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_eject_icon
  220. --@param an_image an image file name
  221. function set_eject_icon(ud_menu,an_image)
  222. data[ud_menu].eject_icon=an_image
  223. return ud_menu
  224. end
  225. ---Define the icon for usb devices in the menu.
  226. --@usage ud_widget:set_Usb_icon(icon)
  227. --@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_Usb_icon
  228. --@param an_image an image file name
  229. function set_Usb_icon(ud_menu,an_image)
  230. data[ud_menu].Usb_icon=an_image
  231. return ud_menu
  232. end
  233. ---Define the icon for Cdrom devices in the menu.
  234. --@usage ud_widget:set_Cdrom_icon(icon)
  235. --@param ud_menu the udisk-glue menu widget or nothing if you use widget:set_Cdrom_icon
  236. --@param an_image an image file name
  237. function set_Cdrom_icon(ud_menu,an_image)
  238. data[ud_menu].Cdrom_icon=an_image
  239. return ud_menu
  240. end
  241. function udisks_glue.new(args)
  242. local args = args or {}
  243. local ud_menu
  244. ud_menu = wibox.widget.imagebox()
  245. ud_menu:set_image(args.menu_icon)
  246. data[ud_menu]={ image = menu_icon,
  247. all_devices= {},
  248. devices_type={},
  249. partition_state={},
  250. menu_visible = "false",
  251. menu={},
  252. Cdrom_icon=args.Cdrom_icon,
  253. Usb_icon=args.Usb_icon,
  254. mount_icon=args.mount_icon,
  255. umount_icon=args.umount_icon,
  256. detach_icon=args.detach_icon,
  257. eject_icon=args.eject_icon,
  258. }
  259. ud_menu.insert_device = udisks_glue.insert_device
  260. ud_menu.mount_device = udisks_glue.mount_device
  261. ud_menu.unmount_device = unmount_device
  262. ud_menu.remove_device = remove_device
  263. ud_menu.set_mount_icon = set_mount_icon
  264. ud_menu.set_umount_icon = set_umount_icon
  265. ud_menu.set_detach_icon = set_detach_icon
  266. ud_menu.set_eject_icon = set_eject_icon
  267. ud_menu.set_Usb_icon = set_Usb_icon
  268. ud_menu.set_Cdrom_icon = set_Cdrom_icon
  269. generate_menu(ud_menu)
  270. display_menu(ud_menu)
  271. return ud_menu
  272. end
  273. function udisks_glue.mt:__call(...)
  274. return udisks_glue.new(...)
  275. end
  276. return setmetatable(udisks_glue, udisks_glue.mt)