From 778a1585970d2cd10ee659905c0282527448d44f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 14 May 2025 07:20:01 +0800 Subject: [PATCH] WebUI: add versioning to local preferences And provide migration path for changing preferences. Fixes #22639. PR #22677. --- src/webui/www/private/scripts/client.js | 2 + .../www/private/scripts/localpreferences.js | 42 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/webui/www/private/scripts/client.js b/src/webui/www/private/scripts/client.js index fa78fd12d..599273585 100644 --- a/src/webui/www/private/scripts/client.js +++ b/src/webui/www/private/scripts/client.js @@ -175,6 +175,8 @@ let setStatusFilter = () => {}; let toggleFilterDisplay = () => {}; window.addEventListener("DOMContentLoaded", (event) => { + window.qBittorrent.LocalPreferences.upgrade(); + let isSearchPanelLoaded = false; let isLogPanelLoaded = false; let isRssPanelLoaded = false; diff --git a/src/webui/www/private/scripts/localpreferences.js b/src/webui/www/private/scripts/localpreferences.js index 73f341127..345b11774 100644 --- a/src/webui/www/private/scripts/localpreferences.js +++ b/src/webui/www/private/scripts/localpreferences.js @@ -1,5 +1,6 @@ /* * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2025 Mike Tzou (Chocobo1) * Copyright (C) 2019 Thomas Piccirello * * This program is free software; you can redistribute it and/or @@ -32,7 +33,8 @@ window.qBittorrent ??= {}; window.qBittorrent.LocalPreferences ??= (() => { const exports = () => { return { - LocalPreferences: LocalPreferences + LocalPreferences: LocalPreferences, + upgrade: upgrade }; }; @@ -53,6 +55,10 @@ window.qBittorrent.LocalPreferences ??= (() => { } } + size() { + return localStorage.length; + } + remove(key) { try { localStorage.removeItem(key); @@ -63,6 +69,40 @@ window.qBittorrent.LocalPreferences ??= (() => { } } + const localPreferences = new LocalPreferences(); + + const upgrade = () => { + const MIGRATION_VERSION = 1; + const MIGRATION_VERSION_KEY = "MigrationVersion"; + + // clean start + if (localPreferences.size() === 0) { + localPreferences.set(MIGRATION_VERSION_KEY, MIGRATION_VERSION); + return; + } + + // already in use + const version = Number(localPreferences.get(MIGRATION_VERSION_KEY)); // `0` on first initialization + if (version !== MIGRATION_VERSION) { + if (version < 1) + resetSideFilters(); + + localPreferences.set(MIGRATION_VERSION_KEY, MIGRATION_VERSION); + } + }; + + const resetSideFilters = () => { + // conditionally reset the filter to default to avoid none selected + const clear = (key) => { + const value = Number(localPreferences.get(key)); + if ((value === 1) || (value === 2)) // affected values + localPreferences.remove(key); + }; + clear("selected_category"); + clear("selected_tag"); + clear("selected_tracker"); + }; + return exports(); })(); Object.freeze(window.qBittorrent.LocalPreferences);