diff --git a/frontend/document/credocument.lua b/frontend/document/credocument.lua index 4348361bf..146210903 100644 --- a/frontend/document/credocument.lua +++ b/frontend/document/credocument.lua @@ -1609,6 +1609,7 @@ function CreDocument:register(registry) registry:addProvider("chm", "application/vnd.ms-htmlhelp", self, 90) registry:addProvider("doc", "application/msword", self, 90) registry:addProvider("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", self, 90) + registry:addProvider("docx", "application/docx", self, 90) -- Alternative mimetype for OPDS. registry:addProvider("docm", "application/vnd.ms-word.document.macroEnabled.12", self, 90) registry:addProvider("epub", "application/epub+zip", self, 100) registry:addProvider("epub", "application/epub", self, 100) -- Alternative mimetype for OPDS. diff --git a/frontend/document/pdfdocument.lua b/frontend/document/pdfdocument.lua index e27f55e7b..d5f04654e 100644 --- a/frontend/document/pdfdocument.lua +++ b/frontend/document/pdfdocument.lua @@ -376,6 +376,7 @@ function PdfDocument:register(registry) registry:addProvider("cbz", "application/x-cbz", self, 100) -- Alternative mimetype for OPDS. registry:addProvider("cfb", "application/octet-stream", self, 80) -- Compound File Binary, a Microsoft general-purpose file with a file-system-like structure. registry:addProvider("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", self, 80) + registry:addProvider("docx", "application/docx", self, 80) -- Alternative mimetype for OPDS. registry:addProvider("epub", "application/epub+zip", self, 50) registry:addProvider("epub3", "application/epub+zip", self, 50) registry:addProvider("fb2", "application/fb2", self, 80) diff --git a/plugins/opds.koplugin/main.lua b/plugins/opds.koplugin/main.lua index de8614e66..37457137e 100644 --- a/plugins/opds.koplugin/main.lua +++ b/plugins/opds.koplugin/main.lua @@ -66,8 +66,11 @@ end function OPDS:onDispatcherRegisterActions() Dispatcher:registerAction("opds_show_catalog", - {category="none", event="ShowOPDSCatalog", title=_("OPDS Catalog"), filemanager=true,} - ) + {category="none", event="ShowOPDSCatalog", title=_("OPDS Catalog"), filemanager=true,}) + Dispatcher:registerAction("opds_sync_all_catalogs", + {category="none", event="SyncAllOPDSCatalogs", arg=false, title=_("Sync all OPDS catalogs"), filemanager=true,}) + Dispatcher:registerAction("opds_sync_all_catalogs", + {category="none", event="SyncAllOPDSCatalogs", arg=true, title=_("Force sync all OPDS catalogs"), filemanager=true,}) end function OPDS:addToMainMenu(menu_items) @@ -81,7 +84,7 @@ function OPDS:addToMainMenu(menu_items) end end -function OPDS:onShowOPDSCatalog() +function OPDS:setupOPDSBrowser() self:loadSettings() self.opds_browser = OPDSBrowser:new{ settings = self.opds_settings, @@ -114,7 +117,19 @@ function OPDS:onShowOPDSCatalog() end end, } +end + +function OPDS:onShowOPDSCatalog() + self:setupOPDSBrowser() UIManager:show(self.opds_browser) + return true +end + +function OPDS:onSyncAllOPDSCatalogs(is_force) + self:setupOPDSBrowser() + self.opds_browser.sync_force = is_force + self.opds_browser:checkSyncDownload() + return true end function OPDS:showFileDownloadedDialog(file) diff --git a/plugins/opds.koplugin/opdsbrowser.lua b/plugins/opds.koplugin/opdsbrowser.lua index 900e56d46..9ca8c983e 100644 --- a/plugins/opds.koplugin/opdsbrowser.lua +++ b/plugins/opds.koplugin/opdsbrowser.lua @@ -4,7 +4,6 @@ local Cache = require("cache") local CheckButton = require("ui/widget/checkbutton") local ConfirmBox = require("ui/widget/confirmbox") local CookieJar = require("cookiejar") -local Device = require("device") local DocumentRegistry = require("document/documentregistry") local InfoMessage = require("ui/widget/infomessage") local InputDialog = require("ui/widget/inputdialog") @@ -13,13 +12,16 @@ local MultiInputDialog = require("ui/widget/multiinputdialog") local NetworkMgr = require("ui/network/manager") local Notification = require("ui/widget/notification") local OPDSClient = require("opdsclient") +local OPDSDownloadList = require("opdsdownloadlist") local OPDSParser = require("opdsparser") local OPDSPSE = require("opdspse") local SpinWidget = require("ui/widget/spinwidget") +local TextBoxWidget = require("ui/widget/textboxwidget") local TextViewer = require("ui/widget/textviewer") local Trapper = require("ui/trapper") local UIManager = require("ui/uimanager") local ffiUtil = require("ffi/util") +local filemanagerutil = require("apps/filemanager/filemanagerutil") local lfs = require("libs/libkoreader-lfs") local logger = require("logger") local ltn12 = require("ltn12") @@ -63,13 +65,31 @@ local OPDSBrowser = Menu:extend{ ["x-stanza-cover-image-thumbnail"] = true, }, - root_catalog_title = nil, - root_catalog_username = nil, - root_catalog_password = nil, - facet_groups = nil, -- Stores OPDS facet groups - http_client = nil, + -- from OPDS main + settings = nil, + servers = nil, + downloads = nil, + pending_syncs = nil, - title_shrink_font_to_fit = true, + download_list = nil, -- module + http_client = nil, -- module + + -- current catalog (server) + root_catalog_title = nil, + root_catalog_username = nil, + root_catalog_password = nil, + root_catalog_raw_names = nil, -- use server names for downloaded files + root_catalog_download_dir = nil, + facet_groups = nil, + search_url = nil, + alt_search_url = nil, + + catalog_title = nil, -- current subcatalog + download_dir = nil, -- chosen in file download dialog for current session, not saved + download_filename = nil, -- ditto + + title_shrink_font_to_fit = true, + title_bar_left_icon = "appbar.menu", } local function get_value(value) @@ -83,12 +103,11 @@ function OPDSBrowser:init() self.item_table = self:genItemTableFromRoot() self:getHttpClient() self.catalog_title = nil - self.title_bar_left_icon = "appbar.menu" self.onLeftButtonTap = function() self:showOPDSMenu() end - self.facet_groups = nil -- Initialize facet groups storage - Menu.init(self) -- call parent's init() + self.facet_groups = nil + Menu.init(self) end function OPDSBrowser:getHttpClient() @@ -100,6 +119,15 @@ function OPDSBrowser:getHttpClient() return self.http_client end +function OPDSBrowser:showLoadingNotification() + if not self.settings.no_loading_notification then -- the setting not in UI + local info = Notification:new{ text = _("Loading…") } + UIManager:show(info) + UIManager:forceRePaint() + UIManager:close(info) + end +end + function OPDSBrowser:showOPDSMenu() local dialog dialog = ButtonDialog:new{ @@ -112,7 +140,14 @@ function OPDSBrowser:showOPDSMenu() end, align = "left", }}, - {}, + {{ + text = _("Set default download folder"), + callback = function() + self:setDefaultDownloadDir() + end, + align = "left", + }}, + {}, -- separator {{ text = _("Sync all catalogs"), callback = function() @@ -135,17 +170,11 @@ function OPDSBrowser:showOPDSMenu() end, align = "left", }}, + {}, -- separator {{ - text = _("Set max number of files to sync"), + text = _("Set default sync folder"), callback = function() - self:setMaxSyncDownload() - end, - align = "left", - }}, - {{ - text = _("Set sync folder"), - callback = function() - self:setSyncDir() + self:setDefaultSyncDir() end, align = "left", }}, @@ -156,6 +185,13 @@ function OPDSBrowser:showOPDSMenu() end, align = "left", }}, + {{ + text = _("Set max number of files to sync"), + callback = function() + self:setMaxSyncDownload() + end, + align = "left", + }}, }, shrink_unneeded_width = true, anchor = function() @@ -248,27 +284,37 @@ function OPDSBrowser:showFacetLinkList(title, links, catalog_url, dialog) UIManager:show(link_list) end +local function getItemMandatory(item) + local t = {} + if item.sync then + table.insert(t, "\u{f46a}") + end + if item.download_dir then + table.insert(t, "\u{f114}") + end + if item.raw_names then + table.insert(t, "\u{f233}") + end + if item.username then + table.insert(t, "\u{f2c0}") + end + return table.concat(t, " ") +end + local function buildRootEntry(server) - local icons = "" - if server.username then - icons = "\u{f2c0}" - end - if server.sync then - icons = "\u{f46a} " .. icons - end return { - text = server.title, - mandatory = icons, - url = server.url, - username = server.username, - password = server.password, - raw_names = server.raw_names, -- use server raw filenames for download - searchable = server.url and server.url:match("%%s") and true or false, - sync = server.sync, + text = server.title, + mandatory = getItemMandatory(server), + url = server.url, + searchable = server.url and server.url:match("%%s") and true or false, + username = server.username, + password = server.password, + raw_names = server.raw_names, + sync = server.sync, + download_dir = server.download_dir, } end --- Builds the root list of catalogs function OPDSBrowser:genItemTableFromRoot() local item_table = { { @@ -286,7 +332,6 @@ function OPDSBrowser:getServerFromRootItem(item) return item and item.idx and self.servers[item.idx - 1] end --- Shows dialog to edit properties of the new/existing catalog function OPDSBrowser:addEditCatalog(item) local fields = { { @@ -356,7 +401,6 @@ function OPDSBrowser:addEditCatalog(item) dialog:onShowKeyboard() end --- Shows dialog to add a subcatalog to the root list function OPDSBrowser:addSubCatalog(item_url) local dialog dialog = InputDialog:new{ @@ -391,22 +435,25 @@ function OPDSBrowser:addSubCatalog(item_url) dialog:onShowKeyboard() end --- Saves catalog properties from input dialog function OPDSBrowser:editCatalogFromInput(fields, item, no_refresh) local old_server = self:getServerFromRootItem(item) - local new_server = { + local new_server = { -- server entry in settings/opds.lua title = fields[1], url = fields[2]:match("^%a+://") and fields[2] or "http://" .. fields[2], username = fields[3] ~= "" and fields[3] or nil, password = fields[4] ~= "" and fields[4] or nil, raw_names = fields[5], sync = fields[6], + -- sync_dir -- local path (sync) + -- last_download -- url (sync) + -- download_dir -- local path } if old_server then new_server.sync_dir = old_server.sync_dir if old_server.url == new_server.url then new_server.last_download = old_server.last_download end + new_server.download_dir = old_server.download_dir end local new_item = buildRootEntry(new_server) local new_idx, itemnumber @@ -425,7 +472,37 @@ function OPDSBrowser:editCatalogFromInput(fields, item, no_refresh) self._manager.updated = true end --- Deletes catalog from the root list +function OPDSBrowser:setDefaultDownloadDir() + -- OPDS plugin uses common download_dir + local title_header = _("Default download folder:") + local current_path = self._manager.ui.folder_shortcuts:expandPath("%D%") + local caller_callback = function(path) + if path ~= current_path then + self._manager.ui.folder_shortcuts:updateShortcut("download_dir", path) + G_reader_settings:saveSetting("download_dir", path) + end + end + filemanagerutil.showChooseDialog(title_header, caller_callback, current_path) +end + +function OPDSBrowser:setCatalogDownloadDir(item) + local title_header = _("Catalog download folder:") + local default_path = self._manager.ui.folder_shortcuts:expandPath("%D%") + local current_path = item.download_dir or default_path + local caller_callback = function(path) + if path ~= current_path then + local server = self:getServerFromRootItem(item) + path = path ~= default_path and path or nil + server.download_dir = path + item.download_dir = path + item.mandatory = getItemMandatory(item) + self:switchItemTable(nil, self.item_table, -1) + self._manager.updated = true + end + end + filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path) +end + function OPDSBrowser:deleteCatalog(item) table.remove(self.servers, item.idx - 1) table.remove(self.item_table, item.idx) @@ -458,7 +535,6 @@ function OPDSBrowser:getConditionalFeedHeaders(cached_feed) return headers end --- Fetches feed from server function OPDSBrowser:fetchFeed(item_url, headers_only, extra_headers) local sink = {} local block_timeout, total_timeout = socketutil:set_timeout(socketutil.LARGE_BLOCK_TIMEOUT, socketutil.LARGE_TOTAL_TIMEOUT) @@ -522,7 +598,6 @@ function OPDSBrowser:fetchFeed(item_url, headers_only, extra_headers) logger.dbg(string.format("OPDS: Failed to fetch catalog `%s`: %s", item_url, text)) end --- Parses feed to catalog function OPDSBrowser:parseFeed(item_url) local cache_key = "opds|catalog|" .. item_url local cache = self:getCatalogCache() @@ -596,7 +671,6 @@ function OPDSBrowser:getServerFileName(item_url, filetype) return filename end --- Generates link to search in catalog function OPDSBrowser:getSearchTemplate(osd_url) -- parse search descriptor local search_descriptor = self:parseFeed(osd_url) @@ -609,7 +683,6 @@ function OPDSBrowser:getSearchTemplate(osd_url) end end --- Generates menu items from the fetched list of catalog entries function OPDSBrowser:genItemTableFromURL(item_url) local ok, catalog = pcall(self.parseFeed, self, item_url) if not ok then @@ -793,6 +866,7 @@ function OPDSBrowser:getItemFromPublication(entry, item_url) type = in_link.type, title = in_link.title, href = url.absolute(item_url, in_link.href), + size = in_link.size, }) end end @@ -802,6 +876,7 @@ function OPDSBrowser:getItemFromPublication(entry, item_url) type = link.type, title = link.title, href = url.absolute(item_url, link.href), + size = link.size, }) end end @@ -820,7 +895,6 @@ function OPDSBrowser:getItemFromPublication(entry, item_url) } end --- Generates catalog item table and processes OPDS facets/search links function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) local item_table = {} self.facet_groups = nil @@ -847,7 +921,7 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) hrefs[link.rel] = build_href(link.href) end end - if not self.sync then + if not self.syncing then if link.href then if not self.search_url and link.type:find(self.search_type) then local template = self:getSearchTemplate(build_href(link.href)) @@ -904,6 +978,7 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) type = link.type, href = link_href, title = link.title, + size = link.length, }) elseif link.rel == self.stream_rel then -- https://vaemendis.net/opds-pse/ @@ -935,6 +1010,7 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) type = link.type, href = link_href, title = link.title, + size = link.length, }) end -- This statement grabs the catalog items that are @@ -983,35 +1059,37 @@ function OPDSBrowser:genItemTableFromCatalog(catalog, item_url) title = entry.title.div end end - item.text = title - local author = _("Unknown Author") + local author if type(entry.author) == "table" then - local names = {} - local empty_name = false - for _, entry_author in ipairs(entry.author) do - local name = type(entry_author) == "table" and entry_author.name or nil - if type(name) == "table" then - if #name > 0 then - name = table.concat(name, ", ") - else - -- we may get an empty table on https://gallica.bnf.fr/opds - empty_name = true - name = nil + author = entry.author.name + if not author then + local names = {} + local empty_name = false + for _, entry_author in ipairs(entry.author) do + local name = type(entry_author) == "table" and entry_author.name or nil + if type(name) == "table" then + if #name > 0 then + name = table.concat(name, ", ") + else + -- we may get an empty table on https://gallica.bnf.fr/opds + empty_name = true + name = nil + end + end + if type(name) == "string" then + table.insert(names, name) end end - if type(name) == "string" then - table.insert(names, name) + if #names > 0 then + author = table.concat(names, ", ") + elseif empty_name then + author = nil end end - if #names > 0 then - author = table.concat(names, ", ") - item.text = title .. " - " .. author - elseif empty_name then - author = nil - end end + item.text = author and title .. " - " .. author or title item.title = title - item.author = author + item.author = author or _("Unknown Author") item.content = entry.content or entry.summary table.insert(item_table, item) end @@ -1051,8 +1129,8 @@ function OPDSBrowser:setCatalogMenu(item_url, menu_table) end end --- Requests and shows updated list of catalog entries function OPDSBrowser:updateCatalog(item_url, paths_updated) + self:showLoadingNotification() local menu_table = self:genItemTableFromURL(item_url) if #menu_table > 0 or self.facet_groups or self.search_url then if not paths_updated then @@ -1071,7 +1149,6 @@ function OPDSBrowser:updateCatalog(item_url, paths_updated) end end --- Requests and adds more catalog entries to fill out the page function OPDSBrowser:appendCatalog(item_url) local menu_table = self:genItemTableFromURL(item_url) if #menu_table > 0 then @@ -1084,7 +1161,6 @@ function OPDSBrowser:appendCatalog(item_url) end end --- Shows dialog to search in catalog function OPDSBrowser:searchCatalog(item_url) local dialog dialog = InputDialog:new{ @@ -1120,20 +1196,45 @@ function OPDSBrowser:searchCatalog(item_url) dialog:onShowKeyboard() end --- Shows dialog to download / stream a book function OPDSBrowser:showDownloads(item) - local acquisitions = item.acquisitions - local filename, filename_orig = self:getFileName(item) - - local function createTitle(path, file) -- title for ButtonDialog - return T(_("Download folder:\n%1\n\nDownload filename:\n%2\n\nDownload file type:"), - BD.dirpath(path), file or _("")) + local filename_orig + self.download_filename, filename_orig = self:getFileName(item) + local function item_path_func() + return self:getCurrentDownloadDir(), self.download_filename or _("") end + local function download_buttons_func(close_parent_dialog) + return self:getDownloadButtons(item, close_parent_dialog) + end + local function change_folder_func() + local default_path = self.root_catalog_download_dir or self._manager.ui.folder_shortcuts:expandPath("%D%") + return { + default_path = default_path, + current_path = self.download_dir or default_path, + caller_callback = function(path_arg) + self.download_dir = path_arg ~= default_path and path_arg or nil + end, + } + end + local function change_filename_func() + return { + input = self.download_filename or filename_orig, + input_hint = filename_orig, + caller_callback = function(filename_arg) + if filename_arg ~= self.download_filename then + self.download_filename = filename_arg ~= "" and filename_arg or filename_orig + return true + end + end, + } + end + self:showDownloadDialog(item, item_path_func, + download_buttons_func, change_folder_func, change_filename_func) +end - local buttons = {} -- buttons for ButtonDialog +function OPDSBrowser:getDownloadButtons(item, close_parent_dialog) local stream_buttons -- page stream buttons local download_buttons = {} -- file type download buttons - for i, acquisition in ipairs(acquisitions) do -- filter out unsupported file types + for i, acquisition in ipairs(item.acquisitions) do -- filter out unsupported file types if acquisition.count then stream_buttons = { { @@ -1142,7 +1243,7 @@ function OPDSBrowser:showDownloads(item) text = "\u{23EE} " .. _("Page stream"), -- prepend BLACK LEFT-POINTING DOUBLE TRIANGLE WITH BAR callback = function() OPDSPSE:streamPages(acquisition.href, acquisition.count, false, self.root_catalog_username, self.root_catalog_password) - UIManager:close(self.download_dialog) + close_parent_dialog() end, }, { @@ -1150,12 +1251,11 @@ function OPDSBrowser:showDownloads(item) text = _("Stream from page") .. " \u{23E9}", -- append BLACK RIGHT-POINTING DOUBLE TRIANGLE callback = function() OPDSPSE:streamPages(acquisition.href, acquisition.count, true, self.root_catalog_username, self.root_catalog_password) - UIManager:close(self.download_dialog) + close_parent_dialog() end, }, }, } - if acquisition.last_read then table.insert(stream_buttons, { { @@ -1163,7 +1263,7 @@ function OPDSBrowser:showDownloads(item) text = "\u{25B6} " .. _("Resume stream from page") .. " " .. acquisition.last_read, -- prepend BLACK RIGHT-POINTING TRIANGLE callback = function() OPDSPSE:streamPages(acquisition.href, acquisition.count, false, self.root_catalog_username, self.root_catalog_password, acquisition.last_read) - UIManager:close(self.download_dialog) + close_parent_dialog() end, }, }) @@ -1177,32 +1277,39 @@ function OPDSBrowser:showDownloads(item) local filetype = self.getFiletype(acquisition) if filetype then -- supported file type local text = url.unescape(acquisition.title or string.upper(filetype)) + if acquisition.size then + text = T(_("%1 (%2)"), text, util.getFriendlySize(acquisition.size)) + end table.insert(download_buttons, { text = text .. "\u{2B07}", -- append DOWNWARDS BLACK ARROW callback = function() - UIManager:close(self.download_dialog) - local local_path = self:getLocalDownloadPath(filename, filetype, acquisition.href) + close_parent_dialog() + local local_path = self:getLocalDownloadPath(self.download_filename, filetype, acquisition.href) self:checkDownloadFile(local_path, acquisition.href, self.root_catalog_username, self.root_catalog_password, self.file_downloaded_callback, self.file_read_now_callback) end, hold_callback = function() - UIManager:close(self.download_dialog) + close_parent_dialog() table.insert(self.downloads, { - file = self:getLocalDownloadPath(filename, filetype, acquisition.href), + file = self:getLocalDownloadPath(self.download_filename, filetype, acquisition.href), url = acquisition.href, - info = type(item.content) == "string" and util.htmlToPlainTextIfHtml(item.content) or "", + size = acquisition.size, + image = item.image or item.thumbnail, + info = item.content, catalog = self.root_catalog_title, username = self.root_catalog_username, password = self.root_catalog_password, + datetime = os.date("%Y-%m-%d %H:%M:%S"), }) self._manager.updated = true - Notification:notify(_("Book added to download list"), Notification.SOURCE_OTHER) + UIManager:show(InfoMessage:new{ text = _("Book added to download list"), timeout = 2 }) end, }) end end end + local buttons = {} local buttons_nb = #download_buttons if buttons_nb > 0 then if buttons_nb == 1 then -- one wide button @@ -1215,36 +1322,53 @@ function OPDSBrowser:showDownloads(item) table.insert(buttons, {download_buttons[i], download_buttons[i+1]}) end end - table.insert(buttons, {}) -- separator end if stream_buttons then + table.insert(buttons, {}) -- separator for _, button_list in ipairs(stream_buttons) do table.insert(buttons, button_list) end - table.insert(buttons, {}) -- separator end - table.insert(buttons, { -- action buttons + return buttons +end + +function OPDSBrowser:showDownloadDialog(item, item_path_func, + download_buttons_func, change_folder_func, change_filename_func) + local textviewer + local function close_parent_dialog() + textviewer:onClose() + end + local buttons_table = download_buttons_func(close_parent_dialog) + table.insert(buttons_table, {}) -- separator + + table.insert(buttons_table, { { text = _("Choose folder"), callback = function() - require("ui/downloadmgr"):new{ - onConfirm = function(path) - logger.dbg("Download folder set to", path) - self._manager.ui.folder_shortcuts:updateShortcut("download_dir", path) - G_reader_settings:saveSetting("download_dir", path) - self.download_dialog:setTitle(createTitle(path, filename)) - end, - }:chooseDir(self:getCurrentDownloadDir()) + local change_folder = change_folder_func() + local title_header = _("Download folder:") + local default_path = change_folder.default_path + local current_path = change_folder.current_path + local caller_callback = function(path) + if path ~= current_path then + change_folder.caller_callback(path) + textviewer:onClose() -- reopen + self:showDownloadDialog(item, item_path_func, + download_buttons_func, change_folder_func, change_filename_func) + end + end + filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path) end, }, { text = _("Change filename"), callback = function() + local change_filename = change_filename_func() local dialog dialog = InputDialog:new{ title = _("Enter filename"), - input = filename or filename_orig, - input_hint = filename_orig, + input = change_filename.input, + input_hint = change_filename.input_hint, buttons = { { { @@ -1258,12 +1382,12 @@ function OPDSBrowser:showDownloads(item) text = _("Set filename"), is_enter_default = true, callback = function() - filename = dialog:getInputValue() - if filename == "" then - filename = filename_orig - end UIManager:close(dialog) - self.download_dialog:setTitle(createTitle(self:getCurrentDownloadDir(), filename)) + if change_filename.caller_callback(dialog:getInputValue()) then + textviewer:onClose() -- reopen + self:showDownloadDialog(item, item_path_func, + download_buttons_func, change_folder_func, change_filename_func) + end end, }, } @@ -1274,37 +1398,60 @@ function OPDSBrowser:showDownloads(item) end, }, }) + local cover_link = item.image or item.thumbnail - table.insert(buttons, { + table.insert(buttons_table, { { text = _("Book cover"), enabled = cover_link and true or false, callback = function() - OPDSPSE:streamPages(cover_link, 1, false, self.root_catalog_username, self.root_catalog_password) + NetworkMgr:runWhenConnected(function() + OPDSPSE:streamPages(cover_link, 1, false, + item.username or self.root_catalog_username, item.password or self.root_catalog_password) + end) end, }, { - text = _("Book information"), - enabled = type(item.content) == "string", + text = _("Close"), callback = function() - UIManager:show(TextViewer:new{ - title = item.text, - title_multilines = true, - text = util.htmlToPlainTextIfHtml(item.content), - text_type = "book_info", - }) + textviewer:onClose() end, }, }) - self.download_dialog = ButtonDialog:new{ - title = createTitle(self:getCurrentDownloadDir(), filename), - buttons = buttons, + local dl_folder, dl_filename = item_path_func() + local info = item.info or item.content + info = type(info) == "string" and util.htmlToPlainTextIfHtml(info) or "" + local text = table.concat({ + TextBoxWidget.PTF_HEADER, + TextBoxWidget.PTF_BOLD_START, _("Download folder"), TextBoxWidget.PTF_BOLD_END, "\n", + BD.dirpath(dl_folder), "\n", "\n", + TextBoxWidget.PTF_BOLD_START, _("Download filename"), TextBoxWidget.PTF_BOLD_END, "\n", + dl_filename, "\n", "\n", + TextBoxWidget.PTF_BOLD_START, _("Book description"), TextBoxWidget.PTF_BOLD_END, "\n", + info, + }) + textviewer = TextViewer:new{ + title = item.catalog or self.root_catalog_title, + text = text, + text_type = "book_info", + buttons_table = buttons_table, } - UIManager:show(self.download_dialog) + UIManager:show(textviewer) +end + +function OPDSBrowser:getFileName(item) + local filename = item.title + if item.author then + filename = item.author .. " - " .. filename + end + local filename_orig = filename + if self.root_catalog_raw_names then + filename = nil + end + return util.replaceAllInvalidChars(filename), util.replaceAllInvalidChars(filename_orig) end --- Helper function to get the filetype from an acquisitions table function OPDSBrowser.getFiletype(link) local filetype = util.getFileNameSuffix(link.href) if not DocumentRegistry:hasProvider("dummy." .. filetype) then @@ -1316,16 +1463,11 @@ function OPDSBrowser.getFiletype(link) return filetype end -function OPDSBrowser:getSyncDir(server) - return server and server.sync_dir or self.settings.sync_dir -end - function OPDSBrowser:getCurrentDownloadDir() - if self.sync then + if self.syncing then return self:getSyncDir(self.sync_server) - else - return self._manager.ui.folder_shortcuts:expandPath("%D%") end + return self.download_dir or self.root_catalog_download_dir or self._manager.ui.folder_shortcuts:expandPath("%D%") end function OPDSBrowser:getLocalDownloadPath(filename, filetype, remote_url) @@ -1336,7 +1478,6 @@ function OPDSBrowser:getLocalDownloadPath(filename, filetype, remote_url) return filename end --- Downloads a book (with "File already exists" dialog) function OPDSBrowser:checkDownloadFile(local_path, remote_url, username, password, caller_callback, read_now_callback) local function download() UIManager:scheduleIn(1, function() @@ -1416,7 +1557,6 @@ function OPDSBrowser:downloadFile(local_path, remote_url, username, password, ca end end --- Menu action on item tap (Download a book / Show subcatalog / Search in catalog) function OPDSBrowser:onMenuSelect(item) if item.acquisitions and item.acquisitions[1] then -- book logger.dbg("Downloads available:", item) @@ -1425,14 +1565,16 @@ function OPDSBrowser:onMenuSelect(item) if #self.paths == 0 then -- root list if item.idx == 1 then if #self.downloads > 0 then - self:showDownloadList() + self.download_list = self.download_list or OPDSDownloadList:new{ opdsbrowser = self } + self.download_list:show() end return true end - self.root_catalog_title = item.text - self.root_catalog_username = item.username - self.root_catalog_password = item.password - self.root_catalog_raw_names = item.raw_names + self.root_catalog_title = item.text + self.root_catalog_username = item.username + self.root_catalog_password = item.password + self.root_catalog_raw_names = item.raw_names + self.root_catalog_download_dir = item.download_dir end local connect_callback if item.searchable then @@ -1450,9 +1592,10 @@ function OPDSBrowser:onMenuSelect(item) return true end --- Menu action on item long-press (dialog Edit / Delete catalog) function OPDSBrowser:onMenuHold(item) if #self.paths > 0 or item.idx == 1 then return true end -- not root list or Downloads item + local server = self:getServerFromRootItem(item) + local has_sync_dir = self:getSyncDir(server) and true or false local dialog dialog = ButtonDialog:new{ title = item.text, @@ -1460,43 +1603,55 @@ function OPDSBrowser:onMenuHold(item) buttons = { { { - text = _("Sync settings"), + text = _("Set sync folder"), callback = function() - UIManager:close(dialog) - self:showSyncSettingsDialog(item) + if not has_sync_dir then -- Sync buttons are disabled; reopen + UIManager:close(dialog) + end + self:setCatalogSyncDir(item) end, }, }, { { text = _("Force sync"), + enabled = has_sync_dir, callback = function() UIManager:close(dialog) NetworkMgr:runWhenConnected(function() self.sync_force = true - self:checkSyncDownload(item.idx) + self:checkSyncDownload(server) end) end, }, { text = _("Sync"), + enabled = has_sync_dir, callback = function() UIManager:close(dialog) NetworkMgr:runWhenConnected(function() self.sync_force = false - self:checkSyncDownload(item.idx) + self:checkSyncDownload(server) end) end, }, }, - {}, + {}, -- separator { { - text = _("Delete"), + text = _("Set download folder"), + callback = function() + self:setCatalogDownloadDir(item) + end, + }, + }, + { + { + text = _("Remove catalog"), callback = function() UIManager:show(ConfirmBox:new{ - text = _("Delete OPDS catalog?"), - ok_text = _("Delete"), + text = _("Remove OPDS catalog?"), + ok_text = _("Remove"), ok_callback = function() UIManager:close(dialog) self:deleteCatalog(item) @@ -1505,7 +1660,7 @@ function OPDSBrowser:onMenuHold(item) end, }, { - text = _("Edit"), + text = _("Catalog settings"), callback = function() UIManager:close(dialog) self:addEditCatalog(item) @@ -1518,7 +1673,6 @@ function OPDSBrowser:onMenuHold(item) return true end --- Menu action on return-arrow tap (go to one-level upper catalog) function OPDSBrowser:onReturn() table.remove(self.paths) local path = self.paths[#self.paths] @@ -1541,14 +1695,15 @@ function OPDSBrowser:onReturn() return true end --- Menu action on return-arrow long-press (return to root path) function OPDSBrowser:onHoldReturn() self:init() return true end --- Menu action on next-page chevron tap (request and show more catalog entries) function OPDSBrowser:onNextPage(fill_only) + if not fill_only then -- not from updateCatalog() that shows its notification + self:showLoadingNotification() + end -- self.page_num comes from menu.lua local page_num = self.page_num -- fetch more entries until we fill out one page or reach the end @@ -1570,313 +1725,46 @@ function OPDSBrowser:onNextPage(fill_only) return true end -function OPDSBrowser:showDownloadList() - self.download_list = Menu:new{ - covers_fullscreen = true, - is_borderless = true, - is_popout = false, - title_bar_fm_style = true, - onMenuSelect = self.showDownloadListItemDialog, - _manager = self, - title_bar_left_icon = "appbar.menu", - onLeftButtonTap = self.showDownloadListMenu - } - self.download_list.close_callback = function() - UIManager:close(self.download_list) - self.download_list = nil - if self.download_list_updated then - self.download_list_updated = nil - self.item_table[1].mandatory = #self.downloads - self:updateItems(1, true) - end - end - self:updateDownloadListItemTable() - UIManager:show(self.download_list) +-- sync + +function OPDSBrowser:getSyncDir(server) + return server and server.sync_dir or self.settings.sync_dir end -function OPDSBrowser:showDownloadListMenu() - local dialog - dialog = ButtonDialog:new{ - buttons = { - {{ - text = _("Download all"), - callback = function() - UIManager:close(dialog) - self._manager:confirmDownloadDownloadList() - end, - align = "left", - }}, - {{ - text = _("Remove all"), - callback = function() - UIManager:close(dialog) - self._manager:confirmClearDownloadList() - end, - align = "left", - }}, - }, - shrink_unneeded_width = true, - anchor = function() - return self.title_bar.left_button.image.dimen - end, - } - UIManager:show(dialog) -end - -function OPDSBrowser:updateDownloadListItemTable(item_table) - if item_table == nil then - item_table = {} - for i, item in ipairs(self.downloads) do - item_table[i] = { - text = item.file:gsub(".*/", ""), - mandatory = item.catalog, - } - end - end - local title = T(_("Downloads (%1)"), #item_table) - self.download_list:switchItemTable(title, item_table) -end - -function OPDSBrowser:confirmDownloadDownloadList() - UIManager:show(ConfirmBox:new{ - text = _("Download all books?\nExisting files will be overwritten."), - ok_text = _("Download"), - ok_callback = function() - NetworkMgr:runWhenConnected(function() - Trapper:wrap(function() - self:downloadDownloadList() - end) - end) - end, - }) -end - -function OPDSBrowser:confirmClearDownloadList() - UIManager:show(ConfirmBox:new{ - text = _("Remove all downloads?"), - ok_text = _("Remove"), - ok_callback = function() - for i in ipairs(self.downloads) do - self.downloads[i] = nil - end - self.download_list_updated = true +function OPDSBrowser:setDefaultSyncDir() + local title_header = _("Default sync folder:") + local current_path = self.settings.sync_dir or _("not set") + local caller_callback = function(path) + if path ~= current_path then + self.settings.sync_dir = path self._manager.updated = true - self.download_list:close_callback() - end, - }) -end - -function OPDSBrowser:showDownloadListItemDialog(item) - local dl_item = self._manager.downloads[item.idx] - local textviewer - local function remove_item() - textviewer:onClose() - table.remove(self._manager.downloads, item.idx) - table.remove(self.item_table, item.idx) - self._manager:updateDownloadListItemTable(self.item_table) - self._manager.download_list_updated = true - self._manager._manager.updated = true - end - local buttons_table = { - { - { - text = _("Remove"), - callback = function() - remove_item() - end, - }, - { - text = _("Download"), - callback = function() - local function file_downloaded_callback(local_path) - remove_item() - self._manager.file_downloaded_callback(local_path) - end - local function file_read_now_callback(local_path) - -- Keep the item in the download list, it has not been downloaded - textviewer:onClose() - self._manager.file_read_now_callback(local_path) - end - NetworkMgr:runWhenConnected(function() - self._manager:checkDownloadFile(dl_item.file, dl_item.url, dl_item.username, dl_item.password, - file_downloaded_callback, file_read_now_callback) - end) - end, - }, - }, - {}, -- separator - { - { - text = _("Remove all"), - callback = function() - textviewer:onClose() - self._manager:confirmClearDownloadList() - end, - }, - { - text = _("Download all"), - callback = function() - textviewer:onClose() - self._manager:confirmDownloadDownloadList() - end, - }, - }, - } - local TextBoxWidget = require("ui/widget/textboxwidget") - local text = table.concat({ - TextBoxWidget.PTF_HEADER, - TextBoxWidget.PTF_BOLD_START, _("Folder"), TextBoxWidget.PTF_BOLD_END, "\n", - util.splitFilePathName(dl_item.file), "\n", "\n", - TextBoxWidget.PTF_BOLD_START, _("File"), TextBoxWidget.PTF_BOLD_END, "\n", - item.text, "\n", "\n", - TextBoxWidget.PTF_BOLD_START, _("Description"), TextBoxWidget.PTF_BOLD_END, "\n", - dl_item.info, - }) - textviewer = TextViewer:new{ - title = dl_item.catalog, - text = text, - text_type = "book_info", - buttons_table = buttons_table, - } - UIManager:show(textviewer) - return true -end - --- Download whole download list -function OPDSBrowser:downloadDownloadList() - local info = InfoMessage:new{ text = _("Downloading… (tap to cancel)") } - UIManager:show(info) - UIManager:forceRePaint() - local completed, downloaded = Trapper:dismissableRunInSubprocess(function() - local dl = {} - for _, item in ipairs(self.downloads) do - if self:downloadFile(item.file, item.url, item.username, item.password) then - dl[item.file] = true - end - end - return dl - end, info) - if completed then - UIManager:close(info) - end - local dl_count = #self.downloads - for i = dl_count, 1, -1 do - local item = self.downloads[i] - if downloaded and downloaded[item.file] then - table.remove(self.downloads, i) - else -- if subprocess has been interrupted, check for the downloaded file - local attr = lfs.attributes(item.file) - if attr then - if attr.size > 0 then - table.remove(self.downloads, i) - else -- incomplete download - os.remove(item.file) - end - end end end - dl_count = dl_count - #self.downloads - if dl_count > 0 then - self:updateDownloadListItemTable() - self.download_list_updated = true - self._manager.updated = true - UIManager:show(InfoMessage:new{ text = T(N_("1 book downloaded", "%1 books downloaded", dl_count), dl_count) }) - end + filemanagerutil.showChooseDialog(title_header, caller_callback, current_path) end -function OPDSBrowser:showSyncSettingsDialog(item) +function OPDSBrowser:setCatalogSyncDir(item) local server = self:getServerFromRootItem(item) - local sync_dialog - local catalog_sync_dir = server.sync_dir and BD.dirpath(server.sync_dir) or _("default sync folder") - local default_sync_dir = self.settings.sync_dir and BD.dirpath(self.settings.sync_dir) or _("not set") - sync_dialog = ButtonDialog:new{ - title = server.title .. "\n\n" .. T(_("Catalog sync folder:\n%1\n\nDefault sync folder:\n%2"), - catalog_sync_dir, default_sync_dir), - title_align = "center", - buttons = { - { - { - text = _("Choose catalog sync folder"), - callback = function() - UIManager:close(sync_dialog) - self:setSyncDir(server, function() - self:showSyncSettingsDialog(item) - end) - end, - }, - }, - { - { - text = _("Use default sync folder"), - enabled = server.sync_dir ~= nil, - callback = function() - server.sync_dir = nil - self._manager.updated = true - UIManager:close(sync_dialog) - self:showSyncSettingsDialog(item) - end, - }, - }, - }, - } - UIManager:show(sync_dialog) -end - -function OPDSBrowser:setMaxSyncDownload() - local current_max_dl = self.settings.sync_max_dl or 50 - local spin = SpinWidget:new{ - title_text = "Set maximum sync size", - info_text = "Set the max number of books to download at a time", - value = current_max_dl, - value_min = 0, - value_max = 1000, - value_step = 10, - value_hold_step = 50, - default_value = 50, - wrap = true, - ok_text = "Save", - callback = function(spin) - self.settings.sync_max_dl = spin.value + local title_header = _("Catalog sync folder:") + local default_path = self.settings.sync_dir or _("not set") + local current_path = server.sync_dir or default_path + local caller_callback = function(path) + if path ~= current_path then + path = path ~= default_path and path or nil + server.sync_dir = path self._manager.updated = true - end, - } - UIManager:show(spin) -end - -function OPDSBrowser:setSyncDir(server, callback) - local force_chooser_dir - if Device:isAndroid() then - force_chooser_dir = Device.home_dir - elseif server then - force_chooser_dir = self:getSyncDir(server) + end end - - require("ui/downloadmgr"):new{ - onConfirm = function(inbox) - if server then - logger.info("set opds catalog sync folder", server.title, inbox) - server.sync_dir = inbox - else - logger.info("set opds sync folder", inbox) - self.settings.sync_dir = inbox - end - self._manager.updated = true - if callback then - callback() - end - end, - }:chooseDir(force_chooser_dir) + filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path) end --- Set string for desired filetypes -function OPDSBrowser:setSyncFiletypes(filetype_list) - local input = self.settings.filetypes +function OPDSBrowser:setSyncFiletypes() local dialog dialog = InputDialog:new{ title = _("File types to sync"), description = _("A comma separated list of desired filetypes"), + input = self.settings.filetypes, input_hint = _("epub, mobi"), - input = input, buttons = { { { @@ -1903,48 +1791,49 @@ function OPDSBrowser:setSyncFiletypes(filetype_list) dialog:onShowKeyboard() end --- Helper function to get filename and set nil if using raw names -function OPDSBrowser:getFileName(item) - local filename = item.title - if item.author then - filename = item.author .. " - " .. filename - end - local filename_orig = filename - if self.root_catalog_raw_names then - filename = nil - end - return util.replaceAllInvalidChars(filename), util.replaceAllInvalidChars(filename_orig) +function OPDSBrowser:setMaxSyncDownload() + UIManager:show(SpinWidget:new{ + title_text = _("Max number of files"), + info_text = _("Max number of files to download at a time"), + value = self.settings.sync_max_dl or 50, + value_min = 0, + value_max = 1000, + value_step = 10, + value_hold_step = 50, + default_value = 50, + wrap = true, + ok_text = _("Save"), + callback = function(spin) + self.settings.sync_max_dl = spin.value + self._manager.updated = true + end, + }) end -function OPDSBrowser:updateFieldInCatalog(item, name, value) - item[name] = value - self._manager.updated = true -end - -function OPDSBrowser:checkSyncDownload(idx) +function OPDSBrowser:checkSyncDownload(server_to_sync) local servers_to_sync = {} - if idx then - local server = self.servers[idx-1] -- First item is "Downloads" - if server and self:getSyncDir(server) then - table.insert(servers_to_sync, server) - end - else + if server_to_sync then + table.insert(servers_to_sync, server_to_sync) + else -- all + local server_checked for _, server in ipairs(self.servers) do - if server.sync and self:getSyncDir(server) then - table.insert(servers_to_sync, server) + if server.sync then + server_checked = true + if self:getSyncDir(server) then + table.insert(servers_to_sync, server) + end end end + if #servers_to_sync == 0 then + UIManager:show(InfoMessage:new{ + text = server_checked and _("Please choose a folder for sync downloads first") + or _("Please choose catalogs to sync"), + }) + return + end end - if #servers_to_sync == 0 then - UIManager:show(InfoMessage:new{ - text = idx and _("Please choose a folder for this catalog's sync downloads first") - or _("Please choose a folder for sync downloads first"), - }) - return - end - - self.sync = true + self.syncing = true self.sync_server_list = {} local info local ok, err = pcall(function() @@ -1979,13 +1868,12 @@ function OPDSBrowser:checkSyncDownload(idx) if info then UIManager:close(info) end - self.sync = false + self.syncing = false if not ok then error(err, 0) end end --- Add entries to self.pending_syncs function OPDSBrowser:fillPendingSyncs(server) self.root_catalog_password = server.password self.root_catalog_raw_names = server.raw_names @@ -2049,12 +1937,11 @@ function OPDSBrowser:fillPendingSyncs(server) self.sync_server_list[server.url] = true if new_last_download then logger.dbg("Updating opds last download for server", server.title, "to", new_last_download) - self:updateFieldInCatalog(server, "last_download", new_last_download) + server.last_download = new_last_download + self._manager.updated = true end - end --- Get list of books to download bigger than sync_max_dl function OPDSBrowser:getSyncDownloadList(url_arg) local sync_table = {} local fetch_url = url_arg or self.sync_server.url @@ -2118,7 +2005,6 @@ function OPDSBrowser:getSyncDownloadList(url_arg) return sync_table end --- Download pending syncs list function OPDSBrowser:downloadPendingSyncs() local dl_list = self.pending_syncs local function dismissable_download() @@ -2181,7 +2067,6 @@ function OPDSBrowser:downloadPendingSyncs() end local duplicate_list = dismissable_download() - if duplicate_list and #duplicate_list > 0 then local textviewer local duplicate_files = { _("These files are already on the device:") } diff --git a/plugins/opds.koplugin/opdsdownloadlist.lua b/plugins/opds.koplugin/opdsdownloadlist.lua new file mode 100644 index 000000000..6e224d0fd --- /dev/null +++ b/plugins/opds.koplugin/opdsdownloadlist.lua @@ -0,0 +1,230 @@ +local BookList = require("ui/widget/booklist") +local ButtonDialog = require("ui/widget/buttondialog") +local ConfirmBox = require("ui/widget/confirmbox") +local InfoMessage = require("ui/widget/infomessage") +local NetworkMgr = require("ui/network/manager") +local Trapper = require("ui/trapper") +local UIManager = require("ui/uimanager") +local ffiUtil = require("ffi/util") +local filemanagerutil = require("apps/filemanager/filemanagerutil") +local lfs = require("libs/libkoreader-lfs") +local _ = require("gettext") +local N_ = _.ngettext +local T = ffiUtil.template + +local OPDSDownloadList = BookList:extend{ + title_bar_left_icon = "appbar.menu", + opdsbrowser = nil, +} + +function OPDSDownloadList:init() + self.downloads = self.opdsbrowser.downloads + self.close_callback = function() + UIManager:close(self) + self.item_table = nil + if self.updated then + self.updated = nil + self.opdsbrowser._manager.updated = true + if self.downloads_nb ~= #self.downloads then -- refresh root catalog list + self.opdsbrowser.item_table[1].mandatory = #self.downloads + self.opdsbrowser:updateItems(1, true) + end + end + end + self.onReturn = self.close_callback + BookList.init(self) + self.paths = { true } -- enable return arrow +end + +function OPDSDownloadList:show() + self.downloads_nb = #self.downloads + self:updateItemTable() + UIManager:show(self) +end + +function OPDSDownloadList:updateItemTable(item_table) + if item_table == nil then + item_table = {} + for i, item in ipairs(self.downloads) do + item_table[i] = { + text = item.file:gsub(".*/", ""), + mandatory = item.catalog, + } + end + end + local title = T(_("Downloads (%1)"), #item_table) + self:switchItemTable(title, item_table) +end + +function OPDSDownloadList:onMenuSelect(item) + local dl_item = self.downloads[item.idx] + local function item_path_func() -- folder without trailing slash, filename + return ffiUtil.dirname(dl_item.file), item.text + end + local function download_buttons_func(close_parent_dialog) + local function remove_item() + close_parent_dialog() + table.remove(self.downloads, item.idx) + table.remove(self.item_table, item.idx) + self:updateItemTable(self.item_table) + self.updated = true + end + return { + { + { + text = _("Remove"), + callback = remove_item, + }, + { + text = _("Download"), + callback = function() + local function file_downloaded_callback(local_path) + remove_item() + self.opdsbrowser.file_downloaded_callback(local_path) + end + local function file_read_now_callback(local_path) + -- file already exists, don't download and keep it in the download list + close_parent_dialog() + self.opdsbrowser.file_read_now_callback(local_path) + end + NetworkMgr:runWhenConnected(function() + self.opdsbrowser:checkDownloadFile(dl_item.file, dl_item.url, dl_item.username, dl_item.password, + file_downloaded_callback, file_read_now_callback) + end) + end, + }, + }, + } + end + local function change_folder_func() + return { + default_path = self.opdsbrowser._manager.ui.folder_shortcuts:expandPath("%D%"), + current_path = ffiUtil.dirname(dl_item.file), + caller_callback = function(path_arg) + dl_item.file = path_arg .. "/" .. ffiUtil.basename(dl_item.file) + self.updated = true + end, + } + end + local function change_filename_func() + local filename_without_suffix, filetype = filemanagerutil.splitFileNameType(dl_item.file) + return { + input = filename_without_suffix, + caller_callback = function(filename_arg) + if filename_arg ~= "" and filename_arg ~= filename_without_suffix then + item.text = filename_arg .. "." .. filetype + dl_item.file = ffiUtil.dirname(dl_item.file) .. "/" .. item.text + self:updateItems(1, true) + self.updated = true + return true + end + end, + } + end + self.opdsbrowser:showDownloadDialog(dl_item, item_path_func, + download_buttons_func, change_folder_func, change_filename_func) + return true +end + +function OPDSDownloadList:onLeftButtonTap() + local dialog + dialog = ButtonDialog:new{ + buttons = { + {{ + text = _("Download all"), + callback = function() + UIManager:close(dialog) + self:confirmDownloadList() + end, + align = "left", + }}, + {{ + text = _("Remove all"), + callback = function() + UIManager:close(dialog) + self:confirmClearList() + end, + align = "left", + }}, + }, + shrink_unneeded_width = true, + anchor = function() + return self.title_bar.left_button.image.dimen + end, + } + UIManager:show(dialog) +end + +function OPDSDownloadList:confirmDownloadList() + UIManager:show(ConfirmBox:new{ + text = _("Download all books?\nExisting files will be overwritten."), + ok_text = _("Download"), + ok_callback = function() + NetworkMgr:runWhenConnected(function() + Trapper:wrap(function() + self:downloadList() + end) + end) + end, + }) +end + +function OPDSDownloadList:confirmClearList() + UIManager:show(ConfirmBox:new{ + text = _("Remove all downloads?"), + ok_text = _("Remove"), + ok_callback = function() + for i in ipairs(self.downloads) do -- keep the reference to the table + self.downloads[i] = nil + end + self.updated = true + self:close_callback() + end, + }) +end + +function OPDSDownloadList:downloadList() + local info = InfoMessage:new{ text = _("Downloading… (tap to cancel)") } + UIManager:show(info) + UIManager:forceRePaint() + local completed, downloaded = Trapper:dismissableRunInSubprocess(function() + local dl = {} + for _, item in ipairs(self.downloads) do + if self.opdsbrowser:downloadFile(item.file, item.url, item.username, item.password) then + dl[item.file] = true + end + end + return dl + end, info) + if completed then + UIManager:close(info) + end + local dl_count = #self.downloads + for i = dl_count, 1, -1 do + local item = self.downloads[i] + if downloaded and downloaded[item.file] then + table.remove(self.downloads, i) + else -- subprocess has been interrupted, check for the downloaded file + local attr = lfs.attributes(item.file) + if attr then + if attr.size > 0 then + table.remove(self.downloads, i) + else -- incomplete download + os.remove(item.file) + end + end + end + end + dl_count = dl_count - #self.downloads + if dl_count > 0 then + self.updated = true + if #self.downloads == 0 then + self:close_callback() + else + self:updateItemTable() + end + UIManager:show(InfoMessage:new{ text = T(N_("1 book downloaded", "%1 books downloaded", dl_count), dl_count) }) + end +end + +return OPDSDownloadList diff --git a/spec/unit/opds_spec.lua b/spec/unit/opds_spec.lua index 6d71ac460..5c8fc2514 100644 --- a/spec/unit/opds_spec.lua +++ b/spec/unit/opds_spec.lua @@ -779,7 +779,7 @@ describe("OPDS module", function() it("should resolve sync paths for each catalog without a global folder", function() local browser = OPDSBrowser:extend{ settings = {}, - sync = true, + syncing = true, sync_server = { sync_dir = "/comics" }, } assert.are.same("/comics", browser:getCurrentDownloadDir()) @@ -791,85 +791,6 @@ describe("OPDS module", function() assert.are.same("/default", browser:getCurrentDownloadDir()) end) - describe("dialogs", function() - local UIManager, browser, shown - - before_each(function() - UIManager = require("ui/uimanager") - local ButtonDialog = require("ui/widget/buttondialog") - stub(ButtonDialog, "new", function(_, options) return options end) - stub(UIManager, "show", function(_, dialog) shown = dialog end) - stub(UIManager, "close") - browser = OPDSBrowser:extend{ - paths = {}, - settings = { sync_dir = "/default" }, - servers = {{ title = "Catalog", url = "http://example.org/opds" }}, - _manager = {}, - } - end) - - after_each(function() - require("ui/widget/buttondialog").new:revert() - UIManager.show:revert() - UIManager.close:revert() - end) - - it("should put sync settings first without a following separator", function() - browser:onMenuHold{ idx = 2, text = "Catalog" } - assert.are.same("Sync settings", shown.buttons[1][1].text) - assert.are.same("Force sync", shown.buttons[2][1].text) - shown.buttons[1][1].callback() - assert.are.same("Choose catalog sync folder", shown.buttons[1][1].text) - end) - - it("should explain fallback and offer only the two folder actions", function() - browser:showSyncSettingsDialog{ idx = 2 } - assert.truthy(shown.title:find("default sync folder", 1, true)) - assert.are.same(2, #shown.buttons) - assert.are.same("Choose catalog sync folder", shown.buttons[1][1].text) - assert.are.same("Use default sync folder", shown.buttons[2][1].text) - assert.is_false(shown.buttons[2][1].enabled) - end) - - it("should clear only the catalog folder and refresh the dialog", function() - local server = browser.servers[1] - server.sync_dir = "/catalog" - server.last_download = "latest" - browser:showSyncSettingsDialog{ idx = 2 } - assert.is_true(shown.buttons[2][1].enabled) - shown.buttons[2][1].callback() - assert.is_nil(server.sync_dir) - assert.are.same("/default", browser.settings.sync_dir) - assert.are.same("latest", server.last_download) - assert.is_true(browser._manager.updated) - assert.is_false(shown.buttons[2][1].enabled) - end) - - it("should choose and persist a catalog folder without changing the default", function() - local DownloadMgr = require("ui/downloadmgr") - local Device = require("device") - local picker, initial_dir - stub(DownloadMgr, "new", function(_, options) - picker = options - options.chooseDir = function(_, path) initial_dir = path end - return options - end) - stub(Device, "isAndroid", function() return false end) - finally(function() - DownloadMgr.new:revert() - Device.isAndroid:revert() - end) - browser:showSyncSettingsDialog{ idx = 2 } - shown.buttons[1][1].callback() - assert.are.same("/default", initial_dir) - picker.onConfirm("/catalog") - assert.are.same("/catalog", browser.servers[1].sync_dir) - assert.are.same("/default", browser.settings.sync_dir) - assert.is_true(browser._manager.updated) - assert.is_true(shown.buttons[2][1].enabled) - end) - end) - describe("sync selection", function() local UIManager, browser, collected, downloaded, messages @@ -910,7 +831,7 @@ describe("OPDS module", function() it("should sync enabled catalogs with their own folders without a global folder", function() browser:checkSyncDownload() assert.are.same({ "comics", "ebooks" }, collected) - assert.is_false(browser.sync) + assert.is_false(browser.syncing) assert.is_false(downloaded) end) @@ -921,34 +842,27 @@ describe("OPDS module", function() end) it("should allow explicit sync of a catalog excluded from automatic sync", function() - browser:checkSyncDownload(4) + browser:checkSyncDownload(browser.servers[3]) assert.are.same({ "disabled" }, collected) end) it("should ignore pending downloads from a previous catalog selection", function() browser.sync_server_list = { ebooks = true } browser.pending_syncs = {{ catalog = "ebooks" }} - browser:checkSyncDownload(2) + browser:checkSyncDownload(browser.servers[1]) assert.is_false(downloaded) assert.is_nil(browser.sync_server_list.ebooks) browser.pending_syncs = {{ catalog = "comics" }} - browser:checkSyncDownload(2) + browser:checkSyncDownload(browser.servers[1]) assert.is_true(downloaded) end) - it("should request a folder when the selected catalog has no destination", function() - browser:checkSyncDownload(5) - assert.are.same({}, collected) - assert.are.same("Please choose a folder for this catalog's sync downloads first", messages[1]) - assert.is_false(downloaded) - end) - it("should leave sync mode after feed collection fails", function() browser.fillPendingSyncs = function() error("feed failed") end - local ok, err = pcall(function() browser:checkSyncDownload(2) end) + local ok, err = pcall(function() browser:checkSyncDownload(browser.servers[1]) end) assert.is_false(ok) assert.truthy(err:find("feed failed", 1, true)) - assert.is_false(browser.sync) + assert.is_false(browser.syncing) assert.is_false(downloaded) end) end)