2013-04-23 22:59:52 +00:00
|
|
|
require "ui/device"
|
2013-04-24 14:57:03 +00:00
|
|
|
require "ui/widget/dict"
|
2013-04-23 22:59:52 +00:00
|
|
|
|
|
|
|
ReaderDictionary = EventListener:new{}
|
|
|
|
|
|
|
|
function ReaderDictionary:init()
|
|
|
|
local dev_mod = Device:getModel()
|
|
|
|
if dev_mod == "KindlePaperWhite" or dev_mod == "KindleTouch" then
|
2013-04-24 14:57:03 +00:00
|
|
|
require("liblipclua")
|
2013-04-27 05:30:25 +00:00
|
|
|
DEBUG("init lipc handler com.github.koreader.dictionary")
|
2013-04-23 22:59:52 +00:00
|
|
|
self.lipc_handle = lipc.init("com.github.koreader.dictionary")
|
|
|
|
end
|
2013-04-30 10:45:12 +00:00
|
|
|
JSON = require("JSON")
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderDictionary:onLookupWord(word)
|
2013-04-30 10:45:12 +00:00
|
|
|
self:stardictLookup(word)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderDictionary:nativeDictLookup(word)
|
2013-04-23 22:59:52 +00:00
|
|
|
DEBUG("lookup word:", word)
|
2013-04-24 14:57:03 +00:00
|
|
|
--self:quickLookup()
|
2013-04-30 10:45:12 +00:00
|
|
|
if self.lipc_handle and word then
|
2013-04-23 22:59:52 +00:00
|
|
|
self.lipc_handle:set_string_property(
|
2013-04-27 05:30:25 +00:00
|
|
|
"com.github.koreader.kpvbooklet.dict", "lookup", word)
|
2013-04-24 14:57:03 +00:00
|
|
|
local results_str = self.lipc_handle:get_string_property(
|
2013-04-27 05:30:25 +00:00
|
|
|
"com.github.koreader.kpvbooklet.word", word)
|
2013-04-24 14:57:03 +00:00
|
|
|
if results_str then
|
2013-04-30 10:45:12 +00:00
|
|
|
--DEBUG("result str:", word, results_str)
|
|
|
|
local ok, results_tab = pcall(JSON.decode, JSON, results_str)
|
|
|
|
--DEBUG("lookup result table:", word, results_tab)
|
|
|
|
if results_tab and results_tab[1] then
|
|
|
|
self:showDict(results_tab[1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderDictionary:stardictLookup(word)
|
|
|
|
DEBUG("lookup word:", word)
|
|
|
|
if word then
|
2013-06-15 15:09:22 +00:00
|
|
|
-- strip punctuation characters around selected word
|
|
|
|
word = string.gsub(word, "^%p+", '')
|
|
|
|
word = string.gsub(word, "%p+$", '')
|
|
|
|
DEBUG("stripped word:", word)
|
2013-05-01 09:53:18 +00:00
|
|
|
-- escape quotes and other funny characters in word
|
|
|
|
local std_out = io.popen("./sdcv -nj "..("%q"):format(word), "r")
|
2013-04-30 10:45:12 +00:00
|
|
|
local results_str = std_out:read("*all")
|
|
|
|
if results_str then
|
|
|
|
--DEBUG("result str:", word, results_str)
|
2013-04-24 14:57:03 +00:00
|
|
|
local ok, results_tab = pcall(JSON.decode, JSON, results_str)
|
2013-04-30 10:45:12 +00:00
|
|
|
--DEBUG("lookup result table:", word, results_tab)
|
|
|
|
if results_tab and results_tab[1] then
|
|
|
|
self:showDict(results_tab[1])
|
2013-04-24 14:57:03 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-23 22:59:52 +00:00
|
|
|
end
|
|
|
|
end
|
2013-04-24 14:57:03 +00:00
|
|
|
|
2013-04-30 10:45:12 +00:00
|
|
|
function ReaderDictionary:showDict(result)
|
2013-04-24 14:57:03 +00:00
|
|
|
-- UIManager:show(DictQuickLookup:new{
|
|
|
|
-- dict = "Oxford Dictionary of English",
|
|
|
|
-- definition = "coordination n. [mass noun] 1 the organization of the different elements of a \
|
|
|
|
-- complex body or activity so as to enable them to work together effectively: an important managerial \
|
|
|
|
-- task is the control and coordination of activities. cooperative effort resulting in an effective \
|
|
|
|
-- relationship: action groups work in coordination with local groups to end rainforest destruction. \
|
|
|
|
-- the ability to use different parts of the body together smoothly and efficiently: changing from \
|
|
|
|
-- one foot position to another requires coordination and balance.",
|
|
|
|
-- id = "/mnt/us/documents/dictionaries/Oxford_Dictionary_of_English.azw",
|
|
|
|
-- lang = "en",
|
|
|
|
-- })
|
|
|
|
if result then
|
|
|
|
UIManager:show(DictQuickLookup:new{
|
2013-06-15 15:09:22 +00:00
|
|
|
ui = self.ui,
|
2013-04-24 14:57:03 +00:00
|
|
|
dict = result.dict,
|
|
|
|
definition = result.definition,
|
|
|
|
id = result.ID,
|
|
|
|
lang = result.lang,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|