2017-04-15 12:45:56 +00:00
|
|
|
--[[--
|
|
|
|
This module handles generic settings as well as KOReader's global settings system.
|
|
|
|
]]
|
|
|
|
|
2016-06-12 18:28:44 +00:00
|
|
|
local dump = require("dump")
|
|
|
|
|
|
|
|
local LuaSettings = {}
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Opens a settings file.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:open(file_path)
|
|
|
|
local new = {file=file_path}
|
|
|
|
local ok, stored
|
|
|
|
|
|
|
|
ok, stored = pcall(dofile, new.file)
|
|
|
|
if ok and stored then
|
|
|
|
new.data = stored
|
|
|
|
else
|
|
|
|
new.data = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(new, {__index = LuaSettings})
|
|
|
|
end
|
|
|
|
|
2017-03-28 22:10:36 +00:00
|
|
|
-- TODO: DocSettings can return a LuaSettings to use following awesome features.
|
|
|
|
function LuaSettings:wrap(data)
|
|
|
|
local new = {data = type(data) == "table" and data or {}}
|
|
|
|
return setmetatable(new, {__index = LuaSettings})
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--[[--Reads child settings.
|
|
|
|
|
|
|
|
@usage
|
|
|
|
|
|
|
|
Settings:saveSetting("key", {
|
|
|
|
a = "b",
|
|
|
|
c = "true",
|
|
|
|
d = false,
|
|
|
|
})
|
|
|
|
|
|
|
|
local child = Settings:child("key")
|
|
|
|
|
|
|
|
child:readSetting("a")
|
|
|
|
-- result "b"
|
|
|
|
]]
|
2017-03-28 22:10:36 +00:00
|
|
|
function LuaSettings:child(key)
|
|
|
|
return LuaSettings:wrap(self:readSetting(key))
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Reads a setting.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:readSetting(key)
|
|
|
|
return self.data[key]
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Saves a setting.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:saveSetting(key, value)
|
|
|
|
self.data[key] = value
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2016-06-12 18:28:44 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Deletes a setting.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:delSetting(key)
|
|
|
|
self.data[key] = nil
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2016-06-12 18:28:44 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Checks if setting exists.
|
2016-09-01 07:05:40 +00:00
|
|
|
function LuaSettings:has(key)
|
|
|
|
return self:readSetting(key) ~= nil
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Checks if setting does not exist.
|
2016-09-01 07:05:40 +00:00
|
|
|
function LuaSettings:hasNot(key)
|
|
|
|
return self:readSetting(key) == nil
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Checks if setting is `true`.
|
2016-09-01 07:05:40 +00:00
|
|
|
function LuaSettings:isTrue(key)
|
|
|
|
return string.lower(tostring(self:readSetting(key))) == "true"
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Checks if setting is `false`.
|
2017-03-28 22:10:36 +00:00
|
|
|
function LuaSettings:isFalse(key)
|
|
|
|
return string.lower(tostring(self:readSetting(key))) == "false"
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Checks if setting is `nil` or `true`.
|
2016-09-01 07:05:40 +00:00
|
|
|
function LuaSettings:nilOrTrue(key)
|
|
|
|
return self:hasNot(key) or self:isTrue(key)
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Checks if setting is `nil` or `false`.
|
2017-03-28 22:10:36 +00:00
|
|
|
function LuaSettings:nilOrFalse(key)
|
|
|
|
return self:hasNot(key) or self:isFalse(key)
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Flips `nil` or `true` to `false`.
|
2016-09-01 07:05:40 +00:00
|
|
|
function LuaSettings:flipNilOrTrue(key)
|
|
|
|
if self:nilOrTrue(key) then
|
|
|
|
self:saveSetting(key, false)
|
|
|
|
else
|
|
|
|
self:delSetting(key)
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2016-09-01 07:05:40 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Flips `nil` or `false` to `true`.
|
2017-03-28 22:10:36 +00:00
|
|
|
function LuaSettings:flipNilOrFalse(key)
|
|
|
|
if self:nilOrFalse(key) then
|
|
|
|
self:saveSetting(key, true)
|
|
|
|
else
|
|
|
|
self:delSetting(key)
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2017-03-28 22:10:36 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Flips setting to `true`.
|
2016-09-01 07:05:40 +00:00
|
|
|
function LuaSettings:flipTrue(key)
|
|
|
|
if self:isTrue(key) then
|
|
|
|
self:delSetting(key)
|
|
|
|
else
|
|
|
|
self:saveSetting(key, true)
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2016-09-01 07:05:40 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Flips setting to `false`.
|
2017-03-28 22:10:36 +00:00
|
|
|
function LuaSettings:flipFalse(key)
|
|
|
|
if self:isFalse(key) then
|
|
|
|
self:delSetting(key)
|
|
|
|
else
|
|
|
|
self:saveSetting(key, true)
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2017-03-28 22:10:36 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Replaces existing settings with table.
|
2017-03-24 06:36:15 +00:00
|
|
|
function LuaSettings:reset(table)
|
|
|
|
self.data = table
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2017-03-24 06:36:15 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Writes settings to disk.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:flush()
|
2017-03-28 22:10:36 +00:00
|
|
|
if not self.file then return end
|
2016-06-12 18:28:44 +00:00
|
|
|
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.data))
|
|
|
|
f_out:write("\n")
|
|
|
|
f_out:close()
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2016-06-12 18:28:44 +00:00
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Closes settings file.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:close()
|
|
|
|
self:flush()
|
|
|
|
end
|
|
|
|
|
2017-04-15 12:45:56 +00:00
|
|
|
--- Purges settings file.
|
2016-06-12 18:28:44 +00:00
|
|
|
function LuaSettings:purge()
|
|
|
|
if self.file then
|
|
|
|
os.remove(self.file)
|
|
|
|
end
|
2017-06-14 17:32:16 +00:00
|
|
|
return self
|
2016-06-12 18:28:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return LuaSettings
|