Plugin disable dialog (#15341)
This commit is contained in:
1 parent
5f6c1b02f8
commit
b2aa813980
2 files changed
+135
-147
No files matched your search
+135
-61
@@ -11,13 +11,15 @@ Plugins are controlled by the following settings.
|
||||
- plugins_disabled
|
||||
- extra_plugin_paths
|
||||
]]
|
||||
local ButtonDialog = require("ui/widget/buttondialog")
|
||||
local ConfirmBox = require("ui/widget/confirmbox")
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
local UIManager = require("ui/uimanager")
|
||||
local dbg = require("dbg")
|
||||
local lfs = require("libs/libkoreader-lfs")
|
||||
local logger = require("logger")
|
||||
local ffiUtil = require("ffi/util")
|
||||
local _ = require("gettext")
|
||||
local T = ffiUtil.template
|
||||
|
||||
local DEFAULT_PLUGIN_PATH = "plugins"
|
||||
|
||||
@@ -170,11 +172,7 @@ local PluginLoader = {
|
||||
}
|
||||
|
||||
function PluginLoader:_discover()
|
||||
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled")
|
||||
if type(plugins_disabled) ~= "table" then
|
||||
plugins_disabled = {}
|
||||
end
|
||||
|
||||
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled", {})
|
||||
local discovered = {}
|
||||
local lookup_path_list = { DEFAULT_PLUGIN_PATH }
|
||||
local extra_paths = G_reader_settings:readSetting("extra_plugin_paths")
|
||||
@@ -269,10 +267,8 @@ function PluginLoader:_load(t)
|
||||
end
|
||||
package.path = package_path
|
||||
package.cpath = package_cpath
|
||||
|
||||
end
|
||||
|
||||
|
||||
function PluginLoader:loadPlugins()
|
||||
if self.enabled_plugins then return self.enabled_plugins, self.disabled_plugins end
|
||||
|
||||
@@ -298,25 +294,22 @@ function PluginLoader:genPluginManagerSubItem()
|
||||
if not self.all_plugins then
|
||||
local enabled_plugins, disabled_plugins = self:loadPlugins()
|
||||
self.all_plugins = {}
|
||||
|
||||
for _, plugin in ipairs(enabled_plugins) do
|
||||
local element = getMenuTable(plugin)
|
||||
element.enable = true
|
||||
table.insert(self.all_plugins, element)
|
||||
end
|
||||
|
||||
for _, plugin in ipairs(disabled_plugins) do
|
||||
local element = getMenuTable(plugin)
|
||||
element.enable = false
|
||||
table.insert(self.all_plugins, element)
|
||||
end
|
||||
|
||||
table.sort(self.all_plugins, function(v1, v2) return v1.fullname < v2.fullname end)
|
||||
end
|
||||
|
||||
local builtin_plugin_items = {}
|
||||
local user_plugin_items = {}
|
||||
for __, plugin in ipairs(self.all_plugins) do
|
||||
for _, plugin in ipairs(self.all_plugins) do
|
||||
local item = {
|
||||
text = plugin.fullname,
|
||||
checked_func = function()
|
||||
@@ -337,51 +330,11 @@ function PluginLoader:genPluginManagerSubItem()
|
||||
UIManager:askForRestart()
|
||||
end
|
||||
end,
|
||||
hold_callback = function()
|
||||
local CheckButton = require("ui/widget/checkbutton")
|
||||
local ConfirmBox = require("ui/widget/confirmbox")
|
||||
local instance = self:getPluginInstance(plugin.name)
|
||||
local deletePluginSettingsFn = instance and instance.deletePluginSettings
|
||||
local delete_plugin_settings = false
|
||||
local delete_plugin_settings_checkbox
|
||||
local confirmbox = ConfirmBox:new{
|
||||
text = plugin.description .. "\n\n" .. T(_("Are you sure you want to delete the plugin '%1'?"), plugin.fullname),
|
||||
ok_text = _("Delete"),
|
||||
ok_callback = function()
|
||||
self:stopPluginInstanceByName(plugin.name)
|
||||
local success, err = ffiUtil.purgeDir(plugin.path)
|
||||
if success then
|
||||
if delete_plugin_settings then
|
||||
self:deletePluginSettingsByName(plugin.name)
|
||||
end
|
||||
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled") or {}
|
||||
if plugins_disabled[plugin.name] then
|
||||
plugins_disabled[plugin.name] = nil
|
||||
G_reader_settings:saveSetting("plugins_disabled", plugins_disabled)
|
||||
end
|
||||
self.all_plugins = nil
|
||||
UIManager:askForRestart()
|
||||
else
|
||||
local InfoMessage = require("ui/widget/infomessage")
|
||||
UIManager:show(InfoMessage:new{
|
||||
text = _("Failed to delete plugin:") .. "\n" .. err,
|
||||
})
|
||||
end
|
||||
end,
|
||||
}
|
||||
delete_plugin_settings_checkbox = CheckButton:new{
|
||||
parent = confirmbox,
|
||||
text = _("Also delete plugin settings"),
|
||||
enabled = type(deletePluginSettingsFn) == "function",
|
||||
callback = function()
|
||||
delete_plugin_settings = not delete_plugin_settings
|
||||
end,
|
||||
}
|
||||
confirmbox:addWidget(delete_plugin_settings_checkbox)
|
||||
UIManager:show(confirmbox)
|
||||
hold_callback = function(touchmenu_instance)
|
||||
self:showPluginDialog(plugin, touchmenu_instance)
|
||||
end,
|
||||
}
|
||||
if BUILTIN_PLUGINS[plugin.name] == true then
|
||||
if BUILTIN_PLUGINS[plugin.name] then
|
||||
table.insert(builtin_plugin_items, item)
|
||||
else
|
||||
table.insert(user_plugin_items, item)
|
||||
@@ -390,17 +343,131 @@ function PluginLoader:genPluginManagerSubItem()
|
||||
return {
|
||||
{
|
||||
text = _("Built-in plugins"),
|
||||
enabled_func = function() return #builtin_plugin_items > 0 end,
|
||||
enabled_func = function()
|
||||
return #builtin_plugin_items > 0
|
||||
end,
|
||||
sub_item_table = builtin_plugin_items,
|
||||
},
|
||||
{
|
||||
text = _("User plugins"),
|
||||
enabled_func = function() return #user_plugin_items > 0 end,
|
||||
enabled_func = function()
|
||||
return #user_plugin_items > 0
|
||||
end,
|
||||
sub_item_table = user_plugin_items,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
function PluginLoader:showPluginDialog(plugin, touchmenu_instance)
|
||||
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled")
|
||||
local function set_and_restart(enable, disabled)
|
||||
plugin.enable = enable
|
||||
plugins_disabled[plugin.name] = disabled
|
||||
touchmenu_instance:updateItems()
|
||||
if self.show_info then
|
||||
self.show_info = false
|
||||
UIManager:askForRestart()
|
||||
end
|
||||
end
|
||||
local plugin_instance = self:getPluginInstance(plugin.name)
|
||||
local can_delete_settings = (plugin_instance and (plugin_instance.deletePluginSettings
|
||||
or plugin_instance.settings_file or plugin_instance.settings_key)) and true or false
|
||||
local plugin_dialog, buttons
|
||||
if plugin.enable then
|
||||
buttons = {
|
||||
{{
|
||||
text = _("Disable plugin"),
|
||||
callback = function()
|
||||
UIManager:close(plugin_dialog)
|
||||
if plugin_instance then
|
||||
self:stopPluginInstance(plugin_instance)
|
||||
end
|
||||
set_and_restart(false, true)
|
||||
end,
|
||||
}},
|
||||
}
|
||||
if can_delete_settings then
|
||||
table.insert(buttons, {{
|
||||
text = _("Disable plugin and delete settings"),
|
||||
callback = function()
|
||||
UIManager:show(ConfirmBox:new{
|
||||
text = _("Delete plugin settings?"),
|
||||
ok_text = _("Delete"),
|
||||
ok_callback = function()
|
||||
UIManager:close(plugin_dialog)
|
||||
self:deletePluginSettings(plugin_instance)
|
||||
self:stopPluginInstance(plugin_instance)
|
||||
set_and_restart(false, true)
|
||||
end,
|
||||
})
|
||||
end,
|
||||
}})
|
||||
end
|
||||
else -- disabled
|
||||
buttons = {
|
||||
{{
|
||||
text = _("Enable plugin"),
|
||||
callback = function()
|
||||
UIManager:close(plugin_dialog)
|
||||
set_and_restart(true, false)
|
||||
end,
|
||||
}},
|
||||
}
|
||||
end
|
||||
if not BUILTIN_PLUGINS[plugin.name] then
|
||||
table.insert(buttons, {}) -- separator
|
||||
table.insert(buttons, {{
|
||||
text = _("Delete plugin"),
|
||||
callback = function()
|
||||
UIManager:show(ConfirmBox:new{
|
||||
text = _("Delete plugin?"),
|
||||
ok_text = _("Delete"),
|
||||
ok_callback = function()
|
||||
local ok, err = ffiUtil.purgeDir(plugin.path)
|
||||
if ok then
|
||||
UIManager:close(plugin_dialog)
|
||||
if plugin_instance then
|
||||
self:stopPluginInstance(plugin_instance)
|
||||
end
|
||||
set_and_restart(false, nil)
|
||||
else
|
||||
UIManager:show(InfoMessage:new{ text = _("Failed to delete plugin:") .. "\n" .. err })
|
||||
end
|
||||
end,
|
||||
})
|
||||
end,
|
||||
}})
|
||||
if can_delete_settings then
|
||||
table.insert(buttons, {{
|
||||
text = _("Delete plugin and settings"),
|
||||
callback = function()
|
||||
UIManager:show(ConfirmBox:new{
|
||||
text = _("Delete plugin and settings?"),
|
||||
ok_text = _("Delete"),
|
||||
ok_callback = function()
|
||||
local ok, err = ffiUtil.purgeDir(plugin.path)
|
||||
if ok then
|
||||
UIManager:close(plugin_dialog)
|
||||
self:deletePluginSettings(plugin_instance)
|
||||
self:stopPluginInstance(plugin_instance)
|
||||
set_and_restart(false, nil)
|
||||
else
|
||||
UIManager:show(InfoMessage:new{ text = _("Failed to delete plugin:") .. "\n" .. err })
|
||||
end
|
||||
end,
|
||||
})
|
||||
end,
|
||||
}})
|
||||
end
|
||||
end
|
||||
plugin_dialog = ButtonDialog:new{
|
||||
title = plugin.fullname .. "\n\n" .. plugin.description .. "\n",
|
||||
title_align = "center",
|
||||
buttons = buttons,
|
||||
}
|
||||
UIManager:show(plugin_dialog)
|
||||
end
|
||||
|
||||
function PluginLoader:createPluginInstance(plugin, attr)
|
||||
local ok, re = pcall(plugin.new, plugin, attr)
|
||||
if ok then -- re is a plugin instance
|
||||
@@ -456,13 +523,20 @@ end
|
||||
--- @param instance table The plugin instance whose settings should be deleted.
|
||||
--- @return boolean Success, string|nil
|
||||
function PluginLoader:deletePluginSettings(instance)
|
||||
local ok, err = false, "no deletePluginSettings method"
|
||||
local ok
|
||||
local fn = instance.deletePluginSettings
|
||||
if type(fn) == "function" then
|
||||
ok, err = pcall(fn, instance)
|
||||
ok = pcall(fn, instance)
|
||||
end
|
||||
if ok then return true, nil end
|
||||
return false, err
|
||||
if instance.settings_file then
|
||||
os.remove(instance.settings_file)
|
||||
ok = true
|
||||
end
|
||||
if instance.settings_key then
|
||||
G_reader_settings:delSetting(instance.settings_key)
|
||||
ok = true
|
||||
end
|
||||
return ok
|
||||
end
|
||||
|
||||
--- Calls the stopPlugin() method on a plugin instance if it's currently loaded.
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
describe("PluginLoader module", function()
|
||||
local PluginLoader, UIManager
|
||||
local ffiUtil
|
||||
local original_lfs
|
||||
|
||||
setup(function()
|
||||
require("commonrequire")
|
||||
UIManager = require("ui/uimanager")
|
||||
ffiUtil = require("ffi/util")
|
||||
original_lfs = require("libs/libkoreader-lfs")
|
||||
end)
|
||||
|
||||
@@ -163,90 +161,6 @@ describe("PluginLoader module", function()
|
||||
PluginLoader.loadPlugins:revert()
|
||||
end)
|
||||
|
||||
it("deletes plugin settings by internal plugin id", function()
|
||||
local shown_widget
|
||||
local instance = {
|
||||
deletePluginSettings = function() end,
|
||||
}
|
||||
|
||||
stub(PluginLoader, "loadPlugins", function()
|
||||
return {
|
||||
{
|
||||
name = "test",
|
||||
fullname = "Pretty Test",
|
||||
description = "from meta",
|
||||
path = "plugins/test.koplugin",
|
||||
},
|
||||
}, {}
|
||||
end)
|
||||
stub(PluginLoader, "getPluginInstance", function()
|
||||
return instance
|
||||
end)
|
||||
stub(PluginLoader, "stopPluginInstanceByName")
|
||||
stub(PluginLoader, "deletePluginSettingsByName")
|
||||
stub(ffiUtil, "purgeDir", function()
|
||||
return true
|
||||
end)
|
||||
stub(UIManager, "show", function(_, widget)
|
||||
shown_widget = widget
|
||||
end)
|
||||
stub(UIManager, "askForRestart")
|
||||
|
||||
G_reader_settings:saveSetting("plugins_disabled", {
|
||||
test = true,
|
||||
})
|
||||
|
||||
local plugin_items = PluginLoader:genPluginManagerSubItem()
|
||||
plugin_items[2].sub_item_table[1].hold_callback()
|
||||
shown_widget._added_widgets[1].checked = true
|
||||
shown_widget._added_widgets[1].callback()
|
||||
shown_widget.ok_callback()
|
||||
|
||||
assert.is_true(shown_widget._added_widgets[1].enabled)
|
||||
assert.stub(PluginLoader.stopPluginInstanceByName).was.called()
|
||||
assert.stub(PluginLoader.deletePluginSettingsByName).was.called()
|
||||
assert.is_nil(G_reader_settings:readSetting("plugins_disabled").test)
|
||||
|
||||
UIManager.askForRestart:revert()
|
||||
UIManager.show:revert()
|
||||
ffiUtil.purgeDir:revert()
|
||||
PluginLoader.deletePluginSettingsByName:revert()
|
||||
PluginLoader.stopPluginInstanceByName:revert()
|
||||
PluginLoader.getPluginInstance:revert()
|
||||
PluginLoader.loadPlugins:revert()
|
||||
end)
|
||||
|
||||
it("shows the delete settings checkbox disabled when the plugin does not provide that hook", function()
|
||||
local shown_widget
|
||||
|
||||
stub(PluginLoader, "loadPlugins", function()
|
||||
return {
|
||||
{
|
||||
name = "test",
|
||||
fullname = "Pretty Test",
|
||||
description = "from meta",
|
||||
path = "plugins/test.koplugin",
|
||||
},
|
||||
}, {}
|
||||
end)
|
||||
stub(PluginLoader, "getPluginInstance", function()
|
||||
return nil
|
||||
end)
|
||||
stub(UIManager, "show", function(_, widget)
|
||||
shown_widget = widget
|
||||
end)
|
||||
|
||||
local plugin_items = PluginLoader:genPluginManagerSubItem()
|
||||
plugin_items[2].sub_item_table[1].hold_callback()
|
||||
|
||||
assert.is_not_nil(shown_widget._added_widgets)
|
||||
assert.is_false(shown_widget._added_widgets[1].enabled)
|
||||
|
||||
UIManager.show:revert()
|
||||
PluginLoader.getPluginInstance:revert()
|
||||
PluginLoader.loadPlugins:revert()
|
||||
end)
|
||||
|
||||
it("calls deletePluginSettings on the loaded plugin instance by internal plugin id", function()
|
||||
local called_instance
|
||||
local instance = {
|
||||
|
||||
Reference in new issue
Block a user