private void startButton_Click(object sender, EventArgs e) { if (!_running) { if (!string.IsNullOrEmpty(linkText.Text.Trim())) { linkText.Text = linkText.Text.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase) ? linkText.Text : "http://" + linkText; if (KitaabGhar.IsValidLink(linkText.Text.Trim())) { if (logList.Items.Count > 0) { logList.Items.Clear(); } progress.Value = 0; LogMessage("Link: " + linkText.Text); LogMessage("Gathering information."); if (ApplicationSettings.Smart) { LogMessage("Smart identification is on so this may take a while."); } statusText.Text = "Running..."; Application.DoEvents(); _running = true; var mainThread = new Thread(Main) { IsBackground = true }; mainThread.Start(linkText.Text); } else { LogMessage("Invalid download link."); statusText.Text = "Please enter the link to the novel"; } } else { LogMessage("Link missing."); statusText.Text = "Please enter the link to the novel"; } } else { statusText.Text = "Application is already running."; } }
private void Main(object link) { try { _novel = KitaabGhar.GetNovelInformation(link.ToString()); LogMessage("Information collected."); LogMessage(string.Format("Total pages: {0} ({1} - {2})", _novel.TotalPages, _novel.FirstIndex, _novel.LastIndex)); } catch (Exception ex) { LogMessage("Error: " + ex.Message); statusText.Text = "Unable to get the required information. Please try again."; _running = false; return; } var directory = Path.Combine(ApplicationSettings.DownloadLocation, _novel.Name); var imageFormat = Path.Combine(directory, "{0}.gif"); Directory.CreateDirectory(directory); for (var i = _novel.FirstIndex; i <= _novel.LastIndex; i++) { statusText.Text = "Downloading page: " + i; var r = 0; Retry: if (!_running) { return; } try { if (r <= ApplicationSettings.Retries) { if (!(ApplicationSettings.Resume && File.Exists(string.Format(imageFormat, i)))) { var tempImage = Http.DownloadImage(_novel.GetImageLink(i), _novel.GetRefLink(i), _novel.NewFormat); tempImage.Save(string.Format(imageFormat, i)); tempImage.Dispose(); } if (!_running) { return; } Progress((int)((double)((i - _novel.FirstIndex) * 100) / _novel.TotalPages)); } else { LogMessage("Unable to download within the provided number of retries."); } } catch (Exception ex) { LogMessage("Unable to download page: " + i); LogMessage("Trying to download page" + i + " again."); LogMessage("Error: " + ex.Message); r++; goto Retry; } } LogMessage("Download complete."); if (ApplicationSettings.CreatePdf) { LogMessage("Creating PDF file."); try { using (var pdfFile = new pdfDocument(_novel.Name, "Abdullah Saleem")) { for (var i = _novel.FirstIndex; i <= _novel.LastIndex; i++) { var file = string.Format(imageFormat, i); statusText.Text = "Processing page: " + i; if (File.Exists(file)) { try { using (var image = Image.FromFile(file)) { pdfFile.addImageReference(file, i.ToString(CultureInfo.InvariantCulture)); var tempPage = pdfFile.addPage(image.Height, image.Width); tempPage.addImage( pdfFile.getImageReference(i.ToString(CultureInfo.InvariantCulture)), 0, 0); } statusText.Text = "Page " + i + " added."; } catch (Exception) { LogMessage("Page number " + i + "missing."); } } else { LogMessage("Page number " + i + "missing."); } } pdfFile.createPDF(Path.Combine(directory, _novel.Name + ".pdf")); statusText.Text = "PDF Created!"; if (ApplicationSettings.OpenPdf) { Process.Start(Path.Combine(directory, _novel.Name + ".pdf")); } } } catch (Exception ex) { LogMessage("Error occured while creating the PDF."); LogMessage("Error: " + ex.Message); } } if (ApplicationSettings.DeleteImages) { LogMessage("Deleting images."); statusText.Text = "Deleting images."; for (var i = _novel.FirstIndex; i <= _novel.LastIndex; i++) { if (File.Exists(string.Format(imageFormat, i))) { try { File.Delete(string.Format(imageFormat, i)); } catch (Exception ex) { LogMessage("Error occured while deleting images."); LogMessage("Error: " + ex.Message); } } } } statusText.Text = "Completed."; Invoke(new MethodInvoker(DownloadCompeted)); }