OSDN Git Service

First step for Cue Sheet splitting: Call the FileAnalyzer thread in order to analyze...
[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 "Dialog_WorkingBanner.h"
28 #include "Thread_FileAnalyzer.h"
29
30 #include <QFileInfo>
31 #include <QMessageBox>
32 #include <QTimer>
33 #include <QFileDialog>
34 #include <QProgressDialog>
35 #include <QMenu>
36
37 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
38
39 ////////////////////////////////////////////////////////////
40 // Constructor & Destructor
41 ////////////////////////////////////////////////////////////
42
43 CueImportDialog::CueImportDialog(QWidget *parent)
44 :
45         QDialog(parent)
46 {
47         //Init the dialog, from the .ui file
48         setupUi(this);
49
50         //Fix size
51         setMinimumSize(this->size());
52         setMaximumHeight(this->height());
53
54         //Create model
55         m_model = new CueSheetModel();
56         connect(m_model, SIGNAL(modelReset()), this, SLOT(modelChanged()));
57         
58         //Setup table view
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);
65
66         //Enable up/down button
67         connect(imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
68         connect(browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
69
70         //Translate
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.")));
72 }
73
74 CueImportDialog::~CueImportDialog(void)
75 {
76         LAMEXP_DELETE(m_model);
77 }
78
79 ////////////////////////////////////////////////////////////
80 // EVENTS
81 ////////////////////////////////////////////////////////////
82
83 void CueImportDialog::showEvent(QShowEvent *event)
84 {
85         QDialog::showEvent(event);
86         modelChanged();
87 }
88
89 ////////////////////////////////////////////////////////////
90 // Slots
91 ////////////////////////////////////////////////////////////
92
93 int CueImportDialog::exec(const QString &cueFile)
94 {
95         WorkingBanner *progress = new WorkingBanner(dynamic_cast<QWidget*>(parent()));
96         progress->show(tr("Loading Cue Sheet file, please be patient..."));
97         
98         QFileInfo cueFileInfo(cueFile);
99         m_outputDir = QFileInfo(cueFile).canonicalPath();
100
101         setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
102
103         if(!cueFileInfo.exists() || !cueFileInfo.isFile() || m_outputDir.isEmpty())
104         {
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("-", "&minus;");
106                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
107                 progress->close();
108                 LAMEXP_DELETE(progress);
109                 return CueSheetModel::ErrorIOFailure;
110         }
111
112         int iResult = m_model->loadCueSheet(cueFile, QApplication::instance());
113         if(iResult != CueSheetModel::ErrorSuccess)
114         {
115                 QString errorMsg = tr("An unknown error has occured!");
116                 
117                 switch(iResult)
118                 {
119                 case CueSheetModel::ErrorIOFailure:
120                         errorMsg = tr("The file could not be opened for reading. Make sure you have the required rights!");
121                         break;
122                 case CueSheetModel::ErrorBadFile:
123                         errorMsg = tr("The provided file does not look like a valid Cue Sheet disc image file!");
124                         break;
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."));
127                         break;
128                 case CueSheetModel::ErrorInconsistent:
129                         errorMsg = tr("The selected Cue Sheet file contains inconsistent information. Take care!");
130                         break;
131                 }
132                 
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("-", "&minus;");
134                 QMessageBox::warning(progress, tr("Cue Sheet Error"), text);
135                 progress->close();
136                 LAMEXP_DELETE(progress);
137                 return iResult;
138         }
139         
140         progress->close();
141         LAMEXP_DELETE(progress);
142         return QDialog::exec();
143 }
144
145 void CueImportDialog::modelChanged(void)
146 {
147         treeView->expandAll();
148         editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
149 }
150
151 void CueImportDialog::browseButtonClicked(void)
152 {
153         QString newOutDir = QFileDialog::getExistingDirectory(this, tr("Choose Output Directory"));
154         if(!newOutDir.isEmpty())
155         {
156                 m_outputDir = newOutDir;
157                 modelChanged();
158         }
159 }
160
161 void CueImportDialog::importButtonClicked(void)
162 {
163         static const __int64 oneGigabyte = 1073741824i64; 
164         static const __int64 minimumFreeDiskspaceMultiplier = 2i64;
165         static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
166         
167         QFile writeTest(QString("%1/~%2.txt").arg(m_outputDir, lamexp_rand_str()));
168         if(!(writeTest.open(QIODevice::ReadWrite) && (writeTest.write(writeTestBuffer) == strlen(writeTestBuffer))))
169         {
170                 QMessageBox::warning(this, tr("LameXP"), QString("<nobr>%2</nobr>").arg(tr("Error: The selected output directory is not writable!")));
171                 return;
172         }
173         else
174         {
175                 writeTest.close();
176                 writeTest.remove();
177         }
178
179         qint64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir);
180         if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
181         {
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!")));
183                 return;
184         }
185
186         importCueSheet();
187 }
188
189 void CueImportDialog::analyzedFile(const AudioFileModel &file)
190 {
191         qWarning("Received results for: %s", file.filePath().toLatin1().constData());
192         m_fileInfo.insert(file.filePath(), file);
193 }
194
195 ////////////////////////////////////////////////////////////
196 // Private FUnctions
197 ////////////////////////////////////////////////////////////
198
199 void CueImportDialog::importCueSheet(void)
200 {
201         QStringList files;
202         int nFiles = m_model->getFileCount();
203
204         //Fetch all files that are referenced in the Cue Sheet
205         for(int i = 0; i < nFiles; i++)
206         {
207                 files << m_model->getFileName(i);
208         }
209         
210         //Analyze all source files
211         analyzeFiles(files);
212 }
213
214 void CueImportDialog::analyzeFiles(QStringList &files)
215 {
216         m_fileInfo.clear();
217
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);
222
223         progress->show(tr("Adding file(s), please wait..."), analyzer);
224         progress->close();
225         LAMEXP_DELETE(progress);
226 }