2017-04-14 04:11:10 +00:00
|
|
|
--[[--
|
|
|
|
This module is responsible for reading and writing `metadata.lua` files
|
|
|
|
in the so-called sidecar directory
|
|
|
|
([Wikipedia definition](https://en.wikipedia.org/wiki/Sidecar_file)).
|
|
|
|
]]
|
|
|
|
|
2015-06-15 08:46:43 +00:00
|
|
|
local DataStorage = require("datastorage")
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local LuaSettings = require("luasettings")
|
2015-10-03 06:18:47 +00:00
|
|
|
local dump = require("dump")
|
2019-12-10 22:00:08 +00:00
|
|
|
local ffiutil = require("ffi/util")
|
2017-04-14 04:11:10 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2017-08-17 01:38:58 +00:00
|
|
|
local logger = require("logger")
|
2021-10-21 20:43:05 +00:00
|
|
|
local util = require("util")
|
2015-10-03 06:18:47 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local DocSettings = LuaSettings:extend{}
|
2015-10-03 06:18:47 +00:00
|
|
|
|
2016-06-04 05:05:14 +00:00
|
|
|
local HISTORY_DIR = DataStorage:getHistoryDir()
|
2011-12-05 21:31:40 +00:00
|
|
|
|
2022-10-09 17:32:35 +00:00
|
|
|
local function buildCandidates(list)
|
|
|
|
local candidates = {}
|
2022-12-12 00:22:46 +00:00
|
|
|
local previous_entry_exists = false
|
2022-10-09 17:32:35 +00:00
|
|
|
|
|
|
|
for i, file_path in ipairs(list) do
|
|
|
|
-- Ignore missing files.
|
|
|
|
if file_path ~= "" and lfs.attributes(file_path, "mode") == "file" then
|
2022-12-12 00:22:46 +00:00
|
|
|
local mtime = lfs.attributes(file_path, "modification")
|
2022-12-12 01:07:01 +00:00
|
|
|
-- NOTE: Extra trickery: if we're inserting a "backup" file, and its primary buddy exists,
|
|
|
|
-- make sure it will *never* sort ahead of it by using the same mtime.
|
2022-12-12 00:22:46 +00:00
|
|
|
-- This aims to avoid weird UTC/localtime issues when USBMS is involved,
|
|
|
|
-- c.f., https://github.com/koreader/koreader/issues/9227#issuecomment-1345263324
|
|
|
|
if file_path:sub(-4) == ".old" and previous_entry_exists then
|
2022-12-12 01:07:01 +00:00
|
|
|
local primary_mtime = candidates[#candidates].mtime
|
|
|
|
-- Only proceed with the switcheroo when necessary, and warn about it.
|
|
|
|
if primary_mtime < mtime then
|
|
|
|
logger.warn("DocSettings: Backup", file_path, "is newer (", mtime, ") than its primary (", primary_mtime, "), fudging timestamps!")
|
|
|
|
-- Use the most recent timestamp for both (i.e., the backup's).
|
|
|
|
candidates[#candidates].mtime = mtime
|
|
|
|
end
|
2022-12-12 00:22:46 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
table.insert(candidates, {
|
|
|
|
path = file_path,
|
2022-12-12 00:22:46 +00:00
|
|
|
mtime = mtime,
|
2022-10-09 17:32:35 +00:00
|
|
|
prio = i,
|
|
|
|
}
|
|
|
|
)
|
2022-12-12 00:22:46 +00:00
|
|
|
previous_entry_exists = true
|
|
|
|
else
|
|
|
|
previous_entry_exists = false
|
2022-10-09 17:32:35 +00:00
|
|
|
end
|
2016-04-26 02:45:55 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
|
|
|
|
-- MRU sort, tie breaker is insertion order (higher priority locations were inserted first).
|
2022-12-12 01:07:01 +00:00
|
|
|
-- Iff a primary/backup pair of file both exist, of the two of them, the primary one *always* has priority,
|
2022-12-12 00:22:46 +00:00
|
|
|
-- regardless of mtime (c.f., NOTE above).
|
2022-10-09 17:32:35 +00:00
|
|
|
table.sort(candidates, function(l, r)
|
|
|
|
if l.mtime == r.mtime then
|
|
|
|
return l.prio < r.prio
|
|
|
|
else
|
|
|
|
return l.mtime > r.mtime
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
return candidates
|
2016-04-26 02:45:55 +00:00
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Returns path to sidecar directory (`filename.sdr`).
|
|
|
|
--
|
2016-04-26 02:45:55 +00:00
|
|
|
-- Sidecar directory is the file without _last_ suffix.
|
2017-04-14 04:11:10 +00:00
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
|
|
|
-- @treturn string path to the sidecar directory (e.g., `/foo/bar.sdr`)
|
2016-01-06 05:28:26 +00:00
|
|
|
function DocSettings:getSidecarDir(doc_path)
|
2017-01-21 09:32:42 +00:00
|
|
|
if doc_path == nil or doc_path == '' then return '' end
|
2016-12-03 13:19:35 +00:00
|
|
|
local file_without_suffix = doc_path:match("(.*)%.")
|
|
|
|
if file_without_suffix then
|
|
|
|
return file_without_suffix..".sdr"
|
|
|
|
end
|
|
|
|
return doc_path..".sdr"
|
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Returns path to `metadata.lua` file.
|
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
|
|
|
-- @treturn string path to `/foo/bar.sdr/metadata.lua` file
|
2017-01-21 09:32:42 +00:00
|
|
|
function DocSettings:getSidecarFile(doc_path)
|
|
|
|
if doc_path == nil or doc_path == '' then return '' end
|
2017-02-08 18:46:55 +00:00
|
|
|
-- If the file does not have a suffix or we are working on a directory, we
|
|
|
|
-- should ignore the suffix part in metadata file path.
|
|
|
|
local suffix = doc_path:match(".*%.(.+)")
|
|
|
|
if suffix == nil then
|
|
|
|
suffix = ''
|
|
|
|
end
|
|
|
|
return self:getSidecarDir(doc_path) .. "/metadata." .. suffix .. ".lua"
|
2017-01-21 09:32:42 +00:00
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Returns `true` if there is a `metadata.lua` file.
|
|
|
|
-- @string doc_path path to the document (e.g., `/foo/bar.pdf`)
|
|
|
|
-- @treturn bool
|
2017-02-08 18:37:36 +00:00
|
|
|
function DocSettings:hasSidecarFile(doc_path)
|
2017-02-08 18:46:55 +00:00
|
|
|
return lfs.attributes(self:getSidecarFile(doc_path), "mode") == "file"
|
2016-01-06 05:28:26 +00:00
|
|
|
end
|
|
|
|
|
2012-12-12 01:35:49 +00:00
|
|
|
function DocSettings:getHistoryPath(fullpath)
|
2022-10-09 17:32:35 +00:00
|
|
|
return HISTORY_DIR .. "/[" .. fullpath:gsub("(.*/)([^/]+)", "%1] %2"):gsub("/", "#") .. ".lua"
|
2012-12-12 01:35:49 +00:00
|
|
|
end
|
|
|
|
|
2013-03-12 04:51:00 +00:00
|
|
|
function DocSettings:getPathFromHistory(hist_name)
|
2017-01-21 09:32:42 +00:00
|
|
|
if hist_name == nil or hist_name == '' then return '' end
|
2018-08-06 19:30:05 +00:00
|
|
|
if hist_name:sub(-4) ~= ".lua" then return '' end -- ignore .lua.old backups
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 1. select everything included in brackets
|
|
|
|
local s = string.match(hist_name,"%b[]")
|
2017-01-21 09:32:42 +00:00
|
|
|
if s == nil or s == '' then return '' end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 2. crop the bracket-sign from both sides
|
|
|
|
-- 3. and finally replace decorative signs '#' to dir-char '/'
|
2022-10-09 17:32:35 +00:00
|
|
|
return string.gsub(string.sub(s, 2, -3), "#", "/")
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:getNameFromHistory(hist_name)
|
2017-01-21 09:32:42 +00:00
|
|
|
if hist_name == nil or hist_name == '' then return '' end
|
2018-08-06 19:30:05 +00:00
|
|
|
if hist_name:sub(-4) ~= ".lua" then return '' end -- ignore .lua.old backups
|
2016-09-13 03:02:48 +00:00
|
|
|
local s = string.match(hist_name, "%b[]")
|
2017-01-21 09:32:42 +00:00
|
|
|
if s == nil or s == '' then return '' end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- at first, search for path length
|
|
|
|
-- and return the rest of string without 4 last characters (".lua")
|
2016-09-13 03:02:48 +00:00
|
|
|
return string.sub(hist_name, string.len(s)+2, -5)
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
2020-08-29 16:25:36 +00:00
|
|
|
function DocSettings:getLastSaveTime(doc_path)
|
|
|
|
local attr = lfs.attributes(self:getSidecarFile(doc_path))
|
|
|
|
if attr and attr.mode == "file" then
|
|
|
|
return attr.modification
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-21 09:32:42 +00:00
|
|
|
function DocSettings:ensureSidecar(sidecar)
|
|
|
|
if lfs.attributes(sidecar, "mode") ~= "directory" then
|
|
|
|
lfs.mkdir(sidecar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Opens a document's individual settings (font, margin, dictionary, etc.)
|
|
|
|
-- @string docfile path to the document (e.g., `/foo/bar.pdf`)
|
|
|
|
-- @treturn DocSettings object
|
2011-12-05 21:31:40 +00:00
|
|
|
function DocSettings:open(docfile)
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @todo (zijiehe): Remove history_path, use only sidecar.
|
2014-07-03 12:42:48 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- NOTE: Beware, our new instance is new, but self is still DocSettings!
|
|
|
|
local new = DocSettings:extend{}
|
|
|
|
new.history_file = new:getHistoryPath(docfile)
|
|
|
|
|
|
|
|
local sidecar = new:getSidecarDir(docfile)
|
2016-06-26 00:53:08 +00:00
|
|
|
new.sidecar = sidecar
|
2017-01-21 09:32:42 +00:00
|
|
|
DocSettings:ensureSidecar(sidecar)
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- If there is a file which has a same name as the sidecar directory,
|
|
|
|
-- or the file system is read-only, we should not waste time to read it.
|
2016-06-26 00:53:08 +00:00
|
|
|
if lfs.attributes(sidecar, "mode") == "directory" then
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- New sidecar file name is metadata.{file last suffix}.lua.
|
|
|
|
-- So we can handle two files with only different suffixes.
|
|
|
|
new.sidecar_file = new:getSidecarFile(docfile)
|
2016-06-26 00:53:08 +00:00
|
|
|
new.legacy_sidecar_file = sidecar.."/"..
|
2021-12-05 21:08:20 +00:00
|
|
|
ffiutil.basename(docfile)..".lua"
|
2016-06-26 00:53:08 +00:00
|
|
|
end
|
2016-04-26 02:45:55 +00:00
|
|
|
|
2022-10-09 17:32:35 +00:00
|
|
|
-- Candidates list, in order of priority:
|
|
|
|
local candidates_list = {
|
|
|
|
-- New sidecar file
|
|
|
|
new.sidecar_file or "",
|
|
|
|
-- Backup file of new sidecar file
|
|
|
|
new.sidecar_file and (new.sidecar_file .. ".old") or "",
|
|
|
|
-- Legacy sidecar file
|
|
|
|
new.legacy_sidecar_file or "",
|
|
|
|
-- Legacy history folder
|
|
|
|
new.history_file,
|
|
|
|
-- Backup file in legacy history folder
|
|
|
|
new.history_file .. ".old",
|
|
|
|
-- Legacy kpdfview setting
|
|
|
|
docfile..".kpdfview.lua",
|
|
|
|
}
|
|
|
|
-- We get back an array of tables for *existing* candidates, sorted MRU first (insertion order breaks ties).
|
|
|
|
local candidates = buildCandidates(candidates_list)
|
|
|
|
|
2020-02-24 14:25:08 +00:00
|
|
|
local ok, stored, filepath
|
2022-10-09 17:32:35 +00:00
|
|
|
for _, t in ipairs(candidates) do
|
|
|
|
local candidate_path = t.path
|
2017-10-12 17:52:01 +00:00
|
|
|
-- Ignore empty files
|
2022-10-09 17:32:35 +00:00
|
|
|
if lfs.attributes(candidate_path, "size") > 0 then
|
|
|
|
ok, stored = pcall(dofile, candidate_path)
|
|
|
|
-- Ignore empty tables
|
2017-10-12 17:52:01 +00:00
|
|
|
if ok and next(stored) ~= nil then
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings: data is read from", candidate_path)
|
|
|
|
filepath = candidate_path
|
2017-10-12 17:52:01 +00:00
|
|
|
break
|
|
|
|
end
|
2014-07-03 12:42:48 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings:", candidate_path, "is invalid, removed.")
|
|
|
|
os.remove(candidate_path)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2014-06-04 18:55:39 +00:00
|
|
|
if ok and stored then
|
2014-03-13 13:52:43 +00:00
|
|
|
new.data = stored
|
2016-06-26 00:53:08 +00:00
|
|
|
new.candidates = candidates
|
2020-02-24 14:25:08 +00:00
|
|
|
new.filepath = filepath
|
2016-06-26 00:53:08 +00:00
|
|
|
else
|
|
|
|
new.data = {}
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2016-04-18 22:38:14 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
return new
|
2012-04-18 09:29:27 +00:00
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Serializes settings and writes them to `metadata.lua`.
|
2012-03-04 16:29:19 +00:00
|
|
|
function DocSettings:flush()
|
2016-04-26 02:45:55 +00:00
|
|
|
-- write serialized version of the data table into one of
|
|
|
|
-- i) sidecar directory in the same directory of the document or
|
|
|
|
-- ii) history directory in root directory of KOReader
|
2014-07-03 12:42:48 +00:00
|
|
|
if not self.history_file and not self.sidecar_file then
|
2014-03-13 13:52:43 +00:00
|
|
|
return
|
|
|
|
end
|
2014-07-03 12:42:48 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- If we can write to sidecar_file, we do not need to write to history_file anymore.
|
2021-11-16 18:02:42 +00:00
|
|
|
local serials = {}
|
|
|
|
if self.sidecar_file then
|
|
|
|
table.insert(serials, self.sidecar_file)
|
|
|
|
end
|
|
|
|
if self.history_file then
|
|
|
|
table.insert(serials, self.history_file)
|
|
|
|
end
|
2017-01-21 09:32:42 +00:00
|
|
|
self:ensureSidecar(self.sidecar)
|
2014-11-14 15:33:52 +00:00
|
|
|
local s_out = dump(self.data)
|
2021-10-21 20:43:05 +00:00
|
|
|
for _, f in ipairs(serials) do
|
2019-12-10 22:00:08 +00:00
|
|
|
local directory_updated = false
|
2017-08-17 01:38:58 +00:00
|
|
|
if lfs.attributes(f, "mode") == "file" then
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- As an additional safety measure (to the ffiutil.fsync* calls used below),
|
|
|
|
-- we only backup the file to .old when it has not been modified in the last 60 seconds.
|
|
|
|
-- This should ensure in the case the fsync calls are not supported
|
|
|
|
-- that the OS may have itself sync'ed that file content in the meantime.
|
2019-12-10 22:00:08 +00:00
|
|
|
local mtime = lfs.attributes(f, "modification")
|
|
|
|
if mtime < os.time() - 60 then
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings: Renamed", f, "to", f .. ".old")
|
2019-12-10 22:00:08 +00:00
|
|
|
os.rename(f, f .. ".old")
|
|
|
|
directory_updated = true -- fsync directory content too below
|
|
|
|
end
|
2017-08-17 01:38:58 +00:00
|
|
|
end
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings: Writing to", f)
|
2016-04-26 02:45:55 +00:00
|
|
|
local f_out = io.open(f, "w")
|
2014-07-03 12:42:48 +00:00
|
|
|
if f_out ~= nil then
|
2014-11-14 15:33:52 +00:00
|
|
|
f_out:write("-- we can read Lua syntax here!\nreturn ")
|
2014-07-03 12:42:48 +00:00
|
|
|
f_out:write(s_out)
|
2014-11-14 15:33:52 +00:00
|
|
|
f_out:write("\n")
|
2019-12-10 22:00:08 +00:00
|
|
|
ffiutil.fsyncOpenedFile(f_out) -- force flush to the storage device
|
2014-07-03 12:42:48 +00:00
|
|
|
f_out:close()
|
2016-06-04 19:26:05 +00:00
|
|
|
|
|
|
|
if self.candidates ~= nil
|
2021-03-06 21:44:18 +00:00
|
|
|
and G_reader_settings:nilOrFalse("preserve_legacy_docsetting") then
|
2022-10-09 17:32:35 +00:00
|
|
|
for _, t in ipairs(self.candidates) do
|
|
|
|
local candidate_path = t.path
|
|
|
|
if candidate_path ~= f and candidate_path ~= f .. ".old" then
|
|
|
|
logger.dbg("DocSettings: Removed legacy file", candidate_path)
|
|
|
|
os.remove(candidate_path)
|
2016-06-04 19:26:05 +00:00
|
|
|
-- We should not remove sidecar folder, as it may
|
|
|
|
-- contain Kindle history files.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-10 22:00:08 +00:00
|
|
|
if directory_updated then
|
|
|
|
-- Ensure the file renaming is flushed to storage device
|
|
|
|
ffiutil.fsyncDirectory(f)
|
|
|
|
end
|
2016-04-26 02:45:55 +00:00
|
|
|
break
|
2014-07-03 12:42:48 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
|
2020-02-24 14:25:08 +00:00
|
|
|
function DocSettings:getFilePath()
|
|
|
|
return self.filepath
|
|
|
|
end
|
|
|
|
|
2017-04-14 04:11:10 +00:00
|
|
|
--- Purges (removes) sidecar directory.
|
2021-10-21 20:43:05 +00:00
|
|
|
function DocSettings:purge(full)
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- Remove any of the old ones we may consider as candidates in DocSettings:open()
|
2014-10-07 05:06:52 +00:00
|
|
|
if self.history_file then
|
|
|
|
os.remove(self.history_file)
|
2021-10-21 20:43:05 +00:00
|
|
|
os.remove(self.history_file .. ".old")
|
|
|
|
end
|
|
|
|
if self.legacy_sidecar_file then
|
|
|
|
os.remove(self.legacy_sidecar_file)
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
2016-06-04 05:05:14 +00:00
|
|
|
if lfs.attributes(self.sidecar, "mode") == "directory" then
|
2021-10-21 20:43:05 +00:00
|
|
|
if full then
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- Asked to remove all the content of this .sdr directory, whether it's ours or not
|
2021-10-21 20:43:05 +00:00
|
|
|
ffiutil.purgeDir(self.sidecar)
|
|
|
|
else
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- Only remove the files we know we may have created with our usual names.
|
2021-10-21 20:43:05 +00:00
|
|
|
for f in lfs.dir(self.sidecar) do
|
|
|
|
local fullpath = self.sidecar.."/"..f
|
|
|
|
local to_remove = false
|
|
|
|
if lfs.attributes(fullpath, "mode") == "file" then
|
|
|
|
-- Currently, we only create a single file in there,
|
|
|
|
-- named metadata.suffix.lua (ie. metadata.epub.lua),
|
|
|
|
-- with possibly backups named metadata.epub.lua.old and
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- metadata.epub.lua.old_dom20180528,
|
|
|
|
-- so all sharing the same base: self.sidecar_file
|
2021-10-21 20:43:05 +00:00
|
|
|
if util.stringStartsWith(fullpath, self.sidecar_file) then
|
|
|
|
to_remove = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if to_remove then
|
|
|
|
os.remove(fullpath)
|
2022-10-09 17:32:35 +00:00
|
|
|
logger.dbg("DocSettings: purged:", fullpath)
|
2021-10-21 20:43:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
-- If the sidecar folder ends up empty, os.remove() can delete it.
|
|
|
|
-- Otherwise, the following statement has no effect.
|
|
|
|
os.remove(self.sidecar)
|
|
|
|
end
|
|
|
|
end
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
-- We should have meet the candidate we used and remove it above.
|
|
|
|
-- But in case we didn't, remove it.
|
2021-10-21 20:43:05 +00:00
|
|
|
if self.filepath and lfs.attributes(self.filepath, "mode") == "file" then
|
|
|
|
os.remove(self.filepath)
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
2017-01-21 09:32:42 +00:00
|
|
|
self.data = {}
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return DocSettings
|