2014-08-14 11:49:42 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2021-01-07 22:15:32 +00:00
|
|
|
local _ = require("gettext")
|
2014-04-23 13:57:48 +00:00
|
|
|
|
2017-04-06 09:12:35 +00:00
|
|
|
local DEFAULT_PLUGIN_PATH = "plugins"
|
2021-01-07 22:15:32 +00:00
|
|
|
|
|
|
|
-- plugin names that were removed and are no longer available.
|
2018-08-17 18:54:11 +00:00
|
|
|
local OBSOLETE_PLUGINS = {
|
2021-01-15 19:31:44 +00:00
|
|
|
calibrecompanion = true,
|
2021-08-30 07:11:23 +00:00
|
|
|
evernote = true,
|
2021-08-31 06:53:04 +00:00
|
|
|
goodreads = true,
|
2019-08-01 17:08:09 +00:00
|
|
|
kobolight = true,
|
2022-01-30 21:01:51 +00:00
|
|
|
send2ebook = true,
|
|
|
|
storagestat = true,
|
2021-07-22 07:01:57 +00:00
|
|
|
zsync = true,
|
2018-08-17 18:54:11 +00:00
|
|
|
}
|
2021-01-07 22:15:32 +00:00
|
|
|
-- Deprecated plugins are still available, but show a hint about deprecation.
|
|
|
|
local function getMenuTable(plugin)
|
|
|
|
local t = {}
|
|
|
|
t.name = plugin.name
|
|
|
|
t.fullname = string.format("%s%s", plugin.fullname or plugin.name,
|
|
|
|
plugin.deprecated and " (" .. _("outdated") .. ")" or "")
|
|
|
|
t.description = string.format("%s%s", plugin.description,
|
|
|
|
type(plugin.deprecated) == "string" and "\n\n" .. plugin.deprecated or "")
|
|
|
|
return t
|
|
|
|
end
|
2017-04-21 11:04:02 +00:00
|
|
|
|
|
|
|
local function sandboxPluginEventHandlers(plugin)
|
|
|
|
for key, value in pairs(plugin) do
|
|
|
|
if key:sub(1, 2) == "on" and type(value) == "function" then
|
|
|
|
plugin[key] = function(self, ...)
|
|
|
|
local ok, re = pcall(value, self, ...)
|
|
|
|
if ok then
|
|
|
|
return re
|
|
|
|
else
|
|
|
|
logger.err("failed to call event handler", key, re)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2018-08-17 18:54:11 +00:00
|
|
|
local PluginLoader = {
|
|
|
|
show_info = true,
|
2021-04-29 17:30:18 +00:00
|
|
|
enabled_plugins = nil,
|
|
|
|
disabled_plugins = nil,
|
|
|
|
loaded_plugins = nil,
|
|
|
|
all_plugins = nil,
|
2018-08-17 18:54:11 +00:00
|
|
|
}
|
2014-04-23 13:57:48 +00:00
|
|
|
|
|
|
|
function PluginLoader:loadPlugins()
|
2018-08-17 18:54:11 +00:00
|
|
|
if self.enabled_plugins then return self.enabled_plugins, self.disabled_plugins end
|
2014-04-23 13:57:48 +00:00
|
|
|
|
2018-08-17 18:54:11 +00:00
|
|
|
self.enabled_plugins = {}
|
|
|
|
self.disabled_plugins = {}
|
2021-04-29 17:30:18 +00:00
|
|
|
self.loaded_plugins = {}
|
2017-04-06 09:12:35 +00:00
|
|
|
local lookup_path_list = { DEFAULT_PLUGIN_PATH }
|
|
|
|
local extra_paths = G_reader_settings:readSetting("extra_plugin_paths")
|
|
|
|
if extra_paths then
|
|
|
|
if type(extra_paths) == "string" then
|
|
|
|
extra_paths = { extra_paths }
|
|
|
|
end
|
|
|
|
if type(extra_paths) == "table" then
|
|
|
|
for _,extra_path in ipairs(extra_paths) do
|
|
|
|
local extra_path_mode = lfs.attributes(extra_path, "mode")
|
|
|
|
if extra_path_mode == "directory" and extra_path ~= DEFAULT_PLUGIN_PATH then
|
|
|
|
table.insert(lookup_path_list, extra_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
logger.err("extra_plugin_paths config only accepts string or table value")
|
|
|
|
end
|
2020-11-06 16:46:26 +00:00
|
|
|
else
|
|
|
|
local data_dir = require("datastorage"):getDataDir()
|
|
|
|
if data_dir ~= "." then
|
2021-03-30 08:02:06 +00:00
|
|
|
local extra_path = data_dir .. "/plugins/"
|
|
|
|
G_reader_settings:saveSetting("extra_plugin_paths", { extra_path })
|
|
|
|
table.insert(lookup_path_list, extra_path)
|
2020-11-06 16:46:26 +00:00
|
|
|
end
|
2017-04-06 09:12:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- keep reference to old value so they can be restored later
|
|
|
|
local package_path = package.path
|
|
|
|
local package_cpath = package.cpath
|
|
|
|
|
2018-07-05 05:50:40 +00:00
|
|
|
local plugins_disabled = G_reader_settings:readSetting("plugins_disabled")
|
|
|
|
if type(plugins_disabled) ~= "table" then
|
2018-08-06 18:37:47 +00:00
|
|
|
plugins_disabled = {}
|
2018-07-05 05:50:40 +00:00
|
|
|
end
|
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
|
|
|
-- disable obsolete plugins
|
2018-08-17 18:54:11 +00:00
|
|
|
for element in pairs(OBSOLETE_PLUGINS) do
|
|
|
|
plugins_disabled[element] = true
|
|
|
|
end
|
2021-04-02 23:48:35 +00:00
|
|
|
for _, lookup_path in ipairs(lookup_path_list) do
|
|
|
|
logger.info("Loading plugins from directory:", lookup_path)
|
2017-04-06 09:12:35 +00:00
|
|
|
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
|
2018-08-17 18:54:11 +00:00
|
|
|
if mode == "directory" and entry:find(".+%.koplugin$") then
|
2017-04-06 09:12:35 +00:00
|
|
|
local mainfile = plugin_root.."/main.lua"
|
2018-08-18 18:41:37 +00:00
|
|
|
local metafile = plugin_root.."/_meta.lua"
|
|
|
|
if plugins_disabled and plugins_disabled[entry:sub(1, -10)] then
|
|
|
|
mainfile = metafile
|
|
|
|
end
|
2017-04-06 09:12:35 +00:00
|
|
|
package.path = string.format("%s/?.lua;%s", plugin_root, package_path)
|
|
|
|
package.cpath = string.format("%s/lib/?.so;%s", plugin_root, package_cpath)
|
|
|
|
local ok, plugin_module = pcall(dofile, mainfile)
|
|
|
|
if not ok or not plugin_module then
|
|
|
|
logger.warn("Error when loading", mainfile, plugin_module)
|
|
|
|
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")
|
2018-08-17 18:54:11 +00:00
|
|
|
if (plugins_disabled and plugins_disabled[entry:sub(1, -10)]) then
|
|
|
|
table.insert(self.disabled_plugins, plugin_module)
|
|
|
|
else
|
2018-08-18 18:41:37 +00:00
|
|
|
local ok_meta, plugin_metamodule = pcall(dofile, metafile)
|
|
|
|
if ok_meta and plugin_metamodule then
|
|
|
|
for k,v in pairs(plugin_metamodule) do plugin_module[k] = v end
|
|
|
|
end
|
2018-08-17 18:54:11 +00:00
|
|
|
sandboxPluginEventHandlers(plugin_module)
|
|
|
|
table.insert(self.enabled_plugins, plugin_module)
|
|
|
|
end
|
2017-04-06 09:12:35 +00:00
|
|
|
else
|
2022-09-27 23:10:50 +00:00
|
|
|
logger.dbg("Plugin", mainfile, "has been disabled.")
|
2017-04-06 09:12:35 +00:00
|
|
|
end
|
2016-12-04 23:42:22 +00:00
|
|
|
package.path = package_path
|
|
|
|
package.cpath = package_cpath
|
2014-04-23 13:57:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 09:12:35 +00:00
|
|
|
-- set package path for all loaded plugins
|
2021-04-02 23:48:35 +00:00
|
|
|
for _, plugin in ipairs(self.enabled_plugins) do
|
2017-04-06 09:12:35 +00:00
|
|
|
package.path = string.format("%s;%s/?.lua", package.path, plugin.path)
|
|
|
|
package.cpath = string.format("%s;%s/lib/?.so", package.cpath, plugin.path)
|
2014-04-23 13:57:48 +00:00
|
|
|
end
|
|
|
|
|
2018-08-17 18:54:11 +00:00
|
|
|
table.sort(self.enabled_plugins, function(v1,v2) return v1.path < v2.path end)
|
|
|
|
|
|
|
|
return self.enabled_plugins, self.disabled_plugins
|
|
|
|
end
|
|
|
|
|
|
|
|
function PluginLoader:genPluginManagerSubItem()
|
2021-04-29 17:30:18 +00:00
|
|
|
if not self.all_plugins then
|
|
|
|
local enabled_plugins, disabled_plugins = self:loadPlugins()
|
2018-08-17 18:54:11 +00:00
|
|
|
self.all_plugins = {}
|
|
|
|
|
2021-04-29 17:30:18 +00:00
|
|
|
for _, plugin in ipairs(enabled_plugins) do
|
|
|
|
local element = getMenuTable(plugin)
|
|
|
|
element.enable = true
|
2018-08-17 18:54:11 +00:00
|
|
|
table.insert(self.all_plugins, element)
|
|
|
|
end
|
2021-04-29 17:30:18 +00:00
|
|
|
|
|
|
|
for _, plugin in ipairs(disabled_plugins) do
|
|
|
|
local element = getMenuTable(plugin)
|
|
|
|
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)
|
2018-08-17 18:54:11 +00:00
|
|
|
end
|
2014-06-23 14:11:12 +00:00
|
|
|
|
2018-08-17 18:54:11 +00:00
|
|
|
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 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
|
|
|
|
self.show_info = false
|
2022-12-03 20:29:13 +00:00
|
|
|
UIManager:askForRestart()
|
2018-08-17 18:54:11 +00:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
help_text = plugin.description,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
return plugin_table
|
2014-04-23 13:57:48 +00:00
|
|
|
end
|
|
|
|
|
2017-04-21 06:37:04 +00:00
|
|
|
function PluginLoader:createPluginInstance(plugin, attr)
|
|
|
|
local ok, re = pcall(plugin.new, plugin, attr)
|
|
|
|
if ok then -- re is a plugin instance
|
2021-04-29 17:30:18 +00:00
|
|
|
self.loaded_plugins[plugin.name] = re
|
2017-04-21 06:37:04 +00:00
|
|
|
return ok, re
|
|
|
|
else -- re is the error message
|
2022-09-27 23:10:50 +00:00
|
|
|
logger.err("Failed to initialize", plugin.name, "plugin:", re)
|
2017-04-21 06:37:04 +00:00
|
|
|
return nil, re
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-29 17:30:18 +00:00
|
|
|
--- Checks if a specific plugin is instantiated
|
|
|
|
function PluginLoader:isPluginLoaded(name)
|
|
|
|
return self.loaded_plugins[name] ~= nil
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns the current instance of a specific Plugin (if any)
|
|
|
|
--- (NOTE: You can also usually access it via self.ui[plugin_name])
|
|
|
|
function PluginLoader:getPluginInstance(name)
|
|
|
|
return self.loaded_plugins[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
-- *MUST* be called on destruction of whatever called createPluginInstance!
|
|
|
|
function PluginLoader:finalize()
|
|
|
|
-- Unpin stale references
|
|
|
|
self.loaded_plugins = {}
|
|
|
|
end
|
|
|
|
|
2014-04-23 13:57:48 +00:00
|
|
|
return PluginLoader
|