TTKMusicPlayer  3.7.0.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("\n"));
16  if(data.isEmpty())
17  {
18  return false;
19  }
20 
21  if(!data.takeFirst().toLower().contains("[playlist]"))
22  {
23  return false;
24  }
25 
26  const QRegExp regx1("^File(\\d+)=(.+)");
27  const QRegExp regx2("^Length(\\d+)=(-{0,1}\\d+)");
28 
29  int number = 0;
30  bool error = false;
31 
32  for(const QString &line : qAsConst(data))
33  {
34  if(regx1.indexIn(line) > -1)
35  {
36  if((number = regx1.cap(1).toInt()) > 0)
37  {
38  item.m_songs << MusicSong(regx1.cap(2));
39  }
40  else
41  {
42  error = true;
43  }
44  }
45  else if(regx2.indexIn(line) > -1)
46  {
47  if((number = regx2.cap(1).toInt()) > 0)
48  {
49  item.m_songs.back().setDuration(TTKTime::formatDuration(regx2.cap(2).toInt() * TTK_DN_S2MS));
50  }
51  else
52  {
53  error = true;
54  }
55  }
56 
57  if(error)
58  {
59  TTK_ERROR_STREAM("read pls format playlist error");
60  break;
61  }
62  }
63 
64  m_file.close();
65 
66  if(!item.m_songs.isEmpty())
67  {
68  items << item;
69  }
70  return true;
71 }
72 
73 bool MusicPLSConfigManager::writeBuffer(const MusicSongItemList &items)
74 {
75  if(items.isEmpty())
76  {
77  return false;
78  }
79 
80  QStringList data;
81  data << QString("[playlist]");
82 
83  int count = 0;
84  for(int i = 0; i < items.count(); ++i)
85  {
86  const MusicSongItem &item = items[i];
87  for(const MusicSong &song : qAsConst(item.m_songs))
88  {
89  ++count;
90  data << QString("File%1=%2").arg(count).arg(song.path());
91  data << QString("Title%1=%2").arg(count).arg(song.name());
92  data << QString("Length%1=%2").arg(count).arg(TTKTime::formatDuration(song.duration()) / 1000);
93  }
94  }
95 
96  data << "NumberOfEntries=" + QString::number(count);
97  data << "Version=2";
98 
99  m_file.write(data.join("\n").toUtf8());
100  m_file.close();
101  return true;
102 }
virtual bool readBuffer(MusicSongItemList &items) overridefinal
virtual bool writeBuffer(const MusicSongItemList &items) overridefinal
#define qAsConst
Definition: ttkqtglobal.h:53
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:276
The class of the music song item.
Definition: musicsong.h:161
MusicSongList m_songs
Definition: musicsong.h:166
QString m_itemName
Definition: musicsong.h:164
The class of the music song info.
Definition: musicsong.h:28
#define TTK_ERROR_STREAM(msg)
Definition: ttklogger.h:69
The class of the ttk file interface.
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123