2022-01-02 10:58:01 +00:00
|
|
|
local Device = require("device")
|
|
|
|
|
|
|
|
if not Device:isTouchDevice() then
|
|
|
|
return { disabled = true }
|
|
|
|
end
|
|
|
|
|
2020-01-04 00:18:51 +00:00
|
|
|
local BD = require("ui/bidi")
|
2019-10-08 15:14:38 +00:00
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
local FFIUtil = require("ffi/util")
|
|
|
|
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 filemanagerutil = require("apps/filemanager/filemanagerutil")
|
2022-09-27 23:10:50 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2019-10-08 15:14:38 +00:00
|
|
|
local util = require("util")
|
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local DocSettingTweak = WidgetContainer:extend{
|
2020-12-04 15:04:54 +00:00
|
|
|
name = "docsettingtweak",
|
2019-10-08 15:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettingTweak:editDirectoryDefaults()
|
|
|
|
local directory_defaults_file = io.open(directory_defaults_path, "rb")
|
2022-09-27 23:10:50 +00:00
|
|
|
if not directory_defaults_file then
|
|
|
|
return
|
|
|
|
end
|
2019-10-08 15:14:38 +00:00
|
|
|
local defaults = directory_defaults_file:read("*all")
|
|
|
|
directory_defaults_file:close()
|
|
|
|
local config_editor
|
|
|
|
config_editor = InputDialog:new{
|
2020-01-04 00:18:51 +00:00
|
|
|
title = T(_("Directory Defaults: %1"), BD.filepath(directory_defaults_path)),
|
2019-10-08 15:14:38 +00:00
|
|
|
input = defaults,
|
|
|
|
input_type = "string",
|
2019-12-06 21:55:35 +00:00
|
|
|
para_direction_rtl = false, -- force LTR
|
2019-10-08 15:14:38 +00:00
|
|
|
fullscreen = true,
|
|
|
|
condensed = true,
|
|
|
|
allow_newline = true,
|
|
|
|
cursor_at_end = false,
|
|
|
|
add_nav_bar = true,
|
|
|
|
reset_callback = function()
|
|
|
|
return defaults
|
|
|
|
end,
|
|
|
|
save_callback = function(content)
|
|
|
|
if content and #content > 0 then
|
|
|
|
local parse_error = util.checkLuaSyntax(content)
|
|
|
|
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")
|
2022-09-27 23:10:50 +00:00
|
|
|
if not directory_defaults_file then
|
|
|
|
return false, _("Missing defaults file")
|
|
|
|
end
|
2019-10-08 15:14:38 +00:00
|
|
|
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
|
|
|
|
end
|
|
|
|
return false, _("Defaults empty")
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
UIManager:show(config_editor)
|
|
|
|
config_editor:onShowKeyboard()
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocSettingTweak:onDocSettingsLoad(doc_settings, document)
|
|
|
|
-- check that the documents settings are empty & and that we have defaults to customize
|
|
|
|
if next(doc_settings.data) == 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
|
|
|
|
doc_settings.data = util.tableDeepCopy(directory_defaults:readSetting(directory))
|
|
|
|
break
|
|
|
|
else
|
2019-10-29 23:27:54 +00:00
|
|
|
if directory == "/" or directory == "." then
|
|
|
|
-- have reached the filesystem root, abort
|
|
|
|
break
|
|
|
|
else
|
|
|
|
directory = FFIUtil.dirname(directory)
|
|
|
|
end
|
2019-10-08 15:14:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return DocSettingTweak
|