diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index 352da8887..dc5c3be0c 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -1,10 +1,11 @@ local InputContainer = require("ui/widget/container/inputcontainer") -local Cache = require("cache") +local ConfirmBox = require("ui/widget/confirmbox") +local DocSettings = require("docsettings") +local UIManager = require("ui/uimanager") local Geom = require("ui/geometry") local Device = require("ui/device") -local DocSettings = require("docsettings") local Event = require("ui/event") -local UIManager = require("ui/uimanager") +local Cache = require("cache") local DEBUG = require("dbg") local _ = require("gettext") @@ -56,8 +57,6 @@ local ReaderUI = InputContainer:new{ -- the document interface document = nil, - -- initial page or percent inside document on opening - start_pos = nil, -- password for document unlock password = nil, @@ -307,14 +306,36 @@ function ReaderUI:onFlushSettings() return true end +function ReaderUI:closeDocument() + self.document:close() + self.document = nil +end + +function ReaderUI:onCloseDocument() + if self.document:isEdited() then + UIManager:show(ConfirmBox:new{ + text = _("Do you want to save this document?"), + ok_text = _("Yes"), + cancel_text = _("No"), + ok_callback = function() + self:closeDocument() + end, + cancel_callback = function() + self.document:discardChange() + self:closeDocument() + end, + }) + else + self:closeDocument() + end +end + function ReaderUI:onClose() DEBUG("closing reader") self:saveSettings() - self:handleEvent(Event:new("CloseReader")) if self.document ~= nil then - self.document:close() - self.document = nil - self.start_pos = nil + DEBUG("closing document") + self:onCloseDocument() end UIManager:close(self.dialog) -- serialize last used items for later launch diff --git a/frontend/document/document.lua b/frontend/document/document.lua index 5dbdb6109..6fa8b86fe 100644 --- a/frontend/document/document.lua +++ b/frontend/document/document.lua @@ -51,6 +51,9 @@ local Document = { -- flag to show that the document needs to be unlocked by a password is_locked = false, + + -- flag to show that the document is edited and needs to write back to disk + is_edited = false, } function Document:new(o) @@ -85,6 +88,17 @@ function Document:close() end end +-- check if document is edited and needs to write to disk +function Document:isEdited() + return self.is_edited +end + +-- discard change will set is_edited flag to false and implematation of Document +-- should check the is_edited flag before writing document +function Document:discardChange() + self.is_edited = false +end + -- this might be overridden by a document implementation function Document:getNativePageDimensions(pageno) local hash = "pgdim|"..self.file.."|"..pageno diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 5e85a8353..ea88d92d7 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -4,6 +4,7 @@ local KoptOptions = require("ui/data/koptoptions") local Document = require("document/document") local Configurable = require("configurable") local DrawContext = require("ffi/drawcontext") +local DEBUG = require("dbg") local ffi = require("ffi") ffi.cdef[[ typedef struct fz_point_s fz_point; @@ -46,7 +47,6 @@ local PdfDocument = Document:new{ dc_null = DrawContext.new(), options = KoptOptions, koptinterface = nil, - annot_revision = 0, } function PdfDocument:init() @@ -152,7 +152,7 @@ function PdfDocument:getPageLinks(pageno) end function PdfDocument:saveHighlight(pageno, item) - self.annot_revision = self.annot_revision + 1 + self.is_edited = true local n = #item.pboxes local quadpoints = ffi.new("fz_point[?]", 4*n) for i=1, n do @@ -179,11 +179,12 @@ function PdfDocument:saveHighlight(pageno, item) end function PdfDocument:writeDocument() + DEBUG("writing document to", self.file) self._document:writeDocument(self.file) end function PdfDocument:close() - if self.annot_revision ~= 0 then + if self.is_edited then self:writeDocument() end Document.close(self)