Exemplo n.º 1
0
        public void GetImageAsync(FacebookImageDimensions requestedSize, GetImageSourceAsyncCallback callback)
        {
            Verify.IsNotNull(callback, "callback");

            Assert.IsNotNull(SourceService);
            Assert.IsNotNull(SourceService.WebGetter);

            SmallUri ss = _GetSmallUriFromRequestedSize(requestedSize);

            if (ss == default(SmallUri))
            {
                callback(this, new GetImageSourceCompletedEventArgs(new InvalidOperationException("The requested image doesn't exist"), false, this));
            }

            BitmapSource img;

            if (_TryGetFromWeakCache(requestedSize, out img))
            {
                callback(this, new GetImageSourceCompletedEventArgs(img, this));
            }

            var userState = new _ImageCallbackState {
                Callback = callback, RequestedSize = requestedSize
            };

            SourceService.WebGetter.GetImageSourceAsync(this, userState, ss, _AddWeakCacheCallback);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts download of requested image
        /// </summary>
        /// <param name="requestedSize">Size of the image</param>
        /// <param name="callback">Callback that will be called when download completes</param>
        public void GetImageAsync(FacebookImageDimensions requestedSize, GetImageSourceAsyncCallback callback)
        {
#if !SILVERLIGHT
            dispatcher = Dispatcher.CurrentDispatcher;
#else
            dispatcher = System.Windows.Deployment.Current.Dispatcher;
#endif
            Uri = _normal;
            switch (requestedSize)
            {
            case FacebookImageDimensions.Big: Uri = _big; break;

            case FacebookImageDimensions.Small: Uri = _small; break;

            case FacebookImageDimensions.Square: Uri = _square; break;
            }

            if (Uri == null)
            {
                callback(this, new GetImageSourceCompletedEventArgs(new InvalidOperationException(), false, this));
                return;
            }


            WebClientHelper wc = new WebClientHelper(callback);
            wc.RequestCompleted += new EventHandler <RequestCompletedEventArgs>(wc_RequestCompleted);
            wc.SendRequest(Uri, null, null);
        }
Exemplo n.º 3
0
        public bool IsCached(FacebookImageDimensions requestedSize)
        {
            SmallUri sizedString = _GetSmallUriFromRequestedSize(requestedSize);
            //Assert.IsNotDefault(sizedString);
            string path;

            return(SourceService.WebGetter.TryGetImageFile(sizedString, out path));
        }
Exemplo n.º 4
0
        public static Size GetDimensionSize(FacebookImageDimensions dimensions)
        {
            Size size;

            if (_DimensionLookup.TryGetValue(dimensions, out size))
            {
                return(size);
            }

            throw new ArgumentException("Invalid enum value", "dimensions");
        }
Exemplo n.º 5
0
        internal void SaveToFile(FacebookImageDimensions requestedSize, string path, bool addExtension, FacebookImageSaveOptions options, SaveImageAsyncCallback callback, object userState, int?index, int?total)
        {
            Verify.IsNeitherNullNorEmpty(path, "path");
            Verify.IsNotNull(callback, "callback");
            Assert.Implies(total != null, index != null);
            Assert.Implies(total == null, index == null);

            SafeCopyFileOptions scfo = (options == FacebookImageSaveOptions.FindBetterName)
                ? SafeCopyFileOptions.FindBetterName
                : (options == FacebookImageSaveOptions.Overwrite)
                    ? SafeCopyFileOptions.Overwrite
                    : SafeCopyFileOptions.PreserveOriginal;

            SourceService.WebGetter.GetLocalImagePathAsync(this, null, _GetSmallUriFromRequestedSize(requestedSize),
                                                           (sender, e) =>
            {
                string cachePath = e.ImagePath;
                if (addExtension)
                {
                    string ext = Path.GetExtension(cachePath);
                    path       = Path.ChangeExtension(path, ext);
                }

                try
                {
                    string actualPath = Utility.SafeCopyFile(cachePath, path, scfo);
                    if (actualPath == null)
                    {
                        throw new IOException("Unable to save the image to the requested location.");
                    }

                    SaveImageCompletedEventArgs sicea = null;
                    if (total == null)
                    {
                        sicea = new SaveImageCompletedEventArgs(actualPath, userState);
                    }
                    else
                    {
                        sicea = new SaveImageCompletedEventArgs(actualPath, index.Value, total.Value, userState);
                    }

                    callback(this, sicea);
                    return;
                }
                catch (Exception ex)
                {
                    callback(this, new SaveImageCompletedEventArgs(ex, false, userState));
                    return;
                }
            });
        }
Exemplo n.º 6
0
        private SmallUri _GetSmallUriFromRequestedSize(FacebookImageDimensions requestedSize)
        {
            // If one url type isn't available, try to fallback on other provided values.

            SmallUri?str = null;

            switch (requestedSize)
            {
            case FacebookImageDimensions.Big:    str = _big ?? _normal ?? _small ?? _square; break;

            case FacebookImageDimensions.Small:  str = _small ?? _normal ?? _big ?? _square; break;

            case FacebookImageDimensions.Square: str = _square ?? _small ?? _normal ?? _big; break;

            case FacebookImageDimensions.Normal: str = _normal ?? _big ?? _small ?? _square; break;
            }

            return(str ?? default(SmallUri));
        }
Exemplo n.º 7
0
        private bool _TryGetFromWeakCache(FacebookImageDimensions requestedSize, out BitmapSource img)
        {
            img = null;
            switch (requestedSize)
            {
            case FacebookImageDimensions.Big:
                if (_bigWR != null)
                {
                    img = _bigWR.Target as BitmapSource;
                }
                break;

            case FacebookImageDimensions.Normal:
                if (_normalWR != null)
                {
                    img = _normalWR.Target as BitmapSource;
                }
                break;

            case FacebookImageDimensions.Small:
                if (_smallWR != null)
                {
                    img = _smallWR.Target as BitmapSource;
                }
                break;

            case FacebookImageDimensions.Square:
                if (_squareWR != null)
                {
                    img = _squareWR.Target as BitmapSource;
                }
                break;
            }

            return(img != null);
        }
Exemplo n.º 8
0
 public void SaveToFile(FacebookImageDimensions requestedSize, string path, bool addExtension, FacebookImageSaveOptions options, SaveImageAsyncCallback callback, object userState)
 {
     SaveToFile(requestedSize, path, addExtension, options, callback, userState, null, null);
 }
        /// <summary>
        /// Starts download of requested image
        /// </summary>
        /// <param name="requestedSize">Size of the image</param>
        /// <param name="callback">Callback that will be called when download completes</param>
        public void GetImageAsync(FacebookImageDimensions requestedSize, GetImageSourceAsyncCallback callback)
        {
            #if !SILVERLIGHT
            dispatcher = Dispatcher.CurrentDispatcher;
            #else
            dispatcher = System.Windows.Deployment.Current.Dispatcher;
            #endif
            Uri = _normal;
            switch (requestedSize)
            {
                case FacebookImageDimensions.Big: Uri = _big; break;
                case FacebookImageDimensions.Small: Uri = _small; break;
                case FacebookImageDimensions.Square: Uri = _square; break;
            }

            if (Uri == null)
            {
                callback(this, new GetImageSourceCompletedEventArgs(new InvalidOperationException(), false, this));
                return;
            }

            WebClientHelper wc = new WebClientHelper(callback);
            wc.RequestCompleted += new EventHandler<RequestCompletedEventArgs>(wc_RequestCompleted);
            wc.SendRequest(Uri, null, null);
        }