PageRenderTime 249ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/Backup/MainWindow.cs

https://github.com/joslinm/CSL
C# | 352 lines | 304 code | 34 blank | 14 comment | 42 complexity | f614751124e82e30106fd44257f11dcd MD5 | raw file
  1. using System;
  2. using System.ComponentModel;
  3. using System.Windows.Forms;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Data;
  7. using System.Threading;
  8. using System.Collections.Generic;
  9. namespace CSL_Test__1
  10. {
  11. public partial class MainWindow : Form
  12. {
  13. #region Local Variables
  14. TorrentBuilder tb = new TorrentBuilder();
  15. OptionsWindow ow = new OptionsWindow();
  16. BWHandler bw;
  17. static System.Timers.Timer timer = new System.Timers.Timer();
  18. static System.Timers.Timer internaltimer = new System.Timers.Timer();
  19. DataGridViewHandler dgvh;
  20. List<FileInfo> items;
  21. string[] files;
  22. string[] torrents;
  23. string[] zips;
  24. delegate void ProcessTorrentsInvoker();
  25. delegate void RefreshInvoker();
  26. private Object lockingobject = new Object();
  27. #endregion
  28. public MainWindow()
  29. {
  30. InitializeComponent();
  31. TorrentXMLHandler.Initialize(); //Initialize data source (read XML file)
  32. dgvh = new DataGridViewHandler(dataGridView);
  33. bw = new BWHandler(dataGridViewProgressBar, StatusLabel);
  34. internaltimer.Interval = 2;
  35. internaltimer.Elapsed += new System.Timers.ElapsedEventHandler(internaltimer_Elapsed);
  36. timer.Interval = (double)SettingsHandler.GetAutoHandleTime();
  37. timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
  38. if (SettingsHandler.GetAutoHandleBool())
  39. timer.Start();
  40. Arrow.Visible = false;
  41. StatusLabel.Visible = false;
  42. ArrowText.Visible = false;
  43. notifyIcon.Visible = false;
  44. try
  45. {
  46. if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
  47. {
  48. System.Deployment.Application.ApplicationDeployment ad =
  49. System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
  50. string version = ad.CurrentVersion.ToString();
  51. if (SettingsHandler.GetCurrentVersion().CompareTo(version) < 0)
  52. {
  53. UpdatedInformationWindow uw = new UpdatedInformationWindow();
  54. uw.ShowDialog();
  55. SettingsHandler.SetCurrentVersion(version);
  56. }
  57. }
  58. }
  59. catch (Exception) { }
  60. }
  61. #region General
  62. /*GENERAL*/
  63. private void RefreshData()
  64. {
  65. Thread.Sleep(100);
  66. dataGridViewProgressBar.Visible = false;
  67. StatusLabel.Visible = false;
  68. ProcessTorrentsButton.Enabled = true;
  69. RefreshButton.Enabled = true;
  70. DeleteButton.Enabled = true;
  71. dgvh.ResumeLayout();
  72. if (timer.Enabled == false && SettingsHandler.GetAutoHandleBool())
  73. timer.Start();
  74. try
  75. {
  76. dgvh = new DataGridViewHandler(dataGridView);
  77. }
  78. catch { }
  79. }
  80. private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
  81. {
  82. timer.Stop();
  83. timer.Close();
  84. TorrentXMLHandler.SaveAndClose();
  85. this.Dispose();
  86. }
  87. private void MainWindow_Resize(object sender, EventArgs e)
  88. {
  89. if (this.WindowState == FormWindowState.Minimized)
  90. {
  91. if (SettingsHandler.GetMinimizeToTray())
  92. {
  93. Hide();
  94. notifyIcon.Visible = true;
  95. }
  96. }
  97. //DATAGRIDVIEW
  98. dataGridView.Location = new System.Drawing.Point(0, 187);
  99. dataGridView.Size = new System.Drawing.Size(this.Width - 15, this.Height - 225);
  100. dataGridView.ScrollBars = ScrollBars.Both;
  101. //DELETE BUTTON
  102. DeleteButton.Location = new System.Drawing.Point(this.Width - 105, 113);
  103. //uTORRENT SEND BUTTON
  104. uTorrentSendIndividualButton.Location = new System.Drawing.Point(this.Width - 145, 150);
  105. //REFRESH BUTTON
  106. RefreshButton.Location = new System.Drawing.Point(0, 151);
  107. }
  108. private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
  109. {
  110. Show();
  111. WindowState = FormWindowState.Normal;
  112. notifyIcon.Visible = false;
  113. }
  114. #endregion
  115. #region Buttons
  116. /*<BUTTONS>*/
  117. //Click
  118. private void DeleteButton_Click(object sender, EventArgs e)
  119. {
  120. if (!dgvh.IsBusy)
  121. {
  122. dgvh.ProgressChanged += new ProgressChangedEventHandler(DeleteWorkerProgressChange);
  123. dgvh.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DeleteWorkerCompleted);
  124. try { dgvh.SuspendLayout(); }
  125. catch { }
  126. dataGridViewProgressBar.Visible = true;
  127. StatusLabel.Visible = true;
  128. dgvh.RunWorkerAsync("Delete");
  129. }
  130. }
  131. private void RefreshButton_Click(object sender, EventArgs e)
  132. {
  133. dgvh.Refresh();
  134. }
  135. private void ProcessTorrentsButton_Click(object sender, EventArgs e)
  136. {
  137. items = new List<FileInfo>();
  138. torrents = DirectoryHandler.GetTorrents();
  139. zips = DirectoryHandler.GetTorrentZips();
  140. if (torrents != null || zips != null)
  141. {
  142. if (dgvh.dv != null)
  143. dgvh.SuspendLayout();
  144. ProcessTorrentsButton.Enabled = false;
  145. RefreshButton.Enabled = false;
  146. DeleteButton.Enabled = false;
  147. internaltimer.Start(); //Periodically check to see if BW threads finished
  148. timer.Stop();
  149. if (torrents != null)
  150. {
  151. foreach (string torrent in torrents)
  152. {
  153. try
  154. {
  155. items.Add(new FileInfo(torrent));
  156. }
  157. catch (Exception ee)
  158. {
  159. DirectoryHandler.LogError(ee.Message + "\n" + ee.StackTrace);
  160. }
  161. }
  162. }
  163. if (zips != null)
  164. {
  165. bw.Unzip(zips);
  166. }
  167. bw.Process(items);
  168. }
  169. else
  170. {
  171. ProcessTorrentsButton.Enabled = true;
  172. }
  173. }
  174. private void uTorrentSendIndividualButton_Click(object sender, EventArgs e)
  175. {
  176. dgvh.SendIndividualTorrent();
  177. }
  178. private void uTorrentSendAllButton_Click(object sender, EventArgs e)
  179. {
  180. uTorrentHandler.SendAllTorrents();
  181. dgvh.dv.Update();
  182. }
  183. private void PerformClickProcessTorrents()
  184. {
  185. ProcessTorrentsButton.PerformClick();
  186. }
  187. private void PerformClickRefreshButton()
  188. {
  189. RefreshButton.PerformClick();
  190. }
  191. private void ShowProcessTorrentsButton()
  192. {
  193. ProcessTorrentsButton.Visible = true;
  194. }
  195. //Hover
  196. private void DeleteButton_MouseEnter(object sender, EventArgs e)
  197. {
  198. RefreshButton.Visible = false;
  199. Arrow.Visible = true;
  200. ArrowText.Visible = true;
  201. }
  202. private void DeleteButton_MouseLeave(object sender, EventArgs e)
  203. {
  204. RefreshButton.Visible = true;
  205. ArrowText.Visible = false;
  206. Arrow.Visible = false;
  207. }
  208. #endregion
  209. #region Tool Strip
  210. /*TOOL STRIP*/
  211. private void optionsToolStripMenuItem1_Click(object sender, EventArgs e)
  212. {
  213. }
  214. private void optionsToolStripMenuItem2_Click(object sender, EventArgs e)
  215. {
  216. ow.Show();
  217. }
  218. #endregion
  219. #region DataGridView
  220. /*DataGridView*/
  221. private void dataGridView_DragDrop(object sender, DragEventArgs e)
  222. {
  223. items = new List<FileInfo>();
  224. files = (string[])e.Data.GetData(DataFormats.FileDrop);
  225. dataGridViewProgressBar.Visible = true;
  226. StatusLabel.Visible = true;
  227. foreach (string file in files)
  228. {
  229. string extension = Path.GetExtension(file);
  230. if (Path.GetExtension(file).Equals(".zip") || Path.GetExtension(file).Equals(".rar"))
  231. {
  232. foreach (string unzipped in DirectoryHandler.UnzipFile(file))
  233. items.Add(new FileInfo(unzipped));
  234. }
  235. else if (Path.GetExtension(file).Equals(".torrent"))
  236. {
  237. items.Add(new FileInfo(file));
  238. }
  239. }
  240. tb.RunWorkerAsync(items);
  241. }
  242. private void dataGridView_DragEnter(object sender, DragEventArgs e)
  243. {
  244. // make sure they're actually dropping files
  245. if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
  246. // allow them to continue
  247. // (without this, the cursor stays a "NO" symbol)
  248. e.Effect = DragDropEffects.All;
  249. }
  250. private void dataGridView_KeyUp(object sender, KeyEventArgs e)
  251. {
  252. if (e.KeyData.Equals(Keys.Delete))
  253. DeleteButton_Click(sender, e);
  254. }
  255. #endregion
  256. #region Timer
  257. /*TIMER*/
  258. public static void UpdateTimer(bool active, decimal time)
  259. {
  260. if (active)
  261. {
  262. if (timer == null)
  263. timer = new System.Timers.Timer();
  264. timer.Interval = (double)time;
  265. if (!timer.Enabled)
  266. timer.Start();
  267. }
  268. else
  269. {
  270. timer.Stop();
  271. timer.Close();
  272. }
  273. }
  274. void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  275. {
  276. if (ProcessTorrentsButton.InvokeRequired && !BWHandler.Busy)
  277. this.Invoke(new ProcessTorrentsInvoker(PerformClickProcessTorrents));
  278. else if (!BWHandler.Busy)
  279. ProcessTorrentsButton.PerformClick();
  280. }
  281. void internaltimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  282. {
  283. if (!BWHandler.Busy)
  284. {
  285. if (this.InvokeRequired)
  286. this.Invoke(new RefreshInvoker(RefreshData));
  287. else
  288. RefreshData();
  289. if (ProcessTorrentsButton.InvokeRequired)
  290. this.Invoke(new ProcessTorrentsInvoker(ShowProcessTorrentsButton));
  291. else
  292. ProcessTorrentsButton.Visible = true;
  293. internaltimer.Stop();
  294. }
  295. }
  296. #endregion
  297. #region BackgroundWorker
  298. void DeleteWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  299. {
  300. dgvh.ResumeLayout();
  301. dataGridViewProgressBar.Visible = false;
  302. StatusLabel.Visible = false;
  303. RefreshButton.Enabled = true;
  304. }
  305. void DeleteWorkerProgressChange(object sender, ProgressChangedEventArgs e)
  306. {
  307. int progress = 0;
  308. if (!(e.ProgressPercentage > 100 || e.ProgressPercentage < 0))
  309. progress = e.ProgressPercentage;
  310. if (progress % 10 == 0)
  311. StatusLabel.Text = "Deleting " + 10 * (progress / 10) + "%";
  312. else
  313. StatusLabel.Text += ".";
  314. dataGridViewProgressBar.Value = progress;
  315. }
  316. #endregion
  317. }
  318. }