mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Delegate "lastfile" management to ReadHistory (#6128)
Simplify (and avoid edge cases) in other code by having ReadHistory manage the "lastfile" setting on add, remove, rename... Fixed a few other cases of things not updated.
This commit is contained in:
@@ -234,7 +234,7 @@ function FileManager:init()
|
||||
ok_text = _("Purge"),
|
||||
ok_callback = function()
|
||||
filemanagerutil.purgeSettings(file)
|
||||
filemanagerutil.removeFileFromHistoryIfWanted(file)
|
||||
require("readhistory"):fileSettingsPurged(file)
|
||||
self:refreshPath()
|
||||
UIManager:close(self.file_dialog)
|
||||
end,
|
||||
@@ -258,8 +258,7 @@ function FileManager:init()
|
||||
ok_text = _("Delete"),
|
||||
ok_callback = function()
|
||||
deleteFile(file)
|
||||
filemanagerutil.removeFileFromHistoryIfWanted(file)
|
||||
filemanagerutil.ensureLastFileExists()
|
||||
require("readhistory"):fileDeleted(file)
|
||||
self:refreshPath()
|
||||
UIManager:close(self.file_dialog)
|
||||
end,
|
||||
@@ -787,12 +786,8 @@ function FileManager:pasteHere(file)
|
||||
if self:moveFile(orig, dest) then
|
||||
-- Update history and collections.
|
||||
local dest_file = string.format("%s/%s", dest, BaseUtil.basename(orig))
|
||||
require("readhistory"):updateItemByPath(orig, dest_file)
|
||||
require("readhistory"):updateItemByPath(orig, dest_file) -- (will update "lastfile" if needed)
|
||||
ReadCollection:updateItemByPath(orig, dest_file)
|
||||
-- Update last open file.
|
||||
if G_reader_settings:readSetting("lastfile") == orig then
|
||||
G_reader_settings:saveSetting("lastfile", dest_file)
|
||||
end
|
||||
UIManager:show(InfoMessage:new {
|
||||
text = T(_("Moved to: %1"), BD.dirpath(dest)),
|
||||
timeout = 2,
|
||||
@@ -897,6 +892,7 @@ function FileManager:renameFile(file)
|
||||
if BaseUtil.basename(file) ~= self.rename_dialog:getInputText() then
|
||||
local dest = BaseUtil.joinPath(BaseUtil.dirname(file), self.rename_dialog:getInputText())
|
||||
if self:moveFile(file, dest) then
|
||||
require("readhistory"):updateItemByPath(file, dest) -- (will update "lastfile" if needed)
|
||||
ReadCollection:updateItemByPath(file, dest)
|
||||
if lfs.attributes(dest, "mode") == "file" then
|
||||
local doc = require("docsettings")
|
||||
|
||||
@@ -58,7 +58,7 @@ function FileManagerHistory:onMenuHold(item)
|
||||
ok_text = _("Purge"),
|
||||
ok_callback = function()
|
||||
filemanagerutil.purgeSettings(item.file)
|
||||
filemanagerutil.removeFileFromHistoryIfWanted(item.file)
|
||||
require("readhistory"):fileSettingsPurged(item.file)
|
||||
self._manager:updateItemTable()
|
||||
UIManager:close(self.histfile_dialog)
|
||||
end,
|
||||
@@ -86,9 +86,7 @@ function FileManagerHistory:onMenuHold(item)
|
||||
ok_callback = function()
|
||||
local FileManager = require("apps/filemanager/filemanager")
|
||||
FileManager:deleteFile(item.file)
|
||||
filemanagerutil.removeFileFromHistoryIfWanted(item.file)
|
||||
require("readhistory"):setDeleted(item)
|
||||
filemanagerutil.ensureLastFileExists()
|
||||
require("readhistory"):fileDeleted(item.file) -- (will update "lastfile" if needed)
|
||||
self._manager:updateItemTable()
|
||||
UIManager:close(self.histfile_dialog)
|
||||
end,
|
||||
|
||||
@@ -55,28 +55,4 @@ function filemanagerutil.purgeSettings(file)
|
||||
end
|
||||
end
|
||||
|
||||
-- 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)
|
||||
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
|
||||
|
||||
@@ -72,16 +72,7 @@ function ReaderMenu:init()
|
||||
end
|
||||
|
||||
function ReaderMenu:getPreviousFile()
|
||||
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
|
||||
return require("readhistory"):getPreviousFile(self.ui.document.file)
|
||||
end
|
||||
|
||||
function ReaderMenu:onReaderReady()
|
||||
|
||||
@@ -198,11 +198,9 @@ function ReaderStatus:deleteFile(file, text_end_book)
|
||||
ok_text = _("Delete"),
|
||||
ok_callback = function()
|
||||
local FileManager = require("apps/filemanager/filemanager")
|
||||
local filemanagerutil = require("apps/filemanager/filemanagerutil")
|
||||
self.ui:onClose()
|
||||
FileManager:deleteFile(file)
|
||||
filemanagerutil.removeFileFromHistoryIfWanted(file)
|
||||
filemanagerutil.ensureLastFileExists()
|
||||
require("readhistory"):fileDeleted(file) -- (will update "lastfile")
|
||||
if FileManager.instance then
|
||||
FileManager.instance.file_chooser:refreshPath()
|
||||
else
|
||||
|
||||
@@ -550,8 +550,7 @@ function ReaderUI:doShowReader(file, provider)
|
||||
end
|
||||
end
|
||||
end
|
||||
require("readhistory"):addItem(file)
|
||||
G_reader_settings:saveSetting("lastfile", file)
|
||||
require("readhistory"):addItem(file) -- (will update "lastfile")
|
||||
local reader = ReaderUI:new{
|
||||
dimen = Screen:getSize(),
|
||||
covers_fullscreen = true, -- hint for UIManager:_repaint()
|
||||
@@ -704,11 +703,7 @@ function ReaderUI:dealWithLoadDocumentFailure()
|
||||
-- We must still remove it from lastfile and history (as it has
|
||||
-- already been added there) so that koreader don't crash again
|
||||
-- at next launch...
|
||||
local readhistory = require("readhistory")
|
||||
readhistory:removeItemByPath(self.document.file)
|
||||
if G_reader_settings:readSetting("lastfile") == self.document.file then
|
||||
G_reader_settings:saveSetting("lastfile", #readhistory.hist > 0 and readhistory.hist[1].file or nil)
|
||||
end
|
||||
require("readhistory"):removeItemByPath(self.document.file) -- (will update "lastfile")
|
||||
-- As we are in a coroutine, we can pause and show an InfoMessage before exiting
|
||||
local _coroutine = coroutine.running()
|
||||
if coroutine then
|
||||
|
||||
@@ -141,6 +141,60 @@ function ReadHistory:_init()
|
||||
self:reload()
|
||||
end
|
||||
|
||||
function ReadHistory:ensureLastFile()
|
||||
local last_existing_file = nil
|
||||
for i=1, #self.hist do
|
||||
if lfs.attributes(self.hist[i].file, "mode") == "file" then
|
||||
last_existing_file = self.hist[i].file
|
||||
break
|
||||
end
|
||||
end
|
||||
G_reader_settings:saveSetting("lastfile", last_existing_file)
|
||||
end
|
||||
|
||||
function ReadHistory:getLastFile()
|
||||
self:ensureLastFile()
|
||||
return G_reader_settings:readSetting("lastfile")
|
||||
end
|
||||
|
||||
function ReadHistory:getPreviousFile(current_file)
|
||||
-- Get last or previous file in history that is not current_file
|
||||
-- (self.ui.document.file, probided as current_file, might have
|
||||
-- been removed from history)
|
||||
if not current_file then
|
||||
current_file = G_reader_settings:readSetting("lastfile")
|
||||
end
|
||||
for i=1, #self.hist do
|
||||
-- skip current document and deleted items kept in history
|
||||
local file = self.hist[i].file
|
||||
if file ~= current_file and lfs.attributes(file, "mode") == "file" then
|
||||
return file
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ReadHistory:fileDeleted(path)
|
||||
if G_reader_settings:readSetting("autoremove_deleted_items_from_history") then
|
||||
self:removeItemByPath(path)
|
||||
else
|
||||
-- Make it dimed
|
||||
for i=1, #self.hist do
|
||||
if self.hist[i].file == path then
|
||||
self.hist[i].dim = true
|
||||
break
|
||||
end
|
||||
end
|
||||
self:ensureLastFile()
|
||||
end
|
||||
end
|
||||
|
||||
function ReadHistory:fileSettingsPurged(path)
|
||||
if G_reader_settings:readSetting("autoremove_deleted_items_from_history") then
|
||||
-- Also remove it from history on purge when that setting is enabled
|
||||
self:removeItemByPath(path)
|
||||
end
|
||||
end
|
||||
|
||||
function ReadHistory:clearMissing()
|
||||
assert(self ~= nil)
|
||||
for i = #self.hist, 1, -1 do
|
||||
@@ -148,6 +202,7 @@ function ReadHistory:clearMissing()
|
||||
table.remove(self.hist, i)
|
||||
end
|
||||
end
|
||||
self:ensureLastFile()
|
||||
end
|
||||
|
||||
function ReadHistory:removeItemByPath(path)
|
||||
@@ -158,6 +213,7 @@ function ReadHistory:removeItemByPath(path)
|
||||
break
|
||||
end
|
||||
end
|
||||
self:ensureLastFile()
|
||||
end
|
||||
|
||||
function ReadHistory:updateItemByPath(old_path, new_path)
|
||||
@@ -165,6 +221,7 @@ function ReadHistory:updateItemByPath(old_path, new_path)
|
||||
for i = #self.hist, 1, -1 do
|
||||
if self.hist[i].file == old_path then
|
||||
self.hist[i].file = new_path
|
||||
self.hist[i].text = new_path:gsub(".*/", "")
|
||||
self:_flush()
|
||||
self.hist[i].callback = function()
|
||||
local ReaderUI = require("apps/reader/readerui")
|
||||
@@ -173,6 +230,10 @@ function ReadHistory:updateItemByPath(old_path, new_path)
|
||||
break
|
||||
end
|
||||
end
|
||||
if G_reader_settings:readSetting("lastfile") == old_path then
|
||||
G_reader_settings:saveSetting("lastfile", new_path)
|
||||
end
|
||||
self:ensureLastFile()
|
||||
end
|
||||
|
||||
function ReadHistory:removeItem(item)
|
||||
@@ -181,6 +242,7 @@ function ReadHistory:removeItem(item)
|
||||
os.remove(DocSettings:getHistoryPath(item.file))
|
||||
self:_indexing(item.index)
|
||||
self:_flush()
|
||||
self:ensureLastFile()
|
||||
end
|
||||
|
||||
function ReadHistory:addItem(file)
|
||||
@@ -198,13 +260,7 @@ function ReadHistory:addItem(file)
|
||||
self:_sort()
|
||||
self:_reduce()
|
||||
self:_flush()
|
||||
end
|
||||
end
|
||||
|
||||
function ReadHistory:setDeleted(item)
|
||||
assert(self ~= nil)
|
||||
if self.hist[item.index] then
|
||||
self.hist[item.index].dim = true
|
||||
G_reader_settings:saveSetting("lastfile", file)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -106,12 +106,8 @@ function MoveToArchive:commonProcess(is_move_process, moved_done_text)
|
||||
FileManager:copyRecursive(DocSettings:getSidecarDir(document_full_path), self.archive_dir_path)
|
||||
end
|
||||
local dest_file = string.format("%s%s", self.archive_dir_path, filename)
|
||||
ReadHistory:updateItemByPath(document_full_path, dest_file)
|
||||
ReadHistory:updateItemByPath(document_full_path, dest_file) -- (will update "lastfile" if needed)
|
||||
ReadCollection:updateItemByPath(document_full_path, dest_file)
|
||||
-- Update last open file.
|
||||
if G_reader_settings:readSetting("lastfile") == document_full_path then
|
||||
G_reader_settings:saveSetting("lastfile", dest_file)
|
||||
end
|
||||
UIManager:show(ConfirmBox:new{
|
||||
text = moved_done_text,
|
||||
ok_callback = function ()
|
||||
|
||||
@@ -744,7 +744,7 @@ function Wallabag:deleteLocalArticle(path)
|
||||
os.remove(path)
|
||||
local sdr_dir = DocSettings:getSidecarDir(path)
|
||||
FFIUtil.purgeDir(sdr_dir)
|
||||
filemanagerutil.removeFileFromHistoryIfWanted(path)
|
||||
ReadHistory:fileDeleted(path)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user