Some code clean-up in "About" dialog.
[LameXP.git] / src / Model_FileList.cpp
blobc914d8e9240bf90fed7b0dbfe5a88b239bdb3a86
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Model_FileList.h"
25 //Internal
26 #include "Global.h"
28 //MUtils
29 #include <MUtils/Global.h>
31 //Qt
32 #include <QFileInfo>
33 #include <QDir>
34 #include <QFile>
35 #include <QTextCodec>
36 #include <QTextStream>
37 #include <QInputDialog>
39 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
40 #define CHECK_HDR(STR,NAM) (!(STR).compare((NAM), Qt::CaseInsensitive))
41 #define MAKE_KEY(PATH) (QDir::fromNativeSeparators(PATH).toLower())
43 static inline int LOG10(int x)
45 int ret = 1;
46 while(x >= 10)
48 ret++; x /= 10;
50 return ret;
53 ////////////////////////////////////////////////////////////
54 // Constructor & Destructor
55 ////////////////////////////////////////////////////////////
57 FileListModel::FileListModel(void)
59 m_blockUpdates(false),
60 m_fileIcon(":/icons/page_white_cd.png")
64 FileListModel::~FileListModel(void)
68 ////////////////////////////////////////////////////////////
69 // Public Functions
70 ////////////////////////////////////////////////////////////
72 int FileListModel::columnCount(const QModelIndex &parent) const
74 return 2;
77 int FileListModel::rowCount(const QModelIndex &parent) const
79 return m_fileList.count();
82 QVariant FileListModel::data(const QModelIndex &index, int role) const
84 if(((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) && (index.row() < m_fileList.count()) && (index.row() >= 0))
86 switch(index.column())
88 case 0:
89 return m_fileStore.value(m_fileList.at(index.row())).metaInfo().title();
90 break;
91 case 1:
92 return QDir::toNativeSeparators(m_fileStore.value(m_fileList.at(index.row())).filePath());
93 break;
94 default:
95 return QVariant();
96 break;
99 else if((role == Qt::DecorationRole) && (index.column() == 0))
101 return m_fileIcon;
103 else
105 return QVariant();
109 QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
111 if(role == Qt::DisplayRole)
113 if(orientation == Qt::Horizontal)
115 switch(section)
117 case 0:
118 return QVariant(tr("Title"));
119 break;
120 case 1:
121 return QVariant(tr("Full Path"));
122 break;
123 default:
124 return QVariant();
125 break;
128 else
130 return int2str(section + 1);
133 else
135 return QVariant();
139 void FileListModel::addFile(const QString &filePath)
141 QFileInfo fileInfo(filePath);
142 const QString key = MAKE_KEY(fileInfo.canonicalFilePath());
143 const bool flag = (!m_blockUpdates);
145 if(!m_fileStore.contains(key))
147 AudioFileModel audioFile(fileInfo.canonicalFilePath());
148 audioFile.metaInfo().setTitle(fileInfo.baseName());
149 if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
150 m_fileStore.insert(key, audioFile);
151 m_fileList.append(key);
152 if(flag) endInsertRows();
153 emit rowAppended();
157 void FileListModel::addFile(const AudioFileModel &file)
159 const QString key = MAKE_KEY(file.filePath());
160 const bool flag = (!m_blockUpdates);
162 if(!m_fileStore.contains(key))
164 if(flag) beginInsertRows(QModelIndex(), m_fileList.count(), m_fileList.count());
165 m_fileStore.insert(key, file);
166 m_fileList.append(key);
167 if(flag) endInsertRows();
168 emit rowAppended();
172 bool FileListModel::removeFile(const QModelIndex &index)
174 const int row = index.row();
175 if(row >= 0 && row < m_fileList.count())
177 beginResetModel();
178 m_fileStore.remove(m_fileList.at(row));
179 m_fileList.removeAt(row);
180 endResetModel();
181 return true;
183 return false;
186 void FileListModel::clearFiles(void)
188 beginResetModel();
189 m_fileList.clear();
190 m_fileStore.clear();
191 endResetModel();
194 bool FileListModel::moveFile(const QModelIndex &index, int delta)
196 if(delta != 0 && index.row() >= 0 && index.row() < m_fileList.count() && index.row() + delta >= 0 && index.row() + delta < m_fileList.count())
198 beginResetModel();
199 m_fileList.move(index.row(), index.row() + delta);
200 endResetModel();
201 return true;
203 else
205 return false;
209 const AudioFileModel &FileListModel::getFile(const QModelIndex &index)
211 if(index.row() >= 0 && index.row() < m_fileList.count())
213 return m_fileStore[m_fileList.at(index.row())]; //return m_fileStore.value(m_fileList.at(index.row()));
215 else
217 return m_nullAudioFile;
221 AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
223 const QString key = m_fileList.at(index.row());
224 return m_fileStore[key];
227 bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
229 if(index.row() >= 0 && index.row() < m_fileList.count())
231 const QString oldKey = m_fileList.at(index.row());
232 const QString newKey = MAKE_KEY(audioFile.filePath());
234 beginResetModel();
235 m_fileList.replace(index.row(), newKey);
236 m_fileStore.remove(oldKey);
237 m_fileStore.insert(newKey, audioFile);
238 endResetModel();
239 return true;
241 else
243 return false;
247 int FileListModel::exportToCsv(const QString &outFile)
249 const int nFiles = m_fileList.count();
251 bool havePosition = false, haveTitle = false, haveArtist = false, haveAlbum = false, haveGenre = false, haveYear = false, haveComment = false;
253 for(int i = 0; i < nFiles; i++)
255 const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
256 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
258 if(metaInfo.position() > 0) havePosition = true;
259 if(!metaInfo.title().isEmpty()) haveTitle = true;
260 if(!metaInfo.artist().isEmpty()) haveArtist = true;
261 if(!metaInfo.album().isEmpty()) haveAlbum = true;
262 if(!metaInfo.genre().isEmpty()) haveGenre = true;
263 if(metaInfo.year() > 0) haveYear = true;
264 if(!metaInfo.comment().isEmpty()) haveComment = true;
267 if(!(haveTitle || haveArtist || haveAlbum || haveGenre || haveYear || haveComment))
269 return CsvError_NoTags;
272 QFile file(outFile);
274 if(!file.open(QIODevice::WriteOnly))
276 return CsvError_FileOpen;
278 else
280 QStringList line;
282 if(havePosition) line << "POSITION";
283 if(haveTitle) line << "TITLE";
284 if(haveArtist) line << "ARTIST";
285 if(haveAlbum) line << "ALBUM";
286 if(haveGenre) line << "GENRE";
287 if(haveYear) line << "YEAR";
288 if(haveComment) line << "COMMENT";
290 if(file.write(line.join(";").append("\r\n").toUtf8().prepend("\xef\xbb\xbf")) < 1)
292 file.close();
293 return CsvError_FileWrite;
297 for(int i = 0; i < nFiles; i++)
299 QStringList line;
300 const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
301 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
303 if(havePosition) line << QString::number(metaInfo.position());
304 if(haveTitle) line << metaInfo.title().trimmed();
305 if(haveArtist) line << metaInfo.artist().trimmed();
306 if(haveAlbum) line << metaInfo.album().trimmed();
307 if(haveGenre) line << metaInfo.genre().trimmed();
308 if(haveYear) line << QString::number(metaInfo.year());
309 if(haveComment) line << metaInfo.comment().trimmed();
311 if(file.write(line.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
313 file.close();
314 return CsvError_FileWrite;
318 file.close();
319 return CsvError_OK;
322 int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
324 QFile file(inFile);
325 if(!file.open(QIODevice::ReadOnly))
327 return CsvError_FileOpen;
330 QTextCodec *codec = NULL;
331 QByteArray bomCheck = file.peek(16);
333 if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf"))
335 codec = QTextCodec::codecForName("UTF-8");
337 else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe"))
339 codec = QTextCodec::codecForName("UTF-16LE");
341 else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff"))
343 codec = QTextCodec::codecForName("UTF-16BE");
345 else
347 const QString systemDefault = tr("(System Default)");
349 QStringList codecList;
350 codecList.append(systemDefault);
351 codecList.append(MUtils::available_codepages());
353 QInputDialog *input = new QInputDialog(parent);
354 input->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:")));
355 input->setOkButtonText(tr("OK"));
356 input->setCancelButtonText(tr("Cancel"));
357 input->setTextEchoMode(QLineEdit::Normal);
358 input->setComboBoxItems(codecList);
360 if(input->exec() < 1)
362 MUTILS_DELETE(input);
363 return CsvError_Aborted;
366 if(input->textValue().compare(systemDefault, Qt::CaseInsensitive))
368 qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData());
369 codec = QTextCodec::codecForName(input->textValue().toLatin1().constData());
371 else
373 qDebug("Going to use the system's default codec!");
374 codec = QTextCodec::codecForName("System");
377 MUTILS_DELETE(input);
380 bomCheck.clear();
382 //----------------------//
384 QTextStream stream(&file);
385 stream.setAutoDetectUnicode(false);
386 stream.setCodec(codec);
388 QString headerLine = stream.readLine().simplified();
390 while(headerLine.isEmpty())
392 if(stream.atEnd())
394 qWarning("The file appears to be empty!");
395 return CsvError_FileRead;
397 qWarning("Skipping a blank line at beginning of CSV file!");
398 headerLine = stream.readLine().simplified();
401 QStringList header = headerLine.split(";", QString::KeepEmptyParts);
403 const int nCols = header.count();
404 const int nFiles = m_fileList.count();
406 if(nCols < 1)
408 qWarning("Header appears to be empty!");
409 return CsvError_FileRead;
412 bool *ignore = new bool[nCols];
413 memset(ignore, 0, sizeof(bool) * nCols);
415 for(int i = 0; i < nCols; i++)
417 if((header[i] = header[i].trimmed()).isEmpty())
419 ignore[i] = true;
423 //----------------------//
425 for(int i = 0; i < nFiles; i++)
427 if(stream.atEnd())
429 MUTILS_DELETE_ARRAY(ignore);
430 return CsvError_Incomplete;
433 QString line = stream.readLine().simplified();
435 if(line.isEmpty())
437 qWarning("Skipping a blank line in CSV file!");
438 continue;
441 QStringList data = line.split(";", QString::KeepEmptyParts);
443 if(data.count() < header.count())
445 qWarning("Skipping an incomplete line in CSV file!");
446 continue;
449 const QString key = m_fileList[i];
451 for(int j = 0; j < nCols; j++)
453 if(ignore[j])
455 continue;
457 else if(CHECK_HDR(header.at(j), "POSITION"))
459 bool ok = false;
460 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
461 if(ok) m_fileStore[key].metaInfo().setPosition(temp);
463 else if(CHECK_HDR(header.at(j), "TITLE"))
465 QString temp = data.at(j).trimmed();
466 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp);
468 else if(CHECK_HDR(header.at(j), "ARTIST"))
470 QString temp = data.at(j).trimmed();
471 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp);
473 else if(CHECK_HDR(header.at(j), "ALBUM"))
475 QString temp = data.at(j).trimmed();
476 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp);
478 else if(CHECK_HDR(header.at(j), "GENRE"))
480 QString temp = data.at(j).trimmed();
481 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp);
483 else if(CHECK_HDR(header.at(j), "YEAR"))
485 bool ok = false;
486 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
487 if(ok) m_fileStore[key].metaInfo().setYear(temp);
489 else if(CHECK_HDR(header.at(j), "COMMENT"))
491 QString temp = data.at(j).trimmed();
492 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp);
494 else
496 qWarning("Unkonw field '%s' will be ignored!", MUTILS_UTF8(header.at(j)));
497 ignore[j] = true;
499 if(!checkArray(ignore, false, nCols))
501 qWarning("No known fields left, aborting!");
502 return CsvError_NoTags;
508 //----------------------//
510 MUTILS_DELETE_ARRAY(ignore);
511 return CsvError_OK;
514 bool FileListModel::checkArray(const bool *a, const bool val, size_t len)
516 for(size_t i = 0; i < len; i++)
518 if(a[i] == val) return true;
521 return false;
524 QString FileListModel::int2str(const int &value) const
526 if(m_fileList.count() < 10)
528 return QString().sprintf("%d", value);
530 else
532 const QString format = QString().sprintf("%%0%dd", LOG10(m_fileList.count()));
533 return QString().sprintf(format.toLatin1().constData(), value);