File indexing completed on 2024-05-05 16:05:15

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 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0036     out.setCodec("UTF-8");
0037 #endif
0038 
0039     out << header;
0040 
0041     if (domain.contains(QLatin1String("vendor"))) {
0042         out << "<vendor>" << domain[QStringLiteral("vendor")].toHtmlEscaped() << "</vendor>\n";
0043     }
0044     if (domain.contains(QLatin1String("vendorurl"))) {
0045         out << "<vendor_url>" << domain[QStringLiteral("vendorurl")] << "</vendor_url>\n";
0046     }
0047     if (domain.contains(QLatin1String("icon"))) {
0048         out << "<icon_name>" << domain[QStringLiteral("icon")] << "</icon_name>\n";
0049     }
0050 
0051     for (const Action &action : actions) {
0052         out << dent << "<action id=\"" << action.name << "\" >\n";
0053 
0054         // Not a typo, messages and descriptions are actually inverted
0055         for (auto i = action.messages.cbegin(); i != action.messages.cend(); ++i) {
0056             out << dent << dent << "<description";
0057             if (i.key() != QLatin1String("en")) {
0058                 out << " xml:lang=\"" << i.key() << '"';
0059             }
0060 
0061             out << '>' << i.value().toHtmlEscaped() << "</description>\n";
0062         }
0063 
0064         for (auto i = action.descriptions.cbegin(); i != action.descriptions.cend(); ++i) {
0065             out << dent << dent << "<message";
0066             if (i.key() != QLatin1String("en")) {
0067                 out << " xml:lang=\"" << i.key() << '"';
0068             }
0069 
0070             out << '>' << i.value().toHtmlEscaped() << "</message>\n";
0071         }
0072 
0073         QString policy = action.policy;
0074         QString policyInactive = action.policyInactive.isEmpty() ? QLatin1String("no") : action.policyInactive;
0075         if (!action.persistence.isEmpty() && policy != QLatin1String("yes") && policy != QLatin1String("no")) {
0076             policy += QLatin1String("_keep");
0077         }
0078         if (!action.persistence.isEmpty() && policyInactive != QLatin1String("yes") && policyInactive != QLatin1String("no")) {
0079             policyInactive += QLatin1String("_keep");
0080         }
0081 
0082         out << QString(QLatin1String(policy_tag)).arg(policyInactive, policy);
0083 
0084         out << dent << "</action>\n";
0085     }
0086 
0087     out << "</policyconfig>\n";
0088 }