/FreeboxHDVideoPlayer/MainForm.cs

http://freeboxhdvideoplayer.codeplex.com · C# · 258 lines · 234 code · 19 blank · 5 comment · 29 complexity · 98d5d919cf6a95ccb652956e9be275d2 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Diagnostics;
  10. using System.IO;
  11. using EnterpriseDT.Net.Ftp;
  12. using ComponentFactory.Krypton.Toolkit;
  13. namespace FreeboxHDVideoPlayer
  14. {
  15. /// <summary>
  16. /// + temps restant + vitesse + available locally or not
  17. /// - worker pour checker/download des videos
  18. /// </summary>
  19. public partial class MainForm : KryptonForm
  20. {
  21. private KryptonManager kryptonManager;
  22. private const long hddSize = 42949672960;
  23. private ListVideoWorker listVideoWorker;
  24. private DeleteVideoWorker deleteVideoWorker;
  25. private PlayVideoWorker playFTPVideoWorker;
  26. private DownloadVideosWorker downloadVideosWorker;
  27. private bool ShowProgress
  28. {
  29. set
  30. {
  31. if (value)
  32. {
  33. this.loadingCircle.Active = true;
  34. this.loadingCircle.Visible = true;
  35. }
  36. else
  37. {
  38. this.loadingCircle.Active = false;
  39. this.loadingCircle.Visible = false;
  40. }
  41. this.labelProgress.Text = "";
  42. }
  43. }
  44. public MainForm()
  45. {
  46. InitializeComponent();
  47. this.components = new System.ComponentModel.Container();
  48. this.kryptonManager = new KryptonManager(this.components);
  49. kryptonManager.GlobalPaletteMode = PaletteModeManager.Office2010Black;
  50. this.ShowProgress = false;
  51. string host = "hd1.freebox.fr";
  52. int port = 21;
  53. string user = "freebox";
  54. string password = "";
  55. listVideoWorker = new ListVideoWorker(host, port, user, password);
  56. listVideoWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
  57. listVideoWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(listVideoWorker_RunWorkerCompleted);
  58. deleteVideoWorker = new DeleteVideoWorker(host, port, user, password);
  59. deleteVideoWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
  60. deleteVideoWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(deleteVideoWorker_RunWorkerCompleted);
  61. playFTPVideoWorker = new PlayVideoWorker(host, port, user, password);
  62. playFTPVideoWorker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
  63. playFTPVideoWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(playFTPVideoWorker_RunWorkerCompleted);
  64. downloadVideosWorker = new DownloadVideosWorker(host, port, user, password);
  65. downloadVideosWorker.ProgressChanged += new ProgressChangedEventHandler(downloadVideosWorker_ProgressChanged);
  66. downloadVideosWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(downloadVideosWorker_RunWorkerCompleted);
  67. VideoLocalCache.Init();
  68. }
  69. void downloadVideosWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  70. {
  71. if (e.Error != null)
  72. {
  73. MessageBox.Show(this, "Erreur: " + e.Error, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
  74. }
  75. EnableButtons = true;
  76. }
  77. void downloadVideosWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  78. {
  79. DownloadVideosWorker.DownloadProgress progress = e.UserState as DownloadVideosWorker.DownloadProgress;
  80. ListViewItem item = this.listView1.Items[progress.FileName];
  81. item.SubItems[1].Text = progress.Message;
  82. }
  83. void playFTPVideoWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  84. {
  85. if (e.Error != null)
  86. {
  87. MessageBox.Show(this, "Erreur: " + e.Error, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
  88. }
  89. ShowProgress = false;
  90. }
  91. void deleteVideoWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  92. {
  93. if (e.Error != null)
  94. {
  95. MessageBox.Show(this, "Erreur: " + e.Error, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
  96. }
  97. else
  98. {
  99. if (!listVideoWorker.IsBusy)
  100. {
  101. listVideoWorker.RunWorkerAsync();
  102. }
  103. }
  104. ShowProgress = false;
  105. }
  106. void listVideoWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  107. {
  108. if (e.Error != null)
  109. {
  110. MessageBox.Show(this, "Erreur: " + e.Error, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
  111. }
  112. else
  113. {
  114. ListVideoWorker.Result result = (ListVideoWorker.Result) e.Result;
  115. if (result.Size > 0)
  116. {
  117. long allocSizeGo = result.Size;
  118. long allocPercentage = (allocSizeGo * 100) / hddSize;
  119. this.toolStripProgressBarHDD.Value = (int) allocPercentage;
  120. this.toolStripLabelHDD.Text = BytesFormatter.Format(allocSizeGo)
  121. + " / " + BytesFormatter.Format(hddSize);
  122. }
  123. this.listView1.BeginUpdate();
  124. this.listView1.Items.Clear();
  125. List<Video> videosToDownload = new List<Video>();
  126. foreach (Video video in result.Videos)
  127. {
  128. string status = "Disponible à distance";
  129. bool download = true;
  130. if (VideoLocalCache.Exists(video.Name))
  131. {
  132. if (VideoLocalCache.Size(video.Name) == video.Size)
  133. {
  134. status = "Disponible en local";
  135. download = false;
  136. }
  137. else
  138. {
  139. VideoLocalCache.Delete(video.Name);
  140. }
  141. }
  142. if (download)
  143. {
  144. videosToDownload.Add(video);
  145. }
  146. ListViewItem item = new ListViewItem(new string[]{video.Name, status});
  147. item.Name = video.Name;
  148. this.listView1.Items.Add(item);
  149. }
  150. this.listView1.EndUpdate();
  151. if (!this.downloadVideosWorker.IsBusy)
  152. {
  153. //videosToDownload.Clear();
  154. this.downloadVideosWorker.RunWorkerAsync(new DownloadVideosWorker.Arg(videosToDownload));
  155. }
  156. }
  157. this.ShowProgress = false;
  158. }
  159. void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  160. {
  161. this.ShowProgress = true;
  162. this.labelProgress.Text = e.UserState.ToString();
  163. }
  164. private void Form1_Load(object sender, EventArgs e)
  165. {
  166. toolStripButtonRefresh_Click(this, EventArgs.Empty);
  167. }
  168. private void buttonQuit_Click(object sender, EventArgs e)
  169. {
  170. this.Close();
  171. }
  172. private void toolStripButton1_Click(object sender, EventArgs e)
  173. {
  174. if (this.SelectedVideoItem != null)
  175. {
  176. if (!this.playFTPVideoWorker.IsBusy)
  177. {
  178. this.playFTPVideoWorker.Video = this.SelectedVideoName;
  179. this.playFTPVideoWorker.RunWorkerAsync();
  180. }
  181. }
  182. }
  183. private ListViewItem SelectedVideoItem
  184. {
  185. get
  186. {
  187. ListViewItem item = null;
  188. if (this.listView1.SelectedItems.Count == 1)
  189. {
  190. item = this.listView1.SelectedItems[0];
  191. }
  192. return item;
  193. }
  194. }
  195. private string SelectedVideoName
  196. {
  197. get
  198. {
  199. string name = null;
  200. if (this.SelectedVideoItem != null)
  201. {
  202. name = this.SelectedVideoItem.Text;
  203. }
  204. return name;
  205. }
  206. }
  207. private void toolStripButtonDelete_Click(object sender, EventArgs e)
  208. {
  209. if (this.SelectedVideoItem != null)
  210. {
  211. string video = this.SelectedVideoName;
  212. if (DialogResult.Yes == MessageBox.Show(this, "Voulez-vous supprimer " + video + " ?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
  213. {
  214. if (!this.deleteVideoWorker.IsBusy)
  215. {
  216. this.deleteVideoWorker.Video = video;
  217. this.deleteVideoWorker.RunWorkerAsync();
  218. }
  219. }
  220. }
  221. }
  222. bool EnableButtons
  223. {
  224. set
  225. {
  226. this.toolStripButtonDelete.Enabled = value;
  227. this.toolStripButtonRefresh.Enabled = value;
  228. }
  229. }
  230. private void toolStripButtonRefresh_Click(object sender, EventArgs e)
  231. {
  232. if (!listVideoWorker.IsBusy)
  233. {
  234. EnableButtons = false;
  235. listVideoWorker.RunWorkerAsync();
  236. }
  237. }
  238. }
  239. }