Example #1
0
        public void PropertyChangedHandler(object sender, PropertyChangedEventArgs e)
        {
            WebDownloadClient download = (WebDownloadClient)sender;

            if (e.PropertyName == "AverageSpeedAndTotalTime" && download.Status != DownloadStatus.Deleting)
            {
                this.Dispatcher.Invoke(new PropertyChangedEventHandler(UpdatePropertiesList), sender, e);
            }
        }
Example #2
0
        public void DownloadCompletedHandler(object sender, EventArgs e)
        {
            if (Settings.Default.ShowBalloonNotification)
            {
                WebDownloadClient download = (WebDownloadClient)sender;

                if (download.Status == DownloadStatus.Completed)
                {
                    string title = "Download Completed";
                    string text  = download.FileName + " has finished downloading.";

                    XNotifyIcon.ShowBalloonTip(title, text, BalloonIcon.Info);
                }
            }
        }
Example #3
0
        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            downloadUrls = getSelectedUrls();

            if (downloadUrls.Count > 0)
            {
                try
                {
                    foreach (string url in downloadUrls)
                    {
                        WebDownloadClient download = new WebDownloadClient(url);
                        download.IsBatch         = true;
                        download.BatchUrlChecked = false;

                        download.FileName = url.Substring(url.LastIndexOf('/') + 1);

                        // Register WebDownloadClient events
                        download.DownloadProgressChanged += download.DownloadProgressChangedHandler;
                        download.DownloadCompleted       += download.DownloadCompletedHandler;
                        download.PropertyChanged         += this.mainWindow.PropertyChangedHandler;
                        download.StatusChanged           += this.mainWindow.StatusChangedHandler;
                        download.DownloadCompleted       += this.mainWindow.DownloadCompletedHandler;

                        // Create path to temporary file
                        if (!Directory.Exists(tbDownloadFolder.Text))
                        {
                            Directory.CreateDirectory(tbDownloadFolder.Text);
                        }
                        string filePath = System.IO.Path.Combine(tbDownloadFolder.Text, download.FileName);
                        string tempPath = filePath + ".tmp";

                        // Check if there is already an ongoing download on that path
                        if (File.Exists(tempPath))
                        {
                            // If there is, skip adding this download
                            continue;
                        }

                        // Check if the file already exists
                        if (File.Exists(filePath))
                        {
                            string           message = "There is already a file with the same name, do you want to overwrite it?";
                            MessageBoxResult result  = Xceed.Wpf.Toolkit.MessageBox.Show(message, "File Name Conflict: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning);

                            if (result == MessageBoxResult.Yes)
                            {
                                File.Delete(filePath);
                            }
                            else
                            {
                                // Skip adding this download
                                continue;
                            }
                        }

                        // Set username and password if HTTP authentication is required
                        if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0))
                        {
                            download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim());
                        }

                        download.TempDownloadPath = tempPath;

                        download.AddedOn     = DateTime.UtcNow;
                        download.CompletedOn = DateTime.MinValue;

                        // Add the download to the downloads list
                        DownloadManager.Instance.DownloadsList.Add(download);

                        // Start downloading the file
                        if (startImmediately)
                        {
                            download.Start();
                        }
                        else
                        {
                            download.Status = DownloadStatus.Paused;
                        }
                    }

                    // Close the Create Batch Download window
                    this.Close();
                }
                catch (Exception ex)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("There are no files to download!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #4
0
        private void btnDownload_Click(object sender, RoutedEventArgs e)
        {
            if (urlValid)
            {
                if (tbSaveAs.Text.Length < 3 || !tbSaveAs.Text.Contains("."))
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show("The local file name is not valid.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                try
                {
                    WebDownloadClient download = new WebDownloadClient(tbURL.Text.Trim());

                    download.FileName = tbSaveAs.Text.Trim();

                    // Register WebDownloadClient events
                    download.DownloadProgressChanged += download.DownloadProgressChangedHandler;
                    download.DownloadCompleted       += download.DownloadCompletedHandler;
                    download.PropertyChanged         += this.mainWindow.PropertyChangedHandler;
                    download.StatusChanged           += this.mainWindow.StatusChangedHandler;
                    download.DownloadCompleted       += this.mainWindow.DownloadCompletedHandler;

                    // Create path to temporary file
                    if (!Directory.Exists(tbDownloadFolder.Text))
                    {
                        Directory.CreateDirectory(tbDownloadFolder.Text);
                    }
                    string filePath = tbDownloadFolder.Text + download.FileName;
                    string tempPath = filePath + ".tmp";

                    // Check if there is already an ongoing download on that path
                    if (File.Exists(tempPath))
                    {
                        string message = "There is already a download in progress at the specified path.";
                        Xceed.Wpf.Toolkit.MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    // Check if the file already exists
                    if (File.Exists(filePath))
                    {
                        string message = "There is already a file with the same name, do you want to overwrite it? "
                                         + "If not, please change the file name or download folder.";
                        MessageBoxResult result = Xceed.Wpf.Toolkit.MessageBox.Show(message, "File Name Conflict: " + filePath, MessageBoxButton.YesNo, MessageBoxImage.Warning);

                        if (result == MessageBoxResult.Yes)
                        {
                            File.Delete(filePath);
                        }
                        else
                        {
                            return;
                        }
                    }

                    // Set username and password if HTTP authentication is required
                    if (cbLoginToServer.IsChecked.Value && (tbUsername.Text.Trim().Length > 0) && (tbPassword.Password.Trim().Length > 0))
                    {
                        download.ServerLogin = new NetworkCredential(tbUsername.Text.Trim(), tbPassword.Password.Trim());
                    }

                    // Check the URL
                    download.CheckUrl();
                    if (download.HasError)
                    {
                        return;
                    }

                    download.TempDownloadPath = tempPath;

                    download.AddedOn              = DateTime.UtcNow;
                    download.CompletedOn          = DateTime.MinValue;
                    download.OpenFileOnCompletion = this.cbOpenFileOnCompletion.IsChecked.Value;

                    // Add the download to the downloads list
                    DownloadManager.Instance.DownloadsList.Add(download);

                    // Start downloading the file
                    if (startImmediately)
                    {
                        download.Start();
                    }
                    else
                    {
                        download.Status = DownloadStatus.Paused;
                    }

                    // Close the Add New Download window
                    this.Close();
                }
                catch (Exception ex)
                {
                    Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("The URL is not a valid download link.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #5
0
        private void StatusChanged(object sender, EventArgs e)
        {
            // Start the first download in the queue, if it exists
            WebDownloadClient dl = (WebDownloadClient)sender;

            if (dl.Status == DownloadStatus.Paused || dl.Status == DownloadStatus.Completed ||
                dl.Status == DownloadStatus.Deleted || dl.HasError)
            {
                foreach (WebDownloadClient d in DownloadManager.Instance.DownloadsList)
                {
                    if (d.Status == DownloadStatus.Queued)
                    {
                        d.Start();
                        break;
                    }
                }
            }

            foreach (WebDownloadClient d in DownloadManager.Instance.DownloadsList)
            {
                if (d.Status == DownloadStatus.Downloading)
                {
                    d.SpeedLimitChanged = true;
                }
            }

            int active    = DownloadManager.Instance.ActiveDownloads;
            int completed = DownloadManager.Instance.CompletedDownloads;

            if (active > 0)
            {
                if (completed == 0)
                {
                    this.statusBarActive.Content = " (" + active + " Active)";
                }
                else
                {
                    this.statusBarActive.Content = " (" + active + " Active, ";
                }
            }
            else
            {
                this.statusBarActive.Content = String.Empty;
            }

            if (completed > 0)
            {
                if (active == 0)
                {
                    this.statusBarCompleted.Content = " (" + completed + " Completed)";
                }
                else
                {
                    this.statusBarCompleted.Content = completed + " Completed)";
                }
            }
            else
            {
                this.statusBarCompleted.Content = String.Empty;
            }
        }
Example #6
0
        private void LoadDownloadsFromXml()
        {
            try
            {
                if (File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Downloads.xml")))
                {
                    // Load downloads from XML file
                    XElement downloads = XElement.Load(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Downloads.xml"));
                    if (downloads.HasElements)
                    {
                        IEnumerable <XElement> downloadsList =
                            from el in downloads.Elements()
                            select el;
                        foreach (XElement download in downloadsList)
                        {
                            // Create WebDownloadClient object based on XML data
                            WebDownloadClient downloadClient = new WebDownloadClient(download.Element("url").Value);

                            downloadClient.FileName = download.Element("file_name").Value;

                            downloadClient.DownloadProgressChanged += downloadClient.DownloadProgressChangedHandler;
                            downloadClient.DownloadCompleted       += downloadClient.DownloadCompletedHandler;
                            downloadClient.PropertyChanged         += this.PropertyChangedHandler;
                            downloadClient.StatusChanged           += this.StatusChangedHandler;
                            downloadClient.DownloadCompleted       += this.DownloadCompletedHandler;

                            string username = download.Element("username").Value;
                            string password = download.Element("password").Value;
                            if (username != String.Empty && password != String.Empty)
                            {
                                downloadClient.ServerLogin = new NetworkCredential(username, password);
                            }

                            downloadClient.TempDownloadPath = download.Element("temp_path").Value;
                            downloadClient.FileSize         = Convert.ToInt64(download.Element("file_size").Value);
                            downloadClient.DownloadedSize   = Convert.ToInt64(download.Element("downloaded_size").Value);

                            DownloadManager.Instance.DownloadsList.Add(downloadClient);

                            if (download.Element("status").Value == "Completed")
                            {
                                downloadClient.Status = DownloadStatus.Completed;
                            }
                            else
                            {
                                downloadClient.Status = DownloadStatus.Paused;
                            }

                            downloadClient.StatusText = download.Element("status_text").Value;

                            downloadClient.ElapsedTime = TimeSpan.Parse(download.Element("total_time").Value);
                            downloadClient.AddedOn     = DateTime.Parse(download.Element("added_on").Value);
                            downloadClient.CompletedOn = DateTime.Parse(download.Element("completed_on").Value);

                            downloadClient.SupportsRange        = Boolean.Parse(download.Element("supports_resume").Value);
                            downloadClient.HasError             = Boolean.Parse(download.Element("has_error").Value);
                            downloadClient.OpenFileOnCompletion = Boolean.Parse(download.Element("open_file").Value);
                            downloadClient.TempFileCreated      = Boolean.Parse(download.Element("temp_created").Value);
                            downloadClient.IsBatch         = Boolean.Parse(download.Element("is_batch").Value);
                            downloadClient.BatchUrlChecked = Boolean.Parse(download.Element("url_checked").Value);

                            if (downloadClient.Status == DownloadStatus.Paused && !downloadClient.HasError && Settings.Default.StartDownloadsOnStartup)
                            {
                                downloadClient.Start();
                            }
                        }

                        // Create empty XML file
                        XElement  root = new XElement("downloads");
                        XDocument xd   = new XDocument();
                        xd.Add(root);
                        xd.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Downloads.xml"));
                    }
                }
            }
            catch (Exception)
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("There was an error while loading the download list.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }