2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2016-12-03 13:19:35 +00:00
|
|
|
local DocSettings = require("docsettings")
|
2017-02-20 14:08:35 +00:00
|
|
|
local Font = require("ui/font")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local ffi = require("ffi")
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2014-06-04 09:22:45 +00:00
|
|
|
local util = require("ffi/util")
|
2014-09-10 04:21:07 +00:00
|
|
|
local _ = require("gettext")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Screen = Device.screen
|
2017-02-12 02:55:31 +00:00
|
|
|
local getFileNameSuffix = require("util").getFileNameSuffix
|
2017-10-20 15:48:32 +00:00
|
|
|
local getFriendlySize = require("util").getFriendlySize
|
2017-04-29 08:38:09 +00:00
|
|
|
|
2014-05-15 12:44:48 +00:00
|
|
|
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
|
|
|
|
2017-02-12 02:55:31 +00:00
|
|
|
local function kobostrcoll(str1, str2)
|
|
|
|
return str1 < str2
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local FileChooser = Menu:extend{
|
2017-04-29 08:38:09 +00:00
|
|
|
cface = Font:getFace("smallinfofont"),
|
2014-03-13 13:52:43 +00:00
|
|
|
no_title = true,
|
|
|
|
path = lfs.currentdir(),
|
|
|
|
parent = nil,
|
|
|
|
show_hidden = nil,
|
2014-09-10 04:21:07 +00:00
|
|
|
exclude_dirs = {"%.sdr$"},
|
2014-10-30 14:41:49 +00:00
|
|
|
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
|
2018-01-13 22:38:53 +00:00
|
|
|
perpage = G_reader_settings:readSetting("items_per_page"),
|
2018-02-09 21:33:15 +00:00
|
|
|
goto_letter = true,
|
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
|
2016-07-05 00:38:04 +00:00
|
|
|
if dirname:match(pattern) then return false end
|
2014-09-10 04:21:07 +00:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2016-04-14 21:13:53 +00:00
|
|
|
self.list = function(path, dirs, files)
|
|
|
|
-- lfs.dir directory without permission will give error
|
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, path)
|
|
|
|
if ok then
|
|
|
|
for f in iter, dir_obj do
|
|
|
|
if self.show_hidden or not string.match(f, "^%.[^.]") then
|
|
|
|
local filename = path.."/"..f
|
|
|
|
local attributes = lfs.attributes(filename)
|
|
|
|
if attributes ~= nil then
|
|
|
|
if attributes.mode == "directory" and f ~= "." and f~=".." then
|
|
|
|
if self.dir_filter(filename) then
|
2017-02-12 02:55:31 +00:00
|
|
|
table.insert(dirs, {name = f,
|
|
|
|
suffix = getFileNameSuffix(f),
|
|
|
|
fullpath = filename,
|
|
|
|
attr = attributes})
|
2016-04-14 21:13:53 +00:00
|
|
|
end
|
|
|
|
elseif attributes.mode == "file" then
|
|
|
|
if self.file_filter == nil or self.file_filter(filename) then
|
2018-01-17 19:10:25 +00:00
|
|
|
local percent_finished = 0
|
2018-01-19 23:01:09 +00:00
|
|
|
if self.collate == "percent_unopened_first" or self.collate == "percent_unopened_last" then
|
|
|
|
if DocSettings:hasSidecarFile(filename) then
|
|
|
|
local docinfo = DocSettings:open(filename)
|
|
|
|
percent_finished = docinfo.data.percent_finished
|
|
|
|
if percent_finished == nil then
|
|
|
|
percent_finished = 0
|
|
|
|
end
|
2018-01-17 19:10:25 +00:00
|
|
|
end
|
|
|
|
end
|
2017-02-12 02:55:31 +00:00
|
|
|
table.insert(files, {name = f,
|
|
|
|
suffix = getFileNameSuffix(f),
|
|
|
|
fullpath = filename,
|
2018-01-17 19:10:25 +00:00
|
|
|
attr = attributes,
|
|
|
|
percent_finished = percent_finished })
|
2016-04-14 21:13:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-12 02:55:31 +00:00
|
|
|
local strcoll_func = strcoll
|
2014-11-16 18:10:37 +00:00
|
|
|
-- circumvent string collating in Kobo devices. See issue koreader/koreader#686
|
|
|
|
if Device:isKobo() then
|
2017-02-12 02:55:31 +00:00
|
|
|
strcoll_func = kobostrcoll
|
|
|
|
end
|
|
|
|
self.strcoll = function(a, b)
|
|
|
|
if a == nil and b == nil then
|
|
|
|
return false
|
|
|
|
elseif a == nil then
|
|
|
|
return true
|
|
|
|
elseif b == nil then
|
|
|
|
return false
|
|
|
|
elseif DALPHA_SORT_CASE_INSENSITIVE then
|
|
|
|
return strcoll_func(string.lower(a), string.lower(b))
|
|
|
|
else
|
|
|
|
return strcoll_func(a, b)
|
|
|
|
end
|
2014-11-16 18:10:37 +00:00
|
|
|
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
|
|
|
|
2016-04-14 21:13:53 +00:00
|
|
|
self.list(path, dirs, files)
|
2014-10-30 14:41:49 +00:00
|
|
|
|
2017-02-12 02:55:31 +00:00
|
|
|
local sorting
|
2014-10-30 14:41:49 +00:00
|
|
|
if self.collate == "strcoll" then
|
2017-02-12 02:55:31 +00:00
|
|
|
sorting = function(a, b)
|
|
|
|
return self.strcoll(a.name, b.name)
|
|
|
|
end
|
|
|
|
elseif self.collate == "access" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
if DocSettings:hasSidecarFile(a.fullpath) and not DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
return true
|
2014-11-16 18:27:31 +00:00
|
|
|
end
|
2017-02-12 02:55:31 +00:00
|
|
|
if not DocSettings:hasSidecarFile(a.fullpath) and DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
return false
|
2014-11-16 18:27:31 +00:00
|
|
|
end
|
2017-02-12 02:55:31 +00:00
|
|
|
return a.attr.access > b.attr.access
|
2014-11-16 18:10:37 +00:00
|
|
|
end
|
2017-02-12 02:55:31 +00:00
|
|
|
elseif self.collate == "modification" then
|
2014-10-30 14:41:49 +00:00
|
|
|
sorting = function(a, b)
|
2017-02-12 02:55:31 +00:00
|
|
|
return a.attr.modification > b.attr.modification
|
|
|
|
end
|
|
|
|
elseif self.collate == "change" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
if DocSettings:hasSidecarFile(a.fullpath) and not DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
if not DocSettings:hasSidecarFile(a.fullpath) and DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return a.attr.change > b.attr.change
|
|
|
|
end
|
|
|
|
elseif self.collate == "size" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
return a.attr.size < b.attr.size
|
|
|
|
end
|
|
|
|
elseif self.collate == "type" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
if a.suffix == nil and b.suffix == nil then
|
|
|
|
return self.strcoll(a.name, b.name)
|
2014-10-30 14:41:49 +00:00
|
|
|
else
|
2017-02-12 02:55:31 +00:00
|
|
|
return self.strcoll(a.suffix, b.suffix)
|
2014-10-30 14:41:49 +00:00
|
|
|
end
|
|
|
|
end
|
2018-01-17 19:10:25 +00:00
|
|
|
elseif self.collate == "percent_unopened_first" or self.collate == "percent_unopened_last" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
if DocSettings:hasSidecarFile(a.fullpath) and not DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
if self.collate == "percent_unopened_first" then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not DocSettings:hasSidecarFile(a.fullpath) and DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
if self.collate == "percent_unopened_first" then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not DocSettings:hasSidecarFile(a.fullpath) and not DocSettings:hasSidecarFile(b.fullpath) then
|
|
|
|
return a.name < b.name
|
|
|
|
end
|
|
|
|
|
|
|
|
if a.attr.mode == "directory" then return a.name < b.name end
|
|
|
|
if b.attr.mode == "directory" then return a.name < b.name end
|
|
|
|
|
|
|
|
return a.percent_finished < b.percent_finished
|
|
|
|
end
|
2017-02-12 02:55:31 +00:00
|
|
|
else
|
|
|
|
sorting = function(a, b)
|
|
|
|
return a.name < b.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if self.reverse_collate then
|
|
|
|
local sorting_unreversed = sorting
|
|
|
|
sorting = function(a, b) return sorting_unreversed(b, a) end
|
2014-10-30 14:41:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(dirs, sorting)
|
2017-02-25 13:57:30 +00:00
|
|
|
if path ~= "/" then table.insert(dirs, 1, {name = ".."}) end
|
2014-10-30 14:41:49 +00:00
|
|
|
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
|
2016-04-19 07:59:02 +00:00
|
|
|
-- count sume of directories and files inside dir
|
|
|
|
local sub_dirs = {}
|
|
|
|
local dir_files = {}
|
2016-02-16 06:34:28 +00:00
|
|
|
local subdir_path = self.path.."/"..dir.name
|
2016-04-19 07:59:02 +00:00
|
|
|
self.list(subdir_path, sub_dirs, dir_files)
|
2016-12-25 07:43:58 +00:00
|
|
|
local num_items = #sub_dirs + #dir_files
|
|
|
|
local istr
|
|
|
|
if num_items == 1 then
|
|
|
|
istr = _("1 item")
|
|
|
|
else
|
|
|
|
istr = util.template(_("%1 items"), num_items)
|
|
|
|
end
|
2014-09-25 14:21:25 +00:00
|
|
|
table.insert(item_table, {
|
2017-02-25 13:57:30 +00:00
|
|
|
text = dir.name == ".." and "⬆ ../" or dir.name.."/",
|
2014-09-25 14:21:25 +00:00
|
|
|
mandatory = istr,
|
2017-10-13 16:43:02 +00:00
|
|
|
path = subdir_path,
|
|
|
|
is_go_up = dir.name == ".."
|
2014-09-25 14:21:25 +00:00
|
|
|
})
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2016-12-25 07:43:09 +00:00
|
|
|
|
2016-12-27 06:52:53 +00:00
|
|
|
-- set to false to show all files in regular font
|
|
|
|
-- set to "opened" to show opened files in bold
|
|
|
|
-- otherwise, show new files in bold
|
|
|
|
local show_file_in_bold = G_reader_settings:readSetting("show_file_in_bold")
|
2016-12-25 07:43:09 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
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
|
2017-10-20 15:48:32 +00:00
|
|
|
local sstr = getFriendlySize(file_size)
|
2016-12-27 06:52:53 +00:00
|
|
|
local file_item = {
|
2014-10-30 14:41:49 +00:00
|
|
|
text = file.name,
|
2014-09-25 14:21:25 +00:00
|
|
|
mandatory = sstr,
|
|
|
|
path = full_path
|
2016-12-27 06:52:53 +00:00
|
|
|
}
|
|
|
|
if show_file_in_bold ~= false then
|
2017-02-08 18:37:36 +00:00
|
|
|
file_item.bold = DocSettings:hasSidecarFile(full_path)
|
2016-12-27 06:52:53 +00:00
|
|
|
if show_file_in_bold ~= "opened" then
|
|
|
|
file_item.bold = not file_item.bold
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(item_table, file_item)
|
2014-09-25 14:21:25 +00:00
|
|
|
end
|
2016-12-25 07:43:58 +00:00
|
|
|
-- lfs.dir iterated node string may be encoded with some weird codepage on
|
|
|
|
-- Windows we need to encode them to utf-8
|
2014-09-25 14:21:25 +00:00
|
|
|
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()
|
2017-10-21 17:53:56 +00:00
|
|
|
local itemmatch = nil
|
|
|
|
if self.focused_path then
|
|
|
|
itemmatch = {path = self.focused_path}
|
|
|
|
-- We use focused_path only once, but remember it
|
|
|
|
-- for CoverBrower to re-apply it on startup if needed
|
|
|
|
self.prev_focused_path = self.focused_path
|
|
|
|
self.focused_path = nil
|
|
|
|
end
|
|
|
|
self:switchItemTable(nil, self:genItemTableFromPath(self.path), self.path_items[self.path], itemmatch)
|
2014-09-10 04:21:07 +00:00
|
|
|
end
|
|
|
|
|
2017-10-13 16:43:02 +00:00
|
|
|
function FileChooser:changeToPath(path, focused_path)
|
2014-03-13 13:52:43 +00:00
|
|
|
path = util.realpath(path)
|
|
|
|
self.path = path
|
2017-10-13 16:43:02 +00:00
|
|
|
if focused_path then
|
2017-10-21 17:53:56 +00:00
|
|
|
self.focused_path = focused_path
|
2017-10-13 16:43:02 +00:00
|
|
|
end
|
2017-10-21 17:53:56 +00:00
|
|
|
self:refreshPath()
|
2016-07-05 00:38:04 +00:00
|
|
|
self:onPathChanged(path)
|
2014-01-22 18:23:23 +00:00
|
|
|
end
|
|
|
|
|
2017-10-13 16:43:02 +00:00
|
|
|
function FileChooser:changePageToPath(path)
|
|
|
|
if not path then return end
|
|
|
|
for num, item in ipairs(self.item_table) do
|
|
|
|
if item.path == path then
|
|
|
|
local page = math.floor((num-1) / self.perpage) + 1
|
|
|
|
if page ~= self.page then
|
|
|
|
self:onGotoPage(page)
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2017-10-13 16:43:02 +00:00
|
|
|
self:changeToPath(item.path, item.is_go_up and self.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
|
|
|
|
|
2016-07-05 00:38:04 +00:00
|
|
|
function FileChooser:onPathChanged(path)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FileChooser
|