2 * Copyright (C) 2012-2013 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, see
17 * <http://www.gnu.org/licenses/>.
21 #include "MusicThumbLoader.h"
23 #include "TextureCache.h"
24 #include "music/tags/MusicInfoTag.h"
25 #include "music/tags/MusicInfoTagLoaderFactory.h"
26 #include "music/infoscanner/MusicInfoScanner.h"
27 #include "music/Artist.h"
28 #include "video/VideoThumbLoader.h"
31 using namespace MUSIC_INFO;
33 CMusicThumbLoader::CMusicThumbLoader() : CThumbLoader()
35 m_database = new CMusicDatabase;
38 CMusicThumbLoader::~CMusicThumbLoader()
43 void CMusicThumbLoader::Initialize()
49 void CMusicThumbLoader::Deinitialize()
55 void CMusicThumbLoader::OnLoaderStart()
60 void CMusicThumbLoader::OnLoaderFinish()
65 bool CMusicThumbLoader::LoadItem(CFileItem* pItem)
67 if (pItem->m_bIsShareOrDrive)
70 if (pItem->HasMusicInfoTag() && pItem->GetArt().empty())
72 if (FillLibraryArt(*pItem))
74 if (pItem->GetMusicInfoTag()->GetType() == "artist")
75 return true; // no fallback
78 if (pItem->HasVideoInfoTag() && pItem->GetArt().empty())
80 CVideoThumbLoader loader;
81 if (loader.LoadItem(pItem))
85 if (!pItem->HasArt("fanart"))
87 if (pItem->HasMusicInfoTag() && !pItem->GetMusicInfoTag()->GetArtist().empty())
89 std::string artist = pItem->GetMusicInfoTag()->GetArtist()[0];
91 int idArtist = m_database->GetArtistByName(artist);
94 string fanart = m_database->GetArtForItem(idArtist, "artist", "fanart");
97 pItem->SetArt("artist.fanart", fanart);
98 pItem->SetArtFallback("fanart", "artist.fanart");
105 if (!pItem->HasArt("thumb"))
107 // Look for embedded art
108 if (pItem->HasMusicInfoTag() && !pItem->GetMusicInfoTag()->GetCoverArtInfo().empty())
110 // The item has got embedded art but user thumbs overrule, so check for those first
111 if (!FillThumb(*pItem, false)) // Check for user thumbs but ignore folder thumbs
113 // No user thumb, use embedded art
114 CStdString thumb = CTextureCache::GetWrappedImageURL(pItem->GetPath(), "music");
115 pItem->SetArt("thumb", thumb);
120 // Check for user thumbs
121 FillThumb(*pItem, true);
128 bool CMusicThumbLoader::FillThumb(CFileItem &item, bool folderThumbs /* = true */)
130 if (item.HasArt("thumb"))
132 CStdString thumb = GetCachedImage(item, "thumb");
135 thumb = item.GetUserMusicThumb(false, folderThumbs);
136 if (!thumb.IsEmpty())
137 SetCachedImage(item, "thumb", thumb);
139 item.SetArt("thumb", thumb);
140 return !thumb.IsEmpty();
143 bool CMusicThumbLoader::FillLibraryArt(CFileItem &item)
145 CMusicInfoTag &tag = *item.GetMusicInfoTag();
146 if (tag.GetDatabaseId() > -1 && !tag.GetType().empty())
149 map<string, string> artwork;
150 if (m_database->GetArtForItem(tag.GetDatabaseId(), tag.GetType(), artwork))
151 item.SetArt(artwork);
152 else if (tag.GetType() == "song")
153 { // no art for the song, try the album
154 ArtCache::const_iterator i = m_albumArt.find(tag.GetAlbumId());
155 if (i == m_albumArt.end())
157 m_database->GetArtForItem(tag.GetAlbumId(), "album", artwork);
158 i = m_albumArt.insert(make_pair(tag.GetAlbumId(), artwork)).first;
160 if (i != m_albumArt.end())
162 item.AppendArt(i->second, "album");
163 for (map<string, string>::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
164 item.SetArtFallback(j->first, "album." + j->first);
167 if (tag.GetType() == "song" || tag.GetType() == "album")
168 { // fanart from the artist
169 string fanart = m_database->GetArtistArtForItem(tag.GetDatabaseId(), tag.GetType(), "fanart");
172 item.SetArt("artist.fanart", fanart);
173 item.SetArtFallback("fanart", "artist.fanart");
178 return !item.GetArt().empty();
181 bool CMusicThumbLoader::GetEmbeddedThumb(const std::string &path, EmbeddedArt &art)
183 auto_ptr<IMusicInfoTagLoader> pLoader (CMusicInfoTagLoaderFactory::CreateLoader(path));
185 if (NULL != pLoader.get())
186 pLoader->Load(path, tag, &art);