OSDN Git Service

More code refactoring and clean-up.
[lamexp/LameXP.git] / src / Model_FileList.cpp
index c6c2eb4..4ad59b3 100644 (file)
@@ -1,11 +1,12 @@
 ///////////////////////////////////////////////////////////////////////////////
 // LameXP - Audio Encoder Front-End
-// Copyright (C) 2004-2012 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2014 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
 // the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// (at your option) any later version, but always including the *additional*
+// restrictions defined in the "License.txt" file.
 //
 // This program is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include "Model_FileList.h"
 
+//Internal
 #include "Global.h"
 
+//MUtils
+#include <MUtils/Global.h>
+
+//Qt
 #include <QFileInfo>
 #include <QDir>
 #include <QFile>
@@ -32,6 +38,7 @@
 
 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
 #define CHECK_HDR(STR,NAM) (!(STR).compare((NAM), Qt::CaseInsensitive))
+#define MAKE_KEY(PATH) (QDir::fromNativeSeparators(PATH).toLower())
 
 ////////////////////////////////////////////////////////////
 // Constructor & Destructor
@@ -39,6 +46,7 @@
 
 FileListModel::FileListModel(void)
 :
+       m_blockUpdates(false),
        m_fileIcon(":/icons/page_white_cd.png")
 {
 }
@@ -68,10 +76,10 @@ QVariant FileListModel::data(const QModelIndex &index, int role) const
                switch(index.column())
                {
                case 0:
-                       return m_fileList.at(index.row()).fileName();
+                       return m_fileStore.value(m_fileList.at(index.row())).metaInfo().title();
                        break;
                case 1:
-                       return QDir::toNativeSeparators(m_fileList.at(index.row()).filePath());
+                       return QDir::toNativeSeparators(m_fileStore.value(m_fileList.at(index.row())).filePath());
                        break;
                default:
                        return QVariant();
@@ -136,33 +144,34 @@ QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int
 void FileListModel::addFile(const QString &filePath)
 {
        QFileInfo fileInfo(filePath);
+       const QString key = MAKE_KEY(fileInfo.canonicalFilePath()); 
+       const bool flag = (!m_blockUpdates);
 
-       for(int i = 0; i < m_fileList.count(); i++)
+       if(!m_fileStore.contains(key))
        {
-               if(m_fileList.at(i).filePath().compare(fileInfo.canonicalFilePath(), Qt::CaseInsensitive) == 0)
-               {
-                       return;
-               }
+               AudioFileModel audioFile(fileInfo.canonicalFilePath());
+               audioFile.metaInfo().setTitle(fileInfo.baseName());
+               if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
+               m_fileStore.insert(key, audioFile);
+               m_fileList.append(key);
+               if(flag) endInsertRows();
+               emit rowAppended();
        }
-       
-       beginResetModel();
-       m_fileList.append(AudioFileModel(fileInfo.canonicalFilePath(), fileInfo.baseName()));
-       endResetModel();
 }
 
 void FileListModel::addFile(const AudioFileModel &file)
 {
-       for(int i = 0; i < m_fileList.count(); i++)
+       const QString key = MAKE_KEY(file.filePath()); 
+       const bool flag = (!m_blockUpdates);
+
+       if(!m_fileStore.contains(key))
        {
-               if(m_fileList.at(i).filePath().compare(file.filePath(), Qt::CaseInsensitive) == 0)
-               {
-                       return;
-               }
+               if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
+               m_fileStore.insert(key, file);
+               m_fileList.append(key);
+               if(flag) endInsertRows();
+               emit rowAppended();
        }
-       
-       beginResetModel();
-       m_fileList.append(file);
-       endResetModel();
 }
 
 bool FileListModel::removeFile(const QModelIndex &index)
@@ -170,6 +179,7 @@ bool FileListModel::removeFile(const QModelIndex &index)
        if(index.row() >= 0 && index.row() < m_fileList.count())
        {
                beginResetModel();
+               m_fileStore.remove(m_fileList.at(index.row()));
                m_fileList.removeAt(index.row());
                endResetModel();
                return true;
@@ -184,6 +194,7 @@ void FileListModel::clearFiles(void)
 {
        beginResetModel();
        m_fileList.clear();
+       m_fileStore.clear();
        endResetModel();
 }
 
@@ -202,29 +213,35 @@ bool FileListModel::moveFile(const QModelIndex &index, int delta)
        }
 }
 
-AudioFileModel FileListModel::getFile(const QModelIndex &index)
+const AudioFileModel &FileListModel::getFile(const QModelIndex &index)
 {
        if(index.row() >= 0 && index.row() < m_fileList.count())
        {
-               return m_fileList.at(index.row());
+               return m_fileStore[m_fileList.at(index.row())];         //return m_fileStore.value(m_fileList.at(index.row()));
        }
        else
        {
-               return AudioFileModel();
+               return m_nullAudioFile;
        }
 }
 
 AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
 {
-       return m_fileList[index.row()];
+       const QString key = m_fileList.at(index.row());
+       return m_fileStore[key];
 }
 
 bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
 {
        if(index.row() >= 0 && index.row() < m_fileList.count())
        {
+               const QString oldKey = m_fileList.at(index.row());
+               const QString newKey = MAKE_KEY(audioFile.filePath());
+               
                beginResetModel();
-               m_fileList.replace(index.row(), audioFile);
+               m_fileList.replace(index.row(), newKey);
+               m_fileStore.remove(oldKey);
+               m_fileStore.insert(newKey, audioFile);
                endResetModel();
                return true;
        }
@@ -242,13 +259,16 @@ int FileListModel::exportToCsv(const QString &outFile)
        
        for(int i = 0; i < nFiles; i++)
        {
-               if(m_fileList.at(i).filePosition() > 0) havePosition = true;
-               if(!m_fileList.at(i).fileName().isEmpty()) haveTitle = true;
-               if(!m_fileList.at(i).fileArtist().isEmpty()) haveArtist = true;
-               if(!m_fileList.at(i).fileAlbum().isEmpty()) haveAlbum = true;
-               if(!m_fileList.at(i).fileGenre().isEmpty()) haveGenre = true;
-               if(m_fileList.at(i).fileYear() > 0) haveYear = true;
-               if(!m_fileList.at(i).fileComment().isEmpty()) haveComment = true;
+               const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
+               const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
+               
+               if(metaInfo.position() > 0) havePosition = true;
+               if(!metaInfo.title().isEmpty()) haveTitle = true;
+               if(!metaInfo.artist().isEmpty()) haveArtist = true;
+               if(!metaInfo.album().isEmpty()) haveAlbum = true;
+               if(!metaInfo.genre().isEmpty()) haveGenre = true;
+               if(metaInfo.year() > 0) haveYear = true;
+               if(!metaInfo.comment().isEmpty()) haveComment = true;
        }
 
        if(!(haveTitle || haveArtist || haveAlbum || haveGenre || haveYear || haveComment))
@@ -284,14 +304,16 @@ int FileListModel::exportToCsv(const QString &outFile)
        for(int i = 0; i < nFiles; i++)
        {
                QStringList line;
+               const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
+               const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
                
-               if(havePosition) line << QString::number(m_fileList.at(i).filePosition());
-               if(haveTitle) line << m_fileList.at(i).fileName().trimmed();
-               if(haveArtist) line << m_fileList.at(i).fileArtist().trimmed();
-               if(haveAlbum) line << m_fileList.at(i).fileAlbum().trimmed();
-               if(haveGenre) line << m_fileList.at(i).fileGenre().trimmed();
-               if(haveYear) line << QString::number(m_fileList.at(i).fileYear());
-               if(haveComment) line << m_fileList.at(i).fileComment().trimmed();
+               if(havePosition) line << QString::number(metaInfo.position());
+               if(haveTitle) line << metaInfo.title().trimmed();
+               if(haveArtist) line << metaInfo.artist().trimmed();
+               if(haveAlbum) line << metaInfo.album().trimmed();
+               if(haveGenre) line << metaInfo.genre().trimmed();
+               if(haveYear) line << QString::number(metaInfo.year());
+               if(haveComment) line << metaInfo.comment().trimmed();
 
                if(file.write(line.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
                {
@@ -333,7 +355,7 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
 
                QStringList codecList;
                codecList.append(systemDefault);
-               codecList.append(lamexp_available_codepages());
+               codecList.append(MUtils::available_codepages());
 
                QInputDialog *input = new QInputDialog(parent);
                input->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:")));
@@ -344,7 +366,7 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
        
                if(input->exec() < 1)
                {
-                       LAMEXP_DELETE(input);
+                       MUTILS_DELETE(input);
                        return CsvError_Aborted;
                }
        
@@ -359,7 +381,7 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
                        codec = QTextCodec::codecForName("System");
                }
 
-               LAMEXP_DELETE(input);
+               MUTILS_DELETE(input);
        }
 
        bomCheck.clear();
@@ -411,7 +433,7 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
        {
                if(stream.atEnd())
                {
-                       LAMEXP_DELETE_ARRAY(ignore);
+                       MUTILS_DELETE_ARRAY(ignore);
                        return CsvError_Incomplete;
                }
                
@@ -431,6 +453,8 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
                        continue;
                }
 
+               const QString key = m_fileList[i];
+
                for(int j = 0; j < nCols; j++)
                {
                        if(ignore[j])
@@ -441,42 +465,42 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
                        {
                                bool ok = false;
                                unsigned int temp = data.at(j).trimmed().toUInt(&ok);
-                               if(ok) m_fileList[i].setFilePosition(temp);
+                               if(ok) m_fileStore[key].metaInfo().setPosition(temp);
                        }
                        else if(CHECK_HDR(header.at(j), "TITLE"))
                        {
                                QString temp = data.at(j).trimmed();
-                               if(!temp.isEmpty()) m_fileList[i].setFileName(temp);
+                               if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp);
                        }
                        else if(CHECK_HDR(header.at(j), "ARTIST"))
                        {
                                QString temp = data.at(j).trimmed();
-                               if(!temp.isEmpty()) m_fileList[i].setFileArtist(temp);
+                               if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp);
                        }
                        else if(CHECK_HDR(header.at(j), "ALBUM"))
                        {
                                QString temp = data.at(j).trimmed();
-                               if(!temp.isEmpty()) m_fileList[i].setFileAlbum(temp);
+                               if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp);
                        }
                        else if(CHECK_HDR(header.at(j), "GENRE"))
                        {
                                QString temp = data.at(j).trimmed();
-                               if(!temp.isEmpty()) m_fileList[i].setFileGenre(temp);
+                               if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp);
                        }
                        else if(CHECK_HDR(header.at(j), "YEAR"))
                        {
                                bool ok = false;
                                unsigned int temp = data.at(j).trimmed().toUInt(&ok);
-                               if(ok) m_fileList[i].setFileYear(temp);
+                               if(ok) m_fileStore[key].metaInfo().setYear(temp);
                        }
                        else if(CHECK_HDR(header.at(j), "COMMENT"))
                        {
                                QString temp = data.at(j).trimmed();
-                               if(!temp.isEmpty()) m_fileList[i].setFileComment(temp);
+                               if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp);
                        }
                        else
                        {
-                               qWarning("Unkonw field '%s' will be ignored!", header.at(j).toUtf8().constData());
+                               qWarning("Unkonw field '%s' will be ignored!", MUTILS_UTF8(header.at(j)));
                                ignore[j] = true;
                                
                                if(!checkArray(ignore, false, nCols))
@@ -490,7 +514,7 @@ int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
 
        //----------------------//
 
-       LAMEXP_DELETE_ARRAY(ignore);
+       MUTILS_DELETE_ARRAY(ignore);
        return CsvError_OK;
 }