2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/docsettings.lua

117 lines
3.4 KiB
Lua
Raw Normal View History

2014-08-14 11:49:42 +00:00
local lfs = require("libs/libkoreader-lfs")
2013-10-18 20:38:07 +00:00
local DocSettings = {}
local dump = require("dump")
local DataStorage = require("datastorage")
function DocSettings:getHistoryPath(fullpath)
2014-09-18 17:04:00 +00:00
return "./history/[" .. fullpath:gsub("(.*/)([^/]+)","%1] %2"):gsub("/","#") .. ".lua"
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
function DocSettings:open(docfile)
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
history_path = DataStorage:getDataDir() .. "/settings.reader.lua"
2014-03-13 13:52:43 +00:00
else
if lfs.attributes("./history", "mode") ~= "directory" then
2014-03-13 13:52:43 +00:00
lfs.mkdir("history")
end
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
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 "")
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
if ok and stored then
2014-03-13 13:52:43 +00:00
new.data = stored
end
return setmetatable(new, { __index = DocSettings})
end
function DocSettings:readSetting(key)
2014-03-13 13:52:43 +00:00
return self.data[key]
end
function DocSettings:saveSetting(key, value)
2014-03-13 13:52:43 +00:00
self.data[key] = value
end
function DocSettings:delSetting(key)
2014-03-13 13:52:43 +00:00
self.data[key] = nil
end
function DocSettings:flush()
-- 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
local serials = {}
if self.history_file then
pcall(table.insert, serials, io.open(self.history_file, "w"))
end
if self.sidecar_file then
pcall(table.insert, serials, io.open(self.sidecar_file, "w"))
end
os.setlocale('C', 'numeric')
local s_out = dump(self.data)
for _, f_out in ipairs(serials) do
if f_out ~= nil then
f_out:write("-- we can read Lua syntax here!\nreturn ")
f_out:write(s_out)
f_out:write("\n")
f_out:close()
end
2014-03-13 13:52:43 +00:00
end
end
function DocSettings:close()
2014-03-13 13:52:43 +00:00
self:flush()
end
2013-10-18 20:38:07 +00:00
function DocSettings:clear()
if self.history_file then
os.remove(self.history_file)
end
if self.sidecar_file then
os.remove(self.sidecar_file)
end
end
2013-10-18 20:38:07 +00:00
return DocSettings