TTKMusicPlayer  4.3.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 #include "ttkregularexpression.h"
3 
7 {
8 
9 }
10 
11 bool MusicM3UConfigManager::readBuffer(MusicSongItemList &items)
12 {
13  MusicSongItem item;
14  item.m_itemName = QFileInfo(m_file.fileName()).baseName();
15 
16  const QStringList data(QString(m_file.readAll()).split(TTK_LINEFEED));
17  if(data.isEmpty())
18  {
19  return false;
20  }
21 
22  int length = 0;
23  bool valid = false;
24  static TTKRegularExpression regx("#EXTINF:(-{0,1}\\d+),(.*)");
25 
26  for(QString str : qAsConst(data))
27  {
28  str = str.trimmed();
29  if(str.startsWith("#EXTM3U") || str.isEmpty())
30  {
31  continue;
32  }
33 
34  if(regx.match(str) > -1)
35  {
36  length = regx.captured(1).toInt();
37  valid = true;
38  }
39 
40  if(str.startsWith("#"))
41  {
42  continue;
43  }
44 
45  if(valid)
46  {
47  item.m_songs << MusicSong(str, TTKTime::formatDuration(length * TTK_DN_S2MS));
48  }
49  }
50 
51  m_file.close();
52 
53  if(!item.m_songs.isEmpty())
54  {
55  items << item;
56  }
57  return true;
58 }
59 
60 bool MusicM3UConfigManager::writeBuffer(const MusicSongItemList &items)
61 {
62  if(items.isEmpty())
63  {
64  return false;
65  }
66 
67  QStringList data;
68  data << QString("#EXTM3U");
69 
70  for(int i = 0; i < items.count(); ++i)
71  {
72  const MusicSongItem &item = items[i];
73  for(const MusicSong &song : qAsConst(item.m_songs))
74  {
75  data.append(QString("#EXTINF:%1,%2 - %3").arg(TTKTime::formatDuration(song.duration()) / TTK_DN_S2MS).arg(song.artist(), song.title()));
76  data.append(song.path());
77  }
78  }
79 
80  m_file.write(data.join(TTK_LINEFEED).toUtf8());
81  m_file.close();
82  return true;
83 }
QString captured(int index) const
#define qAsConst
Definition: ttkqtglobal.h:57
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:355
The class of the music song item.
Definition: musicsong.h:160
The class of the regular expression.
MusicSongList m_songs
Definition: musicsong.h:166
QString m_itemName
Definition: musicsong.h:164
#define TTK_LINEFEED
Definition: ttkglobal.h:271
The class of the music song info.
Definition: musicsong.h:28
int match(const QString &str, int pos=0)
virtual bool readBuffer(MusicSongItemList &items) overridefinal
The class of the ttk file interface.
static qint64 formatDuration(const QString &time) noexcept
Definition: ttktime.cpp:123