/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. #include "XspfGenerator.h"
  19. #include "utils/Logger.h"
  20. #include "Playlist.h"
  21. #include "PlaylistEntry.h"
  22. #include "Query.h"
  23. #include "Source.h"
  24. #include "Track.h"
  25. #include <QXmlStreamWriter>
  26. #include <QDateTime>
  27. #include <QTimer>
  28. using namespace Tomahawk;
  29. XSPFGenerator::XSPFGenerator( const playlist_ptr& pl, QObject* parent )
  30. : QObject( parent )
  31. , m_playlist( pl )
  32. {
  33. QTimer::singleShot( 0, this, SLOT( generate() ) );
  34. }
  35. XSPFGenerator::~XSPFGenerator()
  36. {
  37. }
  38. void
  39. XSPFGenerator::generate()
  40. {
  41. Q_ASSERT( !m_playlist.isNull() );
  42. QByteArray xspf;
  43. QXmlStreamWriter w( &xspf );
  44. w.setAutoFormatting( true );
  45. w.writeStartDocument();
  46. w.writeStartElement( "playlist" );
  47. w.writeAttribute( "version", "1" );
  48. w.writeAttribute( "xmlns", "http://xspf.org/ns/0/" );
  49. w.writeTextElement( "title", m_playlist->title() );
  50. w.writeTextElement( "creator", m_playlist->creator() );
  51. w.writeTextElement( "date", QDateTime::fromTime_t( m_playlist->createdOn() ).toString( Qt::ISODate ) );
  52. w.writeStartElement( "trackList" );
  53. foreach ( const plentry_ptr& q, m_playlist->entries() )
  54. {
  55. w.writeStartElement( "track" );
  56. w.writeTextElement( "title", q->query()->queryTrack()->track() );
  57. w.writeTextElement( "creator", q->query()->queryTrack()->artist() );
  58. w.writeTextElement( "album", q->query()->queryTrack()->album() );
  59. w.writeEndElement();
  60. }
  61. w.writeEndDocument(); // will close all open elements
  62. emit generated( xspf );
  63. }