Refactored the handling of cover artwork files into a separate class.

This commit is contained in:
LoRd_MuldeR 2011-03-21 22:51:47 +01:00
parent 7e306384f6
commit bbf7e8f446
7 changed files with 195 additions and 49 deletions

View File

@ -460,6 +460,10 @@
RelativePath=".\src\Main.cpp"
>
</File>
<File
RelativePath=".\src\Model_Artwork.cpp"
>
</File>
<File
RelativePath=".\src\Model_AudioFile.cpp"
>
@ -1200,6 +1204,10 @@
RelativePath=".\src\LockedFile.h"
>
</File>
<File
RelativePath=".\src\Model_Artwork.h"
>
</File>
<File
RelativePath=".\src\Model_AudioFile.h"
>

View File

@ -25,8 +25,8 @@
#define VER_LAMEXP_MAJOR 4
#define VER_LAMEXP_MINOR_HI 0
#define VER_LAMEXP_MINOR_LO 1
#define VER_LAMEXP_BUILD 390
#define VER_LAMEXP_SUFFIX Beta-10
#define VER_LAMEXP_BUILD 394
#define VER_LAMEXP_SUFFIX Beta-11
/*
* Tools versions

125
src/Model_Artwork.cpp Normal file
View File

@ -0,0 +1,125 @@
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <QFile>
#include <QMutexLocker>
////////////////////////////////////////////////////////////
QMutex ArtworkModel::m_mutex;
QMap<QString, unsigned int> ArtworkModel::m_refCount;
QMap<QString, QFile*> 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();
}
}

49
src/Model_Artwork.h Normal file
View File

@ -0,0 +1,49 @@
///////////////////////////////////////////////////////////////////////////////
// LameXP - Audio Encoder Front-End
// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
//
// 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 <QString>
#include <QMap>
#include <QMutex>
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<QString, unsigned int> m_refCount;
static QMap<QString, QFile*> m_fileHandle;
};

View File

@ -26,10 +26,6 @@
#include <QMutexLocker>
#include <QFile>
QMutex AudioFileModel::m_mutexCovers;
QMap<QString, unsigned int> AudioFileModel::m_counterCovers;
QMap<QString, QFile*> 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)

View File

@ -21,13 +21,13 @@
#pragma once
#include "Model_Artwork.h"
#include <QObject>
#include <QString>
#include <QMap>
#include <QMutex>
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<QString, unsigned int> m_counterCovers;
static QMap<QString, QFile*> m_locksCovers;
void resetAll(void);
};

View File

@ -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();