KAuth

kauth-policy-gen-polkit1.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Nicola Gigante <nicola.gigante@gmail.com>
3 SPDX-FileCopyrightText: 2009-2010 Dario Freddi <drf@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.1-or-later
6*/
7
8#include <policy-gen/policy-gen.h>
9
10#include <QDebug>
11#include <QTextStream>
12
13#include <cstdio>
14
15const char header[] =
16 ""
17 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
18 "<!DOCTYPE policyconfig PUBLIC\n"
19 "\"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN\"\n"
20 "\"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd\">\n"
21 "<policyconfig>\n";
22
23const char policy_tag[] =
24 ""
25 " <defaults>\n"
26 " <allow_inactive>%1</allow_inactive>\n"
27 " <allow_active>%2</allow_active>\n"
28 " </defaults>\n";
29
30const char dent[] = " ";
31
32void output(const QList<Action> &actions, const QMap<QString, QString> &domain)
33{
34 QTextStream out(stdout);
35 out << header;
36
37 if (domain.contains(QLatin1String("vendor"))) {
38 out << "<vendor>" << domain[QStringLiteral("vendor")].toHtmlEscaped() << "</vendor>\n";
39 }
40 if (domain.contains(QLatin1String("vendorurl"))) {
41 out << "<vendor_url>" << domain[QStringLiteral("vendorurl")] << "</vendor_url>\n";
42 }
43 if (domain.contains(QLatin1String("icon"))) {
44 out << "<icon_name>" << domain[QStringLiteral("icon")] << "</icon_name>\n";
45 }
46
47 for (const Action &action : actions) {
48 out << dent << "<action id=\"" << action.name << "\" >\n";
49
50 // Not a typo, messages and descriptions are actually inverted
51 for (auto i = action.messages.cbegin(); i != action.messages.cend(); ++i) {
52 out << dent << dent << "<description";
53 if (i.key() != QLatin1String("en")) {
54 out << " xml:lang=\"" << i.key() << '"';
55 }
56
57 out << '>' << i.value().toHtmlEscaped() << "</description>\n";
58 }
59
60 for (auto i = action.descriptions.cbegin(); i != action.descriptions.cend(); ++i) {
61 out << dent << dent << "<message";
62 if (i.key() != QLatin1String("en")) {
63 out << " xml:lang=\"" << i.key() << '"';
64 }
65
66 out << '>' << i.value().toHtmlEscaped() << "</message>\n";
67 }
68
69 QString policy = action.policy;
70 QString policyInactive = action.policyInactive.isEmpty() ? QLatin1String("no") : action.policyInactive;
71 if (!action.persistence.isEmpty() && policy != QLatin1String("yes") && policy != QLatin1String("no")) {
72 policy += QLatin1String("_keep");
73 }
74 if (!action.persistence.isEmpty() && policyInactive != QLatin1String("yes") && policyInactive != QLatin1String("no")) {
75 policyInactive += QLatin1String("_keep");
76 }
77
78 out << QString(QLatin1String(policy_tag)).arg(policyInactive, policy);
79
80 out << dent << "</action>\n";
81 }
82
83 out << "</policyconfig>\n";
84}
bool contains(const Key &key) const const
QString arg(Args &&... args) const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:10 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.