TTKMusicPlayer  4.1.3.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 
8 #include <qmmp/trackinfo.h>
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_WSEPARATOR, 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  return MusicFormats::isTrack(path) ? TrackInfo::pathFromUrl(path) : path;
103 }
104 
105 QString TTK::generateSongName(const QString &title, const QString &artist)
106 {
107  return (title.isEmpty() || artist.isEmpty()) ? artist + title : artist + " - " + title;
108 }
109 
110 QString TTK::generateSongTitle(const QString &name, const QString &key)
111 {
112  const QStringList &s = TTK::String::split(name);
113  if(s.count() >= 2)
114  {
115  const int index = name.indexOf(key) + 1;
116  return name.right(name.length() - index).trimmed();
117  }
118  return name;
119 }
120 
121 QString TTK::generateSongArtist(const QString &name, const QString &key)
122 {
123  const QStringList &s = TTK::String::split(name);
124  if(s.count() >= 2)
125  {
126  const int index = name.indexOf(key);
127  return name.left(index).trimmed();
128  }
129  return name;
130 }
131 
132 MusicSongList TTK::generateSongList(const QString &path)
133 {
134  MusicSongList songs;
135  MusicSongMeta meta;
136 
137  if(!meta.read(path))
138  {
139  return songs;
140  }
141 
142  const int size = meta.songMetaCount();
143  for(int i = 0; i < size; ++i)
144  {
145  meta.setSongMetaIndex(i);
146 
147  QString name;
149  {
150  name = TTK::generateSongName(meta.title(), meta.artist());
151  }
152 
153  const QString &path = meta.fileBasePath();
154  songs << MusicSong(path, meta.duration(), name, MusicFormats::isTrack(path));
155  }
156 
157  if(!songs.isEmpty() && !meta.lyrics().isEmpty())
158  {
159  QFile file(TTK::String::lrcDirPrefix() + songs.back().name() + LRC_FILE);
160  if(file.open(QIODevice::WriteOnly))
161  {
162  file.write(meta.lyrics().toUtf8());
163  file.close();
164  }
165  }
166  return songs;
167 }
168 
169 QString TTK::generateNetworkSongTime(const QString &path)
170 {
171  MusicSongMeta meta;
172  return meta.read(TTK::generateNetworkSongPath(path)) ? meta.duration() : TTK_DEFAULT_STR;
173 }
174 
175 QString TTK::generateNetworkSongPath(const QString &path)
176 {
177  QString v = path;
178  if(TTK::String::isNetworkUrl(path))
179  {
180  /*Replace network url path to local path*/
181  const QString &id = path.section("#", -1);
182  if(id != path)
183  {
184  const QString &cachePath = CACHE_DIR_FULL + id;
185  if(QFile::exists(cachePath))
186  {
187  v = cachePath;
188  }
189  }
190  }
191  return v;
192 }
TTK_MODULE_EXPORT QString generateSongName(const QString &title, const QString &artist)
Definition: musicsong.cpp:105
bool read(const QString &url)
TTK_MODULE_EXPORT QString generateSongArtist(const QString &name, const QString &key=TTK_DEFAULT_STR)
Definition: musicsong.cpp:121
void setSongMetaIndex(int index) noexcept
QString title() noexcept
#define CACHE_DIR_FULL
Definition: musicobject.h:132
TTK_MODULE_EXPORT bool playlistRowValid(int index)
Definition: musicsong.cpp:95
QString artist() noexcept
#define TTK_DEFAULT_STR
Definition: ttkglobal.h:203
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:110
TTK_MODULE_EXPORT QString sizeByteToLabel(qint64 size)
QString artist() const noexcept
Definition: musicsong.cpp:54
TTK_MODULE_EXPORT QString generateNetworkSongPath(const QString &path)
Definition: musicsong.cpp:175
const char * name
Definition: http_parser.c:458
#define TTK_SEPARATOR
Definition: ttkglobal.h:196
TTK_MODULE_EXPORT QString lrcDirPrefix()
TTK_MODULE_EXPORT QString generateNetworkSongTime(const QString &path)
Definition: musicsong.cpp:169
int songMetaCount() const noexcept
#define TTK_WSEPARATOR
Definition: ttkglobal.h:197
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
static QString pathFromUrl(const QString &url, int *track=nullptr)
#define MUSIC_NETWORK_LIST
#define MUSIC_LOVEST_LIST
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:189
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:65
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:132