mirror of
https://github.com/koreader/koreader.git
synced 2026-11-29 00:31:12 +00:00
1215 lines
48 KiB
Lua
1215 lines
48 KiB
Lua
local BD = require("ui/bidi")
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
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 ConfirmBox = require("ui/widget/confirmbox")
|
|
local DocSettings = require("docsettings")
|
|
local DocumentRegistry = require("document/documentregistry")
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
local LuaSettings = require("luasettings")
|
|
local Menu = require("ui/widget/menu")
|
|
local PathChooser = require("ui/widget/pathchooser")
|
|
local ReaderAnnotation = require("apps/reader/modules/readerannotation")
|
|
local ReaderBookmark = require("apps/reader/modules/readerbookmark")
|
|
local ReaderHighlight = require("apps/reader/modules/readerhighlight")
|
|
local TextViewer = require("ui/widget/textviewer")
|
|
local UIManager = require("ui/uimanager")
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
local ffiUtil = require("ffi/util")
|
|
local filemanagerutil = require("apps/filemanager/filemanagerutil")
|
|
local lfs = require("libs/libkoreader-lfs")
|
|
local util = require("util")
|
|
local _ = require("gettext")
|
|
local Screen = require("device").screen
|
|
local T = ffiUtil.template
|
|
|
|
local BookmarkBrowser = WidgetContainer:extend{
|
|
display_prefix = ReaderBookmark.display_prefix,
|
|
display_type = ReaderBookmark.display_type,
|
|
separator = ReaderBookmark.separator,
|
|
checkmark = "\u{2713}",
|
|
}
|
|
|
|
function BookmarkBrowser:showSourceDialog(ui, force_show_dialog)
|
|
self.ui = ui
|
|
local home_dir = ui.folder_shortcuts:expandPath("%H%")
|
|
local default_source = G_reader_settings:readSetting("bookmarks_browser_source")
|
|
local source_dialog
|
|
|
|
local function fetch_and_show_bookmarks(self_source_key, fetch_func_or_folder, subfolders)
|
|
UIManager:close(source_dialog)
|
|
UIManager:show(InfoMessage:new{
|
|
text = _("Fetching bookmarks…"),
|
|
timeout = 0.1,
|
|
})
|
|
UIManager:nextTick(function()
|
|
local books = {}
|
|
if type(fetch_func_or_folder) == "function" then
|
|
fetch_func_or_folder(books)
|
|
else
|
|
util.findFiles(fetch_func_or_folder, function(file)
|
|
books[file] = DocumentRegistry:hasProvider(file) or nil
|
|
end, subfolders)
|
|
end
|
|
if self.bm_list then
|
|
self.bm_list:onClose()
|
|
end
|
|
self.source_key = self_source_key
|
|
self:show(books)
|
|
end)
|
|
end
|
|
|
|
local sources = {
|
|
history = {
|
|
text = _("History"),
|
|
callback = function()
|
|
fetch_and_show_bookmarks("history", function(books)
|
|
for _, v in ipairs(require("readhistory").hist) do
|
|
books[v.file] = v.select_enabled or nil
|
|
end
|
|
end)
|
|
end,
|
|
},
|
|
collections = {
|
|
text = _("Collections"),
|
|
callback = function(saved_collections)
|
|
local caller_callback = function(selected_collections)
|
|
if default_source and default_source.collections then
|
|
default_source.collections = selected_collections
|
|
end
|
|
fetch_and_show_bookmarks("collections", function(books)
|
|
for coll_name in pairs(selected_collections) do
|
|
for file in pairs(require("readcollection").coll[coll_name]) do
|
|
books[file] = true
|
|
end
|
|
end
|
|
end)
|
|
end
|
|
if saved_collections and saved_collections ~= true then
|
|
caller_callback(saved_collections)
|
|
else
|
|
ui.collections:onShowCollList({}, caller_callback, true)
|
|
end
|
|
end,
|
|
},
|
|
selected_files = {
|
|
text = _("Selected files"),
|
|
enabled = ui.selected_files ~= nil,
|
|
callback = function()
|
|
fetch_and_show_bookmarks("selected_files", function(books)
|
|
for file in pairs(ui.selected_files) do
|
|
books[file] = true
|
|
end
|
|
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,
|
|
callback = function()
|
|
fetch_and_show_bookmarks("home_folder", home_dir, false)
|
|
end,
|
|
},
|
|
home_folder_subfolders = {
|
|
text = _("Home folder with subfolders"),
|
|
enabled = home_dir ~= nil,
|
|
callback = function()
|
|
fetch_and_show_bookmarks("home_folder_subfolders", home_dir, true)
|
|
end,
|
|
},
|
|
folder = {
|
|
text = _("Folder"),
|
|
callback = function(saved_folder)
|
|
if saved_folder and saved_folder ~= true then
|
|
fetch_and_show_bookmarks("folder", saved_folder, false)
|
|
else
|
|
UIManager:show(PathChooser:new{
|
|
select_file = false,
|
|
path = home_dir,
|
|
onConfirm = function(path)
|
|
if default_source and default_source.folder then
|
|
default_source.folder = path
|
|
end
|
|
fetch_and_show_bookmarks("folder", path, false)
|
|
end,
|
|
})
|
|
end
|
|
end,
|
|
},
|
|
folder_subfolders = {
|
|
text = _("Folder with subfolders"),
|
|
callback = function(saved_folder)
|
|
if saved_folder and saved_folder ~= true then
|
|
fetch_and_show_bookmarks("folder_subfolders", saved_folder, true)
|
|
else
|
|
UIManager:show(PathChooser:new{
|
|
select_file = false,
|
|
path = home_dir,
|
|
onConfirm = function(path)
|
|
if default_source and default_source.folder_subfolders then
|
|
default_source.folder_subfolders = path
|
|
end
|
|
fetch_and_show_bookmarks("folder_subfolders", path, true)
|
|
end,
|
|
})
|
|
end
|
|
end,
|
|
},
|
|
}
|
|
|
|
if not force_show_dialog and default_source then
|
|
local default_source_key, default_source_value = next(default_source)
|
|
if sources[default_source_key].enabled ~= false then
|
|
-- show bookmarks immediately, without source dialog
|
|
sources[default_source_key].callback(default_source_value)
|
|
return
|
|
end
|
|
end
|
|
|
|
-- show source dialog
|
|
local function gen_source_button(source_key)
|
|
local text = sources[source_key].text
|
|
if source_key == self.source_key then
|
|
text = text .. " " .. self.checkmark
|
|
end
|
|
local is_default_source_key = default_source and default_source[source_key]
|
|
if is_default_source_key then
|
|
text = text .. " ★"
|
|
end
|
|
return {{
|
|
text = text,
|
|
enabled = sources[source_key].enabled,
|
|
callback = function() -- fetch and show bookmark list
|
|
sources[source_key].callback()
|
|
end,
|
|
hold_callback = function() -- toggle default source
|
|
if is_default_source_key then
|
|
G_reader_settings:delSetting("bookmarks_browser_source")
|
|
else
|
|
-- for selected_collections/folder: the callback will choose and save it instead of "true"
|
|
G_reader_settings:saveSetting("bookmarks_browser_source", { [source_key] = true })
|
|
end
|
|
UIManager:close(source_dialog)
|
|
self:showSourceDialog(ui, true)
|
|
end,
|
|
}}
|
|
end
|
|
source_dialog = ButtonDialog:new{
|
|
title = _("Book source"),
|
|
title_align = "center",
|
|
width_factor = 0.8,
|
|
buttons = {
|
|
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"),
|
|
gen_source_button("folder_subfolders"),
|
|
},
|
|
}
|
|
UIManager:show(source_dialog)
|
|
end
|
|
|
|
function BookmarkBrowser:show(files, ui)
|
|
self.ui = ui or self.ui
|
|
self.ui.highlight = self.ui.highlight or ReaderHighlight -- for ReaderBookmark methods
|
|
|
|
self.items_per_page = G_reader_settings:readSetting("bookmarks_items_per_page")
|
|
or G_reader_settings:readSetting("items_per_page") or Menu.items_per_page_default
|
|
self.items_font_size = G_reader_settings:readSetting("bookmarks_items_font_size")
|
|
or G_reader_settings:readSetting("items_font_size") or Menu.getItemFontSize(self.items_per_page)
|
|
self.items_text = G_reader_settings:readSetting("bookmarks_items_text_type") or "note"
|
|
self.items_max_lines = G_reader_settings:readSetting("bookmarks_items_max_lines")
|
|
self.multilines_show_more_text = G_reader_settings:isTrue("bookmarks_items_multilines_show_more_text")
|
|
self.line_color = G_reader_settings:isTrue("bookmarks_items_show_separator")
|
|
and Blitbuffer.COLOR_DARK_GRAY or Blitbuffer.COLOR_WHITE
|
|
self.sorting_mode = G_reader_settings:readSetting("bookmarks_items_sorting") or "page"
|
|
self.is_reverse_sorting = G_reader_settings:isTrue("bookmarks_items_reverse_sorting")
|
|
self.hl_color_name = G_reader_settings:readSetting("highlight_color")
|
|
or (Screen:isColorEnabled() and "yellow" or "gray") -- see ReaderView
|
|
self.hl_color = ReaderHighlight:getHighlightColor(self.hl_color_name)
|
|
self.bookmarks_items_show_color = G_reader_settings:isTrue("bookmarks_items_show_color")
|
|
self.bookmarks_items_show_color_default = G_reader_settings:isTrue("bookmarks_items_show_color_default") or nil
|
|
|
|
self.books = self:getBookList(files)
|
|
self.books_updated = {}
|
|
self.filter_table = G_reader_settings:readSetting("bookmarks_browser_filter") or {}
|
|
self.filter_table.enabled_books_nb = #self.books
|
|
self.bm_list = Menu:new{
|
|
subtitle = "",
|
|
is_borderless = true,
|
|
is_popout = false,
|
|
title_bar_fm_style = true,
|
|
items_per_page = self.items_per_page,
|
|
items_font_size = self.items_font_size,
|
|
items_max_lines = self.items_max_lines,
|
|
multilines_show_more_text = self.multilines_show_more_text,
|
|
line_color = self.line_color,
|
|
title_bar_left_icon = "appbar.menu",
|
|
onLeftButtonTap = function()
|
|
self:showBookmarkListMenu()
|
|
end,
|
|
onMenuSelect = function(self_menu, item)
|
|
if item.self_books_idx then
|
|
self:showBookDialog(item)
|
|
else
|
|
self:showBookmarkDetails(item)
|
|
end
|
|
end,
|
|
search_callback = function(search_string)
|
|
self:showSearchDialog(search_string)
|
|
end,
|
|
close_callback = function()
|
|
self:updateBooks()
|
|
self.books = nil
|
|
self.books_updated = nil
|
|
self.source_key = nil
|
|
if self.filter_table.type or self.filter_table.style or self.filter_table.color then
|
|
G_reader_settings:saveSetting("bookmarks_browser_filter", {
|
|
type = self.filter_table.type,
|
|
style = self.filter_table.style,
|
|
color = self.filter_table.color,
|
|
})
|
|
else
|
|
G_reader_settings:delSetting("bookmarks_browser_filter")
|
|
end
|
|
self.filter_table = nil
|
|
self.bm_list = nil
|
|
UIManager:scheduleIn(0.5, function()
|
|
collectgarbage()
|
|
collectgarbage()
|
|
end)
|
|
end,
|
|
}
|
|
self:updateBookmarkList()
|
|
UIManager:show(self.bm_list)
|
|
end
|
|
|
|
function BookmarkBrowser:updateBookmarkList(item_number)
|
|
local item_table, title, subtitle
|
|
if item_number == nil or item_number == -1 then
|
|
item_table = self:getItemTable()
|
|
title = T(_("Books: %1"), self.visible_books_nb) .. self.separator ..
|
|
T(_("Bookmarks: %1"), #item_table - self.visible_books_nb)
|
|
if self.filters_nb > 0 then
|
|
if self.filters_nb > 1 then
|
|
subtitle = T(_("Filters: %1"), self.filters_nb)
|
|
else -- single filter
|
|
if self.filter_table.enabled_books_nb < #self.books then
|
|
subtitle = _("Filtered by book")
|
|
elseif self.filter_table.search_string then
|
|
subtitle = T(_("Query: %1"), self.filter_table.search_string)
|
|
elseif self.filter_table.type then
|
|
subtitle = T(_("Type: %1"), self.display_type[self.filter_table.type])
|
|
elseif self.filter_table.style then
|
|
subtitle = T(_("Style: %1"), ReaderHighlight:getHighlightStyleString(self.filter_table.style))
|
|
elseif self.filter_table.color then
|
|
subtitle = T(_("Color: %1"), ReaderHighlight:getHighlightColorString(self.filter_table.color))
|
|
end
|
|
end
|
|
else
|
|
subtitle = ""
|
|
end
|
|
end
|
|
self.bm_list:switchItemTable(title, item_table, item_number, nil, subtitle)
|
|
end
|
|
|
|
function BookmarkBrowser:getBookList(files)
|
|
-- builds list of books that have been opened, have doc_props, have annotations
|
|
-- sorted by combined title "Authors • Title"
|
|
local current_file = self.ui.document and self.ui.document.file
|
|
local books = {}
|
|
for file in pairs(files) do
|
|
local is_current_file, is_archived, is_deleted, is_locked, doc_path, doc_settings, doc_props, annotations
|
|
local file_ext = file:sub(-3)
|
|
if file_ext == "lua" then -- BookMetadataArchive
|
|
is_archived = true
|
|
doc_settings = DocSettings.openSettingsFile(file) -- cannot flush
|
|
doc_props = doc_settings:readSetting("doc_props")
|
|
annotations = doc_settings:readSetting("annotations")
|
|
doc_path = doc_settings:readSetting("doc_path")
|
|
is_deleted = not lfs.attributes(doc_path)
|
|
else -- document
|
|
is_current_file = current_file == file
|
|
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)
|
|
if doc_settings then
|
|
doc_props = doc_settings:readSetting("doc_props")
|
|
annotations = doc_settings:readSetting("annotations")
|
|
if file_ext == "pdf" then
|
|
is_locked = doc_settings:readSetting("highlight_write_into_pdf")
|
|
if is_locked == nil then
|
|
is_locked = G_reader_settings:readSetting("highlight_write_into_pdf")
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
if annotations and #annotations > 0 then
|
|
local hl_color_name = doc_settings:readSetting("highlight_color")
|
|
if not is_current_file then
|
|
doc_props = self.ui.bookinfo.extendProps(doc_props, doc_path or file)
|
|
end
|
|
doc_props.has_cover = true -- enable "Book cover" button in the book dialog
|
|
table.insert(books, { -- self.books entry
|
|
enabled = true, -- start with showing all books from the source
|
|
file = file,
|
|
doc_path = doc_path,
|
|
is_current_file = is_current_file,
|
|
is_archived = is_archived,
|
|
is_deleted = is_deleted,
|
|
is_locked = is_locked,
|
|
doc_settings = doc_settings,
|
|
doc_props = doc_props,
|
|
annotations = annotations,
|
|
hl_color_name = hl_color_name,
|
|
hl_color = hl_color_name and ReaderHighlight:getHighlightColor(hl_color_name),
|
|
text = util.stringLower((doc_props.authors or "") .. doc_props.display_title), -- for sorting
|
|
-- will be filled when building item table
|
|
item_table_idx = nil,
|
|
bookmarks_nb = nil, -- filtered
|
|
})
|
|
end
|
|
end
|
|
if #books > 1 then
|
|
table.sort(books, BookList.getCollateSortFunc())
|
|
end
|
|
return books
|
|
end
|
|
|
|
function BookmarkBrowser:getFiltersNumber()
|
|
return (self.filter_table.enabled_books_nb < #self.books and 1 or 0)
|
|
+ (self.filter_table.search_string and 1 or 0)
|
|
+ (self.filter_table.type and 1 or 0)
|
|
+ (self.filter_table.style and 1 or 0)
|
|
+ (self.filter_table.color and 1 or 0)
|
|
end
|
|
|
|
function BookmarkBrowser:getItemColor(book, anno)
|
|
if anno.drawer and self.bookmarks_items_show_color then
|
|
if anno.color == nil then
|
|
return self.bookmarks_items_show_color_default and (book.hl_color or self.hl_color)
|
|
elseif anno.color == book.hl_color_name then
|
|
return self.bookmarks_items_show_color_default and book.hl_color
|
|
elseif anno.color == self.hl_color_name then
|
|
return self.bookmarks_items_show_color_default and self.hl_color
|
|
else
|
|
return ReaderHighlight:getHighlightColor(anno.color)
|
|
end
|
|
end
|
|
end
|
|
|
|
function BookmarkBrowser:getItemTable()
|
|
local date_sort_func
|
|
if self.sorting_mode == "date" then
|
|
date_sort_func = self.is_reverse_sorting and function(a, b) return a.datetime > b.datetime end
|
|
or function(a, b) return a.datetime < b.datetime end
|
|
end
|
|
self.filters_nb = self:getFiltersNumber()
|
|
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 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
|
|
local annotations_nb = #book.annotations
|
|
local num = annotations_nb + 1
|
|
local book_item_table = {}
|
|
for i = 1, annotations_nb do
|
|
local a = book.annotations[self.is_reverse_sorting and num - i or i]
|
|
local a_type = ReaderBookmark.getBookmarkType(a)
|
|
if no_filter or self:doesBookmarkMatch(a, a_type) then
|
|
bookmark_idx = bookmark_idx + 1
|
|
local item = { -- item_table boookmark entry
|
|
text = nil,
|
|
text_bgcolor = self:getItemColor(book, a),
|
|
mandatory = a.pageref or a.pageno,
|
|
datetime = a.datetime,
|
|
text_orig = a.text or "",
|
|
chapter = a.chapter,
|
|
note = a.note,
|
|
drawer = a.drawer,
|
|
color = a.color,
|
|
type = a_type,
|
|
bookmark_idx = bookmark_idx,
|
|
book_idx = book_item_table_idx,
|
|
anno_idx = i,
|
|
}
|
|
item.text = ReaderBookmark.getBookmarkItemText(self, item)
|
|
table.insert(book_item_table, item)
|
|
end
|
|
end
|
|
local bookmarks_nb = #book_item_table
|
|
book.bookmarks_nb = bookmarks_nb
|
|
if bookmarks_nb > 0 then
|
|
book.item_table_idx = book_item_table_idx
|
|
self.visible_books_nb = self.visible_books_nb + 1
|
|
local mandatory = "(" .. bookmarks_nb .. ")"
|
|
if book.is_archived and not book.is_deleted then
|
|
mandatory = "\u{e28b} " .. mandatory -- 'book open'
|
|
end
|
|
table.insert(item_table, { -- item_table book entry
|
|
text = self.ui.bookinfo.combineAuthorsTitle(book.doc_props.authors, book.doc_props.display_title),
|
|
bold = true,
|
|
mandatory = mandatory,
|
|
self_books_idx = self_books_idx,
|
|
})
|
|
if date_sort_func and bookmarks_nb > 1 then
|
|
table.sort(book_item_table, date_sort_func)
|
|
end
|
|
table.move(book_item_table, 1, bookmarks_nb, #item_table + 1, item_table)
|
|
end
|
|
end
|
|
end
|
|
return item_table
|
|
end
|
|
|
|
function BookmarkBrowser:doesBookmarkMatch(a, a_type)
|
|
if self.filter_table.type and self.filter_table.type ~= a_type then return end
|
|
if self.filter_table.style and self.filter_table.style ~= a.drawer then return end
|
|
if self.filter_table.color and self.filter_table.color ~= a.color then return end
|
|
if self.filter_table.search_string then
|
|
local text = a.text or ""
|
|
if a.note then
|
|
text = text .. "\u{FFFF}" .. a.note
|
|
end
|
|
if util.stringSearch(text, self.filter_table.search_string, self.filter_table.search_case_sensitive) == 0 then
|
|
return
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
function BookmarkBrowser:showBookDialog(item)
|
|
local book = self.books[item.self_books_idx]
|
|
local file = book.doc_path or book.file
|
|
local book_dialog
|
|
local function close_dialog_callback()
|
|
UIManager:close(book_dialog)
|
|
end
|
|
local buttons = {
|
|
{
|
|
{
|
|
text = _("Open"),
|
|
enabled = not book.is_archived,
|
|
callback = function()
|
|
UIManager:close(book_dialog)
|
|
self.bm_list:onClose()
|
|
if self.ui.document then
|
|
if not book.is_current_file then
|
|
self.ui:switchDocument(file)
|
|
end
|
|
else
|
|
self.ui:openFile(file)
|
|
end
|
|
end,
|
|
},
|
|
filemanagerutil.genBookInformationButton(book.doc_settings, book.doc_props, close_dialog_callback),
|
|
},
|
|
{
|
|
filemanagerutil.genBookCoverButton(file, book.doc_props, close_dialog_callback, book.is_deleted),
|
|
filemanagerutil.genBookDescriptionButton(file, book.doc_props, close_dialog_callback),
|
|
},
|
|
{}, -- separator
|
|
{
|
|
self:genShowBookListButton(close_dialog_callback),
|
|
},
|
|
}
|
|
book_dialog = ButtonDialog:new{
|
|
title = self.ui.bookinfo.combineAuthorsTitle(book.doc_props.authors, book.doc_props.display_title, true),
|
|
title_align = "center",
|
|
buttons = buttons,
|
|
}
|
|
UIManager:show(book_dialog)
|
|
end
|
|
|
|
function BookmarkBrowser:showBookmarkDetails(item)
|
|
local item_table = self.bm_list.item_table
|
|
local items_nb = #item_table
|
|
local book = self.books[item_table[item.book_idx].self_books_idx]
|
|
local anno = book.annotations[item.anno_idx]
|
|
|
|
local textviewer
|
|
local function _showBookmarkDetails(idx)
|
|
UIManager:close(textviewer)
|
|
self:updateBookmarkList(idx)
|
|
self:showBookmarkDetails(item_table[idx])
|
|
end
|
|
local function _showPrevBookmarkDetails()
|
|
if item.idx > 2 then
|
|
local idx = item.idx - 1
|
|
if item_table[idx].self_books_idx then -- book
|
|
idx = idx - 1
|
|
end
|
|
_showBookmarkDetails(idx)
|
|
end
|
|
end
|
|
local function _showNextBookmarkDetails()
|
|
if item.idx < items_nb then
|
|
local idx = item.idx + 1
|
|
if item_table[idx].self_books_idx then -- book
|
|
idx = idx + 1
|
|
end
|
|
_showBookmarkDetails(idx)
|
|
end
|
|
end
|
|
|
|
local label_prev, label_next, label_first, label_last = BD.getArrowLabels()
|
|
local buttons_table = {
|
|
{
|
|
{
|
|
text = label_first,
|
|
enabled = item.idx > 2,
|
|
callback = function()
|
|
_showBookmarkDetails(2)
|
|
end,
|
|
},
|
|
{
|
|
text = label_prev,
|
|
enabled = item.idx > 2,
|
|
callback = _showPrevBookmarkDetails,
|
|
},
|
|
{
|
|
text = label_next,
|
|
enabled = item.idx < items_nb,
|
|
callback = _showNextBookmarkDetails,
|
|
},
|
|
{
|
|
text = label_last,
|
|
enabled = item.idx < items_nb,
|
|
callback = function()
|
|
_showBookmarkDetails(items_nb)
|
|
end,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
text = "\u{F48E}",
|
|
enabled = not book.is_locked,
|
|
callback = function()
|
|
self:removeBookmark(textviewer, item)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Style"),
|
|
enabled = not book.is_locked,
|
|
callback = function()
|
|
self:editBookmarkStyleColor(textviewer, item)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Color"),
|
|
enabled = not book.is_locked,
|
|
callback = function()
|
|
self:editBookmarkStyleColor(textviewer, item, true)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Note"),
|
|
enabled = not book.is_locked,
|
|
callback = function()
|
|
self:editBookmarkNote(textviewer, item)
|
|
end,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
text = _("View in book"),
|
|
enabled = not book.is_archived,
|
|
callback = function()
|
|
textviewer:onClose()
|
|
self.bm_list:onClose()
|
|
local after_open_callback = function(ui)
|
|
ui.link:addCurrentLocationToStack()
|
|
ui.bookmark:gotoBookmark(anno.page, anno.pos0)
|
|
end
|
|
if self.ui.document then
|
|
if book.is_current_file then
|
|
after_open_callback(self.ui)
|
|
else
|
|
self.ui:switchDocument(book.file, nil, after_open_callback)
|
|
end
|
|
else
|
|
self.ui:openFile(book.file, nil, nil, nil, after_open_callback)
|
|
end
|
|
end,
|
|
},
|
|
{
|
|
text = _("Close"),
|
|
callback = function()
|
|
textviewer:onClose()
|
|
end,
|
|
},
|
|
},
|
|
}
|
|
|
|
textviewer = TextViewer:new{
|
|
title = T(_("%1 / %2"), item.bookmark_idx, book.bookmarks_nb),
|
|
text = ReaderBookmark.getBookmarkDetailsText(self, item, book),
|
|
text_type = "bookmark",
|
|
buttons_table = buttons_table,
|
|
page_turn_callback_prev = _showPrevBookmarkDetails,
|
|
page_turn_callback_next = _showNextBookmarkDetails,
|
|
}
|
|
UIManager:show(textviewer)
|
|
end
|
|
|
|
function BookmarkBrowser:editBookmarkStyleColor(textviewer, item, is_color)
|
|
local book = self.books[self.bm_list.item_table[item.book_idx].self_books_idx]
|
|
local anno = book.annotations[item.anno_idx]
|
|
local mode = is_color and "color" or "drawer"
|
|
UIManager:show(ButtonSelector:new{
|
|
current_value = anno[mode],
|
|
values = is_color and ReaderHighlight:getHighlightColorList() or ReaderHighlight.getHighlightStyles(),
|
|
callback = function(value)
|
|
anno[mode] = value
|
|
if book.is_current_file then
|
|
self.ui.highlight:applyHighlightChanges(anno)
|
|
else
|
|
ReaderAnnotation:onAnnotationsModified({ anno })
|
|
self.books_updated[book] = self.books_updated[book] or 0
|
|
end
|
|
UIManager:close(textviewer)
|
|
if self.filter_table[is_color and "color" or "style"] then
|
|
self:updateBookmarkList(-1)
|
|
else
|
|
item[mode] = value
|
|
if is_color then
|
|
item.text_bgcolor = self:getItemColor(book, anno)
|
|
self:updateBookmarkList(item.idx)
|
|
end
|
|
self:showBookmarkDetails(item)
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
|
|
function BookmarkBrowser:editBookmarkNote(textviewer, item)
|
|
local book = self.books[self.bm_list.item_table[item.book_idx].self_books_idx]
|
|
local anno = book.annotations[item.anno_idx]
|
|
|
|
local function _updateBookmarkList()
|
|
UIManager:close(textviewer)
|
|
local new_type = ReaderBookmark.getBookmarkType(anno)
|
|
local type_changed = item.type ~= new_type
|
|
if self.filter_table.type and type_changed then
|
|
self:updateBookmarkList(-1)
|
|
else
|
|
item.type = new_type
|
|
item.note = anno.note
|
|
item.text = ReaderBookmark.getBookmarkItemText(self, item)
|
|
self:updateBookmarkList(item.idx)
|
|
self:showBookmarkDetails(item)
|
|
end
|
|
if not book.is_current_file then
|
|
ReaderAnnotation:onAnnotationsModified({ anno })
|
|
self.books_updated[book] = (self.books_updated[book] or 0) + (type_changed and 1 or 0)
|
|
end
|
|
end
|
|
|
|
if book.is_current_file then
|
|
local caller_callback = function()
|
|
if self.ui.view.highlight.note_mark then -- refresh note marker
|
|
UIManager:setDirty(self.ui.dialog, "ui")
|
|
end
|
|
_updateBookmarkList()
|
|
end
|
|
self.ui.bookmark:setBookmarkNote(item.anno_idx, nil, nil, caller_callback)
|
|
else
|
|
local input_dialog
|
|
input_dialog = InputDialog:new{
|
|
title = _("Edit note"),
|
|
description = " " .. ReaderBookmark._getDialogHeader(self, item),
|
|
input = item.note,
|
|
allow_newline = true,
|
|
add_scroll_buttons = true,
|
|
use_available_height = true,
|
|
buttons = {
|
|
{
|
|
{
|
|
text = _("Cancel"),
|
|
id = "close",
|
|
callback = function()
|
|
UIManager:close(input_dialog)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Paste"), -- insert highlighted text
|
|
enabled = anno.text ~= nil,
|
|
callback = function()
|
|
input_dialog:addTextToInput(anno.text)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Save"),
|
|
is_enter_default = true,
|
|
callback = function()
|
|
local value = input_dialog:getInputText()
|
|
value = value ~= "" and value or nil -- blank input deletes note
|
|
UIManager:close(input_dialog)
|
|
anno.note = value
|
|
_updateBookmarkList()
|
|
end,
|
|
},
|
|
}
|
|
},
|
|
}
|
|
UIManager:show(input_dialog)
|
|
input_dialog:onShowKeyboard()
|
|
end
|
|
end
|
|
|
|
function BookmarkBrowser:removeBookmark(textviewer, item)
|
|
local book = self.books[self.bm_list.item_table[item.book_idx].self_books_idx]
|
|
local anno = book.annotations[item.anno_idx]
|
|
UIManager:show(ConfirmBox:new{
|
|
text = _("Remove this bookmark?"),
|
|
ok_text = _("Remove"),
|
|
ok_callback = function()
|
|
if book.is_current_file then
|
|
self.ui.bookmark:removeItem(anno, item.anno_idx)
|
|
else
|
|
table.remove(book.annotations, item.anno_idx)
|
|
self.books_updated[book] = (self.books_updated[book] or 0) + 1
|
|
end
|
|
UIManager:close(textviewer)
|
|
self:updateBookmarkList(-1)
|
|
end,
|
|
})
|
|
end
|
|
|
|
function BookmarkBrowser:showBookmarkListMenu()
|
|
local are_disabled_books = self.filter_table.enabled_books_nb < #self.books
|
|
local bm_list_menu
|
|
local function close_dialog_callback()
|
|
UIManager:close(bm_list_menu)
|
|
end
|
|
local function reset_filter(filter)
|
|
if self.filter_table[filter] then
|
|
UIManager:close(bm_list_menu)
|
|
self.filter_table[filter] = nil
|
|
self:updateBookmarkList()
|
|
end
|
|
end
|
|
local buttons = {
|
|
{
|
|
{
|
|
text = are_disabled_books
|
|
and T(_("Books: %1 / %2"), self.filter_table.enabled_books_nb, #self.books) or _("Books"),
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
self:showBookList(true)
|
|
end,
|
|
hold_callback = function()
|
|
if are_disabled_books then
|
|
UIManager:close(bm_list_menu)
|
|
for _, book in ipairs(self.books) do
|
|
book.enabled = true
|
|
end
|
|
self.filter_table.enabled_books_nb = #self.books
|
|
self:updateBookmarkList()
|
|
end
|
|
end,
|
|
},
|
|
{
|
|
text = self.filter_table.style
|
|
and T(_("Style: %1"), ReaderHighlight:getHighlightStyleString(self.filter_table.style)) or _("Style"),
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
UIManager:show(ButtonSelector:new{
|
|
current_value = self.filter_table.style,
|
|
values = ReaderHighlight.getHighlightStyles(),
|
|
callback = function(value)
|
|
self.filter_table.style = value
|
|
self:updateBookmarkList()
|
|
end,
|
|
})
|
|
end,
|
|
hold_callback = function()
|
|
reset_filter("style")
|
|
end,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
text = self.filter_table.type
|
|
and T(_("Type: %1"), self.display_type[self.filter_table.type]) or _("Type"),
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
UIManager:show(ButtonSelector:new{
|
|
current_value = self.filter_table.type,
|
|
values = {
|
|
{ self.display_type.highlight, "highlight" },
|
|
{ self.display_type.note, "note" },
|
|
{ self.display_type.bookmark, "bookmark" },
|
|
},
|
|
callback = function(value)
|
|
self.filter_table.type = value
|
|
self:updateBookmarkList()
|
|
end,
|
|
})
|
|
end,
|
|
hold_callback = function()
|
|
reset_filter("type")
|
|
end,
|
|
},
|
|
{
|
|
text = self.filter_table.color
|
|
and T(_("Color: %1"), ReaderHighlight:getHighlightColorString(self.filter_table.color)) or _("Color"),
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
UIManager:show(ButtonSelector:new{
|
|
current_value = self.filter_table.color,
|
|
values = ReaderHighlight:getHighlightColorList(),
|
|
callback = function(selected_color_name)
|
|
self.filter_table.color = selected_color_name
|
|
self:updateBookmarkList()
|
|
end,
|
|
})
|
|
end,
|
|
hold_callback = function()
|
|
reset_filter("color")
|
|
end,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
text = self.filter_table.search_string
|
|
and T(_("Search: %1"), self.filter_table.search_string) or _("Search"),
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
self:showSearchDialog()
|
|
end,
|
|
hold_callback = function()
|
|
if self.filter_table.search_string then
|
|
UIManager:close(bm_list_menu)
|
|
self.filter_table.search_string = nil
|
|
self.filter_table.search_case_sensitive = nil
|
|
self:updateBookmarkList()
|
|
end
|
|
end,
|
|
},
|
|
},
|
|
{
|
|
{
|
|
text = _("Reset all filters"),
|
|
enabled = self.filters_nb > 0,
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
if are_disabled_books then
|
|
for _, book in ipairs(self.books) do
|
|
book.enabled = true
|
|
end
|
|
end
|
|
self.filter_table = { enabled_books_nb = #self.books }
|
|
self:updateBookmarkList()
|
|
end,
|
|
},
|
|
},
|
|
{}, -- separator
|
|
{
|
|
self:genShowBookListButton(close_dialog_callback),
|
|
},
|
|
{
|
|
{
|
|
text = _("Book source"),
|
|
callback = function()
|
|
UIManager:close(bm_list_menu)
|
|
self:showSourceDialog(self.ui, true)
|
|
end,
|
|
},
|
|
},
|
|
}
|
|
bm_list_menu = ButtonDialog:new{
|
|
title = _("Filters"),
|
|
title_align = "center",
|
|
buttons = buttons,
|
|
}
|
|
UIManager:show(bm_list_menu)
|
|
end
|
|
|
|
function BookmarkBrowser:genShowBookListButton(caller_callback)
|
|
return {
|
|
text = _("Book list"),
|
|
enabled = self.visible_books_nb > 0,
|
|
callback = function()
|
|
caller_callback()
|
|
self:showBookList()
|
|
end,
|
|
}
|
|
end
|
|
|
|
function BookmarkBrowser:showBookList(multi_choice)
|
|
local book_list, subtitle, onMenuSelect, title_bar_left_icon, onLeftButtonTap
|
|
local getSubTitle = function(books_nb)
|
|
return T(_("Enabled books: %1"), books_nb)
|
|
end
|
|
local updateBookList = function(books_nb)
|
|
book_list.title_bar:setSubTitle(getSubTitle(books_nb), true)
|
|
book_list:updateItems(1, true)
|
|
end
|
|
local item_table = {}
|
|
if multi_choice then
|
|
subtitle = getSubTitle(self.filter_table.enabled_books_nb)
|
|
local books_enabled = { books_nb = self.filter_table.enabled_books_nb }
|
|
for i, book in ipairs(self.books) do
|
|
books_enabled[i] = book.enabled
|
|
item_table[i] = {
|
|
text = self.ui.bookinfo.combineAuthorsTitle(book.doc_props.authors, book.doc_props.display_title),
|
|
bold = book.is_current_file,
|
|
mandatory = book.enabled and self.checkmark,
|
|
}
|
|
end
|
|
onMenuSelect = function(self_menu, item) -- toggle book selection
|
|
local idx = item.idx
|
|
books_enabled[idx] = not books_enabled[idx] or nil
|
|
books_enabled.books_nb = books_enabled.books_nb + (books_enabled[idx] and 1 or -1)
|
|
item_table[idx].mandatory = books_enabled[idx] and self.checkmark
|
|
updateBookList(books_enabled.books_nb)
|
|
end
|
|
title_bar_left_icon = "check"
|
|
onLeftButtonTap = function()
|
|
self:showBookListMenu(book_list, books_enabled)
|
|
end
|
|
else
|
|
for _, book in ipairs(self.books) do
|
|
if book.enabled and book.bookmarks_nb > 0 then
|
|
local item = self.bm_list.item_table[book.item_table_idx]
|
|
table.insert(item_table, {
|
|
text = item.text,
|
|
bold = book.is_current_file,
|
|
mandatory = item.mandatory,
|
|
item_table_idx = book.item_table_idx,
|
|
})
|
|
end
|
|
end
|
|
onMenuSelect = function(self_menu, item) -- jump to the book in the bookmark list
|
|
UIManager:close(book_list)
|
|
self:updateBookmarkList(item.item_table_idx)
|
|
end
|
|
end
|
|
|
|
book_list = BookList:new{
|
|
title = T(_("Books: %1"), #item_table),
|
|
subtitle = subtitle,
|
|
item_table = item_table,
|
|
title_bar_left_icon = title_bar_left_icon,
|
|
onLeftButtonTap = onLeftButtonTap,
|
|
onMenuSelect = onMenuSelect,
|
|
updateBookList = updateBookList,
|
|
}
|
|
UIManager:show(book_list)
|
|
end
|
|
|
|
function BookmarkBrowser:showBookListMenu(book_list, books_enabled)
|
|
local empty_prop = "\u{0000}" .. _("N/A") -- sorted first
|
|
local book_list_menu
|
|
local function deselectAll()
|
|
for i, item in ipairs(book_list.item_table) do
|
|
books_enabled[i] = nil
|
|
books_enabled.books_nb = 0
|
|
item.mandatory = nil
|
|
end
|
|
end
|
|
local function selectBooksByProp(item_idxs)
|
|
deselectAll()
|
|
for _, idx in ipairs(item_idxs) do
|
|
books_enabled[idx] = true
|
|
book_list.item_table[idx].mandatory = self.checkmark
|
|
end
|
|
books_enabled.books_nb = #item_idxs
|
|
book_list.updateBookList(books_enabled.books_nb)
|
|
end
|
|
local function genMetadataButton(button_text, button_prop)
|
|
return {
|
|
text = button_text,
|
|
callback = function()
|
|
UIManager:close(book_list_menu)
|
|
local prop_values = {}
|
|
for idx, book in ipairs(self.books) do
|
|
local doc_prop = book.doc_props[button_prop]
|
|
if doc_prop == nil then
|
|
doc_prop = { empty_prop }
|
|
elseif button_prop == "series" then
|
|
doc_prop = { doc_prop }
|
|
elseif button_prop == "language" then
|
|
doc_prop = { doc_prop:lower() }
|
|
else -- "authors", "keywords"
|
|
doc_prop = util.splitToArray(doc_prop, "\n")
|
|
end
|
|
for _, prop in ipairs(doc_prop) do
|
|
prop_values[prop] = prop_values[prop] or {}
|
|
table.insert(prop_values[prop], idx)
|
|
end
|
|
end
|
|
self:showPropValueList(button_prop, prop_values, selectBooksByProp)
|
|
end,
|
|
}
|
|
end
|
|
|
|
local buttons = {
|
|
{
|
|
genMetadataButton(_("Author(s)"), "authors"),
|
|
genMetadataButton(_("Series"), "series"),
|
|
},
|
|
{
|
|
genMetadataButton(_("Language"), "language"),
|
|
genMetadataButton(_("Keywords"), "keywords"),
|
|
},
|
|
{}, -- separator
|
|
{
|
|
{
|
|
text = _("Deselect all"),
|
|
callback = function()
|
|
UIManager:close(book_list_menu)
|
|
deselectAll()
|
|
book_list.updateBookList(0)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Select all"),
|
|
callback = function()
|
|
UIManager:close(book_list_menu)
|
|
for i, item in ipairs(book_list.item_table) do
|
|
books_enabled[i] = true
|
|
item.mandatory = self.checkmark
|
|
end
|
|
books_enabled.books_nb = #self.books
|
|
book_list.updateBookList(books_enabled.books_nb)
|
|
end,
|
|
},
|
|
},
|
|
{}, -- separator
|
|
{
|
|
{
|
|
text = _("Apply"),
|
|
callback = function()
|
|
UIManager:close(book_list_menu)
|
|
UIManager:close(book_list)
|
|
local do_update
|
|
for i, book in ipairs(self.books) do
|
|
if book.enabled ~= books_enabled[i] then
|
|
book.enabled = books_enabled[i]
|
|
do_update = true
|
|
end
|
|
end
|
|
if do_update then
|
|
self.filter_table.enabled_books_nb = books_enabled.books_nb
|
|
self:updateBookmarkList()
|
|
end
|
|
end,
|
|
},
|
|
},
|
|
}
|
|
book_list_menu = ButtonDialog:new{
|
|
title = _("Filters"),
|
|
title_align = "center",
|
|
buttons = buttons,
|
|
}
|
|
UIManager:show(book_list_menu)
|
|
end
|
|
|
|
function BookmarkBrowser:showPropValueList(prop, prop_values, caller_callback)
|
|
local prop_list
|
|
local prop_item_table = {}
|
|
for value, item_idxs in pairs(prop_values) do
|
|
table.insert(prop_item_table, {
|
|
text = value,
|
|
mandatory = #item_idxs,
|
|
callback = function()
|
|
UIManager:close(prop_list)
|
|
caller_callback(item_idxs)
|
|
end,
|
|
})
|
|
end
|
|
if #prop_item_table > 1 then
|
|
table.sort(prop_item_table, BookList.getCollateSortFunc())
|
|
end
|
|
prop_list = BookList:new{
|
|
title = T(_("%1: %2"), self.ui.bookinfo.prop_text[prop]:sub(1, -2), #prop_item_table),
|
|
item_table = prop_item_table,
|
|
}
|
|
UIManager:show(prop_list)
|
|
end
|
|
|
|
function BookmarkBrowser:showSearchDialog(search_string)
|
|
local search_dialog, check_button_case
|
|
search_dialog = InputDialog:new{
|
|
title = _("Enter text to search for"),
|
|
input = search_string or self.filter_table.search_string,
|
|
buttons = {
|
|
{
|
|
{
|
|
text = _("Cancel"),
|
|
id = "close",
|
|
callback = function()
|
|
UIManager:close(search_dialog)
|
|
end,
|
|
},
|
|
{
|
|
text = _("Search"),
|
|
callback = function()
|
|
local str = search_dialog:getInputText()
|
|
if str ~= "" then
|
|
UIManager:close(search_dialog)
|
|
self.filter_table.search_string = str
|
|
self.filter_table.search_case_sensitive = check_button_case.checked
|
|
self:updateBookmarkList()
|
|
end
|
|
end,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
check_button_case = CheckButton:new{
|
|
text = _("Case sensitive"),
|
|
checked = self.filter_table.search_case_sensitive,
|
|
parent = search_dialog,
|
|
}
|
|
search_dialog:addWidget(check_button_case)
|
|
UIManager:show(search_dialog)
|
|
search_dialog:onShowKeyboard()
|
|
end
|
|
|
|
function BookmarkBrowser:updateBooks()
|
|
for book, count in pairs(self.books_updated) do
|
|
if book.is_archived then
|
|
local arc_settings = LuaSettings:open(book.file)
|
|
arc_settings.data = book.doc_settings.data
|
|
arc_settings:flush()
|
|
else
|
|
book.doc_settings:flush()
|
|
if count > 0 and self.ui.statistics then
|
|
local highlights_nb, notes_nb = ReaderAnnotation:getNumberOfHighlightsAndNotes(book.annotations)
|
|
self.ui.statistics:updateBookAnnotationsNumber(book.doc_props,
|
|
book.doc_settings:readSetting("partial_md5_checksum"), highlights_nb, notes_nb)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
return BookmarkBrowser
|