Beispiel #1
0
        public PictureDownload GetPicture(Picture pic, bool queueForDownload)
        {
            //check if the requested image exists, if it does then return
            //if image already has a local path then use it (just what we need for local provider where images are not stored in cache).
            var picturePath = string.IsNullOrEmpty(pic.LocalPath) ? pic.CalculateLocalPath(SaveFolder) : pic.LocalPath;

            pic.LocalPath = picturePath;


            if (pic.IsGood)
            {
                //if the wallpaper image already exists, and passes our 0 size check then fire the event
                return(new PictureDownload(pic));
            }

            var fi = new FileInfo(picturePath);

            //for files that do not exist "Delete" does nothing per MSDN docs
            try { fi.Delete(); }
            catch (Exception ex)
            {
                Log.Logger.Write(string.Format("Error deleting 0 byte file '{0}' while prepareing to redownload it. Exception details: {1}", fi.FullName, ex.ToString()), Log.LoggerLevels.Errors);
            }

            PictureDownload pd = new PictureDownload(pic);

            if (queueForDownload)
            {
                //add file to Queue
                QueuePicture(pd);
            }

            return(pd);
        }
Beispiel #2
0
 void p_PictureDownloadProgressChanged(PictureDownload sender)
 {
     if (PictureDownloadProgressChanged != null)
     {
         PictureDownloadProgressChanged(sender);
     }
 }
Beispiel #3
0
 void p_PictureDownloaded(PictureDownload sender)
 {
     if (PictureDownloaded != null)
     {
         PictureDownloaded(sender);
     }
 }
Beispiel #4
0
 void p_PictureDownloading(PictureDownload sender)
 {
     if (PictureDownloading != null)
     {
         PictureDownloading(sender);
     }
 }
Beispiel #5
0
        void Current_PictureAddedToQueue(PictureDownload sender)
        {
            if (pictureStatusList.ContainsKey(sender)) return;

            this.Invoke((Action)delegate
            {
                PictureStatus ps = new PictureStatus();
                flpDownloads.Controls.Add(ps);
                ps.Dock = DockStyle.Top;

                pictureStatusList.Add(sender, ps);

                ps.SetPicture(sender);
            });
        }
Beispiel #6
0
        void Current_PictureRemovedFromQueue(PictureDownload sender)
        {
            lock (pictureStatusList)
            {
                if (!pictureStatusList.ContainsKey(sender)) return;

                this.Invoke((Action)delegate
                {
                    PictureStatus ps = pictureStatusList[sender];

                    ps.UnhookEvents();
                    flpDownloads.Controls.Remove(ps);

                    pictureStatusList.Remove(sender);
                });
            }
        }
Beispiel #7
0
        public void SetPicture(PictureDownload pd)
        {
            this.CurrentPicture = pd;

            CurrentPicture.PictureDownloadStatusChanged += pd_PictureDownloadStatusChanged;
            CurrentPicture.PictureDownloadProgressChanged += pd_PictureDownloadProgressChanged;

            this.Invoke((Action)delegate
            {
                if (!lblFileName.IsDisposed)
                {
                    lblFileName.Text = Path.GetFileName(pd.Picture.LocalPath);

                    SetStatus(pd.Status);
                }
            });
        }
Beispiel #8
0
        public void RemovePictureFromQueue(PictureDownload p)
        {
            if (!_downloadQueue.Contains(p))
            {
                return;
            }

            //call cancel, just in case
            p.CancelDownload();

            _downloadQueue.Remove(p);

            //call removed event
            if (PictureRemovedFromQueue != null)
            {
                PictureRemovedFromQueue(p);
            }
        }
Beispiel #9
0
        private void DownloadSelectedPictures(List <Picture> toProcess)
        {
            //validate that there are actually pictures to be processed (search may return 0 results, or very few results)
            if (toProcess.Count > 0)
            {
                //download the new pictures, wait for them to all finish
                ManualResetEvent[] manualEvents = new ManualResetEvent[toProcess.Count];

                // Queue the work items that create and write to the files.
                for (int i = 0; i < toProcess.Count; i++)
                {
                    manualEvents[i] = new ManualResetEvent(false);

                    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state)
                    {
                        object[] states = (object[])state;

                        ManualResetEvent mre = (ManualResetEvent)states[0];
                        Picture p            = (Picture)states[1];

                        PictureDownload pd = DownloadManager.Current.GetPicture(p, false);

                        //hook events and set mre in event
                        PictureDownload.PictureDownloadEvent pdEvent = new PictureDownload.PictureDownloadEvent(
                            delegate(PictureDownload t)
                        {
                            mre.Set();
                        });

                        //on success or failure make sure to set mre
                        pd.PictureDownloaded         += pdEvent;
                        pd.PictureDownloadingAborted += pdEvent;

                        DownloadManager.Current.QueuePicture(pd);
                    }), new object[] { manualEvents[i], toProcess[i] });
                }

                //wait for all items to finish
                //3 minute timeout
                WaitHandle.WaitAll(manualEvents, 3 * 60 * 1000);

                CurrentPictures.AddRange(toProcess);
            }
        }
Beispiel #10
0
        public void QueuePicture(PictureDownload p)
        {
            if (_downloadQueue.Contains(p.Picture.Url))
            {
                return;
            }

            //add to the queue
            _downloadQueue.Add(p);

            //hook events
            p.PictureDownloaded              += p_PictureDownloaded;
            p.PictureDownloading             += p_PictureDownloading;
            p.PictureDownloadingAborted      += p_PictureDownloadingAborted;
            p.PictureDownloadProgressChanged += p_PictureDownloadProgressChanged;

            //call picture added event
            if (PictureAddedToQueue != null)
            {
                PictureAddedToQueue(p);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Retrieves a random picture from the picture list
        /// </summary>
        /// <param name="pl">Picture list from which to retrieve pictures</param>
        /// <param name="saveFolder">Location where to save the picture</param>
        /// <param name="currentPicture">(optional) the current picture, to avoid repeates.  Pass null if not needed or this is the first picture.</param>
        public PictureDownload GetPicture(PictureList pl, Picture currentPicture, bool queueForDownload)
        {
            Picture pic = null;

            if (pl == null || pl.Pictures.Count == 0)
            {
                return(null);
            }

            //pick the next picture at random
            // only "non-random" bit is that we make sure that the next random picture isn't the same as our current one
            var index = 0;

            do
            {
                index = rnd.Next(pl.Pictures.Count);
            } while (currentPicture != null && currentPicture.Url == pl.Pictures[index].Url);

            pic = pl.Pictures[index];
            //download current picture first
            PictureDownload pd = GetPicture(pic, queueForDownload);

            return(pd);
        }
Beispiel #12
0
 void pd_PictureDownloaded(PictureDownload sender)
 {
     pb1.SizeMode = PictureBoxSizeMode.Zoom;
     pb1.ImageLocation = sender.Picture.LocalPath;
 }
Beispiel #13
0
 void p_PictureDownloadProgressChanged(PictureDownload sender)
 {
     if (PictureDownloadProgressChanged != null) PictureDownloadProgressChanged(sender);
 }
Beispiel #14
0
 void p_PictureDownloadingAborted(PictureDownload sender)
 {
     if (PictureDownloadingAborted != null) PictureDownloadingAborted(sender);
 }
Beispiel #15
0
 void p_PictureDownloading(PictureDownload sender)
 {
     if (PictureDownloading != null) PictureDownloading(sender);
 }
Beispiel #16
0
 void p_PictureDownloaded(PictureDownload sender)
 {
     if (PictureDownloaded != null) PictureDownloaded(sender);
 }
Beispiel #17
0
        public void RemovePictureFromQueue(PictureDownload p)
        {
            if (!_downloadQueue.Contains(p)) return;

            //call cancel, just in case
            p.CancelDownload();

            _downloadQueue.Remove(p);

            //call removed event
            if (PictureRemovedFromQueue != null) PictureRemovedFromQueue(p);
        }
Beispiel #18
0
        public void QueuePicture(PictureDownload p)
        {
            if (_downloadQueue.Contains(p.Picture.Url)) return;

            //add to the queue
            _downloadQueue.Add(p);

            //hook events
            p.PictureDownloaded += p_PictureDownloaded;
            p.PictureDownloading += p_PictureDownloading;
            p.PictureDownloadingAborted += p_PictureDownloadingAborted;
            p.PictureDownloadProgressChanged += p_PictureDownloadProgressChanged;

            //call picture added event
            if (PictureAddedToQueue != null) PictureAddedToQueue(p);
        }
Beispiel #19
0
        public PictureDownload GetPicture(Picture pic, bool queueForDownload)
        {
            //check if the requested image exists, if it does then return
            //if image already has a local path then use it (just what we need for local provider where images are not stored in cache).
            var picturePath = string.IsNullOrEmpty(pic.LocalPath) ? pic.CalculateLocalPath(SaveFolder) : pic.LocalPath;
            pic.LocalPath = picturePath;

            if (pic.IsGood)
            {
                //if the wallpaper image already exists, and passes our 0 size check then fire the event
                return new PictureDownload(pic);
            }

            var fi = new FileInfo(picturePath);

            //for files that do not exist "Delete" does nothing per MSDN docs
            try { fi.Delete(); }
            catch (Exception ex)
            {
                Log.Logger.Write(string.Format("Error deleting 0 byte file '{0}' while prepareing to redownload it. Exception details: {1}", fi.FullName, ex.ToString()), Log.LoggerLevels.Errors);
            }

            PictureDownload pd = new PictureDownload(pic);

            if (queueForDownload)
            {
                //add file to Queue
                QueuePicture(pd);
            }

            return pd;
        }
Beispiel #20
0
 void pd_PictureDownloadStatusChanged(PictureDownload sender)
 {
     this.Invoke((Action)delegate
     {
         SetStatus(sender.Status);
     });
 }
Beispiel #21
0
 void pd_PictureDownloadProgressChanged(PictureDownload sender)
 {
     this.Invoke((Action)delegate
     {
         if(!pbDownloadProgress.IsDisposed)
             pbDownloadProgress.Value = sender.DownloadProgress;
     });
 }