kexi
xmlhandler.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "xmlhandler.h"
00021 #include "macro.h"
00022 #include "macroitem.h"
00023 #include "action.h"
00024
00025 #include <qdom.h>
00026 #include <kdebug.h>
00027
00028 using namespace KoMacro;
00029
00030 namespace KoMacro {
00031
00036 class XMLHandler::Private
00037 {
00038 public:
00039
00044 Macro* const macro;
00045
00052 Private(Macro* const macro)
00053 : macro(macro)
00054 {
00055 }
00056 };
00057
00058 }
00059
00060 XMLHandler::XMLHandler(Macro* const macro)
00061 : d( new Private(macro) )
00062 {
00063 }
00064
00065 XMLHandler::~XMLHandler()
00066 {
00067 delete d;
00068 }
00069
00070 bool XMLHandler::parseXML(const QDomElement& element)
00071 {
00072
00073 d->macro->clearItems();
00074
00075
00076
00077 if(element.tagName() != "macro") {
00078 kdDebug() << QString("XMLHandler::parseXML() Invalid tagname \"%1\"").arg(element.tagName()) << endl;
00079 return false;
00080 }
00081
00082
00083
00084
00085 if( element.attribute("xmlversion") != "1"){
00086 kdDebug() << QString("XMLHandler::parseXML() Invalid xml-version \"%1\"").arg(element.attribute("xmlversion")) << endl;
00087 return false;
00088 }
00089
00090
00091
00092
00093
00094
00095 for(QDomNode itemnode = element.firstChild(); ! itemnode.isNull(); itemnode = itemnode.nextSibling()) {
00096
00097 if(itemnode.nodeName() == "item") {
00098
00099 const QDomElement itemelem = itemnode.toElement();
00100
00101
00102 KSharedPtr<MacroItem> item = new MacroItem();
00103
00104
00105 d->macro->addItem( item );
00106
00107
00108
00109
00110 KSharedPtr<Action> action = Manager::self()->action( itemelem.attribute("action") );
00111 if(action.data()) {
00112 item->setAction(action);
00113 }
00114
00115
00116 item->setComment( itemelem.attribute("comment") );
00117
00118
00119
00120 for(QDomNode childnode = itemnode.firstChild(); ! childnode.isNull(); childnode = childnode.nextSibling()) {
00121
00122 if(childnode.nodeName() == "variable") {
00123
00124 const QDomElement childelem = childnode.toElement();
00125
00126
00127 const QString name = childelem.attribute("name");
00128
00129 const QString value = childelem.text();
00130
00131
00132 item->addVariable(name, value);
00133 }
00134 }
00135 }
00136 }
00137
00138
00139 return true;
00140 }
00141
00142 QDomElement XMLHandler::toXML()
00143 {
00144
00145 QDomDocument document;
00146
00147
00148 QDomElement macroelem = document.createElement("macro");
00149
00150
00151 macroelem.setAttribute("xmlversion","1");
00152
00153
00154
00155
00156
00157
00158
00159
00160 QValueList<KSharedPtr<MacroItem > > items = d->macro->items();
00161
00162
00163 QValueList<KSharedPtr<MacroItem > >::ConstIterator it(items.constBegin()), end(items.constEnd());
00164
00165 for(;it != end; ++it) {
00166
00167 KSharedPtr<MacroItem> item = *it;
00168
00169
00170
00171 bool append = false;
00172
00173
00174 QDomElement itemelem = document.createElement("item");
00175
00176
00177 const KSharedPtr<Action> action = item->action();
00178 if( action ) {
00179 append = true;
00180
00181
00182 itemelem.setAttribute("action", action->name());
00183
00184
00185
00186
00187 QMap<QString, KSharedPtr<Variable > > varmap = item->variables();
00188
00189 for(QMap<QString, KSharedPtr<Variable > >::ConstIterator vit = varmap.constBegin(); vit != varmap.constEnd(); ++vit) {
00190 const KSharedPtr<Variable> v = vit.data();
00191 if(! v.data()) {
00192
00193 continue;
00194 }
00195
00196
00197 QDomElement varelement = document.createElement("variable");
00198
00199
00200 varelement.setAttribute("name", vit.key());
00201
00202
00203 varelement.appendChild(document.createTextNode(v->toString()));
00204
00205
00206 itemelem.appendChild(varelement);
00207 }
00208 }
00209
00210
00211 const QString comment = item->comment();
00212 if(! comment.isEmpty()) {
00213 append = true;
00214 itemelem.setAttribute("comment", item->comment());
00215 }
00216
00217
00218 if(append) {
00219 macroelem.appendChild(itemelem);
00220 }
00221
00222 }
00223
00224
00225 return macroelem;
00226 }
|