@ -141,6 +141,60 @@ function ReadHistory:_init()
self : reload ( )
self : reload ( )
end
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 ( )
function ReadHistory : clearMissing ( )
assert ( self ~= nil )
assert ( self ~= nil )
for i = # self.hist , 1 , - 1 do
for i = # self.hist , 1 , - 1 do
@ -148,6 +202,7 @@ function ReadHistory:clearMissing()
table.remove ( self.hist , i )
table.remove ( self.hist , i )
end
end
end
end
self : ensureLastFile ( )
end
end
function ReadHistory : removeItemByPath ( path )
function ReadHistory : removeItemByPath ( path )
@ -158,6 +213,7 @@ function ReadHistory:removeItemByPath(path)
break
break
end
end
end
end
self : ensureLastFile ( )
end
end
function ReadHistory : updateItemByPath ( old_path , new_path )
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
for i = # self.hist , 1 , - 1 do
if self.hist [ i ] . file == old_path then
if self.hist [ i ] . file == old_path then
self.hist [ i ] . file = new_path
self.hist [ i ] . file = new_path
self.hist [ i ] . text = new_path : gsub ( " .*/ " , " " )
self : _flush ( )
self : _flush ( )
self.hist [ i ] . callback = function ( )
self.hist [ i ] . callback = function ( )
local ReaderUI = require ( " apps/reader/readerui " )
local ReaderUI = require ( " apps/reader/readerui " )
@ -173,6 +230,10 @@ function ReadHistory:updateItemByPath(old_path, new_path)
break
break
end
end
end
end
if G_reader_settings : readSetting ( " lastfile " ) == old_path then
G_reader_settings : saveSetting ( " lastfile " , new_path )
end
self : ensureLastFile ( )
end
end
function ReadHistory : removeItem ( item )
function ReadHistory : removeItem ( item )
@ -181,6 +242,7 @@ function ReadHistory:removeItem(item)
os.remove ( DocSettings : getHistoryPath ( item.file ) )
os.remove ( DocSettings : getHistoryPath ( item.file ) )
self : _indexing ( item.index )
self : _indexing ( item.index )
self : _flush ( )
self : _flush ( )
self : ensureLastFile ( )
end
end
function ReadHistory : addItem ( file )
function ReadHistory : addItem ( file )
@ -198,13 +260,7 @@ function ReadHistory:addItem(file)
self : _sort ( )
self : _sort ( )
self : _reduce ( )
self : _reduce ( )
self : _flush ( )
self : _flush ( )
end
G_reader_settings : saveSetting ( " lastfile " , file )
end
function ReadHistory : setDeleted ( item )
assert ( self ~= nil )
if self.hist [ item.index ] then
self.hist [ item.index ] . dim = true
end
end
end
end