~mixxxdevelopers/mixxx/trunk

« back to all changes in this revision

Viewing changes to mixxx/src/library/dao/playlistdao.cpp

  • Committer: RJ Ryan
  • Date: 2012-03-12 05:23:47 UTC
  • mfrom: (2840.2.23 mixxx-setlog)
  • Revision ID: rryan@mit.edu-20120312052347-ej5ryah1p3nag2dq
Merging Daniel Schürmann's session history branch into lp:mixxx. Thanks Daniel!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
 
2
1
#include <QtDebug>
3
2
#include <QtCore>
4
3
#include <QtSql>
19
18
{
20
19
}
21
20
 
22
 
/** Create a playlist with the given name.
23
 
    @param name The name of the playlist to be created.
24
 
*/
25
 
bool PlaylistDAO::createPlaylist(QString name, bool hidden)
 
21
int PlaylistDAO::createPlaylist(QString name, HiddenType hidden)
26
22
{
27
23
    // qDebug() << "PlaylistDAO::createPlaylist"
28
24
    //          << QThread::currentThread()
38
34
    if (!query.exec()) {
39
35
        LOG_FAILED_QUERY(query);
40
36
        m_database.rollback();
41
 
        return false;
 
37
        return -1;
42
38
    }
43
39
 
44
40
    //Get the id of the last playlist.
50
46
 
51
47
    //qDebug() << "Inserting playlist" << name << "at position" << position;
52
48
 
53
 
    query.prepare("INSERT INTO Playlists (name, position, hidden) "
54
 
                  "VALUES (:name, :position, :hidden)");
 
49
    query.prepare("INSERT INTO Playlists (name, position, hidden, date_created, date_modified) "
 
50
                  "VALUES (:name, :position, :hidden,  CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)");
55
51
    query.bindValue(":name", name);
56
52
    query.bindValue(":position", position);
57
 
    query.bindValue(":hidden", hidden ? 1 : 0);
 
53
    query.bindValue(":hidden", static_cast<int>(hidden));
58
54
 
59
55
    if (!query.exec()) {
60
56
        LOG_FAILED_QUERY(query);
61
57
        m_database.rollback();
62
 
        return false;
 
58
        return -1;
63
59
    }
64
60
 
65
61
    int playlistId = query.lastInsertId().toInt();
66
62
    //Commit the transaction
67
63
    m_database.commit();
68
64
    emit(added(playlistId));
69
 
    return true;
 
65
    return playlistId;
70
66
}
71
67
 
72
 
/** Find out the name of the playlist at the given position */
73
 
QString PlaylistDAO::getPlaylistName(unsigned int position)
 
68
QString PlaylistDAO::getPlaylistName(int playlistId)
74
69
{
75
70
    // qDebug() << "PlaylistDAO::getPlaylistName" << QThread::currentThread() << m_database.connectionName();
76
71
 
77
72
    QSqlQuery query(m_database);
78
73
    query.prepare("SELECT name FROM Playlists "
79
 
                  "WHERE position = :position");
80
 
    query.bindValue(":position", position);
 
74
                  "WHERE id= :id");
 
75
    query.bindValue(":id", playlistId);
81
76
 
82
77
    if (!query.exec()) {
83
78
        LOG_FAILED_QUERY(query);
108
103
    return -1;
109
104
}
110
105
 
111
 
 
112
 
/** Delete a playlist */
113
106
void PlaylistDAO::deletePlaylist(int playlistId)
114
107
{
115
108
    // qDebug() << "PlaylistDAO::deletePlaylist" << QThread::currentThread() << m_database.connectionName();
144
137
    emit(deleted(playlistId));
145
138
}
146
139
 
147
 
 
148
140
void PlaylistDAO::renamePlaylist(int playlistId, const QString& newName) {
149
141
    QSqlQuery query(m_database);
150
142
    query.prepare("UPDATE Playlists SET name = :name WHERE id = :id");
152
144
    query.bindValue(":id", playlistId);
153
145
    if (!query.exec()) {
154
146
        LOG_FAILED_QUERY(query);
 
147
        return;
155
148
    }
 
149
    emit(renamed(playlistId));
156
150
}
157
151
 
158
 
 
159
152
bool PlaylistDAO::setPlaylistLocked(int playlistId, bool locked) {
 
153
    QSqlQuery query(m_database);
 
154
    query.prepare("UPDATE Playlists SET locked = :lock WHERE id = :id");
160
155
    // SQLite3 doesn't support boolean value. Using integer instead.
161
 
    int lock = locked ? 1 : 0;
162
 
 
163
 
    QSqlQuery query(m_database);
164
 
    query.prepare("UPDATE Playlists SET locked = :lock WHERE id = :id");
165
 
    query.bindValue(":lock", lock);
 
156
    query.bindValue(":lock", static_cast<int>(locked));
166
157
    query.bindValue(":id", playlistId);
167
158
 
168
159
    if (!query.exec()) {
169
160
        LOG_FAILED_QUERY(query);
170
161
        return false;
171
162
    }
 
163
    emit(lockChanged(playlistId));
172
164
    return true;
173
165
}
174
166
 
188
180
    return false;
189
181
}
190
182
 
191
 
/** Append a track to a playlist */
192
 
void PlaylistDAO::appendTrackToPlaylist(int trackId, int playlistId)
193
 
{
194
 
    // qDebug() << "PlaylistDAO::appendTrackToPlaylist"
 
183
void PlaylistDAO::appendTracksToPlaylist(QList<int> trackIds, int playlistId) {
 
184
    // qDebug() << "PlaylistDAO::appendTracksToPlaylist"
195
185
    //          << QThread::currentThread() << m_database.connectionName();
196
186
 
197
187
    // Start the transaction
207
197
        LOG_FAILED_QUERY(query);
208
198
    }
209
199
 
210
 
    // Get the position of the highest playlist...
 
200
    // Get the position of the highest track in the playlist.
211
201
    int position = 0;
212
202
    if (query.next()) {
213
203
        position = query.value(query.record().indexOf("position")).toInt();
214
204
    }
215
 
    position++; //Append after the last song.
 
205
    // Append after the last song. If no songs or a failed query then 0 becomes
 
206
    // 1.
 
207
    position++;
216
208
 
217
209
    //Insert the song into the PlaylistTracks table
218
210
    query.prepare("INSERT INTO PlaylistTracks (playlist_id, track_id, position)"
219
211
                  "VALUES (:playlist_id, :track_id, :position)");
220
212
    query.bindValue(":playlist_id", playlistId);
221
 
    query.bindValue(":track_id", trackId);
222
 
    query.bindValue(":position", position);
223
 
 
224
 
    if (!query.exec()) {
225
 
        LOG_FAILED_QUERY(query);
 
213
 
 
214
 
 
215
    foreach (int trackId, trackIds) {
 
216
        query.bindValue(":track_id", trackId);
 
217
        query.bindValue(":position", position++);
 
218
        if (!query.exec()) {
 
219
            LOG_FAILED_QUERY(query);
 
220
        }
226
221
    }
227
222
 
228
223
    // Commit the transaction
229
224
    m_database.commit();
230
225
 
231
 
    emit(trackAdded(playlistId, trackId, position));
 
226
    foreach (int trackId, trackIds) {
 
227
        emit(trackAdded(playlistId, trackId, position));
 
228
    }
232
229
    emit(changed(playlistId));
233
230
}
234
231
 
235
 
/** Find out how many playlists exist. */
 
232
void PlaylistDAO::appendTrackToPlaylist(int trackId, int playlistId) {
 
233
    QList<int> tracks;
 
234
    tracks.append(trackId);
 
235
    appendTracksToPlaylist(tracks, playlistId);
 
236
}
 
237
 
236
238
unsigned int PlaylistDAO::playlistCount()
237
239
{
238
240
    // qDebug() << "PlaylistDAO::playlistCount" << QThread::currentThread() << m_database.connectionName();
249
251
    return numRecords;
250
252
}
251
253
 
252
 
int PlaylistDAO::getPlaylistId(int position)
 
254
int PlaylistDAO::getPlaylistId(int index)
253
255
{
254
256
    // qDebug() << "PlaylistDAO::getPlaylistId"
255
257
    //          << QThread::currentThread() << m_database.connectionName();
257
259
    QSqlQuery query(m_database);
258
260
    query.prepare("SELECT id FROM Playlists");
259
261
 
 
262
    if (!query.exec()) {
 
263
        LOG_FAILED_QUERY(query);
 
264
        return -1;
 
265
    }
 
266
 
 
267
    int currentRow = 0;
 
268
    while(query.next()) {
 
269
        if (currentRow++ == index) {
 
270
            int id = query.value(0).toInt();
 
271
            return id;
 
272
        }
 
273
    }
 
274
    return -1;
 
275
}
 
276
 
 
277
PlaylistDAO::HiddenType PlaylistDAO::getHiddenType(int playlistId) {
 
278
    // qDebug() << "PlaylistDAO::getHiddenType"
 
279
    //          << QThread::currentThread() << m_database.connectionName();
 
280
 
 
281
    QSqlQuery query(m_database);
 
282
    query.prepare("SELECT hidden FROM Playlists WHERE id = :id");
 
283
    query.bindValue(":id", playlistId);
 
284
 
260
285
    if (query.exec()) {
261
 
        int currentRow = 0;
262
 
        while(query.next()) {
263
 
            if (currentRow++ == position) {
264
 
                int id = query.value(0).toInt();
265
 
                return id;
266
 
            }
 
286
        if (query.next()) {
 
287
            return static_cast<HiddenType>(query.value(0).toInt());
267
288
        }
268
289
    } else {
269
290
        LOG_FAILED_QUERY(query);
270
291
    }
271
 
 
272
 
    return -1;
 
292
    qDebug() << "PlaylistDAO::getHiddenType returns PLHT_UNKNOWN for playlistId "
 
293
             << playlistId;
 
294
    return PLHT_UNKNOWN;
273
295
}
274
296
 
275
297
bool PlaylistDAO::isHidden(int playlistId) {
276
298
    // qDebug() << "PlaylistDAO::isHidden"
277
299
    //          << QThread::currentThread() << m_database.connectionName();
278
300
 
279
 
    QSqlQuery query(m_database);
280
 
    query.prepare("SELECT hidden FROM Playlists WHERE id = :id");
281
 
    query.bindValue(":id", playlistId);
282
 
 
283
 
    if (query.exec()) {
284
 
        if (query.next()) {
285
 
            return query.value(0).toBool();
286
 
        }
287
 
    } else {
288
 
        LOG_FAILED_QUERY(query);
 
301
    HiddenType ht = getHiddenType(playlistId);
 
302
    if (ht == PLHT_NOT_HIDDEN) {
 
303
        return false;
289
304
    }
290
 
    return false;
 
305
    return true;
291
306
}
292
307
 
293
308
void PlaylistDAO::removeTrackFromPlaylists(int trackId) {
398
413
    emit(changed(playlistId));
399
414
}
400
415
 
401
 
void PlaylistDAO::addToAutoDJQueue(int playlistId) {
 
416
void PlaylistDAO::addToAutoDJQueue(int playlistId, bool bTop) {
402
417
    //qDebug() << "Adding tracks from playlist " << playlistId << " to the Auto-DJ Queue";
403
418
 
404
419
    // Query the PlaylistTracks database to locate tracks in the selected playlist
408
423
    query.bindValue(":plid", playlistId);
409
424
    if (!query.exec()) {
410
425
        LOG_FAILED_QUERY(query);
 
426
        return;
411
427
    }
412
428
 
413
429
    // Get the ID of the Auto-DJ playlist
414
430
    int autoDJId = getPlaylistIdFromName(AUTODJ_TABLE);
415
 
    // Loop through the tracks, adding them to the Auto-DJ Queue
416
 
    while(query.next()) {
417
 
        appendTrackToPlaylist(query.value(0).toInt(), autoDJId);
418
 
    }
 
431
 
 
432
    // Loop through the tracks, adding them to the Auto-DJ Queue. Start at
 
433
    // position 2 because position 1 was already loaded to the deck
 
434
    int i = 2;
 
435
 
 
436
    while (query.next()) {
 
437
        if (bTop) {
 
438
            insertTrackIntoPlaylist(query.value(0).toInt(), autoDJId, i++);
 
439
        }
 
440
        else {
 
441
            appendTrackToPlaylist(query.value(0).toInt(), autoDJId);
 
442
        }
 
443
    }
 
444
}
 
445
 
 
446
int PlaylistDAO::getPreviousPlaylist(int currentPlaylistId, HiddenType hidden) {
 
447
    // Find out the highest position existing in the playlist so we know what
 
448
    // position this track should have.
 
449
    QSqlQuery query(m_database);
 
450
    query.prepare("SELECT max(id) as id FROM Playlists "
 
451
                  "WHERE id < :id AND hidden = :hidden");
 
452
    query.bindValue(":id", currentPlaylistId);
 
453
    query.bindValue(":hidden", hidden);
 
454
 
 
455
    if (!query.exec()) {
 
456
        LOG_FAILED_QUERY(query);
 
457
        return -1;
 
458
    }
 
459
 
 
460
     // Get the id of the highest playlist
 
461
    int previousPlaylistId = -1;
 
462
    if (query.next()) {
 
463
        previousPlaylistId = query.value(query.record().indexOf("id")).toInt();
 
464
    }
 
465
    return previousPlaylistId;
 
466
}
 
467
 
 
468
void PlaylistDAO::copyPlaylistTracks(int sourcePlaylistID, int targetPlaylistId) {
 
469
    // Query Tracks from the source Playlist
 
470
    QSqlQuery query(m_database);
 
471
    query.prepare("SELECT track_id FROM PlaylistTracks "
 
472
                  "WHERE playlist_id = :plid");
 
473
    query.bindValue(":plid", sourcePlaylistID);
 
474
 
 
475
    if (!query.exec()) {
 
476
        LOG_FAILED_QUERY(query);
 
477
        return;
 
478
    }
 
479
 
 
480
    QList<int> trackIds;
 
481
    while (query.next()) {
 
482
        trackIds.append(query.value(0).toInt());
 
483
    }
 
484
    appendTracksToPlaylist(trackIds, targetPlaylistId);
419
485
}