Implemented multi-threading for file analyzer. Now multiple files can be analyzed...
[LameXP.git] / src / Model_Artwork.cpp
blob8ce08940b1618b1f0e52dacbd7c75b03e89e6ff4
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
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"
24 #include "Global.h"
26 #include <QFile>
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)
41 m_isOwner = false;
44 ArtworkModel::ArtworkModel(const QString &fileName, bool isOwner)
46 m_isOwner = false;
47 setFilePath(fileName, isOwner);
50 ArtworkModel::ArtworkModel(const ArtworkModel &model)
52 m_isOwner = false;
53 setFilePath(model.m_filePath, model.m_isOwner);
56 ArtworkModel &ArtworkModel::operator=(const ArtworkModel &model)
58 setFilePath(model.m_filePath, model.m_isOwner);
59 return (*this);
62 ArtworkModel::~ArtworkModel(void)
64 clear();
68 ////////////////////////////////////////////////////////////
69 // Public Functions
70 ////////////////////////////////////////////////////////////
72 const QString &ArtworkModel::filePath(void) const
74 return m_filePath;
77 bool ArtworkModel::isOwner(void) const
79 return m_isOwner;
82 void ArtworkModel::setFilePath(const QString &newPath, bool isOwner)
84 if(newPath.isEmpty() || m_filePath.isEmpty() || QString::compare(m_filePath, newPath,Qt::CaseInsensitive))
86 clear();
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;
103 m_isOwner = isOwner;
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))
123 if(m_isOwner)
125 fileHandle->remove();
127 else
129 fileHandle->close();
131 LAMEXP_DELETE(fileHandle);
135 if(m_isOwner)
137 QFile::remove(m_filePath);
142 m_filePath.clear();