diff --git a/frontend/apps/filemanager/filemanagerutil.lua b/frontend/apps/filemanager/filemanagerutil.lua index 220a2e2ef..d1b67fb80 100644 --- a/frontend/apps/filemanager/filemanagerutil.lua +++ b/frontend/apps/filemanager/filemanagerutil.lua @@ -48,14 +48,6 @@ function filemanagerutil.splitFileNameType(filepath) return filename_without_suffix, filetype end --- Purge doc settings in sidecar directory -function filemanagerutil.purgeSettings(file) - local file_abs_path = ffiutil.realpath(file) - if file_abs_path then - return DocSettings:open(file_abs_path):purge() - end -end - -- Purge doc settings except kept function filemanagerutil.resetDocumentSettings(file) local settings_to_keep = { @@ -138,34 +130,66 @@ end -- Generate "Reset" file dialog button function filemanagerutil.genResetSettingsButton(file, caller_callback, button_disabled) + file = ffiutil.realpath(file) or file + local has_sidecar_file = DocSettings:hasSidecarFile(file) + local custom_cover_file = DocSettings:findCoverFile(file) + local has_custom_cover_file = custom_cover_file and true or false + local custom_metadata_file = DocSettings:getCustomMetadataFile(file) + local has_custom_metadata_file = custom_metadata_file and true or false return { text = _("Reset"), id = "reset", -- used by covermenu - enabled = (not button_disabled and DocSettings:hasSidecarFile(ffiutil.realpath(file))) and true or false, + enabled = not button_disabled and (has_sidecar_file or has_custom_metadata_file or has_custom_cover_file), callback = function() + local CheckButton = require("ui/widget/checkbutton") local ConfirmBox = require("ui/widget/confirmbox") + local check_button_settings, check_button_cover, check_button_metadata local confirmbox = ConfirmBox:new{ text = T(_("Reset this document?") .. "\n\n%1\n\n" .. - _("Document progress, settings, bookmarks, highlights, notes and custom cover image will be permanently lost."), + _("Resetted information will be permanently lost."), BD.filepath(file)), ok_text = _("Reset"), ok_callback = function() - local custom_metadata_purged = filemanagerutil.purgeSettings(file) - if custom_metadata_purged then -- refresh coverbrowser cached book info - local FileManager = require("apps/filemanager/filemanager") - local ui = FileManager.instance - if not ui then - local ReaderUI = require("apps/reader/readerui") - ui = ReaderUI.instance - end + local data_to_purge = { + doc_settings = check_button_settings.checked, + custom_cover_file = check_button_cover.checked and custom_cover_file, + custom_metadata_file = check_button_metadata.checked and custom_metadata_file, + } + DocSettings:open(file):purge(nil, data_to_purge) + if data_to_purge.custom_cover_file or data_to_purge.custom_metadata_file then + local ui = require("apps/filemanager/filemanager").instance + or require("apps/reader/readerui").instance if ui and ui.coverbrowser then - ui.coverbrowser:deleteBookInfo(file) + ui.coverbrowser:deleteBookInfo(file) -- refresh coverbrowser cached book info end end - require("readhistory"):fileSettingsPurged(file) + if data_to_purge.doc_settings then + require("readhistory"):fileSettingsPurged(file) + end caller_callback() end, } + check_button_settings = CheckButton:new{ + text = _("document settings, progress, bookmarks, highlights, notes"), + checked = has_sidecar_file, + enabled = has_sidecar_file, + parent = confirmbox, + } + confirmbox:addWidget(check_button_settings) + check_button_cover = CheckButton:new{ + text = _("custom cover image"), + checked = has_custom_cover_file, + enabled = has_custom_cover_file, + parent = confirmbox, + } + confirmbox:addWidget(check_button_cover) + check_button_metadata = CheckButton:new{ + text = _("custom book metadata"), + checked = has_custom_metadata_file, + enabled = has_custom_metadata_file, + parent = confirmbox, + } + confirmbox:addWidget(check_button_metadata) UIManager:show(confirmbox) end, } diff --git a/frontend/docsettings.lua b/frontend/docsettings.lua index b1c82344f..559177abc 100644 --- a/frontend/docsettings.lua +++ b/frontend/docsettings.lua @@ -90,11 +90,18 @@ function DocSettings:getSidecarFile(doc_path, force_location) return self:getSidecarDir(doc_path, force_location) .. "/metadata." .. suffix .. ".lua" end +--- Returns `true` if there is a `metadata.lua` file. +-- @string doc_path path to the document (e.g., `/foo/bar.pdf`) +-- @treturn bool +function DocSettings:hasSidecarFile(doc_path) + return self:getDocSidecarFile(doc_path) and true or false +end + --- Returns path of `metadata.lua` file if it exists, or nil. -- @string doc_path path to the document (e.g., `/foo/bar.pdf`) -- @bool no_legacy set to true to skip check of the legacy history file -- @treturn string -function DocSettings:hasSidecarFile(doc_path, no_legacy) +function DocSettings:getDocSidecarFile(doc_path, no_legacy) local sidecar_file = self:getSidecarFile(doc_path, "doc") if lfs.attributes(sidecar_file, "mode") == "file" then return sidecar_file @@ -294,9 +301,22 @@ function DocSettings:flush(data, no_custom_metadata) end --- Purges (removes) sidecar directory. -function DocSettings:purge(sidecar_to_keep) +function DocSettings:purge(sidecar_to_keep, data_to_purge) + local custom_cover_file, custom_metadata_file + if sidecar_to_keep == nil then + custom_cover_file = self:getCoverFile() + custom_metadata_file = self:getCustomMetadataFile() + end + if data_to_purge == nil then + data_to_purge = { + doc_settings = true, + custom_cover_file = custom_cover_file, + custom_metadata_file = custom_metadata_file, + } + end + -- Remove any of the old ones we may consider as candidates in DocSettings:open() - if self.candidates then + if data_to_purge.doc_settings and self.candidates then for _, t in ipairs(self.candidates) do local candidate_path = t.path if lfs.attributes(candidate_path, "mode") == "file" then @@ -309,31 +329,23 @@ function DocSettings:purge(sidecar_to_keep) end end - local custom_metadata_purged - if not sidecar_to_keep then - -- custom cover - local metadata_file = self:getCoverFile() - if metadata_file then - os.remove(metadata_file) - self:getCoverFile(true) -- reset cache - custom_metadata_purged = true - end - -- custom metadata - metadata_file = self:getCustomMetadataFile() - if metadata_file then - os.remove(metadata_file) - custom_metadata_purged = true - end - end - - if lfs.attributes(self.doc_sidecar_dir, "mode") == "directory" then - os.remove(self.doc_sidecar_dir) -- keep parent folders + if data_to_purge.custom_cover_file then + os.remove(data_to_purge.custom_cover_file) + self:getCoverFile(true) -- reset cache end - if lfs.attributes(self.dir_sidecar_dir, "mode") == "directory" then - util.removePath(self.dir_sidecar_dir) -- remove empty parent folders + if data_to_purge.custom_metadata_file then + os.remove(data_to_purge.custom_metadata_file) end - return custom_metadata_purged + if data_to_purge.doc_settings or data_to_purge.custom_cover_file or data_to_purge.custom_metadata_file then + -- remove sidecar dirs iff empty + if lfs.attributes(self.doc_sidecar_dir, "mode") == "directory" then + os.remove(self.doc_sidecar_dir) -- keep parent folders + end + if lfs.attributes(self.dir_sidecar_dir, "mode") == "directory" then + util.removePath(self.dir_sidecar_dir) -- remove empty parent folders + end + end end --- Removes empty sidecar dir. @@ -438,7 +450,7 @@ function DocSettings:getCoverFile(reset_cache) end function DocSettings:getCustomCandidateSidecarDirs(doc_path) - local sidecar_file = self:hasSidecarFile(doc_path, true) -- new locations only + local sidecar_file = self:getDocSidecarFile(doc_path, true) -- new locations only if sidecar_file then -- book was opened, write custom metadata to its sidecar dir local sidecar_dir = util.splitFilePathName(sidecar_file):sub(1, -2) return { sidecar_dir } diff --git a/frontend/document/documentregistry.lua b/frontend/document/documentregistry.lua index 8a719a6cd..039cac81a 100644 --- a/frontend/document/documentregistry.lua +++ b/frontend/document/documentregistry.lua @@ -49,7 +49,7 @@ function DocumentRegistry:getRandomFile(dir, opened, extension) if ok then for entry in iter, dir_obj do local file = dir .. entry - local file_opened = DocSettings:hasSidecarFile(file) and true or false + local file_opened = DocSettings:hasSidecarFile(file) if lfs.attributes(file, "mode") == "file" and self:hasProvider(file) and (opened == nil or file_opened == opened) and (extension == nil or extension[util.getFileNameSuffix(entry)]) then diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index 0b9caa315..e7d0c4bbf 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -159,13 +159,13 @@ function FileChooser:getListItem(f, filename, attributes, collate) } if collate then -- file if G_reader_settings:readSetting("show_file_in_bold") ~= false then - item.opened = DocSettings:hasSidecarFile(filename) and true or false + item.opened = DocSettings:hasSidecarFile(filename) end if collate == "type" then item.suffix = util.getFileNameSuffix(f) elseif collate == "percent_unopened_first" or collate == "percent_unopened_last" then local percent_finished - item.opened = DocSettings:hasSidecarFile(filename) and true or false + item.opened = DocSettings:hasSidecarFile(filename) if item.opened then local doc_settings = DocSettings:open(filename) percent_finished = doc_settings:readSetting("percent_finished")