Plugin manager (#4159)

Also adds descriptions to all plugins.
This commit is contained in:
Robert
2018-08-17 20:54:11 +02:00
committed by poire-z
parent cd37535056
commit 4428ecb422
28 changed files with 160 additions and 22 deletions

View File

@@ -2,8 +2,9 @@ local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local DEFAULT_PLUGIN_PATH = "plugins"
local OBSOLETE_PLUGINS = {
storagestat = true
}
local function sandboxPluginEventHandlers(plugin)
for key, value in pairs(plugin) do
@@ -22,12 +23,15 @@ local function sandboxPluginEventHandlers(plugin)
end
local PluginLoader = {}
local PluginLoader = {
show_info = true,
}
function PluginLoader:loadPlugins()
if self.plugins then return self.plugins end
if self.enabled_plugins then return self.enabled_plugins, self.disabled_plugins end
self.plugins = {}
self.enabled_plugins = {}
self.disabled_plugins = {}
local lookup_path_list = { DEFAULT_PLUGIN_PATH }
local extra_paths = G_reader_settings:readSetting("extra_plugin_paths")
if extra_paths then
@@ -54,16 +58,17 @@ function PluginLoader:loadPlugins()
if type(plugins_disabled) ~= "table" then
plugins_disabled = {}
end
--permanent remove storage stats plugin (#2926)
plugins_disabled["storagestat"] = true
--disable obsolete plugins
for element in pairs(OBSOLETE_PLUGINS) do
plugins_disabled[element] = true
end
for _,lookup_path in ipairs(lookup_path_list) do
logger.info('Loading plugins from directory:', lookup_path)
for entry in lfs.dir(lookup_path) do
local plugin_root = lookup_path.."/"..entry
local mode = lfs.attributes(plugin_root, "mode")
-- valid koreader plugin directory
if mode == "directory" and entry:find(".+%.koplugin$")
and not (plugins_disabled and plugins_disabled[entry:sub(1, -10)]) then
if mode == "directory" and entry:find(".+%.koplugin$") then
local mainfile = plugin_root.."/main.lua"
package.path = string.format("%s/?.lua;%s", plugin_root, package_path)
package.cpath = string.format("%s/lib/?.so;%s", plugin_root, package_cpath)
@@ -73,8 +78,12 @@ function PluginLoader:loadPlugins()
elseif type(plugin_module.disabled) ~= "boolean" or not plugin_module.disabled then
plugin_module.path = plugin_root
plugin_module.name = plugin_module.name or plugin_root:match("/(.-)%.koplugin")
sandboxPluginEventHandlers(plugin_module)
table.insert(self.plugins, plugin_module)
if (plugins_disabled and plugins_disabled[entry:sub(1, -10)]) then
table.insert(self.disabled_plugins, plugin_module)
else
sandboxPluginEventHandlers(plugin_module)
table.insert(self.enabled_plugins, plugin_module)
end
else
logger.info("Plugin ", mainfile, " has been disabled.")
end
@@ -85,14 +94,74 @@ function PluginLoader:loadPlugins()
end
-- set package path for all loaded plugins
for _,plugin in ipairs(self.plugins) do
for _,plugin in ipairs(self.enabled_plugins) do
package.path = string.format("%s;%s/?.lua", package.path, plugin.path)
package.cpath = string.format("%s;%s/lib/?.so", package.cpath, plugin.path)
end
table.sort(self.plugins, function(v1,v2) return v1.path < v2.path end)
table.sort(self.enabled_plugins, function(v1,v2) return v1.path < v2.path end)
return self.plugins
return self.enabled_plugins, self.disabled_plugins
end
function PluginLoader:genPluginManagerSubItem()
local enabled_plugins, disabled_plugins = {}, {}
if self.all_plugins == nil then
self.all_plugins = {}
enabled_plugins, disabled_plugins = self:loadPlugins()
end
for _, plugin in ipairs(enabled_plugins) do
local element = {}
element.fullname = plugin.fullname or plugin.name
element.name = plugin.name
element.description = plugin.description
element.enable = true
table.insert(self.all_plugins, element)
end
for _, plugin in ipairs(disabled_plugins) do
local element = {}
element.fullname = plugin.fullname or plugin.name
element.name = plugin.name
element.description = plugin.description
element.enable = false
if not OBSOLETE_PLUGINS[element.name] then
table.insert(self.all_plugins, element)
end
end
table.sort(self.all_plugins, function(v1, v2) return v1.fullname < v2.fullname end)
local plugin_table = {}
for __, plugin in ipairs(self.all_plugins) do
table.insert(plugin_table, {
text = plugin.fullname,
checked_func = function()
return plugin.enable
end,
callback = function()
local InfoMessage = require("ui/widget/infomessage")
local UIManager = require("ui/uimanager")
local _ = require("gettext")
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled") or {}
plugin.enable = not plugin.enable
if plugin.enable then
plugins_disabled[plugin.name] = nil
else
plugins_disabled[plugin.name] = true
end
G_reader_settings:saveSetting("plugins_disabled", plugins_disabled)
if self.show_info then
UIManager:show(InfoMessage:new{
text = _("This will take effect on next restart."),
})
self.show_info = false
end
end,
help_text = plugin.description,
})
end
return plugin_table
end
function PluginLoader:createPluginInstance(plugin, attr)