private void ExportCatToTextFile(int nOrder, GanjoorCat cat, string targetFolder, DbBrowser dbBrowser, bool separateFiles = false) { string catInLatinLetters = (nOrder + 1).ToString("D3") + "-" + GPersianTextSync.Farglisize(cat._Text); if (catInLatinLetters.Length > 16) { catInLatinLetters = catInLatinLetters.Substring(0, 16); } string catFolder = Path.Combine(targetFolder, catInLatinLetters); if (!Directory.Exists(catFolder)) { Directory.CreateDirectory(catFolder); } StringBuilder sb = new StringBuilder(); if (DRY_ExportCat(dbBrowser, cat, sb, separateFiles ? catFolder : "")) { if (!separateFiles) { File.WriteAllText(Path.Combine(catFolder, catInLatinLetters + ".txt"), sb.ToString()); } } List <GanjoorCat> cats = dbBrowser.GetSubCategories(cat._ID); for (int i = 0; i < cats.Count; i++) { ExportCatToTextFile(i, cats[i], catFolder, dbBrowser, separateFiles); } }
private void ExportPoetToTextFile(GanjoorPoet poet, string targetFolder, DbBrowser dbBrowser) { if (!Directory.Exists(targetFolder)) { Directory.CreateDirectory(targetFolder); } StringBuilder sb = new StringBuilder(); if (Settings.Default.ExportPoetName) { string poetNameSeparator = Settings.Default.ExportPoetSep; if (!string.IsNullOrEmpty(poetNameSeparator)) { sb.AppendLine(poetNameSeparator); } sb.AppendLine(poet._Name); if (!string.IsNullOrEmpty(poetNameSeparator)) { sb.AppendLine(poetNameSeparator); } } GanjoorCat poetCat = dbBrowser.GetCategory(poet._CatID); DRY_ExportCat(dbBrowser, poetCat, sb); List <int> lstSubCategories = dbBrowser.GetAllSubCats(poetCat._ID); foreach (int catId in lstSubCategories) { DRY_ExportCat(dbBrowser, dbBrowser.GetCategory(catId), sb); } File.WriteAllText(Path.Combine(targetFolder, GPersianTextSync.Farglisize(poet._Name) + ".txt"), sb.ToString()); }
private void btnMoveToCat_Click(object sender, EventArgs e) { int PoetId = _db.GetCategory(Settings.Default.LastCat)._PoetID; using (CategorySelector dlg = new CategorySelector(PoetId)) { if (dlg.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { int NewCatId = dlg.SelectedCatID; if (NewCatId == Settings.Default.LastCat) { MessageBox.Show("شما بخش جاری را انتخاب کردهاید!"); } else { GanjoorCat cat = _db.GetCategory(NewCatId); if (MessageBox.Show(string.Format("از انتقال {0} شعر انتخابی از بخش «{1}» به بخش «{2}» اطمینان دارید؟", grdMain.SelectedRows.Count, _db.GetCategory(Settings.Default.LastCat)._Text, cat._Text), "تأییدیه", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No) { return; } _db.BeginBatchOperation(); foreach (DataGridViewRow Row in grdMain.SelectedRows) { int PoemID = Convert.ToInt32(Row.Cells[ClmnID].Value); _db.SetPoemCatID(PoemID, NewCatId); } _db.CommitBatchOperation(); LoadGridData(); } } } }
private bool DRY_ExportCat(DbBrowser dbBrowser, GanjoorCat cat, StringBuilder sb, string catFolderForSeparatePorms = "") { if (Settings.Default.ExportCatName) { string catNameSeparator = Settings.Default.ExportCatSep; if (!string.IsNullOrEmpty(catNameSeparator)) { sb.AppendLine(catNameSeparator); } sb.AppendLine(cat._Text); if (!string.IsNullOrEmpty(catNameSeparator)) { sb.AppendLine(catNameSeparator); } } List <GanjoorPoem> poems = dbBrowser.GetPoems(cat._ID); if (poems.Count > 0) { bool exportPoemName = Settings.Default.ExportPoemName; string poemNameSeparator = Settings.Default.ExportPoemSep; foreach (GanjoorPoem poem in poems) { if (!string.IsNullOrEmpty(catFolderForSeparatePorms)) { sb = new StringBuilder(); } DRY_ExportPoem(dbBrowser, poem, sb, exportPoemName, poemNameSeparator); if (!string.IsNullOrEmpty(catFolderForSeparatePorms)) { File.WriteAllText(Path.Combine(catFolderForSeparatePorms, poem._ID.ToString() + ".txt"), sb.ToString()); } } return(true); } return(false); }
private void mnuImport_Click(object sender, EventArgs e) { int nCurCatId = ganjoorView.CurrentCatId; int nCurPoetId = ganjoorView.CurrentPoetId; if (nCurCatId < 0 || nCurPoetId < 0) { MessageBox.Show("لطفا ابتدا وارد آثار شاعر (و بخش مورد نظر) شوید."); return; } #region Old Code if ((ModifierKeys | Keys.Shift) == ModifierKeys) { using (TextImporter dlgOld = new TextImporter()) { if (dlgOld.ShowDialog(this) == DialogResult.OK) { string fileName = dlgOld.FileName; string mainCatText = dlgOld.MainCatText; string[] subCatTexts = dlgOld.SubCatTexts; int nMaxVerseTextLength = dlgOld.MaxVerseTextLength; bool bTabularVerses = dlgOld.TabularVerses; Cursor = Cursors.WaitCursor; Application.DoEvents(); string[] lines = File.ReadAllLines(fileName); Application.DoEvents(); GanjoorCat curMainCat = null; GanjoorPoem curPoem = null; int nTotalLines = lines.Length; int nCurLine = 0; int nCurVerse = 0; DbBrowser dbBrowser = new DbBrowser(); dbBrowser.BeginBatchOperation(); while (nCurLine < nTotalLines) { if (lines[nCurLine].Contains(mainCatText)) { curMainCat = dbBrowser.CreateNewCategory(lines[nCurLine].Trim(), nCurCatId, nCurPoetId); } else if (curMainCat != null) { bool bNewCatOrPoem = false; foreach (string subCatText in subCatTexts) { if (lines[nCurLine].Contains(subCatText)) { if (bTabularVerses) { ReArrangeTabularVerses(curPoem, dbBrowser); } curPoem = dbBrowser.CreateNewPoem(lines[nCurLine].Trim(), curMainCat._ID); bNewCatOrPoem = true; nCurVerse = 0; break; } } if (!bNewCatOrPoem) { string line = lines[nCurLine].Trim(); if (!string.IsNullOrEmpty(line)) { int nWordsCount = line.Split(' ').Length; bool bVerseDetected = false; if (nWordsCount <= nMaxVerseTextLength) { int nNextLine = nCurLine + 1; if (nNextLine < nTotalLines) { while (nNextLine < nTotalLines) { string nextLine = lines[nNextLine].Trim(); if (string.IsNullOrEmpty(nextLine)) { nNextLine++; } else { int nNextWordsCount = nextLine.Split(' ').Length; if (nNextWordsCount <= nMaxVerseTextLength) { if (nextLine.Contains(mainCatText)) { break; } bool bBreak = false; foreach (string subCatText in subCatTexts) { if (nextLine.Contains(subCatText)) { bBreak = true; break; } } if (bBreak) { break; } if (curPoem == null) { MessageBox.Show("curPoem == null"); return; } bVerseDetected = true; GanjoorVerse v1 = dbBrowser.CreateNewVerse(curPoem._ID, nCurVerse, VersePosition.Right); dbBrowser.SetVerseText(curPoem._ID, v1._Order, line); nCurVerse++; GanjoorVerse v2 = dbBrowser.CreateNewVerse(curPoem._ID, nCurVerse, VersePosition.Left); dbBrowser.SetVerseText(curPoem._ID, v2._Order, nextLine); nCurVerse++; nCurLine = nNextLine; break; } else { break; } } } } } if (!bVerseDetected) { if (curPoem == null) { MessageBox.Show("curPoem == null"); return; } GanjoorVerse p = dbBrowser.CreateNewVerse(curPoem._ID, nCurVerse, VersePosition.Paragraph); dbBrowser.SetVerseText(curPoem._ID, p._Order, line); nCurVerse++; } } } } nCurLine++; } if (bTabularVerses) { ReArrangeTabularVerses(curPoem, dbBrowser); } dbBrowser.CommitBatchOperation(); dbBrowser.CloseDb(); Cursor = Cursors.Default; MessageBox.Show("انجام شد"); ganjoorView.Font = ganjoorView.Font; } } return; } #endregion using (GeneralTextImporter dlg = new GeneralTextImporter()) { if (dlg.ShowDialog(this) == DialogResult.OK) { string fileName = dlg.FileName; string nextPoemStartText = dlg.NextPoemStartText; bool nextPoemStartIsAShortText = dlg.NextPoemStartIsAShortText; int nShortTextLength = dlg.ShortTextLength; Cursor = Cursors.WaitCursor; Application.DoEvents(); string[] lines = File.ReadAllLines(fileName); Application.DoEvents(); int nTotalLines = lines.Length; int nCurLine = 0; int nCurVerse = 0; DbBrowser dbBrowser = new DbBrowser(); GanjoorCat cat = dbBrowser.GetCategory(nCurCatId); if (cat == null) { MessageBox.Show("cat == null"); dbBrowser.CloseDb(); return; } GanjoorPoem curPoem = null; dbBrowser.BeginBatchOperation(); while (nCurLine < nTotalLines) { string line = lines[nCurLine].Trim(); if (!string.IsNullOrEmpty(line)) { if ( (nextPoemStartIsAShortText && line.Length < nShortTextLength) || (!nextPoemStartIsAShortText && line.IndexOf(nextPoemStartText) == 0) ) { curPoem = dbBrowser.CreateNewPoem(line, nCurCatId); nCurVerse = 0; } else if (curPoem != null) { GanjoorVerse v = dbBrowser.CreateNewVerse(curPoem._ID, nCurVerse, nCurVerse % 2 == 0 ? VersePosition.Right : VersePosition.Left); dbBrowser.SetVerseText(curPoem._ID, v._Order, line); nCurVerse++; } } nCurLine++; } dbBrowser.CommitBatchOperation(); dbBrowser.CloseDb(); Cursor = Cursors.Default; MessageBox.Show("انجام شد"); ganjoorView.Font = ganjoorView.Font; } } }
/// <summary> /// سازندۀ پیش فرض : یک نمونه از کلاس را با مقادیر ورودی می سازد /// </summary> public GanjoorCat(GanjoorCat baseCat, int StartPoem) : this(baseCat._ID, baseCat._PoetID, baseCat._Text, baseCat._ParentID, baseCat._Url, StartPoem) { }