2013-10-18 20:38:07 +00:00
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2014-01-18 15:15:44 +00:00
|
|
|
local DEBUG = require("dbg")
|
2013-10-18 20:38:07 +00:00
|
|
|
-- lfs
|
2012-05-27 21:43:00 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local FileChooser = Menu:extend{
|
2012-12-08 17:25:32 +00:00
|
|
|
height = Screen:getHeight(),
|
|
|
|
width = Screen:getWidth(),
|
2013-08-14 09:19:01 +00:00
|
|
|
no_title = true,
|
2012-12-12 01:35:49 +00:00
|
|
|
path = lfs.currentdir(),
|
2012-05-27 22:14:08 +00:00
|
|
|
parent = nil,
|
2013-12-28 09:49:39 +00:00
|
|
|
show_hidden = nil,
|
2013-12-19 15:16:30 +00:00
|
|
|
show_filesize = DSHOWFILESIZE,
|
2012-05-27 21:43:00 +00:00
|
|
|
filter = function(filename) return true end,
|
|
|
|
}
|
|
|
|
|
|
|
|
function FileChooser:init()
|
2013-08-22 04:01:00 +00:00
|
|
|
self.item_table = self:genItemTableFromPath(self.path)
|
2013-08-14 09:19:01 +00:00
|
|
|
Menu.init(self) -- call parent's init()
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
|
|
|
|
2013-08-22 04:01:00 +00:00
|
|
|
function FileChooser:genItemTableFromPath(path)
|
2012-05-27 21:43:00 +00:00
|
|
|
local dirs = {}
|
|
|
|
local files = {}
|
2013-08-14 09:19:01 +00:00
|
|
|
|
2012-05-27 21:43:00 +00:00
|
|
|
for f in lfs.dir(self.path) do
|
|
|
|
if self.show_hidden or not string.match(f, "^%.[^.]") then
|
|
|
|
local filename = self.path.."/"..f
|
|
|
|
local filemode = lfs.attributes(filename, "mode")
|
|
|
|
if filemode == "directory" and f ~= "." and f~=".." then
|
2013-02-19 13:30:35 +00:00
|
|
|
if self.dir_filter(filename) then
|
|
|
|
table.insert(dirs, f)
|
|
|
|
end
|
2012-05-27 21:43:00 +00:00
|
|
|
elseif filemode == "file" then
|
2013-02-19 13:30:35 +00:00
|
|
|
if self.file_filter(filename) then
|
2012-05-27 21:43:00 +00:00
|
|
|
table.insert(files, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(dirs)
|
2013-08-22 04:01:00 +00:00
|
|
|
if path ~= "/" then table.insert(dirs, 1, "..") end
|
2012-05-27 21:43:00 +00:00
|
|
|
table.sort(files)
|
|
|
|
|
2013-08-22 04:01:00 +00:00
|
|
|
local item_table = {}
|
2012-05-27 21:43:00 +00:00
|
|
|
for _, dir in ipairs(dirs) do
|
2013-08-22 04:01:00 +00:00
|
|
|
table.insert(item_table, { text = dir.."/", path = self.path.."/"..dir })
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
|
|
|
for _, file in ipairs(files) do
|
2013-12-19 15:16:30 +00:00
|
|
|
local full_path = self.path.."/"..file
|
|
|
|
if self.show_filesize then
|
|
|
|
local sstr = string.format("%4.1fM",lfs.attributes(full_path, "size")/1024/1024)
|
|
|
|
table.insert(item_table, { text = file, mandatory = sstr, path = full_path })
|
|
|
|
else
|
|
|
|
table.insert(item_table, { text = file, path = full_path })
|
|
|
|
end
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
2013-08-22 04:01:00 +00:00
|
|
|
|
|
|
|
return item_table
|
2013-08-14 09:19:01 +00:00
|
|
|
end
|
2012-05-27 21:43:00 +00:00
|
|
|
|
2013-08-14 09:19:01 +00:00
|
|
|
function FileChooser:changeToPath(path)
|
2013-08-23 11:31:46 +00:00
|
|
|
path = util.realpath(path)
|
2013-08-22 04:01:00 +00:00
|
|
|
self.path = path
|
2014-01-22 18:23:23 +00:00
|
|
|
self:refreshPath()
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:refreshPath()
|
|
|
|
self:swithItemTable(nil, self:genItemTableFromPath(self.path))
|
2012-05-27 21:43:00 +00:00
|
|
|
end
|
2012-05-27 22:14:08 +00:00
|
|
|
|
2013-08-25 04:00:06 +00:00
|
|
|
function FileChooser:toggleHiddenFiles()
|
|
|
|
self.show_hidden = not self.show_hidden
|
|
|
|
self:swithItemTable(nil, self:genItemTableFromPath(self.path))
|
|
|
|
end
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
function FileChooser:onMenuSelect(item)
|
|
|
|
if lfs.attributes(item.path, "mode") == "directory" then
|
|
|
|
self:changeToPath(item.path)
|
2012-05-27 22:14:08 +00:00
|
|
|
else
|
2012-06-10 15:36:19 +00:00
|
|
|
self:onFileSelect(item.path)
|
2012-05-27 22:14:08 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2012-06-10 15:36:19 +00:00
|
|
|
|
2014-01-18 15:15:44 +00:00
|
|
|
function FileChooser:onMenuHold(item)
|
|
|
|
self:onFileHold(item.path)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
function FileChooser:onFileSelect(file)
|
|
|
|
UIManager:close(self)
|
|
|
|
return true
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2014-01-18 15:15:44 +00:00
|
|
|
function FileChooser:onFileHold(file)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FileChooser
|