From 5d103a41f86562c0ba371cc778ab93c22b49ccc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fern=C3=A1ndez?= Date: Tue, 25 Feb 2020 21:26:19 +0100 Subject: [PATCH] Avoid writting highlights into read-only PDFs (#5889) --- frontend/apps/reader/modules/readerhighlight.lua | 14 +++++++++++++- frontend/document/pdfdocument.lua | 8 ++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 8e62a5791..c4458b08c 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -1167,8 +1167,20 @@ end --]] function ReaderHighlight:exportToDocument(page, item) + local setting = G_reader_settings:readSetting("save_document") + if setting == "disable" then return end logger.dbg("export highlight to document", item) - self.ui.document:saveHighlight(page, item) + local can_write = self.ui.document:saveHighlight(page, item) + if can_write == false and not self.warned_once then + self.warned_once = true + UIManager:show(InfoMessage:new{ + text = _([[ +Highlights in this document will be saved in the settings file, but they won't be written in the document itself because the file is in a read-only location. + +If you wish your highlights to be saved in the document, just move it to a writable directory first.]]), + timeout = 5, + }) + end end function ReaderHighlight:addNote() diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index 951df0c3b..6736a573c 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -175,6 +175,14 @@ function PdfDocument:saveHighlight(pageno, item) local suffix = util.getFileNameSuffix(self.file) if string.lower(suffix) ~= "pdf" then return end + if self.is_writable == nil then + local handle = io.open(self.file, 'r+b') + self.is_writable = handle ~= nil + if handle then handle:close() end + end + if self.is_writable == false then + return false + end self.is_edited = true -- will also need mupdf_h.lua to be evaluated once -- but this is guaranteed at this point