Warning, /plasma/print-manager/src/kcm/ui/ConfigValues.qml is written in an unsupported language. File is not indexed.

0001 /**
0002  * SPDX-FileCopyrightText: 2023 Mike Noe <noeerover@gmail.com>
0003  * SPDX-License-Identifier: GPL-2.0-or-later
0004  */
0005 
0006 import QtQuick
0007 
0008 // Config values are {key: value} pairs
0009 // In order to implement apply/reset features, keep
0010 // the initial values immutable.  Changes are stored in pending.
0011 QtObject {
0012 
0013     property var __initial: ({})
0014     property var pending: ({})
0015 
0016     // Check pending based on # of entries (>0) in the object
0017     // otherwise, compare stringify(initial === pending)
0018     property bool usePendingCount: true
0019     property bool hasPending: false
0020 
0021     // remove known unneeded keys (not needed by CUPS)
0022     function clean() {
0023         remove(["device-id", "device-class"
0024                , "device-desc", "device-uris"
0025                , "match", "printer-type"
0026                , "remote", "iconName"])
0027     }
0028 
0029     function init(map = {}) {
0030         __initial = map
0031         pending = Object.assign({}, __initial)
0032     }
0033 
0034     function clear() {
0035         set({})
0036     }
0037 
0038     function remove(keylist) {
0039         if (keylist === undefined)
0040             return
0041 
0042         if (typeof keylist === "object") {
0043             // string array of keys
0044             keylist.forEach(k => delete pending[k])
0045         } else if (typeof keylist === "string") {
0046             // string
0047             delete pending[keylist]
0048         }
0049 
0050         set()
0051     }
0052 
0053     function value(key : string) {
0054         if (key === undefined || key.length === 0) {
0055             return ""
0056         } else {
0057             return pending[key]
0058         }
0059     }
0060 
0061     function add(key : string, value) {
0062         if (key === undefined || key.length === 0 || value === undefined) {
0063             console.warn("KEY and VALUE must have values")
0064         } else {
0065             const obj = {}
0066             obj[key] = value
0067             set(obj)
0068         }
0069     }
0070 
0071     // if obj and has fields, assign obj over pending
0072     // otherwise, clear pending
0073     // always set pending flag
0074     function set(obj) {
0075         if (obj !== undefined && typeof obj === "object") {
0076             if (Object.keys(obj).length > 0) {
0077                 Object.assign(pending, obj)
0078             } else {
0079                 pending = {}
0080             }
0081         }
0082 
0083         if (usePendingCount) {
0084             hasPending = Object.keys(pending).length > 0
0085         } else {
0086             hasPending = JSON.stringify(__initial) !== JSON.stringify(pending)
0087         }
0088     }
0089 
0090     function reset() {
0091         if (usePendingCount) {
0092             pending = {}
0093         } else {
0094             pending = Object.assign({}, __initial)
0095         }
0096         set()
0097     }
0098 }