2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/filechooser.lua

65 lines
1.5 KiB
Lua
Raw Normal View History

require "ui/menu"
2012-05-27 21:43:00 +00:00
FileChooser = Menu:new{
height = Screen:getHeight(),
width = Screen:getWidth(),
path = lfs.currentdir(),
2012-05-27 22:14:08 +00:00
parent = nil,
2012-05-27 21:43:00 +00:00
show_hidden = false,
filter = function(filename) return true end,
}
function FileChooser:init()
self:changeToPath(self.path)
end
function FileChooser:changeToPath(path)
local dirs = {}
local files = {}
2012-05-27 22:14:08 +00:00
self.path = path
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
if self.dir_filter(filename) then
table.insert(dirs, f)
end
2012-05-27 21:43:00 +00:00
elseif filemode == "file" then
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)
if self.path ~= "/" then table.insert(dirs, 1, "..") end
table.sort(files)
self.item_table = {}
for _, dir in ipairs(dirs) do
2012-05-27 22:14:08 +00:00
table.insert(self.item_table, { text = dir.."/", path = self.path.."/"..dir })
2012-05-27 21:43:00 +00:00
end
for _, file in ipairs(files) do
2012-05-27 22:14:08 +00:00
table.insert(self.item_table, { text = file, path = self.path.."/"..file })
2012-05-27 21:43:00 +00:00
end
Menu.init(self) -- call parent's init()
end
2012-05-27 22:14:08 +00:00
function FileChooser:onMenuSelect(item)
if lfs.attributes(item.path, "mode") == "directory" then
2012-05-27 22:14:08 +00:00
UIManager:close(self)
self:changeToPath(item.path)
2012-05-27 22:14:08 +00:00
UIManager:show(self)
else
self:onFileSelect(item.path)
2012-05-27 22:14:08 +00:00
end
return true
end
function FileChooser:onFileSelect(file)
UIManager:close(self)
return true
end