Esempio n. 1
0
 protected virtual void OnUpdating(ThreadEventArgs e)
 {
     if (Updating != null)
     {
         Updating(this, e);
     }
     if (header.IsPastlog || header.IsLimitOverThread || header.IsIgnored)
     {
         e.Cancel = true;
     }
     if (Common.CurrentSettings.Crushed && !header.Repaired)
     {
         header.ClearStatus();
     }
 }
Esempio n. 2
0
 protected virtual void OnUpdated(ThreadEventArgs e)
 {
     if (Updated != null)
     {
         Updated(this, e);
     }
     totalDownloadedImageCount = 0;//プログレスを0%に設定
 }
Esempio n. 3
0
 protected virtual void OnRunning(ThreadEventArgs e)
 {
     if (Running != null)
     {
         Running(this, e);
     }
     running = true;
     header.State = ThreadState.Downloading;
     if (header.IsIgnored)
     {
         e.Cancel = true;
     }
 }
Esempio n. 4
0
        protected virtual void OnRan(ThreadEventArgs e)
        {
            if (running)
            {
                cancellationDownload = false;
                header.State = ThreadState.DownloadComplete;
                if ((header.IsPastlog || header.IsLimitOverThread) && imageHeaders.DownloadCompleted)
                {
                    OnDownloadCompleted();
                }
                running = false;
                //lock (Settings.SyncRoot)
                //{
                //    Common.CurrentSettings.Save();
                //}
                //lock (((System.Collections.ICollection)Common.DownloadedImageHashes).SyncRoot)
                //{
                //    Common.DownloadedImageHashes.Save();
                //}

                if (Ran != null)
                {
                    Ran(this, e);
                }
            }
        }
Esempio n. 5
0
        public bool Update()
        {
            ThreadEventArgs e = new ThreadEventArgs();
            OnUpdating(e);
            if (e.Cancel)
            {
                //OnUpdated(e);
                return false;
            }

            string newReses = header.Update();
            bool updated = !string.IsNullOrEmpty(newReses);
            if (updated)
            {
                string replacedNewReses = Common.ReplaceStr != null ? Common.ReplaceStr.Replace(newReses) : newReses;
                int startResIndex = header.GotResCount - header.NewResCount;
                ThreadUtility.ParseHeaderResult result = ThreadUtility.ParseHeader(startResIndex, replacedNewReses, header.Parent.ExtensionFormat);
                foreach (ImageHeader newImage in result.ImageHeaders)
                {
                    if (!imageHeaders.Contains(newImage) &&
                        Common.Options.NGOptions.NGUrls.All(ngUrl => !newImage.OriginalUrl.Contains(ngUrl)))
                    {
                        imageHeaders.Add(newImage);
                    }
                }
                if (Common.Options.BrowsersOptions.JaneOptions.EnableImageViewURLReplacedatOption)
                {
                    foreach (MaybeImageHeader newUrl in result.MaybeImageHeaders)
                    {
                        if (!imageHeaders.Contains(newUrl))
                        {
                            imageHeaders.Add(newUrl);
                        }
                        //if (!maybeImageHeaders.Contains(newUrl))
                        //{
                        //    maybeImageHeaders.Add(newUrl);
                        //}
                    }
                }
            }

            OnUpdated(e);
            return updated;
        }
Esempio n. 6
0
        /// <summary>
        /// メインプロセスを開始します
        /// </summary>
        /// <exception cref="System.IO.IOException">画像の保存中にエラーが発生しました</exception>
        /// <exception cref="System.Net.WebException">アップローダーへのアクセス中にエラーが発生しました</exception>
        /// <exception cref="System.InvalidOperationException">ダウンロード中にエラーが発生しました</exception>
        public void Run()
        {
            ThreadEventArgs e = new ThreadEventArgs();
            OnRunning(e);
            if (e.Cancel)
            {
                OnRan(e);
                return;
            }

            IEnumerable<IImageHeader> downloadImages = GetDownloadImages();
            totalDownloadingImageCount = downloadImages.Count();
            try
            {
                System.Threading.Tasks.Parallel.ForEach(Uploader.Split(downloadImages), (uploader, loopState) =>
                {
                    int i;
                    for (i = 0; i < uploader.ImageHeaders.Count; i++)
                    {
                        IImageHeader image = uploader.ImageHeaders[i];
                        if (cancellationDownload)
                        {
                            loopState.Break();
                            return;
                        }
                        try
                        {
                            ImageDownloadResult result = null;
                            lock (uploader.SyncRoot)
                            {
                                result = Download(image);
                            }
                            Save(result, image);
                        }
                        catch (WebException ex)
                        {
                            if (ex.Status == WebExceptionStatus.ProtocolError)
                            {
                                if (ex.Response == null)
                                {
                                    throw;
                                }
                                HttpWebResponse errorResponse = (HttpWebResponse)ex.Response;
                                if (PauseDownloadStatuses.Contains(errorResponse.StatusCode))
                                {
                                    //一時定期にアップローダが利用できない場合は次までダウンロードを停止
                                    break;
                                }
                            }
                        }
                        totalDownloadedImageCount++;
                    }

                    //一時停止に設定
                    bool breakDownload = i < uploader.ImageHeaders.Count;
                    if (breakDownload)
                    {
                        for (; i < uploader.ImageHeaders.Count; i++)
                        {
                            uploader.ImageHeaders[i].State = ImageState.DownloadPause;
                        }
                    }
                });
            }
            catch (AggregateException ex)
            {
                OnRan(e);
                ex.Handle((innerException) =>
                {
                    if (innerException is IOException)
                    {
                        Common.Logs.Add("保存エラー", innerException.Message, LogStatus.Error);
                        //System.Windows.Forms.MessageBox.Show(ex.Message + "\nダウンロードを中断しました。", "保存失敗",
                        //    MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return true;
                    }
                    else if (innerException is WebException)
                    {
                        Common.Logs.Add("通信エラー", innerException.Message, LogStatus.Error);
                        //MessageBox.Show(ex.Message + "\nダウンロードを中断しました。", "ダウンロード失敗",
                        //    MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return true;
                    }
                    return false;
                });
                //if (ex.InnerException is IOException || ex.InnerException is WebException)
                //{
                //    throw ex.InnerException;
                //}
                //else
                //{
                //    throw new InvalidOperationException("ダウンロード中にエラーが発生しました。\r\n" + header.Url, ex.InnerException);
                //}
            }
            OnRan(e);
        }