mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
DocSettingsTweak: apply to books with status (#12606)
This commit is contained in:
@@ -1071,6 +1071,17 @@ function util.partialMD5(filepath)
|
||||
return update()
|
||||
end
|
||||
|
||||
function util.readFromFile(filepath, mode)
|
||||
if not filepath then return end
|
||||
local file, err = io.open(filepath, mode)
|
||||
if not file then
|
||||
return nil, err
|
||||
end
|
||||
local data = file:read("*all")
|
||||
file:close()
|
||||
return data
|
||||
end
|
||||
|
||||
function util.writeToFile(data, filepath, force_flush, lua_dofile_ready, directory_updated)
|
||||
if not filepath then return end
|
||||
if lua_dofile_ready then
|
||||
|
||||
@@ -51,12 +51,7 @@ function DocSettingTweak:addToMainMenu(menu_items)
|
||||
end
|
||||
|
||||
function DocSettingTweak:editDirectoryDefaults()
|
||||
local directory_defaults_file = io.open(directory_defaults_path, "rb")
|
||||
if not directory_defaults_file then
|
||||
return
|
||||
end
|
||||
local defaults = directory_defaults_file:read("*all")
|
||||
directory_defaults_file:close()
|
||||
local defaults = util.readFromFile(directory_defaults_path, "rb")
|
||||
local config_editor
|
||||
config_editor = InputDialog:new{
|
||||
title = T(_("Directory Defaults: %1"), BD.filepath(directory_defaults_path)),
|
||||
@@ -77,20 +72,15 @@ function DocSettingTweak:editDirectoryDefaults()
|
||||
if not parse_error then
|
||||
local syntax_okay, syntax_error = pcall(loadstring(content))
|
||||
if syntax_okay then
|
||||
directory_defaults_file = io.open(directory_defaults_path, "w")
|
||||
if not directory_defaults_file then
|
||||
if not util.writeToFile(content, directory_defaults_path) then
|
||||
return false, _("Missing defaults file")
|
||||
end
|
||||
directory_defaults_file:write(content)
|
||||
directory_defaults_file:close()
|
||||
DocSettingTweak:loadDefaults()
|
||||
return true, _("Defaults saved")
|
||||
else
|
||||
return false, T(_("Defaults invalid: %1"), syntax_error)
|
||||
end
|
||||
else
|
||||
return false, T(_("Defaults invalid: %1"), parse_error)
|
||||
end
|
||||
return false, T(_("Defaults invalid: %1"), syntax_error)
|
||||
end
|
||||
return false, T(_("Defaults invalid: %1"), parse_error)
|
||||
end
|
||||
return false, _("Defaults empty")
|
||||
end,
|
||||
@@ -100,18 +90,17 @@ function DocSettingTweak:editDirectoryDefaults()
|
||||
end
|
||||
|
||||
function DocSettingTweak:onDocSettingsLoad(doc_settings, document)
|
||||
-- check that the documents settings are empty & and that we have defaults to customize
|
||||
if util.tableSize(doc_settings.data) == 1 and directory_defaults.data ~= nil then
|
||||
-- check that the document has not been opened yet & and that we have defaults to customize
|
||||
if doc_settings.data.doc_props == nil and directory_defaults.data ~= nil then
|
||||
local base = G_reader_settings:readSetting("home_dir") or filemanagerutil.getDefaultDir()
|
||||
if document.file == nil or document.file == "" then
|
||||
return
|
||||
end
|
||||
local directory = FFIUtil.dirname(document.file)
|
||||
-- check if folder matches our defaults to override
|
||||
while directory:sub(1, #base) == base do
|
||||
if directory_defaults:has(directory) then
|
||||
local summary = doc_settings.data.summary -- keep status
|
||||
doc_settings.data = util.tableDeepCopy(directory_defaults:readSetting(directory))
|
||||
doc_settings.data.doc_path = document.file
|
||||
doc_settings.data.summary = doc_settings.data.summary or summary
|
||||
break
|
||||
else
|
||||
if directory == "/" or directory == "." then
|
||||
|
||||
Reference in New Issue
Block a user