3 * Copyright (C) 2005-2008 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, write to
18 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 #include "threads/Thread.h"
23 #include "music/MusicDatabase.h"
24 #include "MusicAlbumInfo.h"
31 enum SCAN_STATE { PREPARING = 0, REMOVING_OLD, CLEANING_UP_DATABASE, READING_MUSIC_INFO, DOWNLOADING_ALBUM_INFO, DOWNLOADING_ARTIST_INFO, COMPRESSING_DATABASE, WRITING_CHANGES };
33 class IMusicInfoScannerObserver
36 virtual ~IMusicInfoScannerObserver() {}
37 virtual void OnStateChanged(SCAN_STATE state) = 0;
38 virtual void OnDirectoryChanged(const CStdString& strDirectory) = 0;
39 virtual void OnDirectoryScanned(const CStdString& strDirectory) = 0;
40 virtual void OnSetProgress(int currentItem, int itemCount)=0;
41 virtual void OnFinished() = 0;
44 class CMusicInfoScanner : CThread, public IRunnable
48 virtual ~CMusicInfoScanner();
50 void Start(const CStdString& strDirectory);
51 void FetchAlbumInfo(const CStdString& strDirectory, bool refresh=false);
52 void FetchArtistInfo(const CStdString& strDirectory, bool refresh=false);
55 void SetObserver(IMusicInfoScannerObserver* pObserver);
57 /*! \brief Categorise songs into albums
58 Albums are defined uniquely by the album name and album artist.
60 If albumartist is not available in a song, we determine it from the
61 common portion of each song's artist list.
63 eg the common artist for
64 Bob Dylan / Tom Petty / Roy Orbison
66 would be "Bob Dylan / Tom Petty".
68 If all songs that share an album
69 1. have a non-empty album name
70 2. have at least two different primary artists
71 3. have no album artist set
72 4. and no track numbers overlap
73 we assume it is a various artists album, and set the albumartist field accordingly.
75 \param songs [in/out] list of songs to categorise - albumartist field may be altered.
76 \param albums [out] albums found within these songs.
78 static void CategoriseAlbums(VECSONGS &songs, VECALBUMS &albums);
80 /*! \brief Find art for albums
81 Based on the albums in the folder, finds whether we have unique album art
82 and assigns to the album if we do.
85 1. If there is a single album in the folder, then the folder art is assigned to the album.
86 2. We find the art for each song. A .tbn file takes priority over embedded art.
87 3. If we have a unique piece of art for all songs in the album, we assign that to the album
88 and remove that art from each song so that they inherit from the album.
89 4. If there is not a unique piece of art for each song, then no art is assigned
92 \param albums [in/out] list of albums to categorise - art field may be altered.
93 \param path [in] path containing albums.
95 static void FindArtForAlbums(VECALBUMS &albums, const CStdString &path);
97 bool DownloadAlbumInfo(const CStdString& strPath, const CStdString& strArtist, const CStdString& strAlbum, bool& bCanceled, MUSIC_GRABBER::CMusicAlbumInfo& album, CGUIDialogProgress* pDialog=NULL);
98 bool DownloadArtistInfo(const CStdString& strPath, const CStdString& strArtist, bool& bCanceled, CGUIDialogProgress* pDialog=NULL);
100 std::map<std::string, std::string> GetArtistArtwork(long id, const CArtist *artist = NULL);
102 virtual void Process();
103 int RetrieveMusicInfo(CFileItemList& items, const CStdString& strDirectory);
104 int GetPathHash(const CFileItemList &items, CStdString &hash);
105 void GetAlbumArtwork(long id, const CAlbum &artist);
107 bool DoScan(const CStdString& strDirectory);
110 int CountFiles(const CFileItemList& items, bool recursive);
111 int CountFilesRecursively(const CStdString& strPath);
114 IMusicInfoScannerObserver* m_pObserver;
118 bool m_bCanInterrupt;
120 int m_scanType; // 0 - load from files, 1 - albums, 2 - artists
121 CMusicDatabase m_musicDatabase;
123 std::set<CStdString> m_pathsToScan;
124 std::set<CAlbum> m_albumsToScan;
125 std::set<CArtist> m_artistsToScan;
126 std::set<CStdString> m_pathsToCount;
127 std::vector<long> m_artistsScanned;
128 std::vector<long> m_albumsScanned;