2020-01-04 00:18:51 +00:00
|
|
|
local BD = require("ui/bidi")
|
2016-12-20 07:34:00 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2019-11-19 15:52:11 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2017-04-24 06:27:29 +00:00
|
|
|
local FtpApi = require("apps/cloudstorage/ftpapi")
|
2016-12-20 07:34:00 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local MultiInputDialog = require("ui/widget/multiinputdialog")
|
2017-04-02 22:06:59 +00:00
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
2016-12-20 07:34:00 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2021-04-25 15:19:08 +00:00
|
|
|
local ltn12 = require("ltn12")
|
2018-02-22 23:02:32 +00:00
|
|
|
local logger = require("logger")
|
2017-04-02 22:06:59 +00:00
|
|
|
local util = require("util")
|
2016-12-20 07:34:00 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
local T = require("ffi/util").template
|
|
|
|
|
2018-02-22 23:02:32 +00:00
|
|
|
local Ftp = {}
|
2016-12-20 07:34:00 +00:00
|
|
|
|
|
|
|
function Ftp:run(address, user, pass, path)
|
2018-05-04 15:06:58 +00:00
|
|
|
local url = FtpApi:generateUrl(address, util.urlEncode(user), util.urlEncode(pass)) .. path
|
2017-05-20 21:21:49 +00:00
|
|
|
return FtpApi:listFolder(url, path)
|
2016-12-20 07:34:00 +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 Ftp:downloadFile(item, address, user, pass, path, callback_close)
|
2018-05-04 15:06:58 +00:00
|
|
|
local url = FtpApi:generateUrl(address, util.urlEncode(user), util.urlEncode(pass)) .. item.url
|
2018-02-22 23:02:32 +00:00
|
|
|
logger.dbg("downloadFile url", url)
|
2021-04-25 11:08:49 +00:00
|
|
|
path = util.fixUtf8(path, "_")
|
|
|
|
local file, err = io.open(path, "w")
|
|
|
|
if not file then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Could not save file to %1:\n%2"), BD.filepath(path), err),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local response = FtpApi:ftpGet(url, "retr", ltn12.sink.file(file))
|
2016-12-20 07:34:00 +00:00
|
|
|
if response ~= nil then
|
2019-11-19 15:52:11 +00:00
|
|
|
local __, filename = util.splitFilePathName(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(path)),
|
2019-07-21 19:45:02 +00:00
|
|
|
})
|
|
|
|
else
|
|
|
|
UIManager:show(ConfirmBox:new{
|
2019-09-17 12:27:51 +00:00
|
|
|
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(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(path)
|
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
2016-12-20 07:34:00 +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(path)),
|
2016-12-20 07:34:00 +00:00
|
|
|
timeout = 3,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Ftp:config(item, callback)
|
2020-07-25 13:26:26 +00:00
|
|
|
local text_info = _([[
|
|
|
|
The FTP address must be in the following format:
|
|
|
|
ftp://example.domain.com
|
|
|
|
An IP address is also supported, for example:
|
|
|
|
ftp://10.10.10.1
|
|
|
|
Username and password are optional.]])
|
2016-12-20 07:34:00 +00:00
|
|
|
local hint_name = _("Your FTP name")
|
|
|
|
local text_name = ""
|
|
|
|
local hint_address = _("FTP address eg ftp://example.com")
|
|
|
|
local text_address = ""
|
|
|
|
local hint_username = _("FTP username")
|
|
|
|
local text_username = ""
|
|
|
|
local hint_password = _("FTP password")
|
|
|
|
local text_password = ""
|
2018-02-22 23:02:32 +00:00
|
|
|
local hint_folder = _("FTP folder")
|
|
|
|
local text_folder = "/"
|
2016-12-20 07:34:00 +00:00
|
|
|
local title
|
|
|
|
local text_button_right = _("Add")
|
|
|
|
if item then
|
|
|
|
title = _("Edit FTP account")
|
|
|
|
text_button_right = _("Apply")
|
|
|
|
text_name = item.text
|
|
|
|
text_address = item.address
|
|
|
|
text_username = item.username
|
|
|
|
text_password = item.password
|
2018-10-18 04:34:29 +00:00
|
|
|
text_folder = item.url
|
2016-12-20 07:34:00 +00:00
|
|
|
else
|
|
|
|
title = _("Add FTP 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",
|
2018-09-18 18:21:14 +00:00
|
|
|
text_type = "password",
|
2016-12-20 07:34:00 +00:00
|
|
|
hint = hint_password,
|
|
|
|
},
|
2018-02-22 23:02:32 +00:00
|
|
|
{
|
|
|
|
text = text_folder,
|
|
|
|
input_type = "string",
|
|
|
|
hint = hint_folder,
|
|
|
|
},
|
2016-12-20 07:34:00 +00:00
|
|
|
},
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
2022-03-04 20:20:00 +00:00
|
|
|
id = "close",
|
2016-12-20 07:34:00 +00:00
|
|
|
callback = function()
|
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Info"),
|
|
|
|
callback = function()
|
2017-05-08 07:26:01 +00:00
|
|
|
UIManager:show(InfoMessage:new{ text = text_info })
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_button_right,
|
|
|
|
callback = function()
|
2022-10-06 15:11:47 +00:00
|
|
|
local fields = self.settings_dialog:getFields()
|
2016-12-20 07:34:00 +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
|
2017-05-08 07:26:01 +00:00
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Please fill in all fields.")
|
|
|
|
})
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
input_type = "text",
|
|
|
|
}
|
|
|
|
UIManager:show(self.settings_dialog)
|
2018-03-30 10:46:36 +00:00
|
|
|
self.settings_dialog:onShowKeyboard()
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Ftp:info(item)
|
|
|
|
local info_text = T(_"Type: %1\nName: %2\nAddress: %3", "FTP", item.text, item.address)
|
|
|
|
UIManager:show(InfoMessage:new{text = info_text})
|
|
|
|
end
|
|
|
|
|
|
|
|
return Ftp
|