[filesystem][SpecialProtocol] Removed assert from GetPath
[xbmc.git] / xbmc / music / MusicDatabase.h
blob8544eb297d64a4d7d5e4b6623f01dd0b47661b9d
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 /*!
12 \file MusicDatabase.h
13 \brief
16 #include "Album.h"
17 #include "MediaSource.h"
18 #include "addons/Scraper.h"
19 #include "dbwrappers/Database.h"
20 #include "settings/LibExportSettings.h"
21 #include "utils/SortUtils.h"
23 #include <utility>
24 #include <vector>
26 class CArtist;
27 class CFileItem;
28 class CMusicDbUrl;
30 namespace dbiplus
32 class field_value;
33 typedef std::vector<field_value> sql_record;
34 } // namespace dbiplus
36 #include <set>
37 #include <string>
39 // return codes of Cleaning up the Database
40 // numbers are strings from strings.po
41 #define ERROR_OK 317
42 #define ERROR_CANCEL 0
43 #define ERROR_DATABASE 315
44 #define ERROR_REORG_SONGS 319
45 #define ERROR_REORG_ARTIST 321
46 #define ERROR_REORG_OTHER 323
47 #define ERROR_REORG_PATH 325
48 #define ERROR_REORG_ALBUM 327
49 #define ERROR_WRITING_CHANGES 329
50 #define ERROR_COMPRESSING 332
52 #define NUM_SONGS_BEFORE_COMMIT 500
54 /*!
55 \ingroup music
56 \brief A set of std::string objects, used for CMusicDatabase
57 \sa ISETPATHS, CMusicDatabase
59 typedef std::set<std::string> SETPATHS;
61 /*!
62 \ingroup music
63 \brief The SETPATHS iterator
64 \sa SETPATHS, CMusicDatabase
66 typedef std::set<std::string>::iterator ISETPATHS;
68 /*!
69 \ingroup music
70 \brief A structure used for fetching music art data
71 \sa CMusicDatabase::GetArtForItem()
74 typedef struct
76 std::string mediaType;
77 std::string artType;
78 std::string prefix;
79 std::string url;
80 } ArtForThumbLoader;
82 class CGUIDialogProgress;
83 class CFileItemList;
85 /*!
86 \ingroup music
87 \brief Class to store and read tag information
89 CMusicDatabase can be used to read and store
90 tag information for faster access. It is based on
91 sqlite (http://www.sqlite.org).
93 Here is the database layout:
94 \image html musicdatabase.png
96 \sa CAlbum, CSong, VECSONGS, CMapSong, VECARTISTS, VECALBUMS, VECGENRES
98 class CMusicDatabase : public CDatabase
100 friend class DatabaseUtils;
101 friend class TestDatabaseUtilsHelper;
103 public:
104 CMusicDatabase(void);
105 ~CMusicDatabase(void) override;
107 bool Open() override;
108 bool CommitTransaction() override;
109 void EmptyCache();
110 void Clean();
111 int Cleanup(CGUIDialogProgress* progressDialog = nullptr);
112 bool LookupCDDBInfo(bool bRequery = false);
113 void DeleteCDDBInfo();
115 /////////////////////////////////////////////////
116 // Song CRUD
117 /////////////////////////////////////////////////
118 /*! \brief Add a song to the database
119 \param idSong [in] the original database ID of the song to reuse (-1 when new)
120 \param dtDateNew [in] the datetime the original ID was new
121 \param idAlbum [in] the database ID of the album for the song
122 \param strTitle [in] the title of the song (required to be non-empty)
123 \param strMusicBrainzTrackID [in] the MusicBrainz track ID of the song
124 \param strPathAndFileName [in] the path and filename to the song
125 \param strComment [in] the ids of the added songs
126 \param strMood [in] the mood of the added song
127 \param strThumb [in] the ids of the added songs
128 \param artistDisp [in] the assembled artist name(s) display string
129 \param artistSort [in] the artist name(s) sort string
130 \param genres [in] a vector of genres to which this song belongs
131 \param iTrack [in] the track number and disc number of the song
132 \param iDuration [in] the duration of the song
133 \param strReleaseDate [in] the release date of the song ISO8601 format
134 \param strOrigReleaseDate [in] the original release date of the song ISO8601 format
135 \param strDiscSubtitle [in] subtitle of a disc
136 \param iTimesPlayed [in] the number of times the song has been played
137 \param iStartOffset [in] the start offset of the song (when using a single audio file with a .cue)
138 \param iEndOffset [in] the end offset of the song (when using a single audio file with .cue)
139 \param dtLastPlayed [in] the time the song was last played
140 \param rating [in] a rating for the song
141 \param userrating [in] a userrating (my rating) for the song
142 \param votes [in] a vote counter for the song rating
143 \param replayGain [in] album and track replaygain and peak values
144 \return the id of the song
146 int AddSong(const int idSong,
147 const CDateTime& dtDateNew,
148 const int idAlbum,
149 const std::string& strTitle,
150 const std::string& strMusicBrainzTrackID,
151 const std::string& strPathAndFileName,
152 const std::string& strComment,
153 const std::string& strMood,
154 const std::string& strThumb,
155 const std::string& artistDisp,
156 const std::string& artistSort,
157 const std::vector<std::string>& genres,
158 int iTrack,
159 int iDuration,
160 const std::string& strReleaseDate,
161 const std::string& strOrigReleaseDate,
162 std::string& strDiscSubtitle,
163 const int iTimesPlayed,
164 int iStartOffset,
165 int iEndOffset,
166 const CDateTime& dtLastPlayed,
167 float rating,
168 int userrating,
169 int votes,
170 int iBPM,
171 int iBitRate,
172 int iSampleRate,
173 int iChannels,
174 const ReplayGain& replayGain);
175 bool GetSong(int idSong, CSong& song);
177 /*! \brief Update a song and all its nested entities (genres, artists, contributors)
178 \param song [in/out] the song to update, artist ids are returned in artist credits
179 \param bArtists to update artist credits and contributors, default is true
180 \param bArtists to check and log if artist links have changed, default is true
181 \return true if successful
183 bool UpdateSong(CSong& song, bool bArtists = true, bool bArtistLinks = true);
185 /*! \brief Update a song in the database
186 \param idSong [in] the database ID of the song to update
187 \param strTitle [in] the title of the song (required to be non-empty)
188 \param strMusicBrainzTrackID [in] the MusicBrainz track ID of the song
189 \param strPathAndFileName [in] the path and filename to the song
190 \param strComment [in] the ids of the added songs
191 \param strMood [in] the mood of the added song
192 \param strThumb [in] the ids of the added songs
193 \param artistDisp [in] the artist name(s) display string
194 \param artistSort [in] the artist name(s) sort string
195 \param genres [in] a vector of genres to which this song belongs
196 \param iTrack [in] the track number and disc number of the song
197 \param iDuration [in] the duration of the song
198 \param strReleaseDate [in] the release date of the song ISO8601 format
199 \param strOrigReleaseDate [in] the original release date of the song ISO8601 format
200 \param strDiscSubtitle [in] subtitle of a disc
201 \param iTimesPlayed [in] the number of times the song has been played
202 \param iStartOffset [in] the start offset of the song (when using a single audio file with a .cue)
203 \param iEndOffset [in] the end offset of the song (when using a single audio file with .cue)
204 \param dtLastPlayed [in] the time the song was last played
205 \param rating [in] a rating for the song
206 \param userrating [in] a userrating (my rating) for the song
207 \param votes [in] a vote counter for the song rating
208 \param replayGain [in] album and track replaygain and peak values
209 \param iBPM [in] the beats per minute of a song
210 \param iBitRate [in] the bitrate of the song file
211 \param iSampleRate [in] the sample rate of the song file
212 \param iChannels [in] the number of audio channels in the song file
213 \return the id of the song
215 int UpdateSong(int idSong,
216 const std::string& strTitle,
217 const std::string& strMusicBrainzTrackID,
218 const std::string& strPathAndFileName,
219 const std::string& strComment,
220 const std::string& strMood,
221 const std::string& strThumb,
222 const std::string& artistDisp,
223 const std::string& artistSort,
224 const std::vector<std::string>& genres,
225 int iTrack,
226 int iDuration,
227 const std::string& strReleaseDate,
228 const std::string& strOrigReleaseDate,
229 const std::string& strDiscSubtitle,
230 int iTimesPlayed,
231 int iStartOffset,
232 int iEndOffset,
233 const CDateTime& dtLastPlayed,
234 float rating,
235 int userrating,
236 int votes,
237 const ReplayGain& replayGain,
238 int iBPM,
239 int iBitRate,
240 int iSampleRate,
241 int iChannels);
243 //// Misc Song
244 bool GetSongByFileName(const std::string& strFileName, CSong& song, int64_t startOffset = 0);
245 bool GetSongsByPath(const std::string& strPath, MAPSONGS& songmap, bool bAppendToMap = false);
246 bool Search(const std::string& search, CFileItemList& items);
247 bool RemoveSongsFromPath(const std::string& path, MAPSONGS& songmap, bool exact = true);
248 void CheckArtistLinksChanged();
249 bool SetSongUserrating(const std::string& filePath, int userrating);
250 bool SetSongUserrating(int idSong, int userrating);
251 bool SetSongVotes(const std::string& filePath, int votes);
252 int GetSongByArtistAndAlbumAndTitle(const std::string& strArtist,
253 const std::string& strAlbum,
254 const std::string& strTitle);
256 /////////////////////////////////////////////////
257 // Album
258 /////////////////////////////////////////////////
259 /*! \brief Add an album and all its songs to the database
260 \param album the album to add
261 \param idSource the music source id
262 \return the id of the album
264 bool AddAlbum(CAlbum& album, int idSource);
266 /*! \brief Update an album and all its nested entities (artists, songs etc)
267 \param album the album to update
268 \return true or false
270 bool UpdateAlbum(CAlbum& album);
272 /*! \brief Add an album to the database
273 \param strAlbum the album title
274 \param strMusicBrainzAlbumID the Musicbrainz Id
275 \param strArtist the album artist name(s) display string
276 \param strArtistSort the album artist name(s) sort string
277 \param strGenre the album genre(s)
278 \param strReleaseDate [in] the release date of the album ISO8601 format
279 \param strOrigReleaseDate [in] the original release date of the album ISO8601 format
280 \param bBoxedSet if the album is a boxset
281 \param strRecordLabel the recording label
282 \param strType album type (Musicbrainz release type e.g. "Broadcast, Soundtrack, live"),
283 \param strReleaseStatus (see https://musicbrainz.org/doc/Release#Status)
284 \param bCompilation if the album is a compilation
285 \param releaseType "album" or "single"
286 \return the id of the album
288 int AddAlbum(const std::string& strAlbum,
289 const std::string& strMusicBrainzAlbumID,
290 const std::string& strReleaseGroupMBID,
291 const std::string& strArtist,
292 const std::string& strArtistSort,
293 const std::string& strGenre,
294 const std::string& strReleaseDate,
295 const std::string& strOrigReleaseDate,
296 bool bBoxedSet,
297 const std::string& strRecordLabel,
298 const std::string& strType,
299 const std::string& strReleaseStatus,
300 bool bCompilation,
301 CAlbum::ReleaseType releaseType);
303 /*! \brief retrieve an album, optionally with all songs.
304 \param idAlbum the database id of the album.
305 \param album [out] the album to fill.
306 \param getSongs whether or not to retrieve songs, defaults to true.
307 \return true if the album is retrieved, false otherwise.
309 bool GetAlbum(int idAlbum, CAlbum& album, bool getSongs = true);
310 int UpdateAlbum(int idAlbum,
311 const std::string& strAlbum,
312 const std::string& strMusicBrainzAlbumID,
313 const std::string& strReleaseGroupMBID,
314 const std::string& strArtist,
315 const std::string& strArtistSort,
316 const std::string& strGenre,
317 const std::string& strMoods,
318 const std::string& strStyles,
319 const std::string& strThemes,
320 const std::string& strReview,
321 const std::string& strImage,
322 const std::string& strLabel,
323 const std::string& strType,
324 const std::string& strReleaseStatus,
325 float fRating,
326 int iUserrating,
327 int iVotes,
328 const std::string& strReleaseDate,
329 const std::string& strOrigReleaseDate,
330 bool bBoxedSet,
331 bool bCompilation,
332 CAlbum::ReleaseType releaseType,
333 bool bScrapedMBID);
334 bool ClearAlbumLastScrapedTime(int idAlbum);
335 bool HasAlbumBeenScraped(int idAlbum);
337 /////////////////////////////////////////////////
338 // Audiobook
339 /////////////////////////////////////////////////
340 bool AddAudioBook(const CFileItem& item);
341 bool SetResumeBookmarkForAudioBook(const CFileItem& item, int bookmark);
342 bool GetResumeBookmarkForAudioBook(const CFileItem& item, int& bookmark);
344 /*! \brief Checks if the given path is inside a folder that has already been scanned into the library
345 \param path the path we want to check
347 bool InsideScannedPath(const std::string& path);
349 //// Misc Album
350 int GetAlbumIdByPath(const std::string& path);
351 bool GetAlbumFromSong(int idSong, CAlbum& album);
352 int GetAlbumByName(const std::string& strAlbum, const std::string& strArtist = "");
353 int GetAlbumByName(const std::string& strAlbum, const std::vector<std::string>& artist);
354 bool GetMatchingMusicVideoAlbum(const std::string& strAlbum,
355 const std::string& strArtist,
356 int& idAlbum,
357 std::string& strReview);
358 bool SearchAlbumsByArtistName(const std::string& strArtist, CFileItemList& items);
359 int GetAlbumByMatch(const CAlbum& album);
360 std::string GetAlbumById(int id);
361 std::string GetAlbumDiscTitle(int idAlbum, int idDisc);
362 bool SetAlbumUserrating(const int idAlbum, int userrating);
363 int GetAlbumDiscsCount(int idAlbum);
365 /////////////////////////////////////////////////
366 // Artist CRUD
367 /////////////////////////////////////////////////
368 bool UpdateArtist(const CArtist& artist);
370 int AddArtist(const std::string& strArtist,
371 const std::string& strMusicBrainzArtistID,
372 const std::string& strSortName,
373 bool bScrapedMBID = false);
374 int AddArtist(const std::string& strArtist,
375 const std::string& strMusicBrainzArtistID,
376 bool bScrapedMBID = false);
377 bool GetArtist(int idArtist, CArtist& artist, bool fetchAll = false);
378 bool GetArtistExists(int idArtist);
379 int GetLastArtist();
380 int GetArtistFromMBID(const std::string& strMusicBrainzArtistID, std::string& artistname);
381 int UpdateArtist(int idArtist,
382 const std::string& strArtist,
383 const std::string& strSortName,
384 const std::string& strMusicBrainzArtistID,
385 bool bScrapedMBID,
386 const std::string& strType,
387 const std::string& strGender,
388 const std::string& strDisambiguation,
389 const std::string& strBorn,
390 const std::string& strFormed,
391 const std::string& strGenres,
392 const std::string& strMoods,
393 const std::string& strStyles,
394 const std::string& strInstruments,
395 const std::string& strBiography,
396 const std::string& strDied,
397 const std::string& strDisbanded,
398 const std::string& strYearsActive,
399 const std::string& strImage);
400 bool UpdateArtistScrapedMBID(int idArtist, const std::string& strMusicBrainzArtistID);
401 bool GetTranslateBlankArtist() { return m_translateBlankArtist; }
402 void SetTranslateBlankArtist(bool translate) { m_translateBlankArtist = translate; }
403 bool HasArtistBeenScraped(int idArtist);
404 bool ClearArtistLastScrapedTime(int idArtist);
405 int AddArtistDiscography(int idArtist, const CDiscoAlbum& discoAlbum);
406 bool DeleteArtistDiscography(int idArtist);
407 bool GetArtistDiscography(int idArtist, CFileItemList& items);
409 std::string GetArtistById(int id);
410 int GetArtistByName(const std::string& strArtist);
411 int GetArtistByMatch(const CArtist& artist);
412 bool GetArtistFromSong(int idSong, CArtist& artist);
413 bool IsSongArtist(int idSong, int idArtist);
414 bool IsSongAlbumArtist(int idSong, int idArtist);
415 std::string GetRoleById(int id);
417 /*! \brief Propagate artist sort name into the concatenated artist sort name strings
418 held for songs and albums
419 \param int idArtist to propagate sort name for, -1 means all artists
421 bool UpdateArtistSortNames(int idArtist = -1);
423 /////////////////////////////////////////////////
424 // Paths
425 /////////////////////////////////////////////////
426 int AddPath(const std::string& strPath);
428 bool GetPaths(std::set<std::string>& paths);
429 bool SetPathHash(const std::string& path, const std::string& hash);
430 bool GetPathHash(const std::string& path, std::string& hash);
431 bool GetAlbumPaths(int idAlbum, std::vector<std::pair<std::string, int>>& paths);
432 bool GetAlbumPath(int idAlbum, std::string& basePath);
433 int GetDiscnumberForPathID(int idPath);
434 bool GetOldArtistPath(int idArtist, std::string& path);
435 bool GetArtistPath(const CArtist& artist, std::string& path);
436 bool GetAlbumFolder(const CAlbum& album, const std::string& strAlbumPath, std::string& strFolder);
437 bool GetArtistFolderName(const CArtist& artist, std::string& strFolder);
438 bool GetArtistFolderName(const std::string& strArtist,
439 const std::string& strMusicBrainzArtistID,
440 std::string& strFolder);
442 /////////////////////////////////////////////////
443 // Sources
444 /////////////////////////////////////////////////
445 bool UpdateSources();
446 int AddSource(const std::string& strName,
447 const std::string& strMultipath,
448 const std::vector<std::string>& vecPaths,
449 int id = -1);
450 int UpdateSource(const std::string& strOldName,
451 const std::string& strName,
452 const std::string& strMultipath,
453 const std::vector<std::string>& vecPaths);
454 bool RemoveSource(const std::string& strName);
455 int GetSourceFromPath(const std::string& strPath);
456 bool AddAlbumSource(int idAlbum, int idSource);
457 bool AddAlbumSources(int idAlbum, const std::string& strPath);
458 bool DeleteAlbumSources(int idAlbum);
459 bool GetSources(CFileItemList& items);
461 bool GetSourcesByArtist(int idArtist, CFileItem* item);
462 bool GetSourcesByAlbum(int idAlbum, CFileItem* item);
463 bool GetSourcesBySong(int idSong, const std::string& strPath, CFileItem* item);
464 int GetSourceByName(const std::string& strSource);
465 std::string GetSourceById(int id);
467 /////////////////////////////////////////////////
468 // Genres
469 /////////////////////////////////////////////////
470 int AddGenre(std::string& strGenre);
471 std::string GetGenreById(int id);
472 int GetGenreByName(const std::string& strGenre);
474 /////////////////////////////////////////////////
475 // Link tables
476 /////////////////////////////////////////////////
477 bool AddAlbumArtist(int idArtist, int idAlbum, const std::string& strArtist, int iOrder);
478 bool GetAlbumsByArtist(int idArtist, std::vector<int>& albums);
479 bool GetArtistsByAlbum(int idAlbum, CFileItem* item);
480 bool GetArtistsByAlbum(int idAlbum, std::vector<std::string>& artistIDs);
481 bool DeleteAlbumArtistsByAlbum(int idAlbum);
483 int AddRole(const std::string& strRole);
484 bool AddSongArtist(int idArtist,
485 int idSong,
486 const std::string& strRole,
487 const std::string& strArtist,
488 int iOrder);
489 bool AddSongArtist(
490 int idArtist, int idSong, int idRole, const std::string& strArtist, int iOrder);
491 int AddSongContributor(int idSong,
492 const std::string& strRole,
493 const std::string& strArtist,
494 const std::string& strSort);
495 void AddSongContributors(int idSong,
496 const VECMUSICROLES& contributors,
497 const std::string& strSort);
498 int GetRoleByName(const std::string& strRole);
499 bool GetRolesByArtist(int idArtist, CFileItem* item);
500 bool GetSongsByArtist(int idArtist, std::vector<int>& songs);
501 bool GetArtistsBySong(int idSong, std::vector<int>& artists);
502 bool DeleteSongArtistsBySong(int idSong);
504 bool AddSongGenres(int idSong, const std::vector<std::string>& genres);
505 bool GetGenresBySong(int idSong, std::vector<int>& genres);
507 bool GetGenresByAlbum(int idAlbum, CFileItem* item);
509 bool GetGenresByArtist(int idArtist, CFileItem* item);
510 bool GetIsAlbumArtist(int idArtist, CFileItem* item);
512 /////////////////////////////////////////////////
513 // Top 100
514 /////////////////////////////////////////////////
515 bool GetTop100(const std::string& strBaseDir, CFileItemList& items);
516 bool GetTop100Albums(VECALBUMS& albums);
517 bool GetTop100AlbumSongs(const std::string& strBaseDir, CFileItemList& item);
519 /////////////////////////////////////////////////
520 // Recently added
521 /////////////////////////////////////////////////
522 bool GetRecentlyAddedAlbums(VECALBUMS& albums, unsigned int limit = 0);
523 bool GetRecentlyAddedAlbumSongs(const std::string& strBaseDir,
524 CFileItemList& item,
525 unsigned int limit = 0);
526 bool GetRecentlyPlayedAlbums(VECALBUMS& albums);
527 bool GetRecentlyPlayedAlbumSongs(const std::string& strBaseDir, CFileItemList& item);
529 /////////////////////////////////////////////////
530 // Compilations
531 /////////////////////////////////////////////////
532 int GetCompilationAlbumsCount();
534 ////////////////////////////////////////////////
535 // Boxsets
536 ////////////////////////////////////////////////
537 bool IsAlbumBoxset(int idAlbum);
538 int GetBoxsetsCount();
540 int GetSinglesCount();
542 int GetArtistCountForRole(int role);
543 int GetArtistCountForRole(const std::string& strRole);
545 /*! \brief Increment the playcount of an item
546 Increments the playcount and updates the last played date
547 \param item CFileItem to increment the playcount for
549 void IncrementPlayCount(const CFileItem& item);
550 bool CleanupOrphanedItems();
552 /////////////////////////////////////////////////
553 // VIEWS
554 /////////////////////////////////////////////////
555 bool GetGenresNav(const std::string& strBaseDir,
556 CFileItemList& items,
557 const Filter& filter = Filter(),
558 bool countOnly = false);
559 bool GetSourcesNav(const std::string& strBaseDir,
560 CFileItemList& items,
561 const Filter& filter = Filter(),
562 bool countOnly = false);
563 bool GetYearsNav(const std::string& strBaseDir,
564 CFileItemList& items,
565 const Filter& filter = Filter());
566 bool GetRolesNav(const std::string& strBaseDir,
567 CFileItemList& items,
568 const Filter& filter = Filter());
569 bool GetArtistsNav(const std::string& strBaseDir,
570 CFileItemList& items,
571 bool albumArtistsOnly = false,
572 int idGenre = -1,
573 int idAlbum = -1,
574 int idSong = -1,
575 const Filter& filter = Filter(),
576 const SortDescription& sortDescription = SortDescription(),
577 bool countOnly = false);
578 bool GetCommonNav(const std::string& strBaseDir,
579 const std::string& table,
580 const std::string& labelField,
581 CFileItemList& items,
582 const Filter& filter /* = Filter() */,
583 bool countOnly /* = false */);
584 bool GetAlbumTypesNav(const std::string& strBaseDir,
585 CFileItemList& items,
586 const Filter& filter = Filter(),
587 bool countOnly = false);
588 bool GetMusicLabelsNav(const std::string& strBaseDir,
589 CFileItemList& items,
590 const Filter& filter = Filter(),
591 bool countOnly = false);
592 bool GetAlbumsNav(const std::string& strBaseDir,
593 CFileItemList& items,
594 int idGenre = -1,
595 int idArtist = -1,
596 const Filter& filter = Filter(),
597 const SortDescription& sortDescription = SortDescription(),
598 bool countOnly = false);
599 bool GetDiscsNav(const std::string& strBaseDir,
600 CFileItemList& items,
601 int idAlbum,
602 const Filter& filter = Filter(),
603 const SortDescription& sortDescription = SortDescription(),
604 bool countOnly = false);
605 bool GetAlbumsByYear(const std::string& strBaseDir, CFileItemList& items, int year);
606 bool GetSongsNav(const std::string& strBaseDir,
607 CFileItemList& items,
608 int idGenre,
609 int idArtist,
610 int idAlbum,
611 const SortDescription& sortDescription = SortDescription());
612 bool GetSongsByYear(const std::string& baseDir, CFileItemList& items, int year);
613 bool GetSongsByWhere(const std::string& baseDir,
614 const Filter& filter,
615 CFileItemList& items,
616 const SortDescription& sortDescription = SortDescription());
617 bool GetSongsFullByWhere(const std::string& baseDir,
618 const Filter& filter,
619 CFileItemList& items,
620 const SortDescription& sortDescription = SortDescription(),
621 bool artistData = false);
622 bool GetAlbumsByWhere(const std::string& baseDir,
623 const Filter& filter,
624 CFileItemList& items,
625 const SortDescription& sortDescription = SortDescription(),
626 bool countOnly = false);
627 bool GetDiscsByWhere(const std::string& baseDir,
628 const Filter& filter,
629 CFileItemList& items,
630 const SortDescription& sortDescription = SortDescription(),
631 bool countOnly = false);
632 bool GetDiscsByWhere(CMusicDbUrl& musicUrl,
633 const Filter& filter,
634 CFileItemList& items,
635 const SortDescription& sortDescription = SortDescription(),
636 bool countOnly = false);
637 bool GetArtistsByWhere(const std::string& strBaseDir,
638 const Filter& filter,
639 CFileItemList& items,
640 const SortDescription& sortDescription = SortDescription(),
641 bool countOnly = false);
642 int GetDiscsCount(const std::string& baseDir, const Filter& filter = Filter());
643 int GetSongsCount(const Filter& filter = Filter());
644 bool GetFilter(CDbUrl& musicUrl, Filter& filter, SortDescription& sorting) override;
645 int GetOrderFilter(const std::string& type, const SortDescription& sorting, Filter& filter);
647 /////////////////////////////////////////////////
648 // Party Mode
649 /////////////////////////////////////////////////
650 /*! \brief Gets song IDs in random order that match the filter criteria
651 \param filter the criteria to apply in the query
652 \param songIDs a vector of <1, id> pairs suited to party mode use
653 \return count of song ids found.
655 unsigned int GetRandomSongIDs(const Filter& filter, std::vector<std::pair<int, int>>& songIDs);
657 /////////////////////////////////////////////////
658 // JSON-RPC
659 /////////////////////////////////////////////////
660 bool GetGenresJSON(CFileItemList& items, bool bSources = false);
661 bool GetArtistsByWhereJSON(const std::set<std::string>& fields,
662 const std::string& baseDir,
663 CVariant& result,
664 int& total,
665 const SortDescription& sortDescription = SortDescription());
666 bool GetAlbumsByWhereJSON(const std::set<std::string>& fields,
667 const std::string& baseDir,
668 CVariant& result,
669 int& total,
670 const SortDescription& sortDescription = SortDescription());
671 bool GetSongsByWhereJSON(const std::set<std::string>& fields,
672 const std::string& baseDir,
673 CVariant& result,
674 int& total,
675 const SortDescription& sortDescription = SortDescription());
677 /////////////////////////////////////////////////
678 // Scraper
679 /////////////////////////////////////////////////
680 bool SetScraper(int id, const CONTENT_TYPE& content, const ADDON::ScraperPtr& scraper);
681 bool SetScraperAll(const std::string& strBaseDir, const ADDON::ScraperPtr& scraper);
682 bool GetScraper(int id, const CONTENT_TYPE& content, ADDON::ScraperPtr& scraper);
684 /*! \brief Check whether a given scraper is in use.
685 \param scraperID the scraper to check for.
686 \return true if the scraper is in use, false otherwise.
688 bool ScraperInUse(const std::string& scraperID) const;
690 /////////////////////////////////////////////////
691 // Filters
692 /////////////////////////////////////////////////
693 bool GetItems(const std::string& strBaseDir,
694 CFileItemList& items,
695 const Filter& filter = Filter(),
696 const SortDescription& sortDescription = SortDescription());
697 bool GetItems(const std::string& strBaseDir,
698 const std::string& itemType,
699 CFileItemList& items,
700 const Filter& filter = Filter(),
701 const SortDescription& sortDescription = SortDescription());
702 std::string GetItemById(const std::string& itemType, int id);
704 /////////////////////////////////////////////////
705 // XML
706 /////////////////////////////////////////////////
707 void ExportToXML(const CLibExportSettings& settings,
708 CGUIDialogProgress* progressDialog = nullptr);
709 bool ExportSongHistory(TiXmlNode* pNode, CGUIDialogProgress* progressDialog = nullptr);
710 void ImportFromXML(const std::string& xmlFile, CGUIDialogProgress* progressDialog = nullptr);
711 bool ImportSongHistory(const std::string& xmlFile,
712 const int total,
713 CGUIDialogProgress* progressDialog = nullptr);
715 /////////////////////////////////////////////////
716 // Properties
717 /////////////////////////////////////////////////
718 void SetPropertiesForFileItem(CFileItem& item);
719 static void SetPropertiesFromArtist(CFileItem& item, const CArtist& artist);
720 static void SetPropertiesFromAlbum(CFileItem& item, const CAlbum& album);
721 void SetItemUpdated(int mediaId, const std::string& mediaType);
723 /////////////////////////////////////////////////
724 // Art
725 /////////////////////////////////////////////////
726 /*! \brief Sets art for a database item.
727 Sets a single piece of art for a database item.
728 \param mediaId the id in the media (song/artist/album) table.
729 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
730 \param artType the type of art to set, e.g. "thumb", "fanart"
731 \param url the url to the art (this is the original url, not a cached url).
732 \sa GetArtForItem
734 void SetArtForItem(int mediaId,
735 const std::string& mediaType,
736 const std::string& artType,
737 const std::string& url);
739 /*! \brief Sets art for a database item.
740 Sets multiple pieces of art for a database item.
741 \param mediaId the id in the media (song/artist/album) table.
742 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
743 \param art a map of <type, url> where type is "thumb", "fanart", etc. and url is the original url of the art.
744 \sa GetArtForItem
746 void SetArtForItem(int mediaId,
747 const std::string& mediaType,
748 const std::map<std::string, std::string>& art);
751 /*! \brief Fetch all related art for a database item.
752 Fetches multiple pieces of art for a database item including that for related media types
753 Given song id art for the related album, artist(s) and albumartist(s) will also be fetched, looking up the
754 album and artist when ids are not provided.
755 Given album id (and not song id) art for the related artist(s) will also be fetched, looking up the
756 artist(s) when id are not provided.
757 \param songId the id in the song table, -1 when song art not being fetched
758 \param albumId the id in the album table, -1 when album art not being fetched
759 \param artistId the id in the artist table, -1 when artist not known
760 \param bPrimaryArtist true if art from only the first song artist or album artist is to be fetched
761 \param art [out] a vector, each element having media type e.g. "artist", "album" or "song",
762 artType e.g. "thumb", "fanart", etc., prefix of "", "artist" or "albumartist" etc. giving the kind of artist
763 relationship, and the original url of the art.
765 \return true if art is retrieved, false if no art is found.
766 \sa SetArtForItem
768 bool GetArtForItem(int songId,
769 int albumId,
770 int artistId,
771 bool bPrimaryArtist,
772 std::vector<ArtForThumbLoader>& art);
774 /*! \brief Fetch art for a database item.
775 Fetches multiple pieces of art for a database item.
776 \param mediaId the id in the media (song/artist/album) table.
777 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
778 \param art [out] a map of <type, url> where type is "thumb", "fanart", etc. and url is the original url of the art.
779 \return true if art is retrieved, false if no art is found.
780 \sa SetArtForItem
782 bool GetArtForItem(int mediaId,
783 const std::string& mediaType,
784 std::map<std::string, std::string>& art);
786 /*! \brief Fetch art for a database item.
787 Fetches a single piece of art for a database item.
788 \param mediaId the id in the media (song/artist/album) table.
789 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
790 \param artType the type of art to retrieve, eg "thumb", "fanart".
791 \return the original URL to the piece of art, if available.
792 \sa SetArtForItem
794 std::string GetArtForItem(int mediaId, const std::string& mediaType, const std::string& artType);
796 /*! \brief Remove art for a database item.
797 Removes a single piece of art for a database item.
798 \param mediaId the id in the media (song/artist/album) table.
799 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
800 \param artType the type of art to remove, eg "thumb", "fanart".
801 \return true if art is removed, false if no art is found.
802 \sa RemoveArtForItem
804 bool RemoveArtForItem(int mediaId, const MediaType& mediaType, const std::string& artType);
806 /*! \brief Remove art for a database item.
807 Removes multiple pieces of art for a database item.
808 \param mediaId the id in the media (song/artist/album) table.
809 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
810 \param arttypes a set of types, e.g. "thumb", "fanart", etc. to be removed.
811 \return true if art is removed, false if no art is found.
812 \sa RemoveArtForItem
814 bool RemoveArtForItem(int mediaId,
815 const MediaType& mediaType,
816 const std::set<std::string>& artTypes);
818 /*! \brief Fetch the distinct types of art held in the database for a type of media.
819 \param mediaType the type of media, which corresponds to the table the item resides in (song/artist/album).
820 \param artTypes [out] the types of art e.g. "thumb", "fanart", etc.
821 \return true if art is found, false if no art is found.
823 bool GetArtTypes(const MediaType& mediaType, std::vector<std::string>& artTypes);
825 /*! \brief Fetch the distinct types of available-but-unassigned art held in the
826 database for a specific media item.
827 \param mediaId the id in the media (artist/album) table.
828 \param mediaType the type of media, which corresponds to the table the item resides in (artist/album).
829 \return the types of art e.g. "thumb", "fanart", etc.
831 std::vector<std::string> GetAvailableArtTypesForItem(int mediaId, const MediaType& mediaType);
833 /*! \brief Fetch the list of available-but-unassigned art URLs held in the
834 database for a specific media item and art type.
835 \param mediaId the id in the media (artist/album) table.
836 \param mediaType corresponds to the table the item resides in (artist/album).
837 \param artType e.g. "thumb", "fanart", etc.
838 \return list of URLs
840 std::vector<CScraperUrl::SUrlEntry> GetAvailableArtForItem(int mediaId,
841 const MediaType& mediaType,
842 const std::string& artType);
844 /////////////////////////////////////////////////
845 // Tag Scan Version
846 /////////////////////////////////////////////////
847 /*! \brief Check if music files need all tags rescanning regardless of file being unchanged
848 because the tag processing has changed (which may happen without db version changes) since they
849 where last scanned.
850 \return -1 if an error occurred, 0 if no scan is needed, or the version number of tags if not the same as current.
852 virtual int GetMusicNeedsTagScan();
854 /*! \brief Set minimum version number of db needed when tag data scanned from music files
855 \param version the version number of db
857 void SetMusicNeedsTagScan(int version);
859 /*! \brief Set the version number of tag data
860 \param version the version number of db when tags last scanned, 0 (default) means current db version
862 void SetMusicTagScanVersion(int version = 0);
864 std::string GetLibraryLastUpdated();
865 void SetLibraryLastUpdated();
866 std::string GetLibraryLastCleaned();
867 void SetLibraryLastCleaned();
868 std::string GetArtistLinksUpdated();
869 void SetArtistLinksUpdated();
870 std::string GetGenresLastAdded();
871 std::string GetSongsLastAdded();
872 std::string GetAlbumsLastAdded();
873 std::string GetArtistsLastAdded();
874 std::string GetSongsLastModified();
875 std::string GetAlbumsLastModified();
876 std::string GetArtistsLastModified();
879 protected:
880 std::map<std::string, int> m_genreCache;
881 std::map<std::string, int> m_pathCache;
883 void CreateTables() override;
884 void CreateAnalytics() override;
885 int GetMinSchemaVersion() const override { return 32; }
886 int GetSchemaVersion() const override;
888 const char* GetBaseDBName() const override { return "MyMusic"; }
890 private:
891 /*! \brief (Re)Create the generic database views for songs and albums
893 virtual void CreateViews();
894 void CreateNativeDBFunctions();
895 void CreateRemovedLinkTriggers();
897 void SplitPath(const std::string& strFileNameAndPath,
898 std::string& strPath,
899 std::string& strFileName);
901 CSong GetSongFromDataset();
902 CSong GetSongFromDataset(const dbiplus::sql_record* const record, int offset = 0);
903 CArtist GetArtistFromDataset(dbiplus::Dataset* pDS, int offset = 0, bool needThumb = true);
904 CArtist GetArtistFromDataset(const dbiplus::sql_record* const record,
905 int offset = 0,
906 bool needThumb = true);
907 CAlbum GetAlbumFromDataset(dbiplus::Dataset* pDS, int offset = 0, bool imageURL = false);
908 CAlbum GetAlbumFromDataset(const dbiplus::sql_record* const record,
909 int offset = 0,
910 bool imageURL = false);
911 CArtistCredit GetArtistCreditFromDataset(const dbiplus::sql_record* const record, int offset = 0);
912 CMusicRole GetArtistRoleFromDataset(const dbiplus::sql_record* const record, int offset = 0);
913 std::string GetMediaDateFromFile(const std::string& strFileNameAndPath);
914 void GetFileItemFromDataset(CFileItem* item, const CMusicDbUrl& baseUrl);
915 void GetFileItemFromDataset(const dbiplus::sql_record* const record,
916 CFileItem* item,
917 const CMusicDbUrl& baseUrl);
918 void GetFileItemFromArtistCredits(VECARTISTCREDITS& artistCredits, CFileItem* item);
920 bool DeleteRemovedLinks();
922 bool CleanupSongs(CGUIDialogProgress* progressDialog = nullptr);
923 bool CleanupSongsByIds(const std::string& strSongIds);
924 bool CleanupPaths();
925 bool CleanupAlbums();
926 bool CleanupArtists();
927 bool CleanupGenres();
928 bool CleanupInfoSettings();
929 bool CleanupRoles();
930 void UpdateTables(int version) override;
931 bool SearchArtists(const std::string& search, CFileItemList& artists);
932 bool SearchAlbums(const std::string& search, CFileItemList& albums);
933 bool SearchSongs(const std::string& strSearch, CFileItemList& songs);
934 int GetSongIDFromPath(const std::string& filePath);
935 void NormaliseSongDates(std::string& strRelease, std::string& strOriginal);
936 bool TrimImageURLs(std::string& strImage, const size_t space);
938 /*! \brief Build SQL for sort subquery from ignore article token list
939 \param strField original name or title field that articles could be removed from
940 \return SQL string e.g. WHEN strField LIKE 'the_' ESCAPE '_' THEN SUBSTR(strArtist, 5)
942 std::string GetIgnoreArticleSQL(const std::string& strField);
944 /*! \brief Build SQL for sort name scalar subquery from sort attributes and ignore article list.
945 \param strAlias alias name of scalar subquery field
946 \param sortAttributes the sort attributes e.g. SortAttributeIgnoreArticle
947 \param strField original name or title field that articles could be removed from
948 \param strSortField sort name or title field to be used instead of original (when data not null)
949 \return SQL string e.g.
950 CASE WHEN strArtistSort IS NOT NULL THEN strArtistSort
951 WHEN strField LIKE 'the ' OR strField LIKE 'the_' ESCAPE '_' THEN SUBSTR(strArtist, 5)
952 ELSE strField
953 END AS strAlias
955 std::string SortnameBuildSQL(const std::string& strAlias,
956 const SortAttribute& sortAttributes,
957 const std::string& strField,
958 const std::string& strSortField);
960 /*! \brief Build SQL for sorting field naturally and case-insensitively (in SQLite).
961 \param strField field name
962 \param sortOrder the sort order
963 \return SQL string e.g.
964 CASE WHEN CAST(strTitle AS INTEGER) = 0 THEN 100000000
965 ELSE CAST(strTitle AS INTEGER) END DESC, strTitle COLLATE NOCASE DESC
967 std::string AlphanumericSortSQL(const std::string& strField, const SortOrder& sortOrder);
969 /*! \brief Checks that source table matches sources.xml
970 returns true when they do
972 bool CheckSources(VECSOURCES& sources);
974 /*! \brief Initially fills source table from sources.xml for use only at
975 migration of db from an earlier version than 72
976 returns true when successfully done
978 bool MigrateSources();
980 bool m_translateBlankArtist;
982 // Fields should be ordered as they
983 // appear in the songview
984 static enum _SongFields {
985 song_idSong = 0,
986 song_strArtists,
987 song_strArtistSort,
988 song_strGenres,
989 song_strTitle,
990 song_iTrack,
991 song_iDuration,
992 song_strReleaseDate,
993 song_strOrigReleaseDate,
994 song_strDiscSubtitle,
995 song_strFileName,
996 song_strMusicBrainzTrackID,
997 song_iTimesPlayed,
998 song_iStartOffset,
999 song_iEndOffset,
1000 song_lastplayed,
1001 song_rating,
1002 song_userrating,
1003 song_votes,
1004 song_comment,
1005 song_idAlbum,
1006 song_strAlbum,
1007 song_strPath,
1008 song_strReleaseStatus,
1009 song_bCompilation,
1010 song_bBoxedSet,
1011 song_strAlbumArtists,
1012 song_strAlbumArtistSort,
1013 song_strAlbumReleaseType,
1014 song_mood,
1015 song_strReplayGain,
1016 song_iBPM,
1017 song_iBitRate,
1018 song_iSampleRate,
1019 song_iChannels,
1020 song_iAlbumDuration,
1021 song_iDiscTotal,
1022 song_dateAdded,
1023 song_dateNew,
1024 song_dateModified,
1025 song_enumCount // end of the enum, do not add past here
1026 } SongFields;
1028 // Fields should be ordered as they
1029 // appear in the albumview
1030 static enum _AlbumFields {
1031 album_idAlbum = 0,
1032 album_strAlbum,
1033 album_strMusicBrainzAlbumID,
1034 album_strReleaseGroupMBID,
1035 album_strArtists,
1036 album_strArtistSort,
1037 album_strGenres,
1038 album_strReleaseDate,
1039 album_strOrigReleaseDate,
1040 album_bBoxedSet,
1041 album_strMoods,
1042 album_strStyles,
1043 album_strThemes,
1044 album_strReview,
1045 album_strLabel,
1046 album_strType,
1047 album_strReleaseStatus,
1048 album_strThumbURL,
1049 album_fRating,
1050 album_iUserrating,
1051 album_iVotes,
1052 album_bCompilation,
1053 album_bScrapedMBID,
1054 album_lastScraped,
1055 album_dateAdded,
1056 album_dateNew,
1057 album_dateModified,
1058 album_iTimesPlayed,
1059 album_strReleaseType,
1060 album_iTotalDiscs,
1061 album_dtLastPlayed,
1062 album_iAlbumDuration,
1063 album_enumCount // end of the enum, do not add past here
1064 } AlbumFields;
1066 // Fields should be ordered as they
1067 // appear in the songartistview/albumartistview
1068 static enum _ArtistCreditFields {
1069 // used for GetAlbum to get the cascaded album/song artist credits
1070 artistCredit_idEntity = 0, // can be idSong or idAlbum depending on context
1071 artistCredit_idArtist,
1072 artistCredit_idRole,
1073 artistCredit_strRole,
1074 artistCredit_strArtist,
1075 artistCredit_strSortName,
1076 artistCredit_strMusicBrainzArtistID,
1077 artistCredit_iOrder,
1078 artistCredit_enumCount
1079 } ArtistCreditFields;
1081 // Fields should be ordered as they
1082 // appear in the artistview
1083 static enum _ArtistFields {
1084 artist_idArtist = 0,
1085 artist_strArtist,
1086 artist_strSortName,
1087 artist_strMusicBrainzArtistID,
1088 artist_strType,
1089 artist_strGender,
1090 artist_strDisambiguation,
1091 artist_strBorn,
1092 artist_strFormed,
1093 artist_strGenres,
1094 artist_strMoods,
1095 artist_strStyles,
1096 artist_strInstruments,
1097 artist_strBiography,
1098 artist_strDied,
1099 artist_strDisbanded,
1100 artist_strYearsActive,
1101 artist_strImage,
1102 artist_bScrapedMBID,
1103 artist_lastScraped,
1104 artist_dateAdded,
1105 artist_dateNew,
1106 artist_dateModified,
1107 artist_enumCount // end of the enum, do not add past here
1108 } ArtistFields;
1110 // Fields fetched by GetArtistsByWhereJSON, order same as in JSONtoDBArtist
1111 static enum _JoinToArtistFields {
1112 joinToArtist_isSong = 0,
1113 joinToArtist_idSourceAlbum,
1114 joinToArtist_idSourceSong,
1115 joinToArtist_idSongGenreAlbum,
1116 joinToArtist_idSongGenreSong,
1117 joinToArtist_strSongGenreAlbum,
1118 joinToArtist_strSongGenreSong,
1119 joinToArtist_idArt,
1120 joinToArtist_artType,
1121 joinToArtist_artURL,
1122 joinToArtist_idRole,
1123 joinToArtist_strRole,
1124 joinToArtist_iOrderRole,
1125 joinToArtist_isalbumartist,
1126 joinToArtist_thumbnail,
1127 joinToArtist_fanart,
1128 joinToArtist_enumCount // end of the enum, do not add past here
1129 } JoinToArtistFields;
1131 // Fields fetched by GetAlbumsByWhereJSON, order same as in JSONtoDBAlbum
1132 static enum _JoinToAlbumFields {
1133 joinToAlbum_idArtist = 0,
1134 joinToAlbum_strArtist,
1135 joinToAlbum_strArtistMBID,
1136 joinToAlbum_enumCount // end of the enum, do not add past here
1137 } JoinToAlbumFields;
1139 // Fields fetched by GetSongsByWhereJSON, order same as in JSONtoDBSong
1140 static enum _JoinToSongFields {
1141 // Used by GetSongsByWhereJSON
1142 joinToSongs_idAlbumArtist = 0,
1143 joinToSongs_strAlbumArtist,
1144 joinToSongs_strAlbumArtistMBID,
1145 joinToSongs_iOrderAlbumArtist,
1146 joinToSongs_idArtist,
1147 joinToSongs_strArtist,
1148 joinToSongs_strArtistMBID,
1149 joinToSongs_iOrderArtist,
1150 joinToSongs_idRole,
1151 joinToSongs_strRole,
1152 joinToSongs_iOrderRole,
1153 joinToSongs_idGenre,
1154 joinToSongs_iOrderGenre,
1155 joinToSongs_enumCount // end of the enum, do not add past here
1156 } JoinToSongFields;