TTKMusicPlayer  4.1.3.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicplsconfigmanager.cpp
Go to the documentation of this file.
2 
6 {
7 
8 }
9 
10 bool MusicPLSConfigManager::readBuffer(MusicSongItemList &items)
11 {
12  MusicSongItem item;
13  item.m_itemName = QFileInfo(m_file.fileName()).baseName();
14 
15  QStringList data(QString(m_file.readAll()).split(TTK_LINEFEED));
16  if(data.isEmpty())
17  {
18  return false;
19  }
20 
21  if(!data.takeFirst().contains("[playlist]", Qt::CaseInsensitive))
22  {
23  return false;
24  }
25 
26  const QRegExp regx1("^File(\\d+)=(.+)");
27  const QRegExp regx2("^Length(\\d+)=(-{0,1}\\d+)");
28 
29  bool error = false;
30 
31  for(const QString &line : qAsConst(data))
32  {
33  if(regx1.indexIn(line) > -1)
34  {
35  if(regx1.cap(1).toInt() > 0)
36  {
37  item.m_songs << MusicSong(regx1.cap(2));
38  }
39  else
40  {
41  error = true;
42  }
43  }
44  else if(regx2.indexIn(line) > -1)
45  {
46  if(regx2.cap(1).toInt() > 0)
47  {
48  item.m_songs.back().setDuration(TTKTime::formatDuration(regx2.cap(2).toInt() * TTK_DN_S2MS));
49  }
50  else
51  {
52  error = true;
53  }
54  }
55 
56  if(error)
57  {
58  TTK_ERROR_STREAM("read pls format playlist error");
59  break;
60  }
61  }
62 
63  m_file.close();
64 
65  if(!item.m_songs.isEmpty())
66  {
67  items << item;
68  }
69  return true;
70 }
71 
72 bool MusicPLSConfigManager::writeBuffer(const MusicSongItemList &items)
73 {
74  if(items.isEmpty())
75  {
76  return false;
77  }
78 
79  QStringList data;
80  data << QString("[playlist]");
81 
82  int count = 0;
83  for(int i = 0; i < items.count(); ++i)
84  {
85  const MusicSongItem &item = items[i];
86  for(const MusicSong &song : qAsConst(item.m_songs))
87  {
88  ++count;
89  data << QString("File%1=%2").arg(count).arg(song.path());
90  data << QString("Title%1=%2").arg(count).arg(song.name());
91  data << QString("Length%1=%2").arg(count).arg(TTKTime::formatDuration(song.duration()) / 1000);
92  }
93  }
94 
95  data << "NumberOfEntries=" + QString::number(count);
96  data << "Version=2";
97 
98  m_file.write(data.join(TTK_LINEFEED).toUtf8());
99  m_file.close();
100  return true;
101 }
virtual bool readBuffer(MusicSongItemList &items) overridefinal
virtual bool writeBuffer(const MusicSongItemList &items) overridefinal
#define qAsConst
Definition: ttkqtglobal.h:51
TTK_MODULE_EXPORT QStringList split(const QString &value, const QString &key=TTK_DEFAULT_STR)
The class of the read write interface.
#define TTK_DN_S2MS
Definition: ttkglobal.h:281
The class of the music song item.
Definition: musicsong.h:161
MusicSongList m_songs
Definition: musicsong.h:167
QString m_itemName
Definition: musicsong.h:165
#define TTK_LINEFEED
Definition: ttkglobal.h:198
The class of the music song info.
Definition: musicsong.h:28
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:70
The class of the ttk file interface.
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123