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 "Thread_FileAnalyzer.h"
25 #include "LockedFile.h"
26 #include "Model_AudioFile.h"
27 #include "Thread_FileAnalyzer_Task.h"
36 #include <QThreadPool>
39 ////////////////////////////////////////////////////////////
41 ////////////////////////////////////////////////////////////
43 FileAnalyzer::FileAnalyzer(const QStringList
&inputFiles
)
46 m_inputFiles(inputFiles
),
53 FileAnalyzer::~FileAnalyzer(void)
57 QString templatePath
= m_templateFile
->filePath();
58 LAMEXP_DELETE(m_templateFile
);
59 if(QFile::exists(templatePath
)) QFile::remove(templatePath
);
65 ////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////
69 const char *FileAnalyzer::g_tags_gen
[] =
78 "Artist", "Performer",
81 "Released_Date", "Recorded_Date",
90 const char *FileAnalyzer::g_tags_aud
[] =
105 ////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////
109 void FileAnalyzer::run()
111 qWarning("--- FileAnalyzer::run() ---");
118 int nFiles
= m_inputFiles
.count();
120 emit
progressMaxChanged(nFiles
);
121 emit
progressValChanged(0);
127 if(!createTemplate())
129 qWarning("Failed to create template file!");
134 AnalyzeTask::reset();
135 QThreadPool
*pool
= new QThreadPool();
136 QThread::msleep(333);
138 pool
->setMaxThreadCount(qBound(2, ((QThread::idealThreadCount() * 3) / 2), 12));
140 while(!(m_inputFiles
.isEmpty() || m_bAborted
))
142 while(!(m_inputFiles
.isEmpty() || m_bAborted
))
144 if(!AnalyzeTask::waitForFreeSlot(&m_abortFlag
))
146 qWarning("Timeout in AnalyzeTask::waitForFreeSlot() !!!");
151 MessageBeep(MB_ICONERROR
);
158 const QString currentFile
= QDir::fromNativeSeparators(m_inputFiles
.takeFirst());
160 AnalyzeTask
*task
= new AnalyzeTask(currentFile
, m_templateFile
->filePath(), &m_abortFlag
);
161 connect(task
, SIGNAL(fileSelected(QString
)), this, SIGNAL(fileSelected(QString
)), Qt::DirectConnection
);
162 connect(task
, SIGNAL(progressValChanged(unsigned int)), this, SIGNAL(progressValChanged(unsigned int)), Qt::DirectConnection
);
163 connect(task
, SIGNAL(progressMaxChanged(unsigned int)), this, SIGNAL(progressMaxChanged(unsigned int)), Qt::DirectConnection
);
164 connect(task
, SIGNAL(fileAnalyzed(AudioFileModel
)), this, SIGNAL(fileAnalyzed(AudioFileModel
)), Qt::DirectConnection
);
168 if(int count
= AnalyzeTask::getAdditionalFiles(m_inputFiles
))
170 emit
progressMaxChanged(nFiles
+= count
);
175 //One of the Analyze tasks may have gathered additional files from a playlist!
179 if(int count
= AnalyzeTask::getAdditionalFiles(m_inputFiles
))
181 emit
progressMaxChanged(nFiles
+= count
);
191 qWarning("Operation cancelled by user!");
195 qDebug("All files added.\n");
199 ////////////////////////////////////////////////////////////
201 ////////////////////////////////////////////////////////////
203 bool FileAnalyzer::createTemplate(void)
207 qWarning("Template file already exists!");
211 QString templatePath
= QString("%1/%2.txt").arg(lamexp_temp_folder2(), lamexp_rand_str());
213 QFile
templateFile(templatePath
);
214 if(!templateFile
.open(QIODevice::WriteOnly
))
219 templateFile
.write("General;");
220 for(size_t i
= 0; g_tags_gen
[i
]; i
++)
222 templateFile
.write(QString("Gen_%1=%%1%\\n").arg(g_tags_gen
[i
]).toLatin1().constData());
224 templateFile
.write("\\n\r\n");
226 templateFile
.write("Audio;");
227 for(size_t i
= 0; g_tags_aud
[i
]; i
++)
229 templateFile
.write(QString("Aud_%1=%%1%\\n").arg(g_tags_aud
[i
]).toLatin1().constData());
231 templateFile
.write("\\n\r\n");
233 bool success
= (templateFile
.error() == QFile::NoError
);
234 templateFile
.close();
238 QFile::remove(templatePath
);
244 m_templateFile
= new LockedFile(templatePath
);
248 qWarning("Failed to lock template file!");
255 ////////////////////////////////////////////////////////////
257 ////////////////////////////////////////////////////////////
259 unsigned int FileAnalyzer::filesAccepted(void)
261 return AnalyzeTask::filesAccepted();
264 unsigned int FileAnalyzer::filesRejected(void)
266 return AnalyzeTask::filesRejected();
269 unsigned int FileAnalyzer::filesDenied(void)
271 return AnalyzeTask::filesDenied();
274 unsigned int FileAnalyzer::filesDummyCDDA(void)
276 return AnalyzeTask::filesDummyCDDA();
279 unsigned int FileAnalyzer::filesCueSheet(void)
281 return AnalyzeTask::filesCueSheet();
284 ////////////////////////////////////////////////////////////
286 ////////////////////////////////////////////////////////////