File indexing completed on 2025-01-19 04:25:18
0001 /* 0002 * Copyright (C) 2017 Malte Veerman <malte.veerman@gmail.com> 0003 * 0004 * This program is free software; you can redistribute it and/or modify 0005 * it under the terms of the GNU General Public License as published by 0006 * the Free Software Foundation; either version 2 of the License, or 0007 * (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License along 0015 * with this program; if not, write to the Free Software Foundation, Inc., 0016 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 0017 * 0018 */ 0019 0020 #include "AmarokScriptXml.h" 0021 0022 #include "core/support/Debug.h" 0023 0024 #include <QDomDocument> 0025 #include <QJSEngine> 0026 #include <QXmlStreamReader> 0027 0028 using namespace AmarokScript; 0029 0030 AmarokScriptXml::AmarokScriptXml( QJSEngine *engine ) 0031 : QObject( engine ) 0032 , m_reader( nullptr ) 0033 , m_domDocument( new QDomDocument ) 0034 { 0035 QJSValue scriptObject = engine->newQObject( this ); 0036 engine->globalObject().property( QStringLiteral("Amarok") ).setProperty( QStringLiteral("Xml"), scriptObject ); 0037 } 0038 0039 AmarokScriptXml::~AmarokScriptXml() 0040 { 0041 delete m_domDocument; 0042 0043 if( m_reader ) 0044 delete m_reader; 0045 } 0046 0047 void AmarokScriptXml::setReaderData(const QString& data) 0048 { 0049 if( m_reader ) 0050 delete m_reader; 0051 0052 m_reader = new QXmlStreamReader( data ); 0053 } 0054 0055 bool AmarokScriptXml::setDomObjectData(const QString& data) 0056 { 0057 return m_domDocument->setContent( data ); 0058 } 0059 0060 QString AmarokScriptXml::readFirstStreamElementWithName(const QString& name) 0061 { 0062 if( !m_reader ) 0063 return QString(); 0064 0065 while( m_reader->readNextStartElement() ) 0066 { 0067 if( m_reader->name() == name ) 0068 return m_reader->readElementText(); 0069 } 0070 0071 return QString(); 0072 } 0073 0074 QString AmarokScriptXml::textOfFirstDomElementWithName(const QString& name) 0075 { 0076 auto elements = m_domDocument->elementsByTagName( name ); 0077 0078 if( !elements.isEmpty() ) 0079 { 0080 auto element = elements.at( 0 ); 0081 return element.toElement().text(); 0082 } 0083 0084 return QString(); 0085 }