1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 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 static inline int LOG10(int x
)
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 ////////////////////////////////////////////////////////////
70 ////////////////////////////////////////////////////////////
72 int FileListModel::columnCount(const QModelIndex
&parent
) const
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())
89 return m_fileStore
.value(m_fileList
.at(index
.row())).metaInfo().title();
92 return QDir::toNativeSeparators(m_fileStore
.value(m_fileList
.at(index
.row())).filePath());
99 else if((role
== Qt::DecorationRole
) && (index
.column() == 0))
109 QVariant
FileListModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
111 if(role
== Qt::DisplayRole
)
113 if(orientation
== Qt::Horizontal
)
118 return QVariant(tr("Title"));
121 return QVariant(tr("Full Path"));
130 return int2str(section
+ 1);
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();
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();
172 bool FileListModel::removeFile(const QModelIndex
&index
)
174 const int row
= index
.row();
175 if(row
>= 0 && row
< m_fileList
.count())
178 m_fileStore
.remove(m_fileList
.at(row
));
179 m_fileList
.removeAt(row
);
186 void FileListModel::clearFiles(void)
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())
199 m_fileList
.move(index
.row(), index
.row() + delta
);
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()));
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());
235 m_fileList
.replace(index
.row(), newKey
);
236 m_fileStore
.remove(oldKey
);
237 m_fileStore
.insert(newKey
, audioFile
);
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
¤t
= 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
;
274 if(!file
.open(QIODevice::WriteOnly
))
276 return CsvError_FileOpen
;
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)
293 return CsvError_FileWrite
;
297 for(int i
= 0; i
< nFiles
; i
++)
300 const AudioFileModel
¤t
= 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)
314 return CsvError_FileWrite
;
322 int FileListModel::importFromCsv(QWidget
*parent
, const QString
&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");
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());
373 qDebug("Going to use the system's default codec!");
374 codec
= QTextCodec::codecForName("System");
377 MUTILS_DELETE(input
);
382 //----------------------//
384 QTextStream
stream(&file
);
385 stream
.setAutoDetectUnicode(false);
386 stream
.setCodec(codec
);
388 QString headerLine
= stream
.readLine().simplified();
390 while(headerLine
.isEmpty())
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();
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())
423 //----------------------//
425 for(int i
= 0; i
< nFiles
; i
++)
429 MUTILS_DELETE_ARRAY(ignore
);
430 return CsvError_Incomplete
;
433 QString line
= stream
.readLine().simplified();
437 qWarning("Skipping a blank line in CSV file!");
441 QStringList data
= line
.split(";", QString::KeepEmptyParts
);
443 if(data
.count() < header
.count())
445 qWarning("Skipping an incomplete line in CSV file!");
449 const QString key
= m_fileList
[i
];
451 for(int j
= 0; j
< nCols
; j
++)
457 else if(CHECK_HDR(header
.at(j
), "POSITION"))
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"))
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
);
496 qWarning("Unkonw field '%s' will be ignored!", MUTILS_UTF8(header
.at(j
)));
499 if(!checkArray(ignore
, false, nCols
))
501 qWarning("No known fields left, aborting!");
502 return CsvError_NoTags
;
508 //----------------------//
510 MUTILS_DELETE_ARRAY(ignore
);
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;
524 QString
FileListModel::int2str(const int &value
) const
526 if(m_fileList
.count() < 10)
528 return QString().sprintf("%d", value
);
532 const QString format
= QString().sprintf("%%0%dd", LOG10(m_fileList
.count()));
533 return QString().sprintf(format
.toLatin1().constData(), value
);