Ejemplo n.º 1
0
        /// <summary>
        /// Add image by downloading
        /// </summary>
        /// <param name="downloadLink"></param>
        /// <param name="contentSlide"></param>
        /// <param name="slideWidth"></param>
        /// <param name="slideHeight"></param>
        public void AddImageSelectionListItem(string downloadLink,
                                              Slide contentSlide, float slideWidth, float slideHeight)
        {
            if (StringUtil.IsEmpty(downloadLink) || !UrlUtil.IsUrlValid(downloadLink)) // Case 1: If url not valid
            {
                View.ShowErrorMessageBox(TextCollection.PictureSlidesLabText.ErrorUrlLinkIncorrect);
                return;
            }
            var item = new ImageItem
            {
                ImageFile   = StoragePath.LoadingImgPath,
                ContextLink = downloadLink
            };

            UrlUtil.GetMetaInfo(ref downloadLink, item);
            ImageSelectionList.Add(item);
            IsActiveDownloadProgressRing.Flag = true;

            var imagePath = StoragePath.GetPath("img-"
                                                + DateTime.Now.GetHashCode() + "-"
                                                + Guid.NewGuid().ToString().Substring(0, 7));

            ImageDownloader
            .Get(downloadLink, imagePath)
            .After(() =>
            {
                try
                {
                    VerifyIsProperImage(imagePath);     // Case 2: not a proper image
                    item.UpdateDownloadedImage(imagePath);
                    if (ImageSelectionListSelectedItem.ImageItem != null &&
                        imagePath == ImageSelectionListSelectedItem.ImageItem.ImageFile)
                    {
                        UpdatePreviewImages(ImageSelectionListSelectedItem.ImageItem,
                                            contentSlide, slideWidth, slideHeight);
                    }
                }
                catch
                {
                    View.ShowErrorMessageBox(TextCollection.PictureSlidesLabText.ErrorImageDownloadCorrupted);
                    ImageSelectionList.Remove(item);
                }
                finally
                {
                    IsActiveDownloadProgressRing.Flag = false;
                }
            })
            // Case 3: Possibly network timeout
            .OnError(e =>
            {
                IsActiveDownloadProgressRing.Flag = false;
                ImageSelectionList.Remove(item);
                View.ShowErrorMessageBox(TextCollection.PictureSlidesLabText.ErrorFailedToLoad + e.Message);
            })
            .Start();
        }
        /// <summary>
        /// Add image by downloading
        /// </summary>
        /// <param name="downloadLink"></param>
        /// <param name="contentSlide"></param>
        /// <param name="slideWidth"></param>
        /// <param name="slideHeight"></param>
        public void AddImageSelectionListItem(string downloadLink,
                                              Slide contentSlide, float slideWidth, float slideHeight)
        {
            if (StringUtil.IsEmpty(downloadLink) || !UrlUtil.IsUrlValid(downloadLink)) // Case 1: If url not valid
            {
                View.ShowErrorMessageBox(PictureSlidesLabText.ErrorUrlLinkIncorrect);
                Logger.Log("Url link error when add internet image");
                return;
            }
            var item = new ImageItem
            {
                ImageFile   = StoragePath.LoadingImgPath,
                ContextLink = downloadLink,
                Source      = downloadLink
            };

            UrlUtil.GetMetaInfo(ref downloadLink, item);
            ImageSelectionList.Add(item);
            IsActiveDownloadProgressRing.Flag = true;

            var imagePath = StoragePath.GetPath("img-"
                                                + DateTime.Now.GetHashCode() + "-"
                                                + Guid.NewGuid().ToString().Substring(0, 7));

            ImageDownloader
            .Get(downloadLink, imagePath)
            .After((AutoUpdate.Downloader.AfterDownloadEventDelegate)(() =>
            {
                try
                {
                    Logger.Log("Add internet picture begins");
                    VerifyIsProperImage(imagePath);     // Case 2: not a proper image
                    item.UpdateDownloadedImage(imagePath);
                    UpdatePictureInPictureVariationWhenAddedNewOne(item);
                    if (ImageSelectionListSelectedItem.ImageItem != null &&
                        imagePath == ImageSelectionListSelectedItem.ImageItem.FullSizeImageFile)
                    {
                        UpdatePreviewImages(ImageSelectionListSelectedItem.ImageItem,
                                            contentSlide, slideWidth, slideHeight);
                    }
                    else if (IsInPictureVariation())
                    {
                        UpdatePreviewImages(
                            ImageSelectionListSelectedItem.ImageItem ?? View.CreateDefaultPictureItem(),
                            contentSlide, slideWidth, slideHeight);
                    }
                    Logger.Log("Add internet picture ends");
                }
                catch (Exception e)
                {
                    View.ShowErrorMessageBox(PictureSlidesLabText.ErrorImageDownloadCorrupted);
                    ImageSelectionList.Remove(item);
                    Logger.LogException(e, "AddImageSelectionListItem (download)");
                }
                finally
                {
                    IsActiveDownloadProgressRing.Flag = false;
                }
            }))
            // Case 3: Possibly network timeout
            .OnError((AutoUpdate.Downloader.ErrorEventDelegate)(e =>
            {
                IsActiveDownloadProgressRing.Flag = false;
                ImageSelectionList.Remove(item);
                View.ShowErrorMessageBox(PictureSlidesLabText.ErrorFailedToLoad + e.Message);
            }))
            .Start();
        }