PageRenderTime 203ms CodeModel.GetById 100ms app.highlight 15ms RepoModel.GetById 86ms app.codeStats 0ms

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