strip added smb:// shares of their user/pass when adding, and instead store that...
[vuplus_xbmc] / xbmc / music / infoscanner / MusicInfoScanner.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2008 Team XBMC
4  *      http://www.xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
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
20  *
21  */
22 #include "threads/Thread.h"
23 #include "music/MusicDatabase.h"
24 #include "MusicAlbumInfo.h"
25
26 class CAlbum;
27 class CArtist;
28
29 namespace MUSIC_INFO
30 {
31 enum SCAN_STATE { PREPARING = 0, REMOVING_OLD, CLEANING_UP_DATABASE, READING_MUSIC_INFO, DOWNLOADING_ALBUM_INFO, DOWNLOADING_ARTIST_INFO, COMPRESSING_DATABASE, WRITING_CHANGES };
32
33 class IMusicInfoScannerObserver
34 {
35 public:
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;
42 };
43
44 class CMusicInfoScanner : CThread, public IRunnable
45 {
46 public:
47   CMusicInfoScanner();
48   virtual ~CMusicInfoScanner();
49
50   void Start(const CStdString& strDirectory);
51   void FetchAlbumInfo(const CStdString& strDirectory, bool refresh=false);
52   void FetchArtistInfo(const CStdString& strDirectory, bool refresh=false);
53   bool IsScanning();
54   void Stop();
55   void SetObserver(IMusicInfoScannerObserver* pObserver);
56
57   /*! \brief Categorise songs into albums
58    Albums are defined uniquely by the album name and album artist.
59
60    If albumartist is not available in a song, we determine it from the
61    common portion of each song's artist list.
62
63    eg the common artist for
64      Bob Dylan / Tom Petty / Roy Orbison
65      Bob Dylan / Tom Petty
66    would be "Bob Dylan / Tom Petty".
67
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.
74
75    \param songs [in/out] list of songs to categorise - albumartist field may be altered.
76    \param albums [out] albums found within these songs.
77    */
78   static void CategoriseAlbums(VECSONGS &songs, VECALBUMS &albums);
79
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.
83
84    In order of priority:
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
90        to the album.
91
92    \param albums [in/out] list of albums to categorise - art field may be altered.
93    \param path [in] path containing albums.
94    */
95   static void FindArtForAlbums(VECALBUMS &albums, const CStdString &path);
96
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);
99
100   std::map<std::string, std::string> GetArtistArtwork(long id, const CArtist *artist = NULL);
101 protected:
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);
106
107   bool DoScan(const CStdString& strDirectory);
108
109   virtual void Run();
110   int CountFiles(const CFileItemList& items, bool recursive);
111   int CountFilesRecursively(const CStdString& strPath);
112
113 protected:
114   IMusicInfoScannerObserver* m_pObserver;
115   int m_currentItem;
116   int m_itemCount;
117   bool m_bRunning;
118   bool m_bCanInterrupt;
119   bool m_needsCleanup;
120   int m_scanType; // 0 - load from files, 1 - albums, 2 - artists
121   CMusicDatabase m_musicDatabase;
122
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;
129 };
130 }