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 typedef QList<AlbumArtImage*> AlbumArtList;
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 typedef QString UrlList[STREAMURLCOUNT];
77 
79 {
80  Q_DECLARE_TR_FUNCTIONS(MusicMetadata);
81 
82  public:
83 
84  typedef uint32_t IdType;
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_compilation_artist(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_artist_sort; }
127  void setArtist(const QString &lartist,
128  const QString &lartist_sort = nullptr)
129  {
130  m_artist = lartist;
131  m_artist_sort = lartist_sort;
132  m_formattedartist.clear(); m_formattedtitle.clear();
133  ensureSortFields();
134  }
135 
136  QString CompilationArtist() const { return m_compilation_artist; }
137  QString CompilationArtistSort() const { return m_compilation_artist_sort; }
138  void setCompilationArtist(const QString &lcompilation_artist,
139  const QString &lcompilation_artist_sort = nullptr)
140  {
141  m_compilation_artist = lcompilation_artist;
142  m_compilation_artist_sort = 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_album_sort; }
149  void setAlbum(const QString &lalbum,
150  const QString &lalbum_sort = nullptr)
151  {
152  m_album = lalbum;
153  m_album_sort = 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_title_sort; }
160  void setTitle(const QString &ltitle,
161  const QString &ltitle_sort = nullptr)
162  {
163  m_title = ltitle;
164  m_title_sort = 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_artist_sort;
319  QString m_album;
320  QString m_album_sort;
321  QString m_title;
322  QString m_title_sort;
325  QString m_genre;
326  QString m_genre_sort;
327  QString m_format;
328  int m_year {0};
329  int m_tracknum {0};
330  int m_trackCount {0};
331  int m_discnum {0};
332  int m_disccount {0};
333  int m_length {0};
334  int m_rating {0};
335  int m_directoryid {-1};
336  int m_artistid {-1};
337  int m_compartistid {-1};
338  int m_albumid {-1};
339  int m_genreid {-1};
340  QDateTime m_lastplay;
341  QDateTime m_templastplay;
342  QDateTime m_dateadded;
343  int m_playcount {0};
344  int m_tempplaycount {0};
345  bool m_compilation {false};
346 
347  AlbumArtImages *m_albumArt {nullptr};
348 
349  LyricsData *m_lyricsData {nullptr};
350 
352  QString m_filename; // file name as stored in the DB
353  QString m_hostname; // host where file is located as stored in the DB
354  QString m_actualFilename; // actual URL of the file if found
355  uint64_t m_fileSize {0};
356  bool m_changed {false};
357 
358  // radio stream stuff
359  QString m_broadcaster;
360  QString m_channel;
361  QString m_description;
363  QString m_logoUrl;
364  QString m_metaFormat;
365  QString m_country;
366  QString m_language;
367 
368  // Various formatting strings
369  static QString m_formatnormalfileartist;
370  static QString m_formatnormalfiletrack;
371  static QString m_formatnormalcdartist;
372  static QString m_formatnormalcdtrack;
373 
378 };
379 
382 
385 
389 
390 //---------------------------------------------------------------------------
391 
393 {
394 
395  public:
396 
397  explicit MetadataLoadingThread(AllMusic *parent_ptr)
398  : MThread("MetadataLoading"), parent(parent_ptr) {}
399  void run() override; // MThread
400 
401  private:
402 
403  AllMusic *parent {nullptr};
404 };
405 
406 //---------------------------------------------------------------------------
407 
409 {
410  Q_DECLARE_TR_FUNCTIONS(AllMusic);
411 
412  public:
413 
414  AllMusic(void);
415  ~AllMusic();
416 
417  MusicMetadata* getMetadata(int an_id);
418  bool updateMetadata(int an_id, MusicMetadata *the_track);
419  int count() const { return m_numPcs; }
420  int countLoaded() const { return m_numLoaded; }
421  void save();
422  bool startLoading(void);
423  void resync(); // After a CD rip, for example
424 
425  // cd stuff
426  void clearCDData(void);
427  void addCDTrack(const MusicMetadata &the_track);
428  bool checkCDTrack(MusicMetadata *the_track);
429  MusicMetadata* getCDMetadata(int m_the_track);
430  QString getCDTitle(void) const { return m_cdTitle; }
431  void setCDTitle(const QString &a_title) { m_cdTitle = a_title; }
432  int getCDTrackCount(void) const { return m_cdData.count(); }
433 
434  bool doneLoading() const { return m_done_loading; }
435  bool cleanOutThreads();
436 
437  MetadataPtrList *getAllMetadata(void) { return &m_all_music; }
438  MetadataPtrList *getAllCDMetadata(void) { return &m_cdData; }
439 
440  bool isValidID(int an_id);
441 
442  private:
444 
445  int m_numPcs {0};
446  int m_numLoaded {0};
447 
448  typedef QMap<int, MusicMetadata*> MusicMap;
450 
451  // cd stuff
452  MetadataPtrList m_cdData; // More than one cd player?
453  QString m_cdTitle;
454 
455  MetadataLoadingThread *m_metadata_loader {nullptr};
456  bool m_done_loading {false};
457 
458  int m_playcountMin {0};
459  int m_playcountMax {0};
460 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
461  double m_lastplayMin {0.0};
462  double m_lastplayMax {0.0};
463 #else
464  qint64 m_lastplayMin {0};
465  qint64 m_lastplayMax {0};
466 #endif
467 };
468 
469 typedef QList<MusicMetadata*> StreamList;
470 
472 {
473  public:
474 
475  AllStream(void);
476  ~AllStream();
477 
478  void loadStreams(void);
479 
480  bool isValidID(MusicMetadata::IdType an_id);
481 
482  MusicMetadata* getMetadata(MusicMetadata::IdType an_id);
483 
484  StreamList *getStreams(void) { return &m_streamList; }
485 
486  void addStream(MusicMetadata *mdata);
487  void removeStream(MusicMetadata *mdata);
488  void updateStream(MusicMetadata *mdata);
489 
490  private:
492 };
493 
494 //----------------------------------------------------------------------------
495 
497 {
498  public:
499  explicit AlbumArtScannerThread(QStringList strList) :
500  MThread("AlbumArtScanner"), m_strList(strList) {}
501 
502  void run() override // MThread
503  {
504  RunProlog();
506  RunEpilog();
507  }
508 
509  QStringList getResult(void) { return m_strList; }
510 
511  private:
512  QStringList m_strList;
513 };
514 
516 {
517  Q_DECLARE_TR_FUNCTIONS(AlbumArtImages);
518 
519  public:
520  AlbumArtImages(MusicMetadata *metadata, bool loadFromDB = true);
521  ~AlbumArtImages();
522 
523  void scanForImages(void);
524  void addImage(const AlbumArtImage &newImage);
525  uint getImageCount() { return m_imageList.size(); }
526  AlbumArtImage *getImage(ImageType type);
527  AlbumArtImage *getImageByID(int imageID);
528  QStringList getImageFilenames(void) const;
529  AlbumArtList *getImageList(void) { return &m_imageList; }
530  AlbumArtImage *getImageAt(uint index);
531 
532  void dumpToDatabase(void);
533 
534  static ImageType guessImageType(const QString &filename);
535  static QString getTypeName(ImageType type);
536  static QString getTypeFilename(ImageType type);
537  static ImageType getImageTypeFromName(const QString &name);
538 
539  private:
540  void findImages(void);
541 
542  MusicMetadata *m_parent {nullptr};
544 };
545 
547 
548 #endif
void RunEpilog(void)
Cleans up a thread's resources, call this if you reimplement run().
Definition: mthread.cpp:215
#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
QList< MusicMetadata * > StreamList
QMap< int, MusicMetadata * > MusicMap
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
int DiscNumber() const
void setGenreId(int lgenreid)
void setTrack(int ltrack)
bool isRadio(void) const
void setDirectoryId(int ldirectoryid)
QStringList getResult(void)
uint getImageCount()
void setYear(int lyear)
QString LogoUrl(void)
QDateTime m_lastplay
QString Language(void)
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
QDateTime m_dateadded
static QString m_formatcompilationfileartist
void setBroadcaster(const QString &broadcaster)
void setLanguage(const QString &language)
bool operator!=(MusicMetadata &a, MusicMetadata &b)
static QString m_formatcompilationfiletrack
QList< AlbumArtImage * > AlbumArtList
Definition: musicmetadata.h:54
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
unsigned int uint
Definition: compat.h:140
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString Country(void)
void setLogoUrl(const QString &logourl)
AlbumArtList * getImageList(void)
#define STREAMURLCOUNT
Definition: musicmetadata.h:74
QString AlbumSort() const
QString m_title_sort
int Rating() const
int PlayCount() const
void setHostname(const QString &host)
AlbumArtList m_imageList
static QString m_formatcompilationcdtrack
QString m_hostname
Definition: musicmetadata.h:48
MusicMetadata(const MusicMetadata &other)
Definition: metaio.h:17
bool isCDTrack(void) const
unsigned char b
Definition: ParseText.cpp:329
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)
QHash< QString, QString > InfoMap
Definition: mythtypes.h:15
QString UrlList[STREAMURLCOUNT]
Definition: musicmetadata.h:76
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_artist_sort
QString m_logoUrl
QDateTime m_templastplay
StreamList m_streamList
static QString m_formatnormalcdartist
#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)
static QString m_formatcompilationcdartist
int Track() const
AlbumArtScannerThread(QStringList strList)
void setRepo(RepoType repo)
QString Album() const
QString m_channel
QString m_hostname
MusicMap m_music_map
const char * name
Definition: ParseText.cpp:328
AlbumArtImage(void)
Definition: musicmetadata.h:40
StreamList * getStreams(void)
void setFormat(const QString &lformat)
void setTitle(const QString &ltitle, const QString &ltitle_sort=nullptr)
void setArtist(const QString &lartist, const QString &lartist_sort=nullptr)
QString Format() const
QString m_description
QString m_compilation_artist_sort
AlbumArtImage(AlbumArtImage *image)
Definition: musicmetadata.h:42
QString m_artist
QString TitleSort() const
QList< MusicMetadata * > MetadataPtrList
bool Compilation() const
Q_DECLARE_METATYPE(ImageType)
bool doneLoading() const
int Year() const
QString Description(void)
#define METADATA_ID_MASK
Definition: musicmetadata.h:66
QString m_genre_sort
QString Title() const
void setPlaycount(int lplaycount)
QString m_formattedtitle
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
void setMetadataFormat(const QString &metaformat)
QString m_compilation_artist
void setDescription(const QString &description)
static QString m_formatnormalfileartist
QDateTime LastPlay() const
ImageType
Definition: musicmetadata.h:26
QString CompilationArtist() const
QString m_album_sort
int countLoaded() const
QString getCDTitle(void) const
QString m_broadcaster
static QString m_formatnormalfiletrack
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
MetadataPtrList m_all_music
MetadataPtrList * getAllCDMetadata(void)
uint32_t IdType
Definition: musicmetadata.h:84
QString m_cdTitle
QString m_formattedartist
void setID(IdType lid)
void setCountry(const QString &country)
static QString m_formatnormalcdtrack
void setGenre(const QString &lgenre)