Example #1
0
 public static IList <Tuple <IDownloadableResourceFile, String> > pickDownloads(IEnumerable <KeyValuePair <String, List <IDownloadableResourceFile> > > downloadMappings, IWin32Window ownerWindow)
 {
     using (DownloadPicker downloadPicker = new DownloadPicker(downloadMappings)) {
         if (DialogResult.OK != downloadPicker.ShowDialog(ownerWindow))
         {
             return(null);
         }
         return(downloadPicker.downloadableResourceFileListView.listChecked());
     }
 }
Example #2
0
        internal void downloadAllNotDownloadedWith(IDownloader downloader, String downloadsDirectoryPath)
        {
            IEnumerable <IDownloadableResourceFileCollection> available = null;

            IndefiniteProcessingForm.process(
                (cancellationRequestedChecker, log) => {
                log.WriteLine("Searching for new content...");

                // TODO: consider re-writing enumerateResourceFileCollections to be lazily consumable, could perhaps avert the need for a cancellation checker?

                available = downloader.enumerateResourceFileCollections(cancellationRequestedChecker, log);
                return(false);
            },
                "Searching...",
                this
                );
            if (available.isNullOrEmpty())
            {
                return;
            }

            IEnumerable <KeyValuePair <String, List <IDownloadableResourceFile> > > notDownloaded = available.map().enumerateNotDownloaded(downloadsDirectoryPath);

            if (!notDownloaded.Any())
            {
                this.inform("All files checked are up-to-date.", "Downloads");
                return;
            }
            IList <Tuple <IDownloadableResourceFile, String> > picks = DownloadPicker.pickDownloads(notDownloaded, this);

            if (picks.isNullOrEmpty())
            {
                return;
            }

            long downloadedByteCount = 0;
            long totalByteCount      = picks.Select(t => t.Item1.size).Sum();

            BatchProcessingForm <Tuple <IDownloadableResourceFile, String> > .process(
                picks,
                (tuple, log) => {
                IDownloadableResourceFile downloadableResourceFile = tuple.Item1;
                String relativeFilePath = tuple.Item2;
                log.WriteLine("Downloading: \"{0}\"", downloadableResourceFile.name);
                FileInfo fileInfo = new FileInfo(Path.Combine(downloadsDirectoryPath, relativeFilePath));
                fileInfo.Directory.Create();
                if (!downloader.download(downloadableResourceFile, fileInfo.FullName))
                {
                    Console.Error.WriteLine("Downloading of uri: {0} failed", downloadableResourceFile);
                    if (fileInfo.Exists)
                    {
                        Console.Error.WriteLine("Deleting partially downloaded file: {0}", fileInfo.FullName);
                        fileInfo.Delete();
                    }
                    return(-1);
                }
                downloadedByteCount += downloadableResourceFile.size;
                return((int)(Math.Ceiling((Double)(downloadedByteCount) / (Double)(totalByteCount) * 100d)));
            },
                "Downloading...",
                "{0}% downloaded",
                this
                );
        }