File indexing completed on 2024-04-28 03:52:38

0001 /*
0002     SPDX-FileCopyrightText: 2008 Nicola Gigante <nicola.gigante@gmail.com>
0003     SPDX-FileCopyrightText: 2009-2010 Dario Freddi <drf@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include <policy-gen/policy-gen.h>
0009 
0010 #include <QDebug>
0011 #include <QTextStream>
0012 
0013 #include <cstdio>
0014 
0015 const char header[] =
0016     ""
0017     "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
0018     "<!DOCTYPE policyconfig PUBLIC\n"
0019     "\"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN\"\n"
0020     "\"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd\">\n"
0021     "<policyconfig>\n";
0022 
0023 const char policy_tag[] =
0024     ""
0025     "      <defaults>\n"
0026     "         <allow_inactive>%1</allow_inactive>\n"
0027     "         <allow_active>%2</allow_active>\n"
0028     "      </defaults>\n";
0029 
0030 const char dent[] = "   ";
0031 
0032 void output(const QList<Action> &actions, const QMap<QString, QString> &domain)
0033 {
0034     QTextStream out(stdout);
0035     out << header;
0036 
0037     if (domain.contains(QLatin1String("vendor"))) {
0038         out << "<vendor>" << domain[QStringLiteral("vendor")].toHtmlEscaped() << "</vendor>\n";
0039     }
0040     if (domain.contains(QLatin1String("vendorurl"))) {
0041         out << "<vendor_url>" << domain[QStringLiteral("vendorurl")] << "</vendor_url>\n";
0042     }
0043     if (domain.contains(QLatin1String("icon"))) {
0044         out << "<icon_name>" << domain[QStringLiteral("icon")] << "</icon_name>\n";
0045     }
0046 
0047     for (const Action &action : actions) {
0048         out << dent << "<action id=\"" << action.name << "\" >\n";
0049 
0050         // Not a typo, messages and descriptions are actually inverted
0051         for (auto i = action.messages.cbegin(); i != action.messages.cend(); ++i) {
0052             out << dent << dent << "<description";
0053             if (i.key() != QLatin1String("en")) {
0054                 out << " xml:lang=\"" << i.key() << '"';
0055             }
0056 
0057             out << '>' << i.value().toHtmlEscaped() << "</description>\n";
0058         }
0059 
0060         for (auto i = action.descriptions.cbegin(); i != action.descriptions.cend(); ++i) {
0061             out << dent << dent << "<message";
0062             if (i.key() != QLatin1String("en")) {
0063                 out << " xml:lang=\"" << i.key() << '"';
0064             }
0065 
0066             out << '>' << i.value().toHtmlEscaped() << "</message>\n";
0067         }
0068 
0069         QString policy = action.policy;
0070         QString policyInactive = action.policyInactive.isEmpty() ? QLatin1String("no") : action.policyInactive;
0071         if (!action.persistence.isEmpty() && policy != QLatin1String("yes") && policy != QLatin1String("no")) {
0072             policy += QLatin1String("_keep");
0073         }
0074         if (!action.persistence.isEmpty() && policyInactive != QLatin1String("yes") && policyInactive != QLatin1String("no")) {
0075             policyInactive += QLatin1String("_keep");
0076         }
0077 
0078         out << QString(QLatin1String(policy_tag)).arg(policyInactive, policy);
0079 
0080         out << dent << "</action>\n";
0081     }
0082 
0083     out << "</policyconfig>\n";
0084 }