Commit c003aabb authored by Matthieu Gallien's avatar Matthieu Gallien 🎵
Browse files

modify metadata in database but without writing them back to files

only modify the database

will need to also modify the track file itself

cannot add or remove metadata fields

error checking and validity checking not yet done
parent 02b561ec
......@@ -4989,6 +4989,7 @@ private Q_SLOTS:
newTrack[DataTypes::ComposerRole] = QStringLiteral("composer1");
newTrack[DataTypes::LyricistRole] = QStringLiteral("lyricist1");
newTrack[DataTypes::HasEmbeddedCover] = false;
newTrack[DataTypes::ElementTypeRole] = ElisaUtils::Track;
auto temp = {fullTrack, newTrack};
musicDb.insertTracksList(temp, mNewCovers);
......
......@@ -2384,6 +2384,7 @@ void MediaPlayListTest::restoreTrackWithoutAlbum()
newTrack[DataTypes::ComposerRole] = QStringLiteral("composer1");
newTrack[DataTypes::LyricistRole] = QStringLiteral("lyricist1");
newTrack[DataTypes::HasEmbeddedCover] = false;
newTrack[DataTypes::ElementTypeRole] = ElisaUtils::Track;
myDatabaseContent.insertTracksList({newTrack}, mNewCovers);
......
......@@ -1096,41 +1096,30 @@ void DatabaseInterface::insertTracksList(const DataTypes::ListTrackDataType &tra
initChangesTrackers();
for(const auto &oneTrack : tracks) {
d->mSelectTracksMapping.bindValue(QStringLiteral(":fileName"), oneTrack.resourceURI());
auto result = execQuery(d->mSelectTracksMapping);
if (!result || !d->mSelectTracksMapping.isSelect() || !d->mSelectTracksMapping.isActive()) {
Q_EMIT databaseError();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << d->mSelectTracksMapping.lastQuery();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << d->mSelectTracksMapping.boundValues();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << d->mSelectTracksMapping.lastError();
d->mSelectTracksMapping.finish();
rollBackTransaction();
Q_EMIT finishInsertingTracksList();
return;
switch (oneTrack.elementType())
{
case ElisaUtils::Track:
{
qCDebug(orgKdeElisaDatabase()) << "DatabaseInterface::insertTracksList" << "insert one track";
internalInsertOneTrack(oneTrack, covers);
break;
}
bool isNewTrack = !d->mSelectTracksMapping.next();
if (isNewTrack) {
insertTrackOrigin(oneTrack.resourceURI(), oneTrack.fileModificationTime(),
QDateTime::currentDateTime());
} else if (!d->mSelectTracksMapping.record().value(0).isNull() && d->mSelectTracksMapping.record().value(0).toULongLong() != 0) {
updateTrackOrigin(oneTrack.resourceURI(), oneTrack.fileModificationTime());
case ElisaUtils::Radio:
{
qCDebug(orgKdeElisaDatabase()) << "DatabaseInterface::insertTracksList" << "insert one radio";
internalInsertOneRadio(oneTrack);
break;
}
d->mSelectTracksMapping.finish();
bool isInserted = false;
const auto insertedTrackId = internalInsertTrack(oneTrack, covers, isInserted);
if (isInserted && insertedTrackId != 0) {
d->mInsertedTracks.insert(insertedTrackId);
case ElisaUtils::Album:
case ElisaUtils::Artist:
case ElisaUtils::Composer:
case ElisaUtils::Container:
case ElisaUtils::FileName:
case ElisaUtils::Genre:
case ElisaUtils::Lyricist:
case ElisaUtils::Unknown:
qCDebug(orgKdeElisaDatabase()) << "DatabaseInterface::insertTracksList" << "invalid track data";
break;
}
if (d->mStopRequest == 1) {
......@@ -3459,6 +3448,84 @@ void DatabaseInterface::callUpgradeFunctionForVersion(DatabaseVersion databaseVe
}
}
void DatabaseInterface::internalInsertOneTrack(const DataTypes::TrackDataType &oneTrack, const QHash<QString, QUrl> &covers)
{
d->mSelectTracksMapping.bindValue(QStringLiteral(":fileName"), oneTrack.resourceURI());
auto result = execQuery(d->mSelectTracksMapping);
if (!result || !d->mSelectTracksMapping.isSelect() || !d->mSelectTracksMapping.isActive()) {
Q_EMIT databaseError();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << d->mSelectTracksMapping.lastQuery();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << d->mSelectTracksMapping.boundValues();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << d->mSelectTracksMapping.lastError();
d->mSelectTracksMapping.finish();
rollBackTransaction();
Q_EMIT finishInsertingTracksList();
return;
}
bool isNewTrack = !d->mSelectTracksMapping.next();
if (isNewTrack) {
insertTrackOrigin(oneTrack.resourceURI(), oneTrack.fileModificationTime(),
QDateTime::currentDateTime());
} else if (!d->mSelectTracksMapping.record().value(0).isNull() && d->mSelectTracksMapping.record().value(0).toULongLong() != 0) {
updateTrackOrigin(oneTrack.resourceURI(), oneTrack.fileModificationTime());
}
d->mSelectTracksMapping.finish();
bool isInserted = false;
const auto insertedTrackId = internalInsertTrack(oneTrack, covers, isInserted);
if (isInserted && insertedTrackId != 0) {
d->mInsertedTracks.insert(insertedTrackId);
}
}
void DatabaseInterface::internalInsertOneRadio(const DataTypes::TrackDataType &oneTrack)
{
QSqlQuery query = d->mUpdateRadioQuery;
if (oneTrack.databaseId() == -1ull) {
query = d->mInsertRadioQuery;
}
query.bindValue(QStringLiteral(":httpAddress"), oneTrack.resourceURI());
query.bindValue(QStringLiteral(":radioId"), oneTrack.databaseId());
query.bindValue(QStringLiteral(":title"), oneTrack.title());
query.bindValue(QStringLiteral(":comment"), oneTrack.comment());
query.bindValue(QStringLiteral(":trackRating"), oneTrack.rating());
query.bindValue(QStringLiteral(":imageAddress"), oneTrack.albumCover());
auto result = execQuery(query);
if (!result || !query.isActive()) {
Q_EMIT databaseError();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << query.lastQuery();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << query.boundValues();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::insertTracksList" << query.lastError();
} else {
if (oneTrack[DataTypes::TrackDataType::key_type::DatabaseIdRole] == -1) {
auto radio = internalOneRadioPartialData(internalRadioIdFromHttpAddress(oneTrack.resourceURI().toString()));
Q_EMIT radioAdded(radio);
} else {
auto radio = internalOneRadioPartialData(oneTrack.databaseId());
Q_EMIT radioModified(radio);
}
}
query.finish();
}
void DatabaseInterface::initRequest()
{
auto transactionResult = startTransaction();
......@@ -7414,44 +7481,6 @@ void DatabaseInterface::updateTrackInDatabase(const DataTypes::TrackDataType &on
d->mUpdateTrackQuery.finish();
}
void DatabaseInterface::insertRadio(const DataTypes::TrackDataType &oneTrack)
{
QSqlQuery query = d->mUpdateRadioQuery;
if (oneTrack.databaseId() == -1ull) {
query = d->mInsertRadioQuery;
}
query.bindValue(QStringLiteral(":httpAddress"), oneTrack.resourceURI());
query.bindValue(QStringLiteral(":radioId"), oneTrack.databaseId());
query.bindValue(QStringLiteral(":title"), oneTrack.title());
query.bindValue(QStringLiteral(":comment"), oneTrack.comment());
query.bindValue(QStringLiteral(":trackRating"), oneTrack.rating());
query.bindValue(QStringLiteral(":imageAddress"), oneTrack.albumCover());
auto result = execQuery(query);
if (!result || !query.isActive()) {
Q_EMIT databaseError();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::updateTrackInDatabase" << query.lastQuery();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::updateTrackInDatabase" << query.boundValues();
qCDebug(orgKdeElisaDatabase) << "DatabaseInterface::updateTrackInDatabase" << query.lastError();
} else {
if (oneTrack[DataTypes::TrackDataType::key_type::DatabaseIdRole] == -1) {
auto radio = internalOneRadioPartialData(internalRadioIdFromHttpAddress(oneTrack.resourceURI().toString()));
Q_EMIT radioAdded(radio);
} else {
auto radio = internalOneRadioPartialData(oneTrack.databaseId());
Q_EMIT radioModified(radio);
}
}
query.finish();
}
void DatabaseInterface::removeRadio(qulonglong radioId)
{
QSqlQuery query = d->mDeleteRadioQuery;
......@@ -7619,7 +7648,7 @@ qulonglong DatabaseInterface::internalAlbumIdFromTitleAndArtist(const QString &t
d->mSelectAlbumIdFromTitleWithoutArtistQuery.bindValue(QStringLiteral(":title"), title);
d->mSelectAlbumIdFromTitleWithoutArtistQuery.bindValue(QStringLiteral(":albumPath"), albumPath);
auto queryResult = execQuery(d->mSelectAlbumIdFromTitleWithoutArtistQuery);
queryResult = execQuery(d->mSelectAlbumIdFromTitleWithoutArtistQuery);
if (!queryResult || !d->mSelectAlbumIdFromTitleWithoutArtistQuery.isSelect() || !d->mSelectAlbumIdFromTitleWithoutArtistQuery.isActive()) {
Q_EMIT databaseError();
......
......@@ -152,8 +152,6 @@ public Q_SLOTS:
void clearData();
void insertRadio(const DataTypes::TrackDataType &oneTrack);
void removeRadio(qulonglong radioId);
private:
......@@ -338,6 +336,10 @@ private:
void callUpgradeFunctionForVersion(DatabaseVersion databaseVersion);
void internalInsertOneTrack(const DataTypes::TrackDataType &oneTrack, const QHash<QString, QUrl> &covers);
void internalInsertOneRadio(const DataTypes::TrackDataType &oneTrack);
std::unique_ptr<DatabaseInterfacePrivate> d;
};
......
......@@ -123,7 +123,8 @@ public:
{key_type::FileModificationTime, fileModificationTime}, {key_type::ImageUrlRole, std::move(aAlbumCover)},
{key_type::RatingRole, rating}, {key_type::IsSingleDiscAlbumRole, aIsSingleDiscAlbum},
{key_type::GenreRole, std::move(aGenre)}, {key_type::ComposerRole, std::move(aComposer)},
{key_type::LyricistRole, std::move(aLyricist)}, {key_type::HasEmbeddedCover, aHasEmbeddedCover},})
{key_type::LyricistRole, std::move(aLyricist)}, {key_type::HasEmbeddedCover, aHasEmbeddedCover},
{key_type::ElementTypeRole, ElisaUtils::Track},})
{
Q_UNUSED(aValid)
}
......
......@@ -105,6 +105,7 @@ DataTypes::TrackDataType FileScanner::scanOneFile(const QUrl &scanFile, const QF
newTrack[DataTypes::FileModificationTime] = scanFileInfo.metadataChangeTime();
newTrack[DataTypes::ResourceRole] = scanFile;
newTrack[DataTypes::RatingRole] = 0;
newTrack[DataTypes::ElementTypeRole] = ElisaUtils::Track;
#if defined KF5FileMetaData_FOUND && KF5FileMetaData_FOUND
const auto &fileMimeType = d->mMimeDb.mimeTypeForFile(localFileName);
......@@ -160,6 +161,7 @@ DataTypes::TrackDataType FileScanner::scanOneBalooFile(const QUrl &scanFile, con
newTrack[DataTypes::FileModificationTime] = scanFileInfo.metadataChangeTime();
newTrack[DataTypes::ResourceRole] = scanFile;
newTrack[DataTypes::RatingRole] = 0;
newTrack[DataTypes::ElementTypeRole] = ElisaUtils::Track;
Baloo::File match(localFileName);
......
......@@ -62,8 +62,8 @@ void ModelDataLoader::setDatabase(DatabaseInterface *database)
this, &ModelDataLoader::databaseArtistsAdded);
connect(database, &DatabaseInterface::artistRemoved,
this, &ModelDataLoader::artistRemoved);
connect(this, &ModelDataLoader::saveRadioModified,
database, &DatabaseInterface::insertRadio);
connect(this, &ModelDataLoader::saveTrackModified,
database, &DatabaseInterface::insertTracksList);
connect(this, &ModelDataLoader::removeRadio,
database, &DatabaseInterface::removeRadio);
connect(database, &DatabaseInterface::radioAdded,
......
......@@ -81,7 +81,7 @@ Q_SIGNALS:
void albumModified(const ModelDataLoader::AlbumDataType &modifiedAlbum);
void saveRadioModified(const ModelDataLoader::TrackDataType &trackDataType);
void saveTrackModified(const ModelDataLoader::ListTrackDataType &trackDataType, const QHash<QString, QUrl> &covers);
void removeRadio(qulonglong radioId);
......
......@@ -49,7 +49,7 @@ void EditableTrackMetadataModel::saveData()
newTrackData[DataTypes::ImageUrlRole] = QUrl::fromUserInput(QStringLiteral("file:/").append(imageUrl));
}
Q_EMIT saveRadioData(newTrackData);
Q_EMIT saveTrackModified({newTrackData}, {});
}
void EditableTrackMetadataModel::deleteRadio()
......@@ -92,8 +92,8 @@ void EditableTrackMetadataModel::initialize(MusicListenersManager *newManager, D
{
TrackMetadataModel::initialize(newManager, trackDatabase);
connect(this, &EditableTrackMetadataModel::saveRadioData,
&modelDataLoader(), &ModelDataLoader::saveRadioModified);
connect(this, &EditableTrackMetadataModel::saveTrackModified,
&modelDataLoader(), &ModelDataLoader::saveTrackModified);
connect(this, &EditableTrackMetadataModel::deleteRadioData,
&modelDataLoader(), &ModelDataLoader::removeRadio);
}
......
......@@ -55,7 +55,7 @@ Q_SIGNALS:
void errorMessageChanged();
void saveRadioData(const DataTypes::TrackDataType &trackDataType);
void saveTrackModified(const DataTypes::ListTrackDataType &trackDataType, const QHash<QString, QUrl> &covers);
void deleteRadioData(qulonglong radioId);
......
......@@ -492,7 +492,7 @@ void TrackMetadataModel::fillLyricsDataFromTrack()
const TrackMetadataModel::TrackDataType &TrackMetadataModel::allTrackData() const
{
return mTrackData;
return mFullData;
}
void TrackMetadataModel::lyricsValueIsReady()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment