[fix] Ensure "Open last/previous" point to existing files (#4367)

pull/4369/head
poire-z 6 years ago committed by Frans de Jonge
parent 7841351a81
commit 2b9694d7d2

@ -228,6 +228,7 @@ function FileManager:init()
ok_callback = function()
deleteFile(file)
filemanagerutil.removeFileFromHistoryIfWanted(file)
filemanagerutil.ensureLastFileExists()
self:refreshPath()
UIManager:close(self.file_dialog)
end,

@ -87,6 +87,7 @@ function FileManagerHistory:onMenuHold(item)
FileManager:deleteFile(item.file)
filemanagerutil.removeFileFromHistoryIfWanted(item.file)
require("readhistory"):setDeleted(item)
filemanagerutil.ensureLastFileExists()
self._manager:updateItemTable()
UIManager:close(self.histfile_dialog)
end,

@ -50,16 +50,28 @@ function filemanagerutil.purgeSettings(file)
end
end
-- Remove from history and update lastfile to top item in history
-- Remove from history (and update lastfile to an existing file)
-- if autoremove_deleted_items_from_history is enabled
function filemanagerutil.removeFileFromHistoryIfWanted(file)
if G_reader_settings:readSetting("autoremove_deleted_items_from_history") then
local readhistory = require("readhistory")
readhistory:removeItemByPath(file)
if G_reader_settings:readSetting("lastfile") == file then
G_reader_settings:saveSetting("lastfile", #readhistory.hist > 0 and readhistory.hist[1].file or nil)
filemanagerutil.ensureLastFileExists()
end
end
-- Update lastfile setting to the most recent one in history
-- that still exists
function filemanagerutil.ensureLastFileExists()
local last_existing_file = nil
local readhistory = require("readhistory")
for i=1, #readhistory.hist do
if lfs.attributes(readhistory.hist[i].file, "mode") == "file" then
last_existing_file = readhistory.hist[i].file
break
end
end
G_reader_settings:saveSetting("lastfile", last_existing_file)
end
return filemanagerutil

@ -186,29 +186,35 @@ function ReaderMenu:setUpdateItemTable()
end,
}
local previous_file
local readhistory = require("readhistory")
for i=2, #readhistory.hist do -- skip first one which is current book
if lfs.attributes(readhistory.hist[i].file, "mode") == "file" then
previous_file = readhistory.hist[i].file
break
local getPreviousFile = function()
local previous_file = nil
local readhistory = require("readhistory")
for i=2, #readhistory.hist do -- skip first one which is current book
-- skip deleted items kept in history
if lfs.attributes(readhistory.hist[i].file, "mode") == "file" then
previous_file = readhistory.hist[i].file
break
end
end
return previous_file
end
self.menu_items.open_previous_document = {
text_func = function()
local previous_file = getPreviousFile()
if not G_reader_settings:isTrue("open_last_menu_show_filename") or not previous_file then
return _("Open previous document")
end
local path, file_name = util.splitFilePathName(previous_file); -- luacheck: no unused
local path, file_name = util.splitFilePathName(previous_file) -- luacheck: no unused
return T(_("Previous: %1"), file_name)
end,
enabled_func = function()
return previous_file ~= nil
return getPreviousFile() ~= nil
end,
callback = function()
self.ui:switchDocument(previous_file)
self.ui:switchDocument(getPreviousFile())
end,
hold_callback = function()
local previous_file = getPreviousFile()
UIManager:show(ConfirmBox:new{
text = T(_("Would you like to open the previous document: %1?"), previous_file),
ok_text = _("OK"),

Loading…
Cancel
Save