mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
PathChooser: clearer usage and behaviour (#4133)
Explicit new options: select_directory, select_file, show_files. With select_directory, show an entry to select current directory (so, removed undocumented hack "hold on .. to select current directory").
This commit is contained in:
@@ -191,7 +191,7 @@ function ReaderWikipedia:addToMainMenu(menu_items)
|
||||
},
|
||||
{
|
||||
{
|
||||
text = _("Change (select directory by long-pressing)"),
|
||||
text = _("Select another directory"),
|
||||
callback = function()
|
||||
UIManager:close(dialog)
|
||||
-- Use currently read book's directory as starting point,
|
||||
@@ -218,15 +218,10 @@ function ReaderWikipedia:addToMainMenu(menu_items)
|
||||
end
|
||||
local PathChooser = require("ui/widget/pathchooser")
|
||||
local path_chooser = PathChooser:new{
|
||||
title = _("Wikipedia 'Save as EPUB' directory"),
|
||||
select_directory = true,
|
||||
select_file = false,
|
||||
path = dir,
|
||||
show_hidden = G_reader_settings:readSetting("show_hidden"),
|
||||
onConfirm = function(path)
|
||||
-- hack to remove additional parent
|
||||
if path:sub(-3, -1) == "/.." then
|
||||
path = path:sub(1, -4)
|
||||
end
|
||||
path = require("ffi/util").realpath(path)
|
||||
G_reader_settings:saveSetting("wikipedia_save_dir", path)
|
||||
UIManager:show(InfoMessage:new{
|
||||
text = T(_("Wikipedia 'Save as EPUB' directory set to:\n%1"), path),
|
||||
|
||||
@@ -24,7 +24,7 @@ local util = require("ffi/util")
|
||||
local _ = require("gettext")
|
||||
|
||||
local DownloadMgr = {
|
||||
title = _("Long press to choose download directory"),
|
||||
-- title = _("Long press to choose download directory"),
|
||||
onConfirm = function() end,
|
||||
}
|
||||
|
||||
@@ -40,17 +40,16 @@ end
|
||||
function DownloadMgr:chooseDir()
|
||||
local lastdir = G_reader_settings:readSetting("lastdir")
|
||||
local download_dir = G_reader_settings:readSetting("download_dir")
|
||||
local path = download_dir and util.realpath(download_dir .. "/..") or lastdir
|
||||
local path_chooser = PathChooser:new{
|
||||
title = self.title,
|
||||
title = self.title or true, -- use default title if none provided
|
||||
select_directory = true,
|
||||
select_file = false,
|
||||
show_files = false,
|
||||
height = Screen:getHeight(),
|
||||
path = download_dir and (download_dir .. "/..") or lastdir,
|
||||
show_hidden = G_reader_settings:readSetting("show_hidden"),
|
||||
onConfirm = function(path)
|
||||
-- hack to remove additional parent
|
||||
if path:sub(-3, -1) == "/.." then
|
||||
path = path:sub(1, -4)
|
||||
end
|
||||
self.onConfirm(util.realpath(path))
|
||||
path = path,
|
||||
onConfirm = function(dir_path)
|
||||
self.onConfirm(dir_path)
|
||||
end
|
||||
}
|
||||
UIManager:show(path_chooser)
|
||||
|
||||
@@ -198,6 +198,7 @@ function FileChooser:genItemTableFromPath(path)
|
||||
|
||||
table.sort(dirs, sorting)
|
||||
if path ~= "/" then table.insert(dirs, 1, {name = ".."}) end
|
||||
if self.show_current_dir_for_hold then table.insert(dirs, 1, {name = "."}) end
|
||||
table.sort(files, sorting)
|
||||
|
||||
local item_table = {}
|
||||
@@ -214,8 +215,16 @@ function FileChooser:genItemTableFromPath(path)
|
||||
else
|
||||
istr = util.template(_("%1 items"), num_items)
|
||||
end
|
||||
local text
|
||||
if dir.name == ".." then
|
||||
text = "⬆ ../"
|
||||
elseif dir.name == "." then -- possible with show_current_dir_for_hold
|
||||
text = _("Long-press to select current directory")
|
||||
else
|
||||
text = dir.name.."/"
|
||||
end
|
||||
table.insert(item_table, {
|
||||
text = dir.name == ".." and "⬆ ../" or dir.name.."/",
|
||||
text = text,
|
||||
mandatory = istr,
|
||||
path = subdir_path,
|
||||
is_go_up = dir.name == ".."
|
||||
|
||||
@@ -1,46 +1,135 @@
|
||||
local ButtonDialog = require("ui/widget/buttondialog")
|
||||
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
|
||||
local FileChooser = require("ui/widget/filechooser")
|
||||
local Font = require("ui/font")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local util = require("ffi/util")
|
||||
local ffiutil = require("ffi/util")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local util = require("util")
|
||||
local _ = require("gettext")
|
||||
local T = ffiutil.template
|
||||
|
||||
local PathChooser = FileChooser:extend{
|
||||
title = _("Choose Path"),
|
||||
title = true, -- or a string
|
||||
-- if let to true, a generic title will be set in init()
|
||||
no_title = false,
|
||||
show_path = true,
|
||||
is_popout = false,
|
||||
covers_fullscreen = true, -- set it to false if you set is_popout = true
|
||||
is_borderless = true,
|
||||
show_filesize = false,
|
||||
file_filter = function() return false end, -- filter out regular files
|
||||
-- smaller font to allow displaying our long titles
|
||||
tface = Font:getFace("smalltfont"),
|
||||
|
||||
select_directory = true, -- allow selecting directories
|
||||
select_file = true, -- allow selecting files
|
||||
show_files = true, -- show files, even if select_files=false
|
||||
-- (directories are always shown, to allow navigation)
|
||||
show_hidden = G_reader_settings:readSetting("show_hidden"),
|
||||
detailed_file_info = false, -- show size and last mod time in Select message
|
||||
}
|
||||
|
||||
function PathChooser:init()
|
||||
if self.title == true then -- default title depending on options
|
||||
if self.select_directory and not self.select_file then
|
||||
self.title = _("Long-press to select directory")
|
||||
elseif not self.select_directory and self.select_file then
|
||||
self.title = _("Long-press to select file")
|
||||
else
|
||||
self.title = _("Long-press to select")
|
||||
end
|
||||
end
|
||||
if not self.show_files then
|
||||
self.file_filter = function() return false end -- filter out regular files
|
||||
end
|
||||
if self.select_directory then
|
||||
-- Let FileChooser display "Long press to select current directory"
|
||||
self.show_current_dir_for_hold = true
|
||||
end
|
||||
FileChooser.init(self)
|
||||
end
|
||||
|
||||
function PathChooser:onMenuSelect(item)
|
||||
self.path = util.realpath(item.path)
|
||||
local sub_table = self:genItemTableFromPath(self.path)
|
||||
-- if sub table only have one entry(itself) we do nothing
|
||||
if #sub_table > 1 then
|
||||
self:changeToPath(item.path)
|
||||
local path = item.path
|
||||
if path:sub(-2, -1) == "/." then -- with show_current_dir_for_hold
|
||||
-- Don't navigate to same directory
|
||||
return true
|
||||
end
|
||||
path = ffiutil.realpath(path)
|
||||
if not path then
|
||||
-- If starting in a no-more existing directory, allow
|
||||
-- not getting stuck in it
|
||||
self:changeToPath("/")
|
||||
return true
|
||||
end
|
||||
local attr = lfs.attributes(path)
|
||||
if not attr then
|
||||
-- Same as above
|
||||
self:changeToPath("/")
|
||||
return true
|
||||
end
|
||||
if attr.mode ~= "directory" then
|
||||
-- Do nothing if Tap on other than directories
|
||||
return true
|
||||
end
|
||||
-- Let this method check permissions and if we can list
|
||||
-- this directory: we should get at least one item: ".."
|
||||
local sub_table = self:genItemTableFromPath(path)
|
||||
if #sub_table > 0 then
|
||||
self:changeToPath(path)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
function PathChooser:onMenuHold(item)
|
||||
local path = item.path
|
||||
if path:sub(-2, -1) == "/." then -- with show_current_dir_for_hold
|
||||
path = path:sub(1, -3)
|
||||
end
|
||||
path = ffiutil.realpath(path)
|
||||
if not path then
|
||||
return true
|
||||
end
|
||||
local attr = lfs.attributes(path)
|
||||
if not attr then
|
||||
return true
|
||||
end
|
||||
if attr.mode == "file" and not self.select_file then
|
||||
return true
|
||||
end
|
||||
if attr.mode == "directory" and not self.select_directory then
|
||||
return true
|
||||
end
|
||||
local title
|
||||
if attr.mode == "file" then
|
||||
if self.detailed_file_info then
|
||||
local filesize = util.getFormattedSize(attr.size)
|
||||
local lastmod = os.date("%Y-%m-%d %H:%M", attr.modification)
|
||||
title = T(_("Select this file?\n\n%1\n\nFile size: %2 bytes\nLast modified: %3"),
|
||||
path, filesize, lastmod)
|
||||
else
|
||||
title = T(_("Select this file?\n\n%1"), path)
|
||||
end
|
||||
elseif attr.mode == "directory" then
|
||||
title = T(_("Select this directory?\n\n%1"), path)
|
||||
else -- just in case we get something else
|
||||
title = T(_("Select this path?\n\n%1"), path)
|
||||
end
|
||||
local onConfirm = self.onConfirm
|
||||
self.button_dialog = ButtonDialog:new{
|
||||
self.button_dialog = ButtonDialogTitle:new{
|
||||
title = title,
|
||||
buttons = {
|
||||
{
|
||||
{
|
||||
text = _("Confirm"),
|
||||
text = _("Cancel"),
|
||||
callback = function()
|
||||
if onConfirm then onConfirm(item.path) end
|
||||
UIManager:close(self.button_dialog)
|
||||
UIManager:close(self)
|
||||
end,
|
||||
},
|
||||
{
|
||||
text = _("Cancel"),
|
||||
text = _("Select"),
|
||||
callback = function()
|
||||
if onConfirm then
|
||||
onConfirm(path)
|
||||
end
|
||||
UIManager:close(self.button_dialog)
|
||||
UIManager:close(self)
|
||||
end,
|
||||
|
||||
Reference in New Issue
Block a user