LinuxRendererGLES.h: correct path to `DVDVideoCodec.h`
[vuplus_xbmc] / xbmc / music / MusicDatabase.h
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21 /*!
22  \file MusicDatabase.h
23 \brief
24 */
25 #pragma once
26 #include "dbwrappers/Database.h"
27 #include "Album.h"
28 #include "addons/Scraper.h"
29
30 class CArtist;
31 class CFileItem;
32
33 #include <set>
34
35 // return codes of Cleaning up the Database
36 // numbers are strings from strings.xml
37 #define ERROR_OK     317
38 #define ERROR_CANCEL    0
39 #define ERROR_DATABASE    315
40 #define ERROR_REORG_SONGS   319
41 #define ERROR_REORG_ARTIST   321
42 #define ERROR_REORG_GENRE   323
43 #define ERROR_REORG_PATH   325
44 #define ERROR_REORG_ALBUM   327
45 #define ERROR_WRITING_CHANGES  329
46 #define ERROR_COMPRESSING   332
47
48 #define NUM_SONGS_BEFORE_COMMIT 500
49
50 /*!
51  \ingroup music
52  \brief A set of CStdString objects, used for CMusicDatabase
53  \sa ISETPATHES, CMusicDatabase
54  */
55 typedef std::set<CStdString> SETPATHES;
56
57 /*!
58  \ingroup music
59  \brief The SETPATHES iterator
60  \sa SETPATHES, CMusicDatabase
61  */
62 typedef std::set<CStdString>::iterator ISETPATHES;
63
64 class CGUIDialogProgress;
65 class CFileItemList;
66
67 /*!
68  \ingroup music
69  \brief Class to store and read tag information
70
71  CMusicDatabase can be used to read and store
72  tag information for faster access. It is based on
73  sqlite (http://www.sqlite.org).
74
75  Here is the database layout:
76   \image html musicdatabase.png
77
78  \sa CAlbum, CSong, VECSONGS, CMapSong, VECARTISTS, VECALBUMS, VECGENRES
79  */
80 class CMusicDatabase : public CDatabase
81 {
82   class CArtistCache
83   {
84   public:
85     int idArtist;
86     CStdString strArtist;
87   };
88
89   class CPathCache
90   {
91   public:
92     int idPath;
93     CStdString strPath;
94   };
95
96   class CGenreCache
97   {
98   public:
99     int idGenre;
100     CStdString strGenre;
101   };
102
103   class CAlbumCache : public CAlbum
104   {
105   public:
106     int idAlbum;
107     int idArtist;
108   };
109
110 public:
111   CMusicDatabase(void);
112   virtual ~CMusicDatabase(void);
113
114   virtual bool Open();
115   virtual bool CommitTransaction();
116   void EmptyCache();
117   void Clean();
118   int  Cleanup(CGUIDialogProgress *pDlgProgress=NULL);
119   void DeleteAlbumInfo();
120   bool LookupCDDBInfo(bool bRequery=false);
121   void DeleteCDDBInfo();
122   void AddSong(CSong& song, bool bCheck = true);
123   int SetAlbumInfo(int idAlbum, const CAlbum& album, const VECSONGS& songs, bool bTransaction=true);
124   bool DeleteAlbumInfo(int idArtist);
125   int SetArtistInfo(int idArtist, const CArtist& artist);
126   bool DeleteArtistInfo(int idArtist);
127   bool GetAlbumInfo(int idAlbum, CAlbum &info, VECSONGS* songs);
128   bool HasAlbumInfo(int idAlbum);
129   bool GetArtistInfo(int idArtist, CArtist &info, bool needAll=true);
130   bool GetSongByFileName(const CStdString& strFileName, CSong& song);
131   int GetAlbumIdByPath(const CStdString& path);
132   bool GetSongById(int idSong, CSong& song);
133   bool GetSongByKaraokeNumber( int number, CSong& song );
134   bool SetKaraokeSongDelay( int idSong, int delay );
135   bool GetSongsByPath(const CStdString& strPath, CSongMap& songs, bool bAppendToMap = false);
136   bool Search(const CStdString& search, CFileItemList &items);
137
138   bool GetAlbumFromSong(int idSong, CAlbum &album);
139   bool GetAlbumFromSong(const CSong &song, CAlbum &album);
140
141   bool GetArbitraryQuery(const CStdString& strQuery, const CStdString& strOpenRecordSet, const CStdString& strCloseRecordSet,
142                          const CStdString& strOpenRecord, const CStdString& strCloseRecord, const CStdString& strOpenField, const CStdString& strCloseField, CStdString& strResult);
143   bool ArbitraryExec(const CStdString& strExec);
144
145   bool GetTop100(const CStdString& strBaseDir, CFileItemList& items);
146   bool GetTop100Albums(VECALBUMS& albums);
147   bool GetTop100AlbumSongs(const CStdString& strBaseDir, CFileItemList& item);
148   bool GetRecentlyAddedAlbums(VECALBUMS& albums);
149   bool GetRecentlyAddedAlbumSongs(const CStdString& strBaseDir, CFileItemList& item);
150   bool GetRecentlyPlayedAlbums(VECALBUMS& albums);
151   bool GetRecentlyPlayedAlbumSongs(const CStdString& strBaseDir, CFileItemList& item);
152   bool IncrTop100CounterByFileName(const CStdString& strFileName1);
153   bool RemoveSongsFromPath(const CStdString &path, CSongMap &songs, bool exact=true);
154   bool CleanupOrphanedItems();
155   bool GetPaths(std::set<CStdString> &paths);
156   bool SetPathHash(const CStdString &path, const CStdString &hash);
157   bool GetPathHash(const CStdString &path, CStdString &hash);
158   bool GetGenresNav(const CStdString& strBaseDir, CFileItemList& items);
159   bool GetYearsNav(const CStdString& strBaseDir, CFileItemList& items);
160   bool GetArtistsNav(const CStdString& strBaseDir, CFileItemList& items, int idGenre, bool albumArtistsOnly);
161   bool GetAlbumsNav(const CStdString& strBaseDir, CFileItemList& items, int idGenre, int idArtist, int start, int end);
162   bool GetAlbumsByYear(const CStdString &strBaseDir, CFileItemList& items, int year);
163   bool GetSongsNav(const CStdString& strBaseDir, CFileItemList& items, int idGenre, int idArtist,int idAlbum);
164   bool GetSongsByYear(const CStdString& baseDir, CFileItemList& items, int year);
165   bool GetSongsByWhere(const CStdString &baseDir, const CStdString &whereClause, CFileItemList& items);
166   bool GetAlbumsByWhere(const CStdString &baseDir, const CStdString &where, const CStdString &order, CFileItemList &items);
167   bool GetRandomSong(CFileItem* item, int& idSong, const CStdString& strWhere);
168   int GetKaraokeSongsCount();
169   int GetSongsCount(const CStdString& strWhere = "");
170   unsigned int GetSongIDs(const CStdString& strWhere, std::vector<std::pair<int,int> > &songIDs);
171
172   bool GetAlbumPath(int idAlbum, CStdString &path);
173   bool SaveAlbumThumb(int idAlbum, const CStdString &thumb);
174   bool GetAlbumThumb(int idAlbum, CStdString &thumb);
175   bool GetArtistPath(int idArtist, CStdString &path);
176
177   bool GetGenreById(int idGenre, CStdString& strGenre);
178   bool GetArtistById(int idArtist, CStdString& strArtist);
179   bool GetAlbumById(int idAlbum, CStdString& strAlbum);
180
181   int GetArtistByName(const CStdString& strArtist);
182   int GetAlbumByName(const CStdString& strAlbum, const CStdString& strArtist="");
183   int GetGenreByName(const CStdString& strGenre);
184   int GetSongByArtistAndAlbumAndTitle(const CStdString& strArtist, const CStdString& strAlbum, const CStdString& strTitle);
185
186   bool GetVariousArtistsAlbums(const CStdString& strBaseDir, CFileItemList& items);
187   bool GetVariousArtistsAlbumsSongs(const CStdString& strBaseDir, CFileItemList& items);
188   int GetVariousArtistsAlbumsCount();
189
190   bool SetSongRating(const CStdString &filePath, char rating);
191   bool SetScraperForPath(const CStdString& strPath, const ADDON::ScraperPtr& info);
192   bool GetScraperForPath(const CStdString& strPath, ADDON::ScraperPtr& info, const ADDON::TYPE &type);
193
194   /*! \brief Check whether a given scraper is in use.
195    \param scraperID the scraper to check for.
196    \return true if the scraper is in use, false otherwise.
197    */
198   bool ScraperInUse(const CStdString &scraperID) const;
199
200   void ExportToXML(const CStdString &xmlFile, bool singleFiles = false, bool images=false, bool overwrite=false);
201   void ImportFromXML(const CStdString &xmlFile);
202
203   void ExportKaraokeInfo(const CStdString &outFile, bool asHTML );
204   void ImportKaraokeInfo(const CStdString &inputFile );
205
206   void SetPropertiesForFileItem(CFileItem& item);
207   static void SetPropertiesFromArtist(CFileItem& item, const CArtist& artist);
208   static void SetPropertiesFromAlbum(CFileItem& item, const CAlbum& album);
209 protected:
210   std::map<CStdString, int /*CArtistCache*/> m_artistCache;
211   std::map<CStdString, int /*CGenreCache*/> m_genreCache;
212   std::map<CStdString, int /*CPathCache*/> m_pathCache;
213   std::map<CStdString, int /*CPathCache*/> m_thumbCache;
214   std::map<CStdString, CAlbumCache> m_albumCache;
215
216   virtual bool CreateTables();
217   virtual int GetMinVersion() const { return 16; };
218   const char *GetBaseDBName() const { return "MyMusic"; };
219
220   int AddAlbum(const CStdString& strAlbum1, int idArtist, const CStdString &extraArtists, const CStdString &strArtist1, int idThumb, int idGenre, const CStdString &extraGenres, int year);
221   int AddGenre(const CStdString& strGenre);
222   int AddArtist(const CStdString& strArtist);
223   int AddPath(const CStdString& strPath);
224   int AddThumb(const CStdString& strThumb1);
225   void AddExtraAlbumArtists(const CStdStringArray& vecArtists, int idAlbum);
226   void AddExtraSongArtists(const CStdStringArray& vecArtists, int idSong, bool bCheck = true);
227   void AddKaraokeData(const CSong& song);
228   void AddExtraGenres(const CStdStringArray& vecGenres, int idSong, int idAlbum, bool bCheck = true);
229   bool SetAlbumInfoSongs(int idAlbumInfo, const VECSONGS& songs);
230   bool GetAlbumInfoSongs(int idAlbumInfo, VECSONGS& songs);
231 private:
232   void SplitString(const CStdString &multiString, std::vector<CStdString> &vecStrings, CStdString &extraStrings);
233   CSong GetSongFromDataset(bool bWithMusicDbPath=false);
234   CArtist GetArtistFromDataset(dbiplus::Dataset* pDS, bool needThumb=true);
235   CAlbum GetAlbumFromDataset(dbiplus::Dataset* pDS, bool imageURL=false);
236   void GetFileItemFromDataset(CFileItem* item, const CStdString& strMusicDBbasePath);
237   bool CleanupSongs();
238   bool CleanupSongsByIds(const CStdString &strSongIds);
239   bool CleanupPaths();
240   bool CleanupThumbs();
241   bool CleanupAlbums();
242   bool CleanupArtists();
243   bool CleanupGenres();
244   virtual bool UpdateOldVersion(int version);
245   bool SearchArtists(const CStdString& search, CFileItemList &artists);
246   bool SearchAlbums(const CStdString& search, CFileItemList &albums);
247   bool SearchSongs(const CStdString& strSearch, CFileItemList &songs);
248   int GetSongIDFromPath(const CStdString &filePath);
249
250   // Fields should be ordered as they
251   // appear in the songview
252   enum _SongFields
253   {
254     song_idSong=0,
255     song_strExtraArtists,
256     song_strExtraGenres,
257     song_strTitle,
258     song_iTrack,
259     song_iDuration,
260     song_iYear,
261     song_dwFileNameCRC,
262     song_strFileName,
263     song_strMusicBrainzTrackID,
264     song_strMusicBrainzArtistID,
265     song_strMusicBrainzAlbumID,
266     song_strMusicBrainzAlbumArtistID,
267     song_strMusicBrainzTRMID,
268     song_iTimesPlayed,
269     song_iStartOffset,
270     song_iEndOffset,
271     song_lastplayed,
272     song_rating,
273     song_comment,
274     song_idAlbum,
275     song_strAlbum,
276     song_strPath,
277     song_idArtist,
278     song_strArtist,
279     song_idGenre,
280     song_strGenre,
281     song_strThumb,
282     song_iKarNumber,
283     song_iKarDelay,
284     song_strKarEncoding
285   } SongFields;
286
287   // Fields should be ordered as they
288   // appear in the albumview
289   enum _AlbumFields
290   {
291     album_idAlbum=0,
292     album_strAlbum,
293     album_strExtraArtists,
294     album_idArtist,
295     album_strExtraGenres,
296     album_idGenre,
297     album_strArtist,
298     album_strGenre,
299     album_iYear,
300     album_strThumb,
301     album_idAlbumInfo,
302     album_strMoods,
303     album_strStyles,
304     album_strThemes,
305     album_strReview,
306     album_strLabel,
307     album_strType,
308     album_strThumbURL,
309     album_iRating
310   } AlbumFields;
311
312   enum _ArtistFields
313   {
314     artist_idArtist=1, // not a typo - we have the primary key @ 0
315     artist_strBorn,
316     artist_strFormed,
317     artist_strGenres,
318     artist_strMoods,
319     artist_strStyles,
320     artist_strInstruments,
321     artist_strBiography,
322     artist_strDied,
323     artist_strDisbanded,
324     artist_strYearsActive,
325     artist_strImage,
326     artist_strFanart
327   } ArtistFields;
328
329   void AnnounceRemove(std::string content, int id);
330   void AnnounceUpdate(std::string content, int id);
331 };