mirror of
https://github.com/koreader/koreader.git
synced 2026-12-05 11:38:31 +00:00
OPDS update (#16311)
This commit is contained in:
1 parent
801277394b
commit
035e00b21d
6 files changed
+625
-579
No files matched your search
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -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
|
||||
+7
-93
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user