-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cratetablemodel.cpp
221 lines (194 loc) · 7.33 KB
/
cratetablemodel.cpp
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
#include "library/trackset/crate/cratetablemodel.h"
#include <QtDebug>
#include "library/dao/trackschema.h"
#include "library/trackcollection.h"
#include "library/trackcollectionmanager.h"
#include "library/trackset/crate/crate.h"
#include "moc_cratetablemodel.cpp"
#include "track/track.h"
#include "util/db/fwdsqlquery.h"
namespace {
const QString kModelName = QStringLiteral("crate");
} // anonymous namespace
CrateTableModel::CrateTableModel(
QObject* pParent,
TrackCollectionManager* pTrackCollectionManager)
: TrackSetTableModel(
pParent,
pTrackCollectionManager,
"mixxx.db.model.crate") {
}
void CrateTableModel::selectCrate(CrateId crateId) {
//qDebug() << "CrateTableModel::setCrate()" << crateId;
if (crateId == m_selectedCrate) {
qDebug() << "Already focused on crate " << crateId;
return;
}
// Store search text
QString currSearch = currentSearch();
if (m_selectedCrate.isValid()) {
if (!currSearch.trimmed().isEmpty()) {
m_searchTexts.insert(m_selectedCrate, currSearch);
} else {
m_searchTexts.remove(m_selectedCrate);
}
}
m_selectedCrate = crateId;
QString tableName = QStringLiteral("crate_%1").arg(m_selectedCrate.toString());
QStringList columns;
columns << LIBRARYTABLE_ID
<< "'' AS " + LIBRARYTABLE_PREVIEW
// For sorting the cover art column we give LIBRARYTABLE_COVERART
// the same value as the cover digest.
<< LIBRARYTABLE_COVERART_DIGEST + " AS " + LIBRARYTABLE_COVERART;
// We hide files that have been explicitly deleted in the library
// (mixxx_deleted = 0) from the view.
// They are kept in the database, because we treat crate membership as a
// track property, which persist over a hide / unhide cycle.
QString queryString =
QString("CREATE TEMPORARY VIEW IF NOT EXISTS %1 AS "
"SELECT %2 FROM %3 "
"WHERE %4 IN (%5) "
"AND %6=0")
.arg(tableName,
columns.join(","),
LIBRARY_TABLE,
LIBRARYTABLE_ID,
CrateStorage::formatSubselectQueryForCrateTrackIds(
crateId),
LIBRARYTABLE_MIXXXDELETED);
FwdSqlQuery(m_database, queryString).execPrepared();
columns[0] = LIBRARYTABLE_ID;
columns[1] = LIBRARYTABLE_PREVIEW;
columns[2] = LIBRARYTABLE_COVERART;
setTable(tableName,
LIBRARYTABLE_ID,
columns,
m_pTrackCollectionManager->internalCollection()->getTrackSource());
// Restore search text
setSearch(m_searchTexts.value(m_selectedCrate));
setDefaultSort(fieldIndex("artist"), Qt::AscendingOrder);
}
bool CrateTableModel::addTrack(const QModelIndex& index, const QString& location) {
Q_UNUSED(index);
// This will only succeed if the file actually exist.
mixxx::FileInfo fileInfo(location);
if (!fileInfo.checkFileExists()) {
qDebug() << "CrateTableModel::addTrack:"
<< "File" << location << "not found";
return false;
}
// If a track is dropped but it isn't in the library, then add it because
// the user probably dropped a file from outside Mixxx into this crate.
// If the track is already contained in the library it will not insert
// a duplicate. It also handles unremoving logic if the track has been
// removed from the library recently and re-adds it.
const TrackPointer pTrack = m_pTrackCollectionManager->getOrAddTrack(
TrackRef::fromFileInfo(fileInfo));
if (!pTrack) {
qDebug() << "CrateTableModel::addTrack:"
<< "Failed to add track" << location << "to library";
return false;
}
QList<TrackId> trackIds;
trackIds.append(pTrack->getId());
if (!m_pTrackCollectionManager->internalCollection()->addCrateTracks(
m_selectedCrate, trackIds)) {
qDebug() << "CrateTableModel::addTrack:"
<< "Failed to add track" << location << "to crate"
<< m_selectedCrate;
return false;
}
// TODO(rryan) just add the track don't select
select();
return true;
}
TrackModel::Capabilities CrateTableModel::getCapabilities() const {
Capabilities caps =
Capability::ReceiveDrops |
Capability::AddToTrackSet |
Capability::AddToAutoDJ |
Capability::EditMetadata |
Capability::LoadToDeck |
Capability::LoadToSampler |
Capability::LoadToPreviewDeck |
Capability::RemoveCrate |
Capability::ResetPlayed |
Capability::RemoveFromDisk |
Capability::Analyze;
if (m_selectedCrate.isValid()) {
Crate crate;
if (m_pTrackCollectionManager->internalCollection()
->crates()
.readCrateById(m_selectedCrate, &crate)) {
if (crate.isLocked()) {
caps |= Capability::Locked;
}
} else {
qWarning() << "Failed to read create" << m_selectedCrate;
}
}
return caps;
}
int CrateTableModel::addTracks(
const QModelIndex& index, const QList<QString>& locations) {
Q_UNUSED(index);
// If a track is dropped but it isn't in the library, then add it because
// the user probably dropped a file from outside Mixxx into this crate.
QList<TrackId> trackIds =
m_pTrackCollectionManager->resolveTrackIdsFromLocations(locations);
if (!m_pTrackCollectionManager->internalCollection()->addCrateTracks(
m_selectedCrate, trackIds)) {
qWarning() << "CrateTableModel::addTracks could not add"
<< locations.size() << "tracks to crate" << m_selectedCrate;
return 0;
}
select();
return trackIds.size();
}
void CrateTableModel::removeTracks(const QModelIndexList& indices) {
VERIFY_OR_DEBUG_ASSERT(m_selectedCrate.isValid()) {
return;
}
if (indices.empty()) {
return;
}
Crate crate;
if (!m_pTrackCollectionManager->internalCollection()
->crates()
.readCrateById(m_selectedCrate, &crate)) {
qWarning() << "Failed to read create" << m_selectedCrate;
return;
}
VERIFY_OR_DEBUG_ASSERT(!crate.isLocked()) {
return;
}
QList<TrackId> trackIds;
trackIds.reserve(indices.size());
for (const QModelIndex& index : indices) {
trackIds.append(getTrackId(index));
}
if (!m_pTrackCollectionManager->internalCollection()->removeCrateTracks(
crate.getId(), trackIds)) {
qWarning() << "Failed to remove tracks from crate" << crate;
return;
}
select();
}
QString CrateTableModel::modelKey(bool noSearch) const {
if (m_selectedCrate.isValid()) {
if (noSearch) {
return kModelName + QChar(':') + m_selectedCrate.toString();
}
return kModelName + QChar(':') +
m_selectedCrate.toString() +
QChar('#') +
currentSearch();
} else {
if (noSearch) {
return kModelName;
}
return kModelName + QChar('#') +
currentSearch();
}
}