2020-01-04 00:18:51 +00:00
|
|
|
local BD = require("ui/bidi")
|
2018-10-17 12:13:59 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2019-11-19 15:52:11 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2018-10-17 12:13:59 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local MultiInputDialog = require("ui/widget/multiinputdialog")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
|
|
|
local WebDavApi = require("apps/cloudstorage/webdavapi")
|
2019-11-19 15:52:11 +00:00
|
|
|
local util = require("util")
|
2022-11-11 14:53:06 +00:00
|
|
|
local ffiutil = require("ffi/util")
|
2018-10-17 12:13:59 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
local T = require("ffi/util").template
|
|
|
|
|
|
|
|
local WebDav = {}
|
|
|
|
|
2022-11-11 14:53:06 +00:00
|
|
|
function WebDav:run(address, user, pass, path, folder_mode)
|
|
|
|
return WebDavApi:listFolder(address, user, pass, path, folder_mode)
|
2018-10-17 12:13:59 +00:00
|
|
|
end
|
|
|
|
|
ReaderUI: Saner FM/RD lifecycle
* Ensure that going from one to the other tears down the former and
its plugins before instantiating the latter and its plugins.
UIManager: Unify Event sending & broadcasting
* Make the two behave the same way (walk the widget stack from top to
bottom), and properly handle the window stack shrinking shrinking
*and* growing.
Previously, broadcasting happened bottom-to-top and didn't really
handle the list shrinking/growing, while sending only handled the list
shrinking by a single element, and hopefully that element being the one
the event was just sent to.
These two items combined allowed us to optimize suboptimal
refresh behavior with Menu and other Menu classes when
opening/closing a document.
e.g., the "opening document" Notification is now properly regional,
and the "open last doc" option no longer flashes like a crazy person
anymore.
Plugins: Allow optimizing Menu refresh with custom menus, too.
Requires moving Menu's close_callback *after* onMenuSelect, which, eh,
probably makes sense, and is probably harmless in the grand scheme of
things.
2021-05-01 16:53:04 +00:00
|
|
|
function WebDav:downloadFile(item, address, username, password, local_path, callback_close)
|
2022-10-29 15:45:52 +00:00
|
|
|
local code_response = WebDavApi:downloadFile(WebDavApi:getJoinedPath(address, item.url), username, password, local_path)
|
2018-10-17 12:13:59 +00:00
|
|
|
if code_response == 200 then
|
2019-11-19 15:52:11 +00:00
|
|
|
local __, filename = util.splitFilePathName(local_path)
|
|
|
|
if G_reader_settings:isTrue("show_unsupported") and not DocumentRegistry:hasProvider(filename) then
|
2019-07-21 19:45:02 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
2020-01-04 00:18:51 +00:00
|
|
|
text = T(_("File saved to:\n%1"), BD.filepath(local_path)),
|
2019-07-21 19:45:02 +00:00
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(ConfirmBox:new{
|
|
|
|
text = T(_("File saved to:\n%1\nWould you like to read the downloaded book now?"),
|
2020-01-04 00:18:51 +00:00
|
|
|
BD.filepath(local_path)),
|
2019-07-21 19:45:02 +00:00
|
|
|
ok_callback = function()
|
ReaderUI: Saner FM/RD lifecycle
* Ensure that going from one to the other tears down the former and
its plugins before instantiating the latter and its plugins.
UIManager: Unify Event sending & broadcasting
* Make the two behave the same way (walk the widget stack from top to
bottom), and properly handle the window stack shrinking shrinking
*and* growing.
Previously, broadcasting happened bottom-to-top and didn't really
handle the list shrinking/growing, while sending only handled the list
shrinking by a single element, and hopefully that element being the one
the event was just sent to.
These two items combined allowed us to optimize suboptimal
refresh behavior with Menu and other Menu classes when
opening/closing a document.
e.g., the "opening document" Notification is now properly regional,
and the "open last doc" option no longer flashes like a crazy person
anymore.
Plugins: Allow optimizing Menu refresh with custom menus, too.
Requires moving Menu's close_callback *after* onMenuSelect, which, eh,
probably makes sense, and is probably harmless in the grand scheme of
things.
2021-05-01 16:53:04 +00:00
|
|
|
local Event = require("ui/event")
|
|
|
|
UIManager:broadcastEvent(Event:new("SetupShowReader"))
|
|
|
|
|
|
|
|
if callback_close then
|
|
|
|
callback_close()
|
|
|
|
end
|
|
|
|
|
2019-07-21 19:45:02 +00:00
|
|
|
ReaderUI:showReader(local_path)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
2018-10-17 12:13:59 +00:00
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
2020-01-04 00:18:51 +00:00
|
|
|
text = T(_("Could not save file to:\n%1"), BD.filepath(local_path)),
|
2018-10-17 12:13:59 +00:00
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-11 14:53:06 +00:00
|
|
|
function WebDav:uploadFile(url, address, username, password, local_path, callback_close)
|
|
|
|
local path = WebDavApi:getJoinedPath(address, url)
|
|
|
|
path = WebDavApi:getJoinedPath(path, ffiutil.basename(local_path))
|
|
|
|
local code_response = WebDavApi:uploadFile(path, username, password, local_path)
|
|
|
|
if code_response >= 200 and code_response < 300 then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("File uploaded:\n%1"), BD.filepath(address)),
|
|
|
|
})
|
|
|
|
if callback_close then callback_close() end
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Could not upload file:\n%1"), BD.filepath(address)),
|
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function WebDav:createFolder(url, address, username, password, folder_name, callback_close)
|
|
|
|
local code_response = WebDavApi:createFolder(address .. WebDavApi:urlEncode(url .. "/" .. folder_name), username, password, folder_name)
|
|
|
|
if code_response == 201 then
|
|
|
|
if callback_close then
|
|
|
|
callback_close()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Could not create folder:\n%1"), folder_name),
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-17 12:13:59 +00:00
|
|
|
function WebDav:config(item, callback)
|
|
|
|
local text_info = _([[Server address must be of the form http(s)://domain.name/path
|
|
|
|
This can point to a sub-directory of the WebDAV server.
|
|
|
|
The start folder is appended to the server path.]])
|
|
|
|
|
|
|
|
local hint_name = _("Server display name")
|
|
|
|
local text_name = ""
|
2020-02-12 19:38:41 +00:00
|
|
|
local hint_address = _("WebDAV address, for example https://example.com/dav")
|
2018-10-17 12:13:59 +00:00
|
|
|
local text_address = ""
|
|
|
|
local hint_username = _("Username")
|
|
|
|
local text_username = ""
|
|
|
|
local hint_password = _("Password")
|
|
|
|
local text_password = ""
|
|
|
|
local hint_folder = _("Start folder")
|
|
|
|
local text_folder = ""
|
|
|
|
local title
|
|
|
|
local text_button_ok = _("Add")
|
|
|
|
if item then
|
|
|
|
title = _("Edit WebDAV account")
|
|
|
|
text_button_ok = _("Apply")
|
|
|
|
text_name = item.text
|
|
|
|
text_address = item.address
|
|
|
|
text_username = item.username
|
|
|
|
text_password = item.password
|
|
|
|
text_folder = item.url
|
|
|
|
else
|
|
|
|
title = _("Add WebDAV account")
|
|
|
|
end
|
|
|
|
self.settings_dialog = MultiInputDialog:new {
|
|
|
|
title = title,
|
|
|
|
fields = {
|
|
|
|
{
|
|
|
|
text = text_name,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_name ,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_address,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_address ,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_username,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_username,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_password,
|
|
|
|
input_type = "string",
|
|
|
|
text_type = "password",
|
|
|
|
hint = hint_password,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_folder,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_folder,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
2022-03-04 20:20:00 +00:00
|
|
|
id = "close",
|
2018-10-17 12:13:59 +00:00
|
|
|
callback = function()
|
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Info"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:show(InfoMessage:new{ text = text_info })
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_button_ok,
|
|
|
|
callback = function()
|
2022-10-06 15:11:47 +00:00
|
|
|
local fields = self.settings_dialog:getFields()
|
2021-08-27 07:40:14 +00:00
|
|
|
|
|
|
|
-- make sure the URL is a valid path
|
|
|
|
if fields[5] ~= "" then
|
|
|
|
if string.sub(fields[5], 1, 1) ~= '/' then
|
|
|
|
fields[5] = '/' .. fields[5]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-17 12:13:59 +00:00
|
|
|
if fields[1] ~= "" and fields[2] ~= "" then
|
|
|
|
if item then
|
|
|
|
-- edit
|
|
|
|
callback(item, fields)
|
|
|
|
else
|
|
|
|
-- add new
|
|
|
|
callback(fields)
|
|
|
|
end
|
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Please fill in all fields.")
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input_type = "text",
|
|
|
|
}
|
|
|
|
UIManager:show(self.settings_dialog)
|
|
|
|
self.settings_dialog:onShowKeyboard()
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
function WebDav:info(item)
|
|
|
|
local info_text = T(_"Type: %1\nName: %2\nAddress: %3", "WebDAV", item.text, item.address)
|
|
|
|
UIManager:show(InfoMessage:new{text = info_text})
|
|
|
|
end
|
|
|
|
|
|
|
|
return WebDav
|