OSDN Git Service

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