2 * Copyright (C) 2005-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 "GUIDialogSongInfo.h"
22 #include "utils/URIUtils.h"
23 #include "utils/StringUtils.h"
24 #include "dialogs/GUIDialogFileBrowser.h"
25 #include "GUIPassword.h"
26 #include "GUIUserMessages.h"
27 #include "music/MusicDatabase.h"
28 #include "music/windows/GUIWindowMusicBase.h"
29 #include "music/tags/MusicInfoTag.h"
30 #include "guilib/GUIWindowManager.h"
31 #include "guilib/Key.h"
32 #include "filesystem/File.h"
33 #include "filesystem/CurlFile.h"
35 #include "settings/AdvancedSettings.h"
36 #include "settings/MediaSourceSettings.h"
37 #include "settings/Settings.h"
38 #include "guilib/LocalizeStrings.h"
39 #include "TextureCache.h"
40 #include "music/Album.h"
41 #include "storage/MediaManager.h"
42 #include "GUIDialogMusicInfo.h"
44 using namespace XFILE;
47 #define CONTROL_CANCEL 11
48 #define CONTROL_ALBUMINFO 12
49 #define CONTROL_GETTHUMB 13
51 CGUIDialogSongInfo::CGUIDialogSongInfo(void)
52 : CGUIDialog(WINDOW_DIALOG_SONG_INFO, "DialogSongInfo.xml")
53 , m_song(new CFileItem)
56 m_needsUpdate = false;
58 m_loadType = KEEP_IN_MEMORY;
61 CGUIDialogSongInfo::~CGUIDialogSongInfo(void)
65 bool CGUIDialogSongInfo::OnMessage(CGUIMessage& message)
67 switch (message.GetMessage())
69 case GUI_MSG_WINDOW_DEINIT:
71 if (!m_cancelled && m_startRating != m_song->GetMusicInfoTag()->GetRating())
74 if (db.Open()) // OpenForWrite() ?
76 db.SetSongRating(m_song->GetPath(), m_song->GetMusicInfoTag()->GetRating());
82 { // cancelled - reset the song rating
83 SetRating(m_startRating);
84 m_needsUpdate = false;
88 case GUI_MSG_WINDOW_INIT:
94 int iControl = message.GetSenderId();
95 if (iControl == CONTROL_CANCEL)
101 else if (iControl == CONTROL_OK)
107 else if (iControl == CONTROL_ALBUMINFO)
109 CGUIWindowMusicBase *window = (CGUIWindowMusicBase *)g_windowManager.GetWindow(WINDOW_MUSIC_NAV);
112 CFileItem item(*m_song);
113 CStdString path = StringUtils::Format("musicdb://albums/%li",m_albumId);
115 item.m_bIsFolder = true;
116 window->OnInfo(&item, true);
120 else if (iControl == CONTROL_GETTHUMB)
129 return CGUIDialog::OnMessage(message);
132 bool CGUIDialogSongInfo::OnAction(const CAction &action)
134 char rating = m_song->GetMusicInfoTag()->GetRating();
135 if (action.GetID() == ACTION_INCREASE_RATING)
138 SetRating(rating + 1);
141 else if (action.GetID() == ACTION_DECREASE_RATING)
144 SetRating(rating - 1);
147 else if (action.GetID() == ACTION_SHOW_INFO)
152 return CGUIDialog::OnAction(action);
155 bool CGUIDialogSongInfo::OnBack(int actionID)
158 return CGUIDialog::OnBack(actionID);
161 void CGUIDialogSongInfo::OnInitWindow()
166 // no known db info - check if parent dir is an album
167 if (m_song->GetMusicInfoTag()->GetDatabaseId() == -1)
169 CStdString path = URIUtils::GetDirectory(m_song->GetPath());
170 m_albumId = db.GetAlbumIdByPath(path);
175 db.GetAlbumFromSong(m_song->GetMusicInfoTag()->GetDatabaseId(),album);
176 m_albumId = album.idAlbum;
178 CONTROL_ENABLE_ON_CONDITION(CONTROL_ALBUMINFO, m_albumId > -1);
180 CGUIDialog::OnInitWindow();
183 void CGUIDialogSongInfo::SetRating(char rating)
185 if (rating < '0') rating = '0';
186 if (rating > '5') rating = '5';
187 if (rating != m_song->GetMusicInfoTag()->GetRating())
189 m_song->GetMusicInfoTag()->SetRating(rating);
190 // send a message to all windows to tell them to update the fileitem (eg playlistplayer, media windows)
191 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_UPDATE_ITEM, 0, m_song);
192 g_windowManager.SendMessage(msg);
196 void CGUIDialogSongInfo::SetSong(CFileItem *item)
199 m_song->LoadMusicTag();
200 m_startRating = m_song->GetMusicInfoTag()->GetRating();
201 MUSIC_INFO::CMusicInfoLoader::LoadAdditionalTagInfo(m_song.get());
202 // set artist thumb as well
205 if (item->IsMusicDb())
207 std::vector<int> artists;
208 CVariant artistthumbs;
209 db.GetArtistsBySong(item->GetMusicInfoTag()->GetDatabaseId(), true, artists);
210 for (std::vector<int>::const_iterator artistId = artists.begin(); artistId != artists.end(); ++artistId)
212 std::string thumb = db.GetArtForItem(*artistId, "artist", "thumb");
214 artistthumbs.push_back(thumb);
216 if (artistthumbs.size())
218 m_song->SetProperty("artistthumbs", artistthumbs);
219 m_song->SetProperty("artistthumb", artistthumbs[0]);
222 else if (m_song->HasMusicInfoTag() && !m_song->GetMusicInfoTag()->GetArtist().empty())
224 int idArtist = db.GetArtistByName(m_song->GetMusicInfoTag()->GetArtist()[0]);
225 std::string thumb = db.GetArtForItem(idArtist, "artist", "thumb");
227 m_song->SetProperty("artistthumb", thumb);
229 m_needsUpdate = false;
232 CFileItemPtr CGUIDialogSongInfo::GetCurrentListItem(int offset)
237 bool CGUIDialogSongInfo::DownloadThumbnail(const CStdString &thumbFile)
239 // TODO: Obtain the source...
242 http.Download(source, thumbFile);
246 // Get Thumb from user choice.
249 // 2. AllMusic.com thumb
251 // 4. No thumb (if no Local thumb is available)
253 // TODO: Currently no support for "embedded thumb" as there is no easy way to grab it
254 // without sending a file that has this as it's album to this class
255 void CGUIDialogSongInfo::OnGetThumb()
260 // Grab the thumbnail from the web
262 CStdString thumbFromWeb;
263 thumbFromWeb = URIUtils::AddFileToFolder(g_advancedSettings.m_cachePath, "allmusicThumb.jpg");
264 if (DownloadThumbnail(thumbFromWeb))
266 CFileItemPtr item(new CFileItem("thumb://allmusic.com", false));
267 item->SetArt("thumb", thumbFromWeb);
268 item->SetLabel(g_localizeStrings.Get(20055));
273 if (CFile::Exists(m_song->GetArt("thumb")))
275 CFileItemPtr item(new CFileItem("thumb://Current", false));
276 item->SetArt("thumb", m_song->GetArt("thumb"));
277 item->SetLabel(g_localizeStrings.Get(20016));
282 CStdString cachedLocalThumb;
283 CStdString localThumb(m_song->GetUserMusicThumb(true));
284 if (m_song->IsMusicDb())
286 CFileItem item(m_song->GetMusicInfoTag()->GetURL(), false);
287 localThumb = item.GetUserMusicThumb(true);
289 if (CFile::Exists(localThumb))
291 CFileItemPtr item(new CFileItem("thumb://Local", false));
292 item->SetArt("thumb", localThumb);
293 item->SetLabel(g_localizeStrings.Get(20017));
297 { // no local thumb exists, so we are just using the allmusic.com thumb or cached thumb
298 // which is probably the allmusic.com thumb. These could be wrong, so allow the user
299 // to delete the incorrect thumb
300 CFileItemPtr item(new CFileItem("thumb://None", false));
301 item->SetArt("thumb", "DefaultAlbumCover.png");
302 item->SetLabel(g_localizeStrings.Get(20018));
307 VECSOURCES sources(*CMediaSourceSettings::Get().GetSources("music"));
308 CGUIDialogMusicInfo::AddItemPathToFileBrowserSources(sources, *m_song);
309 g_mediaManager.GetLocalDrives(sources);
310 if (!CGUIDialogFileBrowser::ShowAndGetImage(items, sources, g_localizeStrings.Get(1030), result))
311 return; // user cancelled
313 if (result == "thumb://Current")
314 return; // user chose the one they have
316 // delete the thumbnail if that's what the user wants, else overwrite with the
320 if (result == "thumb://None")
322 else if (result == "thumb://allmusic.com")
324 else if (result == "thumb://Local")
325 newThumb = localThumb;
329 // update thumb in the database
333 db.SetArtForItem(m_song->GetMusicInfoTag()->GetDatabaseId(), m_song->GetMusicInfoTag()->GetType(), "thumb", newThumb);
337 m_song->SetArt("thumb", newThumb);
339 // tell our GUI to completely reload all controls (as some of them
340 // are likely to have had this image in use so will need refreshing)
341 CGUIMessage msg(GUI_MSG_NOTIFY_ALL, 0, 0, GUI_MSG_REFRESH_THUMBS);
342 g_windowManager.SendMessage(msg);
344 // m_hasUpdatedThumb = true;