2016-04-18 22:38:14 +00:00
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
local DocSettings = require("docsettings")
|
|
|
|
local dump = require("dump")
|
2019-01-01 08:44:21 +00:00
|
|
|
local getFriendlySize = require("util").getFriendlySize
|
2017-07-19 12:56:17 +00:00
|
|
|
local joinPath = require("ffi/util").joinPath
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
|
|
local realpath = require("ffi/util").realpath
|
2016-04-18 22:38:14 +00:00
|
|
|
|
|
|
|
local history_file = joinPath(DataStorage:getDataDir(), "history.lua")
|
|
|
|
|
|
|
|
local ReadHistory = {
|
|
|
|
hist = {},
|
2017-07-19 12:56:17 +00:00
|
|
|
last_read_time = 0,
|
2016-04-18 22:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local function buildEntry(input_time, input_file)
|
2019-01-01 08:44:21 +00:00
|
|
|
local file_exists = lfs.attributes(input_file, "mode") == "file"
|
2016-04-18 22:38:14 +00:00
|
|
|
return {
|
|
|
|
time = input_time,
|
|
|
|
text = input_file:gsub(".*/", ""),
|
2017-09-22 16:24:38 +00:00
|
|
|
file = realpath(input_file) or input_file, -- keep orig file path of deleted files
|
2019-01-01 08:44:21 +00:00
|
|
|
dim = not file_exists, -- "dim", as expected by Menu
|
|
|
|
mandatory = file_exists and getFriendlySize(lfs.attributes(input_file, "size") or 0),
|
2016-04-18 22:38:14 +00:00
|
|
|
callback = function()
|
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
|
|
|
ReaderUI:showReader(input_file)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-09-13 03:02:48 +00:00
|
|
|
local function fileFirstOrdering(l, r)
|
|
|
|
if l.file == r.file then
|
|
|
|
return l.time > r.time
|
|
|
|
else
|
|
|
|
return l.file < r.file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function timeFirstOrdering(l, r)
|
|
|
|
if l.time == r.time then
|
|
|
|
return l.file < r.file
|
|
|
|
else
|
|
|
|
return l.time > r.time
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-19 05:37:57 +00:00
|
|
|
function ReadHistory:_indexing(start)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2016-07-19 05:37:57 +00:00
|
|
|
-- TODO(Hzj_jie): Use binary search to find an item when deleting it.
|
|
|
|
for i = start, #self.hist, 1 do
|
|
|
|
self.hist[i].index = i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-18 22:38:14 +00:00
|
|
|
function ReadHistory:_sort()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
|
|
|
local autoremove_deleted_items_from_history =
|
|
|
|
not G_reader_settings:nilOrFalse("autoremove_deleted_items_from_history")
|
2017-02-25 21:16:19 +00:00
|
|
|
if autoremove_deleted_items_from_history then
|
|
|
|
self:clearMissing()
|
|
|
|
end
|
2016-09-13 03:02:48 +00:00
|
|
|
table.sort(self.hist, fileFirstOrdering)
|
2016-04-18 22:38:14 +00:00
|
|
|
-- TODO(zijiehe): Use binary insert instead of a loop to deduplicate.
|
|
|
|
for i = #self.hist, 2, -1 do
|
|
|
|
if self.hist[i].file == self.hist[i - 1].file then
|
2016-09-13 03:02:48 +00:00
|
|
|
table.remove(self.hist, i)
|
2016-04-18 22:38:14 +00:00
|
|
|
end
|
|
|
|
end
|
2016-09-13 03:02:48 +00:00
|
|
|
table.sort(self.hist, timeFirstOrdering)
|
2016-07-19 05:37:57 +00:00
|
|
|
self:_indexing(1)
|
2016-04-18 22:38:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Reduces total count in hist list to a reasonable number by removing last
|
|
|
|
-- several items.
|
|
|
|
function ReadHistory:_reduce()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2016-04-18 22:38:14 +00:00
|
|
|
while #self.hist > 500 do
|
|
|
|
table.remove(self.hist, #self.hist)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Flushes current history table into file.
|
|
|
|
function ReadHistory:_flush()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2016-04-18 22:38:14 +00:00
|
|
|
local content = {}
|
|
|
|
for k, v in pairs(self.hist) do
|
|
|
|
content[k] = {
|
|
|
|
time = v.time,
|
|
|
|
file = v.file
|
|
|
|
}
|
|
|
|
end
|
|
|
|
local f = io.open(history_file, "w")
|
|
|
|
f:write("return " .. dump(content) .. "\n")
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
2017-07-19 12:56:17 +00:00
|
|
|
--- Reads history table from file.
|
|
|
|
-- @treturn boolean true if the history_file has been updated and reloaded.
|
2016-04-18 22:38:14 +00:00
|
|
|
function ReadHistory:_read()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
|
|
|
local history_file_modification_time = lfs.attributes(history_file, "modification")
|
|
|
|
if history_file_modification_time == nil
|
|
|
|
or history_file_modification_time <= self.last_read_time then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
self.last_read_time = history_file_modification_time
|
2016-04-18 22:38:14 +00:00
|
|
|
local ok, data = pcall(dofile, history_file)
|
2016-09-13 03:02:48 +00:00
|
|
|
if ok and data then
|
2016-04-18 22:38:14 +00:00
|
|
|
for k, v in pairs(data) do
|
|
|
|
table.insert(self.hist, buildEntry(v.time, v.file))
|
|
|
|
end
|
|
|
|
end
|
2017-07-19 12:56:17 +00:00
|
|
|
return true
|
2016-04-18 22:38:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Reads history from legacy history folder
|
|
|
|
function ReadHistory:_readLegacyHistory()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2016-04-18 22:38:14 +00:00
|
|
|
local history_dir = DataStorage:getHistoryDir()
|
|
|
|
for f in lfs.dir(history_dir) do
|
|
|
|
local path = joinPath(history_dir, f)
|
|
|
|
if lfs.attributes(path, "mode") == "file" then
|
2016-07-19 05:37:57 +00:00
|
|
|
path = DocSettings:getPathFromHistory(f)
|
|
|
|
if path ~= nil and path ~= "" then
|
|
|
|
local file = DocSettings:getNameFromHistory(f)
|
|
|
|
if file ~= nil and file ~= "" then
|
|
|
|
table.insert(
|
|
|
|
self.hist,
|
2016-09-13 03:02:48 +00:00
|
|
|
buildEntry(lfs.attributes(joinPath(history_dir, f), "modification"),
|
2016-07-19 05:37:57 +00:00
|
|
|
joinPath(path, file)))
|
|
|
|
end
|
|
|
|
end
|
2016-04-18 22:38:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReadHistory:_init()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
|
|
|
self:reload()
|
2016-04-18 22:38:14 +00:00
|
|
|
end
|
|
|
|
|
2017-02-25 18:22:04 +00:00
|
|
|
function ReadHistory:clearMissing()
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2017-02-25 18:22:04 +00:00
|
|
|
for i = #self.hist, 1, -1 do
|
|
|
|
if self.hist[i].file == nil or lfs.attributes(self.hist[i].file, "mode") ~= "file" then
|
|
|
|
table.remove(self.hist, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-25 21:16:19 +00:00
|
|
|
function ReadHistory:removeItemByPath(path)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2017-02-25 21:16:19 +00:00
|
|
|
for i = #self.hist, 1, -1 do
|
|
|
|
if self.hist[i].file == path then
|
|
|
|
self:removeItem(self.hist[i])
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-16 17:20:25 +00:00
|
|
|
function ReadHistory:updateItemByPath(old_path, new_path)
|
|
|
|
assert(self ~= nil)
|
|
|
|
for i = #self.hist, 1, -1 do
|
|
|
|
if self.hist[i].file == old_path then
|
|
|
|
self.hist[i].file = new_path
|
|
|
|
self:_flush()
|
|
|
|
self.hist[i].callback = function()
|
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
|
|
|
ReaderUI:showReader(new_path)
|
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-18 22:38:14 +00:00
|
|
|
function ReadHistory:removeItem(item)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2016-04-18 22:38:14 +00:00
|
|
|
table.remove(self.hist, item.index)
|
|
|
|
os.remove(DocSettings:getHistoryPath(item.file))
|
2016-07-19 05:37:57 +00:00
|
|
|
self:_indexing(item.index)
|
2016-04-18 22:38:14 +00:00
|
|
|
self:_flush()
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReadHistory:addItem(file)
|
2017-07-19 12:56:17 +00:00
|
|
|
assert(self ~= nil)
|
2016-04-18 22:38:14 +00:00
|
|
|
if file ~= nil and lfs.attributes(file, "mode") == "file" then
|
2018-05-30 04:50:15 +00:00
|
|
|
local now = os.time()
|
|
|
|
table.insert(self.hist, 1, buildEntry(now, file))
|
2016-04-18 22:38:14 +00:00
|
|
|
-- TODO(zijiehe): We do not need to sort if we can use binary insert and
|
|
|
|
-- binary search.
|
2018-05-30 04:50:15 +00:00
|
|
|
-- util.execute("/bin/touch", "-a", file)
|
|
|
|
-- This emulates `touch -a` in LuaFileSystem's API, since it may be absent (Android)
|
|
|
|
-- or provided by busybox, which doesn't support the `-a` flag.
|
|
|
|
local mtime = lfs.attributes(file, "modification")
|
|
|
|
lfs.touch(file, now, mtime)
|
2016-04-18 22:38:14 +00:00
|
|
|
self:_sort()
|
|
|
|
self:_reduce()
|
|
|
|
self:_flush()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-22 16:24:38 +00:00
|
|
|
function ReadHistory:setDeleted(item)
|
|
|
|
assert(self ~= nil)
|
|
|
|
if self.hist[item.index] then
|
|
|
|
self.hist[item.index].dim = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-07-19 12:56:17 +00:00
|
|
|
--- Reloads history from history_file.
|
|
|
|
-- @treturn boolean true if history_file has been updated and reload happened.
|
|
|
|
function ReadHistory:reload()
|
|
|
|
assert(self ~= nil)
|
|
|
|
if self:_read() then
|
|
|
|
self:_readLegacyHistory()
|
|
|
|
self:_sort()
|
|
|
|
self:_reduce()
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2016-04-18 22:38:14 +00:00
|
|
|
ReadHistory:_init()
|
|
|
|
|
|
|
|
return ReadHistory
|