File indexing completed on 2024-05-19 07:41:31

0001 /*
0002     SPDX-FileCopyrightText: 2008 Nicola Gigante <nicola.gigante@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include <auth/policy-gen/policy-gen.h>
0008 
0009 #include <QDebug>
0010 #include <QTextStream>
0011 #include <cstdio>
0012 
0013 const char header[] =
0014     ""
0015     "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
0016     "<!DOCTYPE policyconfig PUBLIC\n"
0017     "\"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN\"\n"
0018     "\"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd\">\n"
0019     "<policyconfig>\n";
0020 
0021 const char policy_tag[] =
0022     ""
0023     "      <defaults>\n"
0024     "         <allow_inactive>no</allow_inactive>\n"
0025     "         <allow_active>%1</allow_active>\n"
0026     "      </defaults>\n";
0027 
0028 const char dent[] = "   ";
0029 
0030 void output(const QList<Action> &actions, const QMap<QString, QString> &domain)
0031 {
0032     Q_UNUSED(domain)
0033 
0034     QTextStream out(stdout);
0035     out << header;
0036 
0037     for (const Action &action : std::as_const(actions)) {
0038         out << dent << "<action id=\"" << action.name << "\" >\n";
0039 
0040         const auto lstKeys = action.descriptions.keys();
0041         for (const QString &lang : lstKeys) {
0042             out << dent << dent << "<description";
0043             if (lang != "en") {
0044                 out << " xml:lang=\"" << lang << '"';
0045             }
0046             out << '>' << action.messages.value(lang) << "</description>\n";
0047         }
0048 
0049         const auto lstMessagesKeys = action.messages.keys();
0050         for (const QString &lang : lstMessagesKeys) {
0051             out << dent << dent << "<message";
0052             if (lang != "en") {
0053                 out << " xml:lang=\"" << lang << '"';
0054             }
0055             out << '>' << action.descriptions.value(lang) << "</message>\n";
0056         }
0057 
0058         QString policy = action.policy;
0059         if (!action.persistence.isEmpty()) {
0060             policy += "_keep_" + action.persistence;
0061         }
0062 
0063         out << QString(policy_tag).arg(policy);
0064 
0065         out << dent << "</action>\n";
0066     }
0067 
0068     out << "</policyconfig>\n";
0069 }