1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
#include "library/banshee/bansheeplaylistmodel.h"
#include <QtDebug>
#include "library/banshee/bansheedbconnection.h"
#include "library/queryutil.h"
#include "library/starrating.h"
#include "library/trackcollectionmanager.h"
#include "mixer/playermanager.h"
#include "track/beatfactory.h"
#include "track/beats.h"
#define BANSHEE_TABLE "banshee"
#define CLM_TRACK_ID "track_id"
#define CLM_VIEW_ORDER "position"
#define CLM_ARTIST "artist"
#define CLM_TITLE "title"
#define CLM_DURATION "duration"
#define CLM_URI "location"
#define CLM_ALBUM "album"
#define CLM_ALBUM_ARTIST "album_artist"
#define CLM_YEAR "year"
#define CLM_RATING "rating"
#define CLM_GENRE "genre"
#define CLM_GROUPING "grouping"
#define CLM_TRACKNUMBER "tracknumber"
#define CLM_DATEADDED "datetime_added"
#define CLM_BPM "bpm"
#define CLM_BITRATE "bitrate"
#define CLM_COMMENT "comment"
#define CLM_PLAYCOUNT "timesplayed"
#define CLM_COMPOSER "composer"
#define CLM_PREVIEW "preview"
namespace {
QAtomicInt sTableNumber;
}
BansheePlaylistModel::BansheePlaylistModel(QObject* pParent, TrackCollectionManager* pTrackCollectionManager, BansheeDbConnection* pConnection)
: BaseSqlTableModel(pParent, pTrackCollectionManager, "mixxx.db.model.banshee_playlist"),
m_pConnection(pConnection),
m_playlistId(-1) {
m_tempTableName = BANSHEE_TABLE + QString::number(sTableNumber.fetchAndAddAcquire(1));
}
BansheePlaylistModel::~BansheePlaylistModel() {
dropTempTable();
}
void BansheePlaylistModel::dropTempTable() {
if (m_playlistId >= 0) {
// Clear old playlist
m_playlistId = -1;
QSqlQuery query(m_database);
QString strQuery("DROP TABLE IF EXISTS %1");
if (!query.exec(strQuery.arg(m_tempTableName))) {
LOG_FAILED_QUERY(query);
}
}
}
void BansheePlaylistModel::setTableModel(int playlistId) {
//qDebug() << "BansheePlaylistModel::setTableModel" << this << playlistId;
if (m_playlistId == playlistId) {
qDebug() << "Already focused on playlist " << playlistId;
return;
}
dropTempTable();
if (playlistId >= 0) {
// setup new playlist
m_playlistId = playlistId;
QSqlQuery query(m_database);
QString strQuery(QStringLiteral(
"CREATE TEMP TABLE IF NOT EXISTS %1 (" //
CLM_TRACK_ID " INTEGER, " //
CLM_VIEW_ORDER " INTEGER, " //
CLM_ARTIST " TEXT, " //
CLM_TITLE " TEXT, " //
CLM_DURATION " INTEGER, " //
CLM_URI " TEXT, " //
CLM_ALBUM " TEXT, " //
CLM_ALBUM_ARTIST " TEXT, " //
CLM_YEAR " INTEGER, " //
CLM_RATING " INTEGER, " //
CLM_GENRE " TEXT, " //
CLM_GROUPING " TEXT, " //
CLM_TRACKNUMBER " INTEGER, " //
CLM_DATEADDED " INTEGER, " //
CLM_BPM " INTEGER, " //
CLM_BITRATE " INTEGER, " //
CLM_COMMENT " TEXT, " //
CLM_PLAYCOUNT " INTEGER, " //
CLM_COMPOSER " TEXT, " //
CLM_PREVIEW " TEXT)"));
if (!query.exec(strQuery.arg(m_tempTableName))) {
LOG_FAILED_QUERY(query);
}
QString strQuery2(
"INSERT INTO %1"
" (" CLM_TRACK_ID
", " CLM_VIEW_ORDER
", " CLM_ARTIST
", " CLM_TITLE
", " CLM_DURATION
", " CLM_URI
", " CLM_ALBUM
", " CLM_ALBUM_ARTIST
", " CLM_YEAR
", " CLM_RATING
", " CLM_GENRE
", " CLM_GROUPING
", " CLM_TRACKNUMBER
", " CLM_DATEADDED
", " CLM_BPM
", " CLM_BITRATE
", " CLM_COMMENT
", " CLM_PLAYCOUNT
", " CLM_COMPOSER
") "
"VALUES (:" CLM_TRACK_ID
", :" CLM_VIEW_ORDER
", :" CLM_ARTIST
", :" CLM_TITLE
", :" CLM_DURATION
", :" CLM_URI
", :" CLM_ALBUM
", :" CLM_ALBUM_ARTIST
", :" CLM_YEAR
", :" CLM_RATING
", :" CLM_GENRE
", :" CLM_GROUPING
", :" CLM_TRACKNUMBER
", :" CLM_DATEADDED
", :" CLM_BPM
", :" CLM_BITRATE
", :" CLM_COMMENT
", :" CLM_PLAYCOUNT
", :" CLM_COMPOSER
") ");
query.prepare(strQuery2.arg(m_tempTableName));
QList<struct BansheeDbConnection::PlaylistEntry> list =
m_pConnection->getPlaylistEntries(playlistId);
if (!list.isEmpty()) {
beginInsertRows(QModelIndex(), 0, list.size() - 1);
foreach (struct BansheeDbConnection::PlaylistEntry entry, list) {
query.bindValue(":" CLM_TRACK_ID, entry.trackId);
// Note: entry.viewOrder is 0 for all tracks if they have
// never been sorted by the user
query.bindValue(":" CLM_VIEW_ORDER, entry.viewOrder + 1);
query.bindValue(":" CLM_ARTIST, entry.pArtist->name);
query.bindValue(":" CLM_TITLE, entry.pTrack->title);
query.bindValue(":" CLM_DURATION, entry.pTrack->duration / 1000);
query.bindValue(":" CLM_URI, entry.pTrack->uri);
query.bindValue(":" CLM_ALBUM, entry.pAlbum->title);
query.bindValue(":" CLM_ALBUM_ARTIST, entry.pAlbumArtist->name);
query.bindValue(":" CLM_YEAR, entry.pTrack->year);
query.bindValue(":" CLM_RATING, entry.pTrack->rating);
query.bindValue(":" CLM_GENRE, entry.pTrack->genre);
query.bindValue(":" CLM_GROUPING, entry.pTrack->grouping);
query.bindValue(":" CLM_TRACKNUMBER, entry.pTrack->tracknumber);
QDateTime timeAdded;
timeAdded.setTime_t(entry.pTrack->dateadded);
query.bindValue(":" CLM_DATEADDED, timeAdded.toString(Qt::ISODate));
query.bindValue(":" CLM_BPM, entry.pTrack->bpm);
query.bindValue(":" CLM_BITRATE, entry.pTrack->bitrate);
query.bindValue(":" CLM_COMMENT, entry.pTrack->comment);
query.bindValue(":" CLM_PLAYCOUNT, entry.pTrack->playcount);
query.bindValue(":" CLM_COMPOSER, entry.pTrack->composer);
if (!query.exec()) {
LOG_FAILED_QUERY(query);
}
// qDebug() << "-----" << entry.pTrack->title << query.executedQuery();
}
endInsertRows();
}
}
QStringList tableColumns;
tableColumns
<< CLM_TRACK_ID // 0
<< CLM_VIEW_ORDER
<< CLM_PREVIEW; // 3
QStringList trackSourceColumns;
trackSourceColumns
<< CLM_TRACK_ID // 0
<< CLM_ARTIST
<< CLM_TITLE
<< CLM_DURATION
<< CLM_URI
<< CLM_ALBUM
<< CLM_ALBUM_ARTIST
<< CLM_YEAR
<< CLM_RATING
<< CLM_GENRE
<< CLM_GROUPING
<< CLM_TRACKNUMBER
<< CLM_DATEADDED
<< CLM_BPM
<< CLM_BITRATE
<< CLM_COMMENT
<< CLM_PLAYCOUNT
<< CLM_COMPOSER;
QSharedPointer<BaseTrackCache> trackSource(
new BaseTrackCache(m_pTrackCollectionManager->internalCollection(), m_tempTableName, CLM_TRACK_ID, trackSourceColumns, false));
setTable(m_tempTableName, CLM_TRACK_ID, tableColumns, trackSource);
setSearch("");
setDefaultSort(fieldIndex(PLAYLISTTRACKSTABLE_POSITION), Qt::AscendingOrder);
setSort(defaultSortColumn(), defaultSortOrder());
}
TrackModel::CapabilitiesFlags BansheePlaylistModel::getCapabilities() const {
return TRACKMODELCAPS_ADDTOPLAYLIST |
TRACKMODELCAPS_ADDTOCRATE |
TRACKMODELCAPS_ADDTOAUTODJ |
TRACKMODELCAPS_LOADTODECK |
TRACKMODELCAPS_LOADTOSAMPLER;
}
Qt::ItemFlags BansheePlaylistModel::flags(const QModelIndex& index) const {
return readOnlyFlags(index);
}
void BansheePlaylistModel::tracksChanged(QSet<TrackId> trackIds) {
Q_UNUSED(trackIds);
}
TrackId BansheePlaylistModel::doGetTrackId(const TrackPointer& pTrack) const {
if (pTrack) {
for (int row = 0; row < rowCount(); ++row) {
const QUrl rowUrl(getFieldString(index(row, 0), CLM_URI));
if (TrackFile::fromUrl(rowUrl) == pTrack->getFileInfo()) {
return TrackId(getFieldVariant(index(row, 0), CLM_VIEW_ORDER));
}
}
}
return TrackId();
}
QVariant BansheePlaylistModel::getFieldVariant(const QModelIndex& index,
const QString& fieldName) const {
return
|