From 0eeb8bd2b7eb941aca21156f3e924e3dd449347f Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Sun, 21 Nov 2021 21:41:58 +0200 Subject: [PATCH] Backup and restore bookmarks/highlights when switching engines (#8455) Bookmark/highlight formats in crengine and mupdf are incompatible. This backups and restores bookmarks and highlights when opening the document with an incompatible engine, instead of deleting them. --- .../apps/reader/modules/readerbookmark.lua | 17 +++++++++++++- frontend/apps/reader/modules/readerview.lua | 17 +++++++++++++- frontend/apps/reader/readerui.lua | 23 +------------------ 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index e39392dc9..b99ffc1ac 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -264,7 +264,22 @@ function ReaderBookmark:importSavedHighlight(config) end function ReaderBookmark:onReadSettings(config) - self.bookmarks = config:readSetting("bookmarks") or {} + self.bookmarks = config:readSetting("bookmarks", {}) + -- Bookmark formats in crengine and mupdf are incompatible. + -- Backup bookmarks when the document is opened with incompatible engine. + if #self.bookmarks > 0 then + if self.ui.rolling and type(self.bookmarks[1].page) == "number" then + config:saveSetting("bookmarks_paging", self.bookmarks) + self.bookmarks = config:readSetting("bookmarks_rolling", {}) + config:saveSetting("bookmarks", self.bookmarks) + config:delSetting("bookmarks_rolling") + elseif self.ui.paging and type(self.bookmarks[1].page) == "string" then + config:saveSetting("bookmarks_rolling", self.bookmarks) + self.bookmarks = config:readSetting("bookmarks_paging", {}) + config:saveSetting("bookmarks", self.bookmarks) + config:delSetting("bookmarks_paging") + end + end -- need to do this after initialization because checking xpointer -- may cause segfaults before credocuments are inited. self.ui:registerPostInitCallback(function() diff --git a/frontend/apps/reader/modules/readerview.lua b/frontend/apps/reader/modules/readerview.lua index 520f72caa..52e6f3689 100644 --- a/frontend/apps/reader/modules/readerview.lua +++ b/frontend/apps/reader/modules/readerview.lua @@ -789,7 +789,22 @@ function ReaderView:onReadSettings(config) self:resetLayout() local page_scroll = config:readSetting("kopt_page_scroll") or self.document.configurable.page_scroll self.page_scroll = page_scroll == 1 and true or false - self.highlight.saved = config:readSetting("highlight") or {} + self.highlight.saved = config:readSetting("highlight", {}) + -- Highlight formats in crengine and mupdf are incompatible. + -- Backup highlights when the document is opened with incompatible engine. + if #self.highlight.saved > 0 then + if self.ui.rolling and type(self.highlight.saved[1][1].pos0) == "table" then + config:saveSetting("highlight_paging", self.highlight.saved) + self.highlight.saved = config:readSetting("highlight_rolling", {}) + config:saveSetting("highlight", self.highlight.saved) + config:delSetting("highlight_rolling") + elseif self.ui.paging and type(self.highlight.saved[1][1].pos0) == "string" then + config:saveSetting("highlight_rolling", self.highlight.saved) + self.highlight.saved = config:readSetting("highlight_paging", {}) + config:saveSetting("highlight", self.highlight.saved) + config:delSetting("highlight_paging") + end + end self.inverse_reading_order = config:isTrue("inverse_reading_order") or G_reader_settings:isTrue("inverse_reading_order") self.page_overlap_enable = config:isTrue("show_overlap_enable") or G_reader_settings:isTrue("page_overlap_enable") or DSHOWOVERLAP self.page_overlap_style = config:readSetting("page_overlap_style") or G_reader_settings:readSetting("page_overlap_style") or "dim" diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index 8c085d778..19dea5ef3 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -549,30 +549,9 @@ function ReaderUI:showReader(file, provider) -- We can now signal the existing ReaderUI/FileManager instances that it's time to go bye-bye... UIManager:broadcastEvent(Event:new("ShowingReader")) - - -- prevent crash due to incompatible bookmarks - --- @todo Split bookmarks from metadata and do per-engine in conversion. provider = provider or DocumentRegistry:getProvider(file) if provider.provider then - local doc_settings = DocSettings:open(file) - local bookmarks = doc_settings:readSetting("bookmarks") or {} - if #bookmarks >= 1 and - ((provider.provider == "crengine" and type(bookmarks[1].page) == "number") or - (provider.provider == "mupdf" and type(bookmarks[1].page) == "string")) then - UIManager:show(ConfirmBox:new{ - text = T(_("The document '%1' with bookmarks or highlights was previously opened with a different engine. To prevent issues, bookmarks need to be deleted before continuing."), - BD.filepath(file)), - ok_text = _("Delete"), - ok_callback = function() - doc_settings:delSetting("bookmarks") - doc_settings:close() - self:showReaderCoroutine(file, provider) - end, - cancel_callback = function() self:showFileManager() end, - }) - else - self:showReaderCoroutine(file, provider) - end + self:showReaderCoroutine(file, provider) end end