private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { BookEventArg file = e.UserState as BookEventArg; file.Progress = e.ProgressPercentage; BookDownloading?.Invoke(this, file); }
protected void OnBookFound(BookEventArg e) { e.Category = _keyword; BookFound?.Invoke(this, e); if (e.Download) { DownloadBook(e); } }
private void browseToolStripMenuItem_Click(object sender, EventArgs e) { if (lvwBooks.SelectedItems.Count <= 0) { return; } ListViewItem item = lvwBooks.SelectedItems[0]; BookEventArg book = item.Tag as BookEventArg; Process.Start(book.URL); }
private void OnFileDownloading(object sender, BookEventArg e) { if (lvwBooks.InvokeRequired) { this.Invoke(new Provider.BookDownloadingEventHandler(OnFileDownloading), new object[] { sender, e }); } else { ListViewItem item = lvwBooks.Items[e.Index]; item.SubItems[3].Text = e.Progress.ToString(); } }
private void chbGroupByKeyword_CheckedChanged(object sender, EventArgs e) { foreach (ListViewItem item in lvwBooks.Items) { BookEventArg book = item.Tag as BookEventArg; if (book != null) { book.Group = chbGroupByKeyword.Checked; OnFileFound(this, book); } } }
private void explorerToolStripMenuItem_Click(object sender, EventArgs e) { if (lvwBooks.SelectedItems.Count <= 0) { return; } ListViewItem item = lvwBooks.SelectedItems[0]; BookEventArg book = item.Tag as BookEventArg; Process.Start("EXPLORER", "/SELECT," + book.Path); }
private void OnFileDownloaded(object sender, BookEventArg e) { if (lvwBooks.InvokeRequired) { this.Invoke(new Provider.BookDownloadCompletedEventHandler(OnFileDownloaded), new object[] { sender, e }); } else { ListViewItem item = lvwBooks.Items[e.Index]; item.SubItems[3].Text = e.Path; item.ImageIndex = e.Status; } }
private void exportListMenuItem_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() != DialogResult.OK) { return; } XmlWriter xmlWriter = null; try { xmlWriter = XmlWriter.Create(saveFileDialog.FileName); } catch (Exception ex) { MessageBox.Show(ex.Message, "eBookDownloader", MessageBoxButtons.OK, MessageBoxIcon.Error); xmlWriter = null; return; } if (xmlWriter != null) { xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("books"); Provider provider = cbbProviders.SelectedItem as Provider; if (provider != null) { xmlWriter.WriteAttributeString("provider", provider.Name); } foreach (ListViewItem item in lvwBooks.Items) { BookEventArg book = item.Tag as BookEventArg; if (book != null) { xmlWriter.WriteStartElement("book"); xmlWriter.WriteAttributeString("title", book.Title); xmlWriter.WriteAttributeString("url", book.URL); xmlWriter.WriteEndElement(); } } xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); xmlWriter.Flush(); xmlWriter.Close(); MessageBox.Show("Items are exported successfully.", "eBookDownloader", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { BookEventArg book = e.UserState as BookEventArg; if (progressBar.InvokeRequired) { progressBar.Invoke(new MethodInvoker(delegate { progressBar.Value++; })); } else { progressBar.Value++; } }
private void copyLinkMenuItem_Click(object sender, EventArgs e) { if (lvwBooks.SelectedItems.Count <= 0) { return; } ListViewItem item = lvwBooks.SelectedItems[0]; BookEventArg book = item.Tag as BookEventArg; try { Clipboard.Clear(); Clipboard.SetText(book.URL); } catch (Exception ex) { DebugPrint(ex.Message); } }
private void OnDownloadFinished(object sender, AsyncCompletedEventArgs e) { BookEventArg file = e.UserState as BookEventArg; if (e.Cancelled) { file.Status = -1; } else if (e.Error != null) //Failed { file.Status = e.Error.HResult; } else //Success { file.Status = 0; } BookDownloadCompleted?.Invoke(this, file); _downloaders.Remove(file.URL); }
private void btnBrowseWorkingDir_Click(object sender, EventArgs e) { folderBrowserDialog.SelectedPath = txtWorkingDirectory.Text; if (folderBrowserDialog.ShowDialog() != DialogResult.OK) { return; } if (txtWorkingDirectory.Text != folderBrowserDialog.SelectedPath) { txtWorkingDirectory.Text = folderBrowserDialog.SelectedPath; foreach (ListViewItem item in lvwBooks.Items) { BookEventArg book = item.Tag as BookEventArg; if (book != null) { OnFileFound(this, book); } } } }
protected override KeyValuePair <string, string> SearchLink(string link) { HttpWebRequest httpReq = WebRequest.Create(Home + link) as HttpWebRequest; KeyValuePair <string, string> file = new KeyValuePair <string, string>(); if (httpReq != null) { if (IsCancel) { return(file); } WebResponse webResponse = httpReq.GetResponse(); Stream html = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(html); string htmlString = reader.ReadToEnd(); string strDIVOpen = "<div"; string strDIVClose = "</div>"; string signal = "<a target=\"_blank\" class=\"btn btn-block btn-sm btn-primary\" href=\""; int first = htmlString.IndexOf(strDIVOpen, 0); int last = 0; string strLink = string.Empty; bool bFoundCloseTag = false; string strhRef = string.Empty; string strTitle = string.Empty; int ff = 0, ll = 0; int ff2 = 0, ll2 = 0; string bookPath = string.Empty; while (first > 0 && first < htmlString.Length) { if (IsCancel) { return(file); } bFoundCloseTag = true; last = htmlString.IndexOf(strDIVClose, first + strDIVOpen.Length + 1); if ((last > htmlString.Length) || (-1 == last)) { last = htmlString.Length; bFoundCloseTag = false; } strhRef = htmlString.Substring(first + strDIVOpen.Length, last - first - (bFoundCloseTag ? strDIVClose.Length : 0)); ff = strhRef.IndexOf("class=\"book-image\""); if (ff > 0) { ff = strhRef.IndexOf("<img alt=\"", ff); if ((ff > 0) && (ff < strhRef.Length)) { ff += "<img alt=\"".Length; ll = strhRef.IndexOf("\"", ff); if ((ll > strhRef.Length) || (-1 == ll)) { ll = strhRef.Length; } strTitle = strhRef.Substring(ff, ll - ff).Trim(); } } ff2 = strhRef.IndexOf(signal); if (ff2 > 0) { ff2 += signal.Length; ll2 = strhRef.IndexOf("\"", ff2); if (-1 != ll2) { strLink = strhRef.Substring(ff2, ll2 - ff2); if (strLink.Length > 0) { string strID = string.Empty; int nPos = strLink.LastIndexOf("/"); int nStart = 0; int nEnd = strLink.Length; if (nPos > 0) { nStart = nPos + 1; } nPos = strLink.LastIndexOf("."); if (nPos <= nStart) { nPos = strLink.Length; } nEnd = nPos - 1; strID = strLink.Substring(nStart, nEnd - nStart + 1); file = new KeyValuePair <string, string>(strLink, WebUtility.HtmlDecode(WebUtility.HtmlDecode(strTitle))); BookEventArg e = new BookEventArg(strID, file.Value, file.Key); OnBookFound(e); return(file); } } } first = htmlString.IndexOf(strDIVOpen, last + (bFoundCloseTag ? strDIVClose.Length : 0) + 1); if (first < 0) { break; } } } return(new KeyValuePair <string, string>()); }
private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { BookEventArg book = e.UserState as BookEventArg; }
private void OnFileFound(object sender, BookEventArg e) { if (sender == this) { string strDir = txtWorkingDirectory.Text; if (strDir.Substring(strDir.Length - 1, 1) != "\\") { strDir += "\\"; } string strExt = string.Empty; string strCategory = string.Empty; int nPos = e.URL.LastIndexOf("."); if (nPos > 0) { strExt = e.URL.Substring(nPos + 1); } if (e.Group) { strCategory = e.Category; } string strFullPath = strDir; if (strExt != string.Empty) { if (strCategory != string.Empty) { strFullPath += ReplaceSpecialCharacters(strCategory); strFullPath += "\\"; } strFullPath += (ReplaceSpecialCharacters(e.Title) + "." + strExt); } else { strFullPath += ReplaceSpecialCharacters(e.Title); } e.Path = strFullPath; lvwBooks.Items[e.Index].SubItems[3].Text = e.Path; } else { if (lvwBooks.InvokeRequired) { this.Invoke(new Provider.BookFoundEventHandler(OnFileFound), new object[] { sender, e }); } else { ListViewItem item = lvwBooks.Items.Add((lvwBooks.Items.Count + 1).ToString()); item.ImageIndex = -1; item.SubItems.Add(e.Title); item.SubItems.Add(e.URL); lvwBooks.EnsureVisible(item.Index); if (!lblKeyword.Visible) { progressBar.Width = lblKeyword.Left - progressBar.Left; lblKeyword.Visible = true; } string category = WebUtility.UrlDecode(WebUtility.UrlDecode(e.Category)); if (lblKeyword.Text != category) { lblKeyword.Text = category; progressBar.Width = lblKeyword.Left - progressBar.Left; } e.Overwriten = chbOverwritenDownload.Checked; e.Download = chbAutoDownload.Checked; e.Group = chbGroupByKeyword.Checked; string strDir = txtWorkingDirectory.Text; if (strDir.Substring(strDir.Length - 1, 1) != "\\") { strDir += "\\"; } string strExt = string.Empty; string strCategory = string.Empty; int nPos = e.URL.LastIndexOf("."); if (nPos > 0) { strExt = e.URL.Substring(nPos + 1); } if (e.Group) { strCategory = e.Category; } string strFullPath = strDir; if (strExt != string.Empty) { if (strCategory != string.Empty) { strFullPath += strCategory; strFullPath += "\\"; } strFullPath += (ReplaceSpecialCharacters(e.Title) + "." + strExt); } else { strFullPath += ReplaceSpecialCharacters(e.Title); } e.Path = strFullPath; item.SubItems.Add(e.Path); item.Tag = e; e.Index = item.Index; } } }
private void lvwBooks_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (string file in files) { XmlDocument xmlDocument = new XmlDocument(); try { xmlDocument.Load(file); XmlElement docElm = xmlDocument.DocumentElement; if (docElm != null && docElm.ChildNodes.Count > 0) { XmlNode elm = docElm.FirstChild; lvwBooks.Items.Clear(); string title = string.Empty; string url = string.Empty; string id = string.Empty; int pos = -1; while (elm != null) { title = string.Empty; url = string.Empty; id = string.Empty; foreach (XmlAttribute xmlAttribute in elm.Attributes) { if (xmlAttribute.Name == "title") { title = xmlAttribute.Value; } else if (xmlAttribute.Name == "url") { url = xmlAttribute.Value; } } if (url != string.Empty) { pos = url.LastIndexOf("/"); if (pos > 0) { id = url.Substring(pos + 1); } else { id = url; } ListViewItem item = lvwBooks.Items.Add(string.Format("{0}", lvwBooks.Items.Count + 1)); if (item != null) { BookEventArg book = new BookEventArg(id, title, url); book.Index = item.Index; item.Tag = book; item.SubItems.Add(book.Title); item.SubItems.Add(book.URL); item.SubItems.Add(string.Empty); this.OnFileFound(this, book); } } elm = elm.NextSibling; } } } catch (Exception ex) { xmlDocument = null; MessageBox.Show(ex.Message, "Import Files", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } break; //Use first file only } }
protected void DownloadBook(BookEventArg e) { if (IsCancel) { e.Status = -1; e.Progress = 100; BookDownloadCompleted?.Invoke(this, e); return; } WebClient webClient = null; if (_downloaders.ContainsKey(e.URL)) { webClient = _downloaders[e.URL]; } else { webClient = new WebClient(); webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFinished); webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged); _downloaders.Add(e.URL, webClient); } if (IsCancel) { e.Status = -1; e.Progress = 100; BookDownloadCompleted?.Invoke(this, e); return; } if (webClient != null) { if (webClient.IsBusy) { webClient.CancelAsync(); } if (File.Exists(e.Path)) { System.IO.FileInfo fi = new FileInfo(e.Path); if (fi.Length > 0) { BookDownloadingFileExist?.Invoke(this, e); if (!e.Overwriten) { e.Status = 2; e.Progress = 100; BookDownloadCompleted?.Invoke(this, e); return; } } } if (IsCancel) { e.Status = -1; e.Progress = 100; BookDownloadCompleted?.Invoke(this, e); return; } try { if (EnsureDirectoryExist(e.Path)) { webClient.DownloadFile(new Uri(e.URL), e.Path); e.Status = 0; } else { e.Status = -1; } e.Progress = 100; BookDownloadCompleted?.Invoke(this, e); return; } catch (Exception ex) { e.Status = 1; e.Progress = 100; BookDownloadCompleted?.Invoke(this, e); return; } } }
protected override KeyValuePair <string, string> SearchLink(string link) { string strHome = link.Substring(0, Home.Length); string strURL = Home + link; if (string.Compare(strHome, Home, true) == 0) { strURL = link; } HttpWebRequest httpReq = WebRequest.Create(strURL) as HttpWebRequest; KeyValuePair <string, string> file = new KeyValuePair <string, string>(); if (httpReq != null) { if (IsCancel) { return(file); } WebResponse webResponse = httpReq.GetResponse(); Stream html = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(html); string htmlString = reader.ReadToEnd(); string strDIVOpen = "<h1 class=\"single-title\">"; string strDIVClose = "</h1>"; int first = htmlString.IndexOf(strDIVOpen, 0); int last = htmlString.IndexOf(strDIVClose, first + strDIVOpen.Length); string strLink = string.Empty; string strTemp = string.Empty; string strTitle = string.Empty; strURL = string.Empty; if (first > 0) { if (last < first) { last = htmlString.Length; } strTitle = htmlString.Substring(first + strDIVOpen.Length, last - first - strDIVOpen.Length).Trim(); } strDIVOpen = "<span class=\"download-links\">"; strDIVClose = "</span>"; first = htmlString.IndexOf(strDIVOpen, first); if (first > 0) { last = htmlString.IndexOf(strDIVClose, first); if (last > first) { strDIVOpen = "<a href=\""; strDIVClose = "\""; first = htmlString.IndexOf(strDIVOpen, first); if (first > 0) { last = htmlString.IndexOf(strDIVClose, first + strDIVOpen.Length); if (last > first) { strURL = htmlString.Substring(first + strDIVOpen.Length, last - first - strDIVOpen.Length); if (strURL.Length > 0) { first = strURL.LastIndexOf("/"); last = strURL.LastIndexOf("."); if (last < first) { last = strURL.Length; } string strID = strURL.Substring(first, last - first + 1); if (strTitle == string.Empty) { strTitle = strID; } file = new KeyValuePair <string, string>(strURL, WebUtility.HtmlDecode(strTitle)); BookEventArg e = new BookEventArg(strID, file.Value, file.Key); OnBookFound(e); return(file); } } } } } } return(new KeyValuePair <string, string>()); }