|
| 1 | +import QtQuick |
| 2 | + |
| 3 | +// import "code/utils.js" as Utils |
| 4 | + |
| 5 | +Item { |
| 6 | + id: root |
| 7 | + property ListModel model: ListModel {} |
| 8 | + property bool isLoading: true |
| 9 | + readonly property string plasmaEnvFile: "$HOME/.config/plasma-workspace/env/luisbocanegra.smart.video.wallpaper.reborn.sh" |
| 10 | + |
| 11 | + signal updated |
| 12 | + signal loaded |
| 13 | + |
| 14 | + function initModel(envVars) { |
| 15 | + model.clear(); |
| 16 | + let varList = envVars.split("\n"); |
| 17 | + for (let envVar of varList) { |
| 18 | + let [name, value] = envVar.split("="); |
| 19 | + const existsIndex = getIndex(name); |
| 20 | + if (existsIndex !== -1) { |
| 21 | + model.set(existsIndex, { |
| 22 | + name, |
| 23 | + value |
| 24 | + }); |
| 25 | + continue; |
| 26 | + } |
| 27 | + model.append({ |
| 28 | + name, |
| 29 | + value |
| 30 | + }); |
| 31 | + } |
| 32 | + root.isLoading = false; |
| 33 | + loaded(); |
| 34 | + } |
| 35 | + |
| 36 | + function getValue(name) { |
| 37 | + for (let i = 0; i < model.count; i++) { |
| 38 | + const item = model.get(i); |
| 39 | + if (item.name === name) { |
| 40 | + return item.value; |
| 41 | + } |
| 42 | + } |
| 43 | + return ""; |
| 44 | + } |
| 45 | + |
| 46 | + function getIndex(name) { |
| 47 | + for (let i = 0; i < model.count; i++) { |
| 48 | + const item = model.get(i); |
| 49 | + if (item.name === name) { |
| 50 | + return i; |
| 51 | + } |
| 52 | + } |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + function getPlasmaEnvVar(name, c) { |
| 57 | + runCommand.exec(`grep 'export ${name}' "${plasmaEnvFile}" | sed 's/export ${name}=//'`, output => { |
| 58 | + if (c) { |
| 59 | + if (output.exitCode === 0 && output.stdout.length > 0) { |
| 60 | + return c(output.stdout.trim()); |
| 61 | + } |
| 62 | + return c(null); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + function setPlasmaEnvVar(name, value) { |
| 68 | + // check if it exists |
| 69 | + // if it does replace |
| 70 | + // if it does not, append |
| 71 | + runCommand.exec(`grep 'export ${name}' "${plasmaEnvFile}"`, output => { |
| 72 | + if (output.exitCode === 0 && output.stdout.length > 0) { |
| 73 | + console.log(`${name}`, output.stdout); |
| 74 | + console.log(`sed -i 's/export ${name}=.*/export ${name}=${value}/g' "${plasmaEnvFile}"`); |
| 75 | + runCommand.exec(`sed -i 's/export ${name}=.*/export ${name}=${value}/g' "${plasmaEnvFile}"`); |
| 76 | + } else { |
| 77 | + runCommand.exec(`touch "${plasmaEnvFile}" && echo "export ${name}=${value}" >> "${plasmaEnvFile}"`); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + function removePlasmaEnvVar(name) { |
| 83 | + console.log(`sed -i '/export ${name}=.*/d' "${plasmaEnvFile}"`); |
| 84 | + runCommand.exec(`sed -i '/export ${name}=.*/d' "${plasmaEnvFile}"`); |
| 85 | + } |
| 86 | + |
| 87 | + function varExists(name) { |
| 88 | + return getValue(name) !== ""; |
| 89 | + } |
| 90 | + |
| 91 | + RunCommand { |
| 92 | + id: runCommand |
| 93 | + } |
| 94 | + |
| 95 | + function getVars() { |
| 96 | + let vars; |
| 97 | + runCommand.exec(`printenv`, output => { |
| 98 | + if (output.exitCode === 0 && output.stdout.length > 0) { |
| 99 | + initModel(output.stdout.trim()); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + Component.onCompleted: { |
| 105 | + getVars(); |
| 106 | + } |
| 107 | +} |
0 commit comments