1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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, 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"
29 #include <MUtils/Global.h>
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 ////////////////////////////////////////////////////////////
44 // Constructor & Destructor
45 ////////////////////////////////////////////////////////////
47 FileListModel::FileListModel(void)
49 m_blockUpdates(false),
50 m_fileIcon(":/icons/page_white_cd.png")
54 FileListModel::~FileListModel(void)
58 ////////////////////////////////////////////////////////////
60 ////////////////////////////////////////////////////////////
62 int FileListModel::columnCount(const QModelIndex
&parent
) const
67 int FileListModel::rowCount(const QModelIndex
&parent
) const
69 return m_fileList
.count();
72 QVariant
FileListModel::data(const QModelIndex
&index
, int role
) const
74 if((role
== Qt::DisplayRole
|| role
== Qt::ToolTipRole
) && index
.row() < m_fileList
.count() && index
.row() >= 0)
76 switch(index
.column())
79 return m_fileStore
.value(m_fileList
.at(index
.row())).metaInfo().title();
82 return QDir::toNativeSeparators(m_fileStore
.value(m_fileList
.at(index
.row())).filePath());
89 else if(role
== Qt::DecorationRole
&& index
.column() == 0)
99 QVariant
FileListModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
101 if(role
== Qt::DisplayRole
)
103 if(orientation
== Qt::Horizontal
)
108 return QVariant(tr("Title"));
111 return QVariant(tr("Full Path"));
120 if(m_fileList
.count() < 10)
122 return QVariant(QString().sprintf("%d", section
+ 1));
124 else if(m_fileList
.count() < 100)
126 return QVariant(QString().sprintf("%02d", section
+ 1));
128 else if(m_fileList
.count() < 1000)
130 return QVariant(QString().sprintf("%03d", section
+ 1));
134 return QVariant(QString().sprintf("%04d", section
+ 1));
144 void FileListModel::addFile(const QString
&filePath
)
146 QFileInfo
fileInfo(filePath
);
147 const QString key
= MAKE_KEY(fileInfo
.canonicalFilePath());
148 const bool flag
= (!m_blockUpdates
);
150 if(!m_fileStore
.contains(key
))
152 AudioFileModel
audioFile(fileInfo
.canonicalFilePath());
153 audioFile
.metaInfo().setTitle(fileInfo
.baseName());
154 if(flag
) beginInsertRows(QModelIndex(), m_fileList
.count(), m_fileList
.count());
155 m_fileStore
.insert(key
, audioFile
);
156 m_fileList
.append(key
);
157 if(flag
) endInsertRows();
162 void FileListModel::addFile(const AudioFileModel
&file
)
164 const QString key
= MAKE_KEY(file
.filePath());
165 const bool flag
= (!m_blockUpdates
);
167 if(!m_fileStore
.contains(key
))
169 if(flag
) beginInsertRows(QModelIndex(), m_fileList
.count(), m_fileList
.count());
170 m_fileStore
.insert(key
, file
);
171 m_fileList
.append(key
);
172 if(flag
) endInsertRows();
177 bool FileListModel::removeFile(const QModelIndex
&index
)
179 if(index
.row() >= 0 && index
.row() < m_fileList
.count())
182 m_fileStore
.remove(m_fileList
.at(index
.row()));
183 m_fileList
.removeAt(index
.row());
193 void FileListModel::clearFiles(void)
201 bool FileListModel::moveFile(const QModelIndex
&index
, int delta
)
203 if(delta
!= 0 && index
.row() >= 0 && index
.row() < m_fileList
.count() && index
.row() + delta
>= 0 && index
.row() + delta
< m_fileList
.count())
206 m_fileList
.move(index
.row(), index
.row() + delta
);
216 const AudioFileModel
&FileListModel::getFile(const QModelIndex
&index
)
218 if(index
.row() >= 0 && index
.row() < m_fileList
.count())
220 return m_fileStore
[m_fileList
.at(index
.row())]; //return m_fileStore.value(m_fileList.at(index.row()));
224 return m_nullAudioFile
;
228 AudioFileModel
&FileListModel::operator[] (const QModelIndex
&index
)
230 const QString key
= m_fileList
.at(index
.row());
231 return m_fileStore
[key
];
234 bool FileListModel::setFile(const QModelIndex
&index
, const AudioFileModel
&audioFile
)
236 if(index
.row() >= 0 && index
.row() < m_fileList
.count())
238 const QString oldKey
= m_fileList
.at(index
.row());
239 const QString newKey
= MAKE_KEY(audioFile
.filePath());
242 m_fileList
.replace(index
.row(), newKey
);
243 m_fileStore
.remove(oldKey
);
244 m_fileStore
.insert(newKey
, audioFile
);
254 int FileListModel::exportToCsv(const QString
&outFile
)
256 const int nFiles
= m_fileList
.count();
258 bool havePosition
= false, haveTitle
= false, haveArtist
= false, haveAlbum
= false, haveGenre
= false, haveYear
= false, haveComment
= false;
260 for(int i
= 0; i
< nFiles
; i
++)
262 const AudioFileModel
¤t
= m_fileStore
.value(m_fileList
.at(i
));
263 const AudioFileModel_MetaInfo
&metaInfo
= current
.metaInfo();
265 if(metaInfo
.position() > 0) havePosition
= true;
266 if(!metaInfo
.title().isEmpty()) haveTitle
= true;
267 if(!metaInfo
.artist().isEmpty()) haveArtist
= true;
268 if(!metaInfo
.album().isEmpty()) haveAlbum
= true;
269 if(!metaInfo
.genre().isEmpty()) haveGenre
= true;
270 if(metaInfo
.year() > 0) haveYear
= true;
271 if(!metaInfo
.comment().isEmpty()) haveComment
= true;
274 if(!(haveTitle
|| haveArtist
|| haveAlbum
|| haveGenre
|| haveYear
|| haveComment
))
276 return CsvError_NoTags
;
281 if(!file
.open(QIODevice::WriteOnly
))
283 return CsvError_FileOpen
;
289 if(havePosition
) line
<< "POSITION";
290 if(haveTitle
) line
<< "TITLE";
291 if(haveArtist
) line
<< "ARTIST";
292 if(haveAlbum
) line
<< "ALBUM";
293 if(haveGenre
) line
<< "GENRE";
294 if(haveYear
) line
<< "YEAR";
295 if(haveComment
) line
<< "COMMENT";
297 if(file
.write(line
.join(";").append("\r\n").toUtf8().prepend("\xef\xbb\xbf")) < 1)
300 return CsvError_FileWrite
;
304 for(int i
= 0; i
< nFiles
; i
++)
307 const AudioFileModel
¤t
= m_fileStore
.value(m_fileList
.at(i
));
308 const AudioFileModel_MetaInfo
&metaInfo
= current
.metaInfo();
310 if(havePosition
) line
<< QString::number(metaInfo
.position());
311 if(haveTitle
) line
<< metaInfo
.title().trimmed();
312 if(haveArtist
) line
<< metaInfo
.artist().trimmed();
313 if(haveAlbum
) line
<< metaInfo
.album().trimmed();
314 if(haveGenre
) line
<< metaInfo
.genre().trimmed();
315 if(haveYear
) line
<< QString::number(metaInfo
.year());
316 if(haveComment
) line
<< metaInfo
.comment().trimmed();
318 if(file
.write(line
.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
321 return CsvError_FileWrite
;
329 int FileListModel::importFromCsv(QWidget
*parent
, const QString
&inFile
)
332 if(!file
.open(QIODevice::ReadOnly
))
334 return CsvError_FileOpen
;
337 QTextCodec
*codec
= NULL
;
338 QByteArray bomCheck
= file
.peek(16);
340 if((!bomCheck
.isEmpty()) && bomCheck
.startsWith("\xef\xbb\xbf"))
342 codec
= QTextCodec::codecForName("UTF-8");
344 else if((!bomCheck
.isEmpty()) && bomCheck
.startsWith("\xff\xfe"))
346 codec
= QTextCodec::codecForName("UTF-16LE");
348 else if((!bomCheck
.isEmpty()) && bomCheck
.startsWith("\xfe\xff"))
350 codec
= QTextCodec::codecForName("UTF-16BE");
354 const QString systemDefault
= tr("(System Default)");
356 QStringList codecList
;
357 codecList
.append(systemDefault
);
358 codecList
.append(MUtils::available_codepages());
360 QInputDialog
*input
= new QInputDialog(parent
);
361 input
->setLabelText(EXPAND(tr("Select ANSI Codepage for CSV file:")));
362 input
->setOkButtonText(tr("OK"));
363 input
->setCancelButtonText(tr("Cancel"));
364 input
->setTextEchoMode(QLineEdit::Normal
);
365 input
->setComboBoxItems(codecList
);
367 if(input
->exec() < 1)
369 MUTILS_DELETE(input
);
370 return CsvError_Aborted
;
373 if(input
->textValue().compare(systemDefault
, Qt::CaseInsensitive
))
375 qDebug("User-selected codec is: %s", input
->textValue().toLatin1().constData());
376 codec
= QTextCodec::codecForName(input
->textValue().toLatin1().constData());
380 qDebug("Going to use the system's default codec!");
381 codec
= QTextCodec::codecForName("System");
384 MUTILS_DELETE(input
);
389 //----------------------//
391 QTextStream
stream(&file
);
392 stream
.setAutoDetectUnicode(false);
393 stream
.setCodec(codec
);
395 QString headerLine
= stream
.readLine().simplified();
397 while(headerLine
.isEmpty())
401 qWarning("The file appears to be empty!");
402 return CsvError_FileRead
;
404 qWarning("Skipping a blank line at beginning of CSV file!");
405 headerLine
= stream
.readLine().simplified();
408 QStringList header
= headerLine
.split(";", QString::KeepEmptyParts
);
410 const int nCols
= header
.count();
411 const int nFiles
= m_fileList
.count();
415 qWarning("Header appears to be empty!");
416 return CsvError_FileRead
;
419 bool *ignore
= new bool[nCols
];
420 memset(ignore
, 0, sizeof(bool) * nCols
);
422 for(int i
= 0; i
< nCols
; i
++)
424 if((header
[i
] = header
[i
].trimmed()).isEmpty())
430 //----------------------//
432 for(int i
= 0; i
< nFiles
; i
++)
436 MUTILS_DELETE_ARRAY(ignore
);
437 return CsvError_Incomplete
;
440 QString line
= stream
.readLine().simplified();
444 qWarning("Skipping a blank line in CSV file!");
448 QStringList data
= line
.split(";", QString::KeepEmptyParts
);
450 if(data
.count() < header
.count())
452 qWarning("Skipping an incomplete line in CSV file!");
456 const QString key
= m_fileList
[i
];
458 for(int j
= 0; j
< nCols
; j
++)
464 else if(CHECK_HDR(header
.at(j
), "POSITION"))
467 unsigned int temp
= data
.at(j
).trimmed().toUInt(&ok
);
468 if(ok
) m_fileStore
[key
].metaInfo().setPosition(temp
);
470 else if(CHECK_HDR(header
.at(j
), "TITLE"))
472 QString temp
= data
.at(j
).trimmed();
473 if(!temp
.isEmpty()) m_fileStore
[key
].metaInfo().setTitle(temp
);
475 else if(CHECK_HDR(header
.at(j
), "ARTIST"))
477 QString temp
= data
.at(j
).trimmed();
478 if(!temp
.isEmpty()) m_fileStore
[key
].metaInfo().setArtist(temp
);
480 else if(CHECK_HDR(header
.at(j
), "ALBUM"))
482 QString temp
= data
.at(j
).trimmed();
483 if(!temp
.isEmpty()) m_fileStore
[key
].metaInfo().setAlbum(temp
);
485 else if(CHECK_HDR(header
.at(j
), "GENRE"))
487 QString temp
= data
.at(j
).trimmed();
488 if(!temp
.isEmpty()) m_fileStore
[key
].metaInfo().setGenre(temp
);
490 else if(CHECK_HDR(header
.at(j
), "YEAR"))
493 unsigned int temp
= data
.at(j
).trimmed().toUInt(&ok
);
494 if(ok
) m_fileStore
[key
].metaInfo().setYear(temp
);
496 else if(CHECK_HDR(header
.at(j
), "COMMENT"))
498 QString temp
= data
.at(j
).trimmed();
499 if(!temp
.isEmpty()) m_fileStore
[key
].metaInfo().setComment(temp
);
503 qWarning("Unkonw field '%s' will be ignored!", MUTILS_UTF8(header
.at(j
)));
506 if(!checkArray(ignore
, false, nCols
))
508 qWarning("No known fields left, aborting!");
509 return CsvError_NoTags
;
515 //----------------------//
517 MUTILS_DELETE_ARRAY(ignore
);
521 bool FileListModel::checkArray(const bool *a
, const bool val
, size_t len
)
523 for(size_t i
= 0; i
< len
; i
++)
525 if(a
[i
] == val
) return true;