register opened document in documentregistry

so that when calling getCoverPageImage in screensaver mode
the document won't be opened again. This should avoid a lot of
problem such as messing up style and options described in #863.
pull/867/head
chrox 10 years ago
parent 0252139361
commit 132adf8e1b

@ -17,6 +17,7 @@ local CreDocument = Document:new{
PAGE_VIEW_MODE = 1,
_document = false,
_loaded = false,
engine_initilized = false,
line_space_percent = 100,
@ -120,9 +121,16 @@ function CreDocument:init()
self.info.configurable = true
end
function CreDocument:loadDocument()
if not self._loaded then
self._document:loadDocument(self.file)
self._loaded = true
end
end
function CreDocument:render()
-- load document before rendering
self._document:loadDocument(self.file)
self:loadDocument()
self._document:renderDocument()
if not self.info.has_pages then
self.info.doc_height = self._document:getFullHeight()
@ -139,7 +147,7 @@ end
function CreDocument:getCoverPageImage()
-- don't need to render document in order to get cover image
self._document:loadDocument(self.file)
self:loadDocument()
local data, size = self._document:getCoverPageImageData()
if data and size then
local image = Image:fromData(data, size)

@ -81,7 +81,8 @@ end
-- this might be overridden by a document implementation
function Document:close()
if self.is_open then
local DocumentRegistry = require("document/documentregistry")
if self.is_open and DocumentRegistry:closeDocument(self.file) == 0 then
self.is_open = false
self._document:close()
end

@ -2,7 +2,8 @@
This is a registry for document providers
]]--
local DocumentRegistry = {
providers = { }
registry = {},
providers = {},
}
function DocumentRegistry:addProvider(extension, mimetype, provider)
@ -20,9 +21,31 @@ function DocumentRegistry:getProvider(file)
end
function DocumentRegistry:openDocument(file)
local provider = self:getProvider(file)
if provider ~= nil then
return provider:new{file = file}
if not self.registry[file] then
local provider = self:getProvider(file)
if provider ~= nil then
self.registry[file] = {
doc = provider:new{file = file},
refs = 1,
}
end
else
self.registry[file].refs = self.registry[file].refs + 1
end
return self.registry[file].doc
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.")
end
end

Loading…
Cancel
Save