2
0
mirror of https://github.com/koreader/koreader synced 2024-11-04 12:00:25 +00:00
koreader/frontend/ui/widget/filechooser.lua

102 lines
2.4 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local Menu = require("ui/widget/menu")
local Screen = require("ui/screen")
local UIManager = require("ui/uimanager")
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{
height = Screen:getHeight(),
width = Screen:getWidth(),
no_title = true,
path = lfs.currentdir(),
2012-05-27 22:14:08 +00:00
parent = nil,
show_hidden = nil,
show_filesize = DSHOWFILESIZE,
2012-05-27 21:43:00 +00:00
filter = function(filename) return true end,
}
function FileChooser:init()
self.item_table = self:genItemTableFromPath(self.path)
Menu.init(self) -- call parent's init()
2012-05-27 21:43:00 +00:00
end
function FileChooser:genItemTableFromPath(path)
2012-05-27 21:43:00 +00:00
local dirs = {}
local files = {}
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 path ~= "/" then table.insert(dirs, 1, "..") end
2012-05-27 21:43:00 +00:00
table.sort(files)
local item_table = {}
2012-05-27 21:43:00 +00:00
for _, dir in ipairs(dirs) do
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
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
return item_table
end
2012-05-27 21:43:00 +00:00
function FileChooser:changeToPath(path)
path = util.realpath(path)
self.path = path
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
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
self:onFileSelect(item.path)
2012-05-27 22:14:08 +00:00
end
return true
end
function FileChooser:onMenuHold(item)
self:onFileHold(item.path)
return true
end
function FileChooser:onFileSelect(file)
UIManager:close(self)
return true
end
2013-10-18 20:38:07 +00:00
function FileChooser:onFileHold(file)
return true
end
2013-10-18 20:38:07 +00:00
return FileChooser