From a63c22b6fb257c5bd1432ec96aa6e9fe61ee652d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Fdez?= Date: Wed, 1 Sep 2021 16:48:46 +0200 Subject: [PATCH] calibre: gui to choose default extension --- plugins/calibre.koplugin/extensions.lua | 105 +++++++++++++++++------- plugins/calibre.koplugin/main.lua | 47 ++++++++++- plugins/calibre.koplugin/wireless.lua | 4 +- 3 files changed, 123 insertions(+), 33 deletions(-) diff --git a/plugins/calibre.koplugin/extensions.lua b/plugins/calibre.koplugin/extensions.lua index 79e301338..1a59f1465 100644 --- a/plugins/calibre.koplugin/extensions.lua +++ b/plugins/calibre.koplugin/extensions.lua @@ -1,37 +1,80 @@ --[[ File formats supported by KOReader. These are reported when the device talks with calibre wireless server. - Note that the server can allow or restrict file formats based on calibre configuration for each device. - Optionally KOReader users can set their own supported formats to report to the server. + calibre assumes that the list is in desired order. + When sending documents if no format on the list exists then calibre converts the book to the first format. + + See https://www.mobileread.com/forums/showthread.php?t=341423 --]] -local user_path = require("datastorage"):getDataDir() .. "/calibre-extensions.lua" -local ok, extensions = pcall(dofile, user_path) - -if ok then - return extensions -else - return { - "azw", - "cbz", - "chm", - "djv", - "djvu", - "doc", - "docx", - "epub", - "fb2", - "htm", - "html", - "md", - "mobi", - "pdb", - "pdf", - "prc", - "rtf", - "txt", - "xhtml", - "xps", - "zip", - } +local valid_ext = { + "epub", + "fb2", + "mobi", + "azw", + "xps", + "doc", + "docx", + "djv", + "djvu", + "pdf", + "cbz", + "htm", + "html", + "xhtml", + "pdb", + "prc", + "rtf", + "txt", + "md", + "chm", + "zip", +} + +-- if the file "calibre-extensions.lua", under dataDir, returns a table +-- then use it instead of default extensions. +local function getCustomConfig() + local path = require("datastorage"):getDataDir() + local ok, extensions = pcall(dofile, string.format("%s/%s", path, "calibre-extensions.lua")) + if ok then return extensions end +end + +local CalibreExtensions = { + outputs = { "epub", "mobi", "docx", "fb2", "pdf", "txt" }, + default_output = G_reader_settings:readSetting("calibre_wireless_default_format") or "epub", + user_overrides = getCustomConfig(), +} + +function CalibreExtensions:get() + if type(self.user_overrides) == "table" then + return self.user_overrides + else + local sorted = {} + sorted[1] = self.default_output + for _, v in ipairs(valid_ext) do + if v ~= self.default_output then + sorted[#sorted+1] = v + end + end + return sorted + end end + +function CalibreExtensions:getInfo() + local str = "" + local t = self:get() + for i, v in ipairs(t) do + if i == #t then + str = str .. v + else + str = str .. v .. ", " + end + end + return str +end + +function CalibreExtensions:isCustom() + return self.user_overrides ~= nil +end + +return CalibreExtensions diff --git a/plugins/calibre.koplugin/main.lua b/plugins/calibre.koplugin/main.lua index a02383613..78a9cf081 100644 --- a/plugins/calibre.koplugin/main.lua +++ b/plugins/calibre.koplugin/main.lua @@ -8,6 +8,7 @@ --]] local BD = require("ui/bidi") +local CalibreExtensions = require("extensions") local CalibreSearch = require("search") local CalibreWireless = require("wireless") local Dispatcher = require("dispatcher") @@ -225,7 +226,8 @@ function Calibre:getWirelessMenuTable() local enabled = G_reader_settings:nilOrTrue("calibre_wireless") return enabled and not CalibreWireless.calibre_socket end - return { + + local t = { { text = _("Enable wireless client"), separator = true, @@ -335,6 +337,49 @@ function Calibre:getWirelessMenuTable() }, }, } + + if not CalibreExtensions:isCustom() then + table.insert(t, 2, { + text = _("File formats"), + enabled_func = isEnabled, + sub_item_table_func = function() + local submenu = { + { + text = _("About formats"), + keep_menu_open = true, + separator = true, + callback = function() + UIManager:show(InfoMessage:new{ + text = string.format("%s: %s \n\n%s", + _("Supported file formats"), + CalibreExtensions:getInfo(), + _("Unsupported formats will be converted by calibre to the first format of the list.")) + }) + end, + } + } + + for i, v in ipairs(CalibreExtensions.outputs) do + table.insert(submenu, {}) + submenu[i+1].text = v + submenu[i+1].checked_func = function() + if v == CalibreExtensions.default_output then + return true + end + return false + end + submenu[i+1].callback = function() + if type(v) == "string" and v ~= CalibreExtensions.default_output then + CalibreExtensions.default_output = v + G_reader_settings:saveSetting("calibre_wireless_default_format", CalibreExtensions.default_output) + end + end + end + return submenu + end, + }) + end + return t end return Calibre diff --git a/plugins/calibre.koplugin/wireless.lua b/plugins/calibre.koplugin/wireless.lua index 2a6d4a5a4..cb3461ce7 100644 --- a/plugins/calibre.koplugin/wireless.lua +++ b/plugins/calibre.koplugin/wireless.lua @@ -4,6 +4,7 @@ --]] local BD = require("ui/bidi") +local CalibreExtensions = require("extensions") local CalibreMetadata = require("metadata") local CalibreSearch = require("search") local ConfirmBox = require("ui/widget/confirmbox") @@ -25,7 +26,8 @@ local T = FFIUtil.template require("ffi/zeromq_h") -- supported formats -local extensions = require("extensions") +local extensions = CalibreExtensions:get() + local function getExtensionPathLengths() local t = {} for _, v in ipairs(extensions) do