2 * Copyright (C) 2005-2008 Team XBMC
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)
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.
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
26 #include "dbwrappers/Database.h"
28 #include "addons/Scraper.h"
35 // return codes of Cleaning up the Database
36 // numbers are strings from strings.xml
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
48 #define NUM_SONGS_BEFORE_COMMIT 500
52 \brief A set of CStdString objects, used for CMusicDatabase
53 \sa ISETPATHES, CMusicDatabase
55 typedef std::set<CStdString> SETPATHES;
59 \brief The SETPATHES iterator
60 \sa SETPATHES, CMusicDatabase
62 typedef std::set<CStdString>::iterator ISETPATHES;
64 class CGUIDialogProgress;
69 \brief Class to store and read tag information
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).
75 Here is the database layout:
76 \image html musicdatabase.png
78 \sa CAlbum, CSong, VECSONGS, CMapSong, VECARTISTS, VECALBUMS, VECGENRES
80 class CMusicDatabase : public CDatabase
103 class CAlbumCache : public CAlbum
111 CMusicDatabase(void);
112 virtual ~CMusicDatabase(void);
115 virtual bool CommitTransaction();
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);
138 bool GetAlbumFromSong(int idSong, CAlbum &album);
139 bool GetAlbumFromSong(const CSong &song, CAlbum &album);
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);
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);
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);
177 bool GetGenreById(int idGenre, CStdString& strGenre);
178 bool GetArtistById(int idArtist, CStdString& strArtist);
179 bool GetAlbumById(int idAlbum, CStdString& strAlbum);
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);
186 bool GetVariousArtistsAlbums(const CStdString& strBaseDir, CFileItemList& items);
187 bool GetVariousArtistsAlbumsSongs(const CStdString& strBaseDir, CFileItemList& items);
188 int GetVariousArtistsAlbumsCount();
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);
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.
198 bool ScraperInUse(const CStdString &scraperID) const;
200 void ExportToXML(const CStdString &xmlFile, bool singleFiles = false, bool images=false, bool overwrite=false);
201 void ImportFromXML(const CStdString &xmlFile);
203 void ExportKaraokeInfo(const CStdString &outFile, bool asHTML );
204 void ImportKaraokeInfo(const CStdString &inputFile );
206 void SetPropertiesForFileItem(CFileItem& item);
207 static void SetPropertiesFromArtist(CFileItem& item, const CArtist& artist);
208 static void SetPropertiesFromAlbum(CFileItem& item, const CAlbum& album);
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;
216 virtual bool CreateTables();
217 virtual int GetMinVersion() const { return 16; };
218 const char *GetBaseDBName() const { return "MyMusic"; };
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);
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);
238 bool CleanupSongsByIds(const CStdString &strSongIds);
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);
250 // Fields should be ordered as they
251 // appear in the songview
255 song_strExtraArtists,
263 song_strMusicBrainzTrackID,
264 song_strMusicBrainzArtistID,
265 song_strMusicBrainzAlbumID,
266 song_strMusicBrainzAlbumArtistID,
267 song_strMusicBrainzTRMID,
287 // Fields should be ordered as they
288 // appear in the albumview
293 album_strExtraArtists,
295 album_strExtraGenres,
314 artist_idArtist=1, // not a typo - we have the primary key @ 0
320 artist_strInstruments,
324 artist_strYearsActive,
329 void AnnounceRemove(std::string content, int id);
330 void AnnounceUpdate(std::string content, int id);