OSDN Git Service

Make it possible to select *multiple* items on the source files tab (only contiguous...
[lamexp/LameXP.git] / src / Model_FileList.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2015 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.
10 //
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.
15 //
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.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Model_FileList.h"
24
25 //Internal
26 #include "Global.h"
27
28 //MUtils
29 #include <MUtils/Global.h>
30
31 //Qt
32 #include <QFileInfo>
33 #include <QDir>
34 #include <QFile>
35 #include <QTextCodec>
36 #include <QTextStream>
37 #include <QInputDialog>
38
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())
42
43 static inline int LOG10(int x)
44 {
45         int ret = 1;
46         while(x >= 10)
47         {
48                 ret++; x /= 10;
49         }
50         return ret;
51 }
52
53 ////////////////////////////////////////////////////////////
54 // Constructor & Destructor
55 ////////////////////////////////////////////////////////////
56
57 FileListModel::FileListModel(void)
58 :
59         m_blockUpdates(false),
60         m_fileIcon(":/icons/page_white_cd.png")
61 {
62 }
63
64 FileListModel::~FileListModel(void)
65 {
66 }
67
68 ////////////////////////////////////////////////////////////
69 // Public Functions
70 ////////////////////////////////////////////////////////////
71
72 int FileListModel::columnCount(const QModelIndex &parent) const
73 {
74         return 2;
75 }
76
77 int FileListModel::rowCount(const QModelIndex &parent) const
78 {
79         return m_fileList.count();
80 }
81
82 QVariant FileListModel::data(const QModelIndex &index, int role) const
83 {
84         if(((role == Qt::DisplayRole) || (role == Qt::ToolTipRole)) && (index.row() < m_fileList.count()) && (index.row() >= 0))
85         {
86                 switch(index.column())
87                 {
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;
97                 }               
98         }
99         else if((role == Qt::DecorationRole) && (index.column() == 0))
100         {
101                 return m_fileIcon;
102         }
103         else
104         {
105                 return QVariant();
106         }
107 }
108
109 QVariant FileListModel::headerData(int section, Qt::Orientation orientation, int role) const
110 {
111         if(role == Qt::DisplayRole)
112         {
113                 if(orientation == Qt::Horizontal)
114                 {
115                         switch(section)
116                         {
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;
126                         }
127                 }
128                 else
129                 {
130                         return int2str(section + 1);
131                 }
132         }
133         else
134         {
135                 return QVariant();
136         }
137 }
138
139 void FileListModel::addFile(const QString &filePath)
140 {
141         QFileInfo fileInfo(filePath);
142         const QString key = MAKE_KEY(fileInfo.canonicalFilePath()); 
143         const bool flag = (!m_blockUpdates);
144
145         if(!m_fileStore.contains(key))
146         {
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();
154         }
155 }
156
157 void FileListModel::addFile(const AudioFileModel &file)
158 {
159         const QString key = MAKE_KEY(file.filePath()); 
160         const bool flag = (!m_blockUpdates);
161
162         if(!m_fileStore.contains(key))
163         {
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();
169         }
170 }
171
172 bool FileListModel::removeFile(const QModelIndex &index)
173 {
174         const int row = index.row();
175         if(row >= 0 && row < m_fileList.count())
176         {
177                 beginResetModel();
178                 m_fileStore.remove(m_fileList.at(row));
179                 m_fileList.removeAt(row);
180                 endResetModel();
181                 return true;
182         }
183         return false;
184 }
185
186 void FileListModel::clearFiles(void)
187 {
188         beginResetModel();
189         m_fileList.clear();
190         m_fileStore.clear();
191         endResetModel();
192 }
193
194 bool FileListModel::moveFile(const QModelIndex &index, int delta)
195 {
196         if(delta != 0 && index.row() >= 0 && index.row() < m_fileList.count() && index.row() + delta >= 0 && index.row() + delta < m_fileList.count())
197         {
198                 beginResetModel();
199                 m_fileList.move(index.row(), index.row() + delta);
200                 endResetModel();
201                 return true;
202         }
203         else
204         {
205                 return false;
206         }
207 }
208
209 const AudioFileModel &FileListModel::getFile(const QModelIndex &index)
210 {
211         if(index.row() >= 0 && index.row() < m_fileList.count())
212         {
213                 return m_fileStore[m_fileList.at(index.row())];         //return m_fileStore.value(m_fileList.at(index.row()));
214         }
215         else
216         {
217                 return m_nullAudioFile;
218         }
219 }
220
221 AudioFileModel &FileListModel::operator[] (const QModelIndex &index)
222 {
223         const QString key = m_fileList.at(index.row());
224         return m_fileStore[key];
225 }
226
227 bool FileListModel::setFile(const QModelIndex &index, const AudioFileModel &audioFile)
228 {
229         if(index.row() >= 0 && index.row() < m_fileList.count())
230         {
231                 const QString oldKey = m_fileList.at(index.row());
232                 const QString newKey = MAKE_KEY(audioFile.filePath());
233                 
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;
240         }
241         else
242         {
243                 return false;
244         }
245 }
246
247 int FileListModel::exportToCsv(const QString &outFile)
248 {
249         const int nFiles = m_fileList.count();
250         
251         bool havePosition = false, haveTitle = false, haveArtist = false, haveAlbum = false, haveGenre = false, haveYear = false, haveComment = false;
252         
253         for(int i = 0; i < nFiles; i++)
254         {
255                 const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
256                 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
257                 
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;
265         }
266
267         if(!(haveTitle || haveArtist || haveAlbum || haveGenre || haveYear || haveComment))
268         {
269                 return CsvError_NoTags;
270         }
271
272         QFile file(outFile);
273
274         if(!file.open(QIODevice::WriteOnly))
275         {
276                 return CsvError_FileOpen;
277         }
278         else
279         {
280                 QStringList line;
281                 
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";
289
290                 if(file.write(line.join(";").append("\r\n").toUtf8().prepend("\xef\xbb\xbf")) < 1)
291                 {
292                         file.close();
293                         return CsvError_FileWrite;
294                 }
295         }
296
297         for(int i = 0; i < nFiles; i++)
298         {
299                 QStringList line;
300                 const AudioFileModel &current = m_fileStore.value(m_fileList.at(i));
301                 const AudioFileModel_MetaInfo &metaInfo = current.metaInfo();
302                 
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();
310
311                 if(file.write(line.replaceInStrings(";", ",").join(";").append("\r\n").toUtf8()) < 1)
312                 {
313                         file.close();
314                         return CsvError_FileWrite;
315                 }
316         }
317
318         file.close();
319         return CsvError_OK;
320 }
321
322 int FileListModel::importFromCsv(QWidget *parent, const QString &inFile)
323 {
324         QFile file(inFile);
325         if(!file.open(QIODevice::ReadOnly))
326         {
327                 return CsvError_FileOpen;
328         }
329
330         QTextCodec *codec = NULL;
331         QByteArray bomCheck = file.peek(16);
332
333         if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xef\xbb\xbf"))
334         {
335                 codec = QTextCodec::codecForName("UTF-8");
336         }
337         else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xff\xfe"))
338         {
339                 codec = QTextCodec::codecForName("UTF-16LE");
340         }
341         else if((!bomCheck.isEmpty()) && bomCheck.startsWith("\xfe\xff"))
342         {
343                 codec = QTextCodec::codecForName("UTF-16BE");
344         }
345         else
346         {
347                 const QString systemDefault = tr("(System Default)");
348
349                 QStringList codecList;
350                 codecList.append(systemDefault);
351                 codecList.append(MUtils::available_codepages());
352
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);
359         
360                 if(input->exec() < 1)
361                 {
362                         MUTILS_DELETE(input);
363                         return CsvError_Aborted;
364                 }
365         
366                 if(input->textValue().compare(systemDefault, Qt::CaseInsensitive))
367                 {
368                         qDebug("User-selected codec is: %s", input->textValue().toLatin1().constData());
369                         codec = QTextCodec::codecForName(input->textValue().toLatin1().constData());
370                 }
371                 else
372                 {
373                         qDebug("Going to use the system's default codec!");
374                         codec = QTextCodec::codecForName("System");
375                 }
376
377                 MUTILS_DELETE(input);
378         }
379
380         bomCheck.clear();
381
382         //----------------------//
383
384         QTextStream stream(&file);
385         stream.setAutoDetectUnicode(false);
386         stream.setCodec(codec);
387
388         QString headerLine = stream.readLine().simplified();
389
390         while(headerLine.isEmpty())
391         {
392                 if(stream.atEnd())
393                 {
394                         qWarning("The file appears to be empty!");
395                         return CsvError_FileRead;
396                 }
397                 qWarning("Skipping a blank line at beginning of CSV file!");
398                 headerLine = stream.readLine().simplified();
399         }
400
401         QStringList header = headerLine.split(";", QString::KeepEmptyParts);
402
403         const int nCols = header.count();
404         const int nFiles = m_fileList.count();
405
406         if(nCols < 1)
407         {
408                 qWarning("Header appears to be empty!");
409                 return CsvError_FileRead;
410         }
411
412         bool *ignore = new bool[nCols];
413         memset(ignore, 0, sizeof(bool) * nCols);
414
415         for(int i = 0; i < nCols; i++)
416         {
417                 if((header[i] = header[i].trimmed()).isEmpty())
418                 {
419                         ignore[i] = true;
420                 }
421         }
422
423         //----------------------//
424
425         for(int i = 0; i < nFiles; i++)
426         {
427                 if(stream.atEnd())
428                 {
429                         MUTILS_DELETE_ARRAY(ignore);
430                         return CsvError_Incomplete;
431                 }
432                 
433                 QString line = stream.readLine().simplified();
434                 
435                 if(line.isEmpty())
436                 {
437                         qWarning("Skipping a blank line in CSV file!");
438                         continue;
439                 }
440                 
441                 QStringList data = line.split(";", QString::KeepEmptyParts);
442
443                 if(data.count() < header.count())
444                 {
445                         qWarning("Skipping an incomplete line in CSV file!");
446                         continue;
447                 }
448
449                 const QString key = m_fileList[i];
450
451                 for(int j = 0; j < nCols; j++)
452                 {
453                         if(ignore[j])
454                         {
455                                 continue;
456                         }
457                         else if(CHECK_HDR(header.at(j), "POSITION"))
458                         {
459                                 bool ok = false;
460                                 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
461                                 if(ok) m_fileStore[key].metaInfo().setPosition(temp);
462                         }
463                         else if(CHECK_HDR(header.at(j), "TITLE"))
464                         {
465                                 QString temp = data.at(j).trimmed();
466                                 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setTitle(temp);
467                         }
468                         else if(CHECK_HDR(header.at(j), "ARTIST"))
469                         {
470                                 QString temp = data.at(j).trimmed();
471                                 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setArtist(temp);
472                         }
473                         else if(CHECK_HDR(header.at(j), "ALBUM"))
474                         {
475                                 QString temp = data.at(j).trimmed();
476                                 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setAlbum(temp);
477                         }
478                         else if(CHECK_HDR(header.at(j), "GENRE"))
479                         {
480                                 QString temp = data.at(j).trimmed();
481                                 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setGenre(temp);
482                         }
483                         else if(CHECK_HDR(header.at(j), "YEAR"))
484                         {
485                                 bool ok = false;
486                                 unsigned int temp = data.at(j).trimmed().toUInt(&ok);
487                                 if(ok) m_fileStore[key].metaInfo().setYear(temp);
488                         }
489                         else if(CHECK_HDR(header.at(j), "COMMENT"))
490                         {
491                                 QString temp = data.at(j).trimmed();
492                                 if(!temp.isEmpty()) m_fileStore[key].metaInfo().setComment(temp);
493                         }
494                         else
495                         {
496                                 qWarning("Unkonw field '%s' will be ignored!", MUTILS_UTF8(header.at(j)));
497                                 ignore[j] = true;
498                                 
499                                 if(!checkArray(ignore, false, nCols))
500                                 {
501                                         qWarning("No known fields left, aborting!");
502                                         return CsvError_NoTags;
503                                 }
504                         }
505                 }
506         }
507
508         //----------------------//
509
510         MUTILS_DELETE_ARRAY(ignore);
511         return CsvError_OK;
512 }
513
514 bool FileListModel::checkArray(const bool *a, const bool val, size_t len)
515 {
516         for(size_t i = 0; i < len; i++)
517         {
518                 if(a[i] == val) return true;
519         }
520
521         return false;
522 }
523
524 QString FileListModel::int2str(const int &value) const
525 {
526         if(m_fileList.count() < 10)
527         {
528                 return QString().sprintf("%d", value);
529         }
530         else
531         {
532                 const QString format = QString().sprintf("%%0%dd", LOG10(m_fileList.count()));
533                 return QString().sprintf(format.toLatin1().constData(), value);
534         }
535 }