private GDBInfo ExtracInfoFromGDBFileAndAddToGrid(string FileName, string ext, int FileSize) { GDBInfo gdb = null; DbBrowser gdbBrowser = new DbBrowser(FileName); if (gdbBrowser.Poets.Count != 1) { MessageBox.Show("این ابزار در حال حاضر تنها مجموعههای حاوی یک شاعر را پشتیبانی میکند."); } else { GanjoorPoet poet = gdbBrowser.Poets[0]; gdb = new GDBInfo(); gdb.CatName = poet._Name; gdb.PoetID = poet._ID; int minCatID, minPoemID; gdbBrowser.GetMinIDs(poet._ID, out minCatID, out minPoemID); gdb.CatID = minCatID; gdb.FileExt = ext; gdb.FileSizeInByte = FileSize; gdb.LowestPoemID = minPoemID; gdb.PubDate = DateTime.Now; } gdbBrowser.CloseDb(); return(gdb); }
public GdbDownloadInfo(GDBInfo gdbInfo) { InitializeComponent(); this.Tag = gdbInfo; this.lblGdbName.Text = gdbInfo.CatName; }
private int AddGdbOrZipFileToGrid(string FileName) { int FileSize = File.ReadAllBytes(FileName).Length; GDBInfo gdb = null; if (Path.GetExtension(FileName).Equals(".zip", StringComparison.InvariantCultureIgnoreCase)) { using (ZipStorer zip = ZipStorer.Open(FileName, FileAccess.Read)) { List <ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir(); foreach (ZipStorer.ZipFileEntry entry in dir) { string gdbFileName = Path.GetFileName(entry.FilenameInZip); if (Path.GetExtension(gdbFileName).Equals(".gdb") || Path.GetExtension(gdbFileName).Equals(".s3db")) { string ganjoorPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ganjoor"); if (!Directory.Exists(ganjoorPath)) { Directory.CreateDirectory(ganjoorPath); } string gdbExtractPath = Path.Combine(ganjoorPath, gdbFileName); if (zip.ExtractFile(entry, gdbExtractPath)) { gdb = ExtracInfoFromGDBFileAndAddToGrid(gdbExtractPath, Path.GetExtension(FileName), FileSize); File.Delete(gdbExtractPath); } } } } } else { gdb = ExtracInfoFromGDBFileAndAddToGrid(FileName, Path.GetExtension(FileName), FileSize); } if (gdb != null) { if (string.IsNullOrEmpty(txtRefUrl.Text)) { gdb.DownloadUrl = FileName; } else { gdb.DownloadUrl = txtRefUrl.Text + Path.GetFileName(FileName); } return(AddGDBInfo(gdb)); } return(-1); }
private int AddGDBInfo(GDBInfo gdb) { int RowIndex = grd.Rows.Add(); grd.Rows[RowIndex].Cells[CLMN_CATNAME].Value = gdb.CatName; grd.Rows[RowIndex].Cells[CLMN_POETID].Value = gdb.PoetID; grd.Rows[RowIndex].Cells[CLMN_CATID].Value = gdb.CatID; grd.Rows[RowIndex].Cells[CLMN_DWNLDURL].Value = gdb.DownloadUrl; grd.Rows[RowIndex].Cells[CLMN_BLOG].Value = gdb.BlogUrl; grd.Rows[RowIndex].Cells[CLMN_EXT].Value = gdb.FileExt; grd.Rows[RowIndex].Cells[CLMN_IMAGE].Value = gdb.ImageUrl; grd.Rows[RowIndex].Cells[CLMN_SIZE].Value = gdb.FileSizeInByte; grd.Rows[RowIndex].Cells[CLMN_POEMID].Value = gdb.LowestPoemID; grd.Rows[RowIndex].Cells[CLMN_PUBDATE].Value = gdb.PubDate.ToString("yyyy-MM-dd"); return(RowIndex); }
private void grdList_CellContentClick(object sender, DataGridViewCellEventArgs e) { GDBInfo gdb = grdList.Rows[e.RowIndex].Tag as GDBInfo; if (gdb != null) { try { switch (e.ColumnIndex) { case GRDCLMN_DWNLD: if (!string.IsNullOrEmpty(gdb.DownloadUrl)) { Process.Start(gdb.DownloadUrl); } break; case GRDCLMN_MORE: if (!string.IsNullOrEmpty(gdb.BlogUrl)) { Process.Start(gdb.BlogUrl); } break; case GRDCLMN_IGNORE: grdList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !((bool)grdList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); //why do I need this? break; case GRDCLMN_CHECK: grdList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = !Convert.ToBoolean(grdList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value); break; } } catch { //Sometimes something occurs here! } } }
private void SaveToXml(string FileName) { List <GDBInfo> lst = new List <GDBInfo>(); foreach (DataGridViewRow Row in grd.Rows) { if (!Row.IsNewRow) { bool err = false; GDBInfo gdb = ConvertGridRowToGDBInfo(Row, ref err); lst.Add(gdb); if (err) { MessageBox.Show(string.Format("در تبدیل دادههای ردیف {0} مشکلی پیش آمد. اما فایل با توکل به خدا ;) ذخیره خواهد شد. جهت اطمینان دادههای ردیف مذکور را بازبینی کنید.", Row.Index + 1), "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign); } } } if (GDBListProcessor.Save(FileName, txtName.Text, txtDescription.Text, txtMoreInfoUrl.Text, lst)) { if ( MessageBox.Show("فایل به درستی ذخیره شد. میخواهید آن را مشاهده کنید؟", "اعلان", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign) == System.Windows.Forms.DialogResult.Yes ) { try { System.Diagnostics.Process.Start(FileName); } catch { } } } else { MessageBox.Show("تلاش برای ذخیره موفقیتآمیز نبود.", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.RtlReading | MessageBoxOptions.RightAlign); } }
private void btnFromDb_Click(object sender, EventArgs e) { using (FolderBrowserDialog dlg = new FolderBrowserDialog()) { dlg.Description = "مسیر خروجیها را انتخاب کنید"; dlg.ShowNewFolderButton = true; if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { bool embedPictures = false; string picPath = string.Empty; string picUrPrefix = string.Empty; using (GDBPictureDirSelector plg = new GDBPictureDirSelector()) if (plg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { embedPictures = plg.EmbedPictures; picPath = plg.PicturesPath; picUrPrefix = plg.PicturesUrlPrefix; if (!Directory.Exists(picPath)) { embedPictures = false; } } this.Enabled = false; List <int> existingIDs = new List <int>(); foreach (DataGridViewRow Row in grd.Rows) { if (!Row.IsNewRow) { bool err = false; GDBInfo gdb = ConvertGridRowToGDBInfo(Row, ref err); if (!err) { existingIDs.Add(gdb.PoetID); } } } DbBrowser db = new DbBrowser(); foreach (GanjoorPoet Poet in db.Poets) { if (existingIDs.IndexOf(Poet._ID) == -1)//existing items in grid { string outFile = Path.Combine(dlg.SelectedPath, GPersianTextSync.Farglisize(Poet._Name)); string gdbFile = outFile + ".gdb"; if (db.ExportPoet(gdbFile, Poet._ID)) { string zipFile = outFile + ".zip"; using (ZipStorer zipStorer = ZipStorer.Create(zipFile, "")) { zipStorer.AddFile(ZipStorer.Compression.Deflate, gdbFile, Path.GetFileName(gdbFile), ""); if (embedPictures) { string pngPath = Path.Combine(picPath, Poet._ID.ToString() + ".png"); if (File.Exists(pngPath)) { zipStorer.AddFile(ZipStorer.Compression.Deflate, pngPath, Path.GetFileName(pngPath), ""); } } } File.Delete(gdbFile); int RowIndex = AddGdbOrZipFileToGrid(zipFile); if (embedPictures && !string.IsNullOrEmpty(picUrPrefix)) { string pngPath = Path.Combine(picPath, Poet._ID.ToString() + ".png"); if (File.Exists(pngPath)) { grd.Rows[RowIndex].Cells[CLMN_IMAGE].Value = picUrPrefix + Path.GetFileName(pngPath); } } Application.DoEvents(); } else { MessageBox.Show(db.LastError); } } } this.Enabled = true; db.CloseDb(); } } }
private static GDBInfo ConvertGridRowToGDBInfo(DataGridViewRow Row, ref bool err) { GDBInfo gdb = new GDBInfo(); try { gdb.CatName = Row.Cells[CLMN_CATNAME].Value.ToString(); } catch { err = true; } try { gdb.PoetID = Convert.ToInt32(Row.Cells[CLMN_POETID].Value); } catch { err = true; } try { gdb.CatID = Convert.ToInt32(Row.Cells[CLMN_CATID].Value); } catch { err = true; } try { gdb.DownloadUrl = Row.Cells[CLMN_DWNLDURL].Value.ToString(); } catch { err = true; } try { gdb.BlogUrl = Row.Cells[CLMN_BLOG].Value == null ? "" : Row.Cells[CLMN_BLOG].Value.ToString(); } catch { err = true; } try { gdb.FileExt = Row.Cells[CLMN_EXT].Value == null ? "gdb" : Row.Cells[CLMN_EXT].Value.ToString(); } catch { err = true; } try { gdb.ImageUrl = Row.Cells[CLMN_IMAGE].Value == null ? "" : Row.Cells[CLMN_IMAGE].Value.ToString(); } catch { err = true; } try { int iFileSizeInByte; if (Row.Cells[CLMN_SIZE].Value != null && Int32.TryParse(Row.Cells[CLMN_SIZE].Value.ToString(), out iFileSizeInByte)) { gdb.FileSizeInByte = iFileSizeInByte; } else { gdb.FileSizeInByte = 0; } } catch { err = true; } try { int iLowestPoemID; if (Row.Cells[CLMN_POEMID].Value != null && Int32.TryParse(Row.Cells[CLMN_POEMID].Value.ToString(), out iLowestPoemID)) { gdb.LowestPoemID = iLowestPoemID; } else { gdb.LowestPoemID = 0; } } catch { err = true; } try { DateTime dt; if (Row.Cells[CLMN_PUBDATE].Value != null && DateTime.TryParse(Row.Cells[CLMN_PUBDATE].Value.ToString(), out dt)) { gdb.PubDate = dt; } else { gdb.PubDate = DateTime.Now; } } catch { err = true; } return(gdb); }
/// <summary> /// دریافت یک فایل xml و تبدیل آن به لیستی از GDBInfoها /// </summary> /// <param name="url"></param> /// <param name="Exception"></param> /// <returns></returns> public static List <GDBInfo> RetrieveList(string url, out string Exception) { List <GDBInfo> lstGDBs = new List <GDBInfo>(); try { WebRequest req = WebRequest.Create(url); GConnectionManager.ConfigureProxy(ref req); using (WebResponse response = req.GetResponse()) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream)) { XmlDocument doc = new XmlDocument(); doc.LoadXml(reader.ReadToEnd()); //Should Redirect? XmlNodeList ndListRedirect = doc.GetElementsByTagName("RedirectInfo"); if (ndListRedirect.Count > 0) { XmlNode redirectNode = ndListRedirect[0]; foreach (XmlNode Node in redirectNode.ChildNodes) { if (Node.Name == "Url") { if (!string.IsNullOrEmpty(Node.InnerText)) { return(RetrieveList(Node.InnerText, out Exception)); } } } } //Collect List: lstGDBs.Clear(); XmlNodeList gdbNodes = doc.GetElementsByTagName("gdb"); foreach (XmlNode gdbNode in gdbNodes) { GDBInfo gdbInfo = new GDBInfo(); foreach (XmlNode Node in gdbNode.ChildNodes) { switch (Node.Name) { case "CatName": gdbInfo.CatName = Node.InnerText; break; case "PoetID": gdbInfo.PoetID = Convert.ToInt32(Node.InnerText); break; case "CatID": gdbInfo.CatID = Convert.ToInt32(Node.InnerText); break; case "DownloadUrl": gdbInfo.DownloadUrl = Node.InnerText; break; case "BlogUrl": gdbInfo.BlogUrl = Node.InnerText; break; case "FileExt": gdbInfo.FileExt = Node.InnerText; break; case "ImageUrl": gdbInfo.ImageUrl = Node.InnerText; break; case "FileSizeInByte": gdbInfo.FileSizeInByte = Convert.ToInt32(Node.InnerText); break; case "LowestPoemID": gdbInfo.LowestPoemID = Convert.ToInt32(Node.InnerText); break; case "PubDate": { string[] dateParts = Node.InnerText.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); int Year = Convert.ToInt32(dateParts[0]); int Month = Convert.ToInt32(dateParts[1]); int Day = Convert.ToInt32(dateParts[2]); gdbInfo.PubDate = new DateTime(Year, Month, Day); } break; } } lstGDBs.Add(gdbInfo); } } } } Exception = string.Empty; return(lstGDBs); } catch (Exception exp) { Exception = exp.Message; return(null); } }
/// <summary> /// دریافت یک فایل xml و تبدیل آن به لیستی از GDBInfoها /// </summary> /// <param name="fileName"></param> /// <param name="Exception"></param> /// <returns></returns> public static List <GDBInfo> RetrieveListFromFile(string fileName, out string Exception) { List <GDBInfo> lstGDBs = new List <GDBInfo>(); try { using (StreamReader reader = File.OpenText(fileName)) { XmlDocument doc = new XmlDocument(); doc.LoadXml(reader.ReadToEnd()); XmlNodeList gdbNodes = doc.GetElementsByTagName("gdb"); foreach (XmlNode gdbNode in gdbNodes) { GDBInfo gdbInfo = new GDBInfo(); foreach (XmlNode Node in gdbNode.ChildNodes) { switch (Node.Name) { case "CatName": gdbInfo.CatName = Node.InnerText; break; case "PoetID": gdbInfo.PoetID = Convert.ToInt32(Node.InnerText); break; case "CatID": gdbInfo.CatID = Convert.ToInt32(Node.InnerText); break; case "DownloadUrl": gdbInfo.DownloadUrl = Node.InnerText; break; case "BlogUrl": gdbInfo.BlogUrl = Node.InnerText; break; case "FileExt": gdbInfo.FileExt = Node.InnerText; break; case "ImageUrl": gdbInfo.ImageUrl = Node.InnerText; break; case "FileSizeInByte": gdbInfo.FileSizeInByte = Convert.ToInt32(Node.InnerText); break; case "LowestPoemID": gdbInfo.LowestPoemID = Convert.ToInt32(Node.InnerText); break; case "PubDate": { string[] dateParts = Node.InnerText.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); int Year = Convert.ToInt32(dateParts[0]); int Month = Convert.ToInt32(dateParts[1]); int Day = Convert.ToInt32(dateParts[2]); gdbInfo.PubDate = new DateTime(Year, Month, Day); } break; } } lstGDBs.Add(gdbInfo); } } Exception = string.Empty; return(lstGDBs); } catch (Exception exp) { Exception = exp.Message; return(null); } }