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

« back to all changes in this revision

Viewing changes to src/libtomahawk/database/DatabaseCommand_SetDynamicPlaylistRevision.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 "DatabaseCommand_SetDynamicPlaylistRevision.h"
 
20
 
 
21
#include "Source.h"
 
22
#include "DatabaseImpl.h"
 
23
#include "TomahawkSqlQuery.h"
 
24
#include "playlist/dynamic/DynamicPlaylist.h"
 
25
#include "playlist/dynamic/DynamicControl.h"
 
26
#include "network/Servent.h"
 
27
#include "utils/Logger.h"
 
28
 
 
29
#include <QSqlQuery>
 
30
 
 
31
DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s,
 
32
                                                                                const QString& playlistguid,
 
33
                                                                                const QString& newrev,
 
34
                                                                                const QString& oldrev,
 
35
                                                                                const QStringList& orderedguids,
 
36
                                                                                const QList< plentry_ptr >& addedentries,
 
37
                                                                                const QList<plentry_ptr>& entries,
 
38
                                                                                const QString& type,
 
39
                                                                                GeneratorMode mode,
 
40
                                                                                const QList< dyncontrol_ptr >& controls )
 
41
    : DatabaseCommand_SetPlaylistRevision( s, playlistguid, newrev, oldrev, orderedguids, addedentries, entries )
 
42
    , m_type( type )
 
43
    , m_mode( mode )
 
44
    , m_controls( controls )
 
45
    , m_playlist( 0 )
 
46
{
 
47
 
 
48
}
 
49
 
 
50
 
 
51
DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s,
 
52
                                                                                const QString& playlistguid,
 
53
                                                                                const QString& newrev,
 
54
                                                                                const QString& oldrev,
 
55
                                                                                const QString& type,
 
56
                                                                                GeneratorMode mode,
 
57
                                                                                const QList< dyncontrol_ptr >& controls )
 
58
    : DatabaseCommand_SetPlaylistRevision( s, playlistguid, newrev, oldrev, QStringList(), QList< plentry_ptr >(), QList< plentry_ptr >() )
 
59
    , m_type( type )
 
60
    , m_mode( mode )
 
61
    , m_controls( controls )
 
62
    , m_playlist( 0 )
 
63
{
 
64
 
 
65
}
 
66
 
 
67
 
 
68
QVariantList
 
69
DatabaseCommand_SetDynamicPlaylistRevision::controlsV()
 
70
{
 
71
    if ( m_controls.isEmpty() )
 
72
        return m_controlsV;
 
73
 
 
74
    if ( !m_controls.isEmpty() && m_controlsV.isEmpty() )
 
75
    {
 
76
        foreach ( const dyncontrol_ptr& control, m_controls )
 
77
        {
 
78
            m_controlsV << QJson::QObjectHelper::qobject2qvariant( control.data() );
 
79
        }
 
80
    }
 
81
 
 
82
    return m_controlsV;
 
83
}
 
84
 
 
85
 
 
86
void
 
87
DatabaseCommand_SetDynamicPlaylistRevision::postCommitHook()
 
88
{
 
89
    if ( source().isNull() || source()->collection().isNull() )
 
90
    {
 
91
        tDebug() << "Source has gone offline, not emitting to GUI.";
 
92
        return;
 
93
    }
 
94
 
 
95
    QStringList orderedentriesguids;
 
96
    foreach( const QVariant& v, orderedguids() )
 
97
        orderedentriesguids << v.toString();
 
98
 
 
99
    Q_ASSERT( !source().isNull() );
 
100
    Q_ASSERT( !source()->collection().isNull() );
 
101
    tLog() << "Postcommitting this playlist:" << playlistguid() << source().isNull();
 
102
 
 
103
    // private, but we are a friend. will recall itself in its own thread:
 
104
    dynplaylist_ptr playlist = source()->collection()->autoPlaylist( playlistguid() );
 
105
    if ( playlist.isNull() )
 
106
        playlist = source()->collection()->station( playlistguid() );
 
107
    // UGH we don't have a sharedptr from DynamicPlaylist+
 
108
 
 
109
    DynamicPlaylist* rawPl = playlist.data();
 
110
    if( playlist.isNull() ) // if it's neither an auto or station, it must not be auto-loaded, so we MUST have been told about it directly
 
111
        rawPl = m_playlist;
 
112
 
 
113
    if ( rawPl == 0 )
 
114
    {
 
115
        tLog() <<"Got null playlist with guid:" << playlistguid() << "from source and collection:" << source()->friendlyName() << source()->collection()->name() << "and mode is static?:" << (m_mode == Static);
 
116
        Q_ASSERT( false );
 
117
        return;
 
118
    }
 
119
 
 
120
    if ( !m_controlsV.isEmpty() && m_controls.isEmpty() )
 
121
    {
 
122
        QList<QVariantMap> controlMap;
 
123
        foreach( const QVariant& v, m_controlsV )
 
124
            controlMap << v.toMap();
 
125
 
 
126
        if ( m_mode == OnDemand )
 
127
            rawPl->setRevision(  newrev(),
 
128
                                    true, // this *is* the newest revision so far
 
129
                                    m_type,
 
130
                                    controlMap,
 
131
                                    m_applied );
 
132
        else
 
133
            rawPl->setRevision(  newrev(),
 
134
                                    orderedentriesguids,
 
135
                                    m_previous_rev_orderedguids,
 
136
                                    m_type,
 
137
                                    controlMap,
 
138
                                    true, // this *is* the newest revision so far
 
139
                                    m_addedmap,
 
140
                                    m_applied );
 
141
    }
 
142
    else
 
143
    {
 
144
        if ( m_mode == OnDemand )
 
145
            rawPl->setRevision(  newrev(),
 
146
                                    true, // this *is* the newest revision so far
 
147
                                    m_type,
 
148
                                    m_controls,
 
149
                                    m_applied );
 
150
        else
 
151
            rawPl->setRevision(  newrev(),
 
152
                                    orderedentriesguids,
 
153
                                    m_previous_rev_orderedguids,
 
154
                                    m_type,
 
155
                                    m_controls,
 
156
                                    true, // this *is* the newest revision so far
 
157
                                    m_addedmap,
 
158
                                    m_applied );
 
159
    }
 
160
 
 
161
    if ( source()->isLocal() )
 
162
        Servent::instance()->triggerDBSync();
 
163
}
 
164
 
 
165
 
 
166
void
 
167
DatabaseCommand_SetDynamicPlaylistRevision::exec( DatabaseImpl* lib )
 
168
{
 
169
    DatabaseCommand_SetPlaylistRevision::exec( lib );
 
170
 
 
171
    QVariantList newcontrols;
 
172
    if ( m_controlsV.isEmpty() && !m_controls.isEmpty() )
 
173
    {
 
174
        foreach( const dyncontrol_ptr& control, m_controls )
 
175
        {
 
176
            newcontrols << control->id();
 
177
        }
 
178
    }
 
179
    else if( !m_controlsV.isEmpty() )
 
180
    {
 
181
        foreach( const QVariant& v, m_controlsV )
 
182
        {
 
183
            newcontrols << v.toMap().value( "id" );
 
184
        }
 
185
    }
 
186
 
 
187
    QJson::Serializer ser;
 
188
    const QByteArray newcontrols_data = ser.serialize( newcontrols );
 
189
 
 
190
    TomahawkSqlQuery query = lib->newquery();
 
191
    QString sql = "INSERT INTO dynamic_playlist_revision (guid, controls, plmode, pltype) "
 
192
                  "VALUES(?, ?, ?, ?)";
 
193
 
 
194
    query.prepare( sql );
 
195
    query.addBindValue( m_newrev );
 
196
    query.addBindValue( newcontrols_data );
 
197
    query.addBindValue( QString::number( (int) m_mode ) );
 
198
    query.addBindValue( m_type );
 
199
    query.exec();
 
200
 
 
201
    // delete all the old controls, replace with new onws
 
202
    qDebug() << "Deleting controls with playlist id" << m_playlistguid;
 
203
    TomahawkSqlQuery delQuery = lib->newquery();
 
204
    delQuery.prepare( "DELETE FROM dynamic_playlist_controls WHERE playlist = ?" );
 
205
    delQuery.addBindValue( m_playlistguid );
 
206
    if ( !delQuery.exec() )
 
207
        tLog() << "Failed to delete controls from dynamic playlist controls table";
 
208
 
 
209
    TomahawkSqlQuery controlsQuery = lib->newquery();
 
210
    controlsQuery.prepare( "INSERT INTO dynamic_playlist_controls( id, playlist, selectedType, match, input ) "
 
211
                            "VALUES( ?, ?, ?, ?, ? )" );
 
212
    if ( m_controlsV.isEmpty() && !m_controls.isEmpty() )
 
213
    {
 
214
        foreach ( const dyncontrol_ptr& control, m_controls )
 
215
        {
 
216
            qDebug() << "inserting dynamic control:" << control->id() << m_playlistguid << control->selectedType() << control->match() << control->input();
 
217
            controlsQuery.addBindValue( control->id() );
 
218
            controlsQuery.addBindValue( m_playlistguid );
 
219
            controlsQuery.addBindValue( control->selectedType() );
 
220
            controlsQuery.addBindValue( control->match() );
 
221
            controlsQuery.addBindValue( control->input() );
 
222
 
 
223
            controlsQuery.exec();
 
224
        }
 
225
    }
 
226
    else
 
227
    {
 
228
        foreach ( const QVariant& v, m_controlsV )
 
229
        {
 
230
            QVariantMap control = v.toMap();
 
231
            qDebug() << "inserting dynamic control from JSON:" << control.value( "id" ) << m_playlistguid << control.value( "selectedType" ) << control.value( "match" ) << control.value( "input" );
 
232
            controlsQuery.addBindValue( control.value( "id" ) );
 
233
            controlsQuery.addBindValue( m_playlistguid );
 
234
            controlsQuery.addBindValue( control.value( "selectedType" ) );
 
235
            controlsQuery.addBindValue( control.value( "match" ) );
 
236
            controlsQuery.addBindValue( control.value( "input" ) );
 
237
 
 
238
            controlsQuery.exec();
 
239
        }
 
240
    }
 
241
 
 
242
    if ( m_applied )
 
243
    {
 
244
        tLog() << "updating dynamic playlist, optimistic locking okay";
 
245
 
 
246
        TomahawkSqlQuery query2 = lib->newquery();
 
247
        query2.prepare( "UPDATE dynamic_playlist SET pltype = ?, plmode = ? WHERE guid = ?" );
 
248
        query2.bindValue( 0, m_type );
 
249
        query2.bindValue( 1, m_mode );
 
250
        query2.bindValue( 2, m_playlistguid );
 
251
        query2.exec();
 
252
    }
 
253
}
 
254
 
 
255
void
 
256
DatabaseCommand_SetDynamicPlaylistRevision::setPlaylist( DynamicPlaylist* pl )
 
257
{
 
258
    m_playlist = pl;
 
259
}