Files
moonsource-legadorule/moonsource-legadorule.koplugin/moonsource_legadorule/setting.lua
T

206 lines
7.4 KiB
Lua

--[[--
开源阅读书源设置:导入 Legado 书源(地址 / 本地文件 / 粘贴 JSON)、启停与删除、书架换源。
@module moonsource_legadorule.setting
--]]
local _ = require("l10n").get("moonsource_legadorule")
local T = require("ffi/util").template
local Setting = {}
function Setting.rowIcon()
return "library_books"
end
---@return string, boolean
function Setting.rowStatus()
local Sources = require("moonsource_legadorule.sources")
local all, on = Sources.all(), #Sources.enabled()
if #all == 0 then return _("未导入"), false end
return T(_("%1 个,启用 %2 个"), #all, on), on > 0
end
local function toast(text)
require("ui.themes.widgets.toast"):new{ text = text, timeout = 3 }:show()
end
local function finishImport(text)
local Sources = require("moonsource_legadorule.sources")
local rules, skipped = Sources.parse(text)
if #rules == 0 then
toast(_("没有找到有效的书源"))
return
end
Sources.add(rules)
require("source.registry").reload()
toast(T(_("已导入 %1 个书源,跳过 %2 个无效项"), #rules, skipped))
end
local function import(value)
local Text = require("utils.text")
value = Text.trim(value)
if value:match("^https?://") then
require("ui/network/manager"):runWhenConnected(function()
local waiting = require("ui.themes.widgets.progress_dialog"):new{
title = _("导入书源"), text = _("正在导入…"), dismissable = false,
}
waiting:show()
require("http.request").get(value, { timeout = 60, allow_redirects = true }, function(body, err)
waiting:close()
if not body then toast(err or _("无法读取书源")) return end
finishImport(body)
end)
end)
elseif value:sub(1, 1) == "/" then
local f = io.open(value, "rb")
if not f then toast(_("无法读取书源")) return end
local body = f:read("*a")
f:close()
finishImport(body)
else
finishImport(value)
end
end
local function importDialog()
require("ui.themes.widgets.input"):new{
title = _("导入书源"),
fields = { { text = "", hint = _("书源地址、本地文件路径或 JSON") } },
ok_text = _("导入"),
callback = function(value) if value and value ~= "" then import(value) end end,
}:show()
end
local function manageOne(entry)
local Sources = require("moonsource_legadorule.sources")
require("ui.themes.widgets.list"):new{
title = entry.name,
items = {
{ text = entry.enabled and _("停用") or _("启用"), value = "toggle" },
{ text = _("删除"), value = "delete" },
},
on_select = function(value)
if value == "toggle" then
Sources.setEnabled(entry.key, not entry.enabled)
toast(entry.enabled and _("已停用") or _("已启用"))
else
Sources.remove(entry.key)
toast(_("已删除"))
end
require("source.registry").reload()
end,
}:show()
end
local function manage()
local Sources = require("moonsource_legadorule.sources")
local items = {}
for _idx, entry in ipairs(Sources.all()) do
items[#items + 1] = { text = (entry.enabled and "" or "✗ ") .. entry.name, value = entry }
end
if #items == 0 then toast(_("未导入")) return end
require("ui.themes.widgets.list"):new{
title = _("管理书源"), items = items, on_select = manageOne,
}:show()
end
-- ── 书架换源:书的身份、进度、阅读时间都不动,只把读取来源指向另一个书源 ──
local function bookKey(book)
return require("moonsource_legadorule.switch").keyOf(book.stable_id)
end
local function applySwitch(book, picked)
local ok, err = require("moonsource_legadorule.switch").apply(book.stable_id, picked.stable_id)
if ok then toast(T(_("已换到 %1"), picked.source_name or "")) else toast(tostring(err)) end
end
local function pickTarget(book, results)
local current = bookKey(book)
local items = {}
for _idx, row in ipairs(results) do
local key = require("moonsource_legadorule.mapper").splitStableId(row.stable_id)
if key ~= current then
local author = (row.authors and row.authors ~= "") and (" · " .. row.authors) or ""
items[#items + 1] = { text = row.title .. author .. " [" .. tostring(row.source_name) .. "]", value = row }
end
end
if #items == 0 then toast(_("其他书源里没有找到这本书")) return end
require("ui.themes.widgets.list"):new{
title = T(_("换源:%1"), book.title),
items = items,
on_select = function(row) applySwitch(book, row) end,
}:show()
end
local function searchTargets(book)
local source = require("source.registry").resolve("legadorule")
if not source then toast(_("书源不可用")) return end
require("ui/network/manager"):runWhenConnected(function()
local waiting = require("ui.themes.widgets.progress_dialog"):new{
title = _("换源"), text = T(_("正在其他书源里搜索《%1》…"), book.title), dismissable = false,
}
waiting:show()
source:searchAsync(book.title, function(res, err)
waiting:close()
if not res then toast(err or _("搜索失败")) return end
local exact, all = {}, res.data or {}
for _idx, row in ipairs(all) do
if row.title == book.title then exact[#exact + 1] = row end
end
pickTarget(book, #exact > 0 and exact or all)
end)
end)
end
local function switchSource()
local Sources = require("moonsource_legadorule.sources")
local alive = {}
for _idx, entry in ipairs(Sources.enabled()) do alive[entry.key] = true end
local items = {}
for _idx, book in ipairs(require("book.books_db").listLibrary()) do
if book.source_id == "legadorule" then
local broken = not alive[bookKey(book) or ""]
items[#items + 1] = { text = (broken and "✗ " or "") .. book.title, value = book, broken = broken }
end
end
if #items == 0 then toast(_("书架里还没有这个书源的书")) return end
-- 来源已失效的书排在前面
table.sort(items, function(a, b)
if a.broken ~= b.broken then return a.broken end
return a.text < b.text
end)
require("ui.themes.widgets.list"):new{
title = _("书架换源"), items = items, on_select = searchTargets,
}:show()
end
---@return table[]
function Setting.rows()
local Row = function() return require("ui.themes.widgets.row") end
return {
function(iw)
local status, on = Setting.rowStatus()
return Row().widget({
width = iw, chevron = true, kind = "nav", icon = "library_books", title = _("管理书源"),
status = status, status_on = on, callback = manage,
})
end,
function(iw)
return Row().widget({
width = iw, chevron = true, kind = "nav", icon = "download", title = _("导入书源"),
callback = importDialog,
})
end,
function(iw)
return Row().widget({
width = iw, chevron = true, kind = "nav", icon = "sync", title = _("书架换源"),
callback = switchSource,
})
end,
}
end
return Setting