Skip to content

Commit

Permalink
ReaderDictionary: add info message about installing dictionaries
Browse files Browse the repository at this point in the history
Fixes #2816.
  • Loading branch information
Frenzie authored and houqp committed Apr 27, 2017
1 parent fa12f48 commit ed7937f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
22 changes: 22 additions & 0 deletions frontend/apps/reader/modules/readerdictionary.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ function ReaderDictionary:onLookupWord(word, box, highlight)
return true
end

local function dictDirsEmpty(dict_dirs)
for _, dict_dir in ipairs(dict_dirs) do
if not util.isEmptyDir(dict_dir) then
return false
end
end
return true
end

local function tidyMarkup(results)
local cdata_tag = "<!%[CDATA%[(.-)%]%]>"
local format_escape = "&[29Ib%+]{(.-)}"
Expand Down Expand Up @@ -135,6 +144,19 @@ function ReaderDictionary:stardictLookup(word, box)
if lfs.attributes(dict_ext, "mode") == "directory" then
table.insert(dict_dirs, dict_ext)
end
-- early exit if no dictionaries
if dictDirsEmpty(dict_dirs) then
final_results = {
{
dict = "",
word = word,
definition = _([[No dictionaries installed. Please search for "Dictionary support" in the KOReader Wiki to get more information about installing new dictionaries.]]),
}
}
self:onLookupDone()
self:showDict(word, final_results, box)
return
end
for _, dict_dir in ipairs(dict_dirs) do
local results_str = nil
if Device:isAndroid() then
Expand Down
12 changes: 12 additions & 0 deletions frontend/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,18 @@ function util.getFilesystemType(path)
return type
end

--- Checks if directory is empty.
---- @string path
---- @treturn bool
function util.isEmptyDir(path)
for filename in require("lfs").dir(path) do
if filename ~= '.' and filename ~= '..' then
return false
end
end
return true
end

--- Replaces characters that are invalid filenames.
--
-- Replaces the characters <code>\/:*?"<>|</code> with an <code>_</code>.
Expand Down

2 comments on commit ed7937f

@poire-z
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had a crash on the emulator I just updated:

./luajit: frontend/util.lua:290: module 'lfs' not found:
        no field package.preload['lfs']
        no file 'common/lfs.lua'
        [...]
stack traceback:
        [C]: in function 'require'
        frontend/util.lua:290: in function 'isEmptyDir'
        frontend/apps/reader/modules/readerdictionary.lua:48: in function 'dictDirsEmpty'
        frontend/apps/reader/modules/readerdictionary.lua:148: in function 'stardictLookup'

Other modules have local lfs = require("libs/libkoreader-lfs") where you have there require("lfs").dir(path)

@Frenzie
Copy link
Member Author

@Frenzie Frenzie commented on ed7937f Apr 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, you're right. I guess it must've been picking it up from my system.

Please sign in to comment.