readerannotation: fix old backup, keep old bookmarks

reviewable/pr11563/r2
hius07 3 months ago committed by GitHub
parent 7b626f5742
commit 529dca752e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -84,6 +84,21 @@ function ReaderAnnotation:getAnnotationsFromBookmarksHighlights(bookmarks, highl
return annotations return annotations
end end
function ReaderAnnotation:getBookmarksHighlightsFromAnnotations(annotations)
local bookmarks, highlights = {}, {}
for i = #annotations, 1, -1 do
local an = annotations[i]
table.insert(bookmarks, self.buildBookmark(an))
if an.drawer then
if highlights[an.pageno] == nil then
highlights[an.pageno] = {}
end
table.insert(highlights[an.pageno], self.buildHighlight(an))
end
end
return bookmarks, highlights
end
function ReaderAnnotation:onReadSettings(config) function ReaderAnnotation:onReadSettings(config)
local bookmarks, highlights local bookmarks, highlights
local annotations = config:readSetting("annotations") local annotations = config:readSetting("annotations")
@ -91,11 +106,11 @@ function ReaderAnnotation:onReadSettings(config)
local has_annotations = #annotations > 0 local has_annotations = #annotations > 0
local annotations_type = has_annotations and type(annotations[1].page) local annotations_type = has_annotations and type(annotations[1].page)
-- Annotation formats in crengine and mupdf are incompatible. -- Annotation formats in crengine and mupdf are incompatible.
-- Backup annotations when the document is opened with incompatible engine. if self.ui.rolling and annotations_type ~= "string" then -- incompatible format loaded, or empty
if self.ui.rolling and annotations_type ~= "string" then if has_annotations then -- backup incompatible format if not empty
if has_annotations then
config:saveSetting("annotations_paging", annotations) config:saveSetting("annotations_paging", annotations)
end end
-- load compatible format
annotations = config:readSetting("annotations_rolling") or {} annotations = config:readSetting("annotations_rolling") or {}
config:delSetting("annotations_rolling") config:delSetting("annotations_rolling")
elseif self.ui.paging and annotations_type ~= "number" then elseif self.ui.paging and annotations_type ~= "number" then
@ -105,43 +120,53 @@ function ReaderAnnotation:onReadSettings(config)
annotations = config:readSetting("annotations_paging") or {} annotations = config:readSetting("annotations_paging") or {}
config:delSetting("annotations_paging") config:delSetting("annotations_paging")
end end
-- Build bookmarks/highlights bookmarks, highlights = self:getBookmarksHighlightsFromAnnotations(annotations)
bookmarks, highlights = {}, {}
if #annotations > 0 then
local n = #annotations + 1
for i, an in ipairs(annotations) do
bookmarks[n - i] = self.buildBookmark(an)
if an.drawer then
if highlights[an.pageno] == nil then
highlights[an.pageno] = {}
end
table.insert(highlights[an.pageno], self.buildHighlight(an))
end
end
end
else -- old bookmarks/highlights else -- old bookmarks/highlights
bookmarks = config:readSetting("bookmarks") or {} bookmarks = config:readSetting("bookmarks") or {}
highlights = config:readSetting("highlight") or {} highlights = config:readSetting("highlight") or {}
local has_bookmarks = #bookmarks > 0 local has_bookmarks = #bookmarks > 0
local bookmarks_type = has_bookmarks and type(bookmarks[1].page) local bookmarks_type = has_bookmarks and type(bookmarks[1].page)
if self.ui.rolling and bookmarks_type ~= "string" then if self.ui.rolling then
if has_bookmarks then if bookmarks_type == "string" then -- compatible format loaded, check for incompatible old backup
annotations = self:getAnnotationsFromBookmarksHighlights(bookmarks, highlights) if config:has("bookmarks_paging") then -- backup incompatible old backup
config:saveSetting("annotations_paging", annotations) local bookmarks_paging = config:readSetting("bookmarks_paging")
local highlights_paging = config:readSetting("highlight_paging")
local annotations_paging = self:getAnnotationsFromBookmarksHighlights(bookmarks_paging, highlights_paging)
config:saveSetting("annotations_paging", annotations_paging)
config:delSetting("bookmarks_paging")
config:delSetting("highlight_paging")
end
else -- incompatible format loaded, or empty
if has_bookmarks then -- backup incompatible format if not empty
annotations = self:getAnnotationsFromBookmarksHighlights(bookmarks, highlights)
config:saveSetting("annotations_paging", annotations)
end
-- load compatible format
bookmarks = config:readSetting("bookmarks_rolling") or {}
highlights = config:readSetting("highlight_rolling") or {}
config:delSetting("bookmarks_rolling")
config:delSetting("highlight_rolling")
end end
bookmarks = config:readSetting("bookmarks_rolling") or {} else
highlights = config:readSetting("highlight_rolling") or {} if bookmarks_type == "number" then
config:delSetting("bookmarks_rolling") if config:has("bookmarks_rolling") then
config:delSetting("highlight_rolling") local bookmarks_rolling = config:readSetting("bookmarks_rolling")
elseif self.ui.paging and bookmarks_type ~= "number" then local highlights_rolling = config:readSetting("highlight_rolling")
if has_bookmarks then local annotations_rolling = self:getAnnotationsFromBookmarksHighlights(bookmarks_rolling, highlights_rolling)
annotations = self:getAnnotationsFromBookmarksHighlights(bookmarks, highlights) config:saveSetting("annotations_rolling", annotations_rolling)
config:saveSetting("annotations_rolling", annotations) config:delSetting("bookmarks_rolling")
config:delSetting("highlight_rolling")
end
else
if has_bookmarks then
annotations = self:getAnnotationsFromBookmarksHighlights(bookmarks, highlights)
config:saveSetting("annotations_rolling", annotations)
end
bookmarks = config:readSetting("bookmarks_paging") or {}
highlights = config:readSetting("highlight_paging") or {}
config:delSetting("bookmarks_paging")
config:delSetting("highlight_paging")
end end
bookmarks = config:readSetting("bookmarks_paging") or {}
highlights = config:readSetting("highlight_paging") or {}
config:delSetting("bookmarks_paging")
config:delSetting("highlight_paging")
end end
end end
self.ui.bookmark.bookmarks = bookmarks self.ui.bookmark.bookmarks = bookmarks
@ -151,8 +176,8 @@ end
function ReaderAnnotation:onCloseDocument() function ReaderAnnotation:onCloseDocument()
local annotations = self:getAnnotationsFromBookmarksHighlights(self.ui.bookmark.bookmarks, self.view.highlight.saved, true) local annotations = self:getAnnotationsFromBookmarksHighlights(self.ui.bookmark.bookmarks, self.view.highlight.saved, true)
self.ui.doc_settings:saveSetting("annotations", annotations) self.ui.doc_settings:saveSetting("annotations", annotations)
self.ui.doc_settings:delSetting("bookmarks") self.ui.doc_settings:saveSetting("bookmarks", self.ui.bookmark.bookmarks)
self.ui.doc_settings:delSetting("highlight") self.ui.doc_settings:saveSetting("highlight", self.view.highlight.saved)
end end
return ReaderAnnotation return ReaderAnnotation

Loading…
Cancel
Save