OSDN Git Service

The Cue Sheet splitter will now also handle input files that are not PCM/Wave.
[lamexp/LameXP.git] / src / Dialog_CueImport.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Dialog_CueImport.h"
23
24 #include "Global.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"
32
33 #include <QFileInfo>
34 #include <QMessageBox>
35 #include <QTimer>
36 #include <QFileDialog>
37 #include <QProgressDialog>
38 #include <QMenu>
39
40 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
41
42 ////////////////////////////////////////////////////////////
43 // Constructor & Destructor
44 ////////////////////////////////////////////////////////////
45
46 CueImportDialog::CueImportDialog(QWidget *parent, FileListModel *fileList, const QString &cueFile)
47 :
48         QDialog(parent),
49         m_cueFileName(cueFile),
50         m_fileList(fileList)
51 {
52         //Init the dialog, from the .ui file
53         setupUi(this);
54
55         //Fix size
56         setMinimumSize(this->size());
57         setMaximumHeight(this->height());
58
59         //Create model
60         m_model = new CueSheetModel();
61         connect(m_model, SIGNAL(modelReset()), this, SLOT(modelChanged()));
62         
63         //Setup table view
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);
70
71         //Enable up/down button
72         connect(imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
73         connect(browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
74
75         //Translate
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.")));
77 }
78
79 CueImportDialog::~CueImportDialog(void)
80 {
81         LAMEXP_DELETE(m_model);
82 }
83
84 ////////////////////////////////////////////////////////////
85 // EVENTS
86 ////////////////////////////////////////////////////////////
87
88 void CueImportDialog::showEvent(QShowEvent *event)
89 {
90         QDialog::showEvent(event);
91         modelChanged();
92 }
93
94 ////////////////////////////////////////////////////////////
95 // Slots
96 ////////////////////////////////////////////////////////////
97
98 int CueImportDialog::exec(void)
99 {
100         WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
101         progress->show(tr("Loading Cue Sheet file, please be patient..."));
102
103         QFileInfo cueFileInfo(m_cueFileName);
104         if(!cueFileInfo.exists() || !cueFileInfo.isFile())
105         {
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("-", "&minus;");
107                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
108                 progress->close();
109                 LAMEXP_DELETE(progress);
110                 return CueSheetModel::ErrorIOFailure;
111         }
112
113         m_outputDir = QString("%1/%2").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName());
114         setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
115
116         int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance());
117         if(iResult != CueSheetModel::ErrorSuccess)
118         {
119                 QString errorMsg = tr("An unknown error has occured!");
120                 
121                 switch(iResult)
122                 {
123                 case CueSheetModel::ErrorIOFailure:
124                         errorMsg = tr("The file could not be opened for reading. Make sure you have the required rights!");
125                         break;
126                 case CueSheetModel::ErrorBadFile:
127                         errorMsg = tr("The provided file does not look like a valid Cue Sheet disc image file!");
128                         break;
129                 case CueSheetModel::ErrorUnsupported:
130                         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."));
131                         break;
132                 case CueSheetModel::ErrorInconsistent:
133                         errorMsg = tr("The selected Cue Sheet file contains inconsistent information. Take care!");
134                         break;
135                 }
136                 
137                 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("-", "&minus;");
138                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
139                 progress->close();
140                 LAMEXP_DELETE(progress);
141                 return iResult;
142         }
143         
144         progress->close();
145         LAMEXP_DELETE(progress);
146         return QDialog::exec();
147 }
148
149 void CueImportDialog::modelChanged(void)
150 {
151         treeView->expandAll();
152         editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
153 }
154
155 void CueImportDialog::browseButtonClicked(void)
156 {
157         QString newOutDir = QFileDialog::getExistingDirectory(this, tr("Choose Output Directory"));
158         if(!newOutDir.isEmpty())
159         {
160                 m_outputDir = newOutDir;
161                 modelChanged();
162         }
163 }
164
165 void CueImportDialog::importButtonClicked(void)
166 {
167         static const __int64 oneGigabyte = 1073741824i64; 
168         static const __int64 minimumFreeDiskspaceMultiplier = 2i64;
169         static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
170         
171         QDir outputDir(m_outputDir);
172         outputDir.mkpath(".");
173         if(!(outputDir.exists() && outputDir.isReadable()))
174         {
175                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory could not be created!")));
176                 return;
177         }
178
179         QFile writeTest(QString("%1/~%2.txt").arg(m_outputDir, lamexp_rand_str()));
180         if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
181         {
182                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory is not writable!")));
183                 return;
184         }
185         else
186         {
187                 writeTest.close();
188                 writeTest.remove();
189         }
190
191         qint64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir);
192         if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
193         {
194                 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!")));
195                 return;
196         }
197
198         importCueSheet();
199         accept();
200 }
201
202 void CueImportDialog::analyzedFile(const AudioFileModel &file)
203 {
204         qDebug("Received result: <%s> <%s/%s>", file.filePath().toLatin1().constData(), file.formatContainerType().toLatin1().constData(), file.formatAudioType().toLatin1().constData());
205         m_fileInfo << file;
206 }
207
208 ////////////////////////////////////////////////////////////
209 // Private Functions
210 ////////////////////////////////////////////////////////////
211
212 void CueImportDialog::importCueSheet(void)
213 {
214         QStringList files;
215
216         //Fetch all files that are referenced in the Cue Sheet and lock them
217         int nFiles = m_model->getFileCount();
218         for(int i = 0; i < nFiles; i++)
219         {
220                 QString temp = m_model->getFileName(i);
221                 try
222                 {
223                         m_locks << new LockedFile(temp);
224                 }
225                 catch(char *err)
226                 {
227                         qWarning("Failed to lock file: %s", err);
228                         continue;
229                 }
230                 files << temp;
231         }
232         
233         //Analyze all source files first
234         if(analyzeFiles(files))
235         {
236                 //Now split files according to Cue Sheet
237                 splitFiles();
238         }
239
240
241         //Release locks
242         while(!m_locks.isEmpty())
243         {
244                 delete m_locks.takeFirst();
245         }
246 }
247
248 bool CueImportDialog::analyzeFiles(QStringList &files)
249 {
250         m_fileInfo.clear();
251         bool bSuccess = true;
252
253         WorkingBanner *progress = new WorkingBanner(this);
254         FileAnalyzer *analyzer = new FileAnalyzer(files);
255         
256         connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
257         connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
258
259         progress->show(tr("Analyzing file(s), please wait..."), analyzer);
260         progress->close();
261
262         if(analyzer->filesAccepted() < files.count())
263         {
264                 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)
265                 {
266                         bSuccess = false;
267                 }
268         }
269
270         LAMEXP_DELETE(progress);
271         LAMEXP_DELETE(analyzer);
272
273         return bSuccess;
274 }
275
276 void CueImportDialog::splitFiles(void)
277 {
278         int nTracksSkipped = 0;
279
280         WorkingBanner *progress = new WorkingBanner(this);
281         CueSplitter *splitter  = new CueSplitter(m_outputDir, QFileInfo(m_cueFileName).completeBaseName().replace(".", " ").left(42).trimmed(), m_fileInfo);
282         splitter->setAlbumInfo(m_model->getAlbumPerformer(), m_model->getAlbumTitle());
283
284         connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
285         connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
286
287         int nFiles = m_model->getFileCount();
288         for(int i = 0; i < nFiles; i++)
289         {
290                 QString currentFileName = m_model->getFileName(i);
291                 int nTracks = m_model->getTrackCount(i);
292                 
293                 for(int j = 0; j < nTracks; j++)
294                 {
295                         int trackNo = m_model->getTrackNo(i, j);
296                         double startIndex = std::numeric_limits<double>::quiet_NaN();
297                         double duration = std::numeric_limits<double>::quiet_NaN();
298                         m_model->getTrackIndex(i, j, &startIndex, &duration);
299
300                         AudioFileModel metaInfo(QString().sprintf("cue://File%02d/Track%02d", i, j));
301                         metaInfo.setFileName(m_model->getTrackTitle(i, j));
302                         metaInfo.setFileArtist(m_model->getTrackPerformer(i, j));
303                         metaInfo.setFilePosition(trackNo);
304                         
305                         try
306                         {
307                                 splitter->addTrack(trackNo, currentFileName, startIndex, duration, metaInfo);
308                         }
309                         catch(char *err)
310                         {
311                                 qWarning("Failed to add track #%02d: %s", trackNo, err);
312                                 nTracksSkipped++;
313                         }
314                 }
315         }
316
317         progress->show(tr("Splitting file(s), please wait..."), splitter);
318         progress->close();
319
320         if(!splitter->getSuccess())
321         {
322                 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("An unexpected error has occured while splitting the Cue Sheet!"));
323         }
324         else
325         {
326                 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)));
327                 QMessageBox::information(this, tr("Cue Sheet Completed"), text);
328         }
329
330         LAMEXP_DELETE(splitter);
331         LAMEXP_DELETE(progress);
332 }