2010-11-06 23:04:47 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// LameXP - Audio Encoder Front-End
|
2018-01-17 21:18:10 +01:00
|
|
|
// Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
|
2010-11-06 23:04:47 +01:00
|
|
|
//
|
|
|
|
// 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
|
2013-10-23 20:56:57 +02:00
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
2010-11-06 23:04:47 +01:00
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Model_FileList.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//Internal
|
2012-01-04 21:10:33 +01:00
|
|
|
#include "Global.h"
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
//MUtils
|
|
|
|
#include <MUtils/Global.h>
|
|
|
|
|
|
|
|
//Qt
|
2010-11-06 23:04:47 +01:00
|
|
|
#include <QFileInfo>
|
2011-01-25 00:52:00 +01:00
|
|
|
#include <QDir>
|
2012-01-04 03:23:07 +01:00
|
|
|
#include <QFile>
|
2012-01-04 21:10:33 +01:00
|
|
|
#include <QTextCodec>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QInputDialog>
|
|
|
|
|
|
|
|
#define EXPAND(STR) QString(STR).leftJustified(96, ' ')
|
|
|
|
#define CHECK_HDR(STR,NAM) (!(STR).compare((NAM), Qt::CaseInsensitive))
|
2012-05-04 16:39:02 +02:00
|
|
|
#define MAKE_KEY(PATH) (QDir::fromNativeSeparators(PATH).toLower())
|
2010-11-06 23:04:47 +01:00
|
|
|
|
2015-05-10 16:34:07 +02:00
|
|
|
static inline int LOG10(int x)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
while(x >= 10)
|
|
|
|
{
|
|
|
|
ret++; x /= 10;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Constructor & Destructor
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
FileListModel::FileListModel(void)
|
2010-11-20 02:14:22 +01:00
|
|
|
:
|
2012-05-05 21:56:14 +02:00
|
|
|
m_blockUpdates(false),
|
2010-11-20 02:14:22 +01:00
|
|
|
m_fileIcon(":/icons/page_white_cd.png")
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FileListModel::~FileListModel(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Public Functions
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int FileListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return m_fileList.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FileListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2015-05-10 16:34:07 +02:00
|
|
|
if(((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) && (index.row() < m_fileList.count()) && (index.row() >= 0))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
switch(index.column())
|
|
|
|
{
|
|
|
|
case 0:
|
2013-10-12 22:55:41 +02:00
|
|
|
return m_fileStore.value(m_fileList.at(index.row())).metaInfo().title();
|
2010-11-06 23:04:47 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2012-05-04 16:39:02 +02:00
|
|
|
return QDir::toNativeSeparators(m_fileStore.value(m_fileList.at(index.row())).filePath());
|
2010-11-06 23:04:47 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-10 16:34:07 +02:00
|
|
|
else if((role == Qt::DecorationRole) && (index.column() == 0))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
return m_fileIcon;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
|
|
|
if(orientation == Qt::Horizontal)
|
|
|
|
{
|
|
|
|
switch(section)
|
|
|
|
{
|
|
|
|
case 0:
|
2011-01-02 20:47:26 +01:00
|
|
|
return QVariant(tr("Title"));
|
2010-11-06 23:04:47 +01:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-01-02 20:47:26 +01:00
|
|
|
return QVariant(tr("Full Path"));
|
2010-11-06 23:04:47 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-05-10 16:34:07 +02:00
|
|
|
return int2str(section + 1);
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileListModel::addFile(const QString &filePath)
|
|
|
|
{
|
|
|
|
QFileInfo fileInfo(filePath);
|
2012-05-04 16:39:02 +02:00
|
|
|
const QString key = MAKE_KEY(fileInfo.canonicalFilePath());
|
2012-05-05 21:56:14 +02:00
|
|
|
const bool flag = (!m_blockUpdates);
|
2010-11-06 23:04:47 +01:00
|
|
|
|
2012-05-04 16:39:02 +02:00
|
|
|
if(!m_fileStore.contains(key))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
AudioFileModel audioFile(fileInfo.canonicalFilePath());
|
|
|
|
audioFile.metaInfo().setTitle(fileInfo.baseName());
|
2012-05-05 21:56:14 +02:00
|
|
|
if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
|
2013-10-12 22:55:41 +02:00
|
|
|
m_fileStore.insert(key, audioFile);
|
2012-05-04 16:39:02 +02:00
|
|
|
m_fileList.append(key);
|
2012-05-05 21:56:14 +02:00
|
|
|
if(flag) endInsertRows();
|
2012-05-06 04:57:00 +02:00
|
|
|
emit rowAppended();
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileListModel::addFile(const AudioFileModel &file)
|
|
|
|
{
|
2012-05-04 16:39:02 +02:00
|
|
|
const QString key = MAKE_KEY(file.filePath());
|
2012-05-05 21:56:14 +02:00
|
|
|
const bool flag = (!m_blockUpdates);
|
2012-05-04 16:39:02 +02:00
|
|
|
|
|
|
|
if(!m_fileStore.contains(key))
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
2012-05-05 21:56:14 +02:00
|
|
|
if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
|
2012-05-04 16:39:02 +02:00
|
|
|
m_fileStore.insert(key, file);
|
|
|
|
m_fileList.append(key);
|
2012-05-05 21:56:14 +02:00
|
|
|
if(flag) endInsertRows();
|
2012-05-06 04:57:00 +02:00
|
|
|
emit rowAppended();
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileListModel::removeFile(const QModelIndex &index)
|
|
|
|
{
|
2015-05-24 21:32:34 +02:00
|
|
|
const int row = index.row();
|
|
|
|
if(row >= 0 && row < m_fileList.count())
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
beginResetModel();
|
2015-05-24 21:32:34 +02:00
|
|
|
m_fileStore.remove(m_fileList.at(row));
|
|
|
|
m_fileList.removeAt(row);
|
2010-11-06 23:04:47 +01:00
|
|
|
endResetModel();
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-24 21:32:34 +02:00
|
|
|
return false;
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileListModel::clearFiles(void)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_fileList.clear();
|
2012-05-04 16:39:02 +02:00
|
|
|
m_fileStore.clear();
|
2010-11-06 23:04:47 +01:00
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FileListModel::moveFile(const QModelIndex &index, int delta)
|
|
|
|
{
|
|
|
|
if(delta != 0 && index.row() >= 0 && index.row() < m_fileList.count() && index.row() + delta >= 0 && index.row() + delta < m_fileList.count())
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
m_fileList.move(index.row(), index.row() + delta);
|
|
|
|
endResetModel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
const AudioFileModel &FileListModel::getFile(const QModelIndex &index)
|
2010-11-06 23:04:47 +01:00
|
|
|
{
|
|
|
|
if(index.row() >= 0 && index.row() < m_fileList.count())
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
return m_fileStore[m_fileList.at(index.row())]; //return m_fileStore.value(m_fileList.at(index.row()));
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
return m_nullAudioFile;
|
2010-11-06 23:04:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 02:06:01 +01:00
|
|
|
AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
|
|
|
|
{
|
2012-05-04 16:39:02 +02:00
|
|
|
const QString key = m_fileList.at(index.row());
|
|
|
|
return m_fileStore[key];
|
2010-11-08 02:06:01 +01:00
|
|
|
}
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
|
|
|
|
{
|
|
|
|
if(index.row() >= 0 && index.row() < m_fileList.count())
|
|
|
|
{
|
2012-05-04 16:39:02 +02:00
|
|
|
const QString oldKey = m_fileList.at(index.row());
|
|
|
|
const QString newKey = MAKE_KEY(audioFile.filePath());
|
|
|
|
|
2010-11-06 23:04:47 +01:00
|
|
|
beginResetModel();
|
2012-05-04 16:39:02 +02:00
|
|
|
m_fileList.replace(index.row(), newKey);
|
|
|
|
m_fileStore.remove(oldKey);
|
|
|
|
m_fileStore.insert(newKey, audioFile);
|
2010-11-06 23:04:47 +01:00
|
|
|
endResetModel();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 03:23:07 +01:00
|
|
|
|
|
|
|
int FileListModel::exportToCsv(const QString &outFile)
|
|
|
|
{
|
|
|
|
const int nFiles = m_fileList.count();
|
|
|
|
|
|
|
|
bool havePosition = false, haveTitle = false, haveArtist = false, haveAlbum = false, haveGenre = false, haveYear = false, haveComment = false;
|
|
|
|
|
|
|
|
for(int i = 0; i < nFiles; i++)
|
|
|
|
{
|
2013-10-12 22:55:41 +02:00
|
|
|
const AudioFileModel ¤t = m_fileStore.value(m_fileList.at(i));
|
|
|
|
const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
|
2012-05-04 16:39:02 +02:00
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
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;
|
2012-01-04 03:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!(haveTitle || haveArtist || haveAlbum || haveGenre || haveYear || haveComment))
|
|
|
|
{
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_NoTags;
|
2012-01-04 03:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(outFile);
|
|
|
|
|
2012-01-04 21:10:33 +01:00
|
|
|
if(!file.open(QIODevice::WriteOnly))
|
|
|
|
{
|
|
|
|
return CsvError_FileOpen;
|
|
|
|
}
|
|
|
|
else
|
2012-01-04 03:23:07 +01:00
|
|
|
{
|
|
|
|
QStringList line;
|
|
|
|
|
|
|
|
if(havePosition) line << "POSITION";
|
|
|
|
if(haveTitle) line << "TITLE";
|
|
|
|
if(haveArtist) line << "ARTIST";
|
|
|
|
if(haveAlbum) line << "ALBUM";
|
|
|
|
if(haveGenre) line << "GENRE";
|
|
|
|
if(haveYear) line << "YEAR";
|
|
|
|
if(haveComment) line << "COMMENT";
|
|
|
|
|
|
|
|
if(file.write(line.join(";").append("\r\n").toUtf8().prepend("\xef\xbb\xbf")) < 1)
|
|
|
|
{
|
|
|
|
file.close();
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_FileWrite;
|
2012-01-04 03:23:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < nFiles; i++)
|
|
|
|
{
|
|
|
|
QStringList line;
|
2013-10-12 22:55:41 +02:00
|
|
|
const AudioFileModel ¤t = m_fileStore.value(m_fileList.at(i));
|
|
|
|
const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
|
2012-01-04 03:23:07 +01:00
|
|
|
|
2013-10-12 22:55:41 +02:00
|
|
|
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();
|
2012-01-04 03:23:07 +01:00
|
|
|
|
|
|
|
if(file.write(line.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
|
|
|
|
{
|
|
|
|
file.close();
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_FileWrite;
|
2012-01-04 03:23:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.close();
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
|
|
|
|
{
|
|
|
|
QFile file(inFile);
|
|
|
|
if(!file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
return CsvError_FileOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCodec *codec = NULL;
|
|
|
|
QByteArray bomCheck = file.peek(16);
|
|
|
|
|
|
|
|
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);
|
2014-12-19 23:49:11 +01:00
|
|
|
codecList.append(MUtils::available_codepages());
|
2012-01-04 21:10:33 +01:00
|
|
|
|
|
|
|
QInputDialog *input = new QInputDialog(parent);
|
|
|
|
input->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:")));
|
|
|
|
input->setOkButtonText(tr("OK"));
|
|
|
|
input->setCancelButtonText(tr("Cancel"));
|
|
|
|
input->setTextEchoMode(QLineEdit::Normal);
|
|
|
|
input->setComboBoxItems(codecList);
|
|
|
|
|
|
|
|
if(input->exec() < 1)
|
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
MUTILS_DELETE(input);
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_Aborted;
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
MUTILS_DELETE(input);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bomCheck.clear();
|
|
|
|
|
|
|
|
//----------------------//
|
|
|
|
|
|
|
|
QTextStream stream(&file);
|
|
|
|
stream.setAutoDetectUnicode(false);
|
|
|
|
stream.setCodec(codec);
|
|
|
|
|
2012-01-05 21:08:04 +01:00
|
|
|
QString headerLine = stream.readLine().simplified();
|
|
|
|
|
|
|
|
while(headerLine.isEmpty())
|
|
|
|
{
|
|
|
|
if(stream.atEnd())
|
|
|
|
{
|
|
|
|
qWarning("The file appears to be empty!");
|
|
|
|
return CsvError_FileRead;
|
|
|
|
}
|
|
|
|
qWarning("Skipping a blank line at beginning of CSV file!");
|
|
|
|
headerLine = stream.readLine().simplified();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList header = headerLine.split(";", QString::KeepEmptyParts);
|
2012-01-04 21:10:33 +01:00
|
|
|
|
|
|
|
const int nCols = header.count();
|
|
|
|
const int nFiles = m_fileList.count();
|
|
|
|
|
|
|
|
if(nCols < 1)
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
qWarning("Header appears to be empty!");
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_FileRead;
|
|
|
|
}
|
|
|
|
|
2012-01-05 21:08:04 +01:00
|
|
|
bool *ignore = new bool[nCols];
|
|
|
|
memset(ignore, 0, sizeof(bool) * nCols);
|
|
|
|
|
2012-01-04 21:10:33 +01:00
|
|
|
for(int i = 0; i < nCols; i++)
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
if((header[i] = header[i].trimmed()).isEmpty())
|
|
|
|
{
|
|
|
|
ignore[i] = true;
|
|
|
|
}
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------//
|
|
|
|
|
|
|
|
for(int i = 0; i < nFiles; i++)
|
|
|
|
{
|
|
|
|
if(stream.atEnd())
|
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
MUTILS_DELETE_ARRAY(ignore);
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_Incomplete;
|
|
|
|
}
|
|
|
|
|
2012-01-05 21:08:04 +01:00
|
|
|
QString line = stream.readLine().simplified();
|
|
|
|
|
|
|
|
if(line.isEmpty())
|
|
|
|
{
|
|
|
|
qWarning("Skipping a blank line in CSV file!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList data = line.split(";", QString::KeepEmptyParts);
|
2012-01-04 21:10:33 +01:00
|
|
|
|
2012-01-05 21:08:04 +01:00
|
|
|
if(data.count() < header.count())
|
2012-01-04 21:10:33 +01:00
|
|
|
{
|
|
|
|
qWarning("Skipping an incomplete line in CSV file!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-05-04 16:39:02 +02:00
|
|
|
const QString key = m_fileList[i];
|
|
|
|
|
2012-01-04 21:10:33 +01:00
|
|
|
for(int j = 0; j < nCols; j++)
|
|
|
|
{
|
|
|
|
if(ignore[j])
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "POSITION"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
2012-01-05 21:08:04 +01:00
|
|
|
unsigned int temp = data.at(j).trimmed().toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) m_fileStore[key].metaInfo().setPosition(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "TITLE"))
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
QString temp = data.at(j).trimmed();
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "ARTIST"))
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
QString temp = data.at(j).trimmed();
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "ALBUM"))
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
QString temp = data.at(j).trimmed();
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "GENRE"))
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
QString temp = data.at(j).trimmed();
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "YEAR"))
|
|
|
|
{
|
|
|
|
bool ok = false;
|
2012-01-05 21:08:04 +01:00
|
|
|
unsigned int temp = data.at(j).trimmed().toUInt(&ok);
|
2013-10-12 22:55:41 +02:00
|
|
|
if(ok) m_fileStore[key].metaInfo().setYear(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else if(CHECK_HDR(header.at(j), "COMMENT"))
|
|
|
|
{
|
2012-01-05 21:08:04 +01:00
|
|
|
QString temp = data.at(j).trimmed();
|
2013-10-12 22:55:41 +02:00
|
|
|
if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp);
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-25 02:14:42 +01:00
|
|
|
qWarning("Unkonw field '%s' will be ignored!", MUTILS_UTF8(header.at(j)));
|
2012-01-04 21:10:33 +01:00
|
|
|
ignore[j] = true;
|
2012-01-05 21:08:04 +01:00
|
|
|
|
|
|
|
if(!checkArray(ignore, false, nCols))
|
|
|
|
{
|
|
|
|
qWarning("No known fields left, aborting!");
|
|
|
|
return CsvError_NoTags;
|
|
|
|
}
|
2012-01-04 21:10:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------//
|
|
|
|
|
2014-11-25 02:14:42 +01:00
|
|
|
MUTILS_DELETE_ARRAY(ignore);
|
2012-01-04 21:10:33 +01:00
|
|
|
return CsvError_OK;
|
2012-01-04 03:23:07 +01:00
|
|
|
}
|
2012-01-05 21:08:04 +01:00
|
|
|
|
|
|
|
bool FileListModel::checkArray(const bool *a, const bool val, size_t len)
|
|
|
|
{
|
|
|
|
for(size_t i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
if(a[i] == val) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-10 16:34:07 +02:00
|
|
|
|
|
|
|
QString FileListModel::int2str(const int &value) const
|
|
|
|
{
|
|
|
|
if(m_fileList.count() < 10)
|
|
|
|
{
|
|
|
|
return QString().sprintf("%d", value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const QString format = QString().sprintf("%%0%dd", LOG10(m_fileList.count()));
|
|
|
|
return QString().sprintf(format.toLatin1().constData(), value);
|
|
|
|
}
|
|
|
|
}
|