From 6cb245f86f0e67a7bbad9bd9368dbc3aecc4e4f2 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Fri, 2 Jun 2023 18:55:22 +0300 Subject: [PATCH] MoveToArchive register to Dispatcher (#10524) --- plugins/movetoarchive.koplugin/main.lua | 66 ++++++++++++------------- 1 file changed, 32 insertions(+), 34 deletions(-) diff --git a/plugins/movetoarchive.koplugin/main.lua b/plugins/movetoarchive.koplugin/main.lua index 10ae94ce4..57100d403 100644 --- a/plugins/movetoarchive.koplugin/main.lua +++ b/plugins/movetoarchive.koplugin/main.lua @@ -1,9 +1,8 @@ local ConfirmBox = require("ui/widget/confirmbox") local DataStorage = require("datastorage") +local Dispatcher = require("dispatcher") local DocSettings = require("docsettings") -local ReadCollection = require("readcollection") -local ReadHistory = require("readhistory") -local ReaderUI = require("apps/reader/readerui") +local Event = require("ui/event") local FileManager = require("apps/filemanager/filemanager") local InfoMessage = require("ui/widget/infomessage") local LuaSettings = require("frontend/luasettings") @@ -15,9 +14,15 @@ local _ = require("gettext") local MoveToArchive = WidgetContainer:extend{ name = "movetoarchive", + title = _("Move current book to archive"), } +function MoveToArchive:onDispatcherRegisterActions() + Dispatcher:registerAction(self.name, {category="none", event="MoveToArchive", title=self.title, reader=true}) +end + function MoveToArchive:init() + self:onDispatcherRegisterActions() self.ui.menu:registerToMainMenu(self) self.settings = LuaSettings:open(("%s/%s"):format(DataStorage:getSettingsDir(), "move_to_archive_settings.lua")) self.archive_dir_path = self.settings:readSetting("archive_dir") @@ -29,15 +34,15 @@ function MoveToArchive:addToMainMenu(menu_items) text = _("Move to archive"), sub_item_table = { { - text = _("Move current book to archive"), - callback = function() self:moveToArchive() end, + text = self.title, + callback = function() self:onMoveToArchive() end, enabled_func = function() return self:isActionEnabled() end, }, { text = _("Copy current book to archive"), - callback = function() self:copyToArchive() end, + callback = function() self:onMoveToArchive(true) end, enabled_func = function() return self:isActionEnabled() end, @@ -45,23 +50,23 @@ function MoveToArchive:addToMainMenu(menu_items) { text = _("Go to archive folder"), callback = function() - if not self.archive_dir_path then + if self.archive_dir_path then + self:openFileBrowser(self.archive_dir_path) + else self:showNoArchiveConfirmBox() - return end - self:openFileBrowser(self.archive_dir_path) end, }, { text = _("Go to last copied/moved from folder"), callback = function() - if not self.last_copied_from_dir then + if self.last_copied_from_dir then + self:openFileBrowser(self.last_copied_from_dir) + else UIManager:show(InfoMessage:new{ text = _("No previous folder found.") - }) - return + }) end - self:openFileBrowser(self.last_copied_from_dir) end, }, { @@ -70,22 +75,12 @@ function MoveToArchive:addToMainMenu(menu_items) callback = function() self:setArchiveDirectory() end, - } + }, }, } end -function MoveToArchive:moveToArchive() - local move_done_text = _("Book moved.\nDo you want to open it from the archive folder?") - self:commonProcess(true, move_done_text) -end - -function MoveToArchive:copyToArchive() - local copy_done_text = _("Book copied.\nDo you want to open it from the archive folder?") - self:commonProcess(false, copy_done_text) -end - -function MoveToArchive:commonProcess(is_move_process, moved_done_text) +function MoveToArchive:onMoveToArchive(do_copy) if not self.archive_dir_path then self:showNoArchiveConfirmBox() return @@ -93,26 +88,29 @@ function MoveToArchive:commonProcess(is_move_process, moved_done_text) local document_full_path = self.ui.document.file local filename self.last_copied_from_dir, filename = util.splitFilePathName(document_full_path) + local dest_file = string.format("%s%s", self.archive_dir_path, filename) self.settings:saveSetting("last_copied_from_dir", self.last_copied_from_dir) self.settings:flush() - local Event = require("ui/event") UIManager:broadcastEvent(Event:new("SetupShowReader")) self.ui:onClose() - if is_move_process then - FileManager:moveFile(document_full_path, self.archive_dir_path) - else + local text + if do_copy then + text = _("Book copied.\nDo you want to open it from the archive folder?") FileManager:copyFileFromTo(document_full_path, self.archive_dir_path) + else + text = _("Book moved.\nDo you want to open it from the archive folder?") + FileManager:moveFile(document_full_path, self.archive_dir_path) + require("readhistory"):updateItemByPath(document_full_path, dest_file) -- (will update "lastfile" if needed) + require("readcollection"):updateItemByPath(document_full_path, dest_file) end - local dest_file = string.format("%s%s", self.archive_dir_path, filename) - DocSettings:updateLocation(document_full_path, dest_file, not is_move_process) - ReadHistory:updateItemByPath(document_full_path, dest_file) -- (will update "lastfile" if needed) - ReadCollection:updateItemByPath(document_full_path, dest_file) + DocSettings:updateLocation(document_full_path, dest_file, do_copy) UIManager:show(ConfirmBox:new{ - text = moved_done_text, + text = text, ok_callback = function() + local ReaderUI = require("apps/reader/readerui") ReaderUI:showReader(dest_file) end, cancel_callback = function()