DocSettings/Purge .sdr: reword, don't purge other books (#8348)

Reword "Purge .sdr" to "Reset settings".
When purging, remove only the known document metadata
files, and not those for a document with the same name but
a different suffix.
pull/8354/head
poire-z 3 years ago committed by GitHub
parent 2c6943e5d6
commit 37eb53f6e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -249,12 +249,12 @@ function FileManager:setupLayout()
end,
},
{
text = _("Purge .sdr"),
enabled = DocSettings:hasSidecarFile(BaseUtil.realpath(file)),
text = _("Reset settings"),
enabled = is_file and DocSettings:hasSidecarFile(BaseUtil.realpath(file)),
callback = function()
UIManager:show(ConfirmBox:new{
text = T(_("Purge .sdr to reset settings for this document?\n\n%1"), BD.filename(self.file_dialog.title)),
ok_text = _("Purge"),
text = T(_("Reset settings for this document?\n\n%1\n\nAny highlights or bookmarks will be permanently lost."), BD.filepath(file)),
ok_text = _("Reset"),
ok_callback = function()
filemanagerutil.purgeSettings(file)
require("readhistory"):fileSettingsPurged(file)
@ -280,8 +280,8 @@ function FileManager:setupLayout()
callback = function()
UIManager:close(self.file_dialog)
UIManager:show(ConfirmBox:new{
text = is_file and T(_("Delete file?\n%1\nIf you delete a file, it is permanently lost."), BD.filepath(file)) or
T(_("Delete folder?\n%1\nIf you delete a folder, its content is permanently lost."), BD.filepath(file)),
text = is_file and T(_("Delete file?\n\n%1\n\nIf you delete a file, it is permanently lost."), BD.filepath(file)) or
T(_("Delete folder?\n\n%1\n\nIf you delete a folder, its content is permanently lost."), BD.filepath(file)),
ok_text = _("Delete"),
ok_callback = function()
deleteFile(file)

@ -10,6 +10,7 @@ local Screen = require("device").screen
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local util = require("ffi/util")
local _ = require("gettext")
local T = util.template
local FileManagerHistory = InputContainer:extend{
hist_menu_title = _("History"),
@ -50,12 +51,12 @@ function FileManagerHistory:onMenuHold(item)
local buttons = {
{
{
text = _("Purge .sdr"),
text = _("Reset settings"),
enabled = item.file ~= currently_opened_file and DocSettings:hasSidecarFile(util.realpath(item.file)),
callback = function()
UIManager:show(ConfirmBox:new{
text = util.template(_("Purge .sdr to reset settings for this document?\n\n%1"), BD.filename(item.text)),
ok_text = _("Purge"),
text = T(_("Reset settings for this document?\n\n%1\n\nAny highlights or bookmarks will be permanently lost."), BD.filepath(item.file)),
ok_text = _("Reset"),
ok_callback = function()
filemanagerutil.purgeSettings(item.file)
require("readhistory"):fileSettingsPurged(item.file)
@ -80,7 +81,7 @@ function FileManagerHistory:onMenuHold(item)
enabled = (item.file ~= currently_opened_file and lfs.attributes(item.file, "mode")) and true or false,
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Are you sure that you want to delete this file?\n") .. BD.filepath(item.file) .. ("\n") .. _("If you delete a file, it is permanently lost."),
text = T(_("Are you sure that you want to delete this document?\n\n%1\n\nIf you delete a file, it is permanently lost."), BD.filepath(item.file)),
ok_text = _("Delete"),
ok_callback = function()
local FileManager = require("apps/filemanager/filemanager")

@ -33,13 +33,7 @@ end
function filemanagerutil.purgeSettings(file)
local file_abs_path = util.realpath(file)
if file_abs_path then
os.remove(DocSettings:getSidecarFile(file_abs_path))
-- Also remove backup, otherwise it will be used if we re-open this document
-- (it also allows for the sidecar folder to be empty and removed)
os.remove(DocSettings:getSidecarFile(file_abs_path)..".old")
-- If the sidecar folder is empty, os.remove() can delete it.
-- Otherwise, the following statement has no effect.
os.remove(DocSettings:getSidecarDir(file_abs_path))
DocSettings:open(file_abs_path):purge()
end
end

@ -9,6 +9,7 @@ local dump = require("dump")
local ffiutil = require("ffi/util")
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local util = require("util")
local DocSettings = {}
@ -140,7 +141,7 @@ function DocSettings:open(docfile)
end
end)
local ok, stored, filepath
for _, k in pairs(candidates) do
for _, k in ipairs(candidates) do
-- Ignore empty files
if lfs.attributes(k[1], "size") > 0 then
ok, stored = pcall(dofile, k[1])
@ -312,7 +313,7 @@ function DocSettings:flush()
self:ensureSidecar(self.sidecar)
local s_out = dump(self.data)
os.setlocale('C', 'numeric')
for _, f in pairs(serials) do
for _, f in ipairs(serials) do
local directory_updated = false
if lfs.attributes(f, "mode") == "file" then
-- As an additional safety measure (to the ffiutil.fsync* calls
@ -338,7 +339,7 @@ function DocSettings:flush()
if self.candidates ~= nil
and G_reader_settings:nilOrFalse("preserve_legacy_docsetting") then
for _, k in pairs(self.candidates) do
for _, k in ipairs(self.candidates) do
if k[1] ~= f and k[1] ~= f .. ".old" then
logger.dbg("Remove legacy file ", k[1])
os.remove(k[1])
@ -366,12 +367,51 @@ function DocSettings:getFilePath()
end
--- Purges (removes) sidecar directory.
function DocSettings:purge()
function DocSettings:purge(full)
-- Remove any of the old ones we may consider as candidates
-- in DocSettings:open()
if self.history_file then
os.remove(self.history_file)
os.remove(self.history_file .. ".old")
end
if self.legacy_sidecar_file then
os.remove(self.legacy_sidecar_file)
end
if lfs.attributes(self.sidecar, "mode") == "directory" then
ffiutil.purgeDir(self.sidecar)
if full then
-- Asked to remove all the content of this .sdr directory,
-- whether it's ours or not
ffiutil.purgeDir(self.sidecar)
else
-- Only remove the files we know we may have created
-- with our usual names.
for f in lfs.dir(self.sidecar) do
local fullpath = self.sidecar.."/"..f
local to_remove = false
if lfs.attributes(fullpath, "mode") == "file" then
-- Currently, we only create a single file in there,
-- named metadata.suffix.lua (ie. metadata.epub.lua),
-- with possibly backups named metadata.epub.lua.old and
-- metadata.epub.lua.old_dom20180528, so all sharing the
-- same base: self.sidecar_file
if util.stringStartsWith(fullpath, self.sidecar_file) then
to_remove = true
end
end
if to_remove then
os.remove(fullpath)
logger.dbg("purge: removed ", fullpath)
end
end
-- If the sidecar folder ends up empty, os.remove() can delete it.
-- Otherwise, the following statement has no effect.
os.remove(self.sidecar)
end
end
-- We should have meet the candidate we used and remove it above. But in
-- case we didn't, remove it
if self.filepath and lfs.attributes(self.filepath, "mode") == "file" then
os.remove(self.filepath)
end
self.data = {}
end

@ -243,7 +243,7 @@ function CoverMenu:updateItems(select_number)
UIManager:close(self.file_dialog)
end
-- Fudge the "Purge .sdr" button ([1][3]) callback to also trash the cover_info_cache
-- Fudge the "Reset settings" button ([1][3]) callback to also trash the cover_info_cache
local orig_purge_callback = orig_buttons[1][3].callback
orig_buttons[1][3].callback = function()
-- Wipe the cache

Loading…
Cancel
Save