SearchItem.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. if (OnAboutToLoadImage == null)
  94. {
  95. throw new Exception("必须注册这个事件");
  96. }
  97. else
  98. {
  99. OnAboutToLoadImage(this, new EventArgs());
  100. }
  101. try
  102. {
  103. if (this.chooseIndex != -1)
  104. {
  105. Func<MovieBasic, Movie> fuc = new Func<MovieBasic, Movie>(Getor.GetMovie);
  106. fuc.BeginInvoke(this.MovieBasicList[this.chooseIndex], _getMovieCallback, fuc);
  107. RaiseStatusChangeEvent(QStatus.查询完成一个结果, QStatus.获取信息中, "");
  108. }
  109. else
  110. {
  111. this._status = QStatus.出错;
  112. throw new Exception("还没有选定影片");
  113. }
  114. }
  115. catch (Exception ee)
  116. {
  117. throw ee;
  118. }
  119. }
  120. private void _getMovieCallback(IAsyncResult res)
  121. {
  122. try
  123. {
  124. this._mv = (res.AsyncState as Func<MovieBasic, Movie>).EndInvoke(res);
  125. this._status = QStatus.获取信息完成;
  126. if (IsDownloadCover)
  127. {
  128. RaiseStatusChangeEvent(QStatus.获取信息中, QStatus.获取信息完成, "已经获取影片信息!");
  129. this._status = QStatus.下载封面中;
  130. RaiseStatusChangeEvent(QStatus.获取信息完成, QStatus.下载封面中, "已经获取影片信息!");
  131. _startGetCover();
  132. }
  133. else
  134. {
  135. this._status = QStatus.准备好移动文件;
  136. RaiseStatusChangeEvent(QStatus.获取信息中, QStatus.准备好移动文件, "已经获取影片信息,不要求获取封面!");
  137. }
  138. }
  139. catch (Exception)
  140. {
  141. RaiseStatusChangeEvent(QStatus.获取信息中, QStatus.出错, "获取影片信息时出错!");
  142. }
  143. finally
  144. {
  145. if (OnCompletedLoadImage == null)
  146. {
  147. throw new Exception("必须注册这个事件");
  148. }
  149. else
  150. {
  151. OnCompletedLoadImage(this, new EventArgs());
  152. }
  153. }
  154. }
  155. private void _startGetCover()
  156. {
  157. if (IsDownloadCover)
  158. {
  159. Func<Movie, bool> fuc = new Func<Movie, bool>(Getor.GetCover);
  160. fuc.BeginInvoke(this._mv, _getCoverCallBack, fuc);
  161. }
  162. }
  163. private void _getCoverCallBack(IAsyncResult res)
  164. {
  165. try
  166. {
  167. var ress = (res.AsyncState as Func<Movie, bool>).EndInvoke(res);
  168. this._status = QStatus.准备好移动文件;
  169. RaiseStatusChangeEvent(QStatus.下载封面中, QStatus.准备好移动文件, "封面下载完毕");
  170. }
  171. catch (Exception)
  172. {
  173. RaiseStatusChangeEvent(QStatus.下载封面中, QStatus.出错, "下载封面出错");
  174. }
  175. }
  176. virtual protected void RaiseStatusChangeEvent(QStatus b,QStatus aft,string msg)
  177. {
  178. this._status = aft;
  179. if (OnStatusChange!=null)
  180. {
  181. OnStatusChange(this,new StatusChangeEventArgs(b,aft,msg));
  182. }
  183. }
  184. public event StatusChangeEvent OnStatusChange;
  185. private int chooseIndex = -1;
  186. public IGetable Getor
  187. {
  188. get;
  189. set;
  190. }
  191. public void StartQuery()
  192. {
  193. if (OnAboutToLoadInfo==null)
  194. {
  195. throw new Exception("必须注册这个事件");
  196. }
  197. else
  198. {
  199. OnAboutToLoadInfo(this, new EventArgs());
  200. }
  201. if (this.Getor!=null)
  202. {
  203. //this.MovieBasicList = Getor.Query(SeedString);
  204. Func<string, List<MovieBasic>> fuc = new Func<string, List<MovieBasic>>(Getor.Query);
  205. fuc.BeginInvoke(SeedString, queryFinishCallBack, fuc);
  206. RaiseStatusChangeEvent(QStatus.未开始, QStatus.查询中, "");
  207. }
  208. else
  209. {
  210. RaiseStatusChangeEvent(QStatus.查询中, QStatus.出错, "查询影片出错");
  211. }
  212. }
  213. private void queryFinishCallBack(IAsyncResult obj)
  214. {
  215. try
  216. {
  217. this.MovieBasicList = (obj.AsyncState as Func<string, List<MovieBasic>>).EndInvoke(obj);
  218. if (this.MovieBasicList.Count > 1)
  219. {
  220. RaiseStatusChangeEvent(QStatus.查询中, QStatus.多个结果请选择一个, "");
  221. }
  222. else if (this.MovieBasicList.Count == 1)
  223. {
  224. RaiseStatusChangeEvent(QStatus.查询中, QStatus.查询完成一个结果, "");
  225. if (this.IsAutoSelect)
  226. {
  227. RaiseStatusChangeEvent(QStatus.查询完成一个结果, QStatus.获取信息中, "");
  228. Select(0);
  229. }
  230. }
  231. else
  232. {
  233. RaiseStatusChangeEvent(QStatus.查询中, QStatus.查询完成无匹配, "");
  234. }
  235. }
  236. catch (Exception)
  237. {
  238. RaiseStatusChangeEvent(QStatus.查询中, QStatus.出错, "查询出错");
  239. this.MovieBasicList = null;
  240. this.chooseIndex = -1;
  241. }
  242. finally
  243. {
  244. if (OnCompletedLoadInfo == null)
  245. {
  246. throw new Exception("必须注册这个事件");
  247. }
  248. else
  249. {
  250. OnCompletedLoadInfo(this, new EventArgs());
  251. }
  252. }
  253. }
  254. public class StatusChangeEventArgs : EventArgs
  255. {
  256. public QStatus Before { get; set; }
  257. public QStatus After { get; set; }
  258. public string Message { get; set; }
  259. public StatusChangeEventArgs(QStatus _before, QStatus _after,string _msg)
  260. {
  261. this.Before = _before;
  262. this.After = _after;
  263. this.Message = _msg;
  264. }
  265. }
  266. public delegate void StatusChangeEvent(object sender, StatusChangeEventArgs e);
  267. public event EventHandler OnAboutToLoadInfo;
  268. public event EventHandler OnCompletedLoadInfo;
  269. public event EventHandler OnAboutToLoadImage;
  270. public event EventHandler OnCompletedLoadImage;
  271. /// <summary>
  272. /// 是否自动下载封面
  273. /// </summary>
  274. public bool IsDownloadCover { get; set; }
  275. /// <summary>
  276. /// 当查询结果只有一个时候,是否自动下载详细信息。
  277. /// </summary>
  278. public bool IsAutoSelect { get; set; }
  279. }
  280. }