OSDN Git Service

Properly handle plurals in translation files.
[lamexp/LameXP.git] / src / Dialog_CueImport.cpp
index efaec19..a664b9d 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This program is free software; you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 
 #include "Dialog_CueImport.h"
 
+//UIC includes
+#include "../tmp/UIC_CueSheetImport.h"
+
+//LameXP includes
 #include "Global.h"
 #include "Model_CueSheet.h"
 #include "Model_AudioFile.h"
 #include "Dialog_WorkingBanner.h"
 #include "Thread_FileAnalyzer.h"
 #include "Thread_CueSplitter.h"
+#include "Registry_Decoder.h"
 #include "LockedFile.h"
 
+//Qt includes
 #include <QFileInfo>
 #include <QMessageBox>
 #include <QTimer>
 #include <QFileDialog>
 #include <QProgressDialog>
 #include <QMenu>
+#include <QTextCodec>
+#include <QInputDialog>
 
 #define SET_FONT_BOLD(WIDGET,BOLD) { QFont _font = WIDGET->font(); _font.setBold(BOLD); WIDGET->setFont(_font); }
+#define EXPAND(STR) QString(STR).leftJustified(96, ' ')
 
 ////////////////////////////////////////////////////////////
 // Constructor & Destructor
 ////////////////////////////////////////////////////////////
 
-CueImportDialog::CueImportDialog(QWidget *parent, FileListModel *fileList, const QString &cueFile)
+CueImportDialog::CueImportDialog(QWidget *parent, FileListModel *fileList, const QString &cueFile, const SettingsModel *settings)
 :
        QDialog(parent),
+       ui(new Ui::CueSheetImport),
+       m_fileList(fileList),
        m_cueFileName(cueFile),
-       m_fileList(fileList)
+       m_settings(settings)
 {
        //Init the dialog, from the .ui file
-       setupUi(this);
+       ui->setupUi(this);
 
        //Fix size
        setMinimumSize(this->size());
@@ -61,25 +72,26 @@ CueImportDialog::CueImportDialog(QWidget *parent, FileListModel *fileList, const
        connect(m_model, SIGNAL(modelReset()), this, SLOT(modelChanged()));
        
        //Setup table view
-       treeView->setModel(m_model);
-       treeView->header()->setStretchLastSection(false);
-       treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
-       treeView->header()->setResizeMode(1, QHeaderView::Stretch);
-       treeView->header()->setMovable(false);
-       treeView->setItemsExpandable(false);
+       ui->treeView->setModel(m_model);
+       ui->treeView->header()->setStretchLastSection(false);
+       ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
+       ui->treeView->header()->setResizeMode(1, QHeaderView::Stretch);
+       ui->treeView->header()->setMovable(false);
+       ui->treeView->setItemsExpandable(false);
 
        //Enable up/down button
-       connect(imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
-       connect(browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
-       connect(loadOtherButton, SIGNAL(clicked()), this, SLOT(loadOtherButtonClicked()));
+       connect(ui->imprtButton, SIGNAL(clicked()), this, SLOT(importButtonClicked()));
+       connect(ui->browseButton, SIGNAL(clicked()), this, SLOT(browseButtonClicked()));
+       connect(ui->loadOtherButton, SIGNAL(clicked()), this, SLOT(loadOtherButtonClicked()));
 
        //Translate
-       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.")));
+       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.")));
 }
 
 CueImportDialog::~CueImportDialog(void)
 {
        LAMEXP_DELETE(m_model);
+       LAMEXP_DELETE(ui);
 }
 
 ////////////////////////////////////////////////////////////
@@ -111,15 +123,80 @@ int CueImportDialog::exec(void)
                return CueSheetModel::ErrorIOFailure;
        }
 
-       m_outputDir = QString("%1/%2").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName());
-       for(int n = 2; QDir(m_outputDir).exists(); n++)
+       //----------------------//
+
+       QTextCodec *codec = NULL;
+
+       QFile cueFile(cueFileInfo.canonicalFilePath());
+       cueFile.open(QIODevice::ReadOnly);
+       QByteArray bomCheck = cueFile.isOpen() ? cueFile.peek(16) : QByteArray();
+
+       if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf"))
+       {
+               codec = QTextCodec::codecForName("UTF-8");
+       }
+       else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe"))
+       {
+               codec = QTextCodec::codecForName("UTF-16LE");
+       }
+       else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff"))
+       {
+               codec = QTextCodec::codecForName("UTF-16BE");
+       }
+       else
+       {
+               const QString systemDefault = tr("(System Default)");
+
+               QStringList codecList;
+               codecList.append(systemDefault);
+               codecList.append(lamexp_available_codepages());
+
+               QInputDialog *input = new QInputDialog(progress);
+               input->setLabelText(EXPAND(tr("Select ANSI Codepage for Cue Sheet file:")));
+               input->setOkButtonText(tr("OK"));
+               input->setCancelButtonText(tr("Cancel"));
+               input->setTextEchoMode(QLineEdit::Normal);
+               input->setComboBoxItems(codecList);
+       
+               if(input->exec() < 1)
+               {
+                       progress->close();
+                       LAMEXP_DELETE(input);
+                       LAMEXP_DELETE(progress);
+                       return Rejected;
+               }
+       
+               if(input->textValue().compare(systemDefault, Qt::CaseInsensitive))
+               {
+                       qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData());
+                       codec = QTextCodec::codecForName(input->textValue().toLatin1().constData());
+               }
+               else
+               {
+                       qDebug("Going to use the system's default codec!");
+                       codec = QTextCodec::codecForName("System");
+               }
+
+               LAMEXP_DELETE(input);
+       }
+
+       bomCheck.clear();
+
+       //----------------------//
+
+       QString baseName = cueFileInfo.completeBaseName().simplified();
+       while(baseName.endsWith(".") || baseName.endsWith(" ")) baseName.chop(1);
+       if(baseName.isEmpty()) baseName = tr("New Folder");
+
+       m_outputDir = QString("%1/%2").arg(cueFileInfo.canonicalPath(), baseName);
+       for(int n = 2; QDir(m_outputDir).exists() || QFileInfo(m_outputDir).exists(); n++)
        {
-               m_outputDir = QString("%1/%2 (%3)").arg(cueFileInfo.canonicalPath(), cueFileInfo.completeBaseName(), QString::number(n));
+               m_outputDir = QString("%1/%2 (%3)").arg(cueFileInfo.canonicalPath(), baseName, QString::number(n));
        }
 
        setWindowTitle(QString("%1: %2").arg(windowTitle().split(":", QString::SkipEmptyParts).first().trimmed(), cueFileInfo.fileName()));
 
-       int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance());
+       int iResult = m_model->loadCueSheet(m_cueFileName, QApplication::instance(), codec);
        if(iResult != CueSheetModel::ErrorSuccess)
        {
                QString errorMsg = tr("An unknown error has occured!");
@@ -154,8 +231,10 @@ int CueImportDialog::exec(void)
 
 void CueImportDialog::modelChanged(void)
 {
-       treeView->expandAll();
-       editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
+       ui->treeView->expandAll();
+       ui->editOutputDir->setText(QDir::toNativeSeparators(m_outputDir));
+       ui->labelArtist->setText(m_model->getAlbumPerformer().isEmpty() ? tr("Unknown Artist") : m_model->getAlbumPerformer());
+       ui->labelAlbum->setText(m_model->getAlbumTitle().isEmpty() ? tr("Unknown Album") : m_model->getAlbumTitle());
 }
 
 void CueImportDialog::browseButtonClicked(void)
@@ -164,7 +243,7 @@ void CueImportDialog::browseButtonClicked(void)
        
        while(QDir(currentDir).exists())
        {
-               int pos = max(currentDir.lastIndexOf(QChar('\\')), currentDir.lastIndexOf(QChar('/')));
+               int pos = qMax(currentDir.lastIndexOf(QChar('\\')), currentDir.lastIndexOf(QChar('/')));
                if(pos > 0) currentDir.left(pos - 1); else break;
        }
 
@@ -192,8 +271,8 @@ void CueImportDialog::browseButtonClicked(void)
 
 void CueImportDialog::importButtonClicked(void)
 {
-       static const __int64 oneGigabyte = 1073741824i64; 
-       static const __int64 minimumFreeDiskspaceMultiplier = 2i64;
+       static const unsigned __int64 oneGigabyte = 1073741824ui64; 
+       static const unsigned __int64 minimumFreeDiskspaceMultiplier = 2ui64;
        static const char *writeTestBuffer = "LAMEXP_WRITE_TEST";
        
        QDir outputDir(m_outputDir);
@@ -216,8 +295,10 @@ void CueImportDialog::importButtonClicked(void)
                writeTest.remove();
        }
 
-       qint64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir);
-       if(currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier))
+       bool ok = false;
+       unsigned __int64 currentFreeDiskspace = lamexp_free_diskspace(m_outputDir, &ok);
+
+       if(ok && (currentFreeDiskspace < (oneGigabyte * minimumFreeDiskspaceMultiplier)))
        {
                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!")));
                return;
@@ -315,14 +396,18 @@ void CueImportDialog::splitFiles(void)
 
        connect(splitter, SIGNAL(fileSelected(QString)), progress, SLOT(setText(QString)), Qt::QueuedConnection);
        connect(splitter, SIGNAL(fileSplit(AudioFileModel)), m_fileList, SLOT(addFile(AudioFileModel)), Qt::QueuedConnection);
+       connect(splitter, SIGNAL(progressValChanged(unsigned int)), progress, SLOT(setProgressVal(unsigned int)), Qt::QueuedConnection);
+       connect(splitter, SIGNAL(progressMaxChanged(unsigned int)), progress, SLOT(setProgressMax(unsigned int)), Qt::QueuedConnection);
        connect(progress, SIGNAL(userAbort()), splitter, SLOT(abortProcess()), Qt::DirectConnection);
 
+       DecoderRegistry::configureDecoders(m_settings);
+
        progress->show(tr("Splitting file(s), please wait..."), splitter);
        progress->close();
 
-       if(splitter->getAborted())      
+       if(splitter->getAborted())
        {
-               QMessageBox::warning(this, tr("Cue Sheet Error"), tr("Process was aborted by the user after %1 track(s)!").arg(QString::number(splitter->getTracksSuccess())));
+               QMessageBox::warning(this, tr("Cue Sheet Error"), tr("Process was aborted by the user after %n track(s)!", "", splitter->getTracksSuccess()));
        }
        else if(!splitter->getSuccess())
        {
@@ -330,7 +415,7 @@ void CueImportDialog::splitFiles(void)
        }
        else
        {
-               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*/)));
+               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()));
                QMessageBox::information(this, tr("Cue Sheet Completed"), text);
        }