read/save document settings in document sidecar too

pull/688/head
chrox 10 years ago
parent 540e8579c7
commit 2900432efb

@ -32,23 +32,37 @@ function DocSettings:getNameFromHistory(hist_name)
end end
function DocSettings:open(docfile) function DocSettings:open(docfile)
local conf_path = nil local history_path = nil
local sidecar_path = nil
if docfile == ".reader" then if docfile == ".reader" then
-- we handle reader setting as special case -- we handle reader setting as special case
conf_path = "settings.reader.lua" history_path = "settings.reader.lua"
else else
if lfs.attributes("./history","mode") ~= "directory" then if lfs.attributes("./history", "mode") ~= "directory" then
lfs.mkdir("history") lfs.mkdir("history")
end end
conf_path = self:getHistoryPath(docfile) 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"
end end
-- construct settings obj -- construct settings obj
local new = { file = conf_path, data = {} } local new = {
local ok, stored = pcall(dofile, new.file) history_file = history_path,
sidecar_file = sidecar_path,
data = {}
}
local ok, stored = pcall(dofile, new.history_file)
if not ok then if not ok then
-- try legacy conf path, for backward compatibility. this also ok, stored = pcall(dofile, new.sidecar_file)
-- takes care of reader legacy setting if not ok then
ok, stored = pcall(dofile, docfile..".kpdfview.lua") -- try legacy conf path, for backward compatibility. this also
-- takes care of reader legacy setting
ok, stored = pcall(dofile, docfile..".kpdfview.lua")
end
end end
if ok and stored then if ok and stored then
new.data = stored new.data = stored
@ -112,18 +126,30 @@ function DocSettings:_serialize(what, outt, indent, max_lv)
end end
function DocSettings:flush() function DocSettings:flush()
-- write a serialized version of the data table -- write serialized version of the data table into
if not self.file then -- 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
return return
end end
local f_out = io.open(self.file, "w")
if f_out ~= nil then local serials = {}
os.setlocale('C', 'numeric') if self.history_file then
local out = {"-- we can read Lua syntax here!\nreturn "} table.insert(serials, io.open(self.history_file, "w"))
self:_serialize(self.data, out, 0) end
table.insert(out, "\n") if self.sidecar_file then
f_out:write(table.concat(out)) table.insert(serials, io.open(self.sidecar_file, "w"))
f_out:close() 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
end end
end end

Loading…
Cancel
Save