From bbf7e8f44666413b2f44cf59e8680d3b1ca21a9a Mon Sep 17 00:00:00 2001 From: lordmulder Date: Mon, 21 Mar 2011 22:51:47 +0100 Subject: [PATCH] Refactored the handling of cover artwork files into a separate class. --- LameXP.vcproj | 8 +++ src/Config.h | 4 +- src/Model_Artwork.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++++ src/Model_Artwork.h | 49 +++++++++++++++++ src/Model_AudioFile.cpp | 44 ++-------------- src/Model_AudioFile.h | 10 ++-- src/Thread_FileAnalyzer.cpp | 4 ++ 7 files changed, 195 insertions(+), 49 deletions(-) create mode 100644 src/Model_Artwork.cpp create mode 100644 src/Model_Artwork.h diff --git a/LameXP.vcproj b/LameXP.vcproj index f39274aa..249d24f0 100644 --- a/LameXP.vcproj +++ b/LameXP.vcproj @@ -461,6 +461,10 @@ > + + @@ -1201,6 +1205,10 @@ > + + +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#include "Model_Artwork.h" + +#include "Global.h" + +#include +#include + +//////////////////////////////////////////////////////////// + +QMutex ArtworkModel::m_mutex; +QMap ArtworkModel::m_refCount; +QMap ArtworkModel::m_fileHandle; + +//////////////////////////////////////////////////////////// +// Constructor & Destructor +//////////////////////////////////////////////////////////// + +ArtworkModel::ArtworkModel(void) +{ +} + +ArtworkModel::ArtworkModel(const QString &fileName) +{ + setFilePath(fileName); +} + +ArtworkModel::ArtworkModel(const ArtworkModel &model) +{ + setFilePath(model.m_filePath); +} + +ArtworkModel &ArtworkModel::operator=(const ArtworkModel &model) +{ + setFilePath(model.m_filePath); + return (*this); +} + +ArtworkModel::~ArtworkModel(void) +{ + clear(); +} + + +//////////////////////////////////////////////////////////// +// Public Functions +//////////////////////////////////////////////////////////// + +const QString &ArtworkModel::filePath(void) const +{ + return m_filePath; +} + +void ArtworkModel::setFilePath(const QString &newPath) +{ + if(newPath.isEmpty() || m_filePath.isEmpty() || QString::compare(m_filePath, newPath,Qt::CaseInsensitive)) + { + clear(); + + if(!newPath.isEmpty()) + { + QMutexLocker lock(&m_mutex); + + if(!m_refCount.contains(newPath)) + { + m_refCount.insert(newPath, 0); + m_fileHandle.insert(newPath, new QFile(newPath)); + m_fileHandle[newPath]->open(QIODevice::ReadOnly); + } + + m_refCount[newPath]++; + } + + m_filePath = newPath; + } +} + +void ArtworkModel:: clear(void) +{ + if(!m_filePath.isEmpty()) + { + QMutexLocker lock(&m_mutex); + + if(m_refCount.contains(m_filePath)) + { + if(--m_refCount[m_filePath] < 1) + { + m_refCount.remove(m_filePath); + + if(m_fileHandle.contains(m_filePath)) + { + if(QFile *fileHandle = m_fileHandle.take(m_filePath)) + { + fileHandle->remove(); + LAMEXP_DELETE(fileHandle); + } + } + + QFile::remove(m_filePath); + } + } + + m_filePath.clear(); + } +} diff --git a/src/Model_Artwork.h b/src/Model_Artwork.h new file mode 100644 index 00000000..2ad0ee9e --- /dev/null +++ b/src/Model_Artwork.h @@ -0,0 +1,49 @@ +/////////////////////////////////////////////////////////////////////////////// +// LameXP - Audio Encoder Front-End +// Copyright (C) 2004-2011 LoRd_MuldeR +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +// +// http://www.gnu.org/licenses/gpl-2.0.txt +/////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#include +#include +#include + +class QFile; + +class ArtworkModel +{ +public: + ArtworkModel(void); + ArtworkModel(const QString &fileName); + ArtworkModel(const ArtworkModel &model); + ArtworkModel &operator=(const ArtworkModel &model); + ~ArtworkModel(void); + + const QString &filePath(void) const; + void setFilePath(const QString &newPath); + void clear(void); + +private: + QString m_filePath; + + static QMutex m_mutex; + static QMap m_refCount; + static QMap m_fileHandle; +}; diff --git a/src/Model_AudioFile.cpp b/src/Model_AudioFile.cpp index 4dde5ae3..da220470 100644 --- a/src/Model_AudioFile.cpp +++ b/src/Model_AudioFile.cpp @@ -26,10 +26,6 @@ #include #include -QMutex AudioFileModel::m_mutexCovers; -QMap AudioFileModel::m_counterCovers; -QMap AudioFileModel::m_locksCovers; - //////////////////////////////////////////////////////////// // Constructor & Destructor //////////////////////////////////////////////////////////// @@ -64,7 +60,7 @@ AudioFileModel::AudioFileModel(const AudioFileModel &model, bool copyMetaInfo) setFileAlbum(model.m_fileAlbum); setFileGenre(model.m_fileGenre); setFileComment(model.m_fileComment); - setFileCover(model.m_fileCover); + setFileCover(model.m_fileCover.filePath()); setFileYear(model.m_fileYear); setFilePosition(model.m_filePosition); } @@ -78,7 +74,7 @@ AudioFileModel &AudioFileModel::operator=(const AudioFileModel &model) setFileAlbum(model.m_fileAlbum); setFileGenre(model.m_fileGenre); setFileComment(model.m_fileComment); - setFileCover(model.m_fileCover); + setFileCover(model.m_fileCover.filePath()); setFileYear(model.m_fileYear); setFilePosition(model.m_filePosition); setFileDuration(model.m_fileDuration); @@ -97,10 +93,6 @@ AudioFileModel &AudioFileModel::operator=(const AudioFileModel &model) AudioFileModel::~AudioFileModel(void) { - if(!m_fileCover.isEmpty()) - { - setFileCover(QString()); - } } //////////////////////////////////////////////////////////// @@ -172,7 +164,7 @@ const QString &AudioFileModel::fileComment(void) const const QString &AudioFileModel::fileCover(void) const { - return m_fileCover; + return m_fileCover.filePath(); } unsigned int AudioFileModel::fileYear(void) const @@ -347,35 +339,7 @@ void AudioFileModel::setFileComment(const QString &comment) void AudioFileModel::setFileCover(const QString &coverFile) { - QMutexLocker lock(&m_mutexCovers); - if(m_fileCover.isEmpty() || coverFile.isEmpty() || (m_fileCover.compare(coverFile, Qt::CaseInsensitive) != 0)) - { - if(!m_fileCover.isEmpty() && m_counterCovers.contains(m_fileCover)) - { - if(--m_counterCovers[m_fileCover] < 1) - { - m_counterCovers.remove(m_fileCover); - if(m_locksCovers.contains(m_fileCover)) - { - delete m_locksCovers[m_fileCover]; - m_locksCovers.remove(m_fileCover); - } - QFile::remove(m_fileCover); - } - } - if(!coverFile.isEmpty()) - { - if(!m_counterCovers.contains(coverFile)) - { - m_counterCovers.insert(coverFile, 0); - m_locksCovers.insert(coverFile, new QFile(coverFile)); - m_locksCovers[coverFile]->open(QIODevice::ReadOnly); - } - m_counterCovers[coverFile]++; - } - } - - m_fileCover = coverFile; + m_fileCover = ArtworkModel(coverFile); } void AudioFileModel::setFileYear(unsigned int year) diff --git a/src/Model_AudioFile.h b/src/Model_AudioFile.h index 32d908c1..74e99ab9 100644 --- a/src/Model_AudioFile.h +++ b/src/Model_AudioFile.h @@ -21,13 +21,13 @@ #pragma once +#include "Model_Artwork.h" + #include #include #include #include -class QFile; - class AudioFileModel : public QObject { Q_OBJECT @@ -100,7 +100,7 @@ private: QString m_fileAlbum; QString m_fileGenre; QString m_fileComment; - QString m_fileCover; + ArtworkModel m_fileCover; unsigned int m_fileYear; unsigned int m_filePosition; unsigned int m_fileDuration; @@ -114,9 +114,5 @@ private: unsigned int m_formatAudioChannels; unsigned int m_formatAudioBitdepth; - static QMutex m_mutexCovers; - static QMap m_counterCovers; - static QMap m_locksCovers; - void resetAll(void); }; diff --git a/src/Thread_FileAnalyzer.cpp b/src/Thread_FileAnalyzer.cpp index 740cdbf9..20372298 100644 --- a/src/Thread_FileAnalyzer.cpp +++ b/src/Thread_FileAnalyzer.cpp @@ -273,6 +273,10 @@ void FileAnalyzer::updateInfo(AudioFileModel &audioFile, const QString &key, con { if(audioFile.formatContainerProfile().isEmpty()) audioFile.setFormatContainerProfile(value); } + else if(!key.compare("Cover", Qt::CaseInsensitive) || !key.compare("Cover type", Qt::CaseInsensitive)) + { + if(m_currentCover == coverNone) m_currentCover = coverJpeg; + } else if(!key.compare("Cover MIME", Qt::CaseInsensitive)) { QString temp = value.split(" ", QString::SkipEmptyParts, Qt::CaseInsensitive).first(); -- 2.11.0