2020-06-19 21:38:53 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
|
|
|
local DataStorage = require("datastorage")
|
|
|
|
local Dispatcher = require("dispatcher")
|
2020-09-15 18:39:32 +00:00
|
|
|
local FFIUtil = require("ffi/util")
|
2020-06-19 21:38:53 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
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")
|
2020-09-15 18:39:32 +00:00
|
|
|
local T = FFIUtil.template
|
2022-09-17 22:11:03 +00:00
|
|
|
local util = require("util")
|
2020-06-19 21:38:53 +00:00
|
|
|
|
2021-08-15 11:25:19 +00:00
|
|
|
local autostart_done = false
|
|
|
|
|
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 Profiles = WidgetContainer:extend{
|
2020-06-19 21:38:53 +00:00
|
|
|
name = "profiles",
|
|
|
|
profiles_file = DataStorage:getSettingsDir() .. "/profiles.lua",
|
|
|
|
profiles = nil,
|
|
|
|
data = nil,
|
2020-08-04 22:19:28 +00:00
|
|
|
updated = false,
|
2020-06-19 21:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Profiles:init()
|
2020-09-22 00:05:02 +00:00
|
|
|
Dispatcher:init()
|
2020-06-19 21:38:53 +00:00
|
|
|
self.ui.menu:registerToMainMenu(self)
|
2022-09-17 22:11:03 +00:00
|
|
|
self:onDispatcherRegisterActions()
|
2021-08-15 11:25:19 +00:00
|
|
|
self:executeAutostart()
|
2020-06-19 21:38:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Profiles:loadProfiles()
|
|
|
|
if self.profiles then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.profiles = LuaSettings:open(self.profiles_file)
|
|
|
|
self.data = self.profiles.data
|
2022-10-25 10:29:22 +00:00
|
|
|
-- ensure profile name
|
|
|
|
for k, v in pairs(self.data) do
|
|
|
|
if not v.settings then
|
|
|
|
self.data[k].settings = {}
|
|
|
|
end
|
|
|
|
if not self.data[k].settings.name then
|
|
|
|
self.data[k].settings.name = k
|
|
|
|
self.updated = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self:onFlushSettings()
|
2020-06-19 21:38:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Profiles:onFlushSettings()
|
2020-08-04 22:19:28 +00:00
|
|
|
if self.profiles and self.updated then
|
2020-06-19 21:38:53 +00:00
|
|
|
self.profiles:flush()
|
2020-08-04 22:19:28 +00:00
|
|
|
self.updated = false
|
2020-06-19 21:38:53 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-09-17 22:11:03 +00:00
|
|
|
local function dispatcherRegisterProfile(name)
|
|
|
|
Dispatcher:registerAction("profile_exec_"..name,
|
2022-09-19 02:22:41 +00:00
|
|
|
{category="none", event="ProfileExecute", arg=name, title=T(_("Profile %1"), name), general=true})
|
2022-09-17 22:11:03 +00:00
|
|
|
end
|
|
|
|
|
2022-11-22 12:46:44 +00:00
|
|
|
local function dispatcherUnregisterProfile(name)
|
2022-09-17 22:11:03 +00:00
|
|
|
Dispatcher:removeAction("profile_exec_"..name)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Profiles:onDispatcherRegisterActions()
|
|
|
|
self:loadProfiles()
|
2022-10-25 10:29:22 +00:00
|
|
|
for k, v in pairs(self.data) do
|
|
|
|
if v.settings.registered then
|
|
|
|
dispatcherRegisterProfile(k)
|
|
|
|
end
|
2022-09-17 22:11:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-19 21:38:53 +00:00
|
|
|
function Profiles:addToMainMenu(menu_items)
|
|
|
|
menu_items.profiles = {
|
|
|
|
text = _("Profiles"),
|
|
|
|
sub_item_table_func = function()
|
|
|
|
return self:getSubMenuItems()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function Profiles:getSubMenuItems()
|
|
|
|
self:loadProfiles()
|
|
|
|
local sub_item_table = {
|
|
|
|
{
|
|
|
|
text = _("New"),
|
|
|
|
keep_menu_open = true,
|
|
|
|
callback = function(touchmenu_instance)
|
2022-09-17 22:11:03 +00:00
|
|
|
local function editCallback(new_name)
|
2022-10-25 10:29:22 +00:00
|
|
|
self.data[new_name] = {["settings"] = {["name"] = new_name}}
|
2022-09-17 22:11:03 +00:00
|
|
|
self.updated = true
|
|
|
|
touchmenu_instance.item_table = self:getSubMenuItems()
|
|
|
|
touchmenu_instance.page = 1
|
|
|
|
touchmenu_instance:updateItems()
|
|
|
|
end
|
|
|
|
self:editProfileName(editCallback)
|
2020-06-19 21:38:53 +00:00
|
|
|
end,
|
|
|
|
separator = true,
|
2022-09-17 22:11:03 +00:00
|
|
|
},
|
2020-06-19 21:38:53 +00:00
|
|
|
}
|
2022-09-17 22:11:03 +00:00
|
|
|
for k, v in FFIUtil.orderedPairs(self.data) do
|
|
|
|
local edit_actions_sub_items = {}
|
|
|
|
Dispatcher:addSubMenu(self, edit_actions_sub_items, self.data, k)
|
2020-06-19 21:38:53 +00:00
|
|
|
local sub_items = {
|
|
|
|
{
|
2022-09-17 22:11:03 +00:00
|
|
|
text = _("Execute"),
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
touchmenu_instance:onClose()
|
2022-10-25 10:29:22 +00:00
|
|
|
local show_as_quickmenu = v.settings.show_as_quickmenu
|
|
|
|
self.data[k].settings.show_as_quickmenu = nil
|
|
|
|
self:onProfileExecute(k)
|
|
|
|
self.data[k].settings.show_as_quickmenu = show_as_quickmenu
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Show as QuickMenu"),
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
touchmenu_instance:onClose()
|
|
|
|
local show_as_quickmenu = v.settings.show_as_quickmenu
|
|
|
|
self.data[k].settings.show_as_quickmenu = true
|
2022-09-17 22:11:03 +00:00
|
|
|
self:onProfileExecute(k)
|
2022-10-25 10:29:22 +00:00
|
|
|
self.data[k].settings.show_as_quickmenu = show_as_quickmenu
|
2022-09-17 22:11:03 +00:00
|
|
|
end,
|
|
|
|
},
|
2021-08-15 11:25:19 +00:00
|
|
|
{
|
|
|
|
text = _("Autostart"),
|
|
|
|
help_text = _("Execute this profile when KOReader is started with 'file browser' or 'last file'."),
|
|
|
|
checked_func = function()
|
2022-09-17 22:11:03 +00:00
|
|
|
return G_reader_settings:getSettingForExt("autostart_profiles", k)
|
2021-08-15 11:25:19 +00:00
|
|
|
end,
|
|
|
|
callback = function()
|
2022-09-17 22:11:03 +00:00
|
|
|
local new_value = not G_reader_settings:getSettingForExt("autostart_profiles", k) or nil
|
|
|
|
G_reader_settings:saveSettingForExt("autostart_profiles", new_value, k)
|
|
|
|
end,
|
|
|
|
separator = true,
|
|
|
|
},
|
2022-10-25 10:29:22 +00:00
|
|
|
{
|
|
|
|
text = _("Show in action list"),
|
|
|
|
checked_func = function()
|
|
|
|
return v.settings.registered
|
|
|
|
end,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
if v.settings.registered then
|
2022-11-22 12:46:44 +00:00
|
|
|
dispatcherUnregisterProfile(k)
|
2022-10-25 10:29:22 +00:00
|
|
|
self.data[k].settings.registered = nil
|
|
|
|
else
|
|
|
|
dispatcherRegisterProfile(k)
|
|
|
|
self.data[k].settings.registered = true
|
|
|
|
end
|
|
|
|
self.updated = true
|
|
|
|
local actions_sub_menu = {}
|
|
|
|
Dispatcher:addSubMenu(self, actions_sub_menu, self.data, k)
|
|
|
|
touchmenu_instance.item_table[4].sub_item_table = actions_sub_menu -- item index in submenu
|
|
|
|
end,
|
|
|
|
},
|
2022-09-17 22:11:03 +00:00
|
|
|
{
|
2022-09-19 02:22:41 +00:00
|
|
|
text_func = function() return T(_("Edit actions: (%1)"), Dispatcher:menuTextFunc(v)) end,
|
2022-09-17 22:11:03 +00:00
|
|
|
sub_item_table = edit_actions_sub_items,
|
2022-10-25 10:29:22 +00:00
|
|
|
separator = true,
|
2022-09-17 22:11:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
text = T(_("Rename: %1"), k),
|
|
|
|
keep_menu_open = true,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
local function editCallback(new_name)
|
2022-10-25 10:29:22 +00:00
|
|
|
if v.settings.registered then
|
2022-11-22 12:46:44 +00:00
|
|
|
dispatcherUnregisterProfile(k)
|
2022-10-25 10:29:22 +00:00
|
|
|
dispatcherRegisterProfile(new_name)
|
|
|
|
end
|
|
|
|
self:renameAutostart(k, new_name)
|
2022-09-17 22:11:03 +00:00
|
|
|
self.data[new_name] = util.tableDeepCopy(v)
|
2022-09-19 02:22:41 +00:00
|
|
|
self.data[new_name].settings.name = new_name
|
2022-09-17 22:11:03 +00:00
|
|
|
self.data[k] = nil
|
|
|
|
self.updated = true
|
|
|
|
touchmenu_instance.item_table = self:getSubMenuItems()
|
|
|
|
touchmenu_instance:updateItems()
|
|
|
|
end
|
|
|
|
self:editProfileName(editCallback, k)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
2022-09-19 02:22:41 +00:00
|
|
|
text = _("Duplicate"),
|
2022-09-17 22:11:03 +00:00
|
|
|
keep_menu_open = true,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
local function editCallback(new_name)
|
|
|
|
self.data[new_name] = util.tableDeepCopy(v)
|
2022-09-19 02:22:41 +00:00
|
|
|
self.data[new_name].settings.name = new_name
|
2022-10-25 10:29:22 +00:00
|
|
|
self.data[new_name].settings.registered = nil
|
2022-09-17 22:11:03 +00:00
|
|
|
self.updated = true
|
|
|
|
touchmenu_instance.item_table = self:getSubMenuItems()
|
|
|
|
touchmenu_instance:updateItems()
|
|
|
|
end
|
|
|
|
self:editProfileName(editCallback, k)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Delete"),
|
|
|
|
keep_menu_open = true,
|
|
|
|
separator = true,
|
|
|
|
callback = function(touchmenu_instance)
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = _("Do you want to delete this profile?"),
|
|
|
|
ok_text = _("Delete"),
|
|
|
|
ok_callback = function()
|
2022-10-25 10:29:22 +00:00
|
|
|
if v.settings.registered then
|
2022-11-22 12:46:44 +00:00
|
|
|
dispatcherUnregisterProfile(k)
|
2022-10-25 10:29:22 +00:00
|
|
|
end
|
|
|
|
self:renameAutostart(k)
|
2022-09-17 22:11:03 +00:00
|
|
|
self.data[k] = nil
|
|
|
|
self.updated = true
|
|
|
|
touchmenu_instance.item_table = self:getSubMenuItems()
|
|
|
|
touchmenu_instance:updateItems()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end,
|
2021-08-15 11:25:19 +00:00
|
|
|
},
|
2020-06-19 21:38:53 +00:00
|
|
|
}
|
|
|
|
table.insert(sub_item_table, {
|
2022-10-25 10:29:22 +00:00
|
|
|
text_func = function()
|
|
|
|
return (v.settings.show_as_quickmenu and "\u{F0CA} " or "\u{F144} ") .. k
|
|
|
|
end,
|
2020-06-19 21:38:53 +00:00
|
|
|
hold_keep_menu_open = false,
|
|
|
|
sub_item_table = sub_items,
|
|
|
|
hold_callback = function()
|
2022-09-19 02:22:41 +00:00
|
|
|
self:onProfileExecute(k)
|
2020-06-19 21:38:53 +00:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return sub_item_table
|
|
|
|
end
|
|
|
|
|
2022-09-17 22:11:03 +00:00
|
|
|
function Profiles:onProfileExecute(name)
|
2022-09-19 02:22:41 +00:00
|
|
|
Dispatcher:execute(self.data[name])
|
2021-08-15 11:25:19 +00:00
|
|
|
end
|
|
|
|
|
2022-09-17 22:11:03 +00:00
|
|
|
function Profiles:editProfileName(editCallback, old_name)
|
|
|
|
local name_input
|
|
|
|
name_input = InputDialog:new{
|
|
|
|
title = _("Enter profile name"),
|
|
|
|
input = old_name,
|
|
|
|
buttons = {{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(name_input)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Save"),
|
|
|
|
callback = function()
|
|
|
|
local new_name = name_input:getInputText()
|
|
|
|
if new_name == "" or new_name == old_name then return end
|
|
|
|
UIManager:close(name_input)
|
|
|
|
if self.data[new_name] then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Profile already exists: %1"), new_name),
|
|
|
|
})
|
|
|
|
else
|
|
|
|
editCallback(new_name)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
UIManager:show(name_input)
|
|
|
|
name_input:onShowKeyboard()
|
2021-08-15 11:25:19 +00:00
|
|
|
end
|
|
|
|
|
2022-09-17 22:11:03 +00:00
|
|
|
function Profiles:renameAutostart(old_name, new_name)
|
|
|
|
if G_reader_settings:getSettingForExt("autostart_profiles", old_name) then
|
|
|
|
G_reader_settings:saveSettingForExt("autostart_profiles", nil, old_name)
|
|
|
|
if new_name then
|
|
|
|
G_reader_settings:saveSettingForExt("autostart_profiles", true, new_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Profiles:executeAutostart()
|
|
|
|
if autostart_done then return end
|
|
|
|
self:loadProfiles()
|
|
|
|
local autostart_table = G_reader_settings:readSetting("autostart_profiles") or {}
|
|
|
|
for autostart_profile_name, profile_enabled in pairs(autostart_table) do
|
|
|
|
if self.data[autostart_profile_name] and profile_enabled then
|
|
|
|
UIManager:nextTick(function()
|
|
|
|
Dispatcher:execute(self.data[autostart_profile_name])
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
self:renameAutostart(autostart_profile_name) -- remove deleted profile from autostart_profile
|
|
|
|
end
|
|
|
|
end
|
|
|
|
autostart_done = true
|
2020-06-19 21:38:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return Profiles
|