-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add relocate button in the missing track library. #16609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
92f3ab0
e2fe302
d30dd1e
c79a860
c9ac4b2
8cade0f
fb5bc21
0e1fe83
77c0528
1e5aca7
d006eab
76c878b
52c94ef
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| #include "library/dao/trackdao.h" | ||
|
|
||
| #include <qhashfunctions.h> | ||
|
|
||
| #include <QChar> | ||
| #include <QDir> | ||
| #include <QFileInfo> | ||
| #include <QThread> | ||
| #include <QtDebug> | ||
|
|
||
| #include "track/trackref.h" | ||
|
|
||
| #ifdef __SQLITE3__ | ||
| #include <sqlite3.h> | ||
| #endif // __SQLITE3__ | ||
|
|
@@ -1778,6 +1782,119 @@ bool TrackDAO::updateTrack(const Track& track) const { | |
| return true; | ||
| } | ||
|
|
||
| // Relocate the file linked to the track | ||
| std::optional<RelocatedTrack> TrackDAO::relocateTrack( | ||
| const TrackId trackId, const mixxx::FileInfo& newLocation) { | ||
| DEBUG_ASSERT(trackId.isValid()); | ||
|
|
||
| const QString oldLocation = getTrackLocation(trackId); | ||
|
|
||
| kLogger.debug() << "Relocating track" << trackId | ||
| << "to" << newLocation | ||
| << "from" << oldLocation; | ||
|
|
||
| SqlTransaction transaction(m_database); | ||
|
|
||
| // Check for a duplicate track location. This happens if the new location | ||
| // is inside mixxx library folder because it is automatically added to | ||
| // library. If there is no duplicate, the track is outside mixxx library folder. | ||
| DbId duplicateLocationId; | ||
| TrackId duplicateLocationTrackId; | ||
| FwdSqlQuery queryDuplicateLocation(m_database, | ||
| "SELECT track_locations.id, library.id " | ||
| "FROM track_locations " | ||
| "LEFT JOIN library ON library.location = track_locations.id " | ||
| "WHERE track_locations.location = :location " | ||
| "AND (library.id != :trackId OR library.id IS NULL)"); | ||
| queryDuplicateLocation.bindValue(":location", newLocation.location()); | ||
| queryDuplicateLocation.bindValue(":trackId", trackId.toVariant()); | ||
| if (!queryDuplicateLocation.execPrepared()) { | ||
| return std::nullopt; | ||
| } | ||
| if (queryDuplicateLocation.next()) { | ||
| duplicateLocationId = DbId(queryDuplicateLocation.fieldValue(0)); | ||
| duplicateLocationTrackId = TrackId(queryDuplicateLocation.fieldValue(1)); | ||
| } | ||
| if (duplicateLocationId.isValid()) { | ||
| kLogger.debug() << "New track location already exist in db (location id: " | ||
| << duplicateLocationId | ||
| << "). Deleting duplicate track and linking new location to current track."; | ||
|
|
||
| // Fetch current track oprhaned location id. | ||
| DbId orphanedLocationId; | ||
| FwdSqlQuery queryOprhanedLocationId(m_database, | ||
| "SELECT location FROM library WHERE id = :trackId"); | ||
| queryOprhanedLocationId.bindValue(":trackId", trackId.toVariant()); | ||
| if (!queryOprhanedLocationId.execPrepared()) { | ||
| return std::nullopt; | ||
| } | ||
| if (queryOprhanedLocationId.next()) { | ||
| orphanedLocationId = DbId(queryOprhanedLocationId.fieldValue(0)); | ||
| } | ||
|
|
||
| // Delete duplicate track. | ||
| FwdSqlQuery queryDeleteDuplicate(m_database, | ||
| "DELETE FROM library WHERE id = :newTrackId"); | ||
| queryDeleteDuplicate.bindValue(":newTrackId", | ||
| duplicateLocationTrackId.toVariant()); | ||
| if (!queryDeleteDuplicate.execPrepared()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| // Update current track location to new location (duplicate location database entry). | ||
| FwdSqlQuery queryUpdateLocation(m_database, | ||
| "UPDATE library SET location = :loc WHERE id = :trackId"); | ||
| queryUpdateLocation.bindValue(":loc", duplicateLocationId.toVariant()); | ||
| queryUpdateLocation.bindValue(":trackId", trackId.toVariant()); | ||
| if (!queryUpdateLocation.execPrepared()) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| // Delete orphaned location. | ||
| FwdSqlQuery queryDeleteOrphanedLocation(m_database, | ||
| "DELETE FROM track_locations WHERE id = :id"); | ||
| queryDeleteOrphanedLocation.bindValue(":id", | ||
| orphanedLocationId.toVariant()); | ||
| queryDeleteOrphanedLocation.execPrepared(); | ||
| } else { | ||
| // Directly update orphaned location with new data since | ||
| // there is no duplicate. | ||
| FwdSqlQuery queryUpdateNoDuplicate(m_database, | ||
| "UPDATE track_locations SET " | ||
| "location = :location," | ||
| "directory = :directory," | ||
| "filename = :filename," | ||
| "filesize = :filesize," | ||
| "fs_deleted = 0," | ||
| "needs_verification = 0 " | ||
| "WHERE id=(SELECT location FROM library WHERE id =:trackId)"); | ||
| queryUpdateNoDuplicate.bindValue(":location", newLocation.location()); | ||
| queryUpdateNoDuplicate.bindValue(":directory", newLocation.locationPath()); | ||
| queryUpdateNoDuplicate.bindValue(":filename", newLocation.fileName()); | ||
| queryUpdateNoDuplicate.bindValue(":filesize", | ||
| QVariant::fromValue(newLocation.sizeInBytes())); | ||
| queryUpdateNoDuplicate.bindValue(":trackId", trackId.toVariant()); | ||
|
|
||
| if (!queryUpdateNoDuplicate.execPrepared()) { | ||
| return std::nullopt; | ||
| } | ||
| if (queryUpdateNoDuplicate.numRowsAffected() == 0) { | ||
| kLogger.warning() << "relocateTrack had no effect: trackId " << trackId << "invalid."; | ||
| return std::nullopt; | ||
| } | ||
| } | ||
| transaction.commit(); | ||
|
|
||
| const auto missingTrackRef = TrackRef::fromFilePath(oldLocation, trackId); | ||
| TrackRef duplicateTrackRef = TrackRef(); | ||
| if (duplicateLocationId.isValid()) { | ||
| duplicateTrackRef = TrackRef::fromFileInfo(newLocation, duplicateLocationTrackId); | ||
| } else { | ||
| duplicateTrackRef = TrackRef::fromFileInfo(newLocation); | ||
| } | ||
| return RelocatedTrack(missingTrackRef, duplicateTrackRef); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The similarly named variables in this function make it a bit hard to understand the flow. and also add some comments: Which cases it supposed to cover? (library scan already added a track with new location vs. new location not in db yet)
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The else case covers the case where the new file is outside of the library folder. |
||
|
|
||
| // Make sure that `directory` in in track_locations table is indeed a | ||
| // directory path. This works around / removes residues of a bug where tracks | ||
| // are falsely marked missing because `directory` == `location`. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| #include "library/missing_hidden/missingtablemodel.h" | ||
|
|
||
| #include <qfiledialog.h> | ||
| #include <qstandardpaths.h> | ||
|
|
||
| #include "library/dao/trackschema.h" | ||
| #include "library/trackcollection.h" | ||
| #include "library/trackcollectionmanager.h" | ||
| #include "moc_missingtablemodel.cpp" | ||
| #include "sources/soundsourceproxy.h" | ||
| #include "track/track.h" | ||
|
|
||
| namespace { | ||
|
|
||
|
|
@@ -62,6 +67,45 @@ void MissingTableModel::purgeTracks(const QModelIndexList& indices) { | |
| select(); //Repopulate the data model. | ||
| } | ||
|
|
||
| void MissingTableModel::relocateTrack(const QModelIndex& index) { | ||
| QString location; | ||
| QString title; | ||
| TrackId trackId; | ||
| { | ||
| TrackPointer pTrack = getTrack(index); | ||
| if (!pTrack) { | ||
| return; | ||
| } | ||
|
|
||
| location = QFileInfo(pTrack->getLocation()).absolutePath(); | ||
| title = pTrack->getTitle(); | ||
| trackId = pTrack->getId(); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason the above code is in an extra scope?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the track does not update in the cache because there are still reference linked to it. This was an attempt to delete the local reference but maybe it's useless since references are somewhere else. |
||
| if (location.isEmpty() || !QDir(location).exists()) { | ||
| location = QStandardPaths::writableLocation(QStandardPaths::MusicLocation); | ||
| } | ||
|
|
||
| const QString newLocation = QFileDialog::getOpenFileName( | ||
| nullptr, | ||
| tr("Locate missing file: %1").arg(title), | ||
| location, | ||
| QString("Audio Files (%1)") | ||
| .arg(SoundSourceProxy::getSupportedFileNamePatterns().join(" "))); | ||
|
|
||
| if (newLocation.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| const mixxx::FileInfo fileInfo(newLocation); | ||
| if (!trackId.isValid()) { | ||
| return; | ||
| } | ||
|
Comment on lines
+100
to
+102
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since every track in Missing should have a valid id, this can go up right after the line where we read the id. |
||
|
|
||
| if (m_pTrackCollectionManager->relocateTrack(trackId, fileInfo)) { | ||
| select(); // Repopulate the data model | ||
| } | ||
| } | ||
|
|
||
| bool MissingTableModel::isColumnInternal(int column) { | ||
| return column == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_ID) || | ||
| column == fieldIndex(ColumnCache::COLUMN_LIBRARYTABLE_PLAYED) || | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.