~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/playlist/dynamic/database/DatabaseGenerator.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

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