Updated Ukrainian translation.
[LameXP.git] / src / Model_FileList.cpp
blob5d4d5bd4bbfeb3a42b5027d9ed079743405e870d
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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 #include "Global.h"
27 #include <QFileInfo>
28 #include <QDir>
29 #include <QFile>
30 #include <QTextCodec>
31 #include <QTextStream>
32 #include <QInputDialog>
34 #define EXPAND(STR) QString(STR).leftJustified(96, ' ')
35 #define CHECK_HDR(STR,NAM) (!(STR).compare((NAM), Qt::CaseInsensitive))
36 #define MAKE_KEY(PATH) (QDir::fromNativeSeparators(PATH).toLower())
38 ////////////////////////////////////////////////////////////
39 // Constructor & Destructor
40 ////////////////////////////////////////////////////////////
42 FileListModel::FileListModel(void)
44 m_blockUpdates(false),
45 m_fileIcon(":/icons/page_white_cd.png")
49 FileListModel::~FileListModel(void)
53 ////////////////////////////////////////////////////////////
54 // Public Functions
55 ////////////////////////////////////////////////////////////
57 int FileListModel::columnCount(const QModelIndex &parent) const
59 return 2;
62 int FileListModel::rowCount(const QModelIndex &parent) const
64 return m_fileList.count();
67 QVariant FileListModel::data(const QModelIndex &index, int role) const
69 if((role == Qt::DisplayRole || role == Qt::ToolTipRole) && index.row() < m_fileList.count() && index.row() >= 0)
71 switch(index.column())
73 case 0:
74 return m_fileStore.value(m_fileList.at(index.row())).metaInfo().title();
75 break;
76 case 1:
77 return QDir::toNativeSeparators(m_fileStore.value(m_fileList.at(index.row())).filePath());
78 break;
79 default:
80 return QVariant();
81 break;
84 else if(role == Qt::DecorationRole && index.column() == 0)
86 return m_fileIcon;
88 else
90 return QVariant();
94 QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
96 if(role == Qt::DisplayRole)
98 if(orientation == Qt::Horizontal)
100 switch(section)
102 case 0:
103 return QVariant(tr("Title"));
104 break;
105 case 1:
106 return QVariant(tr("Full Path"));
107 break;
108 default:
109 return QVariant();
110 break;
113 else
115 if(m_fileList.count() < 10)
117 return QVariant(QString().sprintf("%d", section + 1));
119 else if(m_fileList.count() < 100)
121 return QVariant(QString().sprintf("%02d", section + 1));
123 else if(m_fileList.count() < 1000)
125 return QVariant(QString().sprintf("%03d", section + 1));
127 else
129 return QVariant(QString().sprintf("%04d", 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 if(index.row() >= 0 && index.row() < m_fileList.count())
176 beginResetModel();
177 m_fileStore.remove(m_fileList.at(index.row()));
178 m_fileList.removeAt(index.row());
179 endResetModel();
180 return true;
182 else
184 return false;
188 void FileListModel::clearFiles(void)
190 beginResetModel();
191 m_fileList.clear();
192 m_fileStore.clear();
193 endResetModel();
196 bool FileListModel::moveFile(const QModelIndex &index, int delta)
198 if(delta != 0 && index.row() >= 0 && index.row() < m_fileList.count() && index.row() + delta >= 0 && index.row() + delta < m_fileList.count())
200 beginResetModel();
201 m_fileList.move(index.row(), index.row() + delta);
202 endResetModel();
203 return true;
205 else
207 return false;
211 const AudioFileModel &FileListModel::getFile(const QModelIndex &index)
213 if(index.row() >= 0 && index.row() < m_fileList.count())
215 return m_fileStore[m_fileList.at(index.row())]; //return m_fileStore.value(m_fileList.at(index.row()));
217 else
219 return m_nullAudioFile;
223 AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
225 const QString key = m_fileList.at(index.row());
226 return m_fileStore[key];
229 bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
231 if(index.row() >= 0 && index.row() < m_fileList.count())
233 const QString oldKey = m_fileList.at(index.row());
234 const QString newKey = MAKE_KEY(audioFile.filePath());
236 beginResetModel();
237 m_fileList.replace(index.row(), newKey);
238 m_fileStore.remove(oldKey);
239 m_fileStore.insert(newKey, audioFile);
240 endResetModel();
241 return true;
243 else
245 return false;
249 int FileListModel::exportToCsv(const QString &outFile)
251 const int nFiles = m_fileList.count();
253 bool havePosition = false, haveTitle = false, haveArtist = false, haveAlbum = false, haveGenre = false, haveYear = false, haveComment = false;
255 for(int i = 0; i < nFiles; i++)
257 const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
258 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
260 if(metaInfo.position() > 0) havePosition = true;
261 if(!metaInfo.title().isEmpty()) haveTitle = true;
262 if(!metaInfo.artist().isEmpty()) haveArtist = true;
263 if(!metaInfo.album().isEmpty()) haveAlbum = true;
264 if(!metaInfo.genre().isEmpty()) haveGenre = true;
265 if(metaInfo.year() > 0) haveYear = true;
266 if(!metaInfo.comment().isEmpty()) haveComment = true;
269 if(!(haveTitle || haveArtist || haveAlbum || haveGenre || haveYear || haveComment))
271 return CsvError_NoTags;
274 QFile file(outFile);
276 if(!file.open(QIODevice::WriteOnly))
278 return CsvError_FileOpen;
280 else
282 QStringList line;
284 if(havePosition) line << "POSITION";
285 if(haveTitle) line << "TITLE";
286 if(haveArtist) line << "ARTIST";
287 if(haveAlbum) line << "ALBUM";
288 if(haveGenre) line << "GENRE";
289 if(haveYear) line << "YEAR";
290 if(haveComment) line << "COMMENT";
292 if(file.write(line.join(";").append("\r\n").toUtf8().prepend("\xef\xbb\xbf")) < 1)
294 file.close();
295 return CsvError_FileWrite;
299 for(int i = 0; i < nFiles; i++)
301 QStringList line;
302 const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
303 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
305 if(havePosition) line << QString::number(metaInfo.position());
306 if(haveTitle) line << metaInfo.title().trimmed();
307 if(haveArtist) line << metaInfo.artist().trimmed();
308 if(haveAlbum) line << metaInfo.album().trimmed();
309 if(haveGenre) line << metaInfo.genre().trimmed();
310 if(haveYear) line << QString::number(metaInfo.year());
311 if(haveComment) line << metaInfo.comment().trimmed();
313 if(file.write(line.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
315 file.close();
316 return CsvError_FileWrite;
320 file.close();
321 return CsvError_OK;
324 int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
326 QFile file(inFile);
327 if(!file.open(QIODevice::ReadOnly))
329 return CsvError_FileOpen;
332 QTextCodec *codec = NULL;
333 QByteArray bomCheck = file.peek(16);
335 if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf"))
337 codec = QTextCodec::codecForName("UTF-8");
339 else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe"))
341 codec = QTextCodec::codecForName("UTF-16LE");
343 else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff"))
345 codec = QTextCodec::codecForName("UTF-16BE");
347 else
349 const QString systemDefault = tr("(System Default)");
351 QStringList codecList;
352 codecList.append(systemDefault);
353 codecList.append(lamexp_available_codepages());
355 QInputDialog *input = new QInputDialog(parent);
356 input->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:")));
357 input->setOkButtonText(tr("OK"));
358 input->setCancelButtonText(tr("Cancel"));
359 input->setTextEchoMode(QLineEdit::Normal);
360 input->setComboBoxItems(codecList);
362 if(input->exec() < 1)
364 LAMEXP_DELETE(input);
365 return CsvError_Aborted;
368 if(input->textValue().compare(systemDefault, Qt::CaseInsensitive))
370 qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData());
371 codec = QTextCodec::codecForName(input->textValue().toLatin1().constData());
373 else
375 qDebug("Going to use the system's default codec!");
376 codec = QTextCodec::codecForName("System");
379 LAMEXP_DELETE(input);
382 bomCheck.clear();
384 //----------------------//
386 QTextStream stream(&file);
387 stream.setAutoDetectUnicode(false);
388 stream.setCodec(codec);
390 QString headerLine = stream.readLine().simplified();
392 while(headerLine.isEmpty())
394 if(stream.atEnd())
396 qWarning("The file appears to be empty!");
397 return CsvError_FileRead;
399 qWarning("Skipping a blank line at beginning of CSV file!");
400 headerLine = stream.readLine().simplified();
403 QStringList header = headerLine.split(";", QString::KeepEmptyParts);
405 const int nCols = header.count();
406 const int nFiles = m_fileList.count();
408 if(nCols < 1)
410 qWarning("Header appears to be empty!");
411 return CsvError_FileRead;
414 bool *ignore = new bool[nCols];
415 memset(ignore, 0, sizeof(bool) * nCols);
417 for(int i = 0; i < nCols; i++)
419 if((header[i] = header[i].trimmed()).isEmpty())
421 ignore[i] = true;
425 //----------------------//
427 for(int i = 0; i < nFiles; i++)
429 if(stream.atEnd())
431 LAMEXP_DELETE_ARRAY(ignore);
432 return CsvError_Incomplete;
435 QString line = stream.readLine().simplified();
437 if(line.isEmpty())
439 qWarning("Skipping a blank line in CSV file!");
440 continue;
443 QStringList data = line.split(";", QString::KeepEmptyParts);
445 if(data.count() < header.count())
447 qWarning("Skipping an incomplete line in CSV file!");
448 continue;
451 const QString key = m_fileList[i];
453 for(int j = 0; j < nCols; j++)
455 if(ignore[j])
457 continue;
459 else if(CHECK_HDR(header.at(j), "POSITION"))
461 bool ok = false;
462 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
463 if(ok) m_fileStore[key].metaInfo().setPosition(temp);
465 else if(CHECK_HDR(header.at(j), "TITLE"))
467 QString temp = data.at(j).trimmed();
468 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp);
470 else if(CHECK_HDR(header.at(j), "ARTIST"))
472 QString temp = data.at(j).trimmed();
473 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp);
475 else if(CHECK_HDR(header.at(j), "ALBUM"))
477 QString temp = data.at(j).trimmed();
478 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp);
480 else if(CHECK_HDR(header.at(j), "GENRE"))
482 QString temp = data.at(j).trimmed();
483 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp);
485 else if(CHECK_HDR(header.at(j), "YEAR"))
487 bool ok = false;
488 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
489 if(ok) m_fileStore[key].metaInfo().setYear(temp);
491 else if(CHECK_HDR(header.at(j), "COMMENT"))
493 QString temp = data.at(j).trimmed();
494 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp);
496 else
498 qWarning("Unkonw field '%s' will be ignored!", QUTF8(header.at(j)));
499 ignore[j] = true;
501 if(!checkArray(ignore, false, nCols))
503 qWarning("No known fields left, aborting!");
504 return CsvError_NoTags;
510 //----------------------//
512 LAMEXP_DELETE_ARRAY(ignore);
513 return CsvError_OK;
516 bool FileListModel::checkArray(const bool *a, const bool val, size_t len)
518 for(size_t i = 0; i < len; i++)
520 if(a[i] == val) return true;
523 return false;