r24228 by jghali - Fix another case of use of obsolete QXmlSimpleReader class

scribus-commit scribus-commit at lists.scribus.net
Wed Nov 11 14:51:05 UTC 2020


Author: jghali
Date: Wed Nov 11 14:51:05 2020
New Revision: 24228

URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=24228
Log:
Fix another case of use of obsolete QXmlSimpleReader class

Modified:
    trunk/Scribus/scribus/plugins/saveastemplateplugin/satdialog.cpp

Modified: trunk/Scribus/scribus/plugins/saveastemplateplugin/satdialog.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=24228&path=/trunk/Scribus/scribus/plugins/saveastemplateplugin/satdialog.cpp
==============================================================================
--- trunk/Scribus/scribus/plugins/saveastemplateplugin/satdialog.cpp	(original)
+++ trunk/Scribus/scribus/plugins/saveastemplateplugin/satdialog.cpp	Wed Nov 11 14:51:05 2020
@@ -11,8 +11,7 @@
 #include "scribusapi.h"
 
 #include <QDir>
-#include <QXmlDefaultHandler>
-#include <QXmlSimpleReader>
+#include <QXmlStreamReader>
 
 #include "prefsmanager.h"
 #include "prefsfile.h"
@@ -20,20 +19,47 @@
 #include "scribuscore.h"
 #include "iconmanager.h"
 
-class CategoriesReader : public QXmlDefaultHandler
+class CategoriesReader
 {
 public:
-	QStringList categories;
-	virtual bool startElement(const QString&, const QString&, const QString &name, const QXmlAttributes &attrs)
-	{
-		if (name == QLatin1String("template"))
+	CategoriesReader() = default;
+
+	const QStringList& categories() { return m_categories; }
+
+	bool parse(const QString& fileName)
+	{
+		m_categories.clear();
+
+		QFile qFile(fileName);
+		if (!qFile.open(QFile::ReadOnly))
+			return false;
+
+		QXmlStreamReader sReader(&qFile);
+		QXmlStreamReader::TokenType tagType;
+		QStringRef tagName;
+
+		while (!sReader.atEnd() && !sReader.hasError())
 		{
-			QString cat = attrs.value(QLatin1String("category"));
-			if (!categories.contains(cat))
-				categories.append(cat);
+			tagType = sReader.readNext();
+			if (tagType != QXmlStreamReader::StartElement)
+				continue;
+
+			tagName = sReader.name();
+			if (tagName != "template")
+				continue;
+
+			QXmlStreamAttributes attrs = sReader.attributes();
+			QString cat = attrs.value(QLatin1String("category")).toString();
+			if (!m_categories.contains(cat))
+				m_categories.append(cat);
 		}
-		return true;
-	}
+		qFile.close();
+
+		return (m_categories.count() > 0);
+	}
+
+private:
+	QStringList m_categories;
 };
 
 SATDialog::SATDialog(QWidget* parent, const QString& tmplName, int pageW, int pageH) : QDialog(parent)
@@ -133,13 +159,11 @@
 
 void SATDialog::readCategories(const QString& fileName)
 {
-	QFile file(fileName);
 	CategoriesReader catReader;
-	QXmlInputSource  xmlSource(&file);
-	QXmlSimpleReader reader;
-	reader.setContentHandler(&catReader);
-	reader.parse(&xmlSource);
-	QStringList& categories = catReader.categories;
+	if (!catReader.parse(fileName))
+		return;
+
+	const QStringList& categories = catReader.categories();
 	for (int i = 0; i < categories.count(); ++i)
 	{
 		const QString& category = categories.at(i);




More information about the scribus-commit mailing list