PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/TVRename#/Forms/AddEditSeasEpFinders.cs

https://gitlab.com/kingofzeal/tvrename
C# | 326 lines | 251 code | 56 blank | 19 comment | 32 complexity | 68eb233a825d3db7ba0099f06924c6a0 MD5 | raw file
  1. //
  2. // Main website for TVRename is http://tvrename.com
  3. //
  4. // Source code available at http://code.google.com/p/tvrename/
  5. //
  6. // This code is released under GPLv3 http://www.gnu.org/licenses/gpl.html
  7. //
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Windows.Forms;
  11. using System.Drawing;
  12. using System.IO;
  13. namespace TVRename
  14. {
  15. /// <summary>
  16. /// Summary for AddEditSeasEpFinders
  17. ///
  18. /// WARNING: If you change the name of this class, you will need to change the
  19. /// 'Resource File Name' property for the managed resource compiler tool
  20. /// associated with all .resx files this class depends on. Otherwise,
  21. /// the designers will not be able to interact properly with localized
  22. /// resources associated with this form.
  23. /// </summary>
  24. public partial class AddEditSeasEpFinders : Form
  25. {
  26. private List<FilenameProcessorRE> Rex;
  27. private List<ShowItem> SIL;
  28. public AddEditSeasEpFinders(List<FilenameProcessorRE> rex, List<ShowItem> sil, ShowItem initialShow, string initialFolder)
  29. {
  30. this.Rex = rex;
  31. this.SIL = sil;
  32. this.InitializeComponent();
  33. this.SetupGrid();
  34. this.FillGrid(this.Rex);
  35. foreach (ShowItem si in this.SIL)
  36. {
  37. this.cbShowList.Items.Add(si.ShowName);
  38. if (si == initialShow)
  39. this.cbShowList.SelectedIndex = this.cbShowList.Items.Count - 1;
  40. }
  41. this.txtFolder.Text = initialFolder;
  42. }
  43. public void SetupGrid()
  44. {
  45. SourceGrid.Cells.Views.Cell titleModel = new SourceGrid.Cells.Views.Cell
  46. {
  47. BackColor = Color.SteelBlue,
  48. ForeColor = Color.White,
  49. TextAlignment = DevAge.Drawing.ContentAlignment.MiddleLeft
  50. };
  51. SourceGrid.Cells.Views.Cell titleModelC = new SourceGrid.Cells.Views.Cell
  52. {
  53. BackColor = Color.SteelBlue,
  54. ForeColor = Color.White,
  55. TextAlignment =
  56. DevAge.Drawing.ContentAlignment.MiddleCenter
  57. };
  58. this.Grid1.Columns.Clear();
  59. this.Grid1.Rows.Clear();
  60. this.Grid1.RowsCount = 1;
  61. this.Grid1.ColumnsCount = 4;
  62. this.Grid1.FixedRows = 1;
  63. this.Grid1.FixedColumns = 0;
  64. this.Grid1.Selection.EnableMultiSelection = false;
  65. this.Grid1.Columns[0].AutoSizeMode = SourceGrid.AutoSizeMode.None;
  66. this.Grid1.Columns[0].Width = 60;
  67. this.Grid1.Columns[1].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize | SourceGrid.AutoSizeMode.EnableStretch;
  68. this.Grid1.Columns[2].AutoSizeMode = SourceGrid.AutoSizeMode.None;
  69. this.Grid1.Columns[2].Width = 60;
  70. this.Grid1.Columns[3].AutoSizeMode = SourceGrid.AutoSizeMode.EnableAutoSize | SourceGrid.AutoSizeMode.EnableStretch;
  71. this.Grid1.AutoStretchColumnsToFitWidth = true;
  72. this.Grid1.Columns.StretchToFit();
  73. //////////////////////////////////////////////////////////////////////
  74. // header row
  75. SourceGrid.Cells.ColumnHeader h;
  76. h = new SourceGrid.Cells.ColumnHeader("Enabled");
  77. h.AutomaticSortEnabled = false;
  78. this.Grid1[0, 0] = h;
  79. this.Grid1[0, 0].View = titleModelC;
  80. h = new SourceGrid.Cells.ColumnHeader("Regex");
  81. h.AutomaticSortEnabled = false;
  82. this.Grid1[0, 1] = h;
  83. this.Grid1[0, 1].View = titleModel;
  84. h = new SourceGrid.Cells.ColumnHeader("Full Path");
  85. h.AutomaticSortEnabled = false;
  86. this.Grid1[0, 2] = h;
  87. this.Grid1[0, 2].View = titleModelC;
  88. h = new SourceGrid.Cells.ColumnHeader("Notes");
  89. h.AutomaticSortEnabled = false;
  90. this.Grid1[0, 3] = h;
  91. this.Grid1[0, 3].View = titleModel;
  92. this.Grid1.Selection.SelectionChanged += this.SelectionChanged;
  93. }
  94. public void SelectionChanged(Object sender, SourceGrid.RangeRegionChangedEventArgs e)
  95. {
  96. this.StartTimer();
  97. }
  98. public void AddNewRow()
  99. {
  100. int r = this.Grid1.RowsCount;
  101. this.Grid1.RowsCount = r + 1;
  102. this.Grid1[r, 0] = new SourceGrid.Cells.CheckBox(null, true);
  103. this.Grid1[r, 1] = new SourceGrid.Cells.Cell("", typeof(string));
  104. this.Grid1[r, 2] = new SourceGrid.Cells.CheckBox(null, false);
  105. this.Grid1[r, 3] = new SourceGrid.Cells.Cell("", typeof(string));
  106. ChangedCont changed = new ChangedCont(this);
  107. for (int c = 0; c < 4; c++)
  108. this.Grid1[r, c].AddController(changed);
  109. }
  110. public void FillGrid(List<FilenameProcessorRE> list)
  111. {
  112. while (this.Grid1.Rows.Count > 1) // leave header row
  113. this.Grid1.Rows.Remove(1);
  114. this.Grid1.RowsCount = list.Count + 1;
  115. int i = 1;
  116. foreach (FilenameProcessorRE re in list)
  117. {
  118. this.Grid1[i, 0] = new SourceGrid.Cells.CheckBox(null, re.Enabled);
  119. this.Grid1[i, 1] = new SourceGrid.Cells.Cell(re.RE, typeof(string));
  120. this.Grid1[i, 2] = new SourceGrid.Cells.CheckBox(null, re.UseFullPath);
  121. this.Grid1[i, 3] = new SourceGrid.Cells.Cell(re.Notes, typeof(string));
  122. ChangedCont changed = new ChangedCont(this);
  123. for (int c = 0; c < 4; c++)
  124. this.Grid1[i, c].AddController(changed);
  125. i++;
  126. }
  127. this.StartTimer();
  128. }
  129. private FilenameProcessorRE REForRow(int i)
  130. {
  131. if ((i < 1) || (i >= this.Grid1.RowsCount)) // row 0 is header
  132. return null;
  133. bool en = (bool) (this.Grid1[i, 0].Value);
  134. string regex = (string) (this.Grid1[i, 1].Value);
  135. bool fullPath = (bool) (this.Grid1[i, 2].Value);
  136. string notes = (string) (this.Grid1[i, 3].Value) ?? "";
  137. if (string.IsNullOrEmpty(regex))
  138. return null;
  139. return new FilenameProcessorRE(en, regex, fullPath, notes);
  140. }
  141. private void bnOK_Click(object sender, System.EventArgs e)
  142. {
  143. this.Rex.Clear();
  144. for (int i = 1; i < this.Grid1.RowsCount; i++) // skip header row
  145. {
  146. FilenameProcessorRE re = this.REForRow(i);
  147. if (re != null)
  148. this.Rex.Add(re);
  149. }
  150. }
  151. private void bnAdd_Click(object sender, System.EventArgs e)
  152. {
  153. this.AddNewRow();
  154. this.Grid1.Selection.Focus(new SourceGrid.Position(this.Grid1.RowsCount - 1, 1), true);
  155. this.StartTimer();
  156. }
  157. private void bnDelete_Click(object sender, System.EventArgs e)
  158. {
  159. // multiselection is off, so we can cheat...
  160. int[] rowsIndex = this.Grid1.Selection.GetSelectionRegion().GetRowsIndex();
  161. if (rowsIndex.Length > 0)
  162. this.Grid1.Rows.Remove(rowsIndex[0]);
  163. this.StartTimer();
  164. }
  165. private void bnBrowse_Click(object sender, System.EventArgs e)
  166. {
  167. if (!string.IsNullOrEmpty(this.txtFolder.Text))
  168. this.folderBrowser.SelectedPath = this.txtFolder.Text;
  169. if (this.folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  170. this.txtFolder.Text = this.folderBrowser.SelectedPath;
  171. this.StartTimer();
  172. }
  173. private void cbShowList_SelectedIndexChanged(object sender, System.EventArgs e)
  174. {
  175. this.StartTimer();
  176. }
  177. private void txtFolder_TextChanged(object sender, System.EventArgs e)
  178. {
  179. this.StartTimer();
  180. }
  181. private void StartTimer()
  182. {
  183. this.lvPreview.Enabled = false;
  184. this.tmrFillPreview.Start();
  185. }
  186. private void tmrFillPreview_Tick(object sender, System.EventArgs e)
  187. {
  188. this.tmrFillPreview.Stop();
  189. this.FillPreview();
  190. }
  191. private void chkTestAll_CheckedChanged(object sender, System.EventArgs e)
  192. {
  193. this.StartTimer();
  194. }
  195. private void FillPreview()
  196. {
  197. this.lvPreview.Items.Clear();
  198. if ((string.IsNullOrEmpty(this.txtFolder.Text)) || (!Directory.Exists(this.txtFolder.Text)))
  199. {
  200. this.txtFolder.BackColor = Helpers.WarningColor();
  201. return;
  202. }
  203. else
  204. this.txtFolder.BackColor = System.Drawing.SystemColors.Window;
  205. if (this.Grid1.RowsCount <= 1) // 1 for header
  206. return; // empty
  207. this.lvPreview.Enabled = true;
  208. List<FilenameProcessorRE> rel = new List<FilenameProcessorRE>();
  209. if (this.chkTestAll.Checked)
  210. {
  211. for (int i = 1; i < this.Grid1.RowsCount; i++)
  212. {
  213. FilenameProcessorRE re = this.REForRow(i);
  214. if (re != null)
  215. rel.Add(re);
  216. }
  217. }
  218. else
  219. {
  220. int[] rowsIndex = this.Grid1.Selection.GetSelectionRegion().GetRowsIndex();
  221. if (rowsIndex.Length == 0)
  222. return;
  223. FilenameProcessorRE re2 = this.REForRow(rowsIndex[0]);
  224. if (re2 != null)
  225. rel.Add(re2);
  226. else
  227. return;
  228. }
  229. this.lvPreview.BeginUpdate();
  230. DirectoryInfo d = new DirectoryInfo(this.txtFolder.Text);
  231. foreach (FileInfo fi in d.GetFiles())
  232. {
  233. int seas;
  234. int ep;
  235. if (!TVSettings.Instance.UsefulExtension(fi.Extension, true))
  236. continue; // move on
  237. ShowItem si = this.cbShowList.SelectedIndex >= 0 ? this.SIL[this.cbShowList.SelectedIndex] : null;
  238. bool r = TVDoc.FindSeasEp(fi, out seas, out ep, si, rel, false);
  239. ListViewItem lvi = new ListViewItem();
  240. lvi.Text = fi.Name;
  241. lvi.SubItems.Add((seas == -1) ? "-" : seas.ToString());
  242. lvi.SubItems.Add((ep == -1) ? "-" : ep.ToString());
  243. if (!r)
  244. lvi.BackColor = Helpers.WarningColor();
  245. this.lvPreview.Items.Add(lvi);
  246. }
  247. this.lvPreview.EndUpdate();
  248. }
  249. private void bnDefaults_Click(object sender, System.EventArgs e)
  250. {
  251. DialogResult dr = MessageBox.Show("Restore to default matching expressions?", "Filename Processors", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
  252. if (dr == DialogResult.Yes)
  253. this.FillGrid(TVSettings.DefaultFNPList());
  254. }
  255. #region Nested type: ChangedCont
  256. public class ChangedCont : SourceGrid.Cells.Controllers.ControllerBase
  257. {
  258. private AddEditSeasEpFinders P;
  259. public ChangedCont(AddEditSeasEpFinders p)
  260. {
  261. this.P = p;
  262. }
  263. public override void OnValueChanged(SourceGrid.CellContext sender, EventArgs e)
  264. {
  265. this.P.StartTimer();
  266. }
  267. }
  268. #endregion
  269. }
  270. }