DocSettingsTweak main: open settings file when needed only (#15355)

This commit is contained in:
hius07 authored and GitHub committed 2026-05-10 08:26:30 +03:00
1 parent b2aa813980
commit fe3775b077
1 file changed
+24 -36
+24 -36
View File
@@ -1,62 +1,47 @@
local Device = require("device")
if Device:hasFewKeys() and not Device:isTouchDevice() then
return { disabled = true }
end
local BD = require("ui/bidi")
local DataStorage = require("datastorage")
local FFIUtil = require("ffi/util")
local Device = require("device")
local InputDialog = require("ui/widget/inputdialog")
local LuaSettings = require("luasettings")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext")
local T = FFIUtil.template
local ffiUtil = require("ffi/util")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local lfs = require("libs/libkoreader-lfs")
local util = require("util")
local _ = require("gettext")
local T = ffiUtil.template
local DocSettingTweak = WidgetContainer:extend{
name = "docsettingtweak",
settings_file = DataStorage:getSettingsDir() .. "/directory_defaults.lua",
}
local directory_defaults_name = "directory_defaults.lua"
local directory_defaults_path = FFIUtil.joinPath(DataStorage:getSettingsDir(), directory_defaults_name)
local directory_defaults = nil
local initialized = false
function DocSettingTweak:init()
if not initialized then
-- Make sure our settings file exists
if not lfs.attributes(directory_defaults_path, "mode") then
FFIUtil.copyFile(FFIUtil.joinPath(self.path, "directory_defaults_template.lua"),
directory_defaults_path)
end
initialized = true
if Device:isTouchDevice() or not Device:hasFewKeys() then
-- cannot exit from text editor on non-touch devices with few keys
self.ui.menu:registerToMainMenu(self)
end
DocSettingTweak:loadDefaults()
self.ui.menu:registerToMainMenu(self)
end
function DocSettingTweak:loadDefaults()
directory_defaults = LuaSettings:open(directory_defaults_path)
end
function DocSettingTweak:addToMainMenu(menu_items)
menu_items.doc_setting_tweak = {
text = _("Tweak document settings"),
callback = function() DocSettingTweak:editDirectoryDefaults() end,
callback = function()
DocSettingTweak:editDirectoryDefaults()
end,
}
end
function DocSettingTweak:editDirectoryDefaults()
local defaults = util.readFromFile(directory_defaults_path, "rb")
if not lfs.attributes(self.settings_file, "mode") then
ffiUtil.copyFile(ffiUtil.joinPath(self.path, "directory_defaults_template.lua"), self.settings_file)
end
local defaults = util.readFromFile(self.settings_file, "rb")
local config_editor
config_editor = InputDialog:new{
title = T(_("Directory Defaults: %1"), BD.filepath(directory_defaults_path)),
title = T(_("Directory Defaults: %1"), BD.filepath(self.settings_file)),
input = defaults,
input_type = "string",
para_direction_rtl = false, -- force LTR
fullscreen = true,
condensed = true,
@@ -72,7 +57,7 @@ function DocSettingTweak:editDirectoryDefaults()
if not parse_error then
local syntax_okay, syntax_error = pcall(loadstring(content))
if syntax_okay then
if not util.writeToFile(content, directory_defaults_path) then
if not util.writeToFile(content, self.settings_file) then
return false, _("Missing defaults file")
end
DocSettingTweak:loadDefaults()
@@ -91,10 +76,12 @@ end
function DocSettingTweak:onDocSettingsLoad(doc_settings, document)
-- check that the document has not been opened yet & and that we have defaults to customize
if document.is_new and directory_defaults.data ~= nil then
if document.is_new and lfs.attributes(self.settings_file, "mode") == "file" then
local directory_defaults = LuaSettings:open(self.settings_file)
if directory_defaults.data == nil then return true end
local base = G_reader_settings:readSetting("home_dir") or filemanagerutil.getDefaultDir()
local absolute_path = FFIUtil.realpath(document.file)
local directory = FFIUtil.dirname(absolute_path)
local absolute_path = ffiUtil.realpath(document.file)
local directory = ffiUtil.dirname(absolute_path)
-- check if folder matches our defaults to override
while directory:sub(1, #base) == base do
if directory_defaults:has(directory) then
@@ -108,11 +95,12 @@ function DocSettingTweak:onDocSettingsLoad(doc_settings, document)
-- have reached the filesystem root, abort
break
else
directory = FFIUtil.dirname(directory)
directory = ffiUtil.dirname(directory)
end
end
end
end
return true -- unique handler at the moment
end
return DocSettingTweak