OSDN Git Service

Moved some more OS-specific functions into the MUtilities library.
[lamexp/LameXP.git] / src / Dialog_CueImport.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Dialog_CueImport.h"
24
25 //UIC includes
26 #include "UIC_CueSheetImport.h"
27
28 //LameXP includes
29 #include "Global.h"
30 #include "Model_CueSheet.h"
31 #include "Model_AudioFile.h"
32 #include "Model_FileList.h"
33 #include "Dialog_WorkingBanner.h"
34 #include "Thread_FileAnalyzer.h"
35 #include "Thread_CueSplitter.h"
36 #include "Registry_Decoder.h"
37 #include "LockedFile.h"
38
39 //MUtils
40 #include <MUtils/Global.h>
41 #include <MUtils/OSSupport.h>
42
43 //Qt includes
44 #include <QFileInfo>
45 #include <QMessageBox>
46 #include <QTimer>
47 #include <QFileDialog>
48 #include <QProgressDialog>
49 #include <QMenu>
50 #include <QTextCodec>
51 #include <QInputDialog>
52
53 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
54 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
55
56 ////////////////////////////////////////////////////////////
57 // Constructor & Destructor
58 ////////////////////////////////////////////////////////////
59
60 CueImportDialog::CueImportDialog(QWidget *parent, FileListModel *fileList, const QString &cueFile, const SettingsModel *settings)
61 :
62         QDialog(parent),
63         ui(new Ui::CueSheetImport),
64         m_fileList(fileList),
65         m_cueFileName(cueFile),
66         m_settings(settings)
67 {
68         //Init the dialog, from the .ui file
69         ui->setupUi(this);
70
71         //Fix size
72         setMinimumSize(this->size());
73         setMaximumHeight(this->height());
74
75         //Create model
76         m_model = new CueSheetModel();
77         connect(m_model, SIGNAL(modelReset()), this, SLOT(modelChanged()));
78         
79         //Setup table view
80         ui->treeView->setModel(m_model);
81         ui->treeView->header()->setStretchLastSection(false);
82         ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
83         ui->treeView->header()->setResizeMode(1, QHeaderView::Stretch);
84         ui->treeView->header()->setMovable(false);
85         ui->treeView->setItemsExpandable(false);
86
87         //Enable up/down button
88         connect(ui->imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
89         connect(ui->browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
90         connect(ui->loadOtherButton, SIGNAL(clicked()), this, SLOT(loadOtherButtonClicked()));
91
92         //Translate
93         ui->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.")));
94 }
95
96 CueImportDialog::~CueImportDialog(void)
97 {
98         MUTILS_DELETE(m_model);
99         MUTILS_DELETE(ui);
100 }
101
102 ////////////////////////////////////////////////////////////
103 // EVENTS
104 ////////////////////////////////////////////////////////////
105
106 void CueImportDialog::showEvent(QShowEvent *event)
107 {
108         QDialog::showEvent(event);
109         modelChanged();
110 }
111
112 ////////////////////////////////////////////////////////////
113 // Slots
114 ////////////////////////////////////////////////////////////
115
116 int CueImportDialog::exec(void)
117 {
118         WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
119         progress->show(tr("Loading Cue Sheet file, please be patient..."));
120
121         QFileInfo cueFileInfo(m_cueFileName);
122         if(!cueFileInfo.exists() || !cueFileInfo.isFile())
123         {
124                 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;");
125                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
126                 progress->close();
127                 MUTILS_DELETE(progress);
128                 return CueSheetModel::ErrorIOFailure;
129         }
130
131         //----------------------//
132
133         QTextCodec *codec = NULL;
134
135         QFile cueFile(cueFileInfo.canonicalFilePath());
136         cueFile.open(QIODevice::ReadOnly);
137         QByteArray bomCheck = cueFile.isOpen() ? cueFile.peek(16) : QByteArray();
138
139         if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf"))
140         {
141                 codec = QTextCodec::codecForName("UTF-8");
142         }
143         else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe"))
144         {
145                 codec = QTextCodec::codecForName("UTF-16LE");
146         }
147         else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff"))
148         {
149                 codec = QTextCodec::codecForName("UTF-16BE");
150         }
151         else
152         {
153                 const QString systemDefault = tr("(System Default)");
154
155                 QStringList codecList;
156                 codecList.append(systemDefault);
157                 codecList.append(lamexp_available_codepages());
158
159                 QInputDialog *input = new QInputDialog(progress);
160                 input->setLabelText(EXPAND(tr("Select ANSI Codepage for Cue Sheet file:")));
161                 input->setOkButtonText(tr("OK"));
162                 input->setCancelButtonText(tr("Cancel"));
163                 input->setTextEchoMode(QLineEdit::Normal);
164                 input->setComboBoxItems(codecList);
165         
166                 if(input->exec() < 1)
167                 {
168                         progress->close();
169                         MUTILS_DELETE(input);
170                         MUTILS_DELETE(progress);
171                         return Rejected;
172                 }
173         
174                 if(input->textValue().compare(systemDefault, Qt::CaseInsensitive))
175                 {
176                         qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData());
177                         codec = QTextCodec::codecForName(input->textValue().toLatin1().constData());
178                 }
179                 else
180                 {
181                         qDebug("Going to use the system's default codec!");
182                         codec = QTextCodec::codecForName("System");
183                 }
184
185                 MUTILS_DELETE(input);
186         }
187
188         bomCheck.clear();
189
190         //----------------------//
191
192         QString baseName = cueFileInfo.completeBaseName().simplified();
193         while(baseName.endsWith(".") || baseName.endsWith(" ")) baseName.chop(1);
194         if(baseName.isEmpty()) baseName = tr("New Folder");
195
196         m_outputDir = QString("%1/%2").arg(cueFileInfo.canonicalPath(), baseName);
197         for(int n = 2; QDir(m_outputDir).exists() || QFileInfo(m_outputDir).exists(); n++)
198         {
199                 m_outputDir = QString("%1/%2 (%3)").arg(cueFileInfo.canonicalPath(), baseName, QString::number(n));
200         }
201
202         setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
203
204         int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance(), codec);
205         if(iResult != CueSheetModel::ErrorSuccess)
206         {
207                 QString errorMsg = tr("An unknown error has occured!");
208                 
209                 switch(iResult)
210                 {
211                 case CueSheetModel::ErrorIOFailure:
212                         errorMsg = tr("The file could not be opened for reading. Make sure you have the required rights!");
213                         break;
214                 case CueSheetModel::ErrorBadFile:
215                         errorMsg = tr("The provided file does not look like a valid Cue Sheet disc image file!");
216                         break;
217                 case CueSheetModel::ErrorUnsupported:
218                         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."));
219                         break;
220                 case CueSheetModel::ErrorInconsistent:
221                         errorMsg = tr("The selected Cue Sheet file contains inconsistent information. Take care!");
222                         break;
223                 }
224                 
225                 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;");
226                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
227                 progress->close();
228                 MUTILS_DELETE(progress);
229                 return iResult;
230         }
231         
232         progress->close();
233         MUTILS_DELETE(progress);
234         return QDialog::exec();
235 }
236
237 void CueImportDialog::modelChanged(void)
238 {
239         ui->treeView->expandAll();
240         ui->editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
241         if(const AudioFileModel_MetaInfo *albumInfo = m_model->getAlbumInfo())
242         {
243                 ui->labelArtist->setText(albumInfo->artist().isEmpty() ? tr("Unknown Artist") : albumInfo->artist());
244                 ui->labelAlbum->setText(albumInfo->album().isEmpty() ? tr("Unknown Album") : albumInfo->album());
245         }
246 }
247
248 void CueImportDialog::browseButtonClicked(void)
249 {
250         QString newOutDir, currentDir = m_outputDir;
251         
252         while(QDir(currentDir).exists())
253         {
254                 int pos = qMax(currentDir.lastIndexOf(QChar('\\')), currentDir.lastIndexOf(QChar('/')));
255                 if(pos > 0) currentDir.left(pos - 1); else break;
256         }
257
258         if(lamexp_themes_enabled())
259         {
260                 newOutDir = QFileDialog::getExistingDirectory(this, tr("Choose Output Directory"), currentDir);
261         }
262         else
263         {
264                 QFileDialog dialog(this, tr("Choose Output Directory"));
265                 dialog.setFileMode(QFileDialog::DirectoryOnly);
266                 dialog.setDirectory(currentDir);
267                 if(dialog.exec())
268                 {
269                         newOutDir = dialog.selectedFiles().first();
270                 }
271         }
272
273         if(!newOutDir.isEmpty())
274         {
275                 m_outputDir = newOutDir;
276                 modelChanged();
277         }
278 }
279
280 void CueImportDialog::importButtonClicked(void)
281 {
282         static const unsigned __int64 oneGigabyte = 1073741824ui64; 
283         static const unsigned __int64 minimumFreeDiskspaceMultiplier = 2ui64;
284         static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
285         
286         QDir outputDir(m_outputDir);
287         outputDir.mkpath(".");
288         if(!(outputDir.exists() && outputDir.isReadable()))
289         {
290                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory could not be created!")));
291                 return;
292         }
293
294         QFile writeTest(QString("%1/~%2.txt").arg(m_outputDir, MUtils::rand_str()));
295         if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
296         {
297                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory is not writable!")));
298                 return;
299         }
300         else
301         {
302                 writeTest.close();
303                 writeTest.remove();
304         }
305
306         quint64 currentFreeDiskspace = 0;
307         if(MUtils::OS::free_diskspace(m_outputDir, currentFreeDiskspace))
308         {
309                 if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
310                 {
311                         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!")));
312                         return;
313                 }
314         }
315
316         importCueSheet();
317         accept();
318 }
319
320 void CueImportDialog::loadOtherButtonClicked(void)
321 {
322         done(-1);
323 }
324
325 void CueImportDialog::analyzedFile(const AudioFileModel &file)
326 {
327         qDebug("Received result: <%s> <%s/%s>", file.filePath().toLatin1().constData(), file.techInfo().containerType().toLatin1().constData(), file.techInfo().audioType().toLatin1().constData());
328         m_fileInfo << file;
329 }
330
331 ////////////////////////////////////////////////////////////
332 // Private Functions
333 ////////////////////////////////////////////////////////////
334
335 void CueImportDialog::importCueSheet(void)
336 {
337         QStringList files;
338         QList<LockedFile*> locks;
339
340         //Fetch all files that are referenced in the Cue Sheet and lock them
341         int nFiles = m_model->getFileCount();
342         for(int i = 0; i < nFiles; i++)
343         {
344                 try
345                 {
346                         LockedFile *temp = new LockedFile(m_model->getFileName(i));
347                         locks << temp;
348                         files << temp->filePath();
349                 }
350                 catch(const std::exception &error)
351                 {
352                         qWarning("Failed to lock file:\n%s\n", error.what());
353                 }
354                 catch(...)
355                 {
356                         qWarning("Failed to lock file!");
357                 }
358         }
359
360         //Check if all files could be locked
361         if(files.count() < m_model->getFileCount())
362         {
363                 if(QMessageBox::warning(this, tr("Cue Sheet Error"), tr("Warning: Some of the required input files could not be found!"), tr("Continue Anyway"), tr("Abort")) == 1)
364                 {
365                         files.clear();
366                 }
367         }
368
369         //Process all avialble input files
370         if(files.count() > 0)
371         {
372                 //Analyze all source files first
373                 if(analyzeFiles(files))
374                 {
375                         //Now split files according to Cue Sheet
376                         splitFiles();
377                 }
378         }
379         
380         //Release locks
381         while(!locks.isEmpty())
382         {
383                 delete locks.takeFirst();
384         }
385 }
386
387 bool CueImportDialog::analyzeFiles(QStringList &files)
388 {
389         m_fileInfo.clear();
390         bool bSuccess = true;
391
392         WorkingBanner *progress = new WorkingBanner(this);
393         FileAnalyzer *analyzer = new FileAnalyzer(files);
394         
395         connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
396         connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
397         connect(progress, SIGNAL(userAbort()), analyzer, SLOT(abortProcess()), Qt::DirectConnection);
398
399         progress->show(tr("Analyzing file(s), please wait..."), analyzer);
400         progress->close();
401
402         if(analyzer->filesAccepted() < static_cast<unsigned int>(files.count()))
403         {
404                 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)
405                 {
406                         bSuccess = false;
407                 }
408         }
409
410         MUTILS_DELETE(progress);
411         MUTILS_DELETE(analyzer);
412
413         return bSuccess;
414 }
415
416 void CueImportDialog::splitFiles(void)
417 {
418         QString baseName = QFileInfo(m_cueFileName).completeBaseName().replace(".", " ").simplified();
419
420         WorkingBanner *progress = new WorkingBanner(this);
421         CueSplitter *splitter  = new CueSplitter(m_outputDir, baseName, m_model, m_fileInfo);
422
423         connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
424         connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
425         connect(splitter, SIGNAL(progressValChanged(unsigned int)), progress, SLOT(setProgressVal(unsigned int)), Qt::QueuedConnection);
426         connect(splitter, SIGNAL(progressMaxChanged(unsigned int)), progress, SLOT(setProgressMax(unsigned int)), Qt::QueuedConnection);
427         connect(progress, SIGNAL(userAbort()), splitter, SLOT(abortProcess()), Qt::DirectConnection);
428
429         DecoderRegistry::configureDecoders(m_settings);
430
431         progress->show(tr("Splitting file(s), please wait..."), splitter);
432         progress->close();
433
434         if(splitter->getAborted())
435         {
436                 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("Process was aborted by the user after %n track(s)!", "", splitter->getTracksSuccess()));
437         }
438         else if(!splitter->getSuccess())
439         {
440                 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("An unexpected error has occured while splitting the Cue Sheet!"));
441         }
442         else
443         {
444                 QString text = QString("<nobr>%1 %2</nobr>").arg(tr("Imported %n track(s) from the Cue Sheet.", "", splitter->getTracksSuccess()), tr("Skipped %n track(s).", "", splitter->getTracksSkipped()));
445                 QMessageBox::information(this, tr("Cue Sheet Completed"), text);
446         }
447
448         MUTILS_DELETE(splitter);
449         MUTILS_DELETE(progress);
450 }