Skip to content

Commit 3a4b5d3

Browse files
authored
desktop: add support for 3rd party dictionary apps (koreader#6167)
1 parent 3934570 commit 3a4b5d3

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

frontend/device/sdl/device.lua

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,34 @@ local function getLinkOpener()
4141
return enabled, tool
4242
end
4343

44+
-- differentiate between urls and commands
45+
local function isUrl(s)
46+
if type(s) == "string" and s:match("*?://") then
47+
return true
48+
end
49+
return false
50+
end
51+
52+
local EXTERNAL_DICTS_AVAILABILITY_CHECKED = false
53+
local EXTERNAL_DICTS = require("device/sdl/dictionaries")
54+
local external_dict_when_back_callback = nil
55+
56+
local function getExternalDicts()
57+
if not EXTERNAL_DICTS_AVAILABILITY_CHECKED then
58+
EXTERNAL_DICTS_AVAILABILITY_CHECKED = true
59+
for i, v in ipairs(EXTERNAL_DICTS) do
60+
local tool = v[4]
61+
if not tool then return end
62+
if isUrl(tool) and getLinkOpener()
63+
or os.execute("which "..tool) == 0 then
64+
v[3] = true
65+
end
66+
end
67+
end
68+
return EXTERNAL_DICTS
69+
end
70+
71+
4472
local Device = Generic:new{
4573
model = "SDL",
4674
isSDL = yes,
@@ -56,7 +84,28 @@ local Device = Generic:new{
5684
openLink = function(self, link)
5785
local enabled, tool = getLinkOpener()
5886
if not enabled or not tool or not link or type(link) ~= "string" then return end
59-
return os.execute('LD_LIBRARY_PATH="" '..tool.." '"..link.."'") == 0
87+
return os.execute('env -u LD_LIBRARY_PATH '..tool.." '"..link.."'") == 0
88+
end,
89+
canExternalDictLookup = yes,
90+
getExternalDictLookupList = getExternalDicts,
91+
doExternalDictLookup = function(self, text, method, callback)
92+
external_dict_when_back_callback = callback
93+
local tool, ok = nil
94+
for i, v in ipairs(getExternalDicts()) do
95+
if v[1] == method then
96+
tool = v[4]
97+
break
98+
end
99+
end
100+
if isUrl(tool) and getLinkOpener() then
101+
ok = self:openLink(tool..text)
102+
else
103+
ok = os.execute('env -u LD_LIBRARY_PATH '..tool.." "..text.." &") == 0
104+
end
105+
if ok and external_dict_when_back_callback then
106+
external_dict_when_back_callback()
107+
external_dict_when_back_callback = nil
108+
end
60109
end,
61110
}
62111

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
local user_path = require("datastorage"):getDataDir() .. "/dictionaries.lua"
2+
local ok, dicts = pcall(dofile, user_path)
3+
4+
local t = {}
5+
6+
table.insert(t, 1, { "Goldendict", "Goldendict", false, "goldendict" })
7+
8+
if jit.os == "OSX" then
9+
table.insert(t, 1, { "Apple", "AppleDict", false, "dict://" })
10+
end
11+
12+
if ok then
13+
-- append user dictionaries to the bottom of the menu
14+
for k, v in pairs(dicts) do
15+
table.insert(t, #t + 1, v)
16+
end
17+
end
18+
19+
return t
20+

0 commit comments

Comments
 (0)