コード例 #1
0
        /// <summary>
        /// Download all images attached to the GUI List Control
        /// TODO: Make part of a GUI Base Window
        /// </summary>
        /// <param name="itemsWithThumbs">List of images to get</param>
        internal static void GetImages(List <GUITmdbImage> itemsWithThumbs)
        {
            StopDownload = false;

            // split the downloads in 5+ groups and do multithreaded downloading
            int groupSize = (int)Math.Max(1, Math.Floor((double)itemsWithThumbs.Count / 5));
            int groups    = (int)Math.Ceiling((double)itemsWithThumbs.Count() / groupSize);

            for (int i = 0; i < groups; i++)
            {
                var groupList = new List <GUITmdbImage>();
                for (int j = groupSize * i; j < groupSize * i + (groupSize * (i + 1) > itemsWithThumbs.Count ? itemsWithThumbs.Count - groupSize * i : groupSize); j++)
                {
                    groupList.Add(itemsWithThumbs[j]);
                }

                // sort images so that images that already exist are displayed first
                //groupList.Sort((s1, s2) =>
                //{
                //    int x = Convert.ToInt32(File.Exists(s1.SeasonImages.Poster.LocalImageFilename(ArtworkType.SeasonPoster)));
                //    int y = Convert.ToInt32(File.Exists(s2.SeasonImages.Poster.LocalImageFilename(ArtworkType.SeasonPoster)));
                //    return y.CompareTo(x);
                //});

                new Thread(obj =>
                {
                    var items = (List <GUITmdbImage>)obj;
                    if (items == null || items.Count == 0)
                    {
                        return;
                    }

                    // all seasons should have the same show reference
                    var showImages = TmdbCache.GetShowImages(items.First().SeasonImages.Id);
                    if (showImages != null)
                    {
                        items.ForEach(s => s.ShowImages = showImages);
                    }

                    foreach (var item in items)
                    {
                        #region Season Poster
                        // stop download if we have exited window
                        if (StopDownload)
                        {
                            break;
                        }

                        bool downloadShowPoster = false;

                        string remoteThumb = string.Empty;
                        string localThumb  = string.Empty;

                        var seasonImages = TmdbCache.GetSeasonImages(item.SeasonImages.Id, item.SeasonImages.Season);
                        if (seasonImages != null)
                        {
                            item.SeasonImages = seasonImages;
                        }

                        if (seasonImages != null && seasonImages.Posters != null && seasonImages.Posters.Count > 0)
                        {
                            remoteThumb = TmdbCache.GetSeasonPosterUrl(seasonImages);
                            localThumb  = TmdbCache.GetSeasonPosterFilename(seasonImages);
                        }
                        else
                        {
                            downloadShowPoster = true;

                            // use show image if season poster not available
                            remoteThumb = TmdbCache.GetShowPosterUrl(showImages);
                            localThumb  = TmdbCache.GetShowPosterFilename(showImages);
                        }

                        if (!string.IsNullOrEmpty(remoteThumb) && !string.IsNullOrEmpty(localThumb))
                        {
                            if (GUIImageHandler.DownloadImage(remoteThumb, localThumb))
                            {
                                // notify that image has been downloaded
                                item.NotifyPropertyChanged(downloadShowPoster ? "ShowPoster" : "SeasonPoster");
                            }
                        }
                        #endregion

                        #region Fanart
                        // stop download if we have exited window
                        if (StopDownload)
                        {
                            break;
                        }
                        if (!TraktSettings.DownloadFanart)
                        {
                            continue;
                        }

                        string remoteFanart = TmdbCache.GetShowBackdropUrl(showImages);
                        string localFanart  = TmdbCache.GetShowBackdropFilename(showImages);

                        if (!string.IsNullOrEmpty(remoteFanart) && !string.IsNullOrEmpty(localFanart))
                        {
                            if (GUIImageHandler.DownloadImage(remoteFanart, localFanart))
                            {
                                // notify that image has been downloaded
                                item.NotifyPropertyChanged("Fanart");
                            }
                        }
                        #endregion
                    }
                })
                {
                    IsBackground = true,
                    Name         = "ImageDownloader" + i.ToString()
                }.Start(groupList);
            }
        }
コード例 #2
0
        /// <summary>
        /// Download all images attached to the GUI List Control
        /// TODO: Make part of a GUI Base Window
        /// </summary>
        /// <param name="itemsWithThumbs">List of images to get</param>
        internal static void GetImages(List <GUITmdbImage> itemsWithThumbs)
        {
            StopDownload = false;

            // split the downloads in 5+ groups and do multithreaded downloading
            int groupSize = (int)Math.Max(1, Math.Floor((double)itemsWithThumbs.Count / 5));
            int groups    = (int)Math.Ceiling((double)itemsWithThumbs.Count() / groupSize);

            for (int i = 0; i < groups; i++)
            {
                var groupList = new List <GUITmdbImage>();
                for (int j = groupSize * i; j < groupSize * i + (groupSize * (i + 1) > itemsWithThumbs.Count ? itemsWithThumbs.Count - groupSize * i : groupSize); j++)
                {
                    groupList.Add(itemsWithThumbs[j]);
                }

                new Thread(delegate(object o)
                {
                    var items = (List <GUITmdbImage>)o;
                    foreach (var item in items)
                    {
                        string remoteThumb = string.Empty;
                        string localThumb  = string.Empty;

                        #region Seasons / Episodes
                        if (item.SeasonImages != null)
                        {
                            // check if we have the image in our cache
                            var seasonImages = TmdbCache.GetSeasonImages(item.SeasonImages.Id, item.SeasonImages.Season);
                            if (seasonImages == null)
                            {
                                continue;
                            }

                            item.SeasonImages = seasonImages;

                            #region Show Season Poster
                            // stop download if we have exited window
                            if (StopDownload)
                            {
                                break;
                            }

                            remoteThumb = TmdbCache.GetSeasonPosterUrl(seasonImages);
                            localThumb  = TmdbCache.GetSeasonPosterFilename(seasonImages);

                            if (!string.IsNullOrEmpty(remoteThumb) && !string.IsNullOrEmpty(localThumb))
                            {
                                if (GUIImageHandler.DownloadImage(remoteThumb, localThumb))
                                {
                                    if (StopDownload)
                                    {
                                        break;
                                    }

                                    // notify that image has been downloaded
                                    item.NotifyPropertyChanged("SeasonPoster");
                                }
                            }
                            #endregion
                        }
                        #endregion

                        #region Shows / Seasons / Episodes
                        if (item.ShowImages != null)
                        {
                            #region Show Poster

                            var showImages = TmdbCache.GetShowImages(item.ShowImages.Id);
                            if (showImages == null)
                            {
                                continue;
                            }

                            item.ShowImages = showImages;

                            // don't download the show poster if we have a season poster
                            if (item.SeasonImages == null || item.SeasonImages.Posters == null || item.SeasonImages.Posters.Count == 0)
                            {
                                // stop download if we have exited window
                                if (StopDownload)
                                {
                                    break;
                                }

                                remoteThumb = TmdbCache.GetShowPosterUrl(showImages);
                                localThumb  = TmdbCache.GetShowPosterFilename(showImages);

                                if (!string.IsNullOrEmpty(remoteThumb) && !string.IsNullOrEmpty(localThumb))
                                {
                                    if (GUIImageHandler.DownloadImage(remoteThumb, localThumb))
                                    {
                                        if (StopDownload)
                                        {
                                            break;
                                        }

                                        // notify that image has been downloaded
                                        item.NotifyPropertyChanged("ShowPoster");
                                    }
                                }
                            }
                            #endregion

                            #region Fanart
                            // stop download if we have exited window
                            if (StopDownload)
                            {
                                break;
                            }
                            if (!TraktSettings.DownloadFanart)
                            {
                                continue;
                            }

                            string remoteFanart = TmdbCache.GetShowBackdropUrl(showImages);;
                            string localFanart  = TmdbCache.GetShowBackdropFilename(showImages);

                            if (!string.IsNullOrEmpty(remoteFanart) && !string.IsNullOrEmpty(localFanart))
                            {
                                if (GUIImageHandler.DownloadImage(remoteFanart, localFanart))
                                {
                                    if (StopDownload)
                                    {
                                        break;
                                    }

                                    // notify that image has been downloaded
                                    item.NotifyPropertyChanged("Fanart");
                                }
                            }
                            #endregion
                        }
                        #endregion

                        #region Movies
                        if (item.MovieImages != null)
                        {
                            // check if we have the image in our cache
                            var movieImages = TmdbCache.GetMovieImages(item.MovieImages.Id);
                            if (movieImages == null)
                            {
                                continue;
                            }

                            item.MovieImages = movieImages;

                            #region Movie Poster
                            // stop download if we have exited window
                            if (StopDownload)
                            {
                                break;
                            }

                            remoteThumb = TmdbCache.GetMoviePosterUrl(movieImages);
                            localThumb  = TmdbCache.GetMoviePosterFilename(movieImages);

                            if (!string.IsNullOrEmpty(remoteThumb) && !string.IsNullOrEmpty(localThumb))
                            {
                                if (GUIImageHandler.DownloadImage(remoteThumb, localThumb))
                                {
                                    if (StopDownload)
                                    {
                                        break;
                                    }

                                    // notify that image has been downloaded
                                    item.NotifyPropertyChanged("MoviePoster");
                                }
                            }
                            #endregion

                            #region Fanart
                            // stop download if we have exited window
                            if (StopDownload)
                            {
                                break;
                            }
                            if (!TraktSettings.DownloadFanart)
                            {
                                continue;
                            }

                            string remoteFanart = TmdbCache.GetMovieBackdropUrl(movieImages);;
                            string localFanart  = TmdbCache.GetMovieBackdropFilename(movieImages);

                            if (!string.IsNullOrEmpty(remoteFanart) && !string.IsNullOrEmpty(localFanart))
                            {
                                if (GUIImageHandler.DownloadImage(remoteFanart, localFanart))
                                {
                                    if (StopDownload)
                                    {
                                        break;
                                    }

                                    // notify that image has been downloaded
                                    item.NotifyPropertyChanged("Fanart");
                                }
                            }
                            #endregion
                        }
                        #endregion

                        #region People

                        if (item.PeopleImages != null)
                        {
                            // check if we have the image in our cache
                            var peopleImages = TmdbCache.GetPersonImages(item.PeopleImages.Id);
                            if (peopleImages == null)
                            {
                                continue;
                            }

                            item.PeopleImages = peopleImages;

                            #region Headshot
                            // stop download if we have exited window
                            if (StopDownload)
                            {
                                break;
                            }

                            remoteThumb = TmdbCache.GetPersonHeadshotUrl(peopleImages);
                            localThumb  = TmdbCache.GetPersonHeadshotFilename(peopleImages);

                            if (!string.IsNullOrEmpty(remoteThumb) && !string.IsNullOrEmpty(localThumb))
                            {
                                if (GUIImageHandler.DownloadImage(remoteThumb, localThumb))
                                {
                                    if (StopDownload)
                                    {
                                        break;
                                    }

                                    // notify that image has been downloaded
                                    item.NotifyPropertyChanged("HeadShot");
                                }
                            }
                            #endregion
                        }

                        #endregion
                    }
                })
                {
                    IsBackground = true,
                    Name         = "ImageDownloader" + i.ToString()
                }.Start(groupList);
            }
        }