TTKMusicPlayer  4.2.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicjspfconfigmanager.cpp
Go to the documentation of this file.
2 
3 #include "qjson/json.h"
4 
8 {
9 
10 }
11 
12 bool MusicJSPFConfigManager::readBuffer(MusicSongItemList &items)
13 {
14  MusicSongItem item;
15  item.m_itemName = QFileInfo(m_file.fileName()).baseName();
16 
17  QJsonParseError ok;
18  const QJsonDocument &json = QJsonDocument::fromJson(m_file.readAll(), &ok);
20  {
21  return false;
22  }
23 
24  QVariantMap value = json.toVariant().toMap();
25  const QVariantList &datas = value["trackList"].toList();
26  for(const QVariant &var : qAsConst(datas))
27  {
28  if(var.isNull())
29  {
30  continue;
31  }
32 
33  value = var.toMap();
34  value = value["track"].toMap();
35 
36  const QString &path = value["location"].toString();
37  if(!path.isEmpty())
38  {
39  item.m_songs << MusicSong(path, value["duration"].toString());
40  }
41  }
42 
43  m_file.close();
44 
45  if(!item.m_songs.isEmpty())
46  {
47  items << item;
48  }
49  return true;
50 }
51 
52 bool MusicJSPFConfigManager::writeBuffer(const MusicSongItemList &items)
53 {
54  if(items.isEmpty())
55  {
56  return false;
57  }
58 
59  QVariantMap datas;
60  for(int i = 0; i < items.count(); ++i)
61  {
62  QVariantList tracks;
63  const MusicSongItem &item = items[i];
64  for(const MusicSong &song : qAsConst(item.m_songs))
65  {
66  QVariantMap meta;
67  meta["location"] = song.path();
68  meta["title"] = song.title();
69  meta["creator"] = song.artist();
70  meta["duration"] = song.duration();
71  meta["album"] = QString();
72  meta["trackNum"] = QString();
73  meta["year"] = QString();
74 
75  QVariantMap track;
76  track["track"] = meta;
77  tracks.push_back(track);
78  }
79 
80  if(!tracks.isEmpty())
81  {
82  datas["trackList"] = tracks;
83  }
84  }
85 
86  const QJsonDocument &json = QJsonDocument::fromVariant(datas);
87 #if TTK_QT_VERSION_CHECK(5,0,0) && !TTK_QT_VERSION_CHECK(5,1,0)
88  m_file.write(json.toJson());
89 #else
91 #endif
92  m_file.close();
93  return true;
94 }
static QJsonDocument fromVariant(const QVariant &variant)
virtual bool writeBuffer(const MusicSongItemList &items) overridefinal
QVariant toVariant() const
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=0)
ParseError error
#define qAsConst
Definition: ttkqtglobal.h:57
virtual bool readBuffer(MusicSongItemList &items) overridefinal
The class of the read write interface.
TTK_MODULE_EXPORT QString toString(Record type) noexcept
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
The class of the music song info.
Definition: musicsong.h:28
QByteArray toJson(JsonFormat format=Indented) const
The class of the ttk file interface.