TTKMusicPlayer  4.3.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 "musicsettingmanager.h"
6 
8  : m_sort(Sort::ByFileName),
9  m_size(0),
10  m_addTime(-1),
11  m_sizeStr(TTK_DEFAULT_STR),
12  m_addTimeStr(TTK_DEFAULT_STR),
13  m_playCount(0),
14  m_name(TTK_DEFAULT_STR),
15  m_path(TTK_DEFAULT_STR),
16  m_format(TTK_DEFAULT_STR),
17  m_duration(TTK_DEFAULT_STR)
18 {
19 
20 }
21 
22 MusicSong::MusicSong(const QString &path, bool track)
23  : MusicSong(path, {}, {}, track)
24 {
25 
26 }
27 
28 MusicSong::MusicSong(const QString &path, const QString &duration, const QString &name, bool track)
29  : MusicSong()
30 {
31  m_path = path;
32  // replace windows \\ path to / path
34 
35  const QFileInfo fin(!track ? m_path : TTK::trackRelatedPath(m_path));
36 
37  m_name = name.isEmpty() ? fin.completeBaseName() : name;
38  m_size = fin.size();
40  m_addTime = fin.lastModified().toMSecsSinceEpoch();
42  m_addTimeStr = QString::number(m_addTime);
44 }
45 
46 QString MusicSong::title() const noexcept
47 {
49 }
50 
51 QString MusicSong::artist() const noexcept
52 {
54 }
55 
56 bool MusicSong::operator== (const MusicSong &other) const noexcept
57 {
58  return m_path == other.m_path;
59 }
60 
61 bool MusicSong::operator< (const MusicSong &other) const noexcept
62 {
63  switch(m_sort)
64  {
65  case Sort::ByFileName: return m_name < other.m_name;
66  case Sort::BySinger: return artist() < other.artist();
67  case Sort::ByFileSize: return m_size < other.m_size;
68  case Sort::ByAddTime: return m_addTime < other.m_addTime;
69  case Sort::ByDuration: return m_duration < other.m_duration;
70  case Sort::ByPlayCount: return m_playCount < other.m_playCount;
71  default: break;
72  }
73  return false;
74 }
75 
76 bool MusicSong::operator> (const MusicSong &other) const noexcept
77 {
78  switch(m_sort)
79  {
80  case Sort::ByFileName: return m_name > other.m_name;
81  case Sort::BySinger: return artist() > other.artist();
82  case Sort::ByFileSize: return m_size > other.m_size;
83  case Sort::ByAddTime: return m_addTime > other.m_addTime;
84  case Sort::ByDuration: return m_duration > other.m_duration;
85  case Sort::ByPlayCount: return m_playCount > other.m_playCount;
86  default: break;
87  }
88  return false;
89 }
90 
91 
92 MusicSongList TTK::generateSongList(const QString &path)
93 {
94  MusicSongList songs;
95  MusicSongMeta meta;
96 
97  if(!meta.read(path))
98  {
99  return songs;
100  }
101 
102  const int size = meta.songMetaCount();
103  for(int i = 0; i < size; ++i)
104  {
105  meta.setSongMetaIndex(i);
106 
107  QString name;
109  {
110  name = TTK::generateSongName(meta.title(), meta.artist());
111  }
112 
113  const QString &path = meta.fileBasePath();
114  songs << MusicSong(path, meta.duration(), name, MusicFormats::isTrack(path));
115  }
116 
117  if(!songs.isEmpty() && !meta.lyrics().isEmpty())
118  {
119  QFile file(TTK::String::lrcDirPrefix() + songs.last().name() + LRC_FILE);
120  if(file.open(QIODevice::WriteOnly))
121  {
122  file.write(meta.lyrics().toUtf8());
123  file.close();
124  }
125  }
126  return songs;
127 }
QString m_format
Definition: musicsong.h:132
bool read(const QString &url)
TTK_MODULE_EXPORT QString generateSongArtist(const QString &name, const QString &key=TTK_DEFAULT_STR)
void setSongMetaIndex(int index) noexcept
QString title() noexcept
QString artist() noexcept
#define TTK_DEFAULT_STR
Definition: ttkglobal.h:276
TTK_MODULE_EXPORT QString trackRelatedPath(const QString &path)
QString path() const noexcept
Definition: musicsong.h:89
QString lyrics() noexcept
QString fileBasePath() noexcept
QString m_sizeStr
Definition: musicsong.h:130
static bool isTrack(const QString &url) noexcept
Definition: musicformats.cpp:5
MusicSong() noexcept
Definition: musicsong.cpp:7
voidpf void uLong size
Definition: ioapi.h:136
TTK_MODULE_EXPORT QString generateSongTitle(const QString &name, const QString &key=TTK_DEFAULT_STR)
TTK_MODULE_EXPORT QString sizeByteToLabel(qint64 size)
qint64 m_addTime
Definition: musicsong.h:129
QString artist() const noexcept
Definition: musicsong.cpp:51
QString m_addTimeStr
Definition: musicsong.h:130
const char * name
Definition: http_parser.c:458
QString m_path
Definition: musicsong.h:132
#define TTK_SEPARATOR
Definition: ttkglobal.h:269
TTK_MODULE_EXPORT QString lrcDirPrefix()
QString m_duration
Definition: musicsong.h:132
int songMetaCount() const noexcept
#define TTK_WSEPARATOR
Definition: ttkglobal.h:270
QString duration() const noexcept
Definition: musicsong.h:105
QString name() const noexcept
Definition: musicsong.h:81
QString duration() noexcept
bool operator<(const MusicSong &other) const noexcept
Definition: musicsong.cpp:61
bool operator==(const MusicSong &other) const noexcept
Definition: musicsong.cpp:56
#define TTK_FILE_SUFFIX(fin)
Definition: ttkqtglobal.h:185
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:76
QString title() const noexcept
Definition: musicsong.cpp:46
qint64 m_size
Definition: musicsong.h:129
bool operator>(const MusicSong &other) const noexcept
Definition: musicsong.cpp:76
TTK_MODULE_EXPORT QString generateSongName(const QString &title, const QString &artist) noexcept
QString m_name
Definition: musicsong.h:132
#define G_SETTING_PTR
TTK_MODULE_EXPORT MusicSongList generateSongList(const QString &path)
Definition: musicsong.cpp:92