SearchItem.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. namespace AVSORTER
  7. {
  8. public class SearchItem
  9. {
  10. #region 构造函数 public FileItem(string codeString,IGetable getor)
  11. public SearchItem(string codeString,IGetable getor)
  12. {
  13. this._queryString = codeString;
  14. this.Getor = getor;
  15. }
  16. #endregion
  17. #region 属性 查询字符串public string SeedString
  18. private string _queryString;
  19. /// <summary>
  20. /// 查询的字符串
  21. /// </summary>
  22. public string SeedString
  23. {
  24. get { return _queryString; }
  25. }
  26. #endregion
  27. #region 属性 影片信息 public Movie MovieDetail
  28. private Movie _mv;
  29. public Movie MovieDetail
  30. {
  31. get { return _mv; }
  32. }
  33. #endregion
  34. /// <summary>
  35. /// 结果列表
  36. /// </summary>
  37. public List<MovieBasic> MovieBasicList { get; set; }
  38. private QStatus _status = QStatus.未开始;
  39. public QStatus Status
  40. {
  41. get { return _status; }
  42. set { _status = value; }
  43. }
  44. public object Tag { get; set; }
  45. /// <summary>
  46. /// 设定选择结果
  47. /// </summary>
  48. /// <param name="index"></param>
  49. public void Select(int index)
  50. {
  51. this.chooseIndex = index;
  52. _startGetMovie();
  53. }
  54. public bool IsSelected
  55. {
  56. get
  57. {
  58. if (this.chooseIndex==-1)
  59. {
  60. return false;
  61. }
  62. else
  63. {
  64. return true;
  65. }
  66. }
  67. }
  68. /// <summary>
  69. /// 设定选择结果
  70. /// </summary>
  71. /// <param name="basic"></param>
  72. public void Select(MovieBasic basic)
  73. {
  74. this.chooseIndex = MovieBasicList.IndexOf(basic);
  75. _startGetMovie();
  76. }
  77. public MovieBasic SelectedMovieBasic
  78. {
  79. get
  80. {
  81. if (this.IsSelected)
  82. {
  83. return this.MovieBasicList[chooseIndex];
  84. }
  85. else
  86. {
  87. return null;
  88. }
  89. }
  90. }
  91. private void _startGetMovie()
  92. {
  93. try
  94. {
  95. if (this.chooseIndex != -1)
  96. {
  97. Func<MovieBasic, Movie> fuc = new Func<MovieBasic, Movie>(Getor.GetMovie);
  98. fuc.BeginInvoke(this.MovieBasicList[this.chooseIndex], _getMovieCallback, fuc);
  99. RaiseStatusChangeEvent(QStatus.查询完成一个结果, QStatus.获取信息中, "");
  100. }
  101. else
  102. {
  103. this._status = QStatus.出错;
  104. throw new Exception("还没有选定影片");
  105. }
  106. }
  107. catch (Exception ee)
  108. {
  109. throw ee;
  110. }
  111. }
  112. private void _getMovieCallback(IAsyncResult res)
  113. {
  114. try
  115. {
  116. this._mv = (res.AsyncState as Func<MovieBasic, Movie>).EndInvoke(res);
  117. this._status = QStatus.获取信息完成;
  118. if (IsDownloadCover)
  119. {
  120. RaiseStatusChangeEvent(QStatus.获取信息中, QStatus.获取信息完成, "已经获取影片信息!");
  121. this._status = QStatus.下载封面中;
  122. RaiseStatusChangeEvent(QStatus.获取信息完成, QStatus.下载封面中, "已经获取影片信息!");
  123. _startGetCover();
  124. }
  125. else
  126. {
  127. this._status = QStatus.准备好移动文件;
  128. RaiseStatusChangeEvent(QStatus.获取信息中, QStatus.准备好移动文件, "已经获取影片信息,不要求获取封面!");
  129. }
  130. }
  131. catch (Exception)
  132. {
  133. RaiseStatusChangeEvent(QStatus.获取信息中, QStatus.出错, "获取影片信息时出错!");
  134. }
  135. }
  136. private void _startGetCover()
  137. {
  138. if (IsDownloadCover)
  139. {
  140. Func<Movie, bool> fuc = new Func<Movie, bool>(Getor.GetCover);
  141. fuc.BeginInvoke(this._mv, _getCoverCallBack, fuc);
  142. }
  143. }
  144. private void _getCoverCallBack(IAsyncResult res)
  145. {
  146. try
  147. {
  148. var ress = (res.AsyncState as Func<Movie, bool>).EndInvoke(res);
  149. this._status = QStatus.准备好移动文件;
  150. RaiseStatusChangeEvent(QStatus.下载封面中, QStatus.准备好移动文件, "封面下载完毕");
  151. }
  152. catch (Exception)
  153. {
  154. RaiseStatusChangeEvent(QStatus.下载封面中, QStatus.出错, "下载封面出错");
  155. }
  156. }
  157. virtual protected void RaiseStatusChangeEvent(QStatus b,QStatus aft,string msg)
  158. {
  159. this._status = aft;
  160. if (OnStatusChange!=null)
  161. {
  162. OnStatusChange(this,new StatusChangeEventArgs(b,aft,msg));
  163. }
  164. }
  165. public event StatusChangeEvent OnStatusChange;
  166. private int chooseIndex = -1;
  167. public IGetable Getor
  168. {
  169. get;
  170. set;
  171. }
  172. public void StartQuery()
  173. {
  174. if (this.Getor!=null)
  175. {
  176. //this.MovieBasicList = Getor.Query(SeedString);
  177. Func<string, List<MovieBasic>> fuc = new Func<string, List<MovieBasic>>(Getor.Query);
  178. fuc.BeginInvoke(SeedString, queryFinishCallBack, fuc);
  179. RaiseStatusChangeEvent(QStatus.未开始, QStatus.查询中, "");
  180. }
  181. else
  182. {
  183. RaiseStatusChangeEvent(QStatus.查询中, QStatus.出错, "查询影片出错");
  184. }
  185. }
  186. private void queryFinishCallBack(IAsyncResult obj)
  187. {
  188. try
  189. {
  190. this.MovieBasicList = (obj.AsyncState as Func<string, List<MovieBasic>>).EndInvoke(obj);
  191. if (this.MovieBasicList.Count > 1)
  192. {
  193. RaiseStatusChangeEvent(QStatus.查询中, QStatus.多个结果请选择一个, "");
  194. }
  195. else if (this.MovieBasicList.Count == 1)
  196. {
  197. RaiseStatusChangeEvent(QStatus.查询中, QStatus.查询完成一个结果, "");
  198. if (this.IsAutoSelect)
  199. {
  200. RaiseStatusChangeEvent(QStatus.查询完成一个结果, QStatus.获取信息中, "");
  201. Select(0);
  202. }
  203. }
  204. else
  205. {
  206. RaiseStatusChangeEvent(QStatus.查询中, QStatus.查询完成无匹配, "");
  207. }
  208. }
  209. catch (Exception)
  210. {
  211. RaiseStatusChangeEvent(QStatus.查询中, QStatus.出错, "查询出错");
  212. this.MovieBasicList = null;
  213. this.chooseIndex = -1;
  214. }
  215. finally
  216. {
  217. RaiseStatusChangeEvent(QStatus.准备好移动文件, QStatus.准备好移动文件, "Release1");
  218. }
  219. }
  220. public class StatusChangeEventArgs : EventArgs
  221. {
  222. public QStatus Before { get; set; }
  223. public QStatus After { get; set; }
  224. public string Message { get; set; }
  225. public StatusChangeEventArgs(QStatus _before, QStatus _after,string _msg)
  226. {
  227. this.Before = _before;
  228. this.After = _after;
  229. this.Message = _msg;
  230. }
  231. }
  232. public delegate void StatusChangeEvent(object sender, StatusChangeEventArgs e);
  233. /// <summary>
  234. /// 是否自动下载封面
  235. /// </summary>
  236. public bool IsDownloadCover { get; set; }
  237. /// <summary>
  238. /// 当查询结果只有一个时候,是否自动下载详细信息。
  239. /// </summary>
  240. public bool IsAutoSelect { get; set; }
  241. }
  242. }