2011-03-21 22:51:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2013-02-08 23:50:51 +01:00
|
|
|
// Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
|
2011-03-21 22:51:47 +01:00
|
|
|
//
|
|
|
|
// 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
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2011-03-21 22:51:47 +01:00
|
|
|
//
|
|
|
|
// 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>
|
2013-10-12 20:36:45 +02:00
|
|
|
#include <QMutex>
|
2011-03-21 22:51:47 +01:00
|
|
|
#include <QMutexLocker>
|
|
|
|
|
2013-10-12 20:36:45 +02:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Shared data class
|
2011-03-21 22:51:47 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
2013-10-12 20:36:45 +02:00
|
|
|
class ArtworkModel_SharedData
|
|
|
|
{
|
|
|
|
friend ArtworkModel;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ArtworkModel_SharedData(const QString &filePath, const bool isOwner)
|
|
|
|
:
|
|
|
|
m_isOwner(isOwner),
|
|
|
|
m_filePath(filePath),
|
|
|
|
m_fileHandle(NULL)
|
|
|
|
{
|
|
|
|
m_referenceCounter = 1;
|
|
|
|
|
|
|
|
if(!m_filePath.isEmpty())
|
|
|
|
{
|
|
|
|
QFile *file = new QFile(m_filePath);
|
|
|
|
if(file->open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
m_fileHandle = file;
|
|
|
|
}
|
2013-10-28 00:28:47 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
qWarning("[ArtworkModel] Failed to open artwork file!");
|
|
|
|
LAMEXP_DELETE(file);
|
|
|
|
}
|
2013-10-12 20:36:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ArtworkModel_SharedData(void)
|
|
|
|
{
|
|
|
|
if(m_fileHandle)
|
|
|
|
{
|
|
|
|
if(m_isOwner)
|
|
|
|
{
|
|
|
|
m_fileHandle->remove();
|
|
|
|
}
|
|
|
|
m_fileHandle->close();
|
|
|
|
LAMEXP_DELETE(m_fileHandle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ArtworkModel_SharedData *attach(ArtworkModel_SharedData *ptr)
|
|
|
|
{
|
|
|
|
if(ptr)
|
|
|
|
{
|
|
|
|
QMutexLocker lock(&s_mutex);
|
|
|
|
ptr->m_referenceCounter = ptr->m_referenceCounter + 1;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void detach(ArtworkModel_SharedData **ptr)
|
|
|
|
{
|
|
|
|
if(*ptr)
|
|
|
|
{
|
|
|
|
QMutexLocker lock(&s_mutex);
|
2013-10-28 00:28:47 +01:00
|
|
|
if((*ptr)->m_referenceCounter > 0)
|
|
|
|
{
|
|
|
|
(*ptr)->m_referenceCounter = (*ptr)->m_referenceCounter - 1;
|
|
|
|
if((*ptr)->m_referenceCounter < 1)
|
|
|
|
{
|
|
|
|
delete (*ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-10-12 20:36:45 +02:00
|
|
|
{
|
2013-10-28 00:28:47 +01:00
|
|
|
qWarning("[ArtworkModel::detach] Ref counter already zero!");
|
2013-10-12 20:36:45 +02:00
|
|
|
}
|
|
|
|
*ptr = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString m_filePath;
|
|
|
|
const bool m_isOwner;
|
|
|
|
QFile *m_fileHandle;
|
|
|
|
unsigned int m_referenceCounter;
|
|
|
|
|
|
|
|
static QMutex s_mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
QMutex ArtworkModel_SharedData::s_mutex;
|
2011-03-21 22:51:47 +01:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Constructor & Destructor
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
ArtworkModel::ArtworkModel(void)
|
2013-10-12 20:36:45 +02:00
|
|
|
:
|
|
|
|
m_mutex(new QMutex)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
m_data = NULL;
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 17:33:03 +01:00
|
|
|
ArtworkModel::ArtworkModel(const QString &fileName, bool isOwner)
|
2013-10-12 20:36:45 +02:00
|
|
|
:
|
|
|
|
m_mutex(new QMutex)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
m_data = new ArtworkModel_SharedData(fileName, isOwner);
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ArtworkModel::ArtworkModel(const ArtworkModel &model)
|
2013-10-12 20:36:45 +02:00
|
|
|
:
|
|
|
|
m_mutex(new QMutex)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
m_data = ArtworkModel_SharedData::attach(model.m_data);
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ArtworkModel &ArtworkModel::operator=(const ArtworkModel &model)
|
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
QMutexLocker lock(m_mutex);
|
2013-10-13 20:50:12 +02:00
|
|
|
if(m_data != model.m_data)
|
|
|
|
{
|
|
|
|
ArtworkModel_SharedData::detach(&m_data);
|
|
|
|
m_data = ArtworkModel_SharedData::attach(model.m_data);
|
|
|
|
}
|
2011-03-21 22:51:47 +01:00
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArtworkModel::~ArtworkModel(void)
|
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
QMutexLocker lock(m_mutex);
|
|
|
|
ArtworkModel_SharedData::detach(&m_data);
|
|
|
|
lock.unlock();
|
|
|
|
LAMEXP_DELETE(m_mutex);
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
const QString &ArtworkModel::filePath(void) const
|
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
QMutexLocker lock(m_mutex);
|
|
|
|
return (m_data) ? m_data->m_filePath : m_nullString;
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
2011-03-22 17:33:03 +01:00
|
|
|
bool ArtworkModel::isOwner(void) const
|
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
QMutexLocker lock(m_mutex);
|
|
|
|
return (m_data) ? m_data->m_isOwner : false;
|
2011-03-22 17:33:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArtworkModel::setFilePath(const QString &newPath, bool isOwner)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
QMutexLocker lock(m_mutex);
|
|
|
|
ArtworkModel_SharedData::detach(&m_data);
|
2013-10-28 00:28:47 +01:00
|
|
|
if(!newPath.isEmpty())
|
|
|
|
{
|
|
|
|
m_data = new ArtworkModel_SharedData(newPath, isOwner);
|
|
|
|
}
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|
|
|
|
|
2013-10-12 20:36:45 +02:00
|
|
|
void ArtworkModel::clear(void)
|
2011-03-21 22:51:47 +01:00
|
|
|
{
|
2013-10-12 20:36:45 +02:00
|
|
|
QMutexLocker lock(m_mutex);
|
|
|
|
ArtworkModel_SharedData::detach(&m_data);
|
2011-03-21 22:51:47 +01:00
|
|
|
}
|