/src/libtomahawk/widgets/newplaylistwidget.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 151 lines · 94 code · 39 blank · 18 comment · 1 complexity · 3edee665f560a93efbe150ae562f5499 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.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 "NewPlaylistWidget.h"
  19. #include "ui_NewPlaylistWidget.h"
  20. #include <QPushButton>
  21. #include <QDialogButtonBox>
  22. #include "SourceList.h"
  23. #include "ViewManager.h"
  24. #include "playlist/PlaylistModel.h"
  25. #include "widgets/OverlayWidget.h"
  26. #include "utils/XspfLoader.h"
  27. #include "utils/TomahawkUtils.h"
  28. #include "utils/Logger.h"
  29. #define FILTER_TIMEOUT 280
  30. NewPlaylistWidget::NewPlaylistWidget( QWidget* parent )
  31. : QWidget( parent )
  32. , ui( new Ui::NewPlaylistWidget )
  33. {
  34. ui->setupUi( this );
  35. m_saveButton = new QPushButton( tr( "&Create Playlist" ) );
  36. m_saveButton->setDefault( true );
  37. m_saveButton->setEnabled( false );
  38. ui->buttonBox->addButton( m_saveButton, QDialogButtonBox::AcceptRole );
  39. connect( ui->titleEdit, SIGNAL( textChanged( QString ) ), SLOT( onTitleChanged( QString ) ) );
  40. connect( ui->tagEdit, SIGNAL( textChanged( QString ) ), SLOT( onTagChanged() ) );
  41. connect( ui->buttonBox, SIGNAL( accepted() ), SLOT( savePlaylist() ) );
  42. connect( ui->buttonBox, SIGNAL( rejected() ), SLOT( cancel() ) );
  43. m_suggestionsModel = new PlaylistModel( ui->suggestionsView );
  44. ui->suggestionsView->setPlaylistModel( m_suggestionsModel );
  45. ui->suggestionsView->overlay()->setEnabled( false );
  46. connect( &m_filterTimer, SIGNAL( timeout() ), SLOT( updateSuggestions() ) );
  47. ui->titleEdit->setFocus();
  48. }
  49. NewPlaylistWidget::~NewPlaylistWidget()
  50. {
  51. delete ui;
  52. }
  53. void
  54. NewPlaylistWidget::changeEvent( QEvent* e )
  55. {
  56. QWidget::changeEvent( e );
  57. switch ( e->type() )
  58. {
  59. case QEvent::LanguageChange:
  60. ui->retranslateUi( this );
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. void
  67. NewPlaylistWidget::onTitleChanged( const QString& title )
  68. {
  69. m_saveButton->setEnabled( !title.isEmpty() );
  70. }
  71. void
  72. NewPlaylistWidget::onTagChanged()
  73. {
  74. m_tag = ui->tagEdit->text();
  75. m_filterTimer.stop();
  76. m_filterTimer.setInterval( FILTER_TIMEOUT );
  77. m_filterTimer.setSingleShot( true );
  78. m_filterTimer.start();
  79. }
  80. void
  81. NewPlaylistWidget::updateSuggestions()
  82. {
  83. QUrl url( QString( "http://ws.audioscrobbler.com/1.0/tag/%1/toptracks.xspf" ).arg( m_tag ) );
  84. XSPFLoader* loader = new XSPFLoader( false );
  85. connect( loader, SIGNAL( ok( Tomahawk::playlist_ptr ) ), SLOT( suggestionsFound() ) );
  86. loader->load( url );
  87. }
  88. void
  89. NewPlaylistWidget::suggestionsFound()
  90. {
  91. XSPFLoader* loader = qobject_cast<XSPFLoader*>( sender() );
  92. m_queries = loader->entries();
  93. delete m_suggestionsModel;
  94. m_suggestionsModel = new PlaylistModel( ui->suggestionsView );
  95. ui->suggestionsView->setPlaylistModel( m_suggestionsModel );
  96. m_suggestionsModel->appendQueries( m_queries );
  97. loader->deleteLater();
  98. }
  99. void
  100. NewPlaylistWidget::savePlaylist()
  101. {
  102. Tomahawk::playlist_ptr playlist;
  103. playlist = Tomahawk::Playlist::create( SourceList::instance()->getLocal(), uuid(), ui->titleEdit->text(), "", "", false, m_queries );
  104. ViewManager::instance()->show( playlist );
  105. cancel();
  106. }
  107. void
  108. NewPlaylistWidget::cancel()
  109. {
  110. // will be deleted by viewmanager
  111. emit destroyed( this );
  112. }