Bump version.
[LameXP.git] / src / Model_Artwork.cpp
blob9b019e92a273b85f80ddf2e4aefe9b5525c48c3e
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2021 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "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"
25 //Internal
26 #include "Global.h"
28 //MUtils
29 #include <MUtils/Global.h>
31 //Qt
32 #include <QFile>
33 #include <QMutex>
34 #include <QMutexLocker>
36 ////////////////////////////////////////////////////////////
37 // Shared data class
38 ////////////////////////////////////////////////////////////
40 class ArtworkModel_SharedData
42 friend ArtworkModel;
44 protected:
45 ArtworkModel_SharedData(const QString &filePath, const bool isOwner)
47 m_isOwner(isOwner),
48 m_filePath(filePath),
49 m_fileHandle(NULL)
51 m_referenceCounter = 1;
53 if(!m_filePath.isEmpty())
55 QFile *file = new QFile(m_filePath);
56 if(file->open(QIODevice::ReadOnly))
58 m_fileHandle = file;
60 else
62 qWarning("[ArtworkModel] Failed to open artwork file!");
63 MUTILS_DELETE(file);
68 ~ArtworkModel_SharedData(void)
70 if(m_fileHandle)
72 if(m_isOwner)
74 m_fileHandle->remove();
76 m_fileHandle->close();
77 MUTILS_DELETE(m_fileHandle);
81 static ArtworkModel_SharedData *attach(ArtworkModel_SharedData *ptr)
83 if(ptr)
85 QMutexLocker lock(&s_mutex);
86 ptr->m_referenceCounter = ptr->m_referenceCounter + 1;
87 return ptr;
89 return NULL;
92 static void detach(ArtworkModel_SharedData **ptr)
94 if(*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)
102 delete (*ptr);
105 else
107 qWarning("[ArtworkModel::detach] Ref counter already zero!");
109 *ptr = NULL;
113 const QString m_filePath;
114 const bool m_isOwner;
115 QFile *m_fileHandle;
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)
129 m_mutex(new QMutex)
131 m_data = NULL;
134 ArtworkModel::ArtworkModel(const QString &fileName, bool isOwner)
136 m_mutex(new QMutex)
138 m_data = new ArtworkModel_SharedData(fileName, isOwner);
141 ArtworkModel::ArtworkModel(const ArtworkModel &model)
143 m_mutex(new QMutex)
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);
156 return (*this);
159 ArtworkModel::~ArtworkModel(void)
161 QMutexLocker lock(m_mutex);
162 ArtworkModel_SharedData::detach(&m_data);
163 lock.unlock();
164 MUTILS_DELETE(m_mutex);
167 ////////////////////////////////////////////////////////////
168 // Public Functions
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);