2021-05-03 03:20:14 +00:00
|
|
|
local ffiUtil = require("ffi/util")
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local Configurable = {}
|
|
|
|
|
2014-03-13 13:25:45 +00:00
|
|
|
function Configurable:new(o)
|
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
function Configurable:reset()
|
2021-05-03 03:20:14 +00:00
|
|
|
for key, value in pairs(self) do
|
2018-10-26 15:27:43 +00:00
|
|
|
local value_type = type(value)
|
|
|
|
if value_type == "number" or value_type == "string" then
|
2016-02-10 07:01:52 +00:00
|
|
|
self[key] = nil
|
2014-03-13 13:25:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-09 17:08:03 +00:00
|
|
|
function Configurable:hash(list)
|
2021-05-03 03:20:14 +00:00
|
|
|
for key, value in ffiUtil.orderedPairs(self) do
|
2018-10-26 15:27:43 +00:00
|
|
|
local value_type = type(value)
|
|
|
|
if value_type == "number" or value_type == "string" then
|
2021-05-09 17:08:03 +00:00
|
|
|
table.insert(list, value)
|
2014-03-13 13:25:45 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Configurable:loadDefaults(config_options)
|
2014-03-13 13:25:45 +00:00
|
|
|
-- reset configurable before loading new options
|
|
|
|
self:reset()
|
2018-10-26 15:27:43 +00:00
|
|
|
local prefix = config_options.prefix.."_"
|
2021-05-03 03:20:14 +00:00
|
|
|
for i=1, #config_options do
|
2014-03-13 13:25:45 +00:00
|
|
|
local options = config_options[i].options
|
2016-02-10 07:01:52 +00:00
|
|
|
for j=1,#options do
|
|
|
|
local key = options[j].name
|
2018-10-26 15:27:43 +00:00
|
|
|
local settings_key = prefix..key
|
2014-07-03 08:30:24 +00:00
|
|
|
local default = G_reader_settings:readSetting(settings_key)
|
2016-02-10 07:01:52 +00:00
|
|
|
self[key] = default or options[j].default_value
|
2014-03-13 13:25:45 +00:00
|
|
|
if not self[key] then
|
2016-02-10 07:01:52 +00:00
|
|
|
self[key] = options[j].default_arg
|
2014-03-13 13:25:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Configurable:loadSettings(settings, prefix)
|
2021-05-03 03:20:14 +00:00
|
|
|
for key, value in pairs(self) do
|
2018-10-26 15:27:43 +00:00
|
|
|
local value_type = type(value)
|
|
|
|
if value_type == "number" or value_type == "string"
|
|
|
|
or value_type == "table" then
|
2014-03-13 13:25:45 +00:00
|
|
|
local saved_value = settings:readSetting(prefix..key)
|
2018-10-26 15:27:43 +00:00
|
|
|
if saved_value ~= nil then
|
|
|
|
self[key] = saved_value
|
|
|
|
end
|
2014-03-13 13:25:45 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Configurable:saveSettings(settings, prefix)
|
2021-05-03 03:20:14 +00:00
|
|
|
for key, value in pairs(self) do
|
2018-10-26 15:27:43 +00:00
|
|
|
local value_type = type(value)
|
|
|
|
if value_type == "number" or value_type == "string"
|
|
|
|
or value_type == "table" then
|
2014-03-13 13:25:45 +00:00
|
|
|
settings:saveSetting(prefix..key, value)
|
|
|
|
end
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return Configurable
|