mirror of https://github.com/koreader/koreader
Misc: Get rid of the legacy defaults.lua globals (#9546)
* This removes support for the following deprecated constants: `DTAP_ZONE_FLIPPING`, `DTAP_ZONE_BOOKMARK`, `DCREREADER_CONFIG_DEFAULT_FONT_GAMMA` * The "Advanced settings" panel now highlights modified values in bold (think about:config in Firefox ;)). * LuaData: Isolate global table lookup shenanigans, and fix a few issues in unused-in-prod codepaths. * CodeStyle: Require module locals for Lua/C modules, too. * ScreenSaver: Actually garbage collect our widget on close (ScreenSaver itself is not an instantiated object). * DateTimeWidget: Code cleanups to ensure child widgets can be GC'ed.pull/9566/head
parent
46f729c248
commit
62059f8d68
@ -1 +1 @@
|
|||||||
Subproject commit 452695ff92a63476e90d9762f172c1486276901a
|
Subproject commit 2ed9aba5de31e5e60f18bf451df735fe91ddde3a
|
@ -0,0 +1,191 @@
|
|||||||
|
--[[--
|
||||||
|
Subclass of LuaSettings dedicated to handling the legacy global constants.
|
||||||
|
]]
|
||||||
|
|
||||||
|
local DataStorage = require("datastorage")
|
||||||
|
local LuaSettings = require("luasettings")
|
||||||
|
local dump = require("dump")
|
||||||
|
local ffiutil = require("ffi/util")
|
||||||
|
local util = require("util")
|
||||||
|
local isAndroid, android = pcall(require, "android")
|
||||||
|
local lfs = require("libs/libkoreader-lfs")
|
||||||
|
local logger = require("logger")
|
||||||
|
|
||||||
|
local LuaDefaults = LuaSettings:new{
|
||||||
|
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"
|
||||||
|
local new = {file = file_path}
|
||||||
|
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
|
||||||
|
if existing then logger.warn("Failed reading", new.file, "(probably corrupted).") end
|
||||||
|
-- Fallback to .old if it exists
|
||||||
|
ok, stored = pcall(dofile, new.file..".old")
|
||||||
|
if ok and stored then
|
||||||
|
if existing then logger.warn("read from backup file", new.file..".old") end
|
||||||
|
new.rw = stored
|
||||||
|
else
|
||||||
|
if existing then logger.warn("no usable backup file for", new.file, "to read from") end
|
||||||
|
new.rw = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- The actual defaults file, on the other hand, is set in stone.
|
||||||
|
-- We just have to deal with some platform shenanigans...
|
||||||
|
local defaults_path = DataStorage:getDataDir() .. "/defaults.lua"
|
||||||
|
if isAndroid then
|
||||||
|
defaults_path = android.dir .. "/defaults.lua"
|
||||||
|
elseif os.getenv("APPIMAGE") then
|
||||||
|
defaults_path = "defaults.lua"
|
||||||
|
end
|
||||||
|
ok, stored = pcall(dofile, defaults_path)
|
||||||
|
if ok and stored then
|
||||||
|
new.ro = stored
|
||||||
|
else
|
||||||
|
error("Failed reading " .. defaults_path)
|
||||||
|
end
|
||||||
|
|
||||||
|
return setmetatable(new, {__index = LuaDefaults})
|
||||||
|
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
|
||||||
|
local directory_updated = false
|
||||||
|
if lfs.attributes(self.file, "mode") == "file" then
|
||||||
|
-- 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.
|
||||||
|
local mtime = lfs.attributes(self.file, "modification")
|
||||||
|
if mtime < os.time() - 60 then
|
||||||
|
os.rename(self.file, self.file .. ".old")
|
||||||
|
directory_updated = true -- fsync directory content too below
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local f_out = io.open(self.file, "w")
|
||||||
|
if f_out ~= nil then
|
||||||
|
os.setlocale('C', 'numeric')
|
||||||
|
f_out:write("-- we can read Lua syntax here!\nreturn ")
|
||||||
|
f_out:write(dump(self.rw, nil, true))
|
||||||
|
f_out:write("\n")
|
||||||
|
ffiutil.fsyncOpenedFile(f_out) -- force flush to the storage device
|
||||||
|
f_out:close()
|
||||||
|
end
|
||||||
|
if directory_updated then
|
||||||
|
-- Ensure the file renaming is flushed to storage device
|
||||||
|
ffiutil.fsyncDirectory(self.file)
|
||||||
|
end
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
return LuaDefaults
|
@ -1,101 +1,49 @@
|
|||||||
describe("defaults module", function()
|
describe("defaults module", function()
|
||||||
local Defaults, DataStorage
|
local Defaults, DataStorage, lfs, persistent_filename
|
||||||
setup(function()
|
setup(function()
|
||||||
require("commonrequire")
|
require("commonrequire")
|
||||||
Defaults = require("apps/filemanager/filemanagersetdefaults")
|
|
||||||
DataStorage = require("datastorage")
|
DataStorage = require("datastorage")
|
||||||
|
persistent_filename = DataStorage:getDataDir() .. "/defaults.defaults_spec.lua"
|
||||||
|
Defaults = require("luadefaults"):open(persistent_filename)
|
||||||
|
lfs = require("libs/libkoreader-lfs")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("should load all defaults from defaults.lua", function()
|
it("should load all defaults from defaults.lua", function()
|
||||||
Defaults:init()
|
assert.is_true(Defaults:has("DHINTCOUNT"))
|
||||||
assert.is_same(98, #Defaults.defaults_name)
|
Defaults:close()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("should save changes to defaults.persistent.lua", function()
|
it("should save changes to defaults.custom.lua", function()
|
||||||
local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua"
|
|
||||||
os.remove(persistent_filename)
|
os.remove(persistent_filename)
|
||||||
|
os.remove(persistent_filename .. ".old")
|
||||||
|
|
||||||
-- To see indices and help updating this when new settings are added:
|
-- This defaults to false
|
||||||
-- for i=1, 98 do print(i.." ".. Defaults.defaults_name[i]) end
|
Defaults:makeTrue("DSHOWOVERLAP")
|
||||||
|
assert.is_true(Defaults:hasBeenCustomized("DSHOWOVERLAP"))
|
||||||
|
assert.is_true(Defaults:isTrue("DSHOWOVERLAP"))
|
||||||
|
|
||||||
-- not in persistent but checked in defaults
|
Defaults:close()
|
||||||
Defaults.changed[18] = true
|
assert.is_true(lfs.attributes(persistent_filename, "mode") == "file")
|
||||||
Defaults.changed[48] = true
|
|
||||||
Defaults.changed[54] = true
|
Defaults = nil
|
||||||
Defaults.changed[83] = true
|
Defaults = require("luadefaults"):open(persistent_filename)
|
||||||
Defaults:saveSettings()
|
assert.is_true(Defaults:hasBeenCustomized("DSHOWOVERLAP"))
|
||||||
assert.is_same(98, #Defaults.defaults_name)
|
assert.is_true(Defaults:isTrue("DSHOWOVERLAP"))
|
||||||
assert.is_same("DTAP_ZONE_BACKWARD", Defaults.defaults_name[84])
|
Defaults:makeFalse("DSHOWOVERLAP")
|
||||||
assert.is_same("DCREREADER_CONFIG_WORD_SPACING_LARGE", Defaults.defaults_name[48])
|
Defaults:close()
|
||||||
assert.is_same("DCREREADER_CONFIG_H_MARGIN_SIZES_XXX_LARGE", Defaults.defaults_name[18])
|
|
||||||
dofile(persistent_filename)
|
|
||||||
assert.is_same(DCREREADER_CONFIG_WORD_SPACING_LARGE, { [1] = 100, [2] = 90 })
|
|
||||||
assert.is_same(DTAP_ZONE_BACKWARD, { ["y"] = 0, ["x"] = 0, ["h"] = 1, ["w"] = 0.25 })
|
|
||||||
assert.is_same(DCREREADER_CONFIG_H_MARGIN_SIZES_XXX_LARGE, { [1] = 50, [2] = 50 })
|
|
||||||
assert.is_same(DDOUBLE_TAP_ZONE_PREV_CHAPTER, { ["y"] = 0, ["x"] = 0, ["h"] = 1, ["w"] = 0.25 })
|
|
||||||
|
|
||||||
-- in persistent
|
|
||||||
Defaults:init()
|
|
||||||
Defaults.changed[54] = true
|
|
||||||
Defaults.defaults_value[54] = {
|
|
||||||
y = 0,
|
|
||||||
x = 0,
|
|
||||||
h = 0.25,
|
|
||||||
w = 0.75
|
|
||||||
}
|
|
||||||
Defaults.changed[84] = true
|
|
||||||
Defaults.defaults_value[84] = {
|
|
||||||
y = 10,
|
|
||||||
x = 10.125,
|
|
||||||
h = 20.25,
|
|
||||||
w = 20.75
|
|
||||||
}
|
|
||||||
Defaults:saveSettings()
|
|
||||||
dofile(persistent_filename)
|
|
||||||
assert.is_same(DCREREADER_CONFIG_WORD_SPACING_LARGE, { [2] = 90, [1] = 100 })
|
|
||||||
assert.is_same(DDOUBLE_TAP_ZONE_PREV_CHAPTER, {
|
|
||||||
["y"] = 0,
|
|
||||||
["x"] = 0,
|
|
||||||
["h"] = 0.25,
|
|
||||||
["w"] = 0.75
|
|
||||||
})
|
|
||||||
assert.is_same(DCREREADER_CONFIG_H_MARGIN_SIZES_XXX_LARGE, { [2] = 50, [1] = 50 })
|
|
||||||
assert.is_same(DTAP_ZONE_BACKWARD, {
|
|
||||||
["y"] = 10,
|
|
||||||
["x"] = 10.125,
|
|
||||||
["h"] = 20.25,
|
|
||||||
["w"] = 20.75
|
|
||||||
})
|
|
||||||
os.remove(persistent_filename)
|
os.remove(persistent_filename)
|
||||||
|
os.remove(persistent_filename .. ".old")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("should delete entry from defaults.persistent.lua if value is reverted back to default", function()
|
it("should delete entry from defaults.custom.lua if value is reverted back to default", function()
|
||||||
local persistent_filename = DataStorage:getDataDir() .. "/defaults.persistent.lua"
|
-- This defaults to false
|
||||||
local fd = io.open(persistent_filename, "w")
|
Defaults:makeTrue("DSHOWOVERLAP")
|
||||||
fd:write(
|
assert.is_true(Defaults:hasBeenCustomized("DSHOWOVERLAP"))
|
||||||
[[-- For configuration changes that persists between updates
|
assert.is_true(Defaults:isTrue("DSHOWOVERLAP"))
|
||||||
DCREREADER_CONFIG_H_MARGIN_SIZES_LARGE = {
|
Defaults:makeFalse("DSHOWOVERLAP")
|
||||||
[1] = 15,
|
assert.is_true(Defaults:hasNotBeenCustomized("DSHOWOVERLAP"))
|
||||||
[2] = 15
|
assert.is_true(Defaults:isFalse("DSHOWOVERLAP"))
|
||||||
}
|
Defaults:close()
|
||||||
DCREREADER_VIEW_MODE = "page"
|
|
||||||
DHINTCOUNT = 2
|
|
||||||
]])
|
|
||||||
fd:close()
|
|
||||||
|
|
||||||
-- in persistent
|
|
||||||
Defaults:init()
|
|
||||||
Defaults.changed[56] = true
|
|
||||||
Defaults.defaults_value[56] = 1
|
|
||||||
Defaults:saveSettings()
|
|
||||||
dofile(persistent_filename)
|
|
||||||
assert.Equals(DCREREADER_VIEW_MODE, "page")
|
|
||||||
assert.is_same(DCREREADER_CONFIG_H_MARGIN_SIZES_LARGE, {
|
|
||||||
[2] = 15,
|
|
||||||
[1] = 15
|
|
||||||
})
|
|
||||||
assert.Equals(DGLOBAL_CACHE_FREE_PROPORTION, 1)
|
|
||||||
assert.Equals(DHINTCOUNT, 2)
|
|
||||||
os.remove(persistent_filename)
|
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
Loading…
Reference in New Issue