1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 "Dialog_CueImport.h"
25 #include "Model_CueSheet.h"
26 #include "Model_AudioFile.h"
27 #include "Model_FileList.h"
28 #include "Dialog_WorkingBanner.h"
29 #include "Thread_FileAnalyzer.h"
30 #include "Thread_CueSplitter.h"
31 #include "LockedFile.h"
34 #include <QMessageBox>
36 #include <QFileDialog>
37 #include <QProgressDialog>
40 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
42 ////////////////////////////////////////////////////////////
43 // Constructor & Destructor
44 ////////////////////////////////////////////////////////////
46 CueImportDialog::CueImportDialog(QWidget *parent, FileListModel *fileList, const QString &cueFile)
49 m_cueFileName(cueFile),
52 //Init the dialog, from the .ui file
56 setMinimumSize(this->size());
57 setMaximumHeight(this->height());
60 m_model = new CueSheetModel();
61 connect(m_model, SIGNAL(modelReset()), this, SLOT(modelChanged()));
64 treeView->setModel(m_model);
65 treeView->header()->setStretchLastSection(false);
66 treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
67 treeView->header()->setResizeMode(1, QHeaderView::Stretch);
68 treeView->header()->setMovable(false);
69 treeView->setItemsExpandable(false);
71 //Enable up/down button
72 connect(imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
73 connect(browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
76 labelHeaderText->setText(QString("<b>%1</b><br>%2").arg(tr("Import Cue Sheet"), tr("The following Cue Sheet will be split and imported into LameXP.")));
79 CueImportDialog::~CueImportDialog(void)
81 LAMEXP_DELETE(m_model);
84 ////////////////////////////////////////////////////////////
86 ////////////////////////////////////////////////////////////
88 void CueImportDialog::showEvent(QShowEvent *event)
90 QDialog::showEvent(event);
94 ////////////////////////////////////////////////////////////
96 ////////////////////////////////////////////////////////////
98 int CueImportDialog::exec(void)
100 WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
101 progress->show(tr("Loading Cue Sheet file, please be patient..."));
103 QFileInfo cueFileInfo(m_cueFileName);
104 if(!cueFileInfo.exists() || !cueFileInfo.isFile())
106 QString text = QString("<nobr>%1</nobr><br><nobr>%2</nobr><br><br><nobr>%3</nobr>").arg(tr("Failed to load the Cue Sheet file:"), QDir::toNativeSeparators(m_cueFileName), tr("The specified file could not be found!")).replace("-", "−");
107 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
109 LAMEXP_DELETE(progress);
110 return CueSheetModel::ErrorIOFailure;
113 m_outputDir = QString("%1/%2").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName());
114 for(int n = 2; QDir(m_outputDir).exists(); n++)
116 m_outputDir = QString("%1/%2 (%3)").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName(), QString::number(n));
119 setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
121 int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance());
122 if(iResult != CueSheetModel::ErrorSuccess)
124 QString errorMsg = tr("An unknown error has occured!");
128 case CueSheetModel::ErrorIOFailure:
129 errorMsg = tr("The file could not be opened for reading. Make sure you have the required rights!");
131 case CueSheetModel::ErrorBadFile:
132 errorMsg = tr("The provided file does not look like a valid Cue Sheet disc image file!");
134 case CueSheetModel::ErrorUnsupported:
135 errorMsg = QString("%1<br>%2").arg(tr("Could not find any supported audio track in the Cue Sheet image!"), tr("Note that LameXP can not handle \"binary\" Cue Sheet images."));
137 case CueSheetModel::ErrorInconsistent:
138 errorMsg = tr("The selected Cue Sheet file contains inconsistent information. Take care!");
142 QString text = QString("<nobr>%1</nobr><br><nobr>%2</nobr><br><br><nobr>%3</nobr>").arg(tr("Failed to load the Cue Sheet file:"), QDir::toNativeSeparators(m_cueFileName), errorMsg).replace("-", "−");
143 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
145 LAMEXP_DELETE(progress);
150 LAMEXP_DELETE(progress);
151 return QDialog::exec();
154 void CueImportDialog::modelChanged(void)
156 treeView->expandAll();
157 editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
160 void CueImportDialog::browseButtonClicked(void)
162 QString newOutDir = QFileDialog::getExistingDirectory(this, tr("Choose Output Directory"));
163 if(!newOutDir.isEmpty())
165 m_outputDir = newOutDir;
170 void CueImportDialog::importButtonClicked(void)
172 static const __int64 oneGigabyte = 1073741824i64;
173 static const __int64 minimumFreeDiskspaceMultiplier = 2i64;
174 static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
176 QDir outputDir(m_outputDir);
177 outputDir.mkpath(".");
178 if(!(outputDir.exists() && outputDir.isReadable()))
180 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory could not be created!")));
184 QFile writeTest(QString("%1/~%2.txt").arg(m_outputDir, lamexp_rand_str()));
185 if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
187 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory is not writable!")));
196 qint64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir);
197 if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
199 QMessageBox::warning(this, tr("Low Diskspace Warning"), QString("<nobr>%1</nobr><br><nobr>%2</nobr>").arg(tr("There are less than %1 GB of free diskspace available in the selected output directory.").arg(QString::number(minimumFreeDiskspaceMultiplier)), tr("It is highly recommend to free up more diskspace before proceeding with the import!")));
207 void CueImportDialog::analyzedFile(const AudioFileModel &file)
209 qDebug("Received result: <%s> <%s/%s>", file.filePath().toLatin1().constData(), file.formatContainerType().toLatin1().constData(), file.formatAudioType().toLatin1().constData());
213 ////////////////////////////////////////////////////////////
215 ////////////////////////////////////////////////////////////
217 void CueImportDialog::importCueSheet(void)
221 //Fetch all files that are referenced in the Cue Sheet and lock them
222 int nFiles = m_model->getFileCount();
223 for(int i = 0; i < nFiles; i++)
225 QString temp = m_model->getFileName(i);
228 m_locks << new LockedFile(temp);
232 qWarning("Failed to lock file: %s", err);
238 //Analyze all source files first
239 if(analyzeFiles(files))
241 //Now split files according to Cue Sheet
247 while(!m_locks.isEmpty())
249 delete m_locks.takeFirst();
253 bool CueImportDialog::analyzeFiles(QStringList &files)
256 bool bSuccess = true;
258 WorkingBanner *progress = new WorkingBanner(this);
259 FileAnalyzer *analyzer = new FileAnalyzer(files);
261 connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
262 connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
263 connect(progress, SIGNAL(userAbort()), analyzer, SLOT(abortProcess()), Qt::DirectConnection);
265 progress->show(tr("Analyzing file(s), please wait..."), analyzer);
268 if(analyzer->filesAccepted() < static_cast<unsigned int>(files.count()))
270 if(QMessageBox::warning(this, tr("Analysis Failed"), tr("Warning: The format of some of the input files could not be determined!"), tr("Continue Anyway"), tr("Abort")) == 1)
276 LAMEXP_DELETE(progress);
277 LAMEXP_DELETE(analyzer);
282 void CueImportDialog::splitFiles(void)
284 QString baseName = QFileInfo(m_cueFileName).completeBaseName().replace(".", " ").left(42).trimmed();
286 WorkingBanner *progress = new WorkingBanner(this);
287 CueSplitter *splitter = new CueSplitter(m_outputDir, baseName, m_model, m_fileInfo);
289 connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
290 connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
291 connect(progress, SIGNAL(userAbort()), splitter, SLOT(abortProcess()), Qt::DirectConnection);
293 progress->show(tr("Splitting file(s), please wait..."), splitter);
296 if(splitter->getAborted())
298 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("Process was aborted by the user after %1 track(s)!").arg(QString::number(splitter->getTracksSuccess())));
300 else if(!splitter->getSuccess())
302 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("An unexpected error has occured while splitting the Cue Sheet!"));
306 QString text = QString("<nobr>%1</nobr>").arg(tr("Imported %1 track(s) from the Cue Sheet and skipped %2 track(s).").arg(QString::number(splitter->getTracksSuccess()), QString::number(splitter->getTracksSkipped() /*+ nTracksSkipped*/)));
307 QMessageBox::information(this, tr("Cue Sheet Completed"), text);
310 LAMEXP_DELETE(splitter);
311 LAMEXP_DELETE(progress);