KDECore
kauth-policy-gen-polkit.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com> 00003 * Copyright (C) 2009-2010 Dario Freddi <drf@kde.org> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Lesser General Public License as published by 00007 * the Free Software Foundation; either version 2.1 of the License, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Lesser General Public License 00016 * along with this program; if not, write to the 00017 * Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . 00019 */ 00020 00021 #include <auth/policy-gen/policy-gen.h> 00022 00023 #include <cstdio> 00024 #include <QDebug> 00025 #include <QTextStream> 00026 00027 const char header[] = "" 00028 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 00029 "<!DOCTYPE policyconfig PUBLIC\n" 00030 "\"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN\"\n" 00031 "\"http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd\">\n" 00032 "<policyconfig>\n"; 00033 00034 const char policy_tag[] = "" 00035 " <defaults>\n" 00036 " <allow_inactive>no</allow_inactive>\n" 00037 " <allow_active>%1</allow_active>\n" 00038 " </defaults>\n"; 00039 00040 const char dent[] = " "; 00041 00042 void output(QList<Action> actions, QHash<QString, QString> domain) 00043 { 00044 QTextStream out(stdout); 00045 out.setCodec("UTF-8"); 00046 00047 out << header; 00048 00049 // Blacklisted characters + replacements 00050 QHash< QChar, QString > blacklist; 00051 blacklist.insert(QChar::fromLatin1('&'), QString::fromLatin1("&")); 00052 00053 if (domain.contains(QLatin1String("vendor"))) { 00054 QHash< QChar, QString >::const_iterator blI; 00055 QString vendor = domain[QLatin1String("vendor")]; 00056 for (blI = blacklist.constBegin(); blI != blacklist.constEnd(); ++blI) { 00057 vendor.replace(blI.key(), blI.value()); 00058 } 00059 out << "<vendor>" << vendor << "</vendor>\n"; 00060 } 00061 if (domain.contains(QLatin1String("vendorurl"))) { 00062 out << "<vendor_url>" << domain[QLatin1String("vendorurl")] << "</vendor_url>\n"; 00063 } 00064 if (domain.contains(QLatin1String("icon"))) { 00065 out << "<icon_name>" << domain[QLatin1String("icon")] << "</icon_name>\n"; 00066 } 00067 00068 foreach (const Action &action, actions) { 00069 out << dent << "<action id=\"" << action.name << "\" >\n"; 00070 00071 for (QHash< QString, QString >::const_iterator i = action.messages.constBegin(); i != action.messages.constEnd(); ++i) { 00072 out << dent << dent << "<description"; 00073 if (i.key() != QLatin1String("en")) { 00074 out << " xml:lang=\"" << i.key() << '"'; 00075 } 00076 00077 QHash< QChar, QString >::const_iterator blI; 00078 QString description = i.value(); 00079 for (blI = blacklist.constBegin(); blI != blacklist.constEnd(); ++blI) { 00080 description.replace(blI.key(), blI.value()); 00081 } 00082 00083 out << '>' << description << "</description>\n"; 00084 } 00085 00086 for (QHash< QString, QString>::const_iterator i = action.descriptions.constBegin(); 00087 i != action.descriptions.constEnd(); 00088 ++i) { 00089 out << dent << dent << "<message"; 00090 if (i.key() != QLatin1String("en")) { 00091 out << " xml:lang=\"" << i.key() << '"'; 00092 } 00093 00094 QHash< QChar, QString >::const_iterator blI; 00095 QString message = i.value(); 00096 for (blI = blacklist.constBegin(); blI != blacklist.constEnd(); ++blI) { 00097 message.replace(blI.key(), blI.value()); 00098 } 00099 00100 out << '>' << message << "</message>\n"; 00101 } 00102 00103 QString policy = action.policy; 00104 if (!action.persistence.isEmpty() && policy != QLatin1String("yes") && policy != QLatin1String("no")) { 00105 policy += QLatin1String("_keep_") + action.persistence; 00106 } 00107 00108 out << QString(QLatin1String(policy_tag)).arg(policy); 00109 00110 out << dent << "</action>\n"; 00111 } 00112 00113 out << "</policyconfig>\n"; 00114 }
KDE 4.6 API Reference