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")
|
|
|
|
local DropBoxApi = require("apps/cloudstorage/dropboxapi")
|
2016-12-20 07:34:00 +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")
|
2019-11-19 15:52:11 +00:00
|
|
|
local util = require("util")
|
2017-04-14 18:12:21 +00:00
|
|
|
local T = require("ffi/util").template
|
|
|
|
local _ = require("gettext")
|
2016-12-20 07:34:00 +00:00
|
|
|
|
2017-04-14 18:12:21 +00:00
|
|
|
local DropBox = {}
|
2016-12-20 07:34:00 +00:00
|
|
|
|
2022-10-04 16:33:53 +00:00
|
|
|
function DropBox:getAccessToken(refresh_token, app_key_colon_secret)
|
|
|
|
return DropBoxApi:getAccessToken(refresh_token, app_key_colon_secret)
|
|
|
|
end
|
|
|
|
|
2019-11-18 17:39:45 +00:00
|
|
|
function DropBox:run(url, password, choose_folder_mode)
|
|
|
|
return DropBoxApi:listFolder(url, password, choose_folder_mode)
|
|
|
|
end
|
|
|
|
|
|
|
|
function DropBox:showFiles(url, password)
|
|
|
|
return DropBoxApi:showFiles(url, password)
|
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 DropBox:downloadFile(item, password, path, callback_close)
|
2016-12-20 07:34:00 +00:00
|
|
|
local code_response = DropBoxApi:downloadFile(item.url, password, path)
|
|
|
|
if code_response == 200 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.filename(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
|
|
|
|
|
2019-11-18 17:39:45 +00:00
|
|
|
function DropBox:downloadFileNoUI(url, password, path)
|
|
|
|
local code_response = DropBoxApi:downloadFile(url, password, path)
|
2022-11-22 12:53:10 +00:00
|
|
|
return code_response == 200
|
2019-11-18 17:39:45 +00:00
|
|
|
end
|
|
|
|
|
2022-01-04 11:29:08 +00:00
|
|
|
function DropBox:uploadFile(url, password, file_path, callback_close)
|
|
|
|
local code_response = DropBoxApi:uploadFile(url, password, file_path)
|
|
|
|
local __, filename = util.splitFilePathName(file_path)
|
|
|
|
if code_response == 200 then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("File uploaded:\n%1"), filename),
|
|
|
|
})
|
|
|
|
if callback_close then
|
|
|
|
callback_close()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_("Could not upload file:\n%1"), filename),
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function DropBox:createFolder(url, password, folder_name, callback_close)
|
|
|
|
local code_response = DropBoxApi:createFolder(url, password, folder_name)
|
|
|
|
if code_response == 200 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
|
|
|
|
|
2016-12-20 07:34:00 +00:00
|
|
|
function DropBox:config(item, callback)
|
2022-10-04 16:33:53 +00:00
|
|
|
local text_info = _([[
|
|
|
|
Dropbox access tokens are short-lived (4 hours).
|
2022-11-22 12:53:10 +00:00
|
|
|
To generate new access token please use Dropbox refresh token and <APP_KEY>:<APP_SECRET> string.
|
2022-10-04 16:33:53 +00:00
|
|
|
|
|
|
|
Some of the previously generated long-lived tokens are still valid.]])
|
2022-11-22 12:53:10 +00:00
|
|
|
local text_name, text_token, text_appkey, text_url
|
2016-12-20 07:34:00 +00:00
|
|
|
if item then
|
2022-10-04 16:33:53 +00:00
|
|
|
text_name = item.text
|
|
|
|
text_token = item.password
|
|
|
|
text_appkey = item.address
|
2022-11-22 12:53:10 +00:00
|
|
|
text_url = item.url
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
self.settings_dialog = MultiInputDialog:new {
|
2022-10-04 16:33:53 +00:00
|
|
|
title = _("Dropbox cloud storage"),
|
2016-12-20 07:34:00 +00:00
|
|
|
fields = {
|
|
|
|
{
|
2022-10-04 16:33:53 +00:00
|
|
|
text = text_name,
|
|
|
|
hint = _("Cloud storage displayed name"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = text_token,
|
|
|
|
hint = _("Dropbox refresh token\nor long-lived token (deprecated)"),
|
2016-12-20 07:34:00 +00:00
|
|
|
},
|
|
|
|
{
|
2022-10-04 16:33:53 +00:00
|
|
|
text = text_appkey,
|
|
|
|
hint = _("Dropbox <APP_KEY>:<APP_SECRET>\n(leave blank for long-lived token)"),
|
2016-12-20 07:34:00 +00:00
|
|
|
},
|
2022-11-22 12:53:10 +00:00
|
|
|
{
|
|
|
|
text = text_url,
|
|
|
|
hint = _("Dropbox folder (/ for root)"),
|
|
|
|
},
|
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
|
|
|
|
},
|
|
|
|
{
|
2022-10-04 16:33:53 +00:00
|
|
|
text = _("Save"),
|
2016-12-20 07:34:00 +00:00
|
|
|
callback = function()
|
2022-10-06 15:11:47 +00:00
|
|
|
local fields = self.settings_dialog:getFields()
|
2022-10-04 16:33:53 +00:00
|
|
|
if item then
|
|
|
|
callback(item, fields)
|
2016-12-20 07:34:00 +00:00
|
|
|
else
|
2022-10-04 16:33:53 +00:00
|
|
|
callback(fields)
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
2022-10-04 16:33:53 +00:00
|
|
|
self.settings_dialog:onClose()
|
|
|
|
UIManager:close(self.settings_dialog)
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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 DropBox:info(token)
|
|
|
|
local info = DropBoxApi:fetchInfo(token)
|
2022-10-04 16:33:53 +00:00
|
|
|
local space_usage = DropBoxApi:fetchInfo(token, true)
|
|
|
|
if info and space_usage then
|
|
|
|
local account_type = info.account_type and info.account_type[".tag"]
|
|
|
|
local name = info.name and info.name.display_name
|
|
|
|
local space_total = space_usage.allocation and space_usage.allocation.allocated
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = T(_"Type: %1\nName: %2\nEmail: %3\nCountry: %4\nSpace total: %5\nSpace used: %6",
|
|
|
|
account_type, name, info.email, info.country,
|
|
|
|
util.getFriendlySize(space_total), util.getFriendlySize(space_usage.used)),
|
|
|
|
})
|
2016-12-20 07:34:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return DropBox
|