Book metadata archive (#15264)

This commit is contained in:
hius07 authored and GitHub committed 2026-04-29 19:34:20 +03:00
1 parent a825df20ca
commit 8a3054342b
11 files changed
+460 -60

No files matched your search

+28 -8
View File
@@ -544,14 +544,17 @@ function FileManager:getPlusDialogButtons()
text = _("Delete"),
enabled = actions_enabled,
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Delete selected files?\nIf you delete a file, it is permanently lost."),
local confirmbox
confirmbox = ConfirmBox:new{
text = _("Delete selected files?\nIf you delete a file, it is permanently lost.") .. "\n",
ok_text = _("Delete"),
ok_callback = function()
UIManager:close(self.plus_dialog)
self:deleteSelectedFiles()
end,
})
}
self.addMetadataArcCheckButton(confirmbox)
UIManager:show(confirmbox)
end,
},
{
@@ -1113,11 +1116,10 @@ function FileManager:showDeleteFileDialog(filepath, post_delete_callback, pre_de
return
end
local is_file = isFile(file)
local text = (is_file and _("Delete file permanently?") or _("Delete folder permanently?")) .. "\n\n" .. BD.filepath(file)
if is_file and BookList.hasBookBeenOpened(file) then
text = text .. "\n\n" .. _("Book settings, highlights and notes will be deleted.")
end
UIManager:show(ConfirmBox:new{
local text = (is_file and _("Delete file permanently?") or _("Delete folder permanently?"))
.. "\n\n" .. BD.filepath(file) .. "\n"
local confirmbox
confirmbox = ConfirmBox:new{
text = text,
ok_text = _("Delete"),
ok_callback = function()
@@ -1128,6 +1130,24 @@ function FileManager:showDeleteFileDialog(filepath, post_delete_callback, pre_de
post_delete_callback()
end
end,
}
if not is_file or BookList.hasBookBeenOpened(file) then
self.addMetadataArcCheckButton(confirmbox)
end
UIManager:show(confirmbox)
end
function FileManager.addMetadataArcCheckButton(confirmbox)
local folder = G_reader_settings:readSetting("document_metadata_arc_folder")
local enabled = folder ~= nil and lfs.attributes(folder, "mode") == "directory"
confirmbox:addWidget(CheckButton:new{
text = _("save book metadata to archive"),
enabled = enabled,
checked = enabled and G_reader_settings:isTrue("document_metadata_arc_on_deletion"),
parent = confirmbox,
callback = function()
G_reader_settings:flipNilOrFalse("document_metadata_arc_on_deletion")
end,
})
end
@@ -87,16 +87,22 @@ function BookInfo:show(doc_settings_or_file, book_props)
has_sidecar = true
end
local folder, filename = util.splitFilePathName(file)
local __, filetype = filemanagerutil.splitFileNameType(filename)
local attr = lfs.attributes(file)
local file_size = attr.size or 0
local size_f = util.getFriendlySize(file_size)
local size_b = util.getFormattedSize(file_size)
local is_file = attr and true or nil
table.insert(kv_pairs, { _("Filename:"), BD.filename(filename) })
table.insert(kv_pairs, { _("Format:"), filetype:upper() })
table.insert(kv_pairs, { _("Size:"), string.format("%s (%s bytes)", size_f, size_b) })
table.insert(kv_pairs, { _("File date:"), os.date("%Y-%m-%d %H:%M:%S", attr.modification) })
table.insert(kv_pairs, { _("Folder:"), BD.dirpath(filemanagerutil.abbreviate(folder)), separator = true })
if is_file then
local __, filetype = filemanagerutil.splitFileNameType(filename)
local file_size = attr.size or 0
local size_f = util.getFriendlySize(file_size)
local size_b = util.getFormattedSize(file_size)
table.insert(kv_pairs, { _("Format:"), filetype:upper() })
table.insert(kv_pairs, { _("Size:"), string.format("%s (%s bytes)", size_f, size_b) })
table.insert(kv_pairs, { _("File date:"), os.date("%Y-%m-%d %H:%M:%S", attr.modification) })
table.insert(kv_pairs, { _("Folder:"), BD.dirpath(filemanagerutil.abbreviate(folder)), separator = true })
else -- for deleted books show doc_settings info only
table.insert(kv_pairs, { _("Metadata:"),
doc_settings_or_file:readSetting("metadata_arc").datetime, separator = true })
end
-- Book section
-- book_props may be provided if caller already has them available
@@ -104,27 +110,33 @@ function BookInfo:show(doc_settings_or_file, book_props)
book_props = book_props or self:getDocProps(file, book_props)
book_props.pages = book_props.pages or BookList.getBookInfo(file).pages
-- cover image
self.custom_book_cover = DocSettings:findCustomCoverFile(file)
local key_text = self.prop_text["cover"]
if self.custom_book_cover then
key_text = "\u{F040} " .. key_text
if is_file then
self.custom_book_cover = DocSettings:findCustomCoverFile(file)
local key_text = self.prop_text["cover"]
if self.custom_book_cover then
key_text = "\u{F040} " .. key_text
end
table.insert(kv_pairs, { key_text, _("Tap to display"),
callback = function()
self:onShowBookCover(file)
end,
hold_callback = function()
self:showCustomDialog(file, book_props)
end,
separator = true,
})
end
table.insert(kv_pairs, { key_text, _("Tap to display"),
callback = function()
self:onShowBookCover(file)
end,
hold_callback = function()
self:showCustomDialog(file, book_props)
end,
separator = true,
})
-- metadata
local n_a = _("N/A")
local custom_props
local custom_metadata_file = DocSettings:findCustomMetadataFile(file)
if custom_metadata_file then
self.custom_doc_settings = DocSettings.openSettingsFile(custom_metadata_file)
custom_props = self.custom_doc_settings:readSetting("custom_props")
if is_file then
local custom_metadata_file = DocSettings:findCustomMetadataFile(file)
if custom_metadata_file then
self.custom_doc_settings = DocSettings.openSettingsFile(custom_metadata_file)
custom_props = self.custom_doc_settings:readSetting("custom_props")
end
else
custom_props = doc_settings_or_file:readSetting("metadata_arc").custom_props
end
local values_lang, callback
for _i, prop_key in ipairs(self.props) do
@@ -154,13 +166,16 @@ function BookInfo:show(doc_settings_or_file, book_props)
self:showBookProp("description", prop)
end
end
key_text = self.prop_text[prop_key]
local key_text = self.prop_text[prop_key]
if custom_props and custom_props[prop_key] then -- customized
if not is_file then -- props not customized yet
prop = custom_props[prop_key]
end
key_text = "\u{F040} " .. key_text
end
table.insert(kv_pairs, { key_text, prop,
callback = callback,
hold_callback = function()
hold_callback = is_file and function()
self:showCustomDialog(file, book_props, prop_key)
end,
})
@@ -240,7 +255,7 @@ Source (print edition):
-- Summary section
local summary = has_sidecar and doc_settings_or_file:readSetting("summary") or {}
local rating = summary.rating or 0
local summary_hold_callback = function()
local summary_hold_callback = is_file and function()
self:editSummary(doc_settings_or_file, book_props)
end
table.insert(kv_pairs, { _("Rating:"), ("★"):rep(rating) .. ("☆"):rep(self.rating_max - rating),
@@ -249,12 +264,14 @@ Source (print edition):
hold_callback = summary_hold_callback, separator = true })
-- Notebook file
local notebook_file = self:getNotebookFile(doc_settings_or_file)
local notebook_file_callback = function()
self:showNotebookFileDialog(notebook_file, doc_settings_or_file, book_props)
if is_file then
local notebook_file = self:getNotebookFile(doc_settings_or_file)
local notebook_file_callback = function()
self:showNotebookFileDialog(notebook_file, doc_settings_or_file, book_props)
end
table.insert(kv_pairs, { _("Notebook file:"), notebook_file:gsub(".*/", ""),
callback = notebook_file_callback })
end
table.insert(kv_pairs, { _("Notebook file:"), notebook_file:gsub(".*/", ""),
callback = notebook_file_callback })
local KeyValuePage = require("ui/widget/keyvaluepage")
self.kvp_widget = KeyValuePage:new{
@@ -910,6 +927,13 @@ end
-- book metadata (sdr)
function BookInfo:onShowBookMetadataArchive()
if G_reader_settings:has("document_metadata_arc_folder") then
local BookMetadataArchive = require("ui/widget/bookmetadataarchive")
BookMetadataArchive:showBookList(self.ui)
end
end
function BookInfo:moveBookMetadata()
-- called by filemanagermenu only
local file_chooser = self.ui.file_chooser
+13 -1
View File
@@ -367,7 +367,7 @@ function filemanagerutil.executeScript(file, caller_callback)
end)
end
function filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path, file_filter)
function filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path, file_filter, reset_button)
local is_file = file_filter and true or false
local path = current_path or default_path
local dialog
@@ -413,6 +413,18 @@ function filemanagerutil.showChooseDialog(title_header, caller_callback, current
},
})
end
if reset_button then
table.insert(buttons, {
{
text = _("Reset"),
enabled = current_path and true or false,
callback = function()
UIManager:close(dialog)
caller_callback()
end,
},
})
end
local title_value = path and (is_file and BD.filepath(path) or BD.dirpath(path))
or _("not set")
local ButtonDialog = require("ui/widget/buttondialog")
+54 -9
View File
@@ -7,6 +7,7 @@ It works using data gathered from a document interface.
local Archiver = require("ffi/archiver")
local BD = require("ui/bidi")
local BookList = require("ui/widget/booklist")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
local DeviceListener = require("device/devicelistener")
local DocCache = require("document/doccache")
@@ -125,7 +126,21 @@ function ReaderUI:init()
self.dialog = self
end
self.doc_settings = DocSettings:open(self.document.file)
local file = self.document.file
self.doc_settings = DocSettings:open(file)
if self.arc_settings_data then -- new book with archived metadata to be applied
local custom_props = self.arc_settings_data.metadata_arc.custom_props
if custom_props then
local custom_doc_settings = DocSettings.openSettingsFile()
custom_doc_settings.data.custom_props = custom_props
custom_doc_settings.data.doc_props = self.arc_settings_data.doc_props
custom_doc_settings:flushCustomMetadata(file)
end
self.arc_settings_data.metadata_arc = nil
self.arc_settings_data.doc_path = file
self.doc_settings.data = self.arc_settings_data
self.arc_settings_data = nil
end
self.document.is_new = self.doc_settings:readSetting("doc_props") == nil
-- Handle local settings migration
SettingsMigration:migrateSettings(self.doc_settings)
@@ -469,13 +484,14 @@ function ReaderUI:init()
local props = self.document:getProps()
self.doc_settings:saveSetting("doc_props", props)
-- And have an extended and customized copy in memory for quick access.
self.doc_props = FileManagerBookInfo.extendProps(props, self.document.file)
self.doc_props = FileManagerBookInfo.extendProps(props, file)
local md5 = self.doc_settings:readSetting("partial_md5_checksum")
if md5 == nil then
md5 = util.partialMD5(self.document.file)
md5 = self.md5_checksum or util.partialMD5(file)
self.doc_settings:saveSetting("partial_md5_checksum", md5)
end
self.md5_checksum = nil
local summary = self.doc_settings:readSetting("summary", {})
if BookList.getBookStatusString(summary.status) == nil then
@@ -484,7 +500,7 @@ function ReaderUI:init()
end
if summary.status ~= "complete" or not G_reader_settings:isTrue("history_freeze_finished_books") then
require("readhistory"):addItem(self.document.file) -- (will update "lastfile")
require("readhistory"):addItem(file) -- (will update "lastfile")
end
-- After initialisation notify that document is loaded and rendered
@@ -503,7 +519,7 @@ function ReaderUI:init()
self.after_open_callback = nil
end
BookList.setBookInfoCache(self.document.file, self.doc_settings)
BookList.setBookInfoCache(file, self.doc_settings)
Device:setIgnoreInput(false) -- Allow processing of events (on Android).
Input:inhibitInputUntil(0.2)
@@ -609,10 +625,38 @@ function ReaderUI:showReader(file, provider, seamless, is_provider_forced, after
provider = self:extendProvider(file, provider, is_provider_forced)
end
if provider and provider.provider then
self.after_open_callback = after_open_callback
-- We can now signal the existing ReaderUI/FileManager instances that it's time to go bye-bye...
UIManager:broadcastEvent(Event:new("ShowingReader"))
self:showReaderCoroutine(file, provider, seamless)
local function do_show(settings_file)
if settings_file then
os.remove(settings_file)
os.remove(settings_file .. ".old")
end
self.after_open_callback = after_open_callback
-- We can now signal the existing ReaderUI/FileManager instances that it's time to go bye-bye...
UIManager:broadcastEvent(Event:new("ShowingReader"))
self:showReaderCoroutine(file, provider, seamless)
end
if BookList.hasBookBeenOpened(file) then
do_show()
else -- new book
self.md5_checksum = util.partialMD5(file)
local arc_settings_file = DocSettings.getSettingsArcFile(self.md5_checksum, true) -- check if exists
if arc_settings_file then
UIManager:show(ConfirmBox:new{
text = _("The book has been read on this device earlier.\nDo you want to use book metadata from the archive?"),
ok_text = _("Use"),
ok_callback = function()
self.arc_settings_data = DocSettings.openSettingsFile(arc_settings_file).data
do_show(arc_settings_file)
end,
cancel_text = _("Don't use"),
cancel_callback = function()
do_show(arc_settings_file)
end,
})
else -- no arc settings file
do_show()
end
end
else
UIManager:show(InfoMessage:new{
text = T(_("File '%1' is not supported."), BD.filepath(filemanagerutil.abbreviate(file)))
@@ -791,6 +835,7 @@ end
function ReaderUI:saveSettings()
self:handleEvent(Event:new("SaveSettings"))
DocSettings.saveSettingsArcFile(self.doc_settings, nil, true)
self.doc_settings:flush()
G_reader_settings:flush()
end
+2
View File
@@ -62,6 +62,7 @@ local settingsList = {
favorites = {category="none", event="ShowColl", title=_("Favorites"), general=true},
collections = {category="none", event="ShowCollList", title=_("Collections"), general=true},
collections_search = {category="none", event="ShowCollectionsSearchDialog", title=_("Collections search"), general=true},
book_metadata_archive = {category="none", event="ShowBookMetadataArchive", title=_("Book metadata archive"), general=true},
bookmark_browser = {category="none", event="ShowBookmarkBrowser", title=_("Bookmark browser"), general=true, separator=true},
----
dictionary_lookup = {category="none", event="ShowDictionaryLookup", title=_("Dictionary lookup"), general=true},
@@ -319,6 +320,7 @@ local dispatcher_menu_order = {
"favorites",
"collections",
"collections_search",
"book_metadata_archive",
"bookmark_browser",
----
"dictionary_lookup",
+42
View File
@@ -463,6 +463,7 @@ function DocSettings.updateLocation(doc_path, new_doc_path, copy)
end
else -- delete
if has_sidecar_file then
DocSettings.saveSettingsArcFile(doc_settings, custom_metadata_file)
local cache_file_path = doc_settings:readSetting("cache_file_path")
if cache_file_path then
os.remove(cache_file_path)
@@ -605,4 +606,45 @@ function DocSettings.findSidecarFilesInHashLocation()
return res
end
function DocSettings.getSettingsArcFile(md5_checksum, does_exist)
local folder = G_reader_settings:readSetting("document_metadata_arc_folder")
if folder and md5_checksum then
local file = folder .. "/" .. md5_checksum .. ".lua"
if does_exist then
return file and isFile(file) and file
end
return file
end
end
function DocSettings.saveSettingsArcFile(doc_settings, custom_metadata_file, on_closing)
if on_closing then
if G_reader_settings:hasNot("document_metadata_arc_on_closing") then
return
end
custom_metadata_file = DocSettings:findCustomMetadataFile(doc_settings:readSetting("doc_path"))
end
local arc_file = DocSettings.getSettingsArcFile(doc_settings:readSetting("partial_md5_checksum"))
if not on_closing then -- on deletion, called by DocSettings.updateLocation()
if G_reader_settings:hasNot("document_metadata_arc_on_deletion") then
if arc_file and isFile(arc_file) then
-- arc file has been saved previously, but the checkbox is unchecked in the deletion dialog
os.remove(arc_file)
os.remove(arc_file .. ".old")
end
return
end
end
doc_settings.data.metadata_arc = {
datetime = os.date("%Y-%m-%d %H:%M:%S"),
on_closing = on_closing,
custom_props = custom_metadata_file
and DocSettings.openSettingsFile(custom_metadata_file):readSetting("custom_props"),
}
local arc_settings = LuaSettings:open(arc_file)
arc_settings.data = doc_settings.data
arc_settings:flush()
doc_settings.data.metadata_arc = nil
end
return DocSettings
@@ -8,6 +8,8 @@ local Language = require("ui/language")
local NetworkMgr = require("ui/network/manager")
local PowerD = Device:getPowerDevice()
local UIManager = require("ui/uimanager")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local util = require("util")
local _ = require("gettext")
local N_ = _.ngettext
local C_ = _.pgettext
@@ -17,7 +19,6 @@ local T = require("ffi/util").template
local common_settings = {}
if Device:isCervantes() then
local util = require("util")
if util.pathExists("/usr/bin/restart.sh") then
common_settings.start_bq = {
text = T(_("Start %1 reader app"), "BQ"),
@@ -689,6 +690,78 @@ common_settings.document_auto_save = {
separator = true,
}
local document_metadata_arc_help_text = _([[
Book metadata (view settings, bookmarks, notes) can be saved to the archive when deleting a book.
If a book is restored on the device, its archived metadata can be used.
Annotations of deleted books can be viewed in Bookmark browser.
To keep book metadata when deleting a book outside of KOReader, book metadata can be saved to archive on book closing.]])
common_settings.document_metadata_arc = {
text = _("Book metadata archive"),
sub_item_table = {
{
text = _("About"),
keep_menu_open = true,
callback = function()
UIManager:show(InfoMessage:new{ text = document_metadata_arc_help_text })
end,
},
{
text_func = function()
return T(_("Archive location: %1"),
G_reader_settings:readSetting("document_metadata_arc_folder") or _("not set"))
end,
keep_menu_open = true,
callback = function(touchmenu_instance)
local title_header = _("Book metadata archive folder:")
local current_path = G_reader_settings:readSetting("document_metadata_arc_folder")
local caller_callback = function(path)
local ok = true
if path and path ~= current_path then
local FileManager = require("apps/filemanager/filemanager")
util.findFiles(current_path, function(fullpath, filename)
if filename:match("%.lua$") then
ok = FileManager:moveFile(fullpath, path .. "/" .. filename)
elseif filename:match("%.old$") then
os.remove(fullpath)
end
end, false)
end
if ok then
G_reader_settings:saveSetting("document_metadata_arc_folder", path)
touchmenu_instance:updateItems()
end
end
filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, nil, nil, true)
end,
},
{
text = _("Save metadata to archive on book closing"),
enabled_func = function()
return G_reader_settings:has("document_metadata_arc_folder")
end,
checked_func = function()
return G_reader_settings:isTrue("document_metadata_arc_on_closing")
end,
callback = function()
G_reader_settings:flipNilOrFalse("document_metadata_arc_on_closing")
end,
separator = true,
},
{
text = _("Archived book list"),
enabled_func = function()
return G_reader_settings:has("document_metadata_arc_folder")
end,
callback = function()
FileManagerBookInfo:onShowBookMetadataArchive()
end,
},
},
separator = true,
}
common_settings.document_end_action = {
text = _("End of document action"),
sub_item_table = {
@@ -41,6 +41,7 @@ local order = {
"document_metadata_location",
"document_metadata_location_move",
"document_auto_save",
"document_metadata_arc",
"document_end_action",
"language_support",
},
@@ -87,6 +87,7 @@ local order = {
document = {
"document_metadata_location",
"document_auto_save",
"document_metadata_arc",
"document_end_action",
"language_support",
"----------------------------",
+36 -8
View File
@@ -4,6 +4,7 @@ local BookList = require("ui/widget/booklist")
local ButtonDialog = require("ui/widget/buttondialog")
local ButtonSelector = require("ui/widget/buttonselector")
local CheckButton = require("ui/widget/checkbutton")
local DocSettings = require("docsettings")
local DocumentRegistry = require("document/documentregistry")
local InfoMessage = require("ui/widget/infomessage")
local InputDialog = require("ui/widget/inputdialog")
@@ -103,6 +104,19 @@ function BookmarkBrowser:showSourceDialog(ui, force_show_dialog)
end)
end,
},
metadata_archive = {
text = _("Book metadata archive"),
enabled = G_reader_settings:has("document_metadata_arc_folder"),
callback = function()
fetch_and_show_bookmarks("metadata_archive", function(books)
util.findFiles(G_reader_settings:readSetting("document_metadata_arc_folder"), function(fullpath, filename)
if filename:match("%.lua$") then
books[fullpath] = true
end
end, false)
end)
end,
},
home_folder = {
text = _("Home folder"),
enabled = home_dir ~= nil,
@@ -204,6 +218,7 @@ function BookmarkBrowser:showSourceDialog(ui, force_show_dialog)
gen_source_button("history"),
gen_source_button("collections"),
gen_source_button("selected_files"),
gen_source_button("metadata_archive"),
gen_source_button("home_folder"),
gen_source_button("home_folder_subfolders"),
gen_source_button("folder"),
@@ -310,14 +325,19 @@ function BookmarkBrowser:getBookList(files)
local current_file = self.ui.document and self.ui.document.file
local books = {}
for file in pairs(files) do
local is_deleted, doc_settings, doc_props, annotations
local is_current_file = file == current_file or nil
local doc_settings, doc_props, annotations
if is_current_file then
doc_settings = self.ui.doc_settings
doc_props = self.ui.doc_props
annotations = self.ui.annotation.annotations
else
doc_settings = BookList.hasBookBeenOpened(file) and BookList.getDocSettings(file)
is_deleted = file:match("%.lua$")
if is_deleted then
doc_settings = DocSettings.openSettingsFile(file)
else
doc_settings = BookList.hasBookBeenOpened(file) and BookList.getDocSettings(file)
end
if doc_settings then
doc_props = doc_settings:readSetting("doc_props")
annotations = doc_settings:readSetting("annotations")
@@ -333,6 +353,7 @@ function BookmarkBrowser:getBookList(files)
enabled = true, -- start with showing all books from the source
file = file,
is_current_file = is_current_file,
is_deleted = is_deleted,
doc_settings = doc_settings,
doc_props = doc_props,
annotations = annotations,
@@ -368,7 +389,7 @@ function BookmarkBrowser:getItemTable()
local no_filter = self.filters_nb == 0
self.visible_books_nb = 0 -- may differ from the enabled books number due to bookmarks filtering
local item_table = {}
for __, book in ipairs(self.books) do
for self_books_idx, book in ipairs(self.books) do
if book.enabled then
local book_item_table_idx = #item_table + 1
local bookmark_idx = 0
@@ -413,12 +434,18 @@ function BookmarkBrowser:getItemTable()
if annotations_nb > 0 then
book.item_table_idx = book_item_table_idx
self.visible_books_nb = self.visible_books_nb + 1
local mandatory = "(" .. annotations_nb .. ")"
if book.is_deleted and book.doc_settings:readSetting("metadata_arc").on_closing then
mandatory = "\u{e28b} " .. mandatory
end
table.insert(item_table, { -- book entry
text = T(_("%1 • %2"), book.authors, book.doc_props.display_title),
bold = true,
mandatory = "(" .. annotations_nb .. ")",
mandatory = mandatory,
self_books_idx = self_books_idx,
file = book.file,
is_current_file = book.is_current_file,
is_deleted = book.is_deleted,
doc_props = book.doc_props,
doc_settings = book.doc_settings,
authors = book.authors,
@@ -460,6 +487,7 @@ function BookmarkBrowser:showBookDialog(item)
{
{
text = _("Open"),
enabled = not item.is_deleted,
callback = function()
UIManager:close(book_dialog)
self.bm_list:onClose()
@@ -475,8 +503,8 @@ function BookmarkBrowser:showBookDialog(item)
filemanagerutil.genBookInformationButton(item.doc_settings, item.doc_props, close_dialog_callback),
},
{
filemanagerutil.genBookCoverButton(file, item.doc_props, close_dialog_callback),
filemanagerutil.genBookDescriptionButton(file, item.doc_props, close_dialog_callback),
filemanagerutil.genBookCoverButton(file, item.doc_props, close_dialog_callback, item.is_deleted),
filemanagerutil.genBookDescriptionButton(file, item.doc_props, close_dialog_callback, item.is_deleted),
},
{}, -- separator
{
@@ -546,6 +574,7 @@ function BookmarkBrowser:showBookmarkDetails(item)
{
{
text = _("View in book"),
enabled = not book.is_deleted,
callback = function()
textviewer:onClose()
self.bm_list:onClose()
@@ -600,7 +629,6 @@ function BookmarkBrowser:showBookmarkListMenu()
{
text = are_disabled_books
and T(_("Books: %1 / %2"), self.filter_table.enabled_books_nb, #self.books) or _("Books"),
enabled = #self.books > 1,
callback = function()
UIManager:close(bm_list_menu)
self:showBookList(true)
@@ -930,7 +958,7 @@ end
function BookmarkBrowser:genShowBookListButton(caller_callback)
return {
text = _("Book list"),
enabled = self.visible_books_nb > 1,
enabled = self.visible_books_nb > 0,
callback = function()
caller_callback()
self:showBookList()
+152
View File
@@ -0,0 +1,152 @@
local BookList = require("ui/widget/booklist")
local ButtonDialog = require("ui/widget/buttondialog")
local ConfirmBox = require("ui/widget/confirmbox")
local DocSettings = require("docsettings")
local LuaSettings = require("luasettings")
local UIManager = require("ui/uimanager")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local ffiUtil = require("ffi/util")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local util = require("util")
local _ = require("gettext")
local T = ffiUtil.template
local BookMetadataArchive = WidgetContainer:extend{
}
function BookMetadataArchive:showBookList(ui)
self.ui = ui or require("apps/filemanager/filemanager").instance or require("apps/reader/readerui").instance
self.books = self.getBookList()
self.book_list = BookList:new{
onMenuSelect = function(self_menu, item)
self:showBookDialog(item)
end,
close_callback = function()
self.books = nil
self.book_list = nil
UIManager:scheduleIn(0.5, function()
collectgarbage()
collectgarbage()
end)
end,
}
self:updateBookList()
UIManager:show(self.book_list)
end
function BookMetadataArchive:updateBookList()
local title = _("Book metadata archive") .. " (" .. #self.books .. ")"
self.book_list:switchItemTable(title, self.books, -1)
end
function BookMetadataArchive.getBookList()
local book_list = {}
util.findFiles(G_reader_settings:readSetting("document_metadata_arc_folder"), function(fullpath, filename)
if filename:match("%.lua$") then
local doc_settings = LuaSettings:open(fullpath)
if doc_settings:has("metadata_arc") then
table.insert(book_list, BookMetadataArchive.buildItem(doc_settings))
end
end
end, false)
if #book_list > 1 then
table.sort(book_list, function(a, b) return ffiUtil.strcoll(a.text, b.text) end)
end
return book_list
end
function BookMetadataArchive.buildItem(doc_settings)
local doc_props = doc_settings:readSetting("doc_props")
local metadata_arc = doc_settings:readSetting("metadata_arc")
if metadata_arc.custom_props then
for prop_key, prop_value in pairs(metadata_arc.custom_props) do
doc_props[prop_key] = prop_value
end
end
doc_props.display_title = doc_props.title
or filemanagerutil.splitFileNameType(doc_settings:readSetting("doc_path"))
local authors = doc_props.authors and doc_props.authors:gsub("\n.*", " et al.") or _("Unknown author")
return {
text = T(_("%1 • %2"), authors, doc_props.display_title),
mandatory = BookMetadataArchive.getItemMandatory(doc_settings),
doc_settings = doc_settings,
}
end
function BookMetadataArchive.getItemMandatory(doc_settings)
local metadata_arc = doc_settings:readSetting("metadata_arc")
local t = {}
if metadata_arc.on_closing then
table.insert(t, "\u{e28b}") -- book
end
if #doc_settings:readSetting("annotations") > 0 then
table.insert(t, "\u{2592}") -- medium shade
end
table.insert(t, metadata_arc.datetime:sub(1, 10)) -- date
return table.concat(t, " ")
end
function BookMetadataArchive:showBookDialog(item)
local doc_settings = item.doc_settings
local metadata_arc = doc_settings:readSetting("metadata_arc")
local arc_file = DocSettings.getSettingsArcFile(doc_settings:readSetting("partial_md5_checksum"))
local book_deleted_button_text = _("Book deleted")
if not metadata_arc.on_closing then
book_deleted_button_text = book_deleted_button_text .. " \u{2713}" -- checkmark
end
local book_dialog
local function close_dialog_callback()
UIManager:close(book_dialog)
end
local buttons = {
{
{
text = _("Delete"),
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Delete metadata from archive?"),
ok_text = _("Delete"),
ok_callback = function()
UIManager:close(book_dialog)
os.remove(arc_file)
os.remove(arc_file .. ".old")
table.remove(self.books, item.idx)
self:updateBookList()
end,
})
end,
},
self.ui.collections:genBookmarkBrowserButton({ [arc_file] = true }, close_dialog_callback,
#doc_settings:readSetting("annotations") == 0),
},
{
{
text = book_deleted_button_text,
enabled = metadata_arc.on_closing or false,
callback = function()
UIManager:show(ConfirmBox:new{
text = _("Mark book as deleted?"),
ok_text = _("Mark"),
ok_callback = function()
UIManager:close(book_dialog)
metadata_arc.on_closing = nil
doc_settings:flush()
self.books[item.idx].mandatory = BookMetadataArchive.getItemMandatory(doc_settings)
self.book_list:updateItems(1, true)
end,
})
end,
},
filemanagerutil.genBookInformationButton(doc_settings, doc_settings:readSetting("doc_props"), close_dialog_callback),
},
}
book_dialog = ButtonDialog:new{
title = item.text:gsub(" • ", "\n"),
title_align = "center",
buttons = buttons,
}
UIManager:show(book_dialog)
end
return BookMetadataArchive