File searcher: fix show folders, include subfolders checkbutton (#8807)

pull/8892/head
hius07 2 years ago committed by GitHub
parent 3e78847c46
commit d7549dbd87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,15 +15,24 @@ local BaseUtil = require("ffi/util")
local util = require("util") local util = require("util")
local _ = require("gettext") local _ = require("gettext")
local Screen = require("device").screen local Screen = require("device").screen
local T = require("ffi/util").template
local FileSearcher = InputContainer:new{ local FileSearcher = InputContainer:new{
dirs = {}, dirs = {},
files = {}, files = {},
results = {}, results = {},
case_sensitive = false,
include_subfolders = true,
}
local sys_folders = { -- do not search in sys_folders
["/dev"] = true,
["/proc"] = true,
["/sys"] = true,
} }
function FileSearcher:readDir() function FileSearcher:readDir()
local FileManager = require("apps/filemanager/filemanager")
local ReaderUI = require("apps/reader/readerui") local ReaderUI = require("apps/reader/readerui")
local show_unsupported = G_reader_settings:isTrue("show_unsupported") local show_unsupported = G_reader_settings:isTrue("show_unsupported")
self.dirs = {self.path} self.dirs = {self.path}
@ -33,38 +42,47 @@ function FileSearcher:readDir()
-- handle each dir -- handle each dir
for __, d in pairs(self.dirs) do for __, d in pairs(self.dirs) do
-- handle files in d -- handle files in d
for f in lfs.dir(d) do local ok, iter, dir_obj = pcall(lfs.dir, d)
local fullpath = d.."/"..f if ok then
local attributes = lfs.attributes(fullpath) or {} for f in iter, dir_obj do
-- Don't traverse hidden folders if we're not showing them local fullpath = "/" .. f
if attributes.mode == "directory" and f ~= "." and f ~= ".." if d ~= "/" then
and (G_reader_settings:isTrue("show_hidden") or not util.stringStartsWith(f, ".")) fullpath = d .. fullpath
and FileChooser:show_dir(f) end
then local attributes = lfs.attributes(fullpath) or {}
table.insert(new_dirs, fullpath) -- Don't traverse hidden folders if we're not showing them
table.insert(self.files, { if attributes.mode == "directory" and f ~= "." and f ~= ".."
dir = d, and (G_reader_settings:isTrue("show_hidden") or not util.stringStartsWith(f, "."))
name = f, and FileChooser:show_dir(f)
text = f.."/", then
attr = attributes, if self.include_subfolders and not sys_folders[fullpath] then
callback = function() table.insert(new_dirs, fullpath)
FileManager:showFiles(fullpath) end
end, table.insert(self.files, {
}) dir = d,
-- Always ignore macOS resource forks, too. name = f,
elseif attributes.mode == "file" and not util.stringStartsWith(f, "._") text = f.."/",
and (show_unsupported or DocumentRegistry:hasProvider(fullpath)) attr = attributes,
and FileChooser:show_file(f) callback = function()
then self:showFolder(fullpath .. "/")
table.insert(self.files, { end,
dir = d, })
name = f, -- Always ignore macOS resource forks, too.
text = f, elseif attributes.mode == "file" and not util.stringStartsWith(f, "._")
attr = attributes, and (show_unsupported or DocumentRegistry:hasProvider(fullpath))
callback = function() and FileChooser:show_file(f)
ReaderUI:showReader(fullpath) then
end, table.insert(self.files, {
}) dir = d,
name = f,
text = f,
mandatory = util.getFriendlySize(attributes.size or 0),
attr = attributes,
callback = function()
ReaderUI:showReader(fullpath)
end,
})
end
end end
end end
end end
@ -163,6 +181,15 @@ function FileSearcher:onShowFileSearch(search_string)
end, end,
} }
self.search_dialog:addWidget(self.check_button_case) self.search_dialog:addWidget(self.check_button_case)
self.check_button_subfolders = CheckButton:new{
text = _("Include subfolders"),
checked = self.include_subfolders,
parent = self.search_dialog,
callback = function()
self.include_subfolders = self.check_button_subfolders.checked
end,
}
self.search_dialog:addWidget(self.check_button_subfolders)
UIManager:show(self.search_dialog) UIManager:show(self.search_dialog)
self.search_dialog:onShowKeyboard() self.search_dialog:onShowKeyboard()
@ -189,15 +216,14 @@ function FileSearcher:showSearchResults()
local sorting = FileChooser:getSortingFunction(collate, reverse_collate) local sorting = FileChooser:getSortingFunction(collate, reverse_collate)
table.sort(self.results, sorting) table.sort(self.results, sorting)
self.search_menu:switchItemTable(_("Search results"), self.results) self.search_menu:switchItemTable(T(_("Search results (%1)"), #self.results), self.results)
UIManager:show(menu_container) UIManager:show(menu_container)
end end
function FileSearcher:onMenuHold(item) function FileSearcher:onMenuHold(item)
local FileManager = require("apps/filemanager/filemanager")
local ReaderUI = require("apps/reader/readerui") local ReaderUI = require("apps/reader/readerui")
local fullpath = item.dir .. "/" .. item.name
local is_file = item.attr.mode == "file" local is_file = item.attr.mode == "file"
local fullpath = item.dir .. "/" .. item.name .. (is_file and "" or "/")
local buttons = { local buttons = {
{ {
{ {
@ -211,13 +237,7 @@ function FileSearcher:onMenuHold(item)
callback = function() callback = function()
UIManager:close(self.results_dialog) UIManager:close(self.results_dialog)
self.close_callback() self.close_callback()
local focused_path = is_file and item.dir or fullpath self._manager:showFolder(fullpath)
local focused_file = is_file and fullpath or nil
if FileManager.instance then
FileManager.instance:reinit(focused_path, focused_file)
else
FileManager:showFiles(focused_path, focused_file)
end
end, end,
}, },
}, },
@ -234,11 +254,21 @@ function FileSearcher:onMenuHold(item)
end end
self.results_dialog = ButtonDialogTitle:new{ self.results_dialog = ButtonDialogTitle:new{
title = is_file and fullpath or fullpath .. "/", title = fullpath,
buttons = buttons, buttons = buttons,
} }
UIManager:show(self.results_dialog) UIManager:show(self.results_dialog)
return true return true
end end
function FileSearcher:showFolder(path)
if self.ui.file_chooser then
local pathname = util.splitFilePathName(path)
self.ui.file_chooser:changeToPath(pathname, path)
else -- called from Reader
self.ui:onClose()
self.ui:showFileManager(path)
end
end
return FileSearcher return FileSearcher

@ -35,14 +35,8 @@ function FileManagerShortcuts:updateItemTable(select_callback)
if self.ui.file_chooser then if self.ui.file_chooser then
self.ui.file_chooser:changeToPath(folder) self.ui.file_chooser:changeToPath(folder)
else -- called from Reader else -- called from Reader
local FileManager = require("apps/filemanager/filemanager")
self.ui:onClose() self.ui:onClose()
if FileManager.instance then self.ui:showFileManager(folder .. "/")
FileManager.instance:reinit(folder)
else
FileManager:showFiles(folder)
end
end end
end end
end end

@ -509,8 +509,9 @@ function ReaderUI:showFileManager(file)
local last_dir, last_file local last_dir, last_file
if file then if file then
last_dir, last_file = util.splitFilePathName(file) last_dir = util.splitFilePathName(file)
last_dir = last_dir:match("(.*)/") last_dir = last_dir:match("(.*)/")
last_file = file
else else
last_dir, last_file = self:getLastDirFile(true) last_dir, last_file = self:getLastDirFile(true)
end end

Loading…
Cancel
Save