OSDN Git Service

efaec198578cf78406afbdf4d59196e4cc5b2dae
[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         connect(loadOtherButton, SIGNAL(clicked()), this, SLOT(loadOtherButtonClicked()));
75
76         //Translate
77         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.")));
78 }
79
80 CueImportDialog::~CueImportDialog(void)
81 {
82         LAMEXP_DELETE(m_model);
83 }
84
85 ////////////////////////////////////////////////////////////
86 // EVENTS
87 ////////////////////////////////////////////////////////////
88
89 void CueImportDialog::showEvent(QShowEvent *event)
90 {
91         QDialog::showEvent(event);
92         modelChanged();
93 }
94
95 ////////////////////////////////////////////////////////////
96 // Slots
97 ////////////////////////////////////////////////////////////
98
99 int CueImportDialog::exec(void)
100 {
101         WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
102         progress->show(tr("Loading Cue Sheet file, please be patient..."));
103
104         QFileInfo cueFileInfo(m_cueFileName);
105         if(!cueFileInfo.exists() || !cueFileInfo.isFile())
106         {
107                 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;");
108                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
109                 progress->close();
110                 LAMEXP_DELETE(progress);
111                 return CueSheetModel::ErrorIOFailure;
112         }
113
114         m_outputDir = QString("%1/%2").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName());
115         for(int n = 2; QDir(m_outputDir).exists(); n++)
116         {
117                 m_outputDir = QString("%1/%2 (%3)").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName(), QString::number(n));
118         }
119
120         setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
121
122         int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance());
123         if(iResult != CueSheetModel::ErrorSuccess)
124         {
125                 QString errorMsg = tr("An unknown error has occured!");
126                 
127                 switch(iResult)
128                 {
129                 case CueSheetModel::ErrorIOFailure:
130                         errorMsg = tr("The file could not be opened for reading. Make sure you have the required rights!");
131                         break;
132                 case CueSheetModel::ErrorBadFile:
133                         errorMsg = tr("The provided file does not look like a valid Cue Sheet disc image file!");
134                         break;
135                 case CueSheetModel::ErrorUnsupported:
136                         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                         break;
138                 case CueSheetModel::ErrorInconsistent:
139                         errorMsg = tr("The selected Cue Sheet file contains inconsistent information. Take care!");
140                         break;
141                 }
142                 
143                 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;");
144                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
145                 progress->close();
146                 LAMEXP_DELETE(progress);
147                 return iResult;
148         }
149         
150         progress->close();
151         LAMEXP_DELETE(progress);
152         return QDialog::exec();
153 }
154
155 void CueImportDialog::modelChanged(void)
156 {
157         treeView->expandAll();
158         editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
159 }
160
161 void CueImportDialog::browseButtonClicked(void)
162 {
163         QString newOutDir, currentDir = m_outputDir;
164         
165         while(QDir(currentDir).exists())
166         {
167                 int pos = max(currentDir.lastIndexOf(QChar('\\')), currentDir.lastIndexOf(QChar('/')));
168                 if(pos > 0) currentDir.left(pos - 1); else break;
169         }
170
171         if(lamexp_themes_enabled() || ((QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) < QSysInfo::WV_XP))
172         {
173                 newOutDir = QFileDialog::getExistingDirectory(this, tr("Choose Output Directory"), currentDir);
174         }
175         else
176         {
177                 QFileDialog dialog(this, tr("Choose Output Directory"));
178                 dialog.setFileMode(QFileDialog::DirectoryOnly);
179                 dialog.setDirectory(currentDir);
180                 if(dialog.exec())
181                 {
182                         newOutDir = dialog.selectedFiles().first();
183                 }
184         }
185
186         if(!newOutDir.isEmpty())
187         {
188                 m_outputDir = newOutDir;
189                 modelChanged();
190         }
191 }
192
193 void CueImportDialog::importButtonClicked(void)
194 {
195         static const __int64 oneGigabyte = 1073741824i64; 
196         static const __int64 minimumFreeDiskspaceMultiplier = 2i64;
197         static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
198         
199         QDir outputDir(m_outputDir);
200         outputDir.mkpath(".");
201         if(!(outputDir.exists() && outputDir.isReadable()))
202         {
203                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory could not be created!")));
204                 return;
205         }
206
207         QFile writeTest(QString("%1/~%2.txt").arg(m_outputDir, lamexp_rand_str()));
208         if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
209         {
210                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory is not writable!")));
211                 return;
212         }
213         else
214         {
215                 writeTest.close();
216                 writeTest.remove();
217         }
218
219         qint64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir);
220         if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
221         {
222                 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!")));
223                 return;
224         }
225
226         importCueSheet();
227         accept();
228 }
229
230 void CueImportDialog::loadOtherButtonClicked(void)
231 {
232         done(-1);
233 }
234
235 void CueImportDialog::analyzedFile(const AudioFileModel &file)
236 {
237         qDebug("Received result: <%s> <%s/%s>", file.filePath().toLatin1().constData(), file.formatContainerType().toLatin1().constData(), file.formatAudioType().toLatin1().constData());
238         m_fileInfo << file;
239 }
240
241 ////////////////////////////////////////////////////////////
242 // Private Functions
243 ////////////////////////////////////////////////////////////
244
245 void CueImportDialog::importCueSheet(void)
246 {
247         QStringList files;
248
249         //Fetch all files that are referenced in the Cue Sheet and lock them
250         int nFiles = m_model->getFileCount();
251         for(int i = 0; i < nFiles; i++)
252         {
253                 QString temp = m_model->getFileName(i);
254                 try
255                 {
256                         m_locks << new LockedFile(temp);
257                 }
258                 catch(char *err)
259                 {
260                         qWarning("Failed to lock file: %s", err);
261                         continue;
262                 }
263                 files << temp;
264         }
265         
266         //Analyze all source files first
267         if(analyzeFiles(files))
268         {
269                 //Now split files according to Cue Sheet
270                 splitFiles();
271         }
272         
273         //Release locks
274         while(!m_locks.isEmpty())
275         {
276                 delete m_locks.takeFirst();
277         }
278 }
279
280 bool CueImportDialog::analyzeFiles(QStringList &files)
281 {
282         m_fileInfo.clear();
283         bool bSuccess = true;
284
285         WorkingBanner *progress = new WorkingBanner(this);
286         FileAnalyzer *analyzer = new FileAnalyzer(files);
287         
288         connect(analyzer, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
289         connect(analyzer, SIGNAL(fileAnalyzed(AudioFileModel)), this, SLOT(analyzedFile(AudioFileModel)), Qt::QueuedConnection);
290         connect(progress, SIGNAL(userAbort()), analyzer, SLOT(abortProcess()), Qt::DirectConnection);
291
292         progress->show(tr("Analyzing file(s), please wait..."), analyzer);
293         progress->close();
294
295         if(analyzer->filesAccepted() < static_cast<unsigned int>(files.count()))
296         {
297                 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)
298                 {
299                         bSuccess = false;
300                 }
301         }
302
303         LAMEXP_DELETE(progress);
304         LAMEXP_DELETE(analyzer);
305
306         return bSuccess;
307 }
308
309 void CueImportDialog::splitFiles(void)
310 {
311         QString baseName = QFileInfo(m_cueFileName).completeBaseName().replace(".", " ").simplified();
312
313         WorkingBanner *progress = new WorkingBanner(this);
314         CueSplitter *splitter  = new CueSplitter(m_outputDir, baseName, m_model, m_fileInfo);
315
316         connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
317         connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
318         connect(progress, SIGNAL(userAbort()), splitter, SLOT(abortProcess()), Qt::DirectConnection);
319
320         progress->show(tr("Splitting file(s), please wait..."), splitter);
321         progress->close();
322
323         if(splitter->getAborted())      
324         {
325                 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("Process was aborted by the user after %1 track(s)!").arg(QString::number(splitter->getTracksSuccess())));
326         }
327         else if(!splitter->getSuccess())
328         {
329                 QMessageBox::warning(this, tr("Cue Sheet Error"), tr("An unexpected error has occured while splitting the Cue Sheet!"));
330         }
331         else
332         {
333                 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*/)));
334                 QMessageBox::information(this, tr("Cue Sheet Completed"), text);
335         }
336
337         LAMEXP_DELETE(splitter);
338         LAMEXP_DELETE(progress);
339 }