1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Model_FileList.h"
30 #include <QTextStream>
31 #include <QInputDialog>
33 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
34 #define CHECK_HDR(STR,NAM) (!(STR).compare((NAM), Qt::CaseInsensitive))
35 #define MAKE_KEY(PATH) (QDir::fromNativeSeparators(PATH).toLower())
37 ////////////////////////////////////////////////////////////
38 // Constructor & Destructor
39 ////////////////////////////////////////////////////////////
41 FileListModel::FileListModel(void)
43 m_blockUpdates(false),
44 m_fileIcon(":/icons/page_white_cd.png")
48 FileListModel::~FileListModel(void)
52 ////////////////////////////////////////////////////////////
54 ////////////////////////////////////////////////////////////
56 int FileListModel::columnCount(const QModelIndex &parent) const
61 int FileListModel::rowCount(const QModelIndex &parent) const
63 return m_fileList.count();
66 QVariant FileListModel::data(const QModelIndex &index, int role) const
68 if((role == Qt::DisplayRole || role == Qt::ToolTipRole) && index.row() < m_fileList.count() && index.row() >= 0)
70 switch(index.column())
73 return m_fileStore.value(m_fileList.at(index.row())).metaInfo().title();
76 return QDir::toNativeSeparators(m_fileStore.value(m_fileList.at(index.row())).filePath());
83 else if(role == Qt::DecorationRole && index.column() == 0)
93 QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
95 if(role == Qt::DisplayRole)
97 if(orientation == Qt::Horizontal)
102 return QVariant(tr("Title"));
105 return QVariant(tr("Full Path"));
114 if(m_fileList.count() < 10)
116 return QVariant(QString().sprintf("%d", section + 1));
118 else if(m_fileList.count() < 100)
120 return QVariant(QString().sprintf("%02d", section + 1));
122 else if(m_fileList.count() < 1000)
124 return QVariant(QString().sprintf("%03d", section + 1));
128 return QVariant(QString().sprintf("%04d", section + 1));
138 void FileListModel::addFile(const QString &filePath)
140 QFileInfo fileInfo(filePath);
141 const QString key = MAKE_KEY(fileInfo.canonicalFilePath());
142 const bool flag = (!m_blockUpdates);
144 if(!m_fileStore.contains(key))
146 AudioFileModel audioFile(fileInfo.canonicalFilePath());
147 audioFile.metaInfo().setTitle(fileInfo.baseName());
148 if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
149 m_fileStore.insert(key, audioFile);
150 m_fileList.append(key);
151 if(flag) endInsertRows();
156 void FileListModel::addFile(const AudioFileModel &file)
158 const QString key = MAKE_KEY(file.filePath());
159 const bool flag = (!m_blockUpdates);
161 if(!m_fileStore.contains(key))
163 if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
164 m_fileStore.insert(key, file);
165 m_fileList.append(key);
166 if(flag) endInsertRows();
171 bool FileListModel::removeFile(const QModelIndex &index)
173 if(index.row() >= 0 && index.row() < m_fileList.count())
176 m_fileStore.remove(m_fileList.at(index.row()));
177 m_fileList.removeAt(index.row());
187 void FileListModel::clearFiles(void)
195 bool FileListModel::moveFile(const QModelIndex &index, int delta)
197 if(delta != 0 && index.row() >= 0 && index.row() < m_fileList.count() && index.row() + delta >= 0 && index.row() + delta < m_fileList.count())
200 m_fileList.move(index.row(), index.row() + delta);
210 const AudioFileModel &FileListModel::getFile(const QModelIndex &index)
212 if(index.row() >= 0 && index.row() < m_fileList.count())
214 return m_fileStore[m_fileList.at(index.row())]; //return m_fileStore.value(m_fileList.at(index.row()));
218 return m_nullAudioFile;
222 AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
224 const QString key = m_fileList.at(index.row());
225 return m_fileStore[key];
228 bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
230 if(index.row() >= 0 && index.row() < m_fileList.count())
232 const QString oldKey = m_fileList.at(index.row());
233 const QString newKey = MAKE_KEY(audioFile.filePath());
236 m_fileList.replace(index.row(), newKey);
237 m_fileStore.remove(oldKey);
238 m_fileStore.insert(newKey, audioFile);
248 int FileListModel::exportToCsv(const QString &outFile)
250 const int nFiles = m_fileList.count();
252 bool havePosition = false, haveTitle = false, haveArtist = false, haveAlbum = false, haveGenre = false, haveYear = false, haveComment = false;
254 for(int i = 0; i < nFiles; i++)
256 const AudioFileModel ¤t = m_fileStore.value(m_fileList.at(i));
257 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
259 if(metaInfo.position() > 0) havePosition = true;
260 if(!metaInfo.title().isEmpty()) haveTitle = true;
261 if(!metaInfo.artist().isEmpty()) haveArtist = true;
262 if(!metaInfo.album().isEmpty()) haveAlbum = true;
263 if(!metaInfo.genre().isEmpty()) haveGenre = true;
264 if(metaInfo.year() > 0) haveYear = true;
265 if(!metaInfo.comment().isEmpty()) haveComment = true;
268 if(!(haveTitle || haveArtist || haveAlbum || haveGenre || haveYear || haveComment))
270 return CsvError_NoTags;
275 if(!file.open(QIODevice::WriteOnly))
277 return CsvError_FileOpen;
283 if(havePosition) line << "POSITION";
284 if(haveTitle) line << "TITLE";
285 if(haveArtist) line << "ARTIST";
286 if(haveAlbum) line << "ALBUM";
287 if(haveGenre) line << "GENRE";
288 if(haveYear) line << "YEAR";
289 if(haveComment) line << "COMMENT";
291 if(file.write(line.join(";").append("\r\n").toUtf8().prepend("\xef\xbb\xbf")) < 1)
294 return CsvError_FileWrite;
298 for(int i = 0; i < nFiles; i++)
301 const AudioFileModel ¤t = m_fileStore.value(m_fileList.at(i));
302 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
304 if(havePosition) line << QString::number(metaInfo.position());
305 if(haveTitle) line << metaInfo.title().trimmed();
306 if(haveArtist) line << metaInfo.artist().trimmed();
307 if(haveAlbum) line << metaInfo.album().trimmed();
308 if(haveGenre) line << metaInfo.genre().trimmed();
309 if(haveYear) line << QString::number(metaInfo.year());
310 if(haveComment) line << metaInfo.comment().trimmed();
312 if(file.write(line.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
315 return CsvError_FileWrite;
323 int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
326 if(!file.open(QIODevice::ReadOnly))
328 return CsvError_FileOpen;
331 QTextCodec *codec = NULL;
332 QByteArray bomCheck = file.peek(16);
334 if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf"))
336 codec = QTextCodec::codecForName("UTF-8");
338 else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe"))
340 codec = QTextCodec::codecForName("UTF-16LE");
342 else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff"))
344 codec = QTextCodec::codecForName("UTF-16BE");
348 const QString systemDefault = tr("(System Default)");
350 QStringList codecList;
351 codecList.append(systemDefault);
352 codecList.append(lamexp_available_codepages());
354 QInputDialog *input = new QInputDialog(parent);
355 input->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:")));
356 input->setOkButtonText(tr("OK"));
357 input->setCancelButtonText(tr("Cancel"));
358 input->setTextEchoMode(QLineEdit::Normal);
359 input->setComboBoxItems(codecList);
361 if(input->exec() < 1)
363 LAMEXP_DELETE(input);
364 return CsvError_Aborted;
367 if(input->textValue().compare(systemDefault, Qt::CaseInsensitive))
369 qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData());
370 codec = QTextCodec::codecForName(input->textValue().toLatin1().constData());
374 qDebug("Going to use the system's default codec!");
375 codec = QTextCodec::codecForName("System");
378 LAMEXP_DELETE(input);
383 //----------------------//
385 QTextStream stream(&file);
386 stream.setAutoDetectUnicode(false);
387 stream.setCodec(codec);
389 QString headerLine = stream.readLine().simplified();
391 while(headerLine.isEmpty())
395 qWarning("The file appears to be empty!");
396 return CsvError_FileRead;
398 qWarning("Skipping a blank line at beginning of CSV file!");
399 headerLine = stream.readLine().simplified();
402 QStringList header = headerLine.split(";", QString::KeepEmptyParts);
404 const int nCols = header.count();
405 const int nFiles = m_fileList.count();
409 qWarning("Header appears to be empty!");
410 return CsvError_FileRead;
413 bool *ignore = new bool[nCols];
414 memset(ignore, 0, sizeof(bool) * nCols);
416 for(int i = 0; i < nCols; i++)
418 if((header[i] = header[i].trimmed()).isEmpty())
424 //----------------------//
426 for(int i = 0; i < nFiles; i++)
430 LAMEXP_DELETE_ARRAY(ignore);
431 return CsvError_Incomplete;
434 QString line = stream.readLine().simplified();
438 qWarning("Skipping a blank line in CSV file!");
442 QStringList data = line.split(";", QString::KeepEmptyParts);
444 if(data.count() < header.count())
446 qWarning("Skipping an incomplete line in CSV file!");
450 const QString key = m_fileList[i];
452 for(int j = 0; j < nCols; j++)
458 else if(CHECK_HDR(header.at(j), "POSITION"))
461 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
462 if(ok) m_fileStore[key].metaInfo().setPosition(temp);
464 else if(CHECK_HDR(header.at(j), "TITLE"))
466 QString temp = data.at(j).trimmed();
467 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp);
469 else if(CHECK_HDR(header.at(j), "ARTIST"))
471 QString temp = data.at(j).trimmed();
472 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp);
474 else if(CHECK_HDR(header.at(j), "ALBUM"))
476 QString temp = data.at(j).trimmed();
477 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp);
479 else if(CHECK_HDR(header.at(j), "GENRE"))
481 QString temp = data.at(j).trimmed();
482 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp);
484 else if(CHECK_HDR(header.at(j), "YEAR"))
487 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
488 if(ok) m_fileStore[key].metaInfo().setYear(temp);
490 else if(CHECK_HDR(header.at(j), "COMMENT"))
492 QString temp = data.at(j).trimmed();
493 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp);
497 qWarning("Unkonw field '%s' will be ignored!", QUTF8(header.at(j)));
500 if(!checkArray(ignore, false, nCols))
502 qWarning("No known fields left, aborting!");
503 return CsvError_NoTags;
509 //----------------------//
511 LAMEXP_DELETE_ARRAY(ignore);
515 bool FileListModel::checkArray(const bool *a, const bool val, size_t len)
517 for(size_t i = 0; i < len; i++)
519 if(a[i] == val) return true;