SearchItem.cs 9.3 KB

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