2014-08-29 09:17:08 +00:00
|
|
|
local DEBUG = require("dbg")
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
--[[
|
|
|
|
This is a registry for document providers
|
|
|
|
]]--
|
|
|
|
local DocumentRegistry = {
|
2014-08-28 12:59:42 +00:00
|
|
|
registry = {},
|
|
|
|
providers = {},
|
2013-10-18 20:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function DocumentRegistry:addProvider(extension, mimetype, provider)
|
2014-03-13 13:52:43 +00:00
|
|
|
table.insert(self.providers, { extension = extension, mimetype = mimetype, provider = provider })
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocumentRegistry:getProvider(file)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- TODO: some implementation based on mime types?
|
|
|
|
for _, provider in ipairs(self.providers) do
|
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
|
2014-11-04 17:56:19 +00:00
|
|
|
-- if extension == provider.extension then
|
2014-03-13 13:52:43 +00:00
|
|
|
return provider.provider
|
|
|
|
end
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function DocumentRegistry:openDocument(file)
|
2014-08-28 12:59:42 +00:00
|
|
|
if not self.registry[file] then
|
|
|
|
local provider = self:getProvider(file)
|
|
|
|
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
|
|
|
|
DEBUG("cannot open document", file, doc)
|
|
|
|
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
|