1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Model_Artwork.h"
27 #include <QMutexLocker>
29 ////////////////////////////////////////////////////////////
31 QMutex ArtworkModel::m_mutex;
32 QMap<QString, unsigned int> ArtworkModel::m_refCount;
33 QMap<QString, QFile*> ArtworkModel::m_fileHandle;
35 ////////////////////////////////////////////////////////////
36 // Constructor & Destructor
37 ////////////////////////////////////////////////////////////
39 ArtworkModel::ArtworkModel(void)
44 ArtworkModel::ArtworkModel(const QString &fileName, bool isOwner)
47 setFilePath(fileName, isOwner);
50 ArtworkModel::ArtworkModel(const ArtworkModel &model)
53 setFilePath(model.m_filePath, model.m_isOwner);
56 ArtworkModel &ArtworkModel::operator=(const ArtworkModel &model)
58 setFilePath(model.m_filePath, model.m_isOwner);
62 ArtworkModel::~ArtworkModel(void)
68 ////////////////////////////////////////////////////////////
70 ////////////////////////////////////////////////////////////
72 const QString &ArtworkModel::filePath(void) const
77 bool ArtworkModel::isOwner(void) const
82 void ArtworkModel::setFilePath(const QString &newPath, bool isOwner)
84 if(newPath.isEmpty() || m_filePath.isEmpty() || QString::compare(m_filePath, newPath,Qt::CaseInsensitive))
88 if(!newPath.isEmpty())
90 QMutexLocker lock(&m_mutex);
92 if(!m_refCount.contains(newPath))
94 m_refCount.insert(newPath, 0);
95 m_fileHandle.insert(newPath, new QFile(newPath));
96 m_fileHandle[newPath]->open(QIODevice::ReadOnly);
99 m_refCount[newPath]++;
102 m_filePath = newPath;
107 void ArtworkModel:: clear(void)
109 if(!m_filePath.isEmpty())
111 QMutexLocker lock(&m_mutex);
113 if(m_refCount.contains(m_filePath))
115 if(--m_refCount[m_filePath] < 1)
117 m_refCount.remove(m_filePath);
119 if(m_fileHandle.contains(m_filePath))
121 if(QFile *fileHandle = m_fileHandle.take(m_filePath))
125 fileHandle->remove();
131 LAMEXP_DELETE(fileHandle);
137 QFile::remove(m_filePath);