1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Model_Artwork.h"
29 #include <MUtils/Global.h>
34 #include <QMutexLocker>
36 ////////////////////////////////////////////////////////////
38 ////////////////////////////////////////////////////////////
40 class ArtworkModel_SharedData
45 ArtworkModel_SharedData(const QString
&filePath
, const bool isOwner
)
51 m_referenceCounter
= 1;
53 if(!m_filePath
.isEmpty())
55 QFile
*file
= new QFile(m_filePath
);
56 if(file
->open(QIODevice::ReadOnly
))
62 qWarning("[ArtworkModel] Failed to open artwork file!");
68 ~ArtworkModel_SharedData(void)
74 m_fileHandle
->remove();
76 m_fileHandle
->close();
77 MUTILS_DELETE(m_fileHandle
);
81 static ArtworkModel_SharedData
*attach(ArtworkModel_SharedData
*ptr
)
85 QMutexLocker
lock(&s_mutex
);
86 ptr
->m_referenceCounter
= ptr
->m_referenceCounter
+ 1;
92 static void detach(ArtworkModel_SharedData
**ptr
)
96 QMutexLocker
lock(&s_mutex
);
97 if((*ptr
)->m_referenceCounter
> 0)
99 (*ptr
)->m_referenceCounter
= (*ptr
)->m_referenceCounter
- 1;
100 if((*ptr
)->m_referenceCounter
< 1)
107 qWarning("[ArtworkModel::detach] Ref counter already zero!");
113 const QString m_filePath
;
114 const bool m_isOwner
;
116 unsigned int m_referenceCounter
;
118 static QMutex s_mutex
;
121 QMutex
ArtworkModel_SharedData::s_mutex
;
123 ////////////////////////////////////////////////////////////
124 // Constructor & Destructor
125 ////////////////////////////////////////////////////////////
127 ArtworkModel::ArtworkModel(void)
134 ArtworkModel::ArtworkModel(const QString
&fileName
, bool isOwner
)
138 m_data
= new ArtworkModel_SharedData(fileName
, isOwner
);
141 ArtworkModel::ArtworkModel(const ArtworkModel
&model
)
145 m_data
= ArtworkModel_SharedData::attach(model
.m_data
);
148 ArtworkModel
&ArtworkModel::operator=(const ArtworkModel
&model
)
150 QMutexLocker
lock(m_mutex
);
151 if(m_data
!= model
.m_data
)
153 ArtworkModel_SharedData::detach(&m_data
);
154 m_data
= ArtworkModel_SharedData::attach(model
.m_data
);
159 ArtworkModel::~ArtworkModel(void)
161 QMutexLocker
lock(m_mutex
);
162 ArtworkModel_SharedData::detach(&m_data
);
164 MUTILS_DELETE(m_mutex
);
167 ////////////////////////////////////////////////////////////
169 ////////////////////////////////////////////////////////////
171 const QString
&ArtworkModel::filePath(void) const
173 QMutexLocker
lock(m_mutex
);
174 return (m_data
) ? m_data
->m_filePath
: m_nullString
;
177 bool ArtworkModel::isOwner(void) const
179 QMutexLocker
lock(m_mutex
);
180 return (m_data
) ? m_data
->m_isOwner
: false;
183 void ArtworkModel::setFilePath(const QString
&newPath
, bool isOwner
)
185 QMutexLocker
lock(m_mutex
);
186 ArtworkModel_SharedData::detach(&m_data
);
187 if(!newPath
.isEmpty())
189 m_data
= new ArtworkModel_SharedData(newPath
, isOwner
);
193 void ArtworkModel::clear(void)
195 QMutexLocker
lock(m_mutex
);
196 ArtworkModel_SharedData::detach(&m_data
);