GPodDevice: Use own temporary file class

QTemporaryFile keeps files open.

Fixes #1527
This commit is contained in:
Jonas Kvinge 2024-08-25 17:59:56 +02:00
parent 07c182d5b8
commit 6428ae8b3a
2 changed files with 9 additions and 11 deletions

View File

@ -36,12 +36,12 @@
#include <QString> #include <QString>
#include <QUrl> #include <QUrl>
#include <QImage> #include <QImage>
#include <QTemporaryFile>
#include <QStandardPaths> #include <QStandardPaths>
#include "core/logging.h" #include "core/logging.h"
#include "core/shared_ptr.h" #include "core/shared_ptr.h"
#include "core/application.h" #include "core/application.h"
#include "core/temporaryfile.h"
#include "collection/collectionbackend.h" #include "collection/collectionbackend.h"
#include "collection/collectionmodel.h" #include "collection/collectionmodel.h"
#include "connecteddevice.h" #include "connecteddevice.h"
@ -200,13 +200,11 @@ bool GPodDevice::CopyToStorage(const CopyJob &job, QString &error_text) {
QString temp_path = QStandardPaths::writableLocation(QStandardPaths::TempLocation); QString temp_path = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
#endif #endif
if (!QDir(temp_path).exists()) QDir().mkpath(temp_path); if (!QDir(temp_path).exists()) QDir().mkpath(temp_path);
SharedPtr<QTemporaryFile> cover_file = make_shared<QTemporaryFile>(temp_path + QStringLiteral("/track-albumcover-XXXXXX.jpg")); SharedPtr<TemporaryFile> cover_file = make_shared<TemporaryFile>(temp_path + QStringLiteral("/track-albumcover-XXXXXX.jpg"));
cover_file->setAutoRemove(true); if (!cover_file->filename().isEmpty()) {
if (cover_file->open()) {
cover_file->close();
const QImage &image = job.cover_image_; const QImage &image = job.cover_image_;
if (image.save(cover_file->fileName(), "JPG")) { if (image.save(cover_file->filename(), "JPG")) {
const QByteArray filename = QFile::encodeName(cover_file->fileName()); const QByteArray filename = QFile::encodeName(cover_file->filename());
result = itdb_track_set_thumbnails(track, filename.constData()); result = itdb_track_set_thumbnails(track, filename.constData());
if (result) { if (result) {
cover_files_ << cover_file; cover_files_ << cover_file;
@ -214,11 +212,11 @@ bool GPodDevice::CopyToStorage(const CopyJob &job, QString &error_text) {
} }
} }
else { else {
qLog(Error) << "Failed to save" << cover_file->fileName() << cover_file->errorString(); qLog(Error) << "Failed to save" << cover_file->filename();
} }
} }
else { else {
qLog(Error) << "Failed to open" << cover_file->fileName() << cover_file->errorString(); qLog(Error) << "Failed to obtain temporary file";
} }
} }
else if (!job.cover_source_.isEmpty()) { else if (!job.cover_source_.isEmpty()) {

View File

@ -33,11 +33,11 @@
#include <QString> #include <QString>
#include <QStringList> #include <QStringList>
#include <QUrl> #include <QUrl>
#include <QTemporaryFile>
#include "core/shared_ptr.h" #include "core/shared_ptr.h"
#include "core/song.h" #include "core/song.h"
#include "core/musicstorage.h" #include "core/musicstorage.h"
#include "core/temporaryfile.h"
#include "connecteddevice.h" #include "connecteddevice.h"
#include "gpodloader.h" #include "gpodloader.h"
@ -97,7 +97,7 @@ class GPodDevice : public ConnectedDevice, public virtual MusicStorage {
QMutex db_busy_; QMutex db_busy_;
SongList songs_to_add_; SongList songs_to_add_;
SongList songs_to_remove_; SongList songs_to_remove_;
QList<SharedPtr<QTemporaryFile>> cover_files_; QList<SharedPtr<TemporaryFile>> cover_files_;
}; };
#endif // GPODDEVICE_H #endif // GPODDEVICE_H