[plasma/print-manager] /: libkcups: Refactor constructor/helpers for public use

Mike Noe <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 0886eeed9f0126024fa0fb15fd5edb547655fdd8 by Mike Noe.
Committed on 24/07/2026 at 16:12.
Pushed by noee into branch 'master'.

libkcups: Refactor constructor/helpers for public use

Allow the printer attributes to be mutable execept for
name and type; these are key fields for printer id.

Mostly a convenience for creating mock printers for testing.

M  +4    -0    src/libkcups/KCupsPrinter.cpp
M  +2    -7    src/libkcups/KCupsPrinter.h
M  +7    -0    tests/CMakeLists.txt
A  +52   -0    tests/attributetest.cpp     [License: GPL(v2.0+)]

https://invent.kde.org/plasma/print-manager/-/commit/0886eeed9f0126024fa0fb15fd5edb547655fdd8

diff --git a/src/libkcups/KCupsPrinter.cpp b/src/libkcups/KCupsPrinter.cpp
index 3705d1b5..0816c351 100644
--- a/src/libkcups/KCupsPrinter.cpp
+++ b/src/libkcups/KCupsPrinter.cpp
@@ -166,6 +166,10 @@ QStringList KCupsPrinter::checkMarkerLevels() const
 
 void KCupsPrinter::setAttribute(const QString &key, const QVariant &value)
 {
+    if (key == KCUPS_PRINTER_NAME || key == KCUPS_PRINTER_TYPE) {
+        qCDebug(LIBKCUPS, "Changing printer id attributes is not supported, %s == %s", qPrintable(key), qPrintable(value.toString()));
+        return;
+    }
     if (!key.isEmpty()) {
         m_attributes[key] = value;
     }
diff --git a/src/libkcups/KCupsPrinter.h b/src/libkcups/KCupsPrinter.h
index e59c93f4..254d8eb4 100644
--- a/src/libkcups/KCupsPrinter.h
+++ b/src/libkcups/KCupsPrinter.h
@@ -22,6 +22,8 @@ public:
 
     KCupsPrinter();
     explicit KCupsPrinter(const QString &printer, bool isClass = false);
+    explicit KCupsPrinter(const QVariantMap &attributes);
+    void setAttribute(const QString &key, const QVariant &value);
 
     QString name() const;
     bool isClass() const;
@@ -83,14 +85,7 @@ public:
      */
     QStringList checkMarkerLevels() const;
 
-protected:
-    explicit KCupsPrinter(const QVariantMap &attributes);
-    void setAttribute(const QString &key, const QVariant &value);
-
 private:
-    friend class KCupsRequest;
-    friend class PrinterModel;
-
     QString m_printer;
     bool m_isClass;
     QVariantMap m_attributes;
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 15934698..4823e166 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,6 +1,13 @@
 # SPDX-License-Identifier: BSD-3-Clause
 # SPDX-FileCopyrightText: 2026 Mike Noe <[email protected]>
 
+ecm_add_test(
+    attributetest.cpp
+    TEST_NAME attributetests
+    LINK_LIBRARIES kcups Qt::Test
+    NAME_PREFIX "pm-"
+)
+
 ecm_add_test(
     subscriptiontest.cpp
     TEST_NAME subscriptiontests
diff --git a/tests/attributetest.cpp b/tests/attributetest.cpp
new file mode 100644
index 00000000..1c6ae168
--- /dev/null
+++ b/tests/attributetest.cpp
@@ -0,0 +1,52 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Mike Noe <[email protected]>
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include <QObject>
+#include <QTest>
+
+#include <KCupsPrinter.h>
+
+using namespace Qt::Literals::StringLiterals;
+
+class AttributeTest : public QObject
+{
+    Q_OBJECT
+
+private Q_SLOTS:
+
+    void testPrinterAttributes()
+    {
+        // An idle, available printer
+        KCupsPrinter printer{{{KCUPS_PRINTER_NAME, u"test"_s},
+                              {KCUPS_PRINTER_TYPE, CUPS_PRINTER_LOCAL},
+                              {KCUPS_PRINTER_STATE, KCupsPrinter::Idle},
+                              {KCUPS_PRINTER_STATE_MESSAGE, u"none"_s},
+                              {KCUPS_PRINTER_IS_ACCEPTING_JOBS, true},
+                              {KCUPS_PRINTER_IS_SHARED, false}}};
+
+        // Test printer name can't be changed
+        printer.setAttribute(KCUPS_PRINTER_NAME, u"another-test-name"_s);
+        QVERIFY(printer.name() == u"test"_s);
+
+        // Test printer type can't be changed
+        printer.setAttribute(KCUPS_PRINTER_TYPE, CUPS_PRINTER_DISCOVERED);
+        QVERIFY(printer.type() == CUPS_PRINTER_LOCAL);
+
+        // Test (enum) printer state can be changed
+        printer.setAttribute(KCUPS_PRINTER_STATE, KCupsPrinter::Stopped);
+        QVERIFY(printer.state() == KCupsPrinter::Stopped);
+
+        // Test (bool) printer shared can be changed
+        printer.setAttribute(KCUPS_PRINTER_IS_SHARED, true);
+        QVERIFY(printer.isShared());
+
+        // Test (string) printer state msg can be changed
+        printer.setAttribute(KCUPS_PRINTER_STATE_MESSAGE, u"A new state message"_s);
+        QVERIFY(printer.stateMsg() != u"none"_s);
+    }
+};
+
+QTEST_MAIN(AttributeTest)
+#include "attributetest.moc"
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.