2022-09-27 23:10:50 +00:00
|
|
|
--[[--
|
|
|
|
Subclass of LuaSettings dedicated to handling the legacy global constants.
|
|
|
|
]]
|
|
|
|
|
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
local LuaSettings = require("luasettings")
|
|
|
|
local dump = require("dump")
|
|
|
|
local util = require("util")
|
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
|
|
local logger = require("logger")
|
|
|
|
|
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 LuaDefaults = LuaSettings:extend{
|
2022-09-27 23:10:50 +00:00
|
|
|
ro = nil, -- will contain the defaults.lua k/v pairs (const)
|
|
|
|
rw = nil, -- will only contain non-defaults user-modified k/v pairs
|
|
|
|
}
|
|
|
|
|
|
|
|
--- Opens a settings file.
|
|
|
|
function LuaDefaults:open(path)
|
|
|
|
local file_path = path or DataStorage:getDataDir() .. "/defaults.custom.lua"
|
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 new = LuaDefaults:extend{
|
|
|
|
file = file_path,
|
|
|
|
}
|
2022-09-27 23:10:50 +00:00
|
|
|
local ok, stored
|
|
|
|
|
|
|
|
-- File being absent and returning an empty table is a use case,
|
|
|
|
-- so logger.warn() only if there was an existing file
|
|
|
|
local existing = lfs.attributes(new.file, "mode") == "file"
|
|
|
|
|
|
|
|
ok, stored = pcall(dofile, new.file)
|
|
|
|
if ok and stored then
|
|
|
|
new.rw = stored
|
|
|
|
else
|
2022-10-09 17:32:35 +00:00
|
|
|
if existing then logger.warn("LuaDefaults: Failed reading", new.file, "(probably corrupted).") end
|
2022-09-27 23:10:50 +00:00
|
|
|
-- Fallback to .old if it exists
|
|
|
|
ok, stored = pcall(dofile, new.file..".old")
|
|
|
|
if ok and stored then
|
2022-10-09 17:32:35 +00:00
|
|
|
if existing then logger.warn("LuaDefaults: read from backup file", new.file..".old") end
|
2022-09-27 23:10:50 +00:00
|
|
|
new.rw = stored
|
|
|
|
else
|
2022-10-09 17:32:35 +00:00
|
|
|
if existing then logger.warn("LuaDefaults: no usable backup file for", new.file, "to read from") end
|
2022-09-27 23:10:50 +00:00
|
|
|
new.rw = {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- The actual defaults file, on the other hand, is set in stone.
|
2022-10-06 03:49:18 +00:00
|
|
|
ok, stored = pcall(dofile, "defaults.lua")
|
2022-09-27 23:10:50 +00:00
|
|
|
if ok and stored then
|
|
|
|
new.ro = stored
|
|
|
|
else
|
2022-10-06 03:49:18 +00:00
|
|
|
error("Failed reading defaults.lua")
|
2022-09-27 23:10:50 +00:00
|
|
|
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
|
|
|
return new
|
2022-09-27 23:10:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Reads a setting, optionally initializing it to a default.
|
|
|
|
function LuaDefaults:readSetting(key, default)
|
|
|
|
if not default then
|
|
|
|
if self:hasBeenCustomized(key) then
|
|
|
|
return self.rw[key]
|
|
|
|
else
|
|
|
|
return self.ro[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not self:hasBeenCustomized(key) then
|
|
|
|
self.rw[key] = default
|
|
|
|
return self.rw[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
if self:hasBeenCustomized(key) then
|
|
|
|
return self.rw[key]
|
|
|
|
else
|
|
|
|
return self.ro[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Saves a setting.
|
|
|
|
function LuaDefaults:saveSetting(key, value)
|
|
|
|
if util.tableEquals(self.ro[key], value, true) then
|
|
|
|
-- Only keep actually custom settings in the rw table ;).
|
|
|
|
return self:delSetting(key)
|
|
|
|
else
|
|
|
|
self.rw[key] = value
|
|
|
|
end
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Deletes a setting.
|
|
|
|
function LuaDefaults:delSetting(key)
|
|
|
|
self.rw[key] = nil
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks if setting exists.
|
|
|
|
function LuaDefaults:has(key)
|
|
|
|
return self.ro[key] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks if setting does not exist.
|
|
|
|
function LuaDefaults:hasNot(key)
|
|
|
|
return self.ro[key] == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks if setting has been customized.
|
|
|
|
function LuaDefaults:hasBeenCustomized(key)
|
|
|
|
return self.rw[key] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks if setting has NOT been customized.
|
|
|
|
function LuaDefaults:hasNotBeenCustomized(key)
|
|
|
|
return self.rw[key] == nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks if setting is `true` (boolean).
|
|
|
|
function LuaDefaults:isTrue(key)
|
|
|
|
if self:hasBeenCustomized(key) then
|
|
|
|
return self.rw[key] == true
|
|
|
|
else
|
|
|
|
return self.ro[key] == true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Checks if setting is `false` (boolean).
|
|
|
|
function LuaDefaults:isFalse(key)
|
|
|
|
if self:hasBeenCustomized(key) then
|
|
|
|
return self.rw[key] == false
|
|
|
|
else
|
|
|
|
return self.ro[key] == false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Low-level API for filemanagersetdefaults
|
|
|
|
function LuaDefaults:getDataTables()
|
|
|
|
return self.ro, self.rw
|
|
|
|
end
|
|
|
|
|
|
|
|
function LuaDefaults:readDefaultSetting(key)
|
|
|
|
return self.ro[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- NOP unsupported LuaSettings APIs
|
|
|
|
function LuaDefaults:wrap() end
|
|
|
|
function LuaDefaults:child() end
|
|
|
|
function LuaDefaults:initializeExtSettings() end
|
|
|
|
function LuaDefaults:getSettingForExt() end
|
|
|
|
function LuaDefaults:saveSettingForExt() end
|
|
|
|
function LuaDefaults:addTableItem() end
|
|
|
|
function LuaDefaults:removeTableItem() end
|
|
|
|
function LuaDefaults:reset() end
|
|
|
|
|
|
|
|
--- Writes settings to disk.
|
|
|
|
function LuaDefaults:flush()
|
|
|
|
if not self.file then return end
|
2023-10-17 04:42:07 +00:00
|
|
|
local directory_updated = self:backup() -- LuaSettings
|
|
|
|
util.writeToFile(dump(self.rw, nil, true), self.file, true, true, directory_updated)
|
2022-09-27 23:10:50 +00:00
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
|
|
|
return LuaDefaults
|