예제 #1
0
        public void Download(IEnumerable <string> links, string file, string description, Install.InstallProcedure iproc, Action onCancel)
        {
            string       link = links.First();
            LocationType type; string location;

            if (!LocationUtil.Parse(link, out type, out location))
            {
                return;
            }

            if (!this.Visible)
            {
                this.Show();
            }
            this.BringToFront();

            if (links.Count() > 1)
            {
                onCancel = () => {
                    Log.Write(String.Format("Downloading {0} - switching to backup url {1}", file, links.ElementAt(1)));
                    Download(links.Skip(1), file, description, iproc, onCancel);
                };
            }

            var lvi = lvDownloads.Items.Add(description);

            lvi.Tag = new DownloadItem()
            {
                IProc = iproc, LVI = lvi, OnCancel = onCancel
            };
            lvi.SubItems.Add("DummyProgress");
            lvi.SubItems.Add("Calculating...");
            switch (type)
            {
            case LocationType.Url:
                var wc = new System.Net.WebClient();
                (lvi.Tag as DownloadItem).PerformCancel = wc.CancelAsync;
                wc.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                wc.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                wc.DownloadFileAsync(new Uri(location), file, lvi.Tag);
                break;

            case LocationType.GDrive:
                var gd = new GDrive();
                (lvi.Tag as DownloadItem).PerformCancel = gd.CancelAsync;
                gd.DownloadProgressChanged += new System.Net.DownloadProgressChangedEventHandler(_wc_DownloadProgressChanged);
                gd.DownloadFileCompleted   += new AsyncCompletedEventHandler(_wc_DownloadFileCompleted);
                gd.Download(location, file, lvi.Tag);
                break;

            case LocationType.MegaSharedFolder:
                string[]           parts = location.Split(',');
                Iros.Mega.MegaIros mega;
                if (!_megaFolders.TryGetValue(parts[0], out mega) || mega.Dead)
                {
                    _megaFolders[parts[0]] = mega = new Mega.MegaIros(parts[0], String.Empty);
                }
                DownloadItem           item = (DownloadItem)lvi.Tag;
                Mega.MegaIros.Transfer tfr  = null;
                tfr = mega.Download(parts[1], parts[2], file, () => {
                    this.Invoke((Delegate)(Action)(() => {
                        switch (tfr.State)
                        {
                        case Mega.MegaIros.TransferState.Complete:
                            ProcessDownloadComplete(item, new AsyncCompletedEventArgs(null, false, item));
                            break;

                        case Mega.MegaIros.TransferState.Failed:
                            lvDownloads.Items.Remove(lvi);
                            Sys.Message(new WMessage()
                            {
                                Text = "Error downloading " + lvi.Text
                            });
                            if (onCancel != null)
                            {
                                onCancel();
                            }
                            break;

                        case Mega.MegaIros.TransferState.Canceled:
                            lvDownloads.Items.Remove(lvi);
                            Sys.Message(new WMessage()
                            {
                                Text = String.Format("{0} was canceled", lvi.Text)
                            });
                            break;

                        default:
                            DoDownloadProgress(item, (int)(100 * tfr.Complete / tfr.Size), tfr.Complete);
                            break;
                        }
                    }));
                });
                mega.ConfirmStartTransfer();
                (lvi.Tag as DownloadItem).PerformCancel = () => mega.CancelDownload(tfr);
                break;
            }
        }
예제 #2
0
 public void Download(string link, string file, string description, Install.InstallProcedure iproc, Action onCancel)
 {
     Download(new[] { link }, file, description, iproc, onCancel);
 }