2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/ui/widget/filechooser.lua

117 lines
3.3 KiB
Lua
Raw Normal View History

2014-08-14 11:49:42 +00:00
local lfs = require("libs/libkoreader-lfs")
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")
2014-06-04 09:22:45 +00:00
local util = require("ffi/util")
2014-05-15 12:44:48 +00:00
local ffi = require("ffi")
ffi.cdef[[
int strcoll (char *str1, char *str2);
]]
-- string sort function respecting LC_COLLATE
local function strcoll(str1, str2)
return ffi.C.strcoll(ffi.cast("char*", str1), ffi.cast("char*", str2)) <= 0
end
2012-05-27 21:43:00 +00:00
2013-10-18 20:38:07 +00:00
local FileChooser = Menu:extend{
2014-03-13 13:52:43 +00:00
height = Screen:getHeight(),
width = Screen:getWidth(),
no_title = true,
path = lfs.currentdir(),
parent = nil,
show_hidden = nil,
show_filesize = DSHOWFILESIZE,
filter = function(filename) return true end,
2012-05-27 21:43:00 +00:00
}
function FileChooser:init()
2014-03-13 13:52:43 +00:00
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)
2014-03-13 13:52:43 +00:00
local dirs = {}
local files = {}
-- lfs.dir directory without permission will give error
if pcall(lfs.dir, self.path) then
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
elseif filemode == "file" then
if self.file_filter(filename) then
table.insert(files, f)
end
2014-03-13 13:52:43 +00:00
end
end
end
end
2014-05-15 12:44:48 +00:00
table.sort(dirs, strcoll)
2014-03-13 13:52:43 +00:00
if path ~= "/" then table.insert(dirs, 1, "..") end
2014-05-15 12:44:48 +00:00
table.sort(files, strcoll)
2012-05-27 21:43:00 +00:00
2014-03-13 13:52:43 +00:00
local item_table = {}
for _, dir in ipairs(dirs) do
table.insert(item_table, { text = dir.."/", path = self.path.."/"..dir })
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
end
2014-03-13 13:52:43 +00:00
return item_table
end
2012-05-27 21:43:00 +00:00
function FileChooser:changeToPath(path)
2014-03-13 13:52:43 +00:00
path = util.realpath(path)
self.path = path
self:refreshPath()
end
function FileChooser:refreshPath()
2014-03-13 13:52:43 +00:00
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()
2014-03-13 13:52:43 +00:00
self.show_hidden = not self.show_hidden
self:swithItemTable(nil, self:genItemTableFromPath(self.path))
2013-08-25 04:00:06 +00:00
end
function FileChooser:onMenuSelect(item)
-- parent directory of dir without permission get nil mode
-- we need to change to parent path in this case
if lfs.attributes(item.path, "mode") == "file" then
2014-03-13 13:52:43 +00:00
self:onFileSelect(item.path)
else
self:changeToPath(item.path)
2014-03-13 13:52:43 +00:00
end
return true
2012-05-27 22:14:08 +00:00
end
function FileChooser:onMenuHold(item)
2014-03-13 13:52:43 +00:00
self:onFileHold(item.path)
return true
end
function FileChooser:onFileSelect(file)
2014-03-13 13:52:43 +00:00
UIManager:close(self)
return true
end
2013-10-18 20:38:07 +00:00
function FileChooser:onFileHold(file)
2014-03-13 13:52:43 +00:00
return true
end
2013-10-18 20:38:07 +00:00
return FileChooser