2014-08-14 11:49:42 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2015-06-15 08:46:43 +00:00
|
|
|
local DataStorage = require("datastorage")
|
2015-10-03 06:18:47 +00:00
|
|
|
local dump = require("dump")
|
2016-03-28 00:17:46 +00:00
|
|
|
local purgeDir = require("ffi/util").purgeDir
|
2015-10-03 06:18:47 +00:00
|
|
|
|
|
|
|
local DocSettings = {}
|
|
|
|
|
2016-06-04 05:05:14 +00:00
|
|
|
local HISTORY_DIR = DataStorage:getHistoryDir()
|
2011-12-05 21:31:40 +00:00
|
|
|
|
2016-04-26 02:45:55 +00:00
|
|
|
local function buildCandidate(file_path)
|
|
|
|
if lfs.attributes(file_path, "mode") == "file" then
|
|
|
|
return { file_path, lfs.attributes(file_path, "modification") }
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sidecar directory is the file without _last_ suffix.
|
2016-01-06 05:28:26 +00:00
|
|
|
function DocSettings:getSidecarDir(doc_path)
|
2017-01-21 09:32:42 +00:00
|
|
|
if doc_path == nil or doc_path == '' then return '' end
|
2016-12-03 13:19:35 +00:00
|
|
|
local file_without_suffix = doc_path:match("(.*)%.")
|
|
|
|
if file_without_suffix then
|
|
|
|
return file_without_suffix..".sdr"
|
|
|
|
end
|
|
|
|
return doc_path..".sdr"
|
|
|
|
end
|
|
|
|
|
2017-01-21 09:32:42 +00:00
|
|
|
function DocSettings:getSidecarFile(doc_path)
|
|
|
|
if doc_path == nil or doc_path == '' then return '' end
|
2017-02-08 18:46:55 +00:00
|
|
|
-- If the file does not have a suffix or we are working on a directory, we
|
|
|
|
-- should ignore the suffix part in metadata file path.
|
|
|
|
local suffix = doc_path:match(".*%.(.+)")
|
|
|
|
if suffix == nil then
|
|
|
|
suffix = ''
|
|
|
|
end
|
|
|
|
return self:getSidecarDir(doc_path) .. "/metadata." .. suffix .. ".lua"
|
2017-01-21 09:32:42 +00:00
|
|
|
end
|
|
|
|
|
2017-02-08 18:37:36 +00:00
|
|
|
function DocSettings:hasSidecarFile(doc_path)
|
2017-02-08 18:46:55 +00:00
|
|
|
return lfs.attributes(self:getSidecarFile(doc_path), "mode") == "file"
|
2016-01-06 05:28:26 +00:00
|
|
|
end
|
|
|
|
|
2012-12-12 01:35:49 +00:00
|
|
|
function DocSettings:getHistoryPath(fullpath)
|
2016-06-04 05:05:14 +00:00
|
|
|
return HISTORY_DIR .. "/[" .. fullpath:gsub("(.*/)([^/]+)","%1] %2"):gsub("/","#") .. ".lua"
|
2012-12-12 01:35:49 +00:00
|
|
|
end
|
|
|
|
|
2013-03-12 04:51:00 +00:00
|
|
|
function DocSettings:getPathFromHistory(hist_name)
|
2017-01-21 09:32:42 +00:00
|
|
|
if hist_name == nil or hist_name == '' then return '' end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 1. select everything included in brackets
|
|
|
|
local s = string.match(hist_name,"%b[]")
|
2017-01-21 09:32:42 +00:00
|
|
|
if s == nil or s == '' then return '' end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- 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)
|
2017-01-21 09:32:42 +00:00
|
|
|
if hist_name == nil or hist_name == '' then return '' end
|
2016-09-13 03:02:48 +00:00
|
|
|
local s = string.match(hist_name, "%b[]")
|
2017-01-21 09:32:42 +00:00
|
|
|
if s == nil or s == '' then return '' end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- at first, search for path length
|
|
|
|
-- and return the rest of string without 4 last characters (".lua")
|
2016-09-13 03:02:48 +00:00
|
|
|
return string.sub(hist_name, string.len(s)+2, -5)
|
2013-03-12 04:51:00 +00:00
|
|
|
end
|
|
|
|
|
2017-01-21 09:32:42 +00:00
|
|
|
function DocSettings:ensureSidecar(sidecar)
|
|
|
|
if lfs.attributes(sidecar, "mode") ~= "directory" then
|
|
|
|
lfs.mkdir(sidecar)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-05 21:31:40 +00:00
|
|
|
function DocSettings:open(docfile)
|
2016-04-18 22:38:14 +00:00
|
|
|
-- TODO(zijiehe): Remove history_path, use only sidecar.
|
2016-06-26 00:53:08 +00:00
|
|
|
local new = {}
|
2016-04-18 22:38:14 +00:00
|
|
|
local ok, stored
|
2016-06-26 00:53:08 +00:00
|
|
|
new.history_file = self:getHistoryPath(docfile)
|
2014-07-03 12:42:48 +00:00
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
local sidecar = self:getSidecarDir(docfile)
|
|
|
|
new.sidecar = sidecar
|
2017-01-21 09:32:42 +00:00
|
|
|
DocSettings:ensureSidecar(sidecar)
|
2016-06-26 00:53:08 +00:00
|
|
|
-- If there is a file which has a same name as the sidecar directory, or
|
|
|
|
-- the file system is read-only, we should not waste time to read it.
|
|
|
|
if lfs.attributes(sidecar, "mode") == "directory" then
|
|
|
|
-- New sidecar file name is metadata.{file last suffix}.lua. So we
|
|
|
|
-- can handle two files with only different suffixes.
|
2017-01-21 09:32:42 +00:00
|
|
|
new.sidecar_file = self:getSidecarFile(docfile)
|
2016-06-26 00:53:08 +00:00
|
|
|
new.legacy_sidecar_file = sidecar.."/"..
|
|
|
|
docfile:match("([^%/]+%..+)")..".lua"
|
|
|
|
end
|
2016-04-26 02:45:55 +00:00
|
|
|
|
2016-06-26 00:53:08 +00:00
|
|
|
local candidates = {}
|
|
|
|
-- New sidecar file
|
|
|
|
table.insert(candidates, buildCandidate(new.sidecar_file))
|
|
|
|
-- Legacy sidecar file
|
|
|
|
table.insert(candidates, buildCandidate(new.legacy_sidecar_file))
|
|
|
|
-- Legacy history folder
|
|
|
|
table.insert(candidates, buildCandidate(new.history_file))
|
|
|
|
-- Legacy kpdfview setting
|
|
|
|
table.insert(candidates, buildCandidate(docfile..".kpdfview.lua"))
|
|
|
|
table.sort(candidates, function(l, r)
|
|
|
|
if l == nil then
|
|
|
|
return false
|
|
|
|
elseif r == nil then
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return l[2] > r[2]
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
for _, k in pairs(candidates) do
|
|
|
|
ok, stored = pcall(dofile, k[1])
|
|
|
|
if ok then
|
|
|
|
break
|
2014-07-03 12:42:48 +00:00
|
|
|
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
|
2016-06-26 00:53:08 +00:00
|
|
|
new.candidates = candidates
|
|
|
|
else
|
|
|
|
new.data = {}
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2016-04-18 22:38:14 +00:00
|
|
|
|
2016-06-04 05:05:14 +00:00
|
|
|
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
|
|
|
|
|
2012-03-04 16:29:19 +00:00
|
|
|
function DocSettings:flush()
|
2016-04-26 02:45:55 +00:00
|
|
|
-- write serialized version of the data table into one of
|
|
|
|
-- i) sidecar directory in the same directory of the document or
|
|
|
|
-- ii) history directory in root directory of KOReader
|
2014-07-03 12:42:48 +00:00
|
|
|
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
|
|
|
|
2016-04-26 02:45:55 +00:00
|
|
|
-- If we can write to sidecar_file, we do not need to write to history_file
|
|
|
|
-- anymore.
|
|
|
|
local serials = { self.sidecar_file, self.history_file }
|
2017-01-21 09:32:42 +00:00
|
|
|
self:ensureSidecar(self.sidecar)
|
2014-11-14 15:33:52 +00:00
|
|
|
local s_out = dump(self.data)
|
2016-04-26 02:45:55 +00:00
|
|
|
os.setlocale('C', 'numeric')
|
|
|
|
for _, f in pairs(serials) do
|
|
|
|
local f_out = io.open(f, "w")
|
2014-07-03 12:42:48 +00:00
|
|
|
if f_out ~= nil then
|
2014-11-14 15:33:52 +00:00
|
|
|
f_out:write("-- we can read Lua syntax here!\nreturn ")
|
2014-07-03 12:42:48 +00:00
|
|
|
f_out:write(s_out)
|
2014-11-14 15:33:52 +00:00
|
|
|
f_out:write("\n")
|
2014-07-03 12:42:48 +00:00
|
|
|
f_out:close()
|
2016-06-04 19:26:05 +00:00
|
|
|
|
|
|
|
if self.candidates ~= nil
|
|
|
|
and not G_reader_settings:readSetting(
|
|
|
|
"preserve_legacy_docsetting") then
|
|
|
|
for _, k in pairs(self.candidates) do
|
|
|
|
if k[1] ~= f then
|
|
|
|
os.remove(k[1])
|
|
|
|
-- We should not remove sidecar folder, as it may
|
|
|
|
-- contain Kindle history files.
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-26 02:45:55 +00:00
|
|
|
break
|
2014-07-03 12:42:48 +00:00
|
|
|
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
|
|
|
|
2016-06-04 05:05:14 +00:00
|
|
|
function DocSettings:purge()
|
2014-10-07 05:06:52 +00:00
|
|
|
if self.history_file then
|
|
|
|
os.remove(self.history_file)
|
|
|
|
end
|
2016-06-04 05:05:14 +00:00
|
|
|
if lfs.attributes(self.sidecar, "mode") == "directory" then
|
|
|
|
purgeDir(self.sidecar)
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
2017-01-21 09:32:42 +00:00
|
|
|
self.data = {}
|
2014-10-07 05:06:52 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return DocSettings
|