Updated MediaInfo binary to a current SVN/Trunk version with Opus support. Also remov...
[LameXP.git] / src / Thread_FileAnalyzer.cpp
blobf9cd516a039bc7ced2a14af143bbd8e1359486e8
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 "Thread_FileAnalyzer.h"
24 #include "Global.h"
25 #include "LockedFile.h"
26 #include "Model_AudioFile.h"
27 #include "Thread_FileAnalyzer_Task.h"
29 #include <QDir>
30 #include <QFileInfo>
31 #include <QProcess>
32 #include <QDate>
33 #include <QTime>
34 #include <QDebug>
35 #include <QImage>
36 #include <QThreadPool>
37 #include <QTime>
39 ////////////////////////////////////////////////////////////
40 // Constructor
41 ////////////////////////////////////////////////////////////
43 FileAnalyzer::FileAnalyzer(const QStringList &inputFiles)
45 m_abortFlag(false),
46 m_inputFiles(inputFiles),
47 m_templateFile(NULL)
49 m_bSuccess = false;
50 m_bAborted = false;
53 FileAnalyzer::~FileAnalyzer(void)
55 if(m_templateFile)
57 QString templatePath = m_templateFile->filePath();
58 LAMEXP_DELETE(m_templateFile);
59 if(QFile::exists(templatePath)) QFile::remove(templatePath);
62 AnalyzeTask::reset();
65 ////////////////////////////////////////////////////////////
66 // Static data
67 ////////////////////////////////////////////////////////////
69 const char *FileAnalyzer::g_tags_gen[] =
71 "ID",
72 "Format",
73 "Format_Profile",
74 "Format_Version",
75 "Duration",
76 "Title", "Track",
77 "Track/Position",
78 "Artist", "Performer",
79 "Album",
80 "Genre",
81 "Released_Date", "Recorded_Date",
82 "Comment",
83 "Cover",
84 "Cover_Type",
85 "Cover_Mime",
86 "Cover_Data",
87 NULL
90 const char *FileAnalyzer::g_tags_aud[] =
92 "ID",
93 "Source",
94 "Format",
95 "Format_Profile",
96 "Format_Version",
97 "Channel(s)",
98 "SamplingRate",
99 "BitDepth",
100 "BitRate",
101 "BitRate_Mode",
102 "Encoded_Library",
103 NULL
106 ////////////////////////////////////////////////////////////
107 // Thread Main
108 ////////////////////////////////////////////////////////////
110 void FileAnalyzer::run()
112 m_abortFlag = false;
114 m_bAborted = false;
115 m_bSuccess = false;
117 int nFiles = m_inputFiles.count();
119 emit progressMaxChanged(nFiles);
120 emit progressValChanged(0);
122 m_inputFiles.sort();
124 if(!m_templateFile)
126 if(!createTemplate())
128 qWarning("Failed to create template file!");
129 return;
133 AnalyzeTask::reset();
134 QThreadPool *pool = new QThreadPool();
135 QThread::msleep(333);
137 pool->setMaxThreadCount(qBound(2, ((QThread::idealThreadCount() * 3) / 2), 12));
139 while(!(m_inputFiles.isEmpty() || m_bAborted))
141 while(!(m_inputFiles.isEmpty() || m_bAborted))
143 if(!AnalyzeTask::waitForFreeSlot(&m_abortFlag))
145 qWarning("Timeout in AnalyzeTask::waitForFreeSlot() !!!");
148 if(m_abortFlag)
150 MessageBeep(MB_ICONERROR);
151 m_bAborted = true;
152 break;
155 if(!m_bAborted)
157 const QString currentFile = QDir::fromNativeSeparators(m_inputFiles.takeFirst());
159 AnalyzeTask *task = new AnalyzeTask(currentFile, m_templateFile->filePath(), &m_abortFlag);
160 connect(task, SIGNAL(fileSelected(QString)), this, SIGNAL(fileSelected(QString)), Qt::DirectConnection);
161 connect(task, SIGNAL(progressValChanged(unsigned int)), this, SIGNAL(progressValChanged(unsigned int)), Qt::DirectConnection);
162 connect(task, SIGNAL(progressMaxChanged(unsigned int)), this, SIGNAL(progressMaxChanged(unsigned int)), Qt::DirectConnection);
163 connect(task, SIGNAL(fileAnalyzed(AudioFileModel)), this, SIGNAL(fileAnalyzed(AudioFileModel)), Qt::DirectConnection);
165 pool->start(task);
167 if(int count = AnalyzeTask::getAdditionalFiles(m_inputFiles))
169 emit progressMaxChanged(nFiles += count);
174 //One of the Analyze tasks may have gathered additional files from a playlist!
175 if(!m_bAborted)
177 pool->waitForDone();
178 if(int count = AnalyzeTask::getAdditionalFiles(m_inputFiles))
180 emit progressMaxChanged(nFiles += count);
185 pool->waitForDone();
186 LAMEXP_DELETE(pool);
188 if(m_bAborted)
190 qWarning("Operation cancelled by user!");
191 return;
194 qDebug("All files added.\n");
195 m_bSuccess = true;
198 ////////////////////////////////////////////////////////////
199 // Privtae Functions
200 ////////////////////////////////////////////////////////////
202 bool FileAnalyzer::createTemplate(void)
204 if(m_templateFile)
206 qWarning("Template file already exists!");
207 return true;
210 QString templatePath = QString("%1/%2.txt").arg(lamexp_temp_folder2(), lamexp_rand_str());
212 QFile templateFile(templatePath);
213 if(!templateFile.open(QIODevice::WriteOnly))
215 return false;
218 templateFile.write("General;");
219 for(size_t i = 0; g_tags_gen[i]; i++)
221 templateFile.write(QString("Gen_%1=%%1%\\n").arg(g_tags_gen[i]).toLatin1().constData());
223 templateFile.write("\\n\r\n");
225 templateFile.write("Audio;");
226 for(size_t i = 0; g_tags_aud[i]; i++)
228 templateFile.write(QString("Aud_%1=%%1%\\n").arg(g_tags_aud[i]).toLatin1().constData());
230 templateFile.write("\\n\r\n");
232 bool success = (templateFile.error() == QFile::NoError);
233 templateFile.close();
235 if(!success)
237 QFile::remove(templatePath);
238 return false;
243 m_templateFile = new LockedFile(templatePath);
245 catch(...)
247 qWarning("Failed to lock template file!");
248 return false;
251 return true;
254 ////////////////////////////////////////////////////////////
255 // Public Functions
256 ////////////////////////////////////////////////////////////
258 unsigned int FileAnalyzer::filesAccepted(void)
260 return AnalyzeTask::filesAccepted();
263 unsigned int FileAnalyzer::filesRejected(void)
265 return AnalyzeTask::filesRejected();
268 unsigned int FileAnalyzer::filesDenied(void)
270 return AnalyzeTask::filesDenied();
273 unsigned int FileAnalyzer::filesDummyCDDA(void)
275 return AnalyzeTask::filesDummyCDDA();
278 unsigned int FileAnalyzer::filesCueSheet(void)
280 return AnalyzeTask::filesCueSheet();
283 ////////////////////////////////////////////////////////////
284 // EVENTS
285 ////////////////////////////////////////////////////////////
287 /*NONE*/