mirror of https://github.com/koreader/koreader
refactor thirdparty app integration (#6513)
* refactor thirdparty app integration * Fix fora package namereviewable/pr6579/r1
parent
454ed0ebeb
commit
c7f77de72a
@ -1,20 +0,0 @@
|
||||
local user_path = require("datastorage"):getDataDir() .. "/dictionaries.lua"
|
||||
local ok, dicts = pcall(dofile, user_path)
|
||||
|
||||
if ok then
|
||||
return dicts
|
||||
else
|
||||
return {
|
||||
-- tested dictionary applications
|
||||
{ "Aard2", "Aard2", false, "itkach.aard2", "aard2" },
|
||||
{ "Alpus", "Alpus", false, "com.ngcomputing.fora.android", "search" },
|
||||
{ "ColorDict", "ColorDict", false, "com.socialnmobile.colordict", "colordict" },
|
||||
{ "Eudic", "Eudic", false, "com.eusoft.eudic", "send" },
|
||||
{ "Fora", "Fora Dict", false, "com.ngc.fora", "search" },
|
||||
{ "GoldenFree", "GoldenDict Free", false, "mobi.goldendict.android.free", "send" },
|
||||
{ "GoldenPro", "GoldenDict Pro", false, "mobi.goldendict.android", "send" },
|
||||
{ "Kiwix", "Kiwix", false, "org.kiwix.kiwixmobile", "text" },
|
||||
{ "Mdict", "Mdict", false, "cn.mdict", "send" },
|
||||
{ "QuickDic", "QuickDic", false, "de.reimardoeffinger.quickdic", "quickdic" },
|
||||
}
|
||||
end
|
@ -1,20 +0,0 @@
|
||||
local user_path = require("datastorage"):getDataDir() .. "/dictionaries.lua"
|
||||
local ok, dicts = pcall(dofile, user_path)
|
||||
|
||||
local t = {}
|
||||
|
||||
table.insert(t, 1, { "Goldendict", "Goldendict", false, "goldendict" })
|
||||
|
||||
if jit.os == "OSX" then
|
||||
table.insert(t, 1, { "Apple", "AppleDict", false, "dict://" })
|
||||
end
|
||||
|
||||
if ok then
|
||||
-- append user dictionaries to the bottom of the menu
|
||||
for k, v in pairs(dicts) do
|
||||
table.insert(t, #t + 1, v)
|
||||
end
|
||||
end
|
||||
|
||||
return t
|
||||
|
@ -0,0 +1,73 @@
|
||||
-- module for integration with thirdparty applications
|
||||
local logger = require("logger")
|
||||
|
||||
local roles = {
|
||||
"dict",
|
||||
"translator",
|
||||
}
|
||||
|
||||
local M = {}
|
||||
|
||||
function M:new(o)
|
||||
-- platform specific function to check availability of apps at runtime.
|
||||
if not o.check or type(o.check) ~= "function" then
|
||||
o.check = function(app) return false end
|
||||
end
|
||||
setmetatable(o, self)
|
||||
self.__index = self
|
||||
|
||||
-- one-time availability check
|
||||
for _, role in pairs(roles) do
|
||||
-- user override, if available
|
||||
local user_file = role == "dict" and "dictionaries.lua" or role .. "s.lua"
|
||||
local user = require("datastorage"):getDataDir() .. "/" .. user_file
|
||||
local ok, user_dicts = pcall(dofile, user)
|
||||
if ok then
|
||||
o[role.."s"] = user_dicts
|
||||
o.is_user_list = true
|
||||
else
|
||||
local t = o[role.."s"]
|
||||
for i, value in ipairs(t or {}) do
|
||||
local app = value[4]
|
||||
if o:check(app) then
|
||||
value[3] = true
|
||||
end
|
||||
end
|
||||
if t then
|
||||
self[role.."s"] = t
|
||||
end
|
||||
end
|
||||
end
|
||||
logger.info(o:dump())
|
||||
return o
|
||||
end
|
||||
|
||||
function M:checkMethod(role, method)
|
||||
local tool, action = nil
|
||||
for i, v in ipairs(self[role.."s"] or {}) do
|
||||
if v[1] == method then
|
||||
tool = v[4]
|
||||
action = v[5]
|
||||
break
|
||||
end
|
||||
end
|
||||
if not tool and not action then
|
||||
return false
|
||||
else
|
||||
return true, tool, action
|
||||
end
|
||||
end
|
||||
|
||||
function M:dump()
|
||||
local str = (self.is_user_list and "user" or "platform") .. " thirdparty apps\n"
|
||||
for i, role in ipairs(roles) do
|
||||
local apps = self[role.."s"]
|
||||
for index, _ in ipairs(apps or {}) do
|
||||
str = str .. string.format("-> %s (%s), role: %s, available: %s\n",
|
||||
apps[index][1], apps[index][4], role, tostring(apps[index][3]))
|
||||
end
|
||||
end
|
||||
return str
|
||||
end
|
||||
|
||||
return M
|
Loading…
Reference in New Issue