From 8bb08b503ab5179322ae88ee2ac0ecf799643e9e Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Thu, 17 Feb 2022 11:53:15 +0200 Subject: [PATCH] Cloud storage: enhance download dialog, fix Dropbox uploading (#8809) (1) ButtonDialogTitle: new method setTitle() (2) OPDSbrowser -use setTitle() instead of open/close ButtonDialogTitle (no visual changes to the download dialog) -reduce logger.info output to avoid flooding crash.log (3) CloudStorage -enhance download dialog (similar to OPDS), much optimized code of downloadFile -fix a bug: cannot create new folder or upload files to the root of the Dropbox storage --- frontend/apps/cloudstorage/cloudstorage.lua | 182 ++++++++++---------- frontend/ui/widget/buttondialogtitle.lua | 7 + plugins/opds.koplugin/opdsbrowser.lua | 47 +++-- 3 files changed, 120 insertions(+), 116 deletions(-) diff --git a/frontend/apps/cloudstorage/cloudstorage.lua b/frontend/apps/cloudstorage/cloudstorage.lua index 607b9de55..c69a1607d 100644 --- a/frontend/apps/cloudstorage/cloudstorage.lua +++ b/frontend/apps/cloudstorage/cloudstorage.lua @@ -206,119 +206,115 @@ function CloudStorage:onMenuSelect(item) end function CloudStorage:downloadFile(item) - local lastdir = G_reader_settings:readSetting("lastdir") - local cs_settings = self:readSettings() - local download_dir = cs_settings:readSetting("download_dir") or lastdir - local path = download_dir .. '/' .. item.text - self:cloudFile(item, path) -end - -function CloudStorage:cloudFile(item, path) - local download_text = _("Downloading. This might take a moment.") - local function dropboxDownloadFile(unit_item, password, path_dir, callback_close) + local function startDownloadFile(unit_item, address, username, password, path_dir, callback_close) UIManager:scheduleIn(1, function() - DropBox:downloadFile(unit_item, password, path_dir, callback_close) + if self.type == "dropbox" then + DropBox:downloadFile(unit_item, password, path_dir, callback_close) + elseif self.type == "ftp" then + Ftp:downloadFile(unit_item, address, username, password, path_dir, callback_close) + elseif self.type == "webdav" then + WebDav:downloadFile(unit_item, address, username, password, path_dir, callback_close) + end end) UIManager:show(InfoMessage:new{ - text = download_text, + text = _("Downloading. This might take a moment."), timeout = 1, }) end - local function ftpDownloadFile(unit_item, address, username, password, path_dir, callback_close) - UIManager:scheduleIn(1, function() - Ftp:downloadFile(unit_item, address, username, password, path_dir, callback_close) - end) - UIManager:show(InfoMessage:new{ - text = download_text, - timeout = 1, - }) + local function createTitle(filename_orig, filename, path) -- title for ButtonDialogTitle + return T(_("Filename:\n%1\n\nDownload filename:\n%2\n\nDownload folder:\n%3"), + filename_orig, filename, BD.dirpath(path)) end - local function webdavDownloadFile(unit_item, address, username, password, path_dir, callback_close) - UIManager:scheduleIn(1, function() - WebDav:downloadFile(unit_item, address, username, password, path_dir, callback_close) - end) - UIManager:show(InfoMessage:new{ - text = download_text, - timeout = 1, - }) - end + local cs_settings = self:readSettings() + local download_dir = cs_settings:readSetting("download_dir") or G_reader_settings:readSetting("lastdir") + local filename_orig = item.text + local filename = filename_orig - local path_dir = path - local overwrite_text = _("File already exists. Would you like to overwrite it?") local buttons = { { { - text = _("Download file"), + text = _("Choose folder"), callback = function() - if self.type == "dropbox" then - local callback_close = function() - self:onClose() - end - UIManager:close(self.download_dialog) - if lfs.attributes(path) then - UIManager:show(ConfirmBox:new{ - text = overwrite_text, - ok_callback = function() - dropboxDownloadFile(item, self.password, path_dir, callback_close) - end - }) - else - dropboxDownloadFile(item, self.password, path_dir, callback_close) - end - elseif self.type == "ftp" then - local callback_close = function() - self:onClose() - end - UIManager:close(self.download_dialog) - if lfs.attributes(path) then - UIManager:show(ConfirmBox:new{ - text = overwrite_text, - ok_callback = function() - ftpDownloadFile(item, self.address, self.username, self.password, path_dir, callback_close) - end - }) - else - ftpDownloadFile(item, self.address, self.username, self.password, path_dir, callback_close) - end - elseif self.type == "webdav" then - local callback_close = function() - self:onClose() - end - UIManager:close(self.download_dialog) - if lfs.attributes(path) then - UIManager:show(ConfirmBox:new{ - text = overwrite_text, - ok_callback = function() - webdavDownloadFile(item, self.address, self.username, self.password, path_dir, callback_close) - end - }) - else - webdavDownloadFile(item, self.address, self.username, self.password, path_dir, callback_close) - end - end + require("ui/downloadmgr"):new{ + show_hidden = G_reader_settings:readSetting("show_hidden"), + onConfirm = function(path) + self.cs_settings:saveSetting("download_dir", path) + self.cs_settings:flush() + download_dir = path + self.download_dialog:setTitle(createTitle(filename_orig, filename, download_dir)) + end, + }:chooseDir(download_dir) + end, + }, + { + text = _("Change filename"), + callback = function() + local input_dialog + input_dialog = InputDialog:new{ + title = _("Enter filename"), + input = filename, + input_hint = filename_orig, + buttons = { + { + { + text = _("Cancel"), + callback = function() + UIManager:close(input_dialog) + end, + }, + { + text = _("Set filename"), + is_enter_default = true, + callback = function() + filename = input_dialog:getInputValue() + if filename == "" then + filename = filename_orig + end + UIManager:close(input_dialog) + self.download_dialog:setTitle(createTitle(filename_orig, filename, download_dir)) + end, + }, + } + }, + } + UIManager:show(input_dialog) + input_dialog:onShowKeyboard() end, }, }, { { - text = _("Choose download folder"), + text = _("Cancel"), callback = function() - require("ui/downloadmgr"):new{ - show_hidden = G_reader_settings:readSetting("show_hidden"), - onConfirm = function(path_download) - self.cs_settings:saveSetting("download_dir", path_download) - self.cs_settings:flush() - path_dir = path_download .. '/' .. item.text - end, - }:chooseDir() + UIManager:close(self.download_dialog) + end, + }, + { + text = _("Download"), + callback = function() + UIManager:close(self.download_dialog) + local path_dir = (download_dir ~= "/" and download_dir or "") .. '/' .. filename + local callback_close = function() self:onClose() end + if lfs.attributes(path_dir) then + UIManager:show(ConfirmBox:new{ + text = _("File already exists. Would you like to overwrite it?"), + ok_callback = function() + startDownloadFile(item, self.address, self.username, self.password, path_dir, callback_close) + end + }) + else + startDownloadFile(item, self.address, self.username, self.password, path_dir, callback_close) + end end, }, }, } - self.download_dialog = ButtonDialog:new{ - buttons = buttons + + self.download_dialog = ButtonDialogTitle:new{ + title = createTitle(filename_orig, filename, download_dir), + buttons = buttons, } UIManager:show(self.download_dialog) end @@ -627,8 +623,9 @@ function CloudStorage:uploadFile(url) timeout = 1, }) end) + local url_base = url ~= "/" and url or "" UIManager:tickAfterNext(function() - DropBox:uploadFile(url, self.password, file_path, callback_close) + DropBox:uploadFile(url_base, self.password, file_path, callback_close) end) end end @@ -655,16 +652,17 @@ function CloudStorage:createFolder(url) local folder_name = input_dialog:getInputText() if folder_name == "" then return end UIManager:close(input_dialog) + local url_base = url ~= "/" and url or "" local callback_close = function() if check_button_enter_folder.checked then table.insert(self.paths, { url = url, }) - url = url .. "/" .. folder_name + url = url_base .. "/" .. folder_name end self:openCloudServer(url) end - DropBox:createFolder(url, self.password, folder_name, callback_close) + DropBox:createFolder(url_base, self.password, folder_name, callback_close) end, }, } diff --git a/frontend/ui/widget/buttondialogtitle.lua b/frontend/ui/widget/buttondialogtitle.lua index bb318d56b..6dd615f5d 100644 --- a/frontend/ui/widget/buttondialogtitle.lua +++ b/frontend/ui/widget/buttondialogtitle.lua @@ -87,6 +87,13 @@ function ButtonDialogTitle:init() } end +function ButtonDialogTitle:setTitle(title) + self.title = title + self:free() + self:init() + UIManager:setDirty("all", "ui") +end + function ButtonDialogTitle:onShow() UIManager:setDirty(self, function() return "ui", self[1][1].dimen diff --git a/plugins/opds.koplugin/opdsbrowser.lua b/plugins/opds.koplugin/opdsbrowser.lua index 701965bf3..34b2dc598 100644 --- a/plugins/opds.koplugin/opdsbrowser.lua +++ b/plugins/opds.koplugin/opdsbrowser.lua @@ -90,7 +90,7 @@ end -- This function is a callback fired from the new -- catalog dialog, 'addNewCatalog'. function OPDSBrowser:addServerFromInput(fields) - logger.info("New OPDS catalog input:", fields) + logger.dbg("New OPDS catalog input:", fields) local new_server = { title = fields[1], url = (fields[2]:match("^%a+://") and fields[2] or "http://" .. fields[2]), @@ -291,7 +291,7 @@ function OPDSBrowser:fetchFeed(item_url, username, password, method) user = username, password = password, } - logger.info("Request:", request) + logger.dbg("Request:", request) -- Fire off the request and wait to see what we get back. local code, headers = socket.skip(1, http.request(request)) socketutil:reset_timeout() @@ -603,7 +603,7 @@ function OPDSBrowser:downloadFile(item, filename, remote_url) local download_dir = self.getCurrentDownloadDir() filename = util.getSafeFilename(filename, download_dir) - local local_path = download_dir .. "/" .. filename + local local_path = (download_dir ~= "/" and download_dir or "") .. '/' .. filename local_path = util.fixUtf8(local_path, "_") local function download() @@ -670,20 +670,18 @@ function OPDSBrowser:downloadFile(item, filename, remote_url) end end -function OPDSBrowser:createNewDownloadDialog(path, filename, buttons) - self.download_dialog = ButtonDialogTitle:new{ - title = T(_("Download folder:\n%1\n\nDownload filename:\n%2\n\nDownload file type:"), - BD.dirpath(path), filename), - buttons = buttons - } -end - function OPDSBrowser:showDownloads(item) local acquisitions = item.acquisitions local filename = item.title if item.author then filename = item.author .. " - " .. filename end + local filename_orig = filename + + local function createTitle(path, file) -- title for ButtonDialogTitle + return T(_("Download folder:\n%1\n\nDownload filename:\n%2\n\nDownload file type:"), + BD.dirpath(path), file) + end local buttons = {} -- buttons for ButtonDialogTitle @@ -723,15 +721,11 @@ function OPDSBrowser:showDownloads(item) callback = function() require("ui/downloadmgr"):new{ onConfirm = function(path) - logger.info("Download folder set to", path) + logger.dbg("Download folder set to", path) G_reader_settings:saveSetting("download_dir", path) - UIManager:nextTick(function() - UIManager:close(self.download_dialog) - self:createNewDownloadDialog(path, filename, buttons) - UIManager:show(self.download_dialog) - end) + self.download_dialog:setTitle(createTitle(path, filename)) end, - }:chooseDir() + }:chooseDir(self.getCurrentDownloadDir()) end, }, { @@ -741,6 +735,7 @@ function OPDSBrowser:showDownloads(item) input_dialog = InputDialog:new{ title = _("Enter filename"), input = filename, + input_hint = filename_orig, buttons = { { { @@ -754,10 +749,11 @@ function OPDSBrowser:showDownloads(item) is_enter_default = true, callback = function() filename = input_dialog:getInputValue() + if filename == "" then + filename = filename_orig + end UIManager:close(input_dialog) - UIManager:close(self.download_dialog) - self:createNewDownloadDialog(self.getCurrentDownloadDir(), filename, buttons) - UIManager:show(self.download_dialog) + self.download_dialog:setTitle(createTitle(self.getCurrentDownloadDir(), filename)) end, }, } @@ -790,7 +786,10 @@ function OPDSBrowser:showDownloads(item) }, }) - self:createNewDownloadDialog(self.getCurrentDownloadDir(), filename, buttons) + self.download_dialog = ButtonDialogTitle:new{ + title = createTitle(self.getCurrentDownloadDir(), filename), + buttons = buttons, + } UIManager:show(self.download_dialog) end @@ -873,7 +872,7 @@ function OPDSBrowser:onMenuSelect(item) end function OPDSBrowser:editServerFromInput(item, fields) - logger.info("Edit OPDS catalog input:", fields) + logger.dbg("Edit OPDS catalog input:", fields) for _, server in ipairs(self.opds_servers) do if server.title == item.text or server.url == item.url then server.title = fields[1] @@ -887,7 +886,7 @@ function OPDSBrowser:editServerFromInput(item, fields) end function OPDSBrowser:editOPDSServer(item) - logger.info("Edit OPDS Server:", item) + logger.dbg("Edit OPDS Server:", item) self.edit_server_dialog = MultiInputDialog:new{ title = _("Edit OPDS catalog"), fields = {