-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Cover Art Copy Worker #10902
Cover Art Copy Worker #10902
Changes from 14 commits
048beb3
643272e
36e8696
9a52cd3
690c645
76b9dc8
2e13c64
a333bcf
372741f
775eed1
c63be19
d6102b0
f974a48
e5558a2
97196d8
48977b3
388fe59
a615bed
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#include "library/export/coverartcopyworker.h" | ||
|
||
#include <QDebug> | ||
#include <QFileInfo> | ||
#include <QMessageBox> | ||
|
||
#include "util/fileaccess.h" | ||
#include "util/imagefiledata.h" | ||
#include "util/safelywritablefile.h" | ||
|
||
void CoverArtCopyWorker::run() { | ||
auto coverArtFileInfo = mixxx::FileInfo(m_selectedCoverArtFilePath); | ||
auto selectedCoverFileAccess = mixxx::FileAccess(coverArtFileInfo); | ||
if (!selectedCoverFileAccess.isReadable()) { | ||
if (Sandbox::askForAccess(&coverArtFileInfo)) { | ||
selectedCoverFileAccess = mixxx::FileAccess(coverArtFileInfo); | ||
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. We need also check for failure in the second path. if this code remains. 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. So double check 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. We need the isReadable() == true state at least. I have no clue about the implications on macOS. |
||
} else { | ||
return; | ||
} | ||
} | ||
|
||
ImageFileData imageFileData = ImageFileData::fromFilePath(m_selectedCoverArtFilePath); | ||
if (imageFileData.isNull()) { | ||
// TODO(rryan): feedback | ||
return; | ||
} | ||
|
||
m_coverInfo.type = CoverInfo::FILE; | ||
m_coverInfo.source = CoverInfo::USER_SELECTED; | ||
m_coverInfo.coverLocation = m_selectedCoverArtFilePath; | ||
m_coverInfo.setImage(imageFileData); | ||
|
||
if (QFileInfo(m_oldCoverArtFilePath).canonicalPath() == | ||
QFileInfo(m_selectedCoverArtFilePath).canonicalPath()) { | ||
qDebug() << "Track and selected cover art are in the same path:" | ||
<< QFileInfo(m_selectedCoverArtFilePath).canonicalPath() | ||
<< "Cover art updated without copying"; | ||
emit coverArtUpdated(m_coverInfo); | ||
return; | ||
daschuer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
copyFile(m_selectedCoverArtFilePath, m_oldCoverArtFilePath); | ||
} | ||
|
||
void CoverArtCopyWorker::copyFile( | ||
const QString& m_selectedCoverArtFilePath, | ||
const QString& m_oldCoverArtFilePath) { | ||
QFileInfo coverArtPathFileInfo(m_oldCoverArtFilePath); | ||
ImageFileData imageFileData = ImageFileData::fromFilePath(m_selectedCoverArtFilePath); | ||
|
||
if (coverArtPathFileInfo.exists()) { | ||
switch (makeOverwriteRequest(m_oldCoverArtFilePath)) { | ||
case OverwriteAnswer::Cancel: | ||
return; | ||
case OverwriteAnswer::Overwrite: | ||
break; | ||
} | ||
|
||
mixxx::SafelyWritableFile safelyWritableFile(m_oldCoverArtFilePath, | ||
mixxx::SafelyWritableFile::SafetyMode::Replace); | ||
|
||
DEBUG_ASSERT(!safelyWritableFile.fileName().isEmpty()); | ||
if (imageFileData.saveFile(safelyWritableFile.fileName())) { | ||
qDebug() << "Cover art" | ||
<< m_oldCoverArtFilePath | ||
<< "copied successfully"; | ||
fatihemreyildiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
safelyWritableFile.commit(); | ||
} else { | ||
qWarning() << "Error while copying the cover art to" << safelyWritableFile.fileName(); | ||
} | ||
} else { | ||
if (imageFileData.saveFile(m_oldCoverArtFilePath)) { | ||
qDebug() << "Cover art" | ||
<< m_oldCoverArtFilePath | ||
<< "copied successfully"; | ||
} else { | ||
qWarning() << "Error while copying the cover art to" << m_oldCoverArtFilePath; | ||
fatihemreyildiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
emit coverArtUpdated(m_coverInfo); | ||
quit(); | ||
fatihemreyildiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
CoverArtCopyWorker::OverwriteAnswer CoverArtCopyWorker::makeOverwriteRequest( | ||
const QString& filename) { | ||
QScopedPointer<std::promise<OverwriteAnswer>> mode_promise( | ||
new std::promise<OverwriteAnswer>()); | ||
std::future<OverwriteAnswer> mode_future = mode_promise->get_future(); | ||
emit askOverwrite(filename, mode_promise.data()); | ||
|
||
mode_future.wait(); | ||
|
||
if (!mode_future.valid()) { | ||
qWarning() << "CoverArtCopyWorker::askOverwrite invalid answer from future"; | ||
return OverwriteAnswer::Cancel; | ||
} | ||
|
||
OverwriteAnswer answer = mode_future.get(); | ||
switch (answer) { | ||
case OverwriteAnswer::Cancel: | ||
qDebug() << "Cover art overwrite declined"; | ||
break; | ||
default:; | ||
} | ||
|
||
return answer; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#pragma once | ||
|
||
#include <QObject> | ||
#include <QScopedPointer> | ||
#include <QString> | ||
#include <QThread> | ||
#include <future> | ||
|
||
#include "library/coverart.h" | ||
#include "library/coverartutils.h" | ||
#include "util/fileinfo.h" | ||
#include "util/imagefiledata.h" | ||
|
||
// This is a QThread class for copying the cover art. | ||
|
||
class CoverArtCopyWorker : public QThread { | ||
Q_OBJECT | ||
public: | ||
enum class OverwriteAnswer { | ||
Overwrite, | ||
Cancel = -1, | ||
}; | ||
|
||
CoverArtCopyWorker(const QString& selectedCoverArtFilePath, | ||
const QString& oldCoverArtFilePath) | ||
: m_selectedCoverArtFilePath(selectedCoverArtFilePath), | ||
m_oldCoverArtFilePath(oldCoverArtFilePath) { | ||
qRegisterMetaType<CoverInfoRelative>("CoverInfoRelative"); | ||
} | ||
|
||
~CoverArtCopyWorker() override = default; | ||
|
||
signals: | ||
void askOverwrite(const QString& filename, | ||
std::promise<CoverArtCopyWorker::OverwriteAnswer>* promise); | ||
void coverArtUpdated(const CoverInfoRelative& coverInfo); | ||
|
||
protected: | ||
void run() override; | ||
|
||
private: | ||
void copyFile(const QString& m_selectedCoverArtFilePath, | ||
const QString& m_oldCoverArtFilePath); | ||
|
||
OverwriteAnswer makeOverwriteRequest(const QString& filename); | ||
fatihemreyildiz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
CoverInfoRelative m_coverInfo; | ||
const QString m_selectedCoverArtFilePath; | ||
const QString m_oldCoverArtFilePath; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will issue a message box from this thread. I am not an apply guy. Is this necessary at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I didn't notice that the askForAccess populates message box. That will cause a SEG fault according to my experience with this worker. So I just check if it is readable, if it is not readable emitting fail signal would work IMHO. What do you think?