File indexing completed on 2024-04-21 04:47:52

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Bart Cerneels <bart.cerneels@kde.org>                             *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "OpmlWriter.h"
0018 #include "core/support/Debug.h"
0019 
0020 #include <QUrl>
0021 
0022 OpmlWriter::OpmlWriter(const QList<OpmlOutline *> &rootOutlines,
0023                         const QMap<QString, QString> &headerData,
0024                         QIODevice *device )
0025     : QObject()
0026     , ThreadWeaver::Job()
0027     , m_rootOutlines( rootOutlines )
0028     , m_headerData( headerData )
0029 {
0030     m_xmlWriter = new QXmlStreamWriter( device );
0031 }
0032 
0033 void
0034 OpmlWriter::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
0035 {
0036     Q_UNUSED(self);
0037     Q_UNUSED(thread);
0038 #define _x m_xmlWriter
0039     _x->setAutoFormatting( true );
0040     _x->writeStartDocument();
0041     _x->writeStartElement( "opml" );
0042     _x->writeAttribute( "version", "2.0" );
0043     _x->writeStartElement( "head" );
0044     QMapIterator<QString, QString> ai( m_headerData ); //attributesIterator
0045     while( ai.hasNext() )
0046     {
0047         ai.next();
0048         _x->writeTextElement( ai.key(), ai.value() );
0049     }
0050     _x->writeEndElement(); // head
0051     _x->writeStartElement( "body" );
0052     foreach( const OpmlOutline *childOutline, m_rootOutlines )
0053         writeOutline( childOutline );
0054     _x->writeEndDocument(); //implicitly closes all open tags (opml & body)
0055     Q_EMIT result( 0 );
0056 }
0057 
0058 void
0059 OpmlWriter::defaultBegin(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread)
0060 {
0061     Q_EMIT started(self);
0062     ThreadWeaver::Job::defaultBegin(self, thread);
0063 }
0064 
0065 void
0066 OpmlWriter::defaultEnd(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread)
0067 {
0068     ThreadWeaver::Job::defaultEnd(self, thread);
0069     if (!self->success()) {
0070         Q_EMIT failed(self);
0071     }
0072     Q_EMIT done(self);
0073 }
0074 
0075 void
0076 OpmlWriter::writeOutline( const OpmlOutline *outline )
0077 {
0078     bool hasChildren = outline->children().count() != 0;
0079     if( hasChildren && ( outline->opmlNodeType() != IncludeNode ) )
0080         _x->writeStartElement( "outline" );
0081     else
0082         _x->writeEmptyElement( "outline" );
0083     QMapIterator<QString, QString> ai( outline->attributes() ); // attributesIterator
0084     while( ai.hasNext() )
0085     {
0086         ai.next();
0087         _x->writeAttribute( ai.key(), ai.value() );
0088     }
0089 
0090     // children of expanded include nodes should not be saved.
0091     if( hasChildren && ( outline->opmlNodeType() != IncludeNode ) )
0092     {
0093         foreach( const OpmlOutline *childOutline, outline->children() )
0094             writeOutline( childOutline );
0095         _x->writeEndElement(); // outline
0096     }
0097 }