TTKMusicPlayer  3.7.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/parser.h"
4 #include "qjson/serializer.h"
5 
9 {
10 
11 }
12 
13 bool MusicJSPFConfigManager::readBuffer(MusicSongItemList &items)
14 {
15  MusicSongItem item;
16  item.m_itemName = QFileInfo(m_file.fileName()).baseName();
17 
18  QJson::Parser json;
19  bool ok = false;
20  const QVariant &data = json.parse(m_file.readAll(), &ok);
21  if(!ok)
22  {
23  return false;
24  }
25 
26  QVariantMap value = data.toMap();
27  const QVariantList &datas = value["trackList"].toList();
28  for(const QVariant &var : qAsConst(datas))
29  {
30  if(var.isNull())
31  {
32  continue;
33  }
34 
35  value = var.toMap();
36  value = value["track"].toMap();
37 
38  const QString &path = value["location"].toString();
39  if(!path.isEmpty())
40  {
41  item.m_songs << MusicSong(path, value["duration"].toString());
42  }
43  }
44 
45  m_file.close();
46 
47  if(!item.m_songs.isEmpty())
48  {
49  items << item;
50  }
51  return true;
52 }
53 
54 bool MusicJSPFConfigManager::writeBuffer(const MusicSongItemList &items)
55 {
56  if(items.isEmpty())
57  {
58  return false;
59  }
60 
61  QVariantMap datas;
62  for(int i = 0; i < items.count(); ++i)
63  {
64  QVariantList tracks;
65  const MusicSongItem &item = items[i];
66  for(const MusicSong &song : qAsConst(item.m_songs))
67  {
68  QVariantMap meta;
69  meta["location"] = song.path();
70  meta["title"] = song.title();
71  meta["creator"] = song.artist();
72  meta["duration"] = song.duration();
73  meta["album"] = QString();
74  meta["trackNum"] = QString();
75  meta["year"] = QString();
76 
77  QVariantMap track;
78  track["track"] = meta;
79  tracks.push_back(track);
80  }
81 
82  if(!tracks.isEmpty())
83  {
84  datas["trackList"] = tracks;
85  }
86  }
87 
88  QJson::Serializer json;
89  bool ok = false;
90  const QByteArray &output = json.serialize(datas, &ok);
91  if(!ok)
92  {
93  return false;
94  }
95 
96  m_file.write(output);
97  m_file.close();
98  return true;
99 }
virtual bool writeBuffer(const MusicSongItemList &items) overridefinal
Main class used to convert QVariant objects to JSON data.
Definition: serializer.h:151
#define qAsConst
Definition: ttkqtglobal.h:53
virtual bool readBuffer(MusicSongItemList &items) overridefinal
QVariant parse(QIODevice *io, bool *ok=0)
Read JSON string from the I/O Device and converts it to a QVariant object.
Definition: parser.cpp:69
The class of the read write interface.
The class of the music song item.
Definition: musicsong.h:161
TTK_MODULE_EXPORT QString toString(Record type)
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
Main class used to convert JSON data to QVariant objects.
Definition: parser.h:40
The class of the ttk file interface.
void serialize(const QVariant &variant, QIODevice *out, bool *ok)
This method generates a textual JSON representation and outputs it to the passed in I/O Device...
Definition: serializer.cpp:420