2014-08-14 11:49:42 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2013-10-18 20:38:07 +00:00
|
|
|
local DocSettings = {}
|
2011-12-05 21:31:40 +00:00
|
|
|
|
2012-12-12 01:35:49 +00:00
|
|
|
function DocSettings:getHistoryPath(fullpath)
|
2014-03-13 13:52:43 +00:00
|
|
|
local i = #fullpath - 1
|
|
|
|
-- search for last slash
|
|
|
|
while i > 0 do
|
|
|
|
if fullpath:sub(i,i) == "/" then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
i = i - 1
|
|
|
|
end
|
|
|
|
-- construct path to configuration file in history dir
|
|
|
|
local filename = fullpath:sub(i+1, -1)
|
|
|
|
local basename = fullpath:sub(1, i)
|
|
|
|
return "./history/["..basename:gsub("/","#").."] "..filename..".lua"
|
2012-12-12 01:35:49 +00:00
|
|
|
end
|
|
|
|
|
2013-03-12 04:51:00 +00:00
|
|
|
function DocSettings:getPathFromHistory(hist_name)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 1. select everything included in brackets
|
|
|
|
local s = string.match(hist_name,"%b[]")
|
|
|
|
-- 2. crop the bracket-sign from both sides
|
|
|
|
-- 3. and finally replace decorative signs '#' to dir-char '/'
|
|
|
|
return string.gsub(string.sub(s,2,-3),"#","/")
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:getNameFromHistory(hist_name)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- at first, search for path length
|
|
|
|
local s = string.len(string.match(hist_name,"%b[]"))
|
|
|
|
-- and return the rest of string without 4 last characters (".lua")
|
|
|
|
return string.sub(hist_name, s+2, -5)
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
2011-12-05 21:31:40 +00:00
|
|
|
function DocSettings:open(docfile)
|
2014-07-03 12:42:48 +00:00
|
|
|
local history_path = nil
|
|
|
|
local sidecar_path = nil
|
2014-03-13 13:52:43 +00:00
|
|
|
if docfile == ".reader" then
|
|
|
|
-- we handle reader setting as special case
|
2014-07-03 12:42:48 +00:00
|
|
|
history_path = "settings.reader.lua"
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
2014-07-03 12:42:48 +00:00
|
|
|
if lfs.attributes("./history", "mode") ~= "directory" then
|
2014-03-13 13:52:43 +00:00
|
|
|
lfs.mkdir("history")
|
|
|
|
end
|
2014-07-03 12:42:48 +00:00
|
|
|
history_path = self:getHistoryPath(docfile)
|
|
|
|
|
|
|
|
local sidecar = docfile:match("(.*)%.")..".sdr"
|
|
|
|
if lfs.attributes(sidecar, "mode") ~= "directory" then
|
|
|
|
lfs.mkdir(sidecar)
|
|
|
|
end
|
|
|
|
sidecar_path = sidecar.."/"..docfile:match(".*%/(.*)")..".lua"
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
-- construct settings obj
|
2014-07-03 12:42:48 +00:00
|
|
|
local new = {
|
|
|
|
history_file = history_path,
|
|
|
|
sidecar_file = sidecar_path,
|
|
|
|
data = {}
|
|
|
|
}
|
2014-07-03 17:41:24 +00:00
|
|
|
local ok, stored = pcall(dofile, new.history_file or "")
|
2014-03-13 13:52:43 +00:00
|
|
|
if not ok then
|
2014-07-03 17:41:24 +00:00
|
|
|
ok, stored = pcall(dofile, new.sidecar_file or "")
|
2014-07-03 12:42:48 +00:00
|
|
|
if not ok then
|
|
|
|
-- try legacy conf path, for backward compatibility. this also
|
|
|
|
-- takes care of reader legacy setting
|
|
|
|
ok, stored = pcall(dofile, docfile..".kpdfview.lua")
|
|
|
|
end
|
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
|
|
|
|
end
|
|
|
|
return setmetatable(new, { __index = DocSettings})
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
|
2012-03-20 19:15:24 +00:00
|
|
|
function DocSettings:readSetting(key)
|
2014-03-13 13:52:43 +00:00
|
|
|
return self.data[key]
|
2012-03-04 16:29:19 +00:00
|
|
|
end
|
|
|
|
|
2012-04-18 09:29:27 +00:00
|
|
|
function DocSettings:saveSetting(key, value)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.data[key] = value
|
2012-03-04 16:29:19 +00:00
|
|
|
end
|
|
|
|
|
2012-04-18 09:29:27 +00:00
|
|
|
function DocSettings:delSetting(key)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.data[key] = nil
|
2012-04-18 09:29:27 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
function DocSettings:dump(data, max_lv)
|
2014-03-13 13:52:43 +00:00
|
|
|
local out = {}
|
|
|
|
self:_serialize(data, out, 0, max_lv)
|
|
|
|
return table.concat(out)
|
2012-03-06 22:17:37 +00:00
|
|
|
end
|
|
|
|
|
2012-03-04 16:29:19 +00:00
|
|
|
-- simple serialization function, won't do uservalues, functions, loops
|
2013-03-15 09:01:34 +00:00
|
|
|
function DocSettings:_serialize(what, outt, indent, max_lv)
|
2014-03-13 13:52:43 +00:00
|
|
|
if not max_lv then
|
|
|
|
max_lv = math.huge
|
|
|
|
end
|
2013-03-15 09:01:34 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
if indent > max_lv then
|
|
|
|
return
|
|
|
|
end
|
2013-03-15 09:01:34 +00:00
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
if type(what) == "table" then
|
|
|
|
local didrun = false
|
|
|
|
table.insert(outt, "{")
|
|
|
|
for k, v in pairs(what) do
|
|
|
|
if didrun then
|
|
|
|
table.insert(outt, ",")
|
|
|
|
end
|
|
|
|
table.insert(outt, "\n")
|
|
|
|
table.insert(outt, string.rep("\t", indent+1))
|
|
|
|
table.insert(outt, "[")
|
|
|
|
self:_serialize(k, outt, indent+1, max_lv)
|
|
|
|
table.insert(outt, "] = ")
|
|
|
|
self:_serialize(v, outt, indent+1, max_lv)
|
|
|
|
didrun = true
|
|
|
|
end
|
|
|
|
if didrun then
|
|
|
|
table.insert(outt, "\n")
|
|
|
|
table.insert(outt, string.rep("\t", indent))
|
|
|
|
end
|
|
|
|
table.insert(outt, "}")
|
|
|
|
elseif type(what) == "string" then
|
|
|
|
table.insert(outt, string.format("%q", what))
|
|
|
|
elseif type(what) == "number" or type(what) == "boolean" then
|
|
|
|
table.insert(outt, tostring(what))
|
|
|
|
end
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
|
2012-03-04 16:29:19 +00:00
|
|
|
function DocSettings:flush()
|
2014-07-03 12:42:48 +00:00
|
|
|
-- write serialized version of the data table into
|
|
|
|
-- i) history directory in root directory of koreader
|
|
|
|
-- ii) sidecar directory in the same directory of the document
|
|
|
|
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
|
|
|
|
|
|
|
local serials = {}
|
|
|
|
if self.history_file then
|
2014-07-15 02:14:52 +00:00
|
|
|
pcall(table.insert, serials, io.open(self.history_file, "w"))
|
2014-07-03 12:42:48 +00:00
|
|
|
end
|
|
|
|
if self.sidecar_file then
|
2014-07-15 02:14:52 +00:00
|
|
|
pcall(table.insert, serials, io.open(self.sidecar_file, "w"))
|
2014-07-03 12:42:48 +00:00
|
|
|
end
|
|
|
|
os.setlocale('C', 'numeric')
|
|
|
|
local out = {"-- we can read Lua syntax here!\nreturn "}
|
|
|
|
self:_serialize(self.data, out, 0)
|
|
|
|
table.insert(out, "\n")
|
|
|
|
local s_out = table.concat(out)
|
|
|
|
for _, f_out in ipairs(serials) do
|
|
|
|
if f_out ~= nil then
|
|
|
|
f_out:write(s_out)
|
|
|
|
f_out:close()
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettings:close()
|
2014-03-13 13:52:43 +00:00
|
|
|
self:flush()
|
2011-12-05 21:31:40 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return DocSettings
|