2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/ui/widget/filechooser.lua

75 lines
1.8 KiB
Lua
Raw Normal View History

2013-03-12 17:18:53 +00:00
require "ui/widget/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:compressPath(item_path)
-- compress paths like "test/pdf/../epub" into "test/epub"
local path = item_path
while path:match("/[^/]+[/][\\.][\\.]") do
path = path:gsub("/[^/]+[/][\\.][\\.]", "")
end
return path
end
2012-05-27 21:43:00 +00:00
function FileChooser:changeToPath(path)
path = self:compressPath(path)
2012-05-27 21:43:00 +00:00
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