-- Undo accidental openings without modifying bookorbit.koplugin on disk. -- Server support is required. See README.md before enabling/disabling this patch. -- A persistent hold protects new openings from sweeps and delayed uploads. local Opening = {} local KEYS = { "percent_finished", "last_xpointer", "last_page", "last_page_position" } local MAX_AGE = 24 * 60 * 60 local function copy(value) if type(value) ~= "table" then return value end local result = {} for k, v in pairs(value) do result[k] = copy(v) end return result end -- Only the files involved in lifecycle, queues, and upload acknowledgement are -- pinned. MD5 is a compatibility fingerprint, not a download security check. -- Each profile is a complete tested source set, never a mix of releases. -- Ignore only the release literal; unrelated updates need no new fingerprint. local TESTED_PROFILES = { { -- 2855dbb8a39d20bf711f01772e2678ef0625dd63 (plugin 1.5.5) files = { ["main.lua"] = "0fb0dcfe30ce494812c8d692498649a2", ["bookorbit_api.lua"] = "bdeaeb095543878e2902e3ea8e7f9522", ["bookorbit_book_sync.lua"] = "a796ae57941a69c80a9b2d7578e3c858", ["bookorbit_sweep.lua"] = "c3bd9f40996b4aa1b4beaeb439b9da19", ["bookorbit_main_menu.lua"] = "40392af3dab84f329cdd8065b2fa765e", ["bookorbit_sync_coordinator.lua"] = "2494dc3c629a693957597c98ca3457cd", ["bookorbit_sync_job_runner.lua"] = "c6b54c96b1c1514f85ae74cb42bcb7b9", ["bookorbit_lifecycle_outbox.lua"] = "8ed7c3d6910e05d0d6533441d1a7c353", ["bookorbit_progress_sync.lua"] = "7609f401e37b2aabb7d42aa424595b8f", }, }, { -- c59dd0118fd0d5d0ecbd716d1d7fde2c97f8e2ea (plugin 2.0.0) menu_sheet = true, files = { ["main.lua"] = "6c79fc50f6dfd6c6510f23c3d2355a51", ["bookorbit_api.lua"] = "f6177967c71277b6467148cf567c0694", ["bookorbit_book_sync.lua"] = "baa7f06387bde4d5ad09c2121efee36d", ["bookorbit_sweep.lua"] = "8bd472821092263b62639032681656a5", ["bookorbit_main_menu.lua"] = "94a453099b6446740bfce9944155e484", ["bookorbit_sync_coordinator.lua"] = "2494dc3c629a693957597c98ca3457cd", ["bookorbit_sync_job_runner.lua"] = "c6b54c96b1c1514f85ae74cb42bcb7b9", ["bookorbit_lifecycle_outbox.lua"] = "0e08df00d6271be1736660e00fcbf6cc", ["bookorbit_progress_sync.lua"] = "36f075d7e7ea3153d86a232d70005e1f", ["bookorbit_menu_sheet.lua"] = "541ce1b61f000f105f24a12312647414", }, }, { -- a96ff0c85a4c25e9126dfa4c71ea87537a3baf7a (plugin 2.1.0) menu_sheet = true, files = { ["main.lua"] = "449a37ce45618a3784d61d28621b625d", ["bookorbit_api.lua"] = "f6177967c71277b6467148cf567c0694", ["bookorbit_book_sync.lua"] = "e58f479b65314bef049fb8d0a2ea4052", ["bookorbit_sweep.lua"] = "7a0fb83cda1034ea2827ef1dc8886c19", ["bookorbit_main_menu.lua"] = "4e8c0231ed6b42995872e5e449c839cb", ["bookorbit_sync_coordinator.lua"] = "2494dc3c629a693957597c98ca3457cd", ["bookorbit_sync_job_runner.lua"] = "c6b54c96b1c1514f85ae74cb42bcb7b9", ["bookorbit_lifecycle_outbox.lua"] = "0e08df00d6271be1736660e00fcbf6cc", ["bookorbit_progress_sync.lua"] = "e3f6748e92ac7e2ec875836246be9532", ["bookorbit_menu_sheet.lua"] = "28bc5a291483f8178e09a2e97926da98", }, }, { -- 6f8f5f4818eb737dc4d78fc5b5294fc3adb26860 (plugin 2.2.0) menu_sheet = true, files = { ["main.lua"] = "0eacdea5bc7d975ba2f963b89ab689e8", ["bookorbit_api.lua"] = "f6177967c71277b6467148cf567c0694", ["bookorbit_book_sync.lua"] = "e58f479b65314bef049fb8d0a2ea4052", ["bookorbit_sweep.lua"] = "7a0fb83cda1034ea2827ef1dc8886c19", ["bookorbit_main_menu.lua"] = "44584e1d6b59868d897ce816e0a829ff", ["bookorbit_sync_coordinator.lua"] = "2494dc3c629a693957597c98ca3457cd", ["bookorbit_sync_job_runner.lua"] = "c6b54c96b1c1514f85ae74cb42bcb7b9", ["bookorbit_lifecycle_outbox.lua"] = "0e08df00d6271be1736660e00fcbf6cc", ["bookorbit_progress_sync.lua"] = "e3f6748e92ac7e2ec875836246be9532", ["bookorbit_menu_sheet.lua"] = "24667ce247739dfff65e40cb3f4b3866", }, }, } function Opening.account(client, create) if not G_reader_settings then return nil end local all = G_reader_settings:readSetting("bookorbit_openings", {}) local url = tostring(client.server_url):gsub("^http://localhost([:/])", "http://127.0.0.1%1"):gsub("/+$", "") local key = url .. "\n" .. tostring(client.username) if create and not all[key] then all[key] = { ignored_stats = {} } end if create then G_reader_settings:saveSetting("bookorbit_openings", all) end return all[key] end local function persist() G_reader_settings:flush() end local function liveReader(plugin, row) local ReaderUI = package.loaded["apps/reader/readerui"] local ui = ReaderUI and ReaderUI.instance or plugin.ui return ui and ui.document and ui.document.file == row.file and ui or nil end function Opening.record(plugin) local account = Opening.account(plugin.settings) return account and account.opening end function Opening.held(client, digest) local account = Opening.account(client) local row = account and account.opening return row and row.digest == digest and not row.local_restored and row.phase ~= "done" and row.phase ~= "undone" end function Opening.available(plugin) local row = Opening.record(plugin) return row and (row.phase == "undo_pending" or ((row.phase == "candidate" or row.phase == "active") and os.time() - row.opened_at < MAX_AGE)) end function Opening.blockedRequest(client, method, path, body) if method == "GET" or not body or path:find("/openings", 1, true) then return false end if path == "/koreader/syncs/progress" then return Opening.held(client, body.document) end if not path:match("^/koreader/plugin/") or path:find("match-check", 1, true) then return false end for _, entry in ipairs(body.books or body.items or {}) do if Opening.held(client, entry.hash) then return true end end return false end function Opening.filterStats(client, books) local account = Opening.account(client) local filtered, skipped = {}, {} for _, book in ipairs(books) do local events = {} local intervals = account and account.ignored_stats[book.hash] or {} for _, event in ipairs(book.events or {}) do local ignore = false for _, interval in ipairs(intervals or {}) do if event.startTime >= interval[1] and event.startTime <= interval[2] then ignore = true; break end end if ignore then skipped[book.hash] = math.max(skipped[book.hash] or 0, event.startTime) else table.insert(events, event) end end if #events > 0 then local item = copy(book) item.events = events table.insert(filtered, item) end end return filtered, skipped end local function uuid() return ("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", function(c) return string.format("%x", c == "x" and math.random(0, 15) or math.random(8, 11)) end) end local function savedPositionUnchanged(ds, snapshot) if not snapshot then return false end local dump = require("dump") for _, key in ipairs(KEYS) do if dump(ds:readSetting(key)) ~= dump(snapshot[key]) then return false end end return dump(ds:readSetting("annotations")) == dump(snapshot.annotations) end local function closedUnchanged(ds, row) return savedPositionUnchanged(ds, row.after_close) and require("dump")(ds:readSetting("summary")) == require("dump")(row.after_close.summary) end local function readerUnchanged(ui, row) local summary = ui.doc_settings:readSetting("summary") or {} local dump = require("dump") return ui:getCurrentPage() == row.initial_page and (summary.status == nil or summary.status == "new" or summary.status == "reading") and dump(ui.annotation and ui.annotation.annotations or {}) == dump(row.annotations) end function Opening.beforeLoad(plugin, ds) plugin.opening_before = nil plugin.opening_reopen = nil if not plugin:isLoggedIn() or not plugin.settings.auto_sync then return end if not ds or not plugin.ui or not plugin.ui.document then return end -- Check the closed sidecar BEFORE KOReader updates its summary on reopen. -- A new reader may legitimately change summary.modified, but must not hide -- a manual status/progress/annotation change made while the book was closed. local previous = Opening.record(plugin) if previous and previous.closed_at and previous.file == plugin.ui.document.file and not previous.local_restored and Opening.available(plugin) and closedUnchanged(ds, previous) then plugin.opening_reopen = { row = previous, closed_at = previous.closed_at } end local summary = ds:readSetting("summary") or {} if (summary.status and summary.status ~= "new") or (ds:readSetting("percent_finished") or 0) > 0 then return end local before = {} for _, key in ipairs(KEYS) do before[key] = copy(ds:readSetting(key)) end before.status = summary.status before.modified = summary.modified local history = require("readhistory") local index = history:getIndexByFile(plugin.ui.document.file) before.history_time = index and history.hist[index].time plugin.opening_before = before end function Opening.capture(plugin) plugin.opening_session_id = nil local before, reopen = plugin.opening_before, plugin.opening_reopen plugin.opening_before, plugin.opening_reopen = nil, nil if not plugin:isLoggedIn() or not plugin.settings.auto_sync then return end local ui = plugin.ui if not ui or not ui.document or not ui.doc_settings then return end local previous = Opening.record(plugin) if previous and previous.phase ~= "done" and previous.phase ~= "undone" then -- ReaderReady is not a page turn or a book switch. Reuse the original -- operation after duplicate readiness or a same-cover close/reopen, -- including an offline pending undo. Never renew its ID or 24h window. if Opening.available(plugin) and not previous.local_restored and previous.file == ui.document.file and previous.digest == plugin:getDocumentDigest() and readerUnchanged(ui, previous) and (not previous.closed_at or (reopen and reopen.row == previous and reopen.closed_at == previous.closed_at and savedPositionUnchanged(ui.doc_settings, previous.after_close))) then plugin.opening_session_id = previous.id if previous.closed_at then previous.closed_at, previous.after_close = nil, nil persist() end return end if previous.phase ~= "undo_pending" then previous.phase = "committing"; persist() end return end if not before then return end local digest = plugin:getDocumentDigest() if not digest then return end local account = Opening.account(plugin.settings, true) account.opening = { id = uuid(), digest = digest, file = ui.document.file, title = (ui.doc_props and ui.doc_props.title) or ui.document.file:match("([^/]+)$"), before = before, opened_at = os.time(), phase = "candidate", initial_page = ui:getCurrentPage(), annotations = copy(ui.annotation and ui.annotation.annotations or {}), } plugin.opening_session_id = account.opening.id persist() end local function payload(plugin, row) return { id = row.id, document = row.digest, deviceId = plugin.device_id } end function Opening.ensure(plugin) local row = Opening.record(plugin) if not row or row.phase == "done" or row.phase == "undone" or row.phase == "undo_pending" then return true end if os.time() - row.opened_at >= MAX_AGE then row.phase = "committing" end if (row.phase == "candidate" or row.phase == "active") and not Opening.localUnchanged(plugin, row) then row.phase = "committing" persist() end local client = plugin:newClient() if not row.registered then row.sent = true persist() local body, err = client:request("POST", "/koreader/plugin/openings", payload(plugin, row)) if not body then if err == 404 or err == 409 then row.phase = "done"; persist(); return true end return false end row.registered = true row.remote_pending = body.hardcoverPending if body.phase == "committed" or body.phase == "undone" then row.phase = "done" end persist() end if row.phase == "committing" then local body, err = client:request("POST", "/koreader/plugin/openings/commit", payload(plugin, row)) if not body and err ~= 409 and err ~= 404 then return false end row.phase = "done" persist() elseif row.phase == "candidate" then row.phase = "active"; persist() end return true end function Opening.pageTurn(plugin, page) local row = Opening.record(plugin) if not row or not plugin.ui or not plugin.ui.document or row.file ~= plugin.ui.document.file then return end if (row.phase == "active" or row.phase == "candidate" or (row.phase == "undo_pending" and not row.local_restored)) and page ~= nil and page ~= row.initial_page then row.phase = "committing" persist() plugin:requestOpeningSync() end end function Opening.keep(plugin) local row = Opening.record(plugin) if row and (row.phase == "candidate" or row.phase == "active") then row.phase = "committing" persist() end end function Opening.close(plugin) local row = Opening.record(plugin) if not row or plugin.opening_session_id ~= row.id or not plugin.ui.document or row.file ~= plugin.ui.document.file then return end if (row.phase == "candidate" or row.phase == "active") and not Opening.localUnchanged(plugin, row) then Opening.keep(plugin) end row.closed_at = os.time() local ds = plugin.ui.doc_settings row.after_close = {} for _, key in ipairs(KEYS) do row.after_close[key] = copy(ds:readSetting(key)) end row.after_close.summary = copy(ds:readSetting("summary")) row.after_close.annotations = copy(ds:readSetting("annotations")) persist() end function Opening.localUnchanged(plugin, row) if row.local_restored then return true end local ui = liveReader(plugin, row) if ui then return not row.closed_at and readerUnchanged(ui, row) end if not row.after_close then return false end local ds = require("docsettings"):open(row.file) return closedUnchanged(ds, row) end function Opening.finishLocal(plugin, row) if row.local_restored then return end local UIManager = require("ui/uimanager") local reader = liveReader(plugin, row) -- Reader close flushes its in-memory sidecar; restore only after that flush. if reader then reader:onClose() end local ds = require("docsettings"):open(row.file) for _, key in ipairs(KEYS) do if row.before[key] == nil then ds:delSetting(key) else ds:saveSetting(key, copy(row.before[key])) end end local summary = ds:readSetting("summary") or {} summary.status = row.before.status or "new" summary.modified = row.before.modified ds:saveSetting("summary", summary) ds:flush() local history = require("readhistory") history:removeItemByPath(row.file) if row.before.history_time then history:addItem(row.file, row.before.history_time) end local BookList = require("ui/widget/booklist") BookList.setBookInfoCacheProperty(row.file, "status", summary.status) BookList.setBookInfoCacheProperty(row.file, "percent_finished", row.before.percent_finished or 0) local account = Opening.account(plugin.settings, true) account.ignored_stats[row.digest] = account.ignored_stats[row.digest] or {} table.insert(account.ignored_stats[row.digest], { row.opened_at, row.closed_at or os.time() }) row.local_restored = true persist() if reader then reader:showFileManager(row.file) end UIManager:broadcastEvent(require("ui/event"):new("BookMetadataChanged", { file = row.file, key = "summary" })) UIManager:setDirty("all", "ui") end function Opening.undo(plugin, expected_id) local row = Opening.record(plugin) if expected_id and (not row or row.id ~= expected_id) then return false, "The book changed; nothing was reset." end if not Opening.available(plugin) then return false, "This opening can no longer be undone." end if not Opening.localUnchanged(plugin, row) then row.phase = "committing" persist() plugin:requestOpeningSync() return false, "Reading or annotations changed; nothing was reset." end row.phase = "undo_pending" persist() local pending = false if row.sent then local client = plugin:newClient() if not row.registered then local body, err = client:request("POST", "/koreader/plugin/openings", payload(plugin, row)) if not body then if err == 404 or err == 409 then row.phase = "done"; persist() end return false, "Could not confirm the opening. No reading data was reset." end row.registered = true persist() end local body, err = client:request("POST", "/koreader/plugin/openings/undo", payload(plugin, row)) if not body then if err == 409 or err == 404 then row.phase = "done"; persist() return false, "Reading changed on the server. Undo cannot continue; no further data was reset." end return false, "Undo is pending. Reconnect and retry Undo; reading data is held until then." end pending = body.hardcoverPending == true end -- Network requests yield to the UI. Never reset a page turned while waiting. if not Opening.localUnchanged(plugin, row) then row.phase = "done"; persist() return false, "Reading changed while waiting. The local book was not reset; sync it again to keep the new reading." end Opening.finishLocal(plugin, row) row.phase = pending and "undo_pending" or "undone" persist() return true, pending and "Opening undone locally and in BookOrbit. Hardcover still needs attention; retry Undo to finish." or "Opening undone, including its Hardcover reading if one was created." end function Opening.install(BookOrbit) function BookOrbit:onDocSettingsLoad(ds) Opening.beforeLoad(self, ds) end function BookOrbit:requestOpeningSync() if not self:isLoggedIn() or not require("ui/network/manager"):isConnected() then return end self:submitSyncJob{ family = "reading_opening", label = "Reading opening", source = "opening", interactive = false, priority = 300, run = function() Opening.ensure(self) self:requestLifecycleOutboxDrain("opening") end, } end function BookOrbit:onBookOrbitUndoOpening() local row = Opening.record(self) if not Opening.available(self) then return end local UIManager = require("ui/uimanager") local _ = require("gettext") UIManager:show(require("ui/widget/confirmbox"):new{ text = _("Undo this accidental opening?") .. "\n\n" .. tostring(row.title), ok_text = _("Undo opening"), ok_callback = function() if Opening.record(self) ~= row or not Opening.available(self) then return end -- Prevent an automatic commit from superseding an explicit queued undo. if not Opening.localUnchanged(self, row) then row.phase = "committing" persist() self:requestOpeningSync() UIManager:show(require("ui/widget/infomessage"):new{ text = _("Reading or annotations changed; nothing was reset.") }) return end row.phase = "undo_pending" persist() self:submitSyncJob{ family = "reading_opening", label = _("Undo opening"), source = "manual", priority = 400, interactive = true, run = function() local _success, message = Opening.undo(self, row.id) UIManager:show(require("ui/widget/infomessage"):new{ text = _(message) }) end, } end, }) end end local function callable(value) local mt = type(value) == "table" and getmetatable(value) return type(value) == "function" or (type(mt) == "table" and type(mt.__call) == "function") end function Opening.checkCompatibility(plugin) if type(plugin.path) ~= "string" then return false, "missing plugin path" end local md5 = require("ffi/sha2").md5 local function fingerprint(filename) local file = io.open(plugin.path .. "/" .. filename, "rb") if not file then return nil end local source = file:read("*a") file:close() if source and filename == "main.lua" then source = source:gsub('(local PLUGIN_VERSION = )"[^"\n]+"', '%1""', 1) end return source and md5(source) end local main_hash = fingerprint("main.lua") local profile for _, candidate in ipairs(TESTED_PROFILES) do if candidate.files["main.lua"] == main_hash then profile = candidate; break end end if not profile then return false, "untested main.lua" end for filename, expected in pairs(profile.files) do if filename ~= "main.lua" and fingerprint(filename) ~= expected then return false, "untested " .. filename end end for _, name in ipairs({ "onReaderReady", "onBookOrbitSyncBook", "onBookOrbitPushProgress", "onDispatcherRegisterActions", "_onCloseDocument", "_onPageUpdate", "_onResume", "_onNetworkConnected", "submitSyncJob", "matchOpenBookForAutoSync", "addToMainMenu", "requestLifecycleOutboxDrain", "registerEvents", }) do if not callable(plugin[name]) then return false, "missing method " .. name end end if plugin.onDocSettingsLoad or plugin.onBookOrbitUndoOpening then return false, "another undo implementation is installed" end if profile.menu_sheet and (not callable(plugin.openMenuSheet) or not callable(plugin.onMenuSheetAction)) then return false, "missing menu sheet methods" end for module, methods in pairs({ bookorbit_api = { "request", "uploadPageStats" }, bookorbit_book_sync = { "capture" }, bookorbit_sweep = { "run" }, }) do local value = package.loaded[module] for _, method in ipairs(methods) do if type(value) ~= "table" or type(value[method]) ~= "function" then return false, "missing " .. module .. "." .. method end end end return true, nil, profile end function Opening.hasProtectedData() local accounts = G_reader_settings and G_reader_settings:readSetting("bookorbit_openings", {}) or {} assert(type(accounts) == "table", "invalid opening accounts") local protected = false for _, account in pairs(accounts) do assert(type(account) == "table" and type(account.ignored_stats) == "table", "invalid opening account") local row = account.opening if row and row.phase ~= "done" and row.phase ~= "undone" then protected = true end for _, intervals in pairs(account.ignored_stats) do assert(type(intervals) == "table", "invalid excluded statistics") if next(intervals) then protected = true end end end return protected end local notices = {} local function notice(text) if notices[text] then return end notices[text] = true require("logger").warn("Ereader undo patch:", text) local UIManager = require("ui/uimanager") UIManager:nextTick(function() UIManager:show(require("ui/widget/infomessage"):new{ text = text }) end) end local shared_installed = false local function installSharedHooks() if shared_installed then return end local Api = require("bookorbit_api") local request = Api.request function Api:request(method, path, body, ...) if Opening.blockedRequest(self, method, path, body) then return nil, "opening_held" end return request(self, method, path, body, ...) end local upload = Api.uploadPageStats function Api:uploadPageStats(books) for _, book in ipairs(books) do if Opening.held(self, book.hash) then return nil, "opening_held" end end local filtered, skipped = Opening.filterStats(self, books) local response, err, errbody if #filtered > 0 then response, err, errbody = upload(self, filtered) if not response then return nil, err, errbody end else response = { results = {} } end response.results = response.results or {} for hash, watermark in pairs(skipped) do local found = false for _, result in ipairs(response.results) do if result.hash == hash then result.watermark = math.max(result.watermark or 0, watermark) found = true break end end if not found then local submitted = false for _, book in ipairs(filtered) do if book.hash == hash then submitted = true; break end end -- Only locally excluded events may be acknowledged without a server result. if not submitted then table.insert(response.results, { hash = hash, watermark = watermark }) end end end return response end local BookSync = require("bookorbit_book_sync") local capture = BookSync.capture function BookSync.capture(plugin, ...) local row = Opening.record(plugin) if row and plugin.opening_session_id == row.id and plugin:getDocumentDigest() == row.digest and row.phase ~= "done" and row.phase ~= "committing" then return nil end return capture(plugin, ...) end local Sweep = require("bookorbit_sweep") local run = Sweep.run function Sweep.run(opts) local account = Opening.account(opts.api) if account and account.opening and Opening.held(opts.api, account.opening.digest) then -- Do not reach into private sweep steps or falsely acknowledge skipped books. -- Per-book sync for other books remains available while this manual sweep waits. if opts.interactive then require("ui/uimanager"):show(require("ui/widget/infomessage"):new{ text = "Undo the accidental opening, or continue reading it, before syncing the whole library.", }) end return false end return run(opts) end shared_installed = true end local function before(plugin, name, hook) local original = plugin[name] plugin[name] = function(self, ...) hook(self, ...) return original(self, ...) end end local WRITE_JOBS = { lifecycle_outbox = true, progress_push = true, sweep = true, book_snapshot = true, annotation_exchange = true } local menu_sheet_installed = false local function installMenuSheet() if menu_sheet_installed then return end -- Load lazily, like the stock plugin. Keep its layout and action dispatcher; -- only supply an extra row while this particular opening is eligible. local Sheet = require("bookorbit_menu_sheet") local data, body = Sheet.data, Sheet.body function Sheet.data(plugin, catalog) local d = data(plugin, catalog) if plugin._ereader_undo_installed and plugin:isLoggedIn() and Opening.available(plugin) then local row = Opening.record(plugin) d.ereader_undo = { id = row.id, title = row.title } end return d end function Sheet.body(d, width, opts) local widgets = body(d, width, opts) if d.ereader_undo then local MenuUI = require("bookorbit_menu_ui") table.insert(widgets, 3, MenuUI.group(width, { MenuUI.row(MenuUI.rowsIn(width), { icon = "rotate-ccw", label = require("gettext")("Undo accidental opening"), sub = d.ereader_undo.title, sub_data = true, action = { kind = "ereader-undo-opening", opening_id = d.ereader_undo.id }, }), })) end return widgets end menu_sheet_installed = true end local function apply(plugin, profile) Opening.install(plugin) installSharedHooks() before(plugin, "onReaderReady", Opening.capture) before(plugin, "_onCloseDocument", Opening.close) before(plugin, "_onPageUpdate", Opening.pageTurn) before(plugin, "onBookOrbitSyncBook", Opening.keep) before(plugin, "onBookOrbitPushProgress", Opening.keep) before(plugin, "_onResume", function(self) self:requestOpeningSync() end) before(plugin, "_onNetworkConnected", function(self) self:requestOpeningSync() end) before(plugin, "onDispatcherRegisterActions", function() require("dispatcher"):registerAction("bookorbit_undo_opening", { category = "none", event = "BookOrbitUndoOpening", title = require("gettext")("BookOrbit: undo accidental opening"), general = true, }) end) local match = plugin.matchOpenBookForAutoSync function plugin:matchOpenBookForAutoSync(callback, ...) return match(self, function(matched, ...) if matched then Opening.ensure(self) end if callback then return callback(matched, ...) end end, ...) end local submit = plugin.submitSyncJob function plugin:submitSyncJob(job) if WRITE_JOBS[job.family] then local wrapped = {} for key, value in pairs(job) do wrapped[key] = value end wrapped.run = function(...) Opening.ensure(self) return job.run(...) end return submit(self, wrapped) end return submit(self, job) end local menu = plugin.addToMainMenu function plugin:addToMainMenu(items) menu(self, items) if self:isLoggedIn() and Opening.available(self) and items.bookorbit then table.insert(items.bookorbit.sub_item_table, 1, { id = "undo_opening", text = require("gettext")("Undo accidental opening"), enabled_func = function() return Opening.available(self) end, callback = function() self:onBookOrbitUndoOpening() end, }) end end if profile.menu_sheet then before(plugin, "openMenuSheet", installMenuSheet) local action = plugin.onMenuSheetAction function plugin:onMenuSheetAction(page, catalog, item) if item.kind ~= "ereader-undo-opening" then return action(self, page, catalog, item) end -- A sheet can outlive a book or account change. Never act on a -- different opening from the one whose title the user tapped. local row = Opening.record(self) if self:isLoggedIn() and row and row.id == item.opening_id and Opening.available(self) then page:close() self:onBookOrbitUndoOpening() end return true end end plugin._ereader_undo_installed = true end -- registerPatchPluginFunc runs AFTER plugin:init(). The preflight must run -- BEFORE init so an incompatible sync client cannot drain protected data first. -- Only BookOrbit is intercepted; other plugins use the original factory unchanged. local PluginLoader = require("pluginloader") local create = PluginLoader.createPluginInstance function PluginLoader:createPluginInstance(plugin, attr) if plugin.name ~= "bookorbit" then return create(self, plugin, attr) end local readable, protected = pcall(Opening.hasProtectedData) if not readable then notice("BookOrbit undo patch: saved opening data is unreadable. BookOrbit sync is paused to protect reading history. Keep the saved state and repair it before syncing.") return false, "BookOrbit undo state hold" end if not plugin._ereader_undo_installed then local ok, compatible, reason, profile = pcall(Opening.checkCompatibility, plugin) if not ok then compatible, reason = false, "compatibility check failed" end if not compatible then notice("BookOrbit undo patch: " .. tostring(reason) .. ".\n\n" .. (protected and "BookOrbit sync is paused to protect a pending undo or excluded reading statistics. Update the patch in Patch Manager and restart KOReader. Do not delete its saved state." or "Undo is unavailable. Ordinary BookOrbit sync is unchanged. Update the patch in Patch Manager and restart KOReader.")) if protected then return false, "BookOrbit undo compatibility hold" end return create(self, plugin, attr) end apply(plugin, profile) end local ok, instance = create(self, plugin, attr) if ok then require("ui/uimanager"):scheduleIn(5, function() instance:requestOpeningSync() end) end return ok, instance end return Opening