History: add 'autoremove_deleted_items_from_history' setting

pull/2586/head
Frans de Jonge 7 years ago committed by Qingping Hou
parent 827e77afbe
commit da3ff5e336

@ -163,19 +163,18 @@ function FileManager:init()
text = _("Purge .sdr"), text = _("Purge .sdr"),
enabled = DocSettings:hasSidecarFile(util.realpath(file)), enabled = DocSettings:hasSidecarFile(util.realpath(file)),
callback = function() callback = function()
local full_path = util.realpath(file) local file_abs_path = util.realpath(file)
os.remove(DocSettings:getSidecarFile(full_path)) if file_abs_path then
-- If the sidecar folder is empty, os.remove() can local autoremove_deleted_items_from_history = G_reader_settings:readSetting("autoremove_deleted_items_from_history") or false
-- delete it. Otherwise, the following statement has no os.remove(DocSettings:getSidecarFile(file_abs_path))
-- effect. -- If the sidecar folder is empty, os.remove() can
os.remove(DocSettings:getSidecarDir(full_path)) -- delete it. Otherwise, the following statement has no
self:refreshPath() -- effect.
-- also remove from history if present os.remove(DocSettings:getSidecarDir(file_abs_path))
local readhistory = require("readhistory") self:refreshPath()
for _, hist_item in ipairs(readhistory.hist) do -- also delete from history if autoremove_deleted_items_from_history is enabled
if hist_item.file == full_path then if autoremove_deleted_items_from_history then
readhistory:removeItem(hist_item) require("readhistory"):removeItemByPath(file_abs_path)
break
end end
end end
UIManager:close(self.file_dialog) UIManager:close(self.file_dialog)
@ -198,7 +197,15 @@ function FileManager:init()
UIManager:show(ConfirmBox:new{ UIManager:show(ConfirmBox:new{
text = _("Are you sure that you want to delete this file?\n") .. file .. ("\n") .. _("If you delete a file, it is permanently lost."), text = _("Are you sure that you want to delete this file?\n") .. file .. ("\n") .. _("If you delete a file, it is permanently lost."),
ok_callback = function() ok_callback = function()
local autoremove_deleted_items_from_history = G_reader_settings:readSetting("autoremove_deleted_items_from_history") or false
local file_abs_path = util.realpath(file)
deleteFile(file) deleteFile(file)
-- also delete from history if autoremove_deleted_items_from_history is enabled
if autoremove_deleted_items_from_history then
if file_abs_path then
require("readhistory"):removeItemByPath(file_abs_path)
end
end
self:refreshPath() self:refreshPath()
end, end,
}) })

@ -47,6 +47,10 @@ function ReadHistory:_indexing(start)
end end
function ReadHistory:_sort() function ReadHistory:_sort()
local autoremove_deleted_items_from_history = G_reader_settings:readSetting("autoremove_deleted_items_from_history") or false
if autoremove_deleted_items_from_history then
self:clearMissing()
end
for i = #self.hist, 1, -1 do for i = #self.hist, 1, -1 do
if self.hist[i].file == nil then if self.hist[i].file == nil then
table.remove(self.hist, i) table.remove(self.hist, i)
@ -130,6 +134,15 @@ function ReadHistory:clearMissing()
end end
end end
function ReadHistory:removeItemByPath(path)
for i = #self.hist, 1, -1 do
if self.hist[i].file == path then
self:removeItem(self.hist[i])
break
end
end
end
function ReadHistory:removeItem(item) function ReadHistory:removeItem(item)
table.remove(self.hist, item.index) table.remove(self.hist, item.index)
os.remove(DocSettings:getHistoryPath(item.file)) os.remove(DocSettings:getHistoryPath(item.file))

Loading…
Cancel
Save