f_clean.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace Fuel01
  13. {
  14. public partial class f_clean : Form
  15. {
  16. DataSet1.tb_epreuveDataTable tb_epr = new DataSet1.tb_epreuveDataTable();
  17. List<mesfic> maliste = new List<mesfic>();
  18. public class mesfic
  19. {
  20. public bool isProtected=true;
  21. public bool doDelete = false;
  22. public string name = "";
  23. public string description = "";
  24. public string type = "";
  25. public int order = 1;
  26. public string shortname = "";
  27. public mesfic(string _name,string _type, bool _isProtected)
  28. {
  29. name = _name;
  30. if (_type == "D")
  31. {
  32. type = _type;
  33. if (name != "in" && name != "param")
  34. {
  35. isProtected = _isProtected;
  36. order = analyse(name, _type);
  37. }
  38. description = "Répertoire";
  39. if (name.ToUpper() == "IN")
  40. description = "Répertoire de sctockage des données venant des tablettes";
  41. if (name.ToUpper() == "PARAM")
  42. description = "Répertoire des données de paramètres";
  43. }
  44. if (_type == "F")
  45. {
  46. if (name != "epreuve.json")
  47. {
  48. isProtected = false;
  49. order = analyse(name, _type);
  50. }
  51. else
  52. description = "Fichier des epreuves. Ne pas toucher!!!";
  53. }
  54. //type = _type;
  55. }
  56. private int analyse(string inputData,string _type)
  57. {
  58. int ret = 99999;
  59. if ( _type=="D")
  60. {
  61. if (!Int32.TryParse(inputData, out ret))
  62. return 99999;
  63. else
  64. return ret;
  65. }
  66. string[] result;
  67. string pattern = @"_";
  68. RegexOptions regexOptions = RegexOptions.IgnoreCase;
  69. Regex regex = new Regex(pattern, regexOptions);
  70. //on a des zip ou des txt
  71. if (inputData.ToUpper().Contains(".TXT"))
  72. {
  73. type = "T";
  74. inputData=inputData.ToUpper().Replace(".TXT", "");
  75. result = regex.Split(inputData);
  76. if (result.Length != 3)
  77. return 99999;
  78. if (!Int32.TryParse(result[2], out ret))
  79. return 99999;
  80. description = "Resultat de l'archivage n°:" + ret.ToString(); ;
  81. return ret;
  82. }
  83. else
  84. {
  85. if (inputData.ToUpper().Contains(".ZIP"))
  86. {
  87. type = "A";
  88. inputData=inputData.ToUpper().Replace(".ZIP", "");
  89. result = regex.Split(inputData);
  90. if (result.Length != 7)
  91. return ret;
  92. if (!Int32.TryParse(result[0], out ret))
  93. return ret;
  94. description = "Archive après effacement d'une épreuve.";
  95. return ret;
  96. }
  97. else
  98. return ret;
  99. }
  100. }
  101. }
  102. public f_clean()
  103. {
  104. InitializeComponent();
  105. }
  106. private void button1_Click(object sender, EventArgs e)
  107. {
  108. foreach( Control ctrl in myTable.Controls)
  109. {
  110. try
  111. {
  112. CheckBox myBox = ctrl as CheckBox;
  113. if (myBox != null)
  114. if (myBox.Enabled && myBox.Checked)
  115. {
  116. int myrow = myTable.GetRow(myBox);
  117. Label mylabel = myTable.GetControlFromPosition(3, myrow) as Label;
  118. if (mylabel != null)
  119. {
  120. if (mylabel.Tag.ToString() == "D")
  121. {
  122. DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\AsoFuel\" + mylabel.Text);
  123. di.Delete(true);
  124. }
  125. else
  126. {
  127. FileInfo fi = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\AsoFuel\" + mylabel.Text);
  128. fi.Delete();
  129. }
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. MessageBox.Show(ex.Message, "ERREUR");
  136. }
  137. }
  138. vide();
  139. remplir();
  140. }
  141. private void vide()
  142. {
  143. tb_epr.Clear();
  144. tb_epr.ReadXml(Program.folder + @"\epreuve.json");
  145. maliste.Clear();
  146. myTable.SuspendLayout();
  147. for (int i=myTable.Controls.Count-1; i>=0; i--)
  148. myTable.Controls[i].Dispose();
  149. myTable.Controls.Clear();
  150. myTable.ResumeLayout();
  151. myTable.Dispose();
  152. this.myTable = new System.Windows.Forms.TableLayoutPanel();
  153. this.splitContainer3.Panel2.Controls.Add(this.myTable);
  154. this.myTable.AutoScroll = true;
  155. this.myTable.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
  156. this.myTable.ColumnCount = 5;
  157. this.myTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 25F));
  158. this.myTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 25F));
  159. this.myTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 25F));
  160. this.myTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 250F));
  161. this.myTable.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
  162. this.myTable.Location = new System.Drawing.Point(3, 3);
  163. this.myTable.Name = "myTable";
  164. this.myTable.RowCount = 1;
  165. this.myTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
  166. this.myTable.Size = new System.Drawing.Size(598, 25);
  167. }
  168. private void remplir()
  169. {
  170. DirectoryInfo di = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\AsoFuel");
  171. foreach (DirectoryInfo dir in di.GetDirectories())
  172. {
  173. maliste.Add(new mesfic(dir.Name, "D", tb_epr.FindBykey_epr(dir.Name) != null));
  174. }
  175. foreach (FileInfo fi in di.GetFiles())
  176. {
  177. maliste.Add(new mesfic(fi.Name, "F", false));
  178. }
  179. myTable.SuspendLayout();
  180. maliste.Sort(compare.Compare);
  181. foreach (mesfic onefic in maliste )
  182. {
  183. CheckBox chkA = new CheckBox();
  184. chkA.Checked = onefic.isProtected;
  185. chkA.Enabled = false;
  186. CheckBox chkB = new CheckBox();
  187. chkB.Enabled = !chkA.Checked;
  188. Label lblD = new Label();
  189. lblD.Dock = DockStyle.Fill; ;
  190. lblD.AutoSize = false;
  191. lblD.Text = "";
  192. if ( onefic.type=="D")
  193. lblD.Image = global::Fuel01.Properties.Resources.dossier;
  194. else
  195. if (onefic.type == "T")
  196. lblD.Image = global::Fuel01.Properties.Resources.document;
  197. else
  198. if (onefic.type == "A")
  199. lblD.Image = global::Fuel01.Properties.Resources.archive;
  200. Label lblA = new Label();
  201. lblA.Dock = DockStyle.Fill; ;
  202. lblA.AutoSize = true;
  203. lblA.Text = onefic.name;
  204. lblA.Tag = onefic.type;
  205. Label lblB = new Label();
  206. lblB.Text = onefic.description ;
  207. lblB.Dock = DockStyle.Fill; ;
  208. myTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 25F));
  209. myTable.Controls.Add(chkA, 0, myTable.RowCount - 1);
  210. myTable.Controls.Add(chkB, 1, myTable.RowCount - 1);
  211. myTable.Controls.Add(lblD, 2, myTable.RowCount - 1);
  212. myTable.Controls.Add(lblA, 3, myTable.RowCount-1);
  213. myTable.Controls.Add(lblB, 4, myTable.RowCount-1);
  214. myTable.RowCount++;
  215. }
  216. myTable.RowCount++;
  217. myTable.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.AutoSize, 0F));
  218. myTable.Dock = DockStyle.Fill;
  219. myTable.ResumeLayout(true);
  220. }
  221. private void f_clean_Load(object sender, EventArgs e)
  222. {
  223. vide();
  224. remplir();
  225. }
  226. #region COMPARE
  227. CompareFunction compare = new CompareFunction();
  228. class CompareFunction : IComparer<mesfic>
  229. {
  230. public int Compare(mesfic f1, mesfic f2)
  231. {
  232. int result;
  233. if (mesfic.ReferenceEquals(f1, f2))
  234. {
  235. //result = 0;
  236. result = StringCompare(f1.name, f2.name);
  237. }
  238. else
  239. {
  240. if (f1 == null)
  241. {
  242. result = 1;
  243. }
  244. else if (f2 == null)
  245. {
  246. result = -1;
  247. }
  248. else
  249. {
  250. result = NumberCompare(f1.order, f2.order);
  251. if ( result==0)
  252. result = StringCompare(f1.name, f2.name);
  253. }
  254. }
  255. return result;
  256. }
  257. int NumberCompare(int number1, int number2)
  258. {
  259. int result;
  260. if (number1 > number2)
  261. {
  262. result = 1;
  263. }
  264. else if (number1 < number2)
  265. {
  266. result = -1;
  267. }
  268. else
  269. {
  270. result = 0;
  271. }
  272. return result;
  273. }
  274. int StringCompare(string strFirstString, string secondString)
  275. {
  276. int result;
  277. if (strFirstString == null)
  278. {
  279. if (secondString == null)
  280. {
  281. result = 0;
  282. }
  283. else
  284. {
  285. result = 1;
  286. }
  287. }
  288. else
  289. {
  290. result = strFirstString.CompareTo(secondString);
  291. }
  292. return result;
  293. }
  294. }
  295. #endregion
  296. private void bt_close_Click(object sender, EventArgs e)
  297. {
  298. this.Close();
  299. }
  300. private void bt_sel_Click(object sender, EventArgs e)
  301. {
  302. foreach (Control ctrl in myTable.Controls)
  303. {
  304. try
  305. {
  306. CheckBox myBox = ctrl as CheckBox;
  307. if (myBox != null)
  308. if (myBox.Enabled)
  309. {
  310. myBox.Checked = !myBox.Checked;
  311. }
  312. }
  313. catch (Exception ex)
  314. {
  315. MessageBox.Show(ex.Message, "ERREUR");
  316. }
  317. }
  318. }
  319. }
  320. }