1
/****************************************************************************************
2
 * Copyright (c) 2010 Bart Cerneels <bart.cerneels@kde.org>                             *
3
 *                                                                                      *
4
 * This program is free software; you can redistribute it and/or modify it under        *
5
 * the terms of the GNU General Public License as published by the Free Software        *
6
 * Foundation; either version 2 of the License, or (at your option) any later           *
7
 * version.                                                                             *
8
 *                                                                                      *
9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12
 *                                                                                      *
13
 * You should have received a copy of the GNU General Public License along with         *
14
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15
 ****************************************************************************************/
16
17
#include "OpmlWriter.h"
18
#include "core/support/Debug.h"
19
20
#include <KUrl>
21
22
OpmlWriter::OpmlWriter( const OpmlOutline *rootOutline, QIODevice *device )
23
    : ThreadWeaver::Job()
24
    , m_rootOutline( rootOutline )
25
{
26
    m_xmlWriter = new QXmlStreamWriter( device );
27
}
28
29
void
30
OpmlWriter::run()
31
{
32
#define _x m_xmlWriter
33
    _x->setAutoFormatting( true );
34
    _x->writeStartDocument();
35
    _x->writeStartElement( "opml" );
36
    _x->writeAttribute( "version", "1.0" );
37
    _x->writeStartElement( "head" );
38
    //root outline is threated special, it's attributes will be the elements of <head>
39
    QMapIterator<QString, QString> ai( m_rootOutline->attributes() ); //attributesIterator
40
    while( ai.hasNext() )
41
    {
42
        ai.next();
43
        _x->writeTextElement( ai.key(), ai.value() );
44
    }
45
    _x->writeEndElement(); // head
46
    _x->writeStartElement( "body" );
47
    foreach( const OpmlOutline *childOutline, m_rootOutline->children() )
48
        writeOutline( childOutline );
49
    _x->writeEndDocument(); //implicitly closes all open tags (opml & body)
50
    emit result( 0 );
51
}
52
53
void
54
OpmlWriter::writeOutline( const OpmlOutline *outline )
55
{
56
    bool hasChildren = outline->children().count() != 0;
57
    if( hasChildren )
58
        _x->writeStartElement( "outline" );
59
    else
60
        _x->writeEmptyElement( "outline" );
61
    QMapIterator<QString, QString> ai( outline->attributes() ); // attributesIterator
62
    while( ai.hasNext() )
63
    {
64
        ai.next();
65
        _x->writeAttribute( ai.key(), ai.value() );
66
    }
67
68
    if( hasChildren )
69
    {
70
        foreach( const OpmlOutline *childOutline, outline->children() )
71
            writeOutline( childOutline );
72
        _x->writeEndElement(); // outline
73
    }
74
}