[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / utils / RecentlyAddedJob.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
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)
8  *  any later version.
9  *
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.
14  *
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/>.
18  *
19  */
20
21 #include "utils/log.h"
22 #include "video/VideoDatabase.h"
23 #include "video/VideoInfoTag.h"
24 #include "FileItem.h"
25 #include "RecentlyAddedJob.h"
26 #include "guilib/GUIWindow.h"
27 #include "guilib/GUIWindowManager.h"
28 #include "guilib/WindowIDs.h"
29 #include "music/MusicDatabase.h"
30 #include "music/tags/MusicInfoTag.h"
31 #include "utils/Variant.h"
32 #include "utils/StringUtils.h"
33 #include "settings/AdvancedSettings.h"
34 #include "music/MusicThumbLoader.h"
35 #include "video/VideoThumbLoader.h"
36
37 #define NUM_ITEMS 10
38
39 CRecentlyAddedJob::CRecentlyAddedJob(int flag)
40 {
41   m_flag = flag;
42
43
44 bool CRecentlyAddedJob::UpdateVideo()
45 {
46   CGUIWindow* home = g_windowManager.GetWindow(WINDOW_HOME);
47
48   if ( home == NULL )
49     return false;
50
51   CLog::Log(LOGDEBUG, "CRecentlyAddedJob::UpdateVideos() - Running RecentlyAdded home screen update");
52   
53   int            i = 0;
54   CFileItemList  items;
55   CVideoDatabase videodatabase;
56   CVideoThumbLoader loader;
57   loader.OnLoaderStart();
58   
59   videodatabase.Open();
60
61   if (videodatabase.GetRecentlyAddedMoviesNav("videodb://recentlyaddedmovies/", items, NUM_ITEMS))
62   {  
63     for (; i < items.Size(); ++i)
64     {
65       CFileItemPtr item = items.Get(i);
66       CStdString   value = StringUtils::Format("%i", i + 1);
67       CStdString   strRating = StringUtils::Format("%.1f", item->GetVideoInfoTag()->m_fRating);;
68       
69       home->SetProperty("LatestMovie." + value + ".Title"       , item->GetLabel());
70       home->SetProperty("LatestMovie." + value + ".Rating"      , strRating);
71       home->SetProperty("LatestMovie." + value + ".Year"        , item->GetVideoInfoTag()->m_iYear);
72       home->SetProperty("LatestMovie." + value + ".Plot"        , item->GetVideoInfoTag()->m_strPlot);
73       home->SetProperty("LatestMovie." + value + ".RunningTime" , item->GetVideoInfoTag()->GetDuration() / 60);
74       home->SetProperty("LatestMovie." + value + ".Path"        , item->GetVideoInfoTag()->m_strFileNameAndPath);
75       home->SetProperty("LatestMovie." + value + ".Trailer"     , item->GetVideoInfoTag()->m_strTrailer);
76
77       if (!item->HasArt("thumb"))
78         loader.LoadItem(item.get());
79
80       home->SetProperty("LatestMovie." + value + ".Thumb"       , item->GetArt("thumb"));
81       home->SetProperty("LatestMovie." + value + ".Fanart"      , item->GetArt("fanart"));
82     }
83   } 
84   for (; i < NUM_ITEMS; ++i)
85   {
86     CStdString value = StringUtils::Format("%i", i + 1);
87     home->SetProperty("LatestMovie." + value + ".Title"       , "");
88     home->SetProperty("LatestMovie." + value + ".Thumb"       , "");
89     home->SetProperty("LatestMovie." + value + ".Rating"      , "");
90     home->SetProperty("LatestMovie." + value + ".Year"        , "");
91     home->SetProperty("LatestMovie." + value + ".Plot"        , "");
92     home->SetProperty("LatestMovie." + value + ".RunningTime" , "");
93     home->SetProperty("LatestMovie." + value + ".Path"        , "");
94     home->SetProperty("LatestMovie." + value + ".Trailer"     , "");
95     home->SetProperty("LatestMovie." + value + ".Fanart"      , "");
96   }
97  
98   i = 0;
99   CFileItemList  TVShowItems; 
100  
101   if (videodatabase.GetRecentlyAddedEpisodesNav("videodb://recentlyaddedepisodes/", TVShowItems, NUM_ITEMS))
102   {
103     for (; i < TVShowItems.Size(); ++i)
104     {    
105       CFileItemPtr item          = TVShowItems.Get(i);
106       int          EpisodeSeason = item->GetVideoInfoTag()->m_iSeason;
107       int          EpisodeNumber = item->GetVideoInfoTag()->m_iEpisode;
108       CStdString   EpisodeNo = StringUtils::Format("s%02de%02d", EpisodeSeason, EpisodeNumber);
109       CStdString   value = StringUtils::Format("%i", i + 1);
110       CStdString   strRating = StringUtils::Format("%.1f", item->GetVideoInfoTag()->m_fRating);
111
112       CFileItem show(item->GetVideoInfoTag()->m_strShowPath, true);
113
114       home->SetProperty("LatestEpisode." + value + ".ShowTitle"     , item->GetVideoInfoTag()->m_strShowTitle);
115       home->SetProperty("LatestEpisode." + value + ".EpisodeTitle"  , item->GetVideoInfoTag()->m_strTitle);
116       home->SetProperty("LatestEpisode." + value + ".Rating"        , strRating);      
117       home->SetProperty("LatestEpisode." + value + ".Plot"          , item->GetVideoInfoTag()->m_strPlot);
118       home->SetProperty("LatestEpisode." + value + ".EpisodeNo"     , EpisodeNo);
119       home->SetProperty("LatestEpisode." + value + ".EpisodeSeason" , EpisodeSeason);
120       home->SetProperty("LatestEpisode." + value + ".EpisodeNumber" , EpisodeNumber);
121       home->SetProperty("LatestEpisode." + value + ".Path"          , item->GetVideoInfoTag()->m_strFileNameAndPath);
122
123       if (!item->HasArt("thumb"))
124         loader.LoadItem(item.get());
125
126       std::string seasonThumb;
127       if (item->GetVideoInfoTag()->m_iIdSeason > 0)
128         seasonThumb = videodatabase.GetArtForItem(item->GetVideoInfoTag()->m_iIdSeason, "season", "thumb");
129
130       home->SetProperty("LatestEpisode." + value + ".Thumb"         , item->GetArt("thumb"));
131       home->SetProperty("LatestEpisode." + value + ".ShowThumb"     , item->GetArt("tvshow.thumb"));
132       home->SetProperty("LatestEpisode." + value + ".SeasonThumb"   , seasonThumb);
133       home->SetProperty("LatestEpisode." + value + ".Fanart"        , item->GetArt("fanart"));
134     }
135   } 
136   for (; i < NUM_ITEMS; ++i)
137   {
138     CStdString value = StringUtils::Format("%i", i + 1);
139     home->SetProperty("LatestEpisode." + value + ".ShowTitle"     , "");
140     home->SetProperty("LatestEpisode." + value + ".EpisodeTitle"  , "");
141     home->SetProperty("LatestEpisode." + value + ".Rating"        , "");      
142     home->SetProperty("LatestEpisode." + value + ".Plot"          , "");
143     home->SetProperty("LatestEpisode." + value + ".EpisodeNo"     , "");
144     home->SetProperty("LatestEpisode." + value + ".EpisodeSeason" , "");
145     home->SetProperty("LatestEpisode." + value + ".EpisodeNumber" , "");
146     home->SetProperty("LatestEpisode." + value + ".Path"          , "");
147     home->SetProperty("LatestEpisode." + value + ".Thumb"         , "");
148     home->SetProperty("LatestEpisode." + value + ".ShowThumb"     , "");
149     home->SetProperty("LatestEpisode." + value + ".SeasonThumb"   , "");
150     home->SetProperty("LatestEpisode." + value + ".Fanart"        , "");
151   }  
152
153   i = 0;
154   CFileItemList MusicVideoItems;
155
156   if (videodatabase.GetRecentlyAddedMusicVideosNav("videodb://recentlyaddedmusicvideos/", MusicVideoItems, NUM_ITEMS))
157   {
158     for (; i < MusicVideoItems.Size(); ++i)
159     {
160       CFileItemPtr item = MusicVideoItems.Get(i);
161       CStdString   value = StringUtils::Format("%i", i + 1);
162
163       home->SetProperty("LatestMusicVideo." + value + ".Title"       , item->GetLabel());
164       home->SetProperty("LatestMusicVideo." + value + ".Year"        , item->GetVideoInfoTag()->m_iYear);
165       home->SetProperty("LatestMusicVideo." + value + ".Plot"        , item->GetVideoInfoTag()->m_strPlot);
166       home->SetProperty("LatestMusicVideo." + value + ".RunningTime" , item->GetVideoInfoTag()->GetDuration() / 60);
167       home->SetProperty("LatestMusicVideo." + value + ".Path"        , item->GetVideoInfoTag()->m_strFileNameAndPath);
168       home->SetProperty("LatestMusicVideo." + value + ".Artist"      , StringUtils::Join(item->GetVideoInfoTag()->m_artist, g_advancedSettings.m_videoItemSeparator));
169
170       if (!item->HasArt("thumb"))
171         loader.LoadItem(item.get());
172
173       home->SetProperty("LatestMusicVideo." + value + ".Thumb"       , item->GetArt("thumb"));
174       home->SetProperty("LatestMusicVideo." + value + ".Fanart"      , item->GetArt("fanart"));
175     }
176   }
177   for (; i < NUM_ITEMS; ++i)
178   {
179     CStdString value = StringUtils::Format("%i", i + 1);
180     home->SetProperty("LatestMusicVideo." + value + ".Title"       , "");
181     home->SetProperty("LatestMusicVideo." + value + ".Thumb"       , "");
182     home->SetProperty("LatestMusicVideo." + value + ".Year"        , "");
183     home->SetProperty("LatestMusicVideo." + value + ".Plot"        , "");
184     home->SetProperty("LatestMusicVideo." + value + ".RunningTime" , "");
185     home->SetProperty("LatestMusicVideo." + value + ".Path"        , "");
186     home->SetProperty("LatestMusicVideo." + value + ".Artist"      , "");
187     home->SetProperty("LatestMusicVideo." + value + ".Fanart"      , "");
188   }
189
190   videodatabase.Close();
191   return true;
192 }
193
194 bool CRecentlyAddedJob::UpdateMusic()
195 {
196   CGUIWindow* home = g_windowManager.GetWindow(WINDOW_HOME);
197   
198   if ( home == NULL )
199     return false;
200   
201   CLog::Log(LOGDEBUG, "CRecentlyAddedJob::UpdateMusic() - Running RecentlyAdded home screen update");
202   
203   int            i = 0;
204   CFileItemList  musicItems;
205   CMusicDatabase musicdatabase;
206   CMusicThumbLoader loader;
207   loader.OnLoaderStart();
208   
209   musicdatabase.Open();
210   
211   if (musicdatabase.GetRecentlyAddedAlbumSongs("musicdb://songs/", musicItems, NUM_ITEMS))
212   {
213     long idAlbum = -1;
214     CStdString strAlbumThumb;
215     CStdString strAlbumFanart;
216     for (; i < musicItems.Size(); ++i)
217     {
218       CFileItemPtr item = musicItems.Get(i);
219       CStdString   value = StringUtils::Format("%i", i + 1);
220       
221       CStdString   strRating;
222       CStdString   strAlbum  = item->GetMusicInfoTag()->GetAlbum();
223       CStdString   strArtist = StringUtils::Join(item->GetMusicInfoTag()->GetArtist(), g_advancedSettings.m_musicItemSeparator);
224
225       if (idAlbum != item->GetMusicInfoTag()->GetAlbumId())
226       {
227         strAlbumThumb.clear();
228         strAlbumFanart.clear();
229         idAlbum = item->GetMusicInfoTag()->GetAlbumId();
230
231         if (loader.LoadItem(item.get()))
232         {
233           strAlbumThumb = item->GetArt("thumb");
234           strAlbumFanart = item->GetArt("fanart");
235         }
236       }
237
238       strRating = StringUtils::Format("%c", item->GetMusicInfoTag()->GetRating());
239       
240       home->SetProperty("LatestSong." + value + ".Title"   , item->GetMusicInfoTag()->GetTitle());
241       home->SetProperty("LatestSong." + value + ".Year"    , item->GetMusicInfoTag()->GetYear());
242       home->SetProperty("LatestSong." + value + ".Artist"  , strArtist);      
243       home->SetProperty("LatestSong." + value + ".Album"   , strAlbum);
244       home->SetProperty("LatestSong." + value + ".Rating"  , strRating);
245       home->SetProperty("LatestSong." + value + ".Path"    , item->GetMusicInfoTag()->GetURL());
246       home->SetProperty("LatestSong." + value + ".Thumb"   , strAlbumThumb);
247       home->SetProperty("LatestSong." + value + ".Fanart"  , strAlbumFanart);
248     }
249   }
250   for (; i < NUM_ITEMS; ++i)
251   {
252     CStdString value = StringUtils::Format("%i", i + 1);
253     home->SetProperty("LatestSong." + value + ".Title"   , "");
254     home->SetProperty("LatestSong." + value + ".Year"    , "");
255     home->SetProperty("LatestSong." + value + ".Artist"  , "");      
256     home->SetProperty("LatestSong." + value + ".Album"   , "");
257     home->SetProperty("LatestSong." + value + ".Rating"  , "");
258     home->SetProperty("LatestSong." + value + ".Path"    , "");
259     home->SetProperty("LatestSong." + value + ".Thumb"   , "");
260     home->SetProperty("LatestSong." + value + ".Fanart"  , "");
261   }
262   
263   i = 0;
264   VECALBUMS albums;
265   
266   if (musicdatabase.GetRecentlyAddedAlbums(albums, NUM_ITEMS))
267   { 
268     for (; i < (int)albums.size(); ++i)
269     {
270       CAlbum&    album=albums[i];
271       CStdString value = StringUtils::Format("%i", i + 1);
272       CStdString strThumb = musicdatabase.GetArtForItem(album.idAlbum, "album", "thumb");
273       CStdString strFanart = musicdatabase.GetArtistArtForItem(album.idAlbum, "album", "fanart");
274       CStdString strDBpath = StringUtils::Format("musicdb://albums/%i/", album.idAlbum);
275       CStdString strSQLAlbum = StringUtils::Format("idAlbum=%i", album.idAlbum);
276       CStdString strArtist = musicdatabase.GetSingleValue("albumview", "strArtists", strSQLAlbum);
277       
278       home->SetProperty("LatestAlbum." + value + ".Title"   , album.strAlbum);
279       home->SetProperty("LatestAlbum." + value + ".Year"    , album.iYear);
280       home->SetProperty("LatestAlbum." + value + ".Artist"  , strArtist);      
281       home->SetProperty("LatestAlbum." + value + ".Rating"  , album.iRating);
282       home->SetProperty("LatestAlbum." + value + ".Path"    , strDBpath);
283       home->SetProperty("LatestAlbum." + value + ".Thumb"   , strThumb);
284       home->SetProperty("LatestAlbum." + value + ".Fanart"  , strFanart);
285     }
286   }
287   for (; i < NUM_ITEMS; ++i)
288   {
289     CStdString value = StringUtils::Format("%i", i + 1);
290     home->SetProperty("LatestAlbum." + value + ".Title"   , "");
291     home->SetProperty("LatestAlbum." + value + ".Year"    , "");
292     home->SetProperty("LatestAlbum." + value + ".Artist"  , "");      
293     home->SetProperty("LatestAlbum." + value + ".Rating"  , "");
294     home->SetProperty("LatestAlbum." + value + ".Path"    , "");
295     home->SetProperty("LatestAlbum." + value + ".Thumb"   , "");
296     home->SetProperty("LatestAlbum." + value + ".Fanart"  , "");            
297   }
298   
299   musicdatabase.Close();
300   return true;
301 }
302
303 bool CRecentlyAddedJob::UpdateTotal()
304 {
305   CGUIWindow* home = g_windowManager.GetWindow(WINDOW_HOME);
306   
307   if ( home == NULL )
308     return false;
309   
310   CLog::Log(LOGDEBUG, "CRecentlyAddedJob::UpdateTotal() - Running RecentlyAdded home screen update");
311   
312   CVideoDatabase videodatabase;  
313   CMusicDatabase musicdatabase;
314   
315   musicdatabase.Open();
316   int MusSongTotals   = atoi(musicdatabase.GetSingleValue("songview"       , "count(1)"));
317   int MusAlbumTotals  = atoi(musicdatabase.GetSingleValue("songview"       , "count(distinct strAlbum)"));
318   int MusArtistTotals = atoi(musicdatabase.GetSingleValue("songview"       , "count(distinct strArtists)"));
319   musicdatabase.Close();
320  
321   videodatabase.Open();
322   int tvShowCount     = atoi(videodatabase.GetSingleValue("tvshowview"     , "count(1)"));
323   int movieTotals     = atoi(videodatabase.GetSingleValue("movieview"      , "count(1)"));
324   int movieWatched    = atoi(videodatabase.GetSingleValue("movieview"      , "count(playCount)"));
325   int MusVidTotals    = atoi(videodatabase.GetSingleValue("musicvideoview" , "count(1)"));
326   int MusVidWatched   = atoi(videodatabase.GetSingleValue("musicvideoview" , "count(playCount)"));
327   int EpWatched       = atoi(videodatabase.GetSingleValue("tvshowview"     , "sum(watchedcount)"));
328   int EpCount         = atoi(videodatabase.GetSingleValue("tvshowview"     , "sum(totalcount)"));
329   int TvShowsWatched  = atoi(videodatabase.GetSingleValue("tvshowview"     , "sum(watchedcount = totalcount)"));
330   videodatabase.Close();
331   
332   home->SetProperty("TVShows.Count"         , tvShowCount);
333   home->SetProperty("TVShows.Watched"       , TvShowsWatched);
334   home->SetProperty("TVShows.UnWatched"     , tvShowCount - TvShowsWatched);
335   home->SetProperty("Episodes.Count"        , EpCount);
336   home->SetProperty("Episodes.Watched"      , EpWatched);
337   home->SetProperty("Episodes.UnWatched"    , EpCount-EpWatched);  
338   home->SetProperty("Movies.Count"          , movieTotals);
339   home->SetProperty("Movies.Watched"        , movieWatched);
340   home->SetProperty("Movies.UnWatched"      , movieTotals - movieWatched);
341   home->SetProperty("MusicVideos.Count"     , MusVidTotals);
342   home->SetProperty("MusicVideos.Watched"   , MusVidWatched);
343   home->SetProperty("MusicVideos.UnWatched" , MusVidTotals - MusVidWatched);
344   home->SetProperty("Music.SongsCount"      , MusSongTotals);
345   home->SetProperty("Music.AlbumsCount"     , MusAlbumTotals);
346   home->SetProperty("Music.ArtistsCount"    , MusArtistTotals);
347   
348   return true;
349 }
350
351
352 bool CRecentlyAddedJob::DoWork()
353 {
354   bool ret = true;
355   if (m_flag & Audio)
356     ret &= UpdateMusic();
357   
358   if (m_flag & Video)
359     ret &= UpdateVideo();
360   
361   if (m_flag & Totals)
362     ret &= UpdateTotal();
363     
364   return ret; 
365 }