From ed7937f65d0891f54491be8f77f9864edaaad460 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Wed, 26 Apr 2017 08:12:25 +0200 Subject: [PATCH] ReaderDictionary: add info message about installing dictionaries Fixes #2816. --- .../apps/reader/modules/readerdictionary.lua | 22 +++++++++++++++++++ frontend/util.lua | 12 ++++++++++ 2 files changed, 34 insertions(+) diff --git a/frontend/apps/reader/modules/readerdictionary.lua b/frontend/apps/reader/modules/readerdictionary.lua index 3c262ea33..d046c2a52 100644 --- a/frontend/apps/reader/modules/readerdictionary.lua +++ b/frontend/apps/reader/modules/readerdictionary.lua @@ -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 = "" local format_escape = "&[29Ib%+]{(.-)}" @@ -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 diff --git a/frontend/util.lua b/frontend/util.lua index 457ee3775..9f1968c5f 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -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 \/:*?"<>| with an _.