PageRenderTime 237ms CodeModel.GetById 134ms app.highlight 17ms RepoModel.GetById 84ms app.codeStats 0ms

/src/libtomahawk/utils/XspfGenerator.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 79 lines | 46 code | 16 blank | 17 comment | 0 complexity | 2b975abfab6376cf35e8d998e5c13c86 MD5 | raw file
 1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 2 *
 3 *   Copyright 2011, Leo Franchi <lfranchi@kde.org>
 4 *
 5 *   Tomahawk is free software: you can redistribute it and/or modify
 6 *   it under the terms of the GNU General Public License as published by
 7 *   the Free Software Foundation, either version 3 of the License, or
 8 *   (at your option) any later version.
 9 *
10 *   Tomahawk is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "XspfGenerator.h"
20
21#include "utils/Logger.h"
22
23#include "Playlist.h"
24#include "PlaylistEntry.h"
25#include "Query.h"
26#include "Source.h"
27#include "Track.h"
28
29#include <QXmlStreamWriter>
30#include <QDateTime>
31#include <QTimer>
32
33using namespace Tomahawk;
34
35
36XSPFGenerator::XSPFGenerator( const playlist_ptr& pl, QObject* parent )
37    : QObject( parent )
38    , m_playlist( pl )
39{
40     QTimer::singleShot( 0, this, SLOT( generate() ) );
41}
42
43
44XSPFGenerator::~XSPFGenerator()
45{
46}
47
48
49void
50XSPFGenerator::generate()
51{
52    Q_ASSERT( !m_playlist.isNull() );
53
54    QByteArray xspf;
55    QXmlStreamWriter w( &xspf );
56    w.setAutoFormatting( true );
57    w.writeStartDocument();
58
59    w.writeStartElement( "playlist" );
60    w.writeAttribute( "version", "1" );
61    w.writeAttribute( "xmlns", "http://xspf.org/ns/0/" );
62
63    w.writeTextElement( "title", m_playlist->title() );
64    w.writeTextElement( "creator", m_playlist->creator() );
65    w.writeTextElement( "date", QDateTime::fromTime_t( m_playlist->createdOn() ).toString( Qt::ISODate ) );
66
67    w.writeStartElement( "trackList" );
68    foreach ( const plentry_ptr& q, m_playlist->entries() )
69    {
70        w.writeStartElement( "track" );
71        w.writeTextElement( "title", q->query()->queryTrack()->track() );
72        w.writeTextElement( "creator", q->query()->queryTrack()->artist() );
73        w.writeTextElement( "album", q->query()->queryTrack()->album() );
74        w.writeEndElement();
75    }
76    w.writeEndDocument(); // will close all open elements
77
78    emit generated( xspf );
79}