🚚 (split): 从月读拆出独立仓库,沿用原书源 id
Release / release (push) Successful in 10s

This commit is contained in:
Garfield committed 2026-11-27 08:08:02 +08:00
commit 3598b9f825
21 files changed
+2240

No files matched your search

@@ -0,0 +1,240 @@
--[[--
BookOrbit 源设置:服务器地址、KOReader 同步账号、书库选择、测试连接;
装过 BookOrbit 官方插件时可一键导入它的账号。
配置经月读 utils.settings.getSource / saveSource("bookorbit") 落盘;密码只存 MD5(userkey)。
@module moonsource_bookorbit.setting
--]]
local Client = require("moonsource_bookorbit.client")
local Text = require("utils.text")
local _ = require("l10n").get("moonsource_bookorbit")
local T = require("ffi/util").template
local ID = "bookorbit"
local Setting = {}
---@return table
local function config()
return require("utils.settings").getSource(ID)
end
--- 服务器身份:地址 + 用户名。换了就是另一套书架。
local function libraryFingerprint(cfg)
return table.concat({
Client.normalizeUrl(cfg.server_url or ""),
Text.trim(cfg.username or ""),
}, "\0")
end
---@param text string
local function toast(text)
require("ui.themes.widgets.toast"):new{ text = text, timeout = 3 }:show()
end
--- 落盘并重建源实例;换服务器 / 用户保留记录,先准备同步。
---@param cfg table
---@param previous_fingerprint string|nil
---@return boolean
local function save(cfg, previous_fingerprint)
local function commit()
require("utils.settings").saveSource(ID, cfg)
end
if previous_fingerprint and previous_fingerprint ~= libraryFingerprint(cfg) then
if not require("source.logout").replaceLibrary(ID, commit) then
toast(_("准备本地同步失败"))
return false
end
else
commit()
require("source.registry").reload()
end
return true
end
--- 密码 → x-auth-key(MD5,同 kosync);空串表示清掉。
---@param value string|nil
---@return string
local function userkey(value)
value = Text.trim(value)
return value == "" and "" or require("ffi/sha2").md5(value) --[[@as string]]
end
--- 官方插件存在 G_reader_settings["bookorbit"] 里的账号(它的 server_url 带 /api/v1)。
---@return table|nil
local function officialAccount()
local reader = rawget(_G, "G_reader_settings")
local account = reader and reader:readSetting("bookorbit")
if type(account) ~= "table" then return nil end
local client = Client.new(account)
return client:configured() and client or nil
end
--- 未配置 / 断网当场提示;否则带忙碌提示调 fn(client, done),fn 结束时必须调 done()。
---@param fn fun(client: BookOrbitClient, done: fun())
local function withServer(fn)
local client = Client.new(config())
if not client:configured() then
toast(_("请先填写服务器地址、用户名和密码"))
return
end
if not require("ui/network/manager"):isConnected() then
toast(_("网络不可用,请先连接 Wi-Fi"))
return
end
local busy = require("ui.themes.widgets.toast"):new{ text = _("正在连接 BookOrbit…"), dismissable = false }
busy:show()
fn(client, function() busy:close() end)
end
---@param key string
---@param title string
---@param hint string
---@param password boolean
---@param normalize fun(value: string|nil): string
---@param icon string
---@return fun(iw: number): table
local function field(key, title, hint, password, normalize, icon)
return function(iw)
local value = config()[key] or ""
return require("ui.themes.widgets.row").widget({
width = iw, chevron = true,
kind = "nav", icon = icon, title = title,
status = value ~= "" and (password and "******" or value) or _("未设置"),
status_on = value ~= "",
callback = function()
local cfg = config()
require("ui.themes.widgets.input"):new{
title = title,
-- 密码只存摘要,回填不了原文。
fields = { { text = password and "" or (cfg[key] or ""), hint = hint,
text_type = password and "password" or nil } },
ok_text = _("保存"),
callback = function(input)
local before = libraryFingerprint(cfg)
local next_cfg = require("util").tableDeepCopy(cfg)
next_cfg[key] = normalize(input)
-- 只改密码不算换库;地址 / 用户名变了才撕本地书架。
save(next_cfg, (key == "server_url" or key == "username") and before or nil)
end,
}:show()
end,
})
end
end
--- 多选书库:只同步选中的库;一个都不选 = 全部可访问的书库。
local function pickLibraries()
withServer(function(client, done)
client:json("GET", "/koreader/plugin/catalog/sections/libraries", nil, nil, function(data, err)
done()
if not data then
toast(err)
return
end
local cfg = config()
local chosen = cfg.libraries or {}
local items = {}
for _idx, lib in ipairs(data.items or {}) do
local id = tostring(lib.id)
items[#items + 1] = { text = lib.title or id, value = id, checked = chosen[id] == true }
end
require("ui.themes.widgets.list"):new{
title = _("选择书库(不选 = 全部)"),
multi = true,
items = items,
close_callback = function()
local picked = {}
for _idx, item in ipairs(items) do
if item.checked then picked[item.value] = true end
end
cfg.libraries = picked
save(cfg)
end,
}:show()
end)
end)
end
local function testConnection()
withServer(function(client, done)
client:json("GET", "/koreader/users/auth", nil, nil, function(data, err)
done()
toast(data and data.authorized == "OK" and _("连接正常") or err or _("无法识别服务器响应"))
end)
end)
end
---@return fun(iw: number): table
local function libraryRow()
return function(iw)
local count = 0
for _id in pairs(config().libraries or {}) do count = count + 1 end
return require("ui.themes.widgets.row").widget({
width = iw, chevron = true,
kind = "nav", icon = "library_books", title = _("书库"),
status = count > 0 and T(_("%1 个"), count) or _("全部"),
status_on = true,
callback = pickLibraries,
})
end
end
---@param icon string
---@param title string
---@param callback fun()
---@return fun(iw: number): table
local function action(icon, title, callback)
return function(iw)
return require("ui.themes.widgets.row").widget({
width = iw, kind = "action", icon = icon, title = title, callback = callback,
})
end
end
local function importOfficial()
local account = officialAccount()
if not account then return end
local before = libraryFingerprint(config())
local cfg = require("util").tableDeepCopy(config())
cfg.server_url, cfg.username, cfg.userkey = account.server_url, account.username, account.userkey
if not save(cfg, before) then return end
toast(T(_("已导入账号 %1"), account.username))
end
---@return table[]
function Setting.rows()
local rows = {
field("server_url", _("服务器地址"), "https://books.example.com", false, Client.normalizeUrl, "dns"),
field("username", _("用户名"), _("BookOrbit 设置 → KOReader 里创建的同步账号"), false, Text.trim, "person"),
field("userkey", _("密码"), _("同步账号的密码"), true, userkey, "key"),
libraryRow(),
action("network_check", _("测试连接"), testConnection),
}
local cfg = config()
if (cfg.username or "") ~= "" and (cfg.userkey or "") ~= "" then
rows[#rows + 1] = require("source.logout").row(ID, _("确定退出 BookOrbit 账号?"), function()
local current = config()
current.username, current.userkey = "", ""
-- 只落凭证。reload 会让桌面立刻同步,必须等本地书架清完再 reload。
require("utils.settings").saveSource(ID, current)
end)
end
if officialAccount() then
table.insert(rows, 1, action("download", _("从 BookOrbit 插件导入账号"), importOfficial))
end
return rows
end
--- 远程管理「连接配置」网页上的表单;书库要连服务器挑,仍在设备上选。
Setting.remote = {
{ key = "server_url", label = _("服务器地址"), type = "url", placeholder = "https://books.example.com",
normalize = Client.normalizeUrl },
{ key = "username", label = _("用户名"), normalize = Text.trim },
{ key = "userkey", label = _("密码"), secret = true, normalize = userkey },
}
--- 这些键变了 = 换库,远程保存后整源清本地书架。
Setting.replace_on_remote = { "server_url", "username" }
return Setting