22 #include "kxmlguiversionhandler_p.h"
25 #include <QDomDocument>
29 #include <QtXml/QDomElement>
40 QDomElement parent = doc.documentElement();
41 for (QDomElement e = parent.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
42 if (e.tagName().compare(QLatin1String(
"ToolBar"), Qt::CaseInsensitive) == 0) {
51 QDomElement parent = doc.documentElement();
53 foreach(
const QDomElement& e, toolBars) {
54 parent.removeChild(e);
60 QDomElement parent = doc.documentElement();
61 QDomElement menuBar = parent.namedItem(
"MenuBar").toElement();
62 QDomElement insertAfter = menuBar;
64 insertAfter = parent.firstChildElement();
65 foreach(
const QDomElement& e, toolBars) {
66 QDomNode result = parent.insertAfter(e, insertAfter);
67 Q_ASSERT(!result.isNull());
79 QDomElement actionPropElement = doc.documentElement().namedItem(
"ActionProperties" ).toElement();
81 if ( actionPropElement.isNull() )
84 QDomNode n = actionPropElement.firstChild();
87 QDomElement e = n.toElement();
92 if ( e.tagName().compare(
"action", Qt::CaseInsensitive) != 0 )
95 const QString actionName = e.attribute(
"name" );
96 if ( actionName.isEmpty() )
100 if ( propIt == properties.end() )
103 const QDomNamedNodeMap attributes = e.attributes();
104 const uint attributeslength = attributes.length();
106 for ( uint i = 0; i < attributeslength; ++i )
108 const QDomAttr attr = attributes.item( i ).toAttr();
115 if ( name ==
"name" || name.isEmpty() )
118 (*propIt)[
name ] = attr.value();
129 QDomElement actionPropElement = doc.documentElement().namedItem(
"ActionProperties" ).toElement();
131 if ( actionPropElement.isNull() )
133 actionPropElement = doc.createElement(
"ActionProperties" );
134 doc.documentElement().appendChild( actionPropElement );
140 QDomNode actionNode = actionPropElement.firstChild();
141 while (!actionNode.isNull())
143 if (properties.contains(actionNode.toElement().attribute(
"name")))
145 QDomNode nextNode = actionNode.nextSibling();
146 actionPropElement.removeChild( actionNode );
147 actionNode = nextNode;
149 actionNode = actionNode.nextSibling();
152 ActionPropertiesMap::ConstIterator it = properties.begin();
153 const ActionPropertiesMap::ConstIterator
end = properties.end();
154 for (; it !=
end; ++it )
156 QDomElement action = doc.createElement(
"Action" );
157 action.setAttribute(
"name", it.key() );
158 actionPropElement.appendChild( action );
163 for (; attrIt != attrEnd; ++attrIt )
164 action.setAttribute( attrIt.key(), attrIt.value() );
168 QString KXmlGuiVersionHandler::findVersionNumber(
const QString &xml )
170 enum { ST_START, ST_AFTER_OPEN, ST_AFTER_GUI,
171 ST_EXPECT_VERSION, ST_VERSION_NUM} state = ST_START;
172 const int length = xml.length();
173 for (
int pos = 0; pos < length; pos++) {
177 state = ST_AFTER_OPEN;
182 const int guipos = xml.indexOf(
"gui", pos, Qt::CaseInsensitive);
187 state = ST_AFTER_GUI;
191 state = ST_EXPECT_VERSION;
193 case ST_EXPECT_VERSION:
195 const int verpos = xml.indexOf(
"version", pos, Qt::CaseInsensitive);
199 while (xml.at(pos).isSpace())
201 if (xml.at(pos++) !=
'=')
203 while (xml.at(pos).isSpace())
206 state = ST_VERSION_NUM;
212 for (endpos = pos; endpos < length; endpos++) {
213 const ushort ch = xml[endpos].unicode();
214 if (ch >=
'0' && ch <=
'9')
223 if (endpos != pos && endpos < length ) {
224 const QString matchCandidate = xml.mid(pos, endpos - pos);
225 return matchCandidate;
228 state = ST_EXPECT_VERSION;
238 KXmlGuiVersionHandler::KXmlGuiVersionHandler(
const QStringList& files)
240 Q_ASSERT(!files.isEmpty());
242 if (files.count() == 1) {
244 m_file = files.first();
252 foreach (
const QString &file, files) {
256 allDocuments.append( d );
260 uint bestVersion = 0;
264 for (; docIt != docEnd; ++docIt ) {
265 const QString versionStr = findVersionNumber( (*docIt).data );
266 if ( versionStr.isEmpty() ) {
267 kDebug(260) <<
"found no version in" << (*docIt).file;
272 uint
version = versionStr.toUInt( &ok );
277 if ( version > bestVersion ) {
280 bestVersion = version;
284 if ( best != docEnd ) {
285 if ( best != allDocuments.begin() ) {
291 QDomDocument localDocument;
292 localDocument.setContent( (*local).data );
304 if ( !properties.isEmpty() || !toolbars.isEmpty() ) {
307 QDomDocument document;
308 document.setContent( (*best).data );
311 if (!toolbars.isEmpty()) {
318 (*local).data = document.toString();
323 QFile f( (*local).file );
324 if ( f.open( QIODevice::WriteOnly ) )
326 const QByteArray utf8data = (*local).data.toUtf8();
327 f.write( utf8data.constData(), utf8data.length() );
332 const QString f = (*local).file;
333 const QString backup = f + QLatin1String(
".backup" );
334 QFile::rename( f, backup );
338 m_doc = (*best).data;
339 m_file = (*best).file;
342 m_doc = allDocuments.first().data;
343 m_file = allDocuments.first().file;
QString saveLocation(const char *type, const QString &suffix=QString(), bool create=true) const
const char * name(StandardAction id)
This will return the internal name of a given standard action.
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
static void insertToolBars(QDomDocument &doc, const QList< QDomElement > &toolBars)
static void removeAllToolBars(QDomDocument &doc)
static QString readConfigFile(const QString &filename, const KComponentData &componentData=KComponentData())
QMap< QString, QMap< QString, QString > > ActionPropertiesMap
KGuiItem ok()
Returns the 'Ok' gui item.
static QList< QDomElement > extractToolBars(const QDomDocument &doc)
KGuiItem properties()
Returns the 'Properties' gui item.
static void storeActionProperties(QDomDocument &doc, const ActionPropertiesMap &properties)
QString localkdedir() const
const KShortcut & end()
Goto end of the document.
static ActionPropertiesMap extractActionProperties(const QDomDocument &doc)