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 "Dialog_WorkingBanner.h"
28 #include "Thread_FileAnalyzer.h"
31 #include <QMessageBox>
33 #include <QFileDialog>
34 #include <QProgressDialog>
37 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
39 ////////////////////////////////////////////////////////////
40 // Constructor & Destructor
41 ////////////////////////////////////////////////////////////
43 CueImportDialog::CueImportDialog(QWidget *parent)
47 //Init the dialog, from the .ui file
51 setMinimumSize(this->size());
52 setMaximumHeight(this->height());
55 m_model = new CueSheetModel();
56 connect(m_model, SIGNAL(modelReset()), this, SLOT(modelChanged()));
59 treeView->setModel(m_model);
60 treeView->header()->setStretchLastSection(false);
61 treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
62 treeView->header()->setResizeMode(1, QHeaderView::Stretch);
63 treeView->header()->setMovable(false);
64 treeView->setItemsExpandable(false);
66 //Enable up/down button
67 connect(imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
68 connect(browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
71 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.")));
74 CueImportDialog::~CueImportDialog(void)
76 LAMEXP_DELETE(m_model);
79 ////////////////////////////////////////////////////////////
81 ////////////////////////////////////////////////////////////
83 void CueImportDialog::showEvent(QShowEvent *event)
85 QDialog::showEvent(event);
89 ////////////////////////////////////////////////////////////
91 ////////////////////////////////////////////////////////////
93 int CueImportDialog::exec(const QString &cueFile)
95 WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
96 progress->show(tr("Loading Cue Sheet file, please be patient..."));
98 QFileInfo cueFileInfo(cueFile);
99 m_outputDir = QFileInfo(cueFile).canonicalPath();
101 setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
103 if(!cueFileInfo.exists() || !cueFileInfo.isFile() || m_outputDir.isEmpty())
105 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(cueFile), tr("The specified file could not be found!")).replace("-", "−");
106 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
108 LAMEXP_DELETE(progress);
109 return CueSheetModel::ErrorIOFailure;
112 int iResult = m_model->loadCueSheet(cueFile, QApplication::instance());
113 if(iResult != CueSheetModel::ErrorSuccess)
115 QString errorMsg = tr("An unknown error has occured!");
119 case CueSheetModel::ErrorIOFailure:
120 errorMsg = tr("The file could not be opened for reading. Make sure you have the required rights!");
122 case CueSheetModel::ErrorBadFile:
123 errorMsg = tr("The provided file does not look like a valid Cue Sheet disc image file!");
125 case CueSheetModel::ErrorUnsupported:
126 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."));
128 case CueSheetModel::ErrorInconsistent:
129 errorMsg = tr("The selected Cue Sheet file contains inconsistent information. Take care!");
133 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(cueFile), errorMsg).replace("-", "−");
134 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
136 LAMEXP_DELETE(progress);
141 LAMEXP_DELETE(progress);
142 return QDialog::exec();
145 void CueImportDialog::modelChanged(void)
147 treeView->expandAll();
148 editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
151 void CueImportDialog::browseButtonClicked(void)
153 QString newOutDir = QFileDialog::getExistingDirectory(this, tr("Choose Output Directory"));
154 if(!newOutDir.isEmpty())
156 m_outputDir = newOutDir;
161 void CueImportDialog::importButtonClicked(void)
163 static const __int64 oneGigabyte = 1073741824i64;
164 static const __int64 minimumFreeDiskspaceMultiplier = 2i64;
165 static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
167 QFile writeTest(QString("%1/~%2.txt").arg(m_outputDir, lamexp_rand_str()));
168 if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
170 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory is not writable!")));
179 qint64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir);
180 if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
182 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!")));
189 void CueImportDialog::analyzedFile(const AudioFileModel &file)
191 qWarning("Received results for: %s", file.filePath().toLatin1().constData());
192 m_fileInfo.insert(file.filePath(), file);
195 ////////////////////////////////////////////////////////////
197 ////////////////////////////////////////////////////////////
199 void CueImportDialog::importCueSheet(void)
202 int nFiles = m_model->getFileCount();
204 //Fetch all files that are referenced in the Cue Sheet
205 for(int i = 0; i < nFiles; i++)
207 files << m_model->getFileName(i);
210 //Analyze all source files
214 void CueImportDialog::analyzeFiles(QStringList &files)
218 WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
219 FileAnalyzer *analyzer = new FileAnalyzer(files);
220 connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
221 connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
223 progress->show(tr("Adding file(s), please wait..."), analyzer);
225 LAMEXP_DELETE(progress);