PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/SubtitleTools.Infrastructure/ViewModels/DownloadOpenSubtitlesViewModel.cs

#
C# | 286 lines | 232 code | 52 blank | 2 comment | 30 complexity | cccf0f94ebedd1948ac939d7b8338080 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5. using SubtitleTools.Common.Files;
  6. using SubtitleTools.Common.ISO639;
  7. using SubtitleTools.Common.Logger;
  8. using SubtitleTools.Common.MVVM;
  9. using SubtitleTools.Infrastructure.Core;
  10. using SubtitleTools.Infrastructure.Core.OpenSubtitlesOrg;
  11. using SubtitleTools.Infrastructure.Models;
  12. using SubtitleTools.Infrastructure.Core.OpenSubtitlesOrg.API;
  13. namespace SubtitleTools.Infrastructure.ViewModels
  14. {
  15. using System.Globalization;
  16. public class DownloadOpenSubtitlesViewModel : ViewModelBase
  17. {
  18. #region Fields (2)
  19. bool _downloadSelectedFilesIsBusy;
  20. OsdbItems _osdbItemsData;
  21. #endregion Fields
  22. #region Constructors (1)
  23. public DownloadOpenSubtitlesViewModel()
  24. {
  25. DownloadOpenSubtitlesGuiData = new DownloadOpenSubtitlesGui();
  26. OsdbItemsData = new OsdbItems();
  27. setupCommands();
  28. }
  29. #endregion Constructors
  30. #region Properties (7)
  31. public DelegateCommand<string> DoDebugDownloadSelectedItem { set; get; }
  32. public DelegateCommand<string> DoDownloadSelectedItem { set; get; }
  33. public DelegateCommand<string> DoDownloadSubtitle { set; get; }
  34. public DelegateCommand<string> DoSearch { set; get; }
  35. public DownloadOpenSubtitlesGui DownloadOpenSubtitlesGuiData { set; get; }
  36. public OsdbItems OsdbItemsData
  37. {
  38. set
  39. {
  40. _osdbItemsData = value;
  41. RaisePropertyChanged("OsdbItemsData");
  42. }
  43. get { return _osdbItemsData; }
  44. }
  45. public IList<Language> SubLanguages
  46. {
  47. get
  48. {
  49. var lc = new LanguagesCodes();
  50. return lc.OrderBy(o => o.LanguageName).ToList();
  51. }
  52. }
  53. #endregion Properties
  54. #region Methods (15)
  55. // Private Methods (15) 
  56. static bool canDoDebugDownloadSelectedItem(string data)
  57. {
  58. return true;
  59. }
  60. static bool canDoDownloadSelectedItem(string data)
  61. {
  62. return true;
  63. }
  64. bool canDoDownloadSubtitle(string path)
  65. {
  66. return !string.IsNullOrWhiteSpace(DownloadOpenSubtitlesGuiData.MoviePath);
  67. }
  68. bool canDoSearch(string data)
  69. {
  70. return !string.IsNullOrWhiteSpace(DownloadOpenSubtitlesGuiData.MoviePath);
  71. }
  72. void doDebugDownloadSelectedItem(string data)
  73. {
  74. if (DownloadOpenSubtitlesGuiData.SelectedOsdbItem == null || DownloadOpenSubtitlesGuiData.SelectedOsdbItem.IDSubtitleFile == 0)
  75. return;
  76. new Thread(downloadSelectedItem).Start(true);
  77. }
  78. void doDownloadSelectedItem(string data)
  79. {
  80. if (DownloadOpenSubtitlesGuiData.SelectedOsdbItem == null || DownloadOpenSubtitlesGuiData.SelectedOsdbItem.IDSubtitleFile == 0)
  81. return;
  82. new Thread(downloadSelectedItem).Start(false);
  83. }
  84. void doDownloadSubtitle(string path)
  85. {
  86. new Thread(downloadSelectedFiles).Start();
  87. }
  88. void doSearch(string data)
  89. {
  90. new Thread(searchGetSubsInfo).Start();
  91. }
  92. private void downloadSelectedFiles()
  93. {
  94. if (_downloadSelectedFilesIsBusy)
  95. return;
  96. try//it's mandatory for threading
  97. {
  98. if (string.IsNullOrWhiteSpace(DownloadOpenSubtitlesGuiData.MoviePath))
  99. return;
  100. _downloadSelectedFilesIsBusy = true;
  101. var osdb = new OpenSubtitlesXmlRpc(DownloadOpenSubtitlesGuiData.MoviePath);
  102. foreach (var sub in OsdbItemsData)
  103. {
  104. if (!sub.IsSelected) continue;
  105. var localSub = sub;
  106. osdb.DownloadSubtitle(
  107. sub.IDSubtitleFile.ToString(CultureInfo.InvariantCulture),
  108. sub.SubFileName,
  109. sub.LanguageName,
  110. e => localSub.Progress = e
  111. );
  112. }
  113. }
  114. catch (Exception ex)
  115. {
  116. ExceptionLogger.LogExceptionToFile(ex);
  117. LogWindow.AddMessage(LogType.Error, ex.Message);
  118. }
  119. finally
  120. {
  121. _downloadSelectedFilesIsBusy = false;
  122. }
  123. }
  124. private void downloadSelectedItem(object debugMode)
  125. {
  126. try//it's mandatory for threading
  127. {
  128. if (string.IsNullOrWhiteSpace(DownloadOpenSubtitlesGuiData.MoviePath))
  129. return;
  130. var osdb = new OpenSubtitlesXmlRpc(DownloadOpenSubtitlesGuiData.MoviePath);
  131. osdb.DownloadSubtitle(
  132. DownloadOpenSubtitlesGuiData.SelectedOsdbItem.IDSubtitleFile.ToString(CultureInfo.InvariantCulture),
  133. DownloadOpenSubtitlesGuiData.SelectedOsdbItem.SubFileName,
  134. DownloadOpenSubtitlesGuiData.SelectedOsdbItem.LanguageName,
  135. e => DownloadOpenSubtitlesGuiData.SelectedOsdbItem.Progress = e,
  136. (bool)debugMode
  137. );
  138. }
  139. catch (Exception ex)
  140. {
  141. ExceptionLogger.LogExceptionToFile(ex);
  142. LogWindow.AddMessage(LogType.Error, ex.Message);
  143. }
  144. }
  145. private string getIsoCode(SubtitleDataInfo item)
  146. {
  147. var lang = SubLanguages.FirstOrDefault(l => l.LanguageName == item.LanguageName);
  148. var iso6393166_1 = string.Empty;
  149. if (lang != null)
  150. {
  151. iso6393166_1 = lang.ISO6393166_1;
  152. }
  153. return iso6393166_1;
  154. }
  155. private void searchGetSubsInfo()
  156. {
  157. try //it's mandatory for threading
  158. {
  159. if (string.IsNullOrWhiteSpace(DownloadOpenSubtitlesGuiData.MoviePath))
  160. return;
  161. DownloadOpenSubtitlesGuiData.IsBusy = true;
  162. DownloadOpenSubtitlesGuiData.Progress = 0;
  163. OsdbItemsData.Clear();
  164. RaisePropertyChanged("OsdbItemsData");
  165. var osdb = new OpenSubtitlesXmlRpc(DownloadOpenSubtitlesGuiData.MoviePath);
  166. var subLanguageId = selectLang();
  167. var result = osdb.GetListOfAllSubtitles(e => DownloadOpenSubtitlesGuiData.Progress = e, subLanguageId);
  168. if (result == null || result.data == null || result.data.Length == 0)
  169. {
  170. DownloadOpenSubtitlesGuiData.IsBusy = false;
  171. return;
  172. }
  173. //sort by LanguageName then by SubAddDate
  174. var subtitles = result.data.OrderBy(o => o.LanguageName).ThenBy(o => o.SubAddDate).Distinct().ToArray();
  175. setMovieInfo(result);
  176. OsdbItemsData.Clear();
  177. foreach (var item in subtitles)
  178. {
  179. var iso6393166_1 = getIsoCode(item);
  180. OsdbItemsData.Add(new OsdbItem
  181. {
  182. IDSubtitleFile = int.Parse(item.IDSubtitleFile),
  183. LanguageName = item.LanguageName,
  184. SubAddDate = item.SubAddDate,
  185. ISO639 = item.ISO639,
  186. ISO6393166_1 = iso6393166_1,
  187. SubFileName = item.SubFileName,
  188. SubSize = Info.FormatSize(Convert.ToDouble(item.SubSize))
  189. });
  190. }
  191. RaisePropertyChanged("OsdbItemsData");
  192. }
  193. catch (Exception ex)
  194. {
  195. ExceptionLogger.LogExceptionToFile(ex);
  196. LogWindow.AddMessage(LogType.Error, ex.Message);
  197. }
  198. finally
  199. {
  200. DownloadOpenSubtitlesGuiData.IsBusy = false;
  201. DownloadOpenSubtitlesGuiData.Progress = 0;
  202. }
  203. }
  204. private string selectLang()
  205. {
  206. var subLanguageId = "all";
  207. if (DownloadOpenSubtitlesGuiData.SubLanguage != null)
  208. {
  209. subLanguageId = DownloadOpenSubtitlesGuiData.SubLanguage.LanguageName == "*All Languages*"
  210. ? "all" : DownloadOpenSubtitlesGuiData.SubLanguage.IdSubLanguage.ToLower();
  211. }
  212. return subLanguageId;
  213. }
  214. private void setMovieInfo(SubtitlesSearchResult result)
  215. {
  216. if (result != null && result.data != null && result.data.Length > 0)
  217. {
  218. DownloadOpenSubtitlesGuiData.MovieYear = result.data[0].MovieYear;
  219. DownloadOpenSubtitlesGuiData.MovieName = result.data[0].MovieName;
  220. DownloadOpenSubtitlesGuiData.ImdbRating = result.data[0].MovieImdbRating;
  221. DownloadOpenSubtitlesGuiData.OsdbUrl = string.Format("http://www.opensubtitles.org/search/sublanguageid-all/moviehash-{0}", result.data[0].MovieHash);
  222. DownloadOpenSubtitlesGuiData.ImdbUrl = string.Format("http://www.imdb.com/title/tt{0}/", Convert.ToInt32(result.data[0].IDMovieImdb).ToString("0000000"));
  223. }
  224. }
  225. private void setupCommands()
  226. {
  227. DoDownloadSubtitle = new DelegateCommand<string>(doDownloadSubtitle, canDoDownloadSubtitle);
  228. DoSearch = new DelegateCommand<string>(doSearch, canDoSearch);
  229. DoDownloadSelectedItem = new DelegateCommand<string>(doDownloadSelectedItem, canDoDownloadSelectedItem);
  230. DoDebugDownloadSelectedItem = new DelegateCommand<string>(doDebugDownloadSelectedItem, canDoDebugDownloadSelectedItem);
  231. }
  232. #endregion Methods
  233. }
  234. }