[fix] DocumentRegistry: don't create empty sdr and add hasProvider() (#3675)

As suggested by @poire-z https://github.com/koreader/koreader/pull/3653#issuecomment-364663156
pull/3676/head
Frans de Jonge 6 years ago committed by GitHub
parent 229c492979
commit ced9e45d92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -104,14 +104,14 @@ function DropBoxApi:listFolder(path, token)
table.insert(dropbox_list, { table.insert(dropbox_list, {
text = text, text = text,
url = files.path_display, url = files.path_display,
type = tag type = tag,
}) })
--show only file with supported formats --show only file with supported formats
elseif tag == "file" and DocumentRegistry:getProvider(text) then elseif tag == "file" and DocumentRegistry:hasProvider(text) then
table.insert(dropbox_file, { table.insert(dropbox_file, {
text = text, text = text,
url = files.path_display, url = files.path_display,
type = tag type = tag,
}) })
end end
end end
@ -126,7 +126,7 @@ function DropBoxApi:listFolder(path, token)
table.insert(dropbox_list, { table.insert(dropbox_list, {
text = files.text, text = files.text,
url = files.url, url = files.url,
type = files.type type = files.type,
}) })
end end
return dropbox_list return dropbox_list

@ -38,7 +38,7 @@ function FtpApi:listFolder(address_path,folder_path)
type = type, type = type,
}) })
--show only file with supported formats --show only file with supported formats
elseif extension and DocumentRegistry:getProvider(item) then elseif extension and DocumentRegistry:hasProvider(item) then
type = "file" type = "file"
table.insert(ftp_file, { table.insert(ftp_file, {
text = file_name, text = file_name,

@ -149,7 +149,7 @@ function FileManager:init()
has_close_button = true, has_close_button = true,
perpage = G_reader_settings:readSetting("items_per_page"), perpage = G_reader_settings:readSetting("items_per_page"),
file_filter = function(filename) file_filter = function(filename)
if DocumentRegistry:getProvider(filename) then if DocumentRegistry:hasProvider(filename) then
return true return true
end end
end, end,
@ -631,14 +631,14 @@ function FileManager:deleteFile(file)
return return
end end
local is_doc = DocumentRegistry:getProvider(file_abs_path) local is_doc = DocumentRegistry:hasProvider(file_abs_path)
if lfs.attributes(file_abs_path, "mode") == "file" then if lfs.attributes(file_abs_path, "mode") == "file" then
ok, err = os.remove(file_abs_path) ok, err = os.remove(file_abs_path)
else else
ok, err = util.purgeDir(file_abs_path) ok, err = util.purgeDir(file_abs_path)
end end
if ok and err == nil then if ok and not err then
if is_doc ~= nil then if is_doc then
DocSettings:open(file):purge() DocSettings:open(file):purge()
end end
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{

@ -41,7 +41,7 @@ function FileSearcher:readDir()
if attributes.mode == "directory" and f ~= "." and f~=".." then if attributes.mode == "directory" and f ~= "." and f~=".." then
table.insert(new_dirs, fullpath) table.insert(new_dirs, fullpath)
table.insert(self.files, {name = f, path = fullpath, attr = attributes}) table.insert(self.files, {name = f, path = fullpath, attr = attributes})
elseif attributes.mode == "file" and DocumentRegistry:getProvider(fullpath) then elseif attributes.mode == "file" and DocumentRegistry:hasProvider(fullpath) then
table.insert(self.files, {name = f, path = fullpath, attr = attributes}) table.insert(self.files, {name = f, path = fullpath, attr = attributes})
end end
end end

@ -346,7 +346,7 @@ function Search:find(option)
self.browse_tags[string.sub(j,2)] = (self.browse_tags[string.sub(j,2)] or 0) + 1 self.browse_tags[string.sub(j,2)] = (self.browse_tags[string.sub(j,2)] or 0) + 1
end end
end end
if DocumentRegistry:getProvider(self.data[i][self.path]) then if DocumentRegistry:hasProvider(self.data[i][self.path]) then
if upsearch ~= "" then if upsearch ~= "" then
if string.find(search_content,upsearch,nil,true) then if string.find(search_content,upsearch,nil,true) then
i = i + 1 i = i + 1

@ -13,6 +13,7 @@ local T = require("ffi/util").template
local DocumentRegistry = { local DocumentRegistry = {
registry = {}, registry = {},
providers = {}, providers = {},
filetype_provider = {},
} }
function DocumentRegistry:addProvider(extension, mimetype, provider, weight) function DocumentRegistry:addProvider(extension, mimetype, provider, weight)
@ -22,6 +23,19 @@ function DocumentRegistry:addProvider(extension, mimetype, provider, weight)
provider = provider, provider = provider,
weight = weight or 100, weight = weight or 100,
}) })
self.filetype_provider[extension] = true
end
--- Returns true if file has provider.
-- @string file
-- @treturn boolean
function DocumentRegistry:hasProvider(file)
local filename_suffix = util.getFileNameSuffix(file)
if self.filetype_provider[filename_suffix] then
return true
end
return false
end end
--- Returns the preferred registered document handler. --- Returns the preferred registered document handler.
@ -32,12 +46,14 @@ function DocumentRegistry:getProvider(file)
if providers then if providers then
-- provider for document -- provider for document
local doc_settings_provider = require("docsettings"):open(file):readSetting("provider") local DocSettings = require("docsettings")
if DocSettings:hasSidecarFile(file) then
if doc_settings_provider then local doc_settings_provider = DocSettings:open(file):readSetting("provider")
for _, provider in ipairs(providers) do if doc_settings_provider then
if provider.provider.provider == doc_settings_provider then for _, provider in ipairs(providers) do
return provider.provider if provider.provider.provider == doc_settings_provider then
return provider.provider
end
end end
end end
end end

@ -16,10 +16,10 @@ describe("document registry module", function()
it("should return all supported rendering engines", function() it("should return all supported rendering engines", function()
local providers = DocumentRegistry:getProviders("bla.epub") local providers = DocumentRegistry:getProviders("bla.epub")
assert.is_equal("Cool Reader Engine", assert.is_equal("crengine",
providers[1].provider.provider_name) providers[1].provider.provider)
assert.is_equal("MuPDF", assert.is_equal("mupdf",
providers[2].provider.provider_name) providers[2].provider.provider)
end) end)
it("should set per-document setting for rendering engine", function() it("should set per-document setting for rendering engine", function()

Loading…
Cancel
Save