2017-04-12 16:55:22 +00:00
|
|
|
--[[--
|
2013-10-18 20:38:07 +00:00
|
|
|
This is a registry for document providers
|
|
|
|
]]--
|
2017-04-12 16:55:22 +00:00
|
|
|
|
|
|
|
local logger = require("logger")
|
2018-07-28 18:30:39 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2018-02-02 20:21:52 +00:00
|
|
|
local util = require("util")
|
2017-04-12 16:55:22 +00:00
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local DocumentRegistry = {
|
2014-08-28 12:59:42 +00:00
|
|
|
registry = {},
|
|
|
|
providers = {},
|
2018-02-10 17:36:18 +00:00
|
|
|
filetype_provider = {},
|
2013-10-18 20:38:07 +00:00
|
|
|
}
|
|
|
|
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
function DocumentRegistry:addProvider(extension, mimetype, provider, weight)
|
2018-11-29 18:29:14 +00:00
|
|
|
extension = string.lower(extension)
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
table.insert(self.providers, {
|
|
|
|
extension = extension,
|
|
|
|
mimetype = mimetype,
|
|
|
|
provider = provider,
|
|
|
|
weight = weight or 100,
|
|
|
|
})
|
2018-02-10 17:36:18 +00:00
|
|
|
self.filetype_provider[extension] = true
|
|
|
|
end
|
|
|
|
|
2018-07-28 18:30:39 +00:00
|
|
|
function DocumentRegistry:getRandomFile(dir, opened, extension)
|
|
|
|
local DocSettings = require("docsettings")
|
|
|
|
if string.sub(dir, string.len(dir)) ~= "/" then
|
|
|
|
dir = dir .. "/"
|
|
|
|
end
|
|
|
|
local files = {}
|
|
|
|
local i = 0
|
|
|
|
local ok, iter, dir_obj = pcall(lfs.dir, dir)
|
|
|
|
if ok then
|
|
|
|
for entry in iter, dir_obj do
|
|
|
|
if lfs.attributes(dir .. entry, "mode") == "file" and self:hasProvider(dir .. entry)
|
|
|
|
and (opened == nil or DocSettings:hasSidecarFile(dir .. entry) == opened)
|
|
|
|
and (extension == nil or extension[util.getFileNameSuffix(entry)]) then
|
|
|
|
i = i + 1
|
|
|
|
files[i] = entry
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if i == 0 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
math.randomseed(os.time())
|
|
|
|
return dir .. files[math.random(i)]
|
|
|
|
end
|
|
|
|
|
2018-02-10 17:36:18 +00:00
|
|
|
--- Returns true if file has provider.
|
|
|
|
-- @string file
|
|
|
|
-- @treturn boolean
|
|
|
|
function DocumentRegistry:hasProvider(file)
|
2018-11-29 18:29:14 +00:00
|
|
|
local filename_suffix = string.lower(util.getFileNameSuffix(file))
|
2018-02-10 17:36:18 +00:00
|
|
|
|
2019-10-23 21:15:30 +00:00
|
|
|
local filetype_provider = G_reader_settings:readSetting("provider") or {}
|
|
|
|
if self.filetype_provider[filename_suffix] or filetype_provider[filename_suffix] then
|
2018-02-10 17:36:18 +00:00
|
|
|
return true
|
|
|
|
end
|
2019-10-23 21:15:30 +00:00
|
|
|
local DocSettings = require("docsettings")
|
|
|
|
if DocSettings:hasSidecarFile(file) then
|
|
|
|
local doc_settings_provider = DocSettings:open(file):readSetting("provider")
|
|
|
|
if doc_settings_provider then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2018-02-10 17:36:18 +00:00
|
|
|
return false
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
--- Returns the preferred registered document handler.
|
2017-04-12 16:55:22 +00:00
|
|
|
-- @string file
|
2018-02-02 20:21:52 +00:00
|
|
|
-- @treturn table provider, or nil
|
2013-10-18 20:38:07 +00:00
|
|
|
function DocumentRegistry:getProvider(file)
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
local providers = self:getProviders(file)
|
|
|
|
|
|
|
|
if providers then
|
2018-02-02 20:21:52 +00:00
|
|
|
-- provider for document
|
2018-02-10 17:36:18 +00:00
|
|
|
local DocSettings = require("docsettings")
|
|
|
|
if DocSettings:hasSidecarFile(file) then
|
|
|
|
local doc_settings_provider = DocSettings:open(file):readSetting("provider")
|
|
|
|
if doc_settings_provider then
|
|
|
|
for _, provider in ipairs(providers) do
|
|
|
|
if provider.provider.provider == doc_settings_provider then
|
|
|
|
return provider.provider
|
|
|
|
end
|
2018-02-02 20:21:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- global provider for filetype
|
|
|
|
local filename_suffix = util.getFileNameSuffix(file)
|
|
|
|
local g_settings_provider = G_reader_settings:readSetting("provider")
|
|
|
|
|
|
|
|
if g_settings_provider and g_settings_provider[filename_suffix] then
|
|
|
|
for _, provider in ipairs(providers) do
|
|
|
|
if provider.provider.provider == g_settings_provider[filename_suffix] then
|
|
|
|
return provider.provider
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- highest weighted provider
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
return providers[1].provider
|
2019-10-23 21:15:30 +00:00
|
|
|
else
|
|
|
|
for _, provider in ipairs(self.providers) do
|
|
|
|
if provider.extension == "txt" then
|
|
|
|
return provider.provider
|
|
|
|
end
|
|
|
|
end
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Returns the registered document handlers.
|
|
|
|
-- @string file
|
|
|
|
-- @treturn table providers, or nil
|
|
|
|
function DocumentRegistry:getProviders(file)
|
|
|
|
local providers = {}
|
|
|
|
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @todo some implementation based on mime types?
|
2014-03-13 13:52:43 +00:00
|
|
|
for _, provider in ipairs(self.providers) do
|
2020-03-13 20:29:13 +00:00
|
|
|
local added = false
|
2014-11-04 17:56:19 +00:00
|
|
|
local suffix = string.sub(file, -string.len(provider.extension) - 1)
|
2014-11-04 18:56:11 +00:00
|
|
|
if string.lower(suffix) == "."..provider.extension then
|
2020-03-13 20:29:13 +00:00
|
|
|
for i, prov_prev in ipairs(providers) do
|
|
|
|
if prov_prev.provider == provider.provider then
|
|
|
|
if prov_prev.weight >= provider.weight then
|
|
|
|
added = true
|
|
|
|
else
|
|
|
|
table.remove(providers, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-11-04 17:56:19 +00:00
|
|
|
-- if extension == provider.extension then
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
-- stick highest weighted provider at the front
|
2020-03-13 20:29:13 +00:00
|
|
|
if not added and #providers >= 1 and provider.weight > providers[1].weight then
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
table.insert(providers, 1, provider)
|
2020-03-13 20:29:13 +00:00
|
|
|
elseif not added then
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
table.insert(providers, provider)
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
end
|
[feat] DocumentRegistry: add getProviders() and preferred by weight (#3651)
This is step one toward "open with".
References https://github.com/koreader/koreader/issues/3345
* Fix up some mimetypes
* Add XHTML to supported filetypes
* Add a few image files to MuPDF
* ".bmp",
* ".gif",
* ".hdp",
* ".j2k",
* ".jp2",
* ".jpeg",
* ".jpg",
* ".jpx",
* ".jxr",
* ".pam",
* ".pbm",
* ".pgm",
* ".png",
* ".pnm",
* ".ppm",
* ".tif",
* ".tiff",
* ".wdp",
2018-01-31 19:49:21 +00:00
|
|
|
|
|
|
|
if #providers >= 1 then
|
|
|
|
return providers
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
2018-02-02 20:21:52 +00:00
|
|
|
--- Sets the preferred registered document handler.
|
|
|
|
-- @string file
|
|
|
|
-- @bool all
|
|
|
|
function DocumentRegistry:setProvider(file, provider, all)
|
|
|
|
local _, filename_suffix = util.splitFileNameSuffix(file)
|
|
|
|
|
|
|
|
-- per-document
|
|
|
|
if not all then
|
|
|
|
local DocSettings = require("docsettings"):open(file)
|
|
|
|
DocSettings:saveSetting("provider", provider.provider)
|
|
|
|
DocSettings:flush()
|
|
|
|
-- global
|
|
|
|
else
|
|
|
|
local filetype_provider = G_reader_settings:readSetting("provider") or {}
|
|
|
|
filetype_provider[filename_suffix] = provider.provider
|
|
|
|
G_reader_settings:saveSetting("provider", filetype_provider)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function DocumentRegistry:openDocument(file, provider)
|
2017-04-26 19:46:46 +00:00
|
|
|
-- force a GC, so that any previous document used memory can be reused
|
|
|
|
-- immediately by this new document without having to wait for the
|
|
|
|
-- next regular gc. The second call may help reclaming more memory.
|
|
|
|
collectgarbage()
|
|
|
|
collectgarbage()
|
2014-08-28 12:59:42 +00:00
|
|
|
if not self.registry[file] then
|
2018-02-02 20:21:52 +00:00
|
|
|
provider = provider or self:getProvider(file)
|
|
|
|
|
2014-08-28 12:59:42 +00:00
|
|
|
if provider ~= nil then
|
2014-08-29 09:17:08 +00:00
|
|
|
local ok, doc = pcall(provider.new, provider, {file = file})
|
|
|
|
if ok then
|
|
|
|
self.registry[file] = {
|
|
|
|
doc = doc,
|
|
|
|
refs = 1,
|
|
|
|
}
|
|
|
|
else
|
2016-12-29 08:10:38 +00:00
|
|
|
logger.warn("cannot open document", file, doc)
|
2014-08-29 09:17:08 +00:00
|
|
|
end
|
2014-08-28 12:59:42 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
self.registry[file].refs = self.registry[file].refs + 1
|
|
|
|
end
|
2014-08-29 09:17:08 +00:00
|
|
|
if self.registry[file] then
|
|
|
|
return self.registry[file].doc
|
|
|
|
end
|
2014-08-28 12:59:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocumentRegistry:closeDocument(file)
|
|
|
|
if self.registry[file] then
|
|
|
|
self.registry[file].refs = self.registry[file].refs - 1
|
|
|
|
if self.registry[file].refs == 0 then
|
|
|
|
self.registry[file] = nil
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return self.registry[file].refs
|
|
|
|
end
|
|
|
|
else
|
|
|
|
error("Try to close unregistered file.")
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- load implementations:
|
|
|
|
|
2014-11-04 17:57:09 +00:00
|
|
|
require("document/credocument"):register(DocumentRegistry)
|
2013-10-18 20:38:07 +00:00
|
|
|
require("document/pdfdocument"):register(DocumentRegistry)
|
|
|
|
require("document/djvudocument"):register(DocumentRegistry)
|
2013-10-22 15:19:08 +00:00
|
|
|
require("document/picdocument"):register(DocumentRegistry)
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return DocumentRegistry
|