2011-03-21 22:51:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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)
|
|
|
|
{
|
2011-03-22 17:33:03 +01:00
|
|
|
m_isOwner = false;
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 17:33:03 +01:00
|
|
|
ArtworkModel::ArtworkModel(const QString &fileName, bool isOwner)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
2011-03-22 17:33:03 +01:00
|
|
|
m_isOwner = false;
|
|
|
|
setFilePath(fileName, isOwner);
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ArtworkModel::ArtworkModel(const ArtworkModel &model)
|
|
|
|
{
|
2011-03-22 17:33:03 +01:00
|
|
|
m_isOwner = false;
|
|
|
|
setFilePath(model.m_filePath, model.m_isOwner);
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ArtworkModel &ArtworkModel::operator=(const ArtworkModel &model)
|
|
|
|
{
|
2011-03-22 17:33:03 +01:00
|
|
|
setFilePath(model.m_filePath, model.m_isOwner);
|
2011-03-21 22:51:47 +01:00
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArtworkModel::~ArtworkModel(void)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const QString &ArtworkModel::filePath(void) const
|
|
|
|
{
|
|
|
|
return m_filePath;
|
|
|
|
}
|
|
|
|
|
2011-03-22 17:33:03 +01:00
|
|
|
bool ArtworkModel::isOwner(void) const
|
|
|
|
{
|
|
|
|
return m_isOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArtworkModel::setFilePath(const QString &newPath, bool isOwner)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
|
|
|
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;
|
2011-03-22 17:33:03 +01:00
|
|
|
m_isOwner = isOwner;
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2011-03-22 17:33:03 +01:00
|
|
|
if(m_isOwner)
|
|
|
|
{
|
|
|
|
fileHandle->remove();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fileHandle->close();
|
|
|
|
}
|
2011-03-21 22:51:47 +01:00
|
|
|
LAMEXP_DELETE(fileHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-22 17:33:03 +01:00
|
|
|
if(m_isOwner)
|
|
|
|
{
|
|
|
|
QFile::remove(m_filePath);
|
|
|
|
}
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-22 17:33:03 +01:00
|
|
|
|
2011-03-21 22:51:47 +01:00
|
|
|
m_filePath.clear();
|
|
|
|
}
|
|
|
|
}
|