2014-08-14 11:49:42 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2014-08-21 05:54:41 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Menu = require("ui/widget/menu")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Screen = require("device").screen
|
|
|
|
local Device = require("device")
|
2014-06-04 09:22:45 +00:00
|
|
|
local util = require("ffi/util")
|
2014-08-21 05:54:41 +00:00
|
|
|
local DEBUG = require("dbg")
|
2014-09-10 04:21:07 +00:00
|
|
|
local _ = require("gettext")
|
2014-05-15 12:44:48 +00:00
|
|
|
local ffi = require("ffi")
|
|
|
|
ffi.cdef[[
|
2014-11-16 18:10:37 +00:00
|
|
|
int strcoll (const char *str1, const char *str2);
|
2014-05-15 12:44:48 +00:00
|
|
|
]]
|
|
|
|
|
|
|
|
-- string sort function respecting LC_COLLATE
|
|
|
|
local function strcoll(str1, str2)
|
2014-11-16 18:10:37 +00:00
|
|
|
return ffi.C.strcoll(str1, str2) < 0
|
2014-05-15 12:44:48 +00:00
|
|
|
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
|
|
|
no_title = true,
|
|
|
|
path = lfs.currentdir(),
|
|
|
|
parent = nil,
|
|
|
|
show_hidden = nil,
|
|
|
|
filter = function(filename) return true end,
|
2014-09-10 04:21:07 +00:00
|
|
|
exclude_dirs = {"%.sdr$"},
|
2014-10-30 14:41:49 +00:00
|
|
|
strcoll = strcoll,
|
|
|
|
collate = "strcoll", -- or collate = "access",
|
|
|
|
reverse_collate = false,
|
2015-02-01 17:36:49 +00:00
|
|
|
path_items = {}, -- store last browsed location(item index) for each path
|
2012-05-27 21:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function FileChooser:init()
|
2014-11-25 08:28:27 +00:00
|
|
|
self.width = Screen:getWidth()
|
2014-09-10 04:21:07 +00:00
|
|
|
-- common dir filter
|
|
|
|
self.dir_filter = function(dirname)
|
|
|
|
for _, pattern in ipairs(self.exclude_dirs) do
|
|
|
|
if dirname:match(pattern) then return end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2014-11-16 18:10:37 +00:00
|
|
|
-- circumvent string collating in Kobo devices. See issue koreader/koreader#686
|
|
|
|
if Device:isKobo() then
|
|
|
|
self.strcoll = function(a, b) return a < b end
|
|
|
|
end
|
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
|
|
|
|
|
2013-08-22 04:01:00 +00:00
|
|
|
function FileChooser:genItemTableFromPath(path)
|
2014-03-13 13:52:43 +00:00
|
|
|
local dirs = {}
|
|
|
|
local files = {}
|
2013-08-14 09:19:01 +00:00
|
|
|
|
2014-06-04 11:13:41 +00:00
|
|
|
-- lfs.dir directory without permission will give error
|
2014-08-25 04:20:19 +00:00
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, self.path)
|
|
|
|
if ok then
|
|
|
|
for f in iter, dir_obj do
|
2014-06-04 11:13:41 +00:00
|
|
|
if self.show_hidden or not string.match(f, "^%.[^.]") then
|
|
|
|
local filename = self.path.."/"..f
|
2014-10-30 14:41:49 +00:00
|
|
|
local attributes = lfs.attributes(filename)
|
|
|
|
if attributes.mode == "directory" and f ~= "." and f~=".." then
|
2014-06-04 11:13:41 +00:00
|
|
|
if self.dir_filter(filename) then
|
2014-10-30 14:41:49 +00:00
|
|
|
table.insert(dirs, {name = f, attr = attributes})
|
2014-06-04 11:13:41 +00:00
|
|
|
end
|
2014-10-30 14:41:49 +00:00
|
|
|
elseif attributes.mode == "file" then
|
2014-06-04 11:13:41 +00:00
|
|
|
if self.file_filter(filename) then
|
2014-10-30 14:41:49 +00:00
|
|
|
table.insert(files, {name = f, attr = attributes})
|
2014-06-04 11:13:41 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-30 14:41:49 +00:00
|
|
|
|
|
|
|
local sorting = nil
|
|
|
|
local reverse = self.reverse_collate
|
|
|
|
if self.collate == "strcoll" then
|
2014-11-16 18:27:31 +00:00
|
|
|
if DALPHA_SORT_CASE_INSENSITIVE then
|
|
|
|
sorting = function(a, b)
|
|
|
|
return self.strcoll(string.lower(a.name), string.lower(b.name)) == not reverse
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sorting = function(a, b)
|
|
|
|
return self.strcoll(a.name, b.name) == not reverse
|
|
|
|
end
|
2014-11-16 18:10:37 +00:00
|
|
|
end
|
2014-10-30 14:41:49 +00:00
|
|
|
elseif self.collate == "access" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
if reverse then
|
|
|
|
return a.attr.access < b.attr.access
|
|
|
|
else
|
|
|
|
return a.attr.access > b.attr.access
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(dirs, sorting)
|
|
|
|
if path ~= "/" then table.insert(dirs, 1, {name = ".."}) end
|
|
|
|
table.sort(files, sorting)
|
2012-05-27 21:43:00 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
local item_table = {}
|
2014-09-10 04:21:07 +00:00
|
|
|
for i, dir in ipairs(dirs) do
|
2014-10-30 14:41:49 +00:00
|
|
|
local path = self.path.."/"..dir.name
|
2014-09-10 04:21:07 +00:00
|
|
|
local items = 0
|
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, path)
|
|
|
|
if ok then
|
|
|
|
for f in iter, dir_obj do
|
|
|
|
items = items + 1
|
|
|
|
end
|
|
|
|
-- exclude "." and ".."
|
|
|
|
items = items - 2
|
|
|
|
end
|
2014-11-19 12:25:32 +00:00
|
|
|
local istr = util.template(
|
|
|
|
items == 0 and _("0 items")
|
|
|
|
or items == 1 and _("1 item")
|
|
|
|
or _("%1 items"), items)
|
2014-09-25 14:21:25 +00:00
|
|
|
table.insert(item_table, {
|
2014-10-30 14:41:49 +00:00
|
|
|
text = dir.name.."/",
|
2014-09-25 14:21:25 +00:00
|
|
|
mandatory = istr,
|
|
|
|
path = path
|
|
|
|
})
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
for _, file in ipairs(files) do
|
2014-10-30 14:41:49 +00:00
|
|
|
local full_path = self.path.."/"..file.name
|
2014-09-25 14:21:25 +00:00
|
|
|
local file_size = lfs.attributes(full_path, "size") or 0
|
2014-09-10 04:21:07 +00:00
|
|
|
local sstr = ""
|
|
|
|
if file_size > 1024*1024 then
|
|
|
|
sstr = string.format("%4.1f MB", file_size/1024/1024)
|
|
|
|
elseif file_size > 1024 then
|
|
|
|
sstr = string.format("%4.1f KB", file_size/1024)
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2014-09-10 04:21:07 +00:00
|
|
|
sstr = string.format("%d B", file_size)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-09-25 14:21:25 +00:00
|
|
|
table.insert(item_table, {
|
2014-10-30 14:41:49 +00:00
|
|
|
text = file.name,
|
2014-09-25 14:21:25 +00:00
|
|
|
mandatory = sstr,
|
|
|
|
path = full_path
|
|
|
|
})
|
|
|
|
end
|
|
|
|
-- lfs.dir iterated node string may be encoded with some weird codepage on Windows
|
|
|
|
-- we need to encode them to utf-8
|
|
|
|
if ffi.os == "Windows" then
|
|
|
|
for k, v in pairs(item_table) do
|
|
|
|
if v.text then
|
|
|
|
v.text = util.multiByteToUTF8(v.text) or ""
|
|
|
|
end
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-08-22 04:01:00 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
return item_table
|
2013-08-14 09:19:01 +00:00
|
|
|
end
|
2012-05-27 21:43:00 +00:00
|
|
|
|
2014-11-23 10:06:20 +00:00
|
|
|
function FileChooser:updateItems(select_number)
|
|
|
|
Menu.updateItems(self, select_number) -- call parent's updateItems()
|
2015-02-01 17:36:49 +00:00
|
|
|
self.path_items[self.path] = (self.page - 1) * self.perpage + (select_number or 1)
|
2014-11-23 10:06:20 +00:00
|
|
|
end
|
|
|
|
|
2014-09-10 04:21:07 +00:00
|
|
|
function FileChooser:refreshPath()
|
2015-02-01 17:36:49 +00:00
|
|
|
self:swithItemTable(nil, self:genItemTableFromPath(self.path), self.path_items[self.path])
|
2014-09-10 04:21:07 +00:00
|
|
|
end
|
|
|
|
|
2013-08-14 09:19:01 +00:00
|
|
|
function FileChooser:changeToPath(path)
|
2014-03-13 13:52:43 +00:00
|
|
|
path = util.realpath(path)
|
|
|
|
self.path = path
|
|
|
|
self:refreshPath()
|
2014-01-22 18:23:23 +00:00
|
|
|
end
|
|
|
|
|
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
|
2014-09-10 04:21:07 +00:00
|
|
|
self:refreshPath()
|
2013-08-25 04:00:06 +00:00
|
|
|
end
|
|
|
|
|
2014-10-30 14:41:49 +00:00
|
|
|
function FileChooser:setCollate(collate)
|
|
|
|
self.collate = collate
|
|
|
|
self:refreshPath()
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:toggleReverseCollate()
|
|
|
|
self.reverse_collate = not self.reverse_collate
|
|
|
|
self:refreshPath()
|
|
|
|
end
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
function FileChooser:onMenuSelect(item)
|
2014-06-04 11:13:41 +00:00
|
|
|
-- 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)
|
2014-06-04 11:13:41 +00:00
|
|
|
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
|
2012-06-10 15:36:19 +00:00
|
|
|
|
2014-01-18 15:15:44 +00:00
|
|
|
function FileChooser:onMenuHold(item)
|
2014-03-13 13:52:43 +00:00
|
|
|
self:onFileHold(item.path)
|
|
|
|
return true
|
2014-01-18 15:15:44 +00:00
|
|
|
end
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
function FileChooser:onFileSelect(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:close(self)
|
|
|
|
return true
|
2012-06-10 15:36:19 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2014-01-18 15:15:44 +00:00
|
|
|
function FileChooser:onFileHold(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2014-01-18 15:15:44 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FileChooser
|