os_dialogs_app.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. -- Tool to get data
  2. local function get_file(key, value)
  3. if not value then
  4. return { name = key }
  5. elseif type(value) == 'string' then
  6. return { name = key, content = value }
  7. else
  8. return {
  9. name = key,
  10. -- content = value.content or value,
  11. owner = value.owner,
  12. ctime = value.ctime,
  13. }
  14. end
  15. end
  16. laptop.register_view('os:power_off', {
  17. fullscreen = true,
  18. })
  19. laptop.register_view('os:select_file', {
  20. formspec_func = function(app, mtos)
  21. local param = mtos.bdev:get_app_storage('ram', 'os:select_file').param
  22. local allowed_disks = param.allowed_disks or { 'system' }
  23. local selected_disk
  24. if param.selected_disk_name then
  25. selected_disk = mtos.bdev:get_app_storage(param.selected_disk_name, param.store_name)
  26. end
  27. -- Get all available storage and check the storage_list
  28. local storage_list = {}
  29. for idx, disk in ipairs(allowed_disks) do
  30. local dev_exists = mtos.bdev:get_app_storage(disk, param.store_name)
  31. if dev_exists then
  32. if not selected_disk then
  33. selected_disk = dev_exists
  34. param.selected_disk_name = disk
  35. end
  36. table.insert(storage_list, disk)
  37. end
  38. end
  39. -- Get sorted files list
  40. param.files_list = {}
  41. if selected_disk then
  42. for key, value in pairs(selected_disk) do
  43. table.insert(param.files_list, get_file(key, value))
  44. end
  45. end
  46. table.sort(param.files_list, function(a,b) return a.name<b.name end)
  47. -- adjust tilte
  48. if param.mode == 'save' then
  49. param.message = 'Save file as'
  50. param.button_txt = 'Save'
  51. elseif param.mode == 'open' then
  52. param.message = 'Open file'
  53. param.button_txt = 'Open'
  54. else
  55. param.message = 'select file'
  56. param.button_txt = 'Select'
  57. end
  58. app.app_info = param.message
  59. -- available devices
  60. local formspec = mtos.theme:get_bgcolor_box('0,1.5;1.2,6', 'contrast')
  61. for idx, store in ipairs(storage_list) do
  62. local icon_pos = '0.2,'..(idx+0.8)
  63. if store == param.selected_disk_name then
  64. formspec = formspec .. mtos.theme:get_bgcolor_box('0.1,'..(idx+0.7)..';1,1', 'table_highlight')
  65. end
  66. if store == 'removable' then
  67. formspec = formspec .. 'item_image_button['..icon_pos..';1,1;'.. mtos.bdev:get_removable_disk().def.name..';disksel_removable;]'
  68. elseif store == 'hdd' then
  69. formspec = formspec .. 'item_image_button['..icon_pos..';1,1;'.. mtos.hwdef.nodename..';disksel_hdd;]'
  70. else
  71. formspec = formspec .. mtos.theme:get_button(icon_pos..';1,1', 'minor', 'disksel_'..store, store)
  72. end
  73. end
  74. formspec = formspec .. mtos.theme:get_button('0,7.7;1.4,1', 'minor', 'mount', 'Mount')
  75. -- Files table
  76. formspec = formspec .. mtos.theme:get_tableoptions().."tablecolumns[" ..
  77. "text;".. -- subject
  78. "text,padding=1.5;".. -- owner
  79. "text,padding=1.5,align=right]".. -- date
  80. "table[1.5,1.5;10.8,7.1;filesel;"
  81. local selected_idx = ""
  82. for idx, file in ipairs(param.files_list) do
  83. if idx > 1 then
  84. formspec = formspec..','
  85. end
  86. formspec = formspec..file.name..','..(file.owner or '')..','
  87. if file.ctime then
  88. formspec = formspec..os.date("%c", file.ctime)
  89. end
  90. if file.name == param.selected_file_name then
  91. selected_idx = idx
  92. end
  93. end
  94. formspec = formspec .. ";"..selected_idx.."]"
  95. -- Buttons
  96. if param.mode == 'save' then
  97. formspec = formspec .. mtos.theme:get_bgcolor_box('1.5,8.8;11,1.2', 'contrast')..
  98. mtos.theme:get_label("1.6,9.1", "File name:", "contrast").."field[3.2,9.3;5.5,0.8;filename;;"..(param.selected_file_name or "").."]"
  99. else
  100. formspec = formspec .. mtos.theme:get_label('1.5, 9.3', "Selected file: "..(param.selected_file_name or ""))
  101. end
  102. if param.selected_file_name and param.selected_file_name ~= "" and
  103. selected_disk and selected_disk[param.selected_file_name] then
  104. formspec = formspec .. mtos.theme:get_button('10.2,8.9;1.5,1', 'minor', 'delete', 'Delete')
  105. formspec = formspec .. mtos.theme:get_button('8.5,8.9;1.5,1', 'major', 'select', param.button_txt)
  106. elseif param.mode == 'save' then
  107. formspec = formspec .. mtos.theme:get_button('8.5,8.9;1.5,1', 'major', 'select', param.button_txt)
  108. end
  109. return formspec
  110. end,
  111. -- Input processing
  112. receive_fields_func = function(app, mtos, sender, fields)
  113. local st = mtos.bdev:get_app_storage('ram', 'os:select_file')
  114. local param = st.param
  115. if fields.filename then
  116. param.selected_file_name = fields.filename
  117. end
  118. for field, value in pairs(fields) do
  119. if field:sub(1,7) == 'disksel' then
  120. param.selected_disk_name = field:sub(9,-1)
  121. break
  122. end
  123. end
  124. if fields.mount then
  125. mtos:set_app("removable")
  126. elseif fields.filesel then
  127. local event = minetest.explode_table_event(fields.filesel)
  128. if param.files_list[event.row] then
  129. param.selected_file_name = param.files_list[event.row].name
  130. end
  131. elseif fields.select and param.selected_file_name and param.selected_file_name ~= ""
  132. and param.selected_disk_name and param.selected_disk_name ~= "" then
  133. param.prefix = param.prefix or ""
  134. local pass_fields = {
  135. [param.prefix..'selected_disk'] = param.selected_disk_name,
  136. [param.prefix..'selected_file'] = param.selected_file_name,
  137. }
  138. app:back_app(pass_fields, sender)
  139. st.param = nil
  140. elseif fields.delete and param.selected_disk_name and param.selected_file_name then
  141. local store = mtos.bdev:get_app_storage(param.selected_disk_name, param.store_name)
  142. if store then
  143. store[param.selected_file_name] = nil
  144. end
  145. end
  146. end
  147. })