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>
38 ////////////////////////////////////////////////////////////
40 ////////////////////////////////////////////////////////////
42 FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
45 m_inputFiles(inputFiles),
52 FileAnalyzer::~FileAnalyzer(void)
56 QString templatePath = m_templateFile->filePath();
57 LAMEXP_DELETE(m_templateFile);
58 if(QFile::exists(templatePath)) QFile::remove(templatePath);
64 ////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////
68 const char *FileAnalyzer::g_tags_gen[] =
77 "Artist", "Performer",
80 "Released_Date", "Recorded_Date",
89 const char *FileAnalyzer::g_tags_aud[] =
104 ////////////////////////////////////////////////////////////
106 ////////////////////////////////////////////////////////////
108 void FileAnalyzer::run()
118 if(!createTemplate())
120 qWarning("Failed to create template file!");
125 AnalyzeTask::reset();
126 QThreadPool *pool = new QThreadPool();
128 if(pool->maxThreadCount() < 2)
130 pool->setMaxThreadCount(2);
133 while(!(m_inputFiles.isEmpty() || m_bAborted))
135 while(!(m_inputFiles.isEmpty() || m_bAborted))
137 const QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
139 if(m_inputFiles.isEmpty())
144 AnalyzeTask *task = new AnalyzeTask(currentFile, m_templateFile->filePath(), &m_abortFlag);
145 connect(task, SIGNAL(fileSelected(QString)), this, SIGNAL(fileSelected(QString)), Qt::DirectConnection);
146 connect(task, SIGNAL(fileAnalyzed(AudioFileModel)), this, SIGNAL(fileAnalyzed(AudioFileModel)), Qt::DirectConnection);
148 while(!pool->tryStart(task))
150 pool->waitForDone(250); //No more free threads, wait for active threads!
151 if(m_abortFlag) { LAMEXP_DELETE(task); break; }
156 MessageBeep(MB_ICONERROR);
160 QThread::yieldCurrentThread();
166 AnalyzeTask::getAdditionalFiles(m_inputFiles);
175 qWarning("Operation cancelled by user!");
179 qDebug("All files added.\n");
183 ////////////////////////////////////////////////////////////
185 ////////////////////////////////////////////////////////////
187 bool FileAnalyzer::createTemplate(void)
191 qWarning("Template file already exists!");
195 QString templatePath = QString("%1/%2.txt").arg(lamexp_temp_folder2(), lamexp_rand_str());
197 QFile templateFile(templatePath);
198 if(!templateFile.open(QIODevice::WriteOnly))
203 templateFile.write("General;");
204 for(size_t i = 0; g_tags_gen[i]; i++)
206 templateFile.write(QString("Gen_%1=%%1%\\n").arg(g_tags_gen[i]).toLatin1().constData());
208 templateFile.write("\\n\r\n");
210 templateFile.write("Audio;");
211 for(size_t i = 0; g_tags_aud[i]; i++)
213 templateFile.write(QString("Aud_%1=%%1%\\n").arg(g_tags_aud[i]).toLatin1().constData());
215 templateFile.write("\\n\r\n");
217 bool success = (templateFile.error() == QFile::NoError);
218 templateFile.close();
222 QFile::remove(templatePath);
228 m_templateFile = new LockedFile(templatePath);
232 qWarning("Failed to lock template file!");
239 ////////////////////////////////////////////////////////////
241 ////////////////////////////////////////////////////////////
243 unsigned int FileAnalyzer::filesAccepted(void)
245 return AnalyzeTask::filesAccepted();
248 unsigned int FileAnalyzer::filesRejected(void)
250 return AnalyzeTask::filesRejected();
253 unsigned int FileAnalyzer::filesDenied(void)
255 return AnalyzeTask::filesDenied();
258 unsigned int FileAnalyzer::filesDummyCDDA(void)
260 return AnalyzeTask::filesDummyCDDA();
263 unsigned int FileAnalyzer::filesCueSheet(void)
265 return AnalyzeTask::filesCueSheet();
268 ////////////////////////////////////////////////////////////
270 ////////////////////////////////////////////////////////////