/src/libtomahawk/playlist/dynamic/database/DatabaseGenerator.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 191 lines · 125 code · 44 blank · 22 comment · 9 complexity · 6008bcfef8ebe217b99c606bd03e9349 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-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 "DatabaseGenerator.h"
  19. #include "database/DatabaseCommand_GenericSelect.h"
  20. #include "database/Database.h"
  21. #include "utils/Logger.h"
  22. #include "DatabaseControl.h"
  23. #include "PlaylistEntry.h"
  24. #include "Source.h"
  25. using namespace Tomahawk;
  26. GeneratorInterface*
  27. DatabaseFactory::create()
  28. {
  29. return new DatabaseGenerator();
  30. }
  31. dyncontrol_ptr
  32. DatabaseFactory::createControl ( const QString& controlType )
  33. {
  34. return dyncontrol_ptr( new DatabaseControl( controlType, typeSelectors() ) );
  35. }
  36. dyncontrol_ptr
  37. DatabaseFactory::createControl ( const QString& sql, DatabaseCommand_GenericSelect::QueryType type, const QString& summary )
  38. {
  39. dyncontrol_ptr control = dyncontrol_ptr( new DatabaseControl( sql, summary, typeSelectors() ) );
  40. control->setMatch( QString::number( type ) );
  41. return control;
  42. }
  43. QStringList
  44. DatabaseFactory::typeSelectors() const
  45. {
  46. return QStringList() << "SQL" << "Artist" << "Album" << "Title";
  47. }
  48. DatabaseGenerator::DatabaseGenerator ( QObject* parent )
  49. : GeneratorInterface ( parent )
  50. {
  51. // defaults
  52. m_type = "database";
  53. m_mode = Static;
  54. // m_logo.load( RESPATH "images )
  55. }
  56. DatabaseGenerator::~DatabaseGenerator()
  57. {
  58. }
  59. QPixmap
  60. DatabaseGenerator::logo()
  61. {
  62. return m_logo;
  63. }
  64. void
  65. DatabaseGenerator::dynamicFetched()
  66. {
  67. }
  68. void
  69. DatabaseGenerator::dynamicStarted()
  70. {
  71. }
  72. void
  73. DatabaseGenerator::generate( int number )
  74. {
  75. tLog() << "Generating" << number << "tracks for this database dynamic playlist with" << m_controls.size() << "controls:";
  76. if ( m_controls.isEmpty() )
  77. {
  78. qWarning() << "No controls, can't generate...!";
  79. emit error( "Failed to generate tracks", "No controls!" );
  80. return;
  81. }
  82. foreach ( const dyncontrol_ptr& ctrl, m_controls )
  83. qDebug() << ctrl->selectedType() << ctrl->match() << ctrl->input();
  84. // TODO for now, we just support the special "SQL" control, not meant to be shown to the user. Just does a raw query.
  85. bool hasSql = false;
  86. bool hasOther = false;
  87. foreach ( const dyncontrol_ptr& ctrl, m_controls )
  88. {
  89. if ( ctrl->selectedType() == "SQL" )
  90. hasSql = true;
  91. else
  92. hasOther = true;
  93. }
  94. if ( hasSql == hasOther )
  95. {
  96. qWarning() << "Cannot mix sql and non-sql controls!";
  97. emit error( "Failed to generate tracks", "Cannot mix sql and non-sql controls" );
  98. return;
  99. }
  100. // TODO for now we just support 1 sql query if we're a sql type
  101. if ( hasSql )
  102. {
  103. dyncontrol_ptr control = m_controls.first();
  104. tDebug() << "Generated sql query:" << control.dynamicCast< DatabaseControl >()->sql();
  105. DatabaseCommand_GenericSelect* cmd = new DatabaseCommand_GenericSelect( control.dynamicCast< DatabaseControl >()->sql(),
  106. static_cast< DatabaseCommand_GenericSelect::QueryType >( control->match().toInt() ),
  107. number
  108. );
  109. connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SLOT( tracksGenerated( QList<Tomahawk::query_ptr> ) ) );
  110. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  111. return;
  112. }
  113. }
  114. void
  115. DatabaseGenerator::tracksGenerated ( const QList< query_ptr >& tracks )
  116. {
  117. emit generated( tracks );
  118. }
  119. dyncontrol_ptr
  120. DatabaseGenerator::createControl( const QString& type )
  121. {
  122. m_controls << dyncontrol_ptr( new DatabaseControl( type, GeneratorFactory::typeSelectors( m_type ) ) );
  123. return m_controls.last();
  124. }
  125. dyncontrol_ptr
  126. DatabaseGenerator::createControl ( const QString& sql, DatabaseCommand_GenericSelect::QueryType type, const QString& summary )
  127. {
  128. m_controls << dyncontrol_ptr( new DatabaseControl( sql, summary, GeneratorFactory::typeSelectors( m_type ) ) );
  129. m_controls.last()->setMatch( QString::number( type ) );
  130. return m_controls.last();
  131. }
  132. void
  133. DatabaseGenerator::fetchNext( int /* rating */ )
  134. {
  135. }
  136. QString
  137. DatabaseGenerator::sentenceSummary()
  138. {
  139. if ( !m_controls.isEmpty() && m_controls.first()->type() == "SQL" )
  140. return m_controls.first()->summary();
  141. // TODO
  142. return QString();
  143. }
  144. void
  145. DatabaseGenerator::startOnDemand()
  146. {
  147. }