KDECore
policy-gen.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright (C) 2008 Nicola Gigante <nicola.gigante@gmail.com> 00003 * Copyright (C) 2009 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 "policy-gen.h" 00022 #include <QFile> 00023 00024 #include <QCoreApplication> 00025 #include <QSettings> 00026 #include <QRegExp> 00027 #include <QStringList> 00028 #include <QDebug> 00029 00030 using namespace std; 00031 00032 QList<Action> parse(QSettings &ini); 00033 QHash<QString, QString> parseDomain(QSettings &ini); 00034 00035 int main(int argc, char **argv) 00036 { 00037 QCoreApplication app(argc, argv); 00038 00039 if (argc < 2) { 00040 qCritical("Too few arguments"); 00041 return 1; 00042 } 00043 00044 QSettings ini(QFile::decodeName(argv[1]), QSettings::IniFormat); 00045 ini.setIniCodec("UTF-8"); 00046 if (ini.status()) { 00047 qCritical("Error loading file: %s", argv[1]); 00048 return 1; 00049 } 00050 00051 output(parse(ini), parseDomain(ini)); 00052 } 00053 00054 QList<Action> parse(QSettings &ini) 00055 { 00056 QList<Action> actions; 00057 QRegExp actionExp(QLatin1String("[0-9a-z]+(\\.[0-9a-z]+)*")); 00058 QRegExp descriptionExp(QLatin1String("description(?:\\[(\\w+)\\])?")); 00059 QRegExp nameExp(QLatin1String("name(?:\\[(\\w+)\\])?")); 00060 QRegExp policyExp(QLatin1String("yes|no|auth_self|auth_admin")); 00061 00062 descriptionExp.setCaseSensitivity(Qt::CaseInsensitive); 00063 nameExp.setCaseSensitivity(Qt::CaseInsensitive); 00064 00065 foreach(const QString &name, ini.childGroups()) { 00066 Action action; 00067 00068 if (name == QLatin1String("Domain")) { 00069 continue; 00070 } 00071 00072 if (!actionExp.exactMatch(name)) { 00073 qCritical("Wrong action syntax: %s\n", name.toAscii().data()); 00074 exit(1); 00075 } 00076 00077 action.name = name; 00078 ini.beginGroup(name); 00079 00080 foreach(const QString &key, ini.childKeys()) { 00081 if (descriptionExp.exactMatch(key)) { 00082 QString lang = descriptionExp.capturedTexts().at(1); 00083 00084 if (lang.isEmpty()) 00085 lang = QString::fromLatin1("en"); 00086 00087 action.descriptions.insert(lang, ini.value(key).toString()); 00088 00089 } else if (nameExp.exactMatch(key)) { 00090 QString lang = nameExp.capturedTexts().at(1); 00091 00092 if (lang.isEmpty()) 00093 lang = QString::fromLatin1("en"); 00094 00095 action.messages.insert(lang, ini.value(key).toString()); 00096 00097 } else if (key.toLower() == QLatin1String("policy")) { 00098 QString policy = ini.value(key).toString(); 00099 if (!policyExp.exactMatch(policy)) { 00100 qCritical("Wrong policy: %s", policy.toAscii().data()); 00101 exit(1); 00102 } 00103 action.policy = policy; 00104 00105 } else if (key.toLower() == QLatin1String("persistence")) { 00106 QString persistence = ini.value(key).toString(); 00107 if (persistence != QLatin1String("session") && persistence != QLatin1String("always")) { 00108 qCritical("Wrong persistence: %s", persistence.toAscii().data()); 00109 exit(1); 00110 } 00111 action.persistence = persistence; 00112 } 00113 } 00114 00115 if (action.policy.isEmpty() || action.messages.isEmpty() || action.descriptions.isEmpty()) { 00116 qCritical("Missing option in action: %s", name.toAscii().data()); 00117 exit(1); 00118 } 00119 ini.endGroup(); 00120 00121 actions.append(action); 00122 } 00123 00124 return actions; 00125 } 00126 00127 00128 QHash<QString, QString> parseDomain(QSettings& ini) 00129 { 00130 QHash<QString, QString> rethash; 00131 00132 if (ini.childGroups().contains(QString::fromLatin1("Domain"))) { 00133 if (ini.contains(QString::fromLatin1("Domain/Name"))) { 00134 rethash[QString::fromLatin1("vendor")] = ini.value(QString::fromLatin1("Domain/Name")).toString(); 00135 } 00136 if (ini.contains(QString::fromLatin1("Domain/URL"))) { 00137 rethash[QString::fromLatin1("vendorurl")] = ini.value(QString::fromLatin1("Domain/URL")).toString(); 00138 } 00139 if (ini.contains(QString::fromLatin1("Domain/Icon"))) { 00140 rethash[QString::fromLatin1("icon")] = ini.value(QString::fromLatin1("Domain/Icon")).toString(); 00141 } 00142 } 00143 00144 return rethash; 00145 } 00146 00147
KDE 4.6 API Reference