[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: extragear/pim/ksig
From: Pino Toscano <pino () kde ! org>
Date: 2016-01-11 22:51:29
Message-ID: E1aIlJB-0001Oc-Hk () scm ! kde ! org
[Download RAW message or body]
SVN commit 1449640 by pino:
switch away from QDom
instead of using QDomNode's as storage for the actual signatures in
SigModel, store them in plain text; this can allow us to switch to
easier/faster ways to parse XML (QXmlStreamReader) and save it again
(QXmlStreamWriter)
M +1 -1 CMakeLists.txt
M +64 -99 sigmodel.cpp
M +1 -6 sigmodel.h
--- trunk/extragear/pim/ksig/CMakeLists.txt #1449639:1449640
@@ -18,7 +18,7 @@
kde4_add_executable(ksig ${ksig_SRCS})
-target_link_libraries(ksig ${KDE4_KDEUI_LIBS} ${QT_QTXML_LIBRARY})
+target_link_libraries(ksig ${KDE4_KDEUI_LIBS})
install(TARGETS ksig ${INSTALL_TARGETS_DEFAULT_ARGS})
--- trunk/extragear/pim/ksig/sigmodel.cpp #1449639:1449640
@@ -13,36 +13,12 @@
#include <qregexp.h>
#include <qtextstream.h>
+#include <qxmlstream.h>
#include <kglobal.h>
#include <klocalizedstring.h>
#include <kstandarddirs.h>
-static void nodeToText(const QDomNode &n, QString &s)
-{
- QDomNodeList children = n.childNodes();
-
- for(int i = 0; i < children.count(); i++) {
- if(children.item(i).isText())
- s.append(children.item(i).toText().data());
- else {
- nodeToText(children.item(i), s);
- if(children.item(i).isElement() && \
children.item(i).toElement().tagName() == "p") {
- s.append("\n");
- }
- }
- }
-}
-
-static QString signatureString(const QDomNode &n)
-{
- QString text;
- nodeToText(n, text);
- text.replace(QRegExp("\n$"), "");
- return text;
-}
-
-
SigModel::SigModel(QObject *parent) : QAbstractItemModel(parent)
{
QString dir = KGlobal::dirs()->saveLocation("appdata");
@@ -66,11 +42,11 @@
switch(role) {
case Qt::DisplayRole: {
- const QString sig = m_cachedSignatures.at(index.row());
+ const QString sig = m_signatures.at(index.row());
return sig.isEmpty() ? emptySignatureString() : sig.simplified();
}
case SignatureRole:
- return m_cachedSignatures.at(index.row());
+ return m_signatures.at(index.row());
}
return QVariant();
}
@@ -101,28 +77,7 @@
return false;
beginInsertRows(parent, row, row + count - 1);
- QDomNode node;
- // insert the first row distinguish whether it is in the middle
- // or at the end, and whether there are no elements
- if(m_signatures.isEmpty()) {
- QDomElement newElement = m_doc.createElement("signature");
- node = m_rootElement.appendChild(newElement);
- } else if(row < m_signatures.count()) {
- QDomElement newElement = m_doc.createElement("signature");
- node = m_signatures.at(row);
- node = node.parentNode().insertBefore(newElement, node);
- } else {
- QDomElement newElement = m_doc.createElement("signature");
- node = m_signatures.at(m_signatures.count() - 1);
- node = node.parentNode().insertAfter(newElement, node);
- }
- // start from 1, as above one node has been added already
- for(int i = 1; i < count; ++i) {
- QDomElement newElement = m_doc.createElement("signature");
- node = node.parentNode().insertAfter(newElement, node);
- }
- m_signatures = m_doc.elementsByTagName("signature");
- m_cachedSignatures.insert(row, count, QString());
+ m_signatures.insert(row, count, QString());
endInsertRows();
return true;
}
@@ -138,10 +93,7 @@
return false;
beginRemoveRows(parent, row, row + count - 1);
- for(int i = 0; i < count; ++i)
- m_signatures.at(row).parentNode().removeChild(m_signatures.at(row));
- m_signatures = m_doc.elementsByTagName("signature");
- m_cachedSignatures.remove(row, count);
+ m_signatures.remove(row, count);
endRemoveRows();
return true;
}
@@ -158,7 +110,7 @@
switch(role) {
case Qt::EditRole:
- m_cachedSignatures[index.row()] = value.toString();
+ m_signatures[index.row()] = value.toString();
emit dataChanged(index, index);
return true;
}
@@ -171,16 +123,26 @@
if(m_file.fileName().isEmpty())
return;
- for(int i = 0; i < m_signatures.count(); ++i) {
- updateNode(m_signatures.at(i), m_cachedSignatures.at(i));
- }
-
if(m_file.open(QIODevice::WriteOnly)) {
- QTextStream stream(&m_file);
- stream << m_doc;
- m_file.close();
+ QXmlStreamWriter xml(&m_file);
+ xml.setAutoFormatting(true);
+ xml.setAutoFormattingIndent(1);
+ xml.setCodec("UTF-8");
+
+ xml.writeStartDocument("1.0");
+ xml.writeStartElement("SigML");
+ foreach(const QString &sig, m_signatures) {
+ xml.writeStartElement("signature");
+ const QStringList lines = sig.split(QLatin1Char('\n'), \
QString::KeepEmptyParts); + foreach(const QString &line, lines) {
+ xml.writeTextElement("p", line);
}
+ xml.writeEndElement();
}
+ xml.writeEndElement();
+ xml.writeEndDocument();
+ }
+}
QString SigModel::emptySignatureString() const
{
@@ -193,48 +155,51 @@
{
m_file.setFileName(fileName);
- if(m_file.open(QIODevice::ReadOnly) && m_doc.setContent(&m_file)) {
+ if(m_file.open(QIODevice::ReadOnly)) {
- // find the root element
- QDomNodeList topLevelElements = m_doc.childNodes();
- int i = 0;
- while(topLevelElements.item(i).toElement().tagName() != "SigML" && i < \
topLevelElements.count())
- i++;
-
- if(i < topLevelElements.count())
- // if we didn't hit the end of the list
- m_rootElement = topLevelElements.item(i).toElement();
- else {
- // if we didn't find the root element, create one
- m_rootElement = m_doc.createElement("SigML");
- m_doc.appendChild(m_rootElement);
+ QXmlStreamReader reader(&m_file);
+ bool isSigML = false;
+ bool isSignature = false;
+ bool isP = false;
+ QString current;
+ while (!reader.atEnd() && reader.error() == QXmlStreamReader::NoError) {
+ const QXmlStreamReader::TokenType token = reader.readNext();
+ QStringRef name;
+ switch (token) {
+ case QXmlStreamReader::StartElement:
+ name = reader.name();
+ if (isSignature && name == QLatin1String("p")) {
+ if (!current.isNull())
+ current.append(QLatin1Char('\n'));
+ isP = true;
+ } else if (isSigML && name == QLatin1String("signature"))
+ isSignature = true;
+ else if (name == QLatin1String("SigML"))
+ isSigML = true;
+ break;
+ case QXmlStreamReader::EndElement:
+ name = reader.name();
+ if (isP && name == QLatin1String("p"))
+ isP = false;
+ else if (isSignature && name == QLatin1String("signature")) {
+ m_signatures.append(current);
+ current = QString();
+ isSignature = false;
+ } else if (isSigML && name == QLatin1String("SigML"))
+ isSigML = false;
+ break;
+ case QXmlStreamReader::Characters:
+ if (isP) {
+ const QStringRef text = reader.text();
+ if (!text.isEmpty())
+ current.append(text.toString());
}
-
- m_signatures = m_doc.elementsByTagName("signature");
- m_cachedSignatures.resize(m_signatures.count());
- for(int i = 0; i < m_signatures.count(); ++i)
- m_cachedSignatures[i] = signatureString(m_signatures.at(i));
+ break;
+ default:
+ break;
}
- // if the document could not be opened or setData failed, create the \
document framework
- else {
- m_rootElement = m_doc.createElement("SigML");
- m_doc.appendChild(m_rootElement);
}
+ }
m_file.close();
}
-void SigModel::updateNode(QDomNode element, const QString &text)
-{
- while(!element.firstChild().isNull())
- element.removeChild(element.firstChild());
-
- // create new children
- const QStringList lines = text.split('\n', QString::KeepEmptyParts);
-
- foreach(const QString &line, lines) {
- QDomElement p = m_doc.createElement("p");
- element.appendChild(p);
- p.appendChild(m_doc.createTextNode(line));
- }
-}
-
--- trunk/extragear/pim/ksig/sigmodel.h #1449639:1449640
@@ -11,7 +11,6 @@
#define SIGMODEL_H
#include <qabstractitemmodel.h>
-#include <qdom.h>
#include <qfile.h>
#include <qvector.h>
@@ -40,13 +39,9 @@
private:
QString emptySignatureString() const;
void load(const QString &fileName);
- void updateNode(QDomNode element, const QString &text);
QFile m_file;
- QDomDocument m_doc;
- QDomElement m_rootElement;
- QDomNodeList m_signatures;
- QVector<QString> m_cachedSignatures;
+ QVector<QString> m_signatures;
mutable QString m_emptySigString;
};
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic