mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
[fix] Ensure "Open last/previous" point to existing files (#4367)
This commit is contained in:
@@ -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)
|
||||
end
|
||||
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"),
|
||||
|
||||
Reference in New Issue
Block a user