TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicsong.cpp
Go to the documentation of this file.
1 #include "musicsong.h"
2 #include "musicnumberutils.h"
3 #include "musicsongmeta.h"
4 #include "musicformats.h"
5 #include "musicextractwrapper.h"
6 #include "musicsettingmanager.h"
7 
9 
11  : m_sort(Sort::ByFileName),
12  m_size(0),
13  m_addTime(-1),
14  m_sizeStr(TTK_DEFAULT_STR),
15  m_addTimeStr(TTK_DEFAULT_STR),
16  m_playCount(0),
17  m_name(TTK_DEFAULT_STR),
18  m_path(TTK_DEFAULT_STR),
19  m_format(TTK_DEFAULT_STR),
20  m_duration(TTK_DEFAULT_STR)
21 {
22 
23 }
24 
25 MusicSong::MusicSong(const QString &path, bool track) noexcept
26  : MusicSong(path, {}, {}, track)
27 {
28 
29 }
30 
31 MusicSong::MusicSong(const QString &path, const QString &duration, const QString &name, bool track) noexcept
32  : MusicSong()
33 {
34  m_path = path;
35  // replace windows \\ path to / path
36  m_path.replace(TTK_RSEPARATOR, TTK_SEPARATOR);
37 
38  const QFileInfo fin(!track ? m_path : TTK::trackRelatedPath(m_path));
39 
40  m_name = name.isEmpty() ? fin.completeBaseName() : name;
41  m_size = fin.size();
42  m_format = TTK_FILE_SUFFIX(fin);
43  m_addTime = fin.lastModified().toMSecsSinceEpoch();
44  m_duration = duration;
45  m_addTimeStr = QString::number(m_addTime);
46  m_sizeStr = TTK::Number::sizeByteToLabel(m_size);
47 }
48 
49 QString MusicSong::title() const noexcept
50 {
52 }
53 
54 QString MusicSong::artist() const noexcept
55 {
57 }
58 
59 bool MusicSong::operator== (const MusicSong &other) const noexcept
60 {
61  return m_path == other.m_path;
62 }
63 
64 bool MusicSong::operator< (const MusicSong &other) const noexcept
65 {
66  switch(m_sort)
67  {
68  case Sort::ByFileName: return m_name < other.m_name;
69  case Sort::BySinger: return artist() < other.artist();
70  case Sort::ByFileSize: return m_size < other.m_size;
71  case Sort::ByAddTime: return m_addTime < other.m_addTime;
72  case Sort::ByDuration: return m_duration < other.m_duration;
73  case Sort::ByPlayCount: return m_playCount < other.m_playCount;
74  default: break;
75  }
76  return false;
77 }
78 
79 bool MusicSong::operator> (const MusicSong &other) const noexcept
80 {
81  switch(m_sort)
82  {
83  case Sort::ByFileName: return m_name > other.m_name;
84  case Sort::BySinger: return artist() > other.artist();
85  case Sort::ByFileSize: return m_size > other.m_size;
86  case Sort::ByAddTime: return m_addTime > other.m_addTime;
87  case Sort::ByDuration: return m_duration > other.m_duration;
88  case Sort::ByPlayCount: return m_playCount > other.m_playCount;
89  default: break;
90  }
91  return false;
92 }
93 
94 
95 bool TTK::playlistRowValid(int index)
96 {
97  return index != MUSIC_LOVEST_LIST && index != MUSIC_NETWORK_LIST && index != MUSIC_RECENT_LIST;
98 }
99 
100 QString TTK::trackRelatedPath(const QString &path)
101 {
102  if(!MusicFormats::isTrack(path))
103  {
104  return path;
105  }
106 
107  QString url = path.section("://", -1);
108  url.remove(RegularExpression("#\\d+$"));
109  return url;
110 }
111 
112 QString TTK::generateSongName(const QString &title, const QString &artist)
113 {
114  return (title.isEmpty() || artist.isEmpty()) ? artist + title : artist + " - " + title;
115 }
116 
117 QString TTK::generateSongTitle(const QString &name, const QString &key)
118 {
119  const QStringList &s = TTK::String::split(name);
120  if(s.count() >= 2)
121  {
122  const int index = name.indexOf(key) + 1;
123  return name.right(name.length() - index).trimmed();
124  }
125  return name;
126 }
127 
128 QString TTK::generateSongArtist(const QString &name, const QString &key)
129 {
130  const QStringList &s = TTK::String::split(name);
131  if(s.count() >= 2)
132  {
133  const int index = name.indexOf(key);
134  return name.left(index).trimmed();
135  }
136  return name;
137 }
138 
139 MusicSongList TTK::generateSongList(const QString &path)
140 {
141  MusicSongList songs;
142  MusicSongMeta meta;
143 
144  if(!meta.read(path))
145  {
146  return songs;
147  }
148 
149  const int size = meta.songMetaCount();
150  for(int i = 0; i < size; ++i)
151  {
152  meta.setSongMetaIndex(i);
153 
154  QString name;
156  {
157  name = TTK::generateSongName(meta.title(), meta.artist());
158  }
159 
160  const QString &path = meta.fileBasePath();
161  songs << MusicSong(path, meta.duration(), name, MusicFormats::isTrack(path));
162  }
163 
164  if(!songs.isEmpty() && !meta.lyrics().isEmpty())
165  {
166  QFile file(TTK::String::lrcDirPrefix() + songs.back().name() + LRC_FILE);
167  if(file.open(QIODevice::WriteOnly))
168  {
169  file.write(meta.lyrics().toUtf8());
170  file.close();
171  }
172  }
173  return songs;
174 }
175 
176 QString TTK::generateNetworkSongTime(const QString &path)
177 {
178  MusicSongMeta meta;
179  return meta.read(TTK::generateNetworkSongPath(path)) ? meta.duration() : TTK_DEFAULT_STR;
180 }
181 
182 QString TTK::generateNetworkSongPath(const QString &path)
183 {
184  QString v = path;
185  if(TTK::String::isNetworkUrl(path))
186  {
187  /*Replace network url path to local path*/
188  const QString &id = path.section("#", -1);
189  if(id != path)
190  {
191  const QString &cachePath = CACHE_DIR_FULL + id;
192  if(QFile::exists(cachePath))
193  {
194  v = cachePath;
195  }
196  }
197  }
198  return v;
199 }
TTK_MODULE_EXPORT QString generateSongName(const QString &title, const QString &artist)
Definition: musicsong.cpp:112
bool read(const QString &url)
TTK_MODULE_EXPORT QString generateSongArtist(const QString &name, const QString &key=TTK_DEFAULT_STR)
Definition: musicsong.cpp:128
void setSongMetaIndex(int index) noexcept
QString title() noexcept
#define CACHE_DIR_FULL
Definition: musicobject.h:130
TTK_MODULE_EXPORT bool playlistRowValid(int index)
Definition: musicsong.cpp:95
QString artist() noexcept
#define TTK_DEFAULT_STR
Definition: ttkglobal.h:200
TTK_MODULE_EXPORT QString trackRelatedPath(const QString &path)
Definition: musicsong.cpp:100
QString lyrics() noexcept
QString fileBasePath() noexcept
static constexpr wchar_t key[]
MusicSong() noexcept
Definition: musicsong.cpp:10
voidpf void uLong size
Definition: ioapi.h:136
#define MUSIC_RECENT_LIST
TTK_MODULE_EXPORT QString generateSongTitle(const QString &name, const QString &key=TTK_DEFAULT_STR)
Definition: musicsong.cpp:117
TTK_MODULE_EXPORT QString sizeByteToLabel(qint64 size)
#define TTK_RSEPARATOR
Definition: ttkglobal.h:196
QString artist() const noexcept
Definition: musicsong.cpp:54
TTK_MODULE_EXPORT QString generateNetworkSongPath(const QString &path)
Definition: musicsong.cpp:182
const char * name
Definition: http_parser.c:458
#define TTK_SEPARATOR
Definition: ttkglobal.h:195
TTK_MODULE_EXPORT QString lrcDirPrefix()
TTK_MODULE_EXPORT QString generateNetworkSongTime(const QString &path)
Definition: musicsong.cpp:176
int songMetaCount() const noexcept
TTK_MODULE_EXPORT QStringList split(const QString &value, const QString &key=TTK_DEFAULT_STR)
static bool isTrack(const QString &url)
Definition: musicformats.cpp:5
QString duration() noexcept
#define MUSIC_NETWORK_LIST
#define MUSIC_LOVEST_LIST
The class of the regular expression.
bool operator<(const MusicSong &other) const noexcept
Definition: musicsong.cpp:64
bool operator==(const MusicSong &other) const noexcept
Definition: musicsong.cpp:59
#define TTK_FILE_SUFFIX(fin)
Definition: ttkqtglobal.h:173
The class of the music song meta.
Definition: musicsongmeta.h:30
The class of the music song info.
Definition: musicsong.h:28
#define const
Definition: zconf.h:233
#define LRC_FILE
Definition: musicobject.h:63
QString title() const noexcept
Definition: musicsong.cpp:49
bool operator>(const MusicSong &other) const noexcept
Definition: musicsong.cpp:79
QString m_name
Definition: musicsong.h:133
TTK_MODULE_EXPORT bool isNetworkUrl(const QString &path)
#define G_SETTING_PTR
TTK_MODULE_EXPORT MusicSongList generateSongList(const QString &path)
Definition: musicsong.cpp:139