| 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 | |
| 15 | const char [] = |
| 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 | out << header; |
| 36 | |
| 37 | if (domain.contains(key: QLatin1String("vendor" ))) { |
| 38 | out << "<vendor>" << domain[QStringLiteral("vendor" )].toHtmlEscaped() << "</vendor>\n" ; |
| 39 | } |
| 40 | if (domain.contains(key: QLatin1String("vendorurl" ))) { |
| 41 | out << "<vendor_url>" << domain[QStringLiteral("vendorurl" )] << "</vendor_url>\n" ; |
| 42 | } |
| 43 | if (domain.contains(key: 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(args&: policyInactive, args&: policy); |
| 79 | |
| 80 | out << dent << "</action>\n" ; |
| 81 | } |
| 82 | |
| 83 | out << "</policyconfig>\n" ; |
| 84 | } |
| 85 | |