mirror of
https://github.com/AvengeMedia/DankMaterialShell.git
synced 2026-08-01 19:18:28 -04:00
cc: migrate some settings to cache
port 1.5
This commit is contained in:
@@ -11,16 +11,27 @@ Singleton {
|
||||
id: root
|
||||
readonly property var log: Log.scoped("CacheData")
|
||||
|
||||
readonly property int cacheConfigVersion: 1
|
||||
readonly property int cacheConfigVersion: 2
|
||||
|
||||
readonly property string _stateUrl: StandardPaths.writableLocation(StandardPaths.GenericCacheLocation)
|
||||
readonly property string _stateDir: Paths.strip(_stateUrl)
|
||||
|
||||
property bool _loading: false
|
||||
property bool _hasLoaded: false
|
||||
property int _loadedCacheVersion: 0
|
||||
|
||||
readonly property var _pinKeys: ["brightnessDevicePins", "wifiNetworkPins", "bluetoothDevicePins", "audioInputDevicePins", "audioOutputDevicePins"]
|
||||
readonly property var _dataKeys: ["wallpaperLastPath", "profileLastPath", "fileBrowserSettings"].concat(_pinKeys)
|
||||
|
||||
property string wallpaperLastPath: ""
|
||||
property string profileLastPath: ""
|
||||
|
||||
property var brightnessDevicePins: ({})
|
||||
property var wifiNetworkPins: ({})
|
||||
property var bluetoothDevicePins: ({})
|
||||
property var audioInputDevicePins: ({})
|
||||
property var audioOutputDevicePins: ({})
|
||||
|
||||
property var fileBrowserSettings: ({
|
||||
"wallpaper": {
|
||||
"lastPath": "",
|
||||
@@ -78,8 +89,46 @@ Singleton {
|
||||
|
||||
function loadCache() {
|
||||
_loading = true;
|
||||
parseCache(cacheFile.text());
|
||||
_loading = false;
|
||||
try {
|
||||
parseCache(cacheFile.text());
|
||||
} finally {
|
||||
_loading = false;
|
||||
_hasLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
function set(key, value) {
|
||||
if (_dataKeys.indexOf(key) < 0) {
|
||||
log.warn("Unknown cache key:", key);
|
||||
return;
|
||||
}
|
||||
root[key] = value;
|
||||
saveCache();
|
||||
}
|
||||
|
||||
function migratePins(pins) {
|
||||
if (!pins)
|
||||
return;
|
||||
if (!_hasLoaded)
|
||||
loadCache();
|
||||
if (_loadedCacheVersion >= cacheConfigVersion)
|
||||
return;
|
||||
|
||||
let migrated = false;
|
||||
for (const key of _pinKeys) {
|
||||
const legacy = pins[key];
|
||||
if (!legacy || Object.keys(legacy).length === 0)
|
||||
continue;
|
||||
if (Object.keys(root[key] || {}).length > 0)
|
||||
continue;
|
||||
root[key] = legacy;
|
||||
migrated = true;
|
||||
}
|
||||
|
||||
if (!migrated)
|
||||
return;
|
||||
log.info("Migrated device pins from settings.json");
|
||||
saveCache();
|
||||
}
|
||||
|
||||
function parseCache(content) {
|
||||
@@ -87,6 +136,7 @@ Singleton {
|
||||
try {
|
||||
if (content && content.trim()) {
|
||||
const cache = JSON.parse(content);
|
||||
_loadedCacheVersion = cache.configVersion || 0;
|
||||
|
||||
wallpaperLastPath = cache.wallpaperLastPath !== undefined ? cache.wallpaperLastPath : "";
|
||||
profileLastPath = cache.profileLastPath !== undefined ? cache.profileLastPath : "";
|
||||
@@ -122,6 +172,10 @@ Singleton {
|
||||
};
|
||||
}
|
||||
|
||||
for (const key of _pinKeys) {
|
||||
root[key] = cache[key] !== undefined ? cache[key] : {};
|
||||
}
|
||||
|
||||
if (cache.configVersion === undefined) {
|
||||
migrateFromUndefinedToV1(cache);
|
||||
cleanupUnusedKeys();
|
||||
@@ -138,12 +192,16 @@ Singleton {
|
||||
function saveCache() {
|
||||
if (_loading)
|
||||
return;
|
||||
cacheFile.setText(JSON.stringify({
|
||||
const data = {
|
||||
"wallpaperLastPath": wallpaperLastPath,
|
||||
"profileLastPath": profileLastPath,
|
||||
"fileBrowserSettings": fileBrowserSettings,
|
||||
"configVersion": cacheConfigVersion
|
||||
}, null, 2));
|
||||
};
|
||||
for (const key of _pinKeys) {
|
||||
data[key] = root[key];
|
||||
}
|
||||
cacheFile.setText(JSON.stringify(data, null, 2));
|
||||
}
|
||||
|
||||
function migrateFromUndefinedToV1(cache) {
|
||||
@@ -151,7 +209,7 @@ Singleton {
|
||||
}
|
||||
|
||||
function cleanupUnusedKeys() {
|
||||
const validKeys = ["wallpaperLastPath", "profileLastPath", "fileBrowserSettings", "configVersion"];
|
||||
const validKeys = _dataKeys.concat(["configVersion"]);
|
||||
|
||||
try {
|
||||
const content = cacheFile.text();
|
||||
|
||||
@@ -15,7 +15,7 @@ Singleton {
|
||||
id: root
|
||||
readonly property var log: Log.scoped("SettingsData")
|
||||
|
||||
readonly property int settingsConfigVersion: 12
|
||||
readonly property int settingsConfigVersion: 13
|
||||
|
||||
enum Position {
|
||||
Top,
|
||||
@@ -790,11 +790,6 @@ Singleton {
|
||||
property bool fadeToDpmsEnabled: true
|
||||
property int fadeToDpmsGracePeriod: 5
|
||||
property string launchPrefix: ""
|
||||
property var brightnessDevicePins: ({})
|
||||
property var wifiNetworkPins: ({})
|
||||
property var bluetoothDevicePins: ({})
|
||||
property var audioInputDevicePins: ({})
|
||||
property var audioOutputDevicePins: ({})
|
||||
|
||||
property bool gtkThemingEnabled: false
|
||||
property bool qtThemingEnabled: false
|
||||
@@ -1757,6 +1752,7 @@ Singleton {
|
||||
let obj = (txt && txt.trim()) ? JSON.parse(txt) : null;
|
||||
|
||||
const oldVersion = obj?.configVersion ?? 0;
|
||||
const legacyPins = oldVersion < 13 ? Store.extractPins(obj) : null;
|
||||
if (oldVersion < settingsConfigVersion) {
|
||||
const migrated = Store.migrateToVersion(obj, settingsConfigVersion);
|
||||
if (migrated) {
|
||||
@@ -1764,6 +1760,8 @@ Singleton {
|
||||
obj = migrated;
|
||||
}
|
||||
}
|
||||
if (legacyPins)
|
||||
Qt.callLater(() => CacheData.migratePins(legacyPins));
|
||||
|
||||
if (obj?.lockScreenActiveMonitor !== undefined) {
|
||||
var oldVal = obj.lockScreenActiveMonitor;
|
||||
|
||||
@@ -373,11 +373,6 @@ var SPEC = {
|
||||
fadeToDpmsEnabled: { def: true },
|
||||
fadeToDpmsGracePeriod: { def: 5 },
|
||||
launchPrefix: { def: "" },
|
||||
brightnessDevicePins: { def: {} },
|
||||
wifiNetworkPins: { def: {} },
|
||||
bluetoothDevicePins: { def: {} },
|
||||
audioInputDevicePins: { def: {} },
|
||||
audioOutputDevicePins: { def: {} },
|
||||
|
||||
gtkThemingEnabled: { def: false, onChange: "regenSystemThemes" },
|
||||
qtThemingEnabled: { def: false, onChange: "regenSystemThemes" },
|
||||
|
||||
@@ -2,6 +2,21 @@
|
||||
|
||||
.import "./SettingsSpec.js" as SpecModule
|
||||
|
||||
var PIN_KEYS = ["brightnessDevicePins", "wifiNetworkPins", "bluetoothDevicePins", "audioInputDevicePins", "audioOutputDevicePins"];
|
||||
|
||||
function extractPins(obj) {
|
||||
if (!obj) return null;
|
||||
|
||||
var pins = null;
|
||||
for (var i = 0; i < PIN_KEYS.length; i++) {
|
||||
var value = obj[PIN_KEYS[i]];
|
||||
if (!value || Object.keys(value).length === 0) continue;
|
||||
if (!pins) pins = {};
|
||||
pins[PIN_KEYS[i]] = value;
|
||||
}
|
||||
return pins;
|
||||
}
|
||||
|
||||
function parse(root, jsonObj) {
|
||||
var SPEC = SpecModule.SPEC;
|
||||
|
||||
@@ -263,6 +278,17 @@ function migrateToVersion(obj, targetVersion) {
|
||||
settings.configVersion = 12;
|
||||
}
|
||||
|
||||
if (currentVersion < 13) {
|
||||
console.info("Migrating settings from version", currentVersion, "to version 13");
|
||||
console.info("Moving device and network pins to cache.json");
|
||||
|
||||
for (var p = 0; p < PIN_KEYS.length; p++) {
|
||||
delete settings[PIN_KEYS[p]];
|
||||
}
|
||||
|
||||
settings.configVersion = 13;
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user