TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicm3uconfigmanager.cpp
Go to the documentation of this file.
2 
6 {
7 
8 }
9 
10 bool MusicM3UConfigManager::readBuffer(MusicSongItemList &items)
11 {
12  MusicSongItem item;
13  item.m_itemName = QFileInfo(m_file.fileName()).baseName();
14 
15  const QStringList data(QString(m_file.readAll()).split("\n"));
16  if(data.isEmpty())
17  {
18  return false;
19  }
20 
21  int length = 0;
22  bool valid = false;
23  const QRegExp regx("#EXTINF:(-{0,1}\\d+),(.*)");
24 
25  for(QString str : qAsConst(data))
26  {
27  str = str.trimmed();
28  if(str.startsWith("#EXTM3U") || str.isEmpty())
29  {
30  continue;
31  }
32 
33  if(regx.indexIn(str) > -1)
34  {
35  length = regx.cap(1).toInt();
36  valid = true;
37  }
38 
39  if(str.startsWith("#"))
40  {
41  continue;
42  }
43 
44  if(valid)
45  {
46  item.m_songs << MusicSong(str, TTKTime::formatDuration(length * TTK_DN_S2MS));
47  }
48  }
49 
50  m_file.close();
51 
52  if(!item.m_songs.isEmpty())
53  {
54  items << item;
55  }
56  return true;
57 }
58 
59 bool MusicM3UConfigManager::writeBuffer(const MusicSongItemList &items)
60 {
61  if(items.isEmpty())
62  {
63  return false;
64  }
65 
66  QStringList data;
67  data << QString("#EXTM3U");
68 
69  for(int i = 0; i < items.count(); ++i)
70  {
71  const MusicSongItem &item = items[i];
72  for(const MusicSong &song : qAsConst(item.m_songs))
73  {
74  data.append(QString("#EXTINF:%1,%2 - %3").arg(TTKTime::formatDuration(song.duration()) / TTK_DN_S2MS).arg(song.artist(), song.title()));
75  data.append(song.path());
76  }
77  }
78 
79  m_file.write(data.join("\n").toUtf8());
80  m_file.close();
81  return true;
82 }
#define qAsConst
Definition: ttkqtglobal.h:53
TTK_MODULE_EXPORT QStringList split(const QString &value, const QString &key=TTK_DEFAULT_STR)
virtual bool writeBuffer(const MusicSongItemList &items) overridefinal
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
virtual bool readBuffer(MusicSongItemList &items) overridefinal
The class of the ttk file interface.
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123