Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure "Open last/previous" point to existing files #4367

Merged
merged 1 commit into from
Nov 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/apps/filemanager/filemanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions frontend/apps/filemanager/filemanagerhistory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 15 additions & 3 deletions frontend/apps/filemanager/filemanagerutil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 15 additions & 9 deletions frontend/apps/reader/modules/readermenu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down