MythTV  master
musicmetadata.h
Go to the documentation of this file.
1 #ifndef MUSICMETADATA_H_
2 #define MUSICMETADATA_H_
3 
4 // C/C++
5 #include <cstdint>
6 
7 // qt
8 #include <QStringList>
9 #include <QMap>
10 #include <QDateTime>
11 #include <QImage>
12 #include <QMetaType>
13 #include <QCoreApplication>
14 
15 // mythtv
16 #include "mythtypes.h"
17 #include "mythmetaexp.h"
18 #include "mthread.h"
19 #include "mythcorecontext.h"
20 
21 class AllMusic;
22 class AlbumArtImages;
23 class LyricsData;
24 class MetaIO;
25 
27 {
35 };
36 
38 {
39  public:
40  AlbumArtImage(void) :
41  m_filename(""), m_hostname(""), m_description("") {}
43  m_id(image->m_id), m_filename(image->m_filename),
44  m_hostname(image->m_hostname), m_imageType(image->m_imageType),
45  m_description(image->m_description), m_embedded(image->m_embedded) {}
46  int m_id {0};
47  QString m_filename;
48  QString m_hostname;
49  ImageType m_imageType {IT_UNKNOWN};
50  QString m_description;
51  bool m_embedded {false};
52 };
53 
54 using AlbumArtList = QList<AlbumArtImage*>;
55 
57 {
59  RT_CD = 1,
61 };
62 
63 #define METADATA_BITS_FOR_REPO 8
64 #define METADATA_REPO_SHIFT 24
65 #define METADATA_REPO_MASK 0xff000000
66 #define METADATA_ID_MASK 0x00ffffff
67 
68 #define ID_TO_ID(x) x & METADATA_ID_MASK;
69 #define ID_TO_REPO(x) x >> METADATA_REPO_SHIFT
70 
71 #define METADATA_INVALID_FILENAME "**NOT FOUND**"
72 
73 #define STREAMUPDATEURL "https://services.mythtv.org/music/data/?data=streams"
74 #define STREAMURLCOUNT 5
75 
76 using UrlList = QString[STREAMURLCOUNT];
77 
79 {
80  Q_DECLARE_TR_FUNCTIONS(MusicMetadata);
81 
82  public:
83 
84  using IdType = uint32_t;
85 
86  MusicMetadata(QString lfilename = "", QString lartist = "", QString lcompilation_artist = "",
87  QString lalbum = "", QString ltitle = "", QString lgenre = "",
88  int lyear = 0, int ltracknum = 0, int llength = 0, int lid = 0,
89  int lrating = 0, int lplaycount = 0, QDateTime llastplay = QDateTime(),
90  QDateTime ldateadded = QDateTime(), bool lcompilation = false, QString lformat = "")
91  : m_artist(lartist),
92  m_compilationArtist(lcompilation_artist),
93  m_album(lalbum),
94  m_title(ltitle),
95  m_genre(lgenre),
96  m_format(lformat),
97  m_year(lyear),
98  m_trackNum(ltracknum),
99  m_length(llength),
100  m_rating(lrating),
101  m_lastPlay(llastplay),
102  m_dateAdded(ldateadded),
103  m_playCount(lplaycount),
104  m_compilation(lcompilation),
105  m_id(lid),
106  m_filename(lfilename)
107  {
108  checkEmptyFields();
109  ensureSortFields();
110  }
111 
112  MusicMetadata(int lid, QString lbroadcaster, QString lchannel, QString ldescription, UrlList lurls, QString llogourl,
113  QString lgenre, QString lmetaformat, QString lcountry, QString llanguage, QString lformat);
114 
115  ~MusicMetadata();
116 
118  {
119  *this = other;
120  m_changed = false;
121  }
122 
123  MusicMetadata& operator=(const MusicMetadata &rhs);
124 
125  QString Artist() const { return m_artist; }
126  QString ArtistSort() const { return m_artistSort; }
127  void setArtist(const QString &lartist,
128  const QString &lartist_sort = nullptr)
129  {
130  m_artist = lartist;
131  m_artistSort = lartist_sort;
132  m_formattedArtist.clear(); m_formattedTitle.clear();
133  ensureSortFields();
134  }
135 
136  QString CompilationArtist() const { return m_compilationArtist; }
137  QString CompilationArtistSort() const { return m_compilationArtistSort; }
138  void setCompilationArtist(const QString &lcompilation_artist,
139  const QString &lcompilation_artist_sort = nullptr)
140  {
141  m_compilationArtist = lcompilation_artist;
142  m_compilationArtistSort = lcompilation_artist_sort;
143  m_formattedArtist.clear(); m_formattedTitle.clear();
144  ensureSortFields();
145  }
146 
147  QString Album() const { return m_album; }
148  QString AlbumSort() const { return m_albumSort; }
149  void setAlbum(const QString &lalbum,
150  const QString &lalbum_sort = nullptr)
151  {
152  m_album = lalbum;
153  m_albumSort = lalbum_sort;
154  m_formattedArtist.clear(); m_formattedTitle.clear();
155  ensureSortFields();
156  }
157 
158  QString Title() const { return m_title; }
159  QString TitleSort() const { return m_titleSort; }
160  void setTitle(const QString &ltitle,
161  const QString &ltitle_sort = nullptr)
162  {
163  m_title = ltitle;
164  m_titleSort = ltitle_sort;
165  ensureSortFields();
166  }
167 
168  QString FormatArtist();
169  QString FormatTitle();
170 
171  QString Genre() const { return m_genre; }
172  void setGenre(const QString &lgenre) { m_genre = lgenre; }
173 
174  void setDirectoryId(int ldirectoryid) { m_directoryId = ldirectoryid; }
175  int getDirectoryId();
176 
177  void setArtistId(int lartistid) { m_artistId = lartistid; }
178  int getArtistId();
179 
180  void setAlbumId(int lalbumid) { m_albumId = lalbumid; }
181  int getAlbumId();
182 
183  void setGenreId(int lgenreid) { m_genreId = lgenreid; }
184  int getGenreId();
185 
186  int Year() const { return m_year; }
187  void setYear(int lyear) { m_year = lyear; }
188 
189  int Track() const { return m_trackNum; }
190  void setTrack(int ltrack) { m_trackNum = ltrack; }
191 
192  int GetTrackCount() const { return m_trackCount; }
193  void setTrackCount(int ltrackcount) { m_trackCount = ltrackcount; }
194 
195  int Length() const { return m_length; }
196  void setLength(int llength) { m_length = llength; }
197 
198  int DiscNumber() const {return m_discNum;}
199  void setDiscNumber(int discnum) { m_discNum = discnum; }
200 
201  int DiscCount() const {return m_discCount;}
202  void setDiscCount(int disccount) { m_discCount = disccount; }
203 
204  int Playcount() const { return m_playCount; }
205  void setPlaycount(int lplaycount) { m_playCount = lplaycount; }
206 
207  IdType ID() const { return m_id; }
208  void setID(IdType lid) { m_id = lid; }
209  void setRepo(RepoType repo) { m_id = (m_id & METADATA_ID_MASK) | (repo << METADATA_REPO_SHIFT); }
210 
211  bool isCDTrack(void) const { return ID_TO_REPO(m_id) == RT_CD; }
212  bool isDBTrack(void) const { return ID_TO_REPO(m_id) == RT_Database; }
213  bool isRadio(void) const { return ID_TO_REPO(m_id) == RT_Radio; }
214 
215  QString Filename(bool find = true);
216  void setFilename(const QString &lfilename);
217  QString getLocalFilename(void);
218 
219  QString Hostname(void) { return m_hostname; }
220  void setHostname(const QString &host) { m_hostname = host; }
221 
222  uint64_t FileSize() const { return m_fileSize; }
223  void setFileSize(uint64_t lfilesize) { m_fileSize = lfilesize; }
224 
225  QString Format() const { return m_format; }
226  void setFormat(const QString &lformat) { m_format = lformat; }
227 
228  int Rating() const { return m_rating; }
229  void decRating();
230  void incRating();
231  void setRating(int lrating) { m_rating = lrating; }
232 
233  QDateTime LastPlay() const { return m_lastPlay; }
234  void setLastPlay();
235  void setLastPlay(const QDateTime& lastPlay);
236 
237  int PlayCount() const { return m_playCount; }
238  void incPlayCount();
239 
240  // track is part of a compilation album
241  bool Compilation() const { return m_compilation; }
242  void setCompilation(bool state)
243  {
244  m_compilation = state;
245  m_formattedArtist.clear();
246  m_formattedTitle.clear();
247  }
248  bool determineIfCompilation(bool cd = false);
249 
250  // for radio streams
251  void setBroadcaster(const QString &broadcaster) { m_broadcaster = broadcaster; }
252  QString Broadcaster(void) { return m_broadcaster; }
253 
254  void setChannel(const QString &channel) { m_channel = channel; }
255  QString Channel(void) { return m_channel; }
256 
257  void setDescription(const QString &description) { m_description = description; }
258  QString Description(void) { return m_description; }
259 
260  void setUrl(const QString &url, uint index = 0);
261  QString Url(uint index = 0);
262 
263  void setLogoUrl(const QString &logourl) { m_logoUrl = logourl; }
264  QString LogoUrl(void) { return m_logoUrl; }
265 
266  void setMetadataFormat(const QString &metaformat) { m_metaFormat = metaformat; }
267  QString MetadataFormat(void) { return m_metaFormat; }
268 
269  void setCountry(const QString &country) { m_country = country; }
270  QString Country(void) { return m_country; }
271 
272  void setLanguage(const QString &language) { m_language = language; }
273  QString Language(void) { return m_language; }
274 
275  void setEmbeddedAlbumArt(AlbumArtList &albumart);
276 
277  void reloadMetadata(void);
278  void dumpToDatabase(void);
279  void setField(const QString &field, const QString &data);
280  void getField(const QString& field, QString *data);
281  void toMap(InfoMap &metadataMap, const QString &prefix = "");
282 
283  void persist(void);
284 
285  bool hasChanged(void) const { return m_changed; }
286 
287  bool compare(MusicMetadata *mdata) const;
288 
289  // static functions
290  static MusicMetadata *createFromFilename(const QString &filename);
291  static MusicMetadata *createFromID(int trackid);
292  static void setArtistAndTrackFormats();
293  static QStringList fillFieldList(const QString& field);
294  static bool updateStreamList(void);
295 
296  // this looks for any image available - preferring a front cover if available
297  QString getAlbumArtFile(void);
298  // this looks only for the given image type
299  QString getAlbumArtFile(ImageType type);
300 
301  AlbumArtImages *getAlbumArtImages(void);
302  void reloadAlbumArtImages(void);
303 
304  LyricsData *getLyricsData(void);
305 
306  MetaIO *getTagger(void);
307 
308  private:
309  void setCompilationFormatting(bool cd = false);
310  QString formatReplaceSymbols(const QString &format);
311  void checkEmptyFields(void);
312  void ensureSortFields(void);
313  void saveHostname(void);
314 
315  QString m_artist;
316  QString m_artistSort;
319  QString m_album;
320  QString m_albumSort;
321  QString m_title;
322  QString m_titleSort;
325  QString m_genre;
326  QString m_format;
327  int m_year {0};
328  int m_trackNum {0};
329  int m_trackCount {0};
330  int m_discNum {0};
331  int m_discCount {0};
332  int m_length {0};
333  int m_rating {0};
334  int m_directoryId {-1};
335  int m_artistId {-1};
336  int m_compartistId {-1};
337  int m_albumId {-1};
338  int m_genreId {-1};
339  QDateTime m_lastPlay;
340  QDateTime m_tempLastPlay;
341  QDateTime m_dateAdded;
342  int m_playCount {0};
343  int m_tempPlayCount {0};
344  bool m_compilation {false};
345 
346  AlbumArtImages *m_albumArt {nullptr};
347 
348  LyricsData *m_lyricsData {nullptr};
349 
351  QString m_filename; // file name as stored in the DB
352  QString m_hostname; // host where file is located as stored in the DB
353  QString m_actualFilename; // actual URL of the file if found
354  uint64_t m_fileSize {0};
355  bool m_changed {false};
356 
357  // radio stream stuff
358  QString m_broadcaster;
359  QString m_channel;
360  QString m_description;
362  QString m_logoUrl;
363  QString m_metaFormat;
364  QString m_country;
365  QString m_language;
366 
367  // Various formatting strings
368  static QString s_formatNormalFileArtist;
369  static QString s_formatNormalFileTrack;
370  static QString s_formatNormalCdArtist;
371  static QString s_formatNormalCdTrack;
372 
377 };
378 
381 
384 
388 
389 //---------------------------------------------------------------------------
390 
392 {
393 
394  public:
395 
396  explicit MetadataLoadingThread(AllMusic *parent_ptr)
397  : MThread("MetadataLoading"), m_parent(parent_ptr) {}
398  void run() override; // MThread
399 
400  private:
401 
402  AllMusic *m_parent {nullptr};
403 };
404 
405 //---------------------------------------------------------------------------
406 
408 {
409  Q_DECLARE_TR_FUNCTIONS(AllMusic);
410 
411  public:
412 
413  AllMusic(void);
414  ~AllMusic();
415 
416  MusicMetadata* getMetadata(int an_id);
417  bool updateMetadata(int an_id, MusicMetadata *the_track);
418  int count() const { return m_numPcs; }
419  int countLoaded() const { return m_numLoaded; }
420  void save();
421  bool startLoading(void);
422  void resync(); // After a CD rip, for example
423 
424  // cd stuff
425  void clearCDData(void);
426  void addCDTrack(const MusicMetadata &the_track);
427  bool checkCDTrack(MusicMetadata *the_track);
428  MusicMetadata* getCDMetadata(int m_the_track);
429  QString getCDTitle(void) const { return m_cdTitle; }
430  void setCDTitle(const QString &a_title) { m_cdTitle = a_title; }
431  int getCDTrackCount(void) const { return m_cdData.count(); }
432 
433  bool doneLoading() const { return m_doneLoading; }
434  bool cleanOutThreads();
435 
436  MetadataPtrList *getAllMetadata(void) { return &m_allMusic; }
437  MetadataPtrList *getAllCDMetadata(void) { return &m_cdData; }
438 
439  bool isValidID(int an_id);
440 
441  private:
443 
444  int m_numPcs {0};
445  int m_numLoaded {0};
446 
447  using MusicMap = QMap<int, MusicMetadata*>;
449 
450  // cd stuff
451  MetadataPtrList m_cdData; // More than one cd player?
452  QString m_cdTitle;
453 
454  MetadataLoadingThread *m_metadataLoader {nullptr};
455  bool m_doneLoading {false};
456 
457  int m_playCountMin {0};
458  int m_playCountMax {0};
459 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
460  double m_lastPlayMin {0.0};
461  double m_lastPlayMax {0.0};
462 #else
463  qint64 m_lastPlayMin {0};
464  qint64 m_lastPlayMax {0};
465 #endif
466 };
467 
468 using StreamList = QList<MusicMetadata*>;
469 
471 {
472  public:
473 
474  AllStream(void);
475  ~AllStream();
476 
477  void loadStreams(void);
478 
479  bool isValidID(MusicMetadata::IdType an_id);
480 
481  MusicMetadata* getMetadata(MusicMetadata::IdType an_id);
482 
483  StreamList *getStreams(void) { return &m_streamList; }
484 
485  void addStream(MusicMetadata *mdata);
486  void removeStream(MusicMetadata *mdata);
487  void updateStream(MusicMetadata *mdata);
488 
489  private:
491 };
492 
493 //----------------------------------------------------------------------------
494 
496 {
497  public:
498  explicit AlbumArtScannerThread(QStringList strList) :
499  MThread("AlbumArtScanner"), m_strList(strList) {}
500 
501  void run() override // MThread
502  {
503  RunProlog();
505  RunEpilog();
506  }
507 
508  QStringList getResult(void) { return m_strList; }
509 
510  private:
511  QStringList m_strList;
512 };
513 
515 {
516  Q_DECLARE_TR_FUNCTIONS(AlbumArtImages);
517 
518  public:
519  AlbumArtImages(MusicMetadata *metadata, bool loadFromDB = true);
520  ~AlbumArtImages();
521 
522  void scanForImages(void);
523  void addImage(const AlbumArtImage &newImage);
524  uint getImageCount() { return m_imageList.size(); }
525  AlbumArtImage *getImage(ImageType type);
526  AlbumArtImage *getImageByID(int imageID);
527  QStringList getImageFilenames(void) const;
528  AlbumArtList *getImageList(void) { return &m_imageList; }
529  AlbumArtImage *getImageAt(uint index);
530 
531  void dumpToDatabase(void);
532 
533  static ImageType guessImageType(const QString &filename);
534  static QString getTypeName(ImageType type);
535  static QString getTypeFilename(ImageType type);
536  static ImageType getImageTypeFromName(const QString &name);
537 
538  private:
539  void findImages(void);
540 
541  MusicMetadata *m_parent {nullptr};
543 };
544 
546 
547 #endif
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
QList< AlbumArtImage * > AlbumArtList
Definition: musicmetadata.h:54
#define ID_TO_REPO(x)
Definition: musicmetadata.h:69
void setDiscCount(int disccount)
bool hasChanged(void) const
int getCDTrackCount(void) const
MusicMetadata(QString lfilename="", QString lartist="", QString lcompilation_artist="", QString lalbum="", QString ltitle="", QString lgenre="", int lyear=0, int ltracknum=0, int llength=0, int lid=0, int lrating=0, int lplaycount=0, QDateTime llastplay=QDateTime(), QDateTime ldateadded=QDateTime(), bool lcompilation=false, QString lformat="")
Definition: musicmetadata.h:86
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
QString[STREAMURLCOUNT] UrlList
Definition: musicmetadata.h:76
QString m_compilationArtistSort
int DiscNumber() const
static QString s_formatNormalFileArtist
void setGenreId(int lgenreid)
void setTrack(int ltrack)
bool isRadio(void) const
static QString s_formatCompilationCdTrack
void setDirectoryId(int ldirectoryid)
QStringList getResult(void)
uint getImageCount()
void setYear(int lyear)
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
QString LogoUrl(void)
QDateTime m_lastPlay
QString Language(void)
QString m_titleSort
uint32_t IdType
Definition: musicmetadata.h:84
QString Genre() const
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)
QString Hostname(void)
int GetTrackCount() const
QString m_actualFilename
void setLength(int llength)
RepoType
Definition: musicmetadata.h:56
void setBroadcaster(const QString &broadcaster)
void setLanguage(const QString &language)
bool operator!=(MusicMetadata &a, MusicMetadata &b)
QString m_description
Definition: musicmetadata.h:50
QString CompilationArtistSort() const
void setDiscNumber(int discnum)
QString m_filename
QString ArtistSort() const
QString MetadataFormat(void)
void setCDTitle(const QString &a_title)
int Length() const
uint64_t FileSize() const
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString m_formattedTitle
QString Country(void)
void setLogoUrl(const QString &logourl)
QDateTime m_dateAdded
AlbumArtList * getImageList(void)
#define STREAMURLCOUNT
Definition: musicmetadata.h:74
QString AlbumSort() const
QString m_artistSort
int Rating() const
int PlayCount() const
QDateTime m_tempLastPlay
void setHostname(const QString &host)
AlbumArtList m_imageList
QString m_hostname
Definition: musicmetadata.h:48
MusicMetadata(const MusicMetadata &other)
Definition: metaio.h:17
bool isCDTrack(void) const
QList< MusicMetadata * > StreamList
static QString s_formatCompilationCdArtist
QString m_compilationArtist
void setCompilation(bool state)
QString m_format
QString Artist() const
bool isDBTrack(void) const
MetadataLoadingThread(AllMusic *parent_ptr)
bool SendReceiveStringList(QStringList &strlist, bool quickTimeout=false, bool block=true)
Send a message to the backend and wait for a response.
QString Channel(void)
void setArtistId(int lartistid)
QString m_language
QString m_metaFormat
#define META_PUBLIC
Definition: mythmetaexp.h:9
void setAlbumId(int lalbumid)
QString m_filename
Definition: musicmetadata.h:47
QString m_logoUrl
StreamList m_streamList
#define METADATA_REPO_SHIFT
Definition: musicmetadata.h:64
IdType ID() const
void setChannel(const QString &channel)
void setAlbum(const QString &lalbum, const QString &lalbum_sort=nullptr)
void setRating(int lrating)
QList< MusicMetadata * > MetadataPtrList
int Track() const
MusicMap m_musicMap
AlbumArtScannerThread(QStringList strList)
void setRepo(RepoType repo)
unsigned int uint
Definition: compat.h:140
QString Album() const
QString m_channel
QString m_hostname
AlbumArtImage(void)
Definition: musicmetadata.h:40
StreamList * getStreams(void)
void setFormat(const QString &lformat)
void setTitle(const QString &ltitle, const QString &ltitle_sort=nullptr)
QString m_formattedArtist
void setArtist(const QString &lartist, const QString &lartist_sort=nullptr)
QString Format() const
QString m_description
AlbumArtImage(AlbumArtImage *image)
Definition: musicmetadata.h:42
QString m_artist
QString TitleSort() const
bool Compilation() const
Q_DECLARE_METATYPE(ImageType)
bool doneLoading() const
static QString s_formatNormalCdArtist
QString m_albumSort
int Year() const
QString Description(void)
#define METADATA_ID_MASK
Definition: musicmetadata.h:66
static QString s_formatNormalCdTrack
static QString s_formatCompilationFileTrack
QString Title() const
void setPlaycount(int lplaycount)
MetadataPtrList m_allMusic
void setFileSize(uint64_t lfilesize)
void RunProlog(void)
Sets up a thread, call this if you reimplement run().
Definition: mthread.cpp:202
MetadataPtrList m_cdData
static QString s_formatCompilationFileArtist
void setMetadataFormat(const QString &metaformat)
void setDescription(const QString &description)
QMap< int, MusicMetadata * > MusicMap
QDateTime LastPlay() const
ImageType
Definition: musicmetadata.h:26
QString CompilationArtist() const
int countLoaded() const
QString getCDTitle(void) const
QString m_broadcaster
bool operator==(MusicMetadata &a, MusicMetadata &b)
void run() override
Runs the Qt event loop unless we have a QRunnable, in which case we run the runnable run instead.
QString m_country
int Playcount() const
int DiscCount() const
void setTrackCount(int ltrackcount)
MetadataPtrList * getAllMetadata(void)
QString Broadcaster(void)
void setCompilationArtist(const QString &lcompilation_artist, const QString &lcompilation_artist_sort=nullptr)
int count() const
static QString s_formatNormalFileTrack
MetadataPtrList * getAllCDMetadata(void)
QString m_cdTitle
void setID(IdType lid)
void setCountry(const QString &country)
void setGenre(const QString &lgenre)