1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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_Process.h"
25 #include "Model_AudioFile.h"
26 #include "Model_Progress.h"
27 #include "Encoder_Abstract.h"
29 #include "Model_Settings.h"
35 #include <QMutexLocker>
40 QMutex *ProcessThread::m_mutex_genFileName = NULL;
42 ////////////////////////////////////////////////////////////
44 ////////////////////////////////////////////////////////////
46 ProcessThread::ProcessThread(const AudioFileModel &audioFile, const QString &outputDirectory, AbstractEncoder *encoder)
48 m_audioFile(audioFile),
49 m_outputDirectory(outputDirectory),
51 m_jobId(QUuid::createUuid()),
54 if(m_mutex_genFileName)
56 m_mutex_genFileName = new QMutex;
59 connect(m_encoder, SIGNAL(statusUpdated(int)), this, SLOT(handleUpdate(int)), Qt::DirectConnection);
60 connect(m_encoder, SIGNAL(messageLogged(QString)), this, SLOT(handleMessage(QString)), Qt::DirectConnection);
63 ProcessThread::~ProcessThread(void)
65 LAMEXP_DELETE(m_encoder);
68 void ProcessThread::run()
78 fprintf(stderr, "\nEXCEPTION ERROR !!!\n");
79 FatalAppExit(0, L"Unhandeled exception error, application will exit!");
80 TerminateProcess(GetCurrentProcess(), -1);
84 void ProcessThread::processFile()
88 qDebug("Process thread %s has started.", m_jobId.toString().toLatin1().constData());
89 emit processStateInitialized(m_jobId, QFileInfo(m_audioFile.filePath()).fileName(), "Starting...", ProgressModel::JobRunning);
91 QString outFileName = generateOutFileName();
92 if(outFileName.isEmpty())
94 emit processStateChanged(m_jobId, "Not found!", ProgressModel::JobFailed);
95 emit processStateFinished(m_jobId, outFileName, false);
99 if(!m_encoder->isFormatSupported(m_audioFile.formatContainerType(), m_audioFile.formatContainerProfile(), m_audioFile.formatAudioType(), m_audioFile.formatAudioProfile(), m_audioFile.formatAudioVersion()))
101 handleMessage(QString("The format of this file is NOT supported:\n%1\n\nContainer Format:\t%2\nAudio Format:\t%3").arg(m_audioFile.filePath(), m_audioFile.formatContainerInfo(), m_audioFile.formatAudioCompressInfo()));
102 emit processStateChanged(m_jobId, "Unsupported!", ProgressModel::JobFailed);
103 emit processStateFinished(m_jobId, outFileName, false);
107 bool bSuccess = m_encoder->encode(m_audioFile, outFileName, &m_aborted);
111 QFileInfo fileInfo(outFileName);
112 bSuccess = fileInfo.exists() && fileInfo.isFile() && (fileInfo.size() > 0);
115 emit processStateChanged(m_jobId, (bSuccess ? "Done." : (m_aborted ? "Aborted!" : "Failed!")), (bSuccess ? ProgressModel::JobComplete : ProgressModel::JobFailed));
116 emit processStateFinished(m_jobId, outFileName, bSuccess);
118 qDebug("Process thread is done.");
121 ////////////////////////////////////////////////////////////
123 ////////////////////////////////////////////////////////////
125 void ProcessThread::handleUpdate(int progress)
127 emit processStateChanged(m_jobId, QString("Encoding (%1%)").arg(QString::number(progress)), ProgressModel::JobRunning);
130 void ProcessThread::handleMessage(const QString &line)
132 emit processMessageLogged(m_jobId, line);
135 ////////////////////////////////////////////////////////////
137 ////////////////////////////////////////////////////////////
139 QString ProcessThread::generateOutFileName(void)
141 QMutexLocker lock(m_mutex_genFileName);
145 QFileInfo sourceFile(m_audioFile.filePath());
146 if(!sourceFile.exists() || !sourceFile.isFile())
148 handleMessage(QString("The source audio file could not be found:\n%1").arg(sourceFile.absoluteFilePath()));
152 QFile readTest(sourceFile.canonicalFilePath());
153 if(!readTest.open(QIODevice::ReadOnly))
155 handleMessage(QString("The source audio file could not be opened for reading:\n%1").arg(readTest.fileName()));
163 QString baseName = sourceFile.completeBaseName();
164 QDir targetDir(m_outputDirectory.isEmpty() ? sourceFile.canonicalPath() : m_outputDirectory);
166 if(!targetDir.exists())
168 targetDir.mkpath(".");
169 if(!targetDir.exists())
171 handleMessage(QString("The target output directory doesn't exist and could NOT be created:\n%1").arg(targetDir.absolutePath()));
176 QFile writeTest(QString("%1/%2").arg(targetDir.canonicalPath(), QUuid::createUuid().toString()));
177 if(!writeTest.open(QIODevice::ReadWrite))
179 handleMessage(QString("The target output directory is NOT writable:\n%1").arg(targetDir.absolutePath()));
188 QString outFileName = QString("%1/%2.%3").arg(targetDir.canonicalPath(), baseName, m_encoder->extension());
189 while(QFileInfo(outFileName).exists())
191 outFileName = QString("%1/%2 (%3).%4").arg(targetDir.canonicalPath(), baseName, QString::number(++n), m_encoder->extension());
194 QFile placeholder(outFileName);
195 if(placeholder.open(QIODevice::WriteOnly))
203 ////////////////////////////////////////////////////////////
205 ////////////////////////////////////////////////////////////