OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[lamexp/LameXP.git] / src / Model_Artwork.cpp
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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Model_Artwork.h"
23
24 #include "Global.h"
25
26 #include <QFile>
27 #include <QMutexLocker>
28
29 ////////////////////////////////////////////////////////////
30
31 QMutex ArtworkModel::m_mutex;
32 QMap<QString, unsigned int> ArtworkModel::m_refCount;
33 QMap<QString, QFile*> ArtworkModel::m_fileHandle;
34
35 ////////////////////////////////////////////////////////////
36 // Constructor & Destructor
37 ////////////////////////////////////////////////////////////
38
39 ArtworkModel::ArtworkModel(void)
40 {
41         m_isOwner = false;
42 }
43
44 ArtworkModel::ArtworkModel(const QString &fileName, bool isOwner)
45 {
46         m_isOwner = false;
47         setFilePath(fileName, isOwner);
48 }
49
50 ArtworkModel::ArtworkModel(const ArtworkModel &model)
51 {
52         m_isOwner = false;
53         setFilePath(model.m_filePath, model.m_isOwner);
54 }
55
56 ArtworkModel &ArtworkModel::operator=(const ArtworkModel &model)
57 {
58         setFilePath(model.m_filePath, model.m_isOwner);
59         return (*this);
60 }
61
62 ArtworkModel::~ArtworkModel(void)
63 {
64         clear();
65 }
66
67
68 ////////////////////////////////////////////////////////////
69 // Public Functions
70 ////////////////////////////////////////////////////////////
71
72 const QString &ArtworkModel::filePath(void) const
73 {
74         return m_filePath;
75 }
76
77 bool ArtworkModel::isOwner(void) const
78 {
79         return m_isOwner;
80 }
81
82 void ArtworkModel::setFilePath(const QString &newPath, bool isOwner)
83 {
84         if(newPath.isEmpty() || m_filePath.isEmpty() || QString::compare(m_filePath, newPath,Qt::CaseInsensitive))
85         {
86                 clear();
87         
88                 if(!newPath.isEmpty())
89                 {
90                         QMutexLocker lock(&m_mutex);
91
92                         if(!m_refCount.contains(newPath))
93                         {
94                                 m_refCount.insert(newPath, 0);
95                                 m_fileHandle.insert(newPath, new QFile(newPath));
96                                 m_fileHandle[newPath]->open(QIODevice::ReadOnly);
97                         }
98
99                         m_refCount[newPath]++;
100                 }
101
102                 m_filePath = newPath;
103                 m_isOwner = isOwner;
104         }
105 }
106
107 void ArtworkModel:: clear(void)
108 {
109         if(!m_filePath.isEmpty())
110         {
111                 QMutexLocker lock(&m_mutex);
112
113                 if(m_refCount.contains(m_filePath))
114                 {
115                         if(--m_refCount[m_filePath] < 1)
116                         {
117                                 m_refCount.remove(m_filePath);
118
119                                 if(m_fileHandle.contains(m_filePath))
120                                 {
121                                         if(QFile *fileHandle = m_fileHandle.take(m_filePath))
122                                         {
123                                                 if(m_isOwner)
124                                                 {
125                                                         fileHandle->remove();
126                                                 }
127                                                 else
128                                                 {
129                                                         fileHandle->close();
130                                                 }
131                                                 LAMEXP_DELETE(fileHandle);
132                                         }
133                                 }
134
135                                 if(m_isOwner)
136                                 {
137                                         QFile::remove(m_filePath);
138                                 }
139                         }
140                 }
141
142                 m_filePath.clear();
143         }
144 }