2019-12-06 21:55:39 +00:00
|
|
|
local BD = require("ui/bidi")
|
2023-03-05 20:12:24 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2016-12-03 13:19:35 +00:00
|
|
|
local DocSettings = require("docsettings")
|
2018-05-13 11:07:23 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Menu = require("ui/widget/menu")
|
2023-03-05 20:12:24 +00:00
|
|
|
local OpenWithDialog = require("ui/widget/openwithdialog")
|
2017-04-29 08:38:09 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local ffi = require("ffi")
|
2018-10-14 06:49:00 +00:00
|
|
|
local ffiUtil = require("ffi/util")
|
2023-03-05 20:12:24 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2023-01-16 18:36:22 +00:00
|
|
|
local sort = require("sort")
|
2018-10-14 06:49:00 +00:00
|
|
|
local util = require("util")
|
2023-03-05 20:12:24 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
local Screen = Device.screen
|
|
|
|
local T = ffiUtil.template
|
2017-04-29 08:38:09 +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(),
|
2018-08-01 15:02:34 +00:00
|
|
|
show_path = true,
|
2014-03-13 13:52:43 +00:00
|
|
|
parent = nil,
|
2022-01-04 20:34:22 +00:00
|
|
|
show_hidden = false, -- set to true to show folders/files starting with "."
|
|
|
|
file_filter = nil, -- function defined in the caller, returns true for files to be shown
|
|
|
|
show_unsupported = false, -- set to true to ignore file_filter
|
2021-03-06 21:44:18 +00:00
|
|
|
-- NOTE: Input is *always* a relative entry name
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
exclude_dirs = { -- const
|
2021-03-06 21:44:18 +00:00
|
|
|
-- KOReader / Kindle
|
|
|
|
"%.sdr$",
|
|
|
|
-- Kobo
|
|
|
|
"^%.adobe%-digital%-editions$",
|
2021-08-19 15:01:36 +00:00
|
|
|
"^certificates$",
|
|
|
|
"^custom%-dict$",
|
|
|
|
"^dict$",
|
|
|
|
"^iink$",
|
|
|
|
"^kepub$",
|
|
|
|
"^markups$",
|
|
|
|
"^webstorage$",
|
2021-03-06 21:44:18 +00:00
|
|
|
"^%.kobo%-images$",
|
|
|
|
-- macOS
|
|
|
|
"^%.fseventsd$",
|
|
|
|
"^%.Trashes$",
|
|
|
|
"^%.Spotlight%-V100$",
|
|
|
|
-- *nix
|
|
|
|
"^%.Trash$",
|
|
|
|
"^%.Trash%-%d+$",
|
|
|
|
-- Windows
|
|
|
|
"^RECYCLED$",
|
|
|
|
"^RECYCLER$",
|
|
|
|
"^%$Recycle%.Bin$",
|
|
|
|
"^System Volume Information$",
|
|
|
|
-- Plato
|
|
|
|
"^%.thumbnail%-previews$",
|
|
|
|
"^%.reading%-states$",
|
|
|
|
},
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
exclude_files = { -- const
|
2021-08-19 15:01:36 +00:00
|
|
|
-- Kobo
|
|
|
|
"^BookReader%.sqlite",
|
|
|
|
"^KoboReader%.sqlite",
|
|
|
|
"^device%.salt%.conf$",
|
2021-03-06 21:44:18 +00:00
|
|
|
-- macOS
|
|
|
|
"^%.DS_Store$",
|
|
|
|
-- *nix
|
|
|
|
"^%.directory$",
|
|
|
|
-- Windows
|
|
|
|
"^Thumbs%.db$",
|
|
|
|
-- Calibre
|
|
|
|
"^driveinfo%.calibre$",
|
|
|
|
"^metadata%.calibre$",
|
|
|
|
-- Plato
|
|
|
|
"^%.fat32%-epoch$",
|
|
|
|
"^%.metadata%.json$",
|
|
|
|
},
|
2023-03-05 20:12:24 +00:00
|
|
|
collate = "strcoll",
|
2014-10-30 14:41:49 +00:00
|
|
|
reverse_collate = false,
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
path_items = nil, -- hash, store last browsed location (item index) for each path
|
2018-02-09 21:33:15 +00:00
|
|
|
goto_letter = true,
|
2012-05-27 21:43:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 16:25:30 +00:00
|
|
|
-- Cache of content we knew of for directories that are not readable
|
|
|
|
-- (i.e. /storage/emulated/ on Android that we can meet when coming
|
|
|
|
-- from readable /storage/emulated/0/ - so we know it contains "0/")
|
|
|
|
local unreadable_dir_content = {}
|
|
|
|
|
2021-07-18 18:21:39 +00:00
|
|
|
function FileChooser:show_dir(dirname)
|
|
|
|
for _, pattern in ipairs(self.exclude_dirs) do
|
|
|
|
if dirname:match(pattern) then return false end
|
2014-09-10 04:21:07 +00:00
|
|
|
end
|
2021-07-18 18:21:39 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:show_file(filename)
|
|
|
|
for _, pattern in ipairs(self.exclude_files) do
|
|
|
|
if filename:match(pattern) then return false end
|
2021-03-06 21:44:18 +00:00
|
|
|
end
|
2022-01-04 20:34:22 +00:00
|
|
|
return self.show_unsupported or self.file_filter == nil or self.file_filter(filename)
|
2021-07-18 18:21:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:init()
|
2023-03-05 20:12:24 +00:00
|
|
|
self.up_folder_arrow = BD.mirroredUILayout() and BD.ltr("../ ⬆") or "⬆ ../"
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
self.path_items = {}
|
2021-07-18 18:21:39 +00:00
|
|
|
self.width = Screen:getWidth()
|
2020-02-05 18:32:07 +00:00
|
|
|
self.list = function(path, dirs, files, count_only)
|
2016-04-14 21:13:53 +00:00
|
|
|
-- lfs.dir directory without permission will give error
|
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, path)
|
|
|
|
if ok then
|
2020-08-29 16:25:30 +00:00
|
|
|
unreadable_dir_content[path] = nil
|
2016-04-14 21:13:53 +00:00
|
|
|
for f in iter, dir_obj do
|
2022-01-04 20:34:22 +00:00
|
|
|
if self.show_hidden or not util.stringStartsWith(f, ".") then
|
2016-04-14 21:13:53 +00:00
|
|
|
local filename = path.."/"..f
|
|
|
|
local attributes = lfs.attributes(filename)
|
|
|
|
if attributes ~= nil then
|
2022-01-04 20:34:22 +00:00
|
|
|
local item = true
|
2020-02-05 02:14:21 +00:00
|
|
|
if attributes.mode == "directory" and f ~= "." and f ~= ".." then
|
2021-07-18 18:21:39 +00:00
|
|
|
if self:show_dir(f) then
|
2022-01-04 20:34:22 +00:00
|
|
|
if not count_only then
|
|
|
|
item = {name = f,
|
|
|
|
fullpath = filename,
|
|
|
|
attr = attributes,}
|
|
|
|
end
|
|
|
|
table.insert(dirs, item)
|
2016-04-14 21:13:53 +00:00
|
|
|
end
|
2020-02-05 02:14:21 +00:00
|
|
|
-- Always ignore macOS resource forks.
|
|
|
|
elseif attributes.mode == "file" and not util.stringStartsWith(f, "._") then
|
2021-07-18 18:21:39 +00:00
|
|
|
if self:show_file(f) then
|
2022-01-04 20:34:22 +00:00
|
|
|
if not count_only then
|
2023-02-12 23:14:19 +00:00
|
|
|
local percent_finished
|
2021-03-06 21:44:18 +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)
|
2023-02-12 23:14:19 +00:00
|
|
|
percent_finished = docinfo:readSetting("percent_finished")
|
2018-01-19 23:01:09 +00:00
|
|
|
end
|
2018-01-17 19:10:25 +00:00
|
|
|
end
|
2022-01-04 20:34:22 +00:00
|
|
|
item = {name = f,
|
|
|
|
fullpath = filename,
|
|
|
|
attr = attributes,
|
2023-03-05 20:12:24 +00:00
|
|
|
suffix = util.getFileNameSuffix(f),
|
2023-02-12 23:14:19 +00:00
|
|
|
percent_finished = percent_finished or 0,}
|
2018-01-17 19:10:25 +00:00
|
|
|
end
|
2022-01-04 20:34:22 +00:00
|
|
|
table.insert(files, item)
|
2016-04-14 21:13:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-08-29 16:25:30 +00:00
|
|
|
else -- error, probably "permission denied"
|
|
|
|
if unreadable_dir_content[path] then
|
|
|
|
-- Add this dummy item that will be replaced with a message
|
|
|
|
-- by genItemTableFromPath()
|
|
|
|
table.insert(dirs, {
|
|
|
|
name = "./.",
|
|
|
|
fullpath = path,
|
2020-08-30 03:27:17 +00:00
|
|
|
attr = lfs.attributes(path),
|
2020-08-29 16:25:30 +00:00
|
|
|
})
|
|
|
|
-- If we knew about some content (if we had come up from them
|
|
|
|
-- to this directory), have them shown
|
|
|
|
for k, v in pairs(unreadable_dir_content[path]) do
|
|
|
|
if v.attr and v.attr.mode == "directory" then
|
|
|
|
table.insert(dirs, v)
|
|
|
|
else
|
|
|
|
table.insert(files, v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-04-14 21:13:53 +00:00
|
|
|
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
|
|
|
|
|
2021-07-18 18:21:39 +00:00
|
|
|
function FileChooser:getSortingFunction(collate, reverse_collate)
|
2017-02-12 02:55:31 +00:00
|
|
|
local sorting
|
2021-07-18 18:21:39 +00:00
|
|
|
if collate == "strcoll" then
|
2017-02-12 02:55:31 +00:00
|
|
|
sorting = function(a, b)
|
2020-10-05 14:34:46 +00:00
|
|
|
return ffiUtil.strcoll(a.name, b.name)
|
2017-02-12 02:55:31 +00:00
|
|
|
end
|
2023-03-05 20:12:24 +00:00
|
|
|
elseif collate == "natural" then
|
|
|
|
local natsort
|
|
|
|
-- Only keep the cache if we're an *instance* of FileChooser
|
|
|
|
if self ~= FileChooser then
|
|
|
|
natsort, self.natsort_cache = sort.natsort_cmp(self.natsort_cache)
|
|
|
|
sorting = function(a, b)
|
|
|
|
return natsort(a.name, b.name)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
natsort = sort.natsort_cmp()
|
|
|
|
sorting = function(a, b)
|
|
|
|
return natsort(a.name, b.name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif self.collate == "strcoll_mixed" then
|
|
|
|
sorting = function(a, b)
|
|
|
|
if b.text == self.up_folder_arrow then return false end
|
|
|
|
return ffiUtil.strcoll(a.text, b.text)
|
|
|
|
end
|
2021-07-18 18:21:39 +00:00
|
|
|
elseif collate == "access" then
|
2017-02-12 02:55:31 +00:00
|
|
|
sorting = function(a, b)
|
|
|
|
return a.attr.access > b.attr.access
|
2014-11-16 18:10:37 +00:00
|
|
|
end
|
2021-07-18 18:21:39 +00:00
|
|
|
elseif 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
|
2021-07-18 18:21:39 +00:00
|
|
|
elseif collate == "change" then
|
2017-02-12 02:55:31 +00:00
|
|
|
sorting = function(a, b)
|
2023-02-12 23:14:19 +00:00
|
|
|
local a_opened = DocSettings:hasSidecarFile(a.fullpath)
|
|
|
|
local b_opened = DocSettings:hasSidecarFile(b.fullpath)
|
|
|
|
if a_opened == b_opened then
|
|
|
|
return a.attr.change > b.attr.change
|
2017-02-12 02:55:31 +00:00
|
|
|
end
|
2023-02-12 23:14:19 +00:00
|
|
|
return b_opened
|
2017-02-12 02:55:31 +00:00
|
|
|
end
|
2021-07-18 18:21:39 +00:00
|
|
|
elseif collate == "size" then
|
2017-02-12 02:55:31 +00:00
|
|
|
sorting = function(a, b)
|
|
|
|
return a.attr.size < b.attr.size
|
|
|
|
end
|
2021-07-18 18:21:39 +00:00
|
|
|
elseif collate == "type" then
|
2017-02-12 02:55:31 +00:00
|
|
|
sorting = function(a, b)
|
2023-02-12 23:14:19 +00:00
|
|
|
if (a.suffix or b.suffix) and a.suffix ~= b.suffix then
|
2020-10-05 14:34:46 +00:00
|
|
|
return ffiUtil.strcoll(a.suffix, b.suffix)
|
2014-10-30 14:41:49 +00:00
|
|
|
end
|
2023-02-12 23:14:19 +00:00
|
|
|
return ffiUtil.strcoll(a.name, b.name)
|
2014-10-30 14:41:49 +00:00
|
|
|
end
|
2023-03-05 20:12:24 +00:00
|
|
|
else -- collate == "percent_unopened_first" or collate == "percent_unopened_last"
|
2018-01-17 19:10:25 +00:00
|
|
|
sorting = function(a, b)
|
2023-02-12 23:14:19 +00:00
|
|
|
local a_opened = DocSettings:hasSidecarFile(a.fullpath)
|
|
|
|
local b_opened = DocSettings:hasSidecarFile(b.fullpath)
|
|
|
|
if a_opened == b_opened then
|
|
|
|
if a_opened then
|
|
|
|
return a.percent_finished < b.percent_finished
|
2018-01-17 19:10:25 +00:00
|
|
|
end
|
|
|
|
return a.name < b.name
|
|
|
|
end
|
2023-02-12 23:14:19 +00:00
|
|
|
if collate == "percent_unopened_first" then
|
|
|
|
return b_opened
|
|
|
|
end
|
|
|
|
return a_opened
|
2018-01-17 19:10:25 +00:00
|
|
|
end
|
2017-02-12 02:55:31 +00:00
|
|
|
end
|
|
|
|
|
2021-07-18 18:21:39 +00:00
|
|
|
if reverse_collate then
|
2017-02-12 02:55:31 +00:00
|
|
|
local sorting_unreversed = sorting
|
|
|
|
sorting = function(a, b) return sorting_unreversed(b, a) end
|
2014-10-30 14:41:49 +00:00
|
|
|
end
|
|
|
|
|
2021-07-18 18:21:39 +00:00
|
|
|
return sorting
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:genItemTableFromPath(path)
|
|
|
|
local dirs = {}
|
|
|
|
local files = {}
|
|
|
|
|
|
|
|
self.list(path, dirs, files)
|
|
|
|
|
|
|
|
local sorting = self:getSortingFunction(self.collate, self.reverse_collate)
|
|
|
|
|
2018-08-10 20:26:07 +00:00
|
|
|
if self.collate ~= "strcoll_mixed" then
|
|
|
|
table.sort(files, sorting)
|
2023-03-05 20:12:24 +00:00
|
|
|
if self.collate == "size" or
|
|
|
|
self.collate == "type" or
|
|
|
|
self.collate == "percent_unopened_first" or
|
|
|
|
self.collate == "percent_unopened_last" then
|
|
|
|
sorting = self:getSortingFunction("strcoll", self.reverse_collate)
|
|
|
|
end
|
|
|
|
table.sort(dirs, sorting)
|
2018-08-10 20:26:07 +00:00
|
|
|
end
|
2021-07-23 13:47:02 +00:00
|
|
|
if path ~= "/" and not (G_reader_settings:isTrue("lock_home_folder") and
|
|
|
|
path == G_reader_settings:readSetting("home_dir")) then
|
|
|
|
table.insert(dirs, 1, {name = ".."})
|
|
|
|
end
|
2023-03-05 20:12:24 +00:00
|
|
|
if self.show_current_dir_for_hold then
|
|
|
|
table.insert(dirs, 1, {name = "."})
|
|
|
|
end
|
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-02-16 06:34:28 +00:00
|
|
|
local subdir_path = self.path.."/"..dir.name
|
2022-01-04 20:34:22 +00:00
|
|
|
local text, bidi_wrap_func, istr
|
2018-08-06 18:59:01 +00:00
|
|
|
if dir.name == ".." then
|
2023-03-05 20:12:24 +00:00
|
|
|
text = self.up_folder_arrow
|
2018-08-06 18:59:01 +00:00
|
|
|
elseif dir.name == "." then -- possible with show_current_dir_for_hold
|
2021-08-24 20:19:07 +00:00
|
|
|
text = _("Long-press to choose current folder")
|
2020-08-29 16:25:30 +00:00
|
|
|
elseif dir.name == "./." then -- added as content of an unreadable directory
|
2021-02-28 09:18:00 +00:00
|
|
|
text = _("Current folder not readable. Some content may not be shown.")
|
2018-08-06 18:59:01 +00:00
|
|
|
else
|
|
|
|
text = dir.name.."/"
|
2019-12-06 21:55:39 +00:00
|
|
|
bidi_wrap_func = BD.directory
|
2022-01-04 20:34:22 +00:00
|
|
|
-- count number of folders and files inside dir
|
|
|
|
local sub_dirs = {}
|
|
|
|
local dir_files = {}
|
|
|
|
self.list(subdir_path, sub_dirs, dir_files, true)
|
|
|
|
istr = T("%1 \u{F016}", #dir_files)
|
|
|
|
if #sub_dirs > 0 then
|
|
|
|
istr = T("%1 \u{F114} ", #sub_dirs) .. istr
|
|
|
|
end
|
2018-08-06 18:59:01 +00:00
|
|
|
end
|
2014-09-25 14:21:25 +00:00
|
|
|
table.insert(item_table, {
|
2018-08-06 18:59:01 +00:00
|
|
|
text = text,
|
2019-12-06 21:55:39 +00:00
|
|
|
bidi_wrap_func = bidi_wrap_func,
|
2014-09-25 14:21:25 +00:00
|
|
|
mandatory = istr,
|
2017-10-13 16:43:02 +00:00
|
|
|
path = subdir_path,
|
2023-03-05 20:12:24 +00:00
|
|
|
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
|
|
|
|
2023-03-05 20:12:24 +00:00
|
|
|
for i, file in ipairs(files) do
|
2014-10-30 14:41:49 +00:00
|
|
|
local full_path = self.path.."/"..file.name
|
2023-03-05 20:12:24 +00:00
|
|
|
local sstr = util.getFriendlySize(file.attr.size or 0)
|
2016-12-27 06:52:53 +00:00
|
|
|
local file_item = {
|
2014-10-30 14:41:49 +00:00
|
|
|
text = file.name,
|
2019-12-06 21:55:39 +00:00
|
|
|
bidi_wrap_func = BD.filename,
|
2014-09-25 14:21:25 +00:00
|
|
|
mandatory = sstr,
|
2021-12-16 11:12:25 +00:00
|
|
|
path = full_path,
|
|
|
|
is_file = true,
|
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
|
2021-12-16 11:12:25 +00:00
|
|
|
if self.filemanager and self.filemanager.selected_files and self.filemanager.selected_files[full_path] then
|
|
|
|
file_item.dim = true
|
|
|
|
end
|
2016-12-27 06:52:53 +00:00
|
|
|
table.insert(item_table, file_item)
|
2014-09-25 14:21:25 +00:00
|
|
|
end
|
2018-08-10 20:26:07 +00:00
|
|
|
|
|
|
|
if self.collate == "strcoll_mixed" then
|
|
|
|
table.sort(item_table, sorting)
|
|
|
|
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
|
2023-03-05 20:12:24 +00:00
|
|
|
for _, v in ipairs(item_table) do
|
2014-09-25 14:21:25 +00:00
|
|
|
if v.text then
|
2018-10-14 06:49:00 +00:00
|
|
|
v.text = ffiUtil.multiByteToUTF8(v.text) or ""
|
2014-09-25 14:21:25 +00:00
|
|
|
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()
|
2022-05-06 08:44:25 +00:00
|
|
|
self:mergeTitleBarIntoLayout()
|
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
|
2018-03-21 19:10:35 +00:00
|
|
|
|
2018-10-14 06:49:00 +00:00
|
|
|
local _, folder_name = util.splitFilePathName(self.path)
|
2018-03-21 19:10:35 +00:00
|
|
|
Screen:setWindowTitle(folder_name)
|
|
|
|
|
2017-10-21 17:53:56 +00:00
|
|
|
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
|
2018-03-21 19:10:35 +00:00
|
|
|
|
2017-10-21 17:53:56 +00:00
|
|
|
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)
|
2018-10-14 06:49:00 +00:00
|
|
|
path = ffiUtil.realpath(path)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.path = path
|
2018-03-21 19:10:35 +00:00
|
|
|
|
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
|
2020-08-29 16:25:30 +00:00
|
|
|
-- We know focused_path is a child of path. In case path is
|
|
|
|
-- not a readable directory, we can have focused_path shown,
|
|
|
|
-- to allow the user to go back in it
|
|
|
|
if not unreadable_dir_content[path] then
|
|
|
|
unreadable_dir_content[path] = {}
|
|
|
|
end
|
|
|
|
if not unreadable_dir_content[path][focused_path] then
|
|
|
|
unreadable_dir_content[path][focused_path] = {
|
|
|
|
name = focused_path:sub(#path+2),
|
|
|
|
fullpath = focused_path,
|
2020-08-30 03:27:17 +00:00
|
|
|
attr = lfs.attributes(focused_path),
|
2020-08-29 16:25:30 +00:00
|
|
|
}
|
|
|
|
end
|
2017-10-13 16:43:02 +00:00
|
|
|
end
|
2018-03-21 19:10:35 +00:00
|
|
|
|
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
|
|
|
|
|
2022-01-11 12:47:06 +00:00
|
|
|
function FileChooser:goHome()
|
|
|
|
local home_dir = G_reader_settings:readSetting("home_dir")
|
|
|
|
if not home_dir or lfs.attributes(home_dir, "mode") ~= "directory" then
|
|
|
|
-- Try some sane defaults, depending on platform
|
|
|
|
home_dir = Device.home_dir
|
|
|
|
end
|
|
|
|
if home_dir then
|
|
|
|
-- Jump to the first page if we're already home
|
|
|
|
if self.path and home_dir == self.path then
|
|
|
|
self:onGotoPage(1)
|
|
|
|
-- Also pick up new content, if any.
|
|
|
|
self:refreshPath()
|
|
|
|
else
|
|
|
|
self:changeToPath(home_dir)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-12 18:47:49 +00:00
|
|
|
function FileChooser:onFolderUp()
|
2021-07-23 13:47:02 +00:00
|
|
|
if not (G_reader_settings:isTrue("lock_home_folder") and
|
|
|
|
self.path == G_reader_settings:readSetting("home_dir")) then
|
|
|
|
self:changeToPath(string.format("%s/..", self.path), self.path)
|
|
|
|
end
|
2020-07-12 18:47:49 +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
|
2023-03-05 20:12:24 +00:00
|
|
|
if not item.is_file and item.path == path then
|
2017-10-13 16:43:02 +00:00
|
|
|
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
|
|
|
|
|
2019-07-20 15:36:41 +00:00
|
|
|
function FileChooser:toggleUnsupportedFiles()
|
|
|
|
self.show_unsupported = not self.show_unsupported
|
|
|
|
self:refreshPath()
|
|
|
|
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
|
2023-03-05 20:12:24 +00:00
|
|
|
if item.is_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
|
|
|
|
|
2023-03-05 20:12:24 +00:00
|
|
|
-- Used in ReaderStatus:onOpenNextDocumentInFolder().
|
2018-05-13 11:07:23 +00:00
|
|
|
function FileChooser:getNextFile(curr_file)
|
2023-03-05 20:12:24 +00:00
|
|
|
local is_curr_file_found
|
|
|
|
for i, item in ipairs(self.item_table) do
|
|
|
|
if not is_curr_file_found and item.path == curr_file then
|
|
|
|
is_curr_file_found = true
|
|
|
|
end
|
|
|
|
if is_curr_file_found then
|
|
|
|
local next_file = self.item_table[i+1]
|
|
|
|
if next_file and next_file.is_file and DocumentRegistry:hasProvider(next_file.path) then
|
|
|
|
return next_file.path
|
2018-05-13 11:07:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-12-16 11:12:25 +00:00
|
|
|
-- Used in file manager select mode to select all files in a folder,
|
|
|
|
-- that are visible in all file browser pages, without subfolders.
|
|
|
|
function FileChooser:selectAllFilesInFolder()
|
2023-03-05 20:12:24 +00:00
|
|
|
for _, item in ipairs(self.item_table) do
|
2021-12-16 11:12:25 +00:00
|
|
|
if item.is_file then
|
|
|
|
self.filemanager.selected_files[item.path] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileChooser:showSetProviderButtons(file, one_time_providers)
|
2021-05-10 22:49:35 +00:00
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
|
|
|
|
2018-10-14 06:49:00 +00:00
|
|
|
local __, filename_pure = util.splitFilePathName(file)
|
|
|
|
local filename_suffix = util.getFileNameSuffix(file)
|
|
|
|
|
|
|
|
local buttons = {}
|
|
|
|
local radio_buttons = {}
|
2019-10-23 21:15:30 +00:00
|
|
|
local filetype_provider = G_reader_settings:readSetting("provider") or {}
|
2018-10-14 06:49:00 +00:00
|
|
|
local providers = DocumentRegistry:getProviders(file)
|
2019-10-23 21:15:30 +00:00
|
|
|
if providers ~= nil then
|
|
|
|
for ___, provider in ipairs(providers) do
|
|
|
|
-- we have no need for extension, mimetype, weights, etc. here
|
|
|
|
provider = provider.provider
|
|
|
|
table.insert(radio_buttons, {
|
|
|
|
{
|
|
|
|
text = provider.provider_name,
|
|
|
|
checked = DocumentRegistry:getProvider(file) == provider,
|
|
|
|
provider = provider,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local provider = DocumentRegistry:getProvider(file)
|
2018-10-14 06:49:00 +00:00
|
|
|
table.insert(radio_buttons, {
|
|
|
|
{
|
2019-10-23 21:15:30 +00:00
|
|
|
-- @translators %1 is the provider name, such as Cool Reader Engine or MuPDF.
|
|
|
|
text = T(_("%1 ~Unsupported"), provider.provider_name),
|
|
|
|
checked = true,
|
2018-10-14 06:49:00 +00:00
|
|
|
provider = provider,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
2020-08-29 16:25:38 +00:00
|
|
|
if one_time_providers and #one_time_providers > 0 then
|
|
|
|
for ___, provider in ipairs(one_time_providers) do
|
|
|
|
provider.one_time_provider = true
|
|
|
|
table.insert(radio_buttons, {
|
|
|
|
{
|
|
|
|
text = provider.provider_name,
|
|
|
|
provider = provider,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2018-10-14 06:49:00 +00:00
|
|
|
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.set_provider_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Open"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
|
|
|
local provider = self.set_provider_dialog.radio_button_table.checked_button.provider
|
2020-08-29 16:25:38 +00:00
|
|
|
if provider.one_time_provider then
|
|
|
|
UIManager:close(self.set_provider_dialog)
|
|
|
|
provider.callback()
|
|
|
|
return
|
|
|
|
end
|
2018-10-14 06:49:00 +00:00
|
|
|
|
|
|
|
-- always for this file
|
|
|
|
if self.set_provider_dialog._check_file_button.checked then
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = T(_("Always open '%2' with %1?"),
|
2019-12-06 21:55:39 +00:00
|
|
|
provider.provider_name, BD.filename(filename_pure)),
|
2018-10-14 06:49:00 +00:00
|
|
|
ok_text = _("Always"),
|
|
|
|
ok_callback = function()
|
|
|
|
DocumentRegistry:setProvider(file, provider, false)
|
|
|
|
|
2021-05-10 22:49:35 +00:00
|
|
|
ReaderUI:showReader(file, provider)
|
2018-10-14 06:49:00 +00:00
|
|
|
UIManager:close(self.set_provider_dialog)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
-- always for all files of this file type
|
|
|
|
elseif self.set_provider_dialog._check_global_button.checked then
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = T(_("Always open %2 files with %1?"),
|
|
|
|
provider.provider_name, filename_suffix),
|
|
|
|
ok_text = _("Always"),
|
|
|
|
ok_callback = function()
|
|
|
|
DocumentRegistry:setProvider(file, provider, true)
|
|
|
|
|
2021-05-10 22:49:35 +00:00
|
|
|
ReaderUI:showReader(file, provider)
|
2018-10-14 06:49:00 +00:00
|
|
|
UIManager:close(self.set_provider_dialog)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
else
|
|
|
|
-- just once
|
2021-05-10 22:49:35 +00:00
|
|
|
ReaderUI:showReader(file, provider)
|
2018-10-14 06:49:00 +00:00
|
|
|
UIManager:close(self.set_provider_dialog)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2019-10-23 21:15:30 +00:00
|
|
|
if filetype_provider[filename_suffix] ~= nil then
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Reset default"),
|
|
|
|
callback = function()
|
|
|
|
filetype_provider[filename_suffix] = nil
|
|
|
|
G_reader_settings:saveSetting("provider", filetype_provider)
|
|
|
|
UIManager:close(self.set_provider_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2018-10-14 06:49:00 +00:00
|
|
|
self.set_provider_dialog = OpenWithDialog:new{
|
2019-12-06 21:55:39 +00:00
|
|
|
title = T(_("Open %1 with:"), BD.filename(filename_pure)),
|
2018-10-14 06:49:00 +00:00
|
|
|
radio_buttons = radio_buttons,
|
|
|
|
buttons = buttons,
|
|
|
|
}
|
|
|
|
UIManager:show(self.set_provider_dialog)
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FileChooser
|