KAuth

kauth-policy-gen-polkit1.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Nicola Gigante <[email protected]>
3  SPDX-FileCopyrightText: 2009-2010 Dario Freddi <[email protected]>
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 
15 const 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 
23 const 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 
30 const char dent[] = " ";
31 
32 void output(const QList<Action> &actions, const QMap<QString, QString> &domain)
33 {
34  QTextStream out(stdout);
35 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
36  out.setCodec("UTF-8");
37 #endif
38 
39  out << header;
40 
41  if (domain.contains(QLatin1String("vendor"))) {
42  out << "<vendor>" << domain[QStringLiteral("vendor")].toHtmlEscaped() << "</vendor>\n";
43  }
44  if (domain.contains(QLatin1String("vendorurl"))) {
45  out << "<vendor_url>" << domain[QStringLiteral("vendorurl")] << "</vendor_url>\n";
46  }
47  if (domain.contains(QLatin1String("icon"))) {
48  out << "<icon_name>" << domain[QStringLiteral("icon")] << "</icon_name>\n";
49  }
50 
51  for (const Action &action : actions) {
52  out << dent << "<action id=\"" << action.name << "\" >\n";
53 
54  // Not a typo, messages and descriptions are actually inverted
55  for (auto i = action.messages.cbegin(); i != action.messages.cend(); ++i) {
56  out << dent << dent << "<description";
57  if (i.key() != QLatin1String("en")) {
58  out << " xml:lang=\"" << i.key() << '"';
59  }
60 
61  out << '>' << i.value().toHtmlEscaped() << "</description>\n";
62  }
63 
64  for (auto i = action.descriptions.cbegin(); i != action.descriptions.cend(); ++i) {
65  out << dent << dent << "<message";
66  if (i.key() != QLatin1String("en")) {
67  out << " xml:lang=\"" << i.key() << '"';
68  }
69 
70  out << '>' << i.value().toHtmlEscaped() << "</message>\n";
71  }
72 
73  QString policy = action.policy;
74  QString policyInactive = action.policyInactive.isEmpty() ? QLatin1String("no") : action.policyInactive;
75  if (!action.persistence.isEmpty() && policy != QLatin1String("yes") && policy != QLatin1String("no")) {
76  policy += QLatin1String("_keep");
77  }
78  if (!action.persistence.isEmpty() && policyInactive != QLatin1String("yes") && policyInactive != QLatin1String("no")) {
79  policyInactive += QLatin1String("_keep");
80  }
81 
82  out << QString(QLatin1String(policy_tag)).arg(policyInactive, policy);
83 
84  out << dent << "</action>\n";
85  }
86 
87  out << "</policyconfig>\n";
88 }
bool contains(const Key &key) const const
KGuiItem yes()
QString message
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Feb 5 2023 04:14:58 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.