PageRenderTime 38ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/MyVideoViewer/MyVideoViewerLibrary/MyMoviesXML/MyMoviesXMLBusiness.cs

#
C# | 302 lines | 266 code | 36 blank | 0 comment | 86 complexity | 1fe215a70ee0c4d6a550a6489e1648ac MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. namespace MyVideoViewerLibrary.MyMoviesXML
  7. {
  8. public class MyMoviesXMLBusiness : IMoviesBusiness
  9. {
  10. string[] extensions = new string[] { ".mov", ".avi", ".mpeg", ".mpg", ".mkv", ".wmv", ".mp4", ".dvr-ms", ".m2ts", ".ts", ".divx" };
  11. private List<Movie> movies;
  12. private List<Genre> genres;
  13. private List<Studio> studios;
  14. public List<Generic> categories;
  15. public List<Generic> mpaaRatings;
  16. public MyMoviesXMLBusiness()
  17. {
  18. genres = new List<Genre>();
  19. movies = new List<Movie>();
  20. studios = new List<Studio>();
  21. categories = new List<Generic>();
  22. mpaaRatings = new List<Generic>();
  23. LoadMovies();
  24. }
  25. public void LoadMovies()
  26. {
  27. if (!File.Exists(Settings.Default.MyMoviesXmlFile))
  28. throw new Exception("File does not exist.");
  29. XDocument doc = XDocument.Load(Settings.Default.MyMoviesXmlFile);
  30. XElement root = doc.Element("Titles");
  31. if (root == null)
  32. return;
  33. int tempInt;
  34. DateTime tempDate;
  35. foreach (XElement xmovie in root.Elements("Title"))
  36. {
  37. Movie movie = new Movie()
  38. {
  39. Title = xmovie.Element("OriginalTitle") != null ? xmovie.Element("OriginalTitle").Value : "",
  40. SortTitle = xmovie.Element("SortTitle") != null ? xmovie.Element("SortTitle").Value : "",
  41. Description = xmovie.Element("Description") != null ? xmovie.Element("Description").Value : "",
  42. IMDBId = xmovie.Element("IMDB") != null ? xmovie.Element("IMDB").Value : "",
  43. };
  44. if (xmovie.Element("RunningTime") != null)
  45. {
  46. if (int.TryParse(xmovie.Element("RunningTime").Value, out tempInt))
  47. movie.Runtime = tempInt;
  48. }
  49. if (xmovie.Element("Added") != null)
  50. {
  51. if (DateTime.TryParse(xmovie.Element("Added").Value, out tempDate))
  52. movie.DateAdded = tempDate;
  53. }
  54. if (xmovie.Element("ParentalRating") != null)
  55. {
  56. XElement parentalRating = xmovie.Element("ParentalRating");
  57. if (parentalRating.Element("Value") != null && int.TryParse(parentalRating.Element("Value").Value, out tempInt))
  58. {
  59. movie.MPAARating = MoviesBusiness.GetMPAARatingText((MPAARating)tempInt);
  60. Generic mrating = mpaaRatings.SingleOrDefault(m => m.Name == movie.MPAARating);
  61. if (mrating == null)
  62. {
  63. mrating = new Generic(movie.MPAARating);
  64. mpaaRatings.Add(mrating);
  65. }
  66. mrating.Movies.Add(movie);
  67. }
  68. if (parentalRating.Element("Description") != null)
  69. movie.MPAARatingDescription = parentalRating.Element("Description").Value;
  70. }
  71. if (xmovie.Element("Added") != null)
  72. {
  73. if (DateTime.TryParse(xmovie.Element("Added").Value, out tempDate))
  74. movie.DateAdded = tempDate;
  75. }
  76. if (xmovie.Element("ReleaseDate") != null)
  77. {
  78. if (DateTime.TryParse(xmovie.Element("ReleaseDate").Value, out tempDate))
  79. movie.ReleaseDate = tempDate;
  80. }
  81. XElement localTrailers = xmovie.Element("LocalTrailer");
  82. if (localTrailers != null && localTrailers.Element("URL") != null && localTrailers.Element("Type") != null)
  83. {
  84. if (int.TryParse(localTrailers.Element("Type").Value, out tempInt))
  85. {
  86. if(tempInt == 1)
  87. movie.Trailer = localTrailers.Element("URL").Value;
  88. }
  89. }
  90. var q = xmovie.Descendants("Disc").Where(d => d.Element("LocationSideA") != null && !string.IsNullOrEmpty(d.Element("LocationSideA").Value));
  91. XElement disc1 = q.FirstOrDefault();
  92. if (disc1 != null)
  93. {
  94. DirectoryInfo folder = new DirectoryInfo(disc1.Element("LocationSideA").Value);
  95. if (folder.Exists)
  96. {
  97. movie.PosterImageLocation = Path.Combine(folder.FullName, "folder.jpg");
  98. movie.BackgroundImageLocation = Path.Combine(folder.FullName, "background.jpg");
  99. }
  100. }
  101. if (xmovie.Element("Genres") != null)
  102. {
  103. foreach (XElement xgenre in xmovie.Element("Genres").Descendants("Genre"))
  104. {
  105. if (xgenre == null || string.IsNullOrEmpty(xgenre.Value))
  106. continue;
  107. Genre genre = genres.SingleOrDefault(g => g.GenreDesc == xgenre.Value);
  108. if (genre == null)
  109. {
  110. genre = new Genre() { GenreDesc = xgenre.Value, Movies = new List<Movie>() };
  111. genres.Add(genre);
  112. }
  113. movie.Genres.Add(genre);
  114. genre.Movies.Add(movie);
  115. }
  116. }
  117. if (xmovie.Element("Studios") != null)
  118. {
  119. foreach (XElement xstudio in xmovie.Element("Studios").Descendants("Studio"))
  120. {
  121. if (xstudio == null || string.IsNullOrEmpty(xstudio.Value))
  122. continue;
  123. Studio studio = studios.SingleOrDefault(g => g.Name == xstudio.Value);
  124. if (studio == null)
  125. {
  126. studio = new Studio() { Name = xstudio.Value, Movies = new List<Movie>() };
  127. studios.Add(studio);
  128. }
  129. movie.Studios.Add(studio);
  130. studio.Movies.Add(movie);
  131. }
  132. }
  133. if (xmovie.Element("Categories") != null)
  134. {
  135. foreach (XElement xcat in xmovie.Element("Categories").Descendants("Category"))
  136. {
  137. if (xcat == null || string.IsNullOrEmpty(xcat.Value))
  138. continue;
  139. Generic category = categories.SingleOrDefault(g => g.Name == xcat.Value);
  140. if (category == null)
  141. {
  142. category = new Generic() { Name = xcat.Value, Movies = new List<Movie>() };
  143. categories.Add(category);
  144. }
  145. category.Movies.Add(movie);
  146. }
  147. }
  148. XElement persons = xmovie.Element("Persons");
  149. if (persons != null)
  150. {
  151. IEnumerable<XElement> directors = persons.Elements("Person").Where(p => p.Element("Type") != null && p.Element("Type").Value == "Director");
  152. if (directors != null)
  153. movie.Director = string.Join(", ", directors.Select(d => d.Element("Name").Value).ToArray());
  154. }
  155. movie.OnGetFiles += new GetFilesEvent(movie_OnGetFiles);
  156. movie.Data = xmovie;
  157. movies.Add(movie);
  158. }
  159. }
  160. void movie_OnGetFiles(Movie movie)
  161. {
  162. XElement xmovie = movie.Data as XElement;
  163. var discs = xmovie.Descendants("Disc").Where(d => d.Element("LocationSideA") != null && !string.IsNullOrEmpty(d.Element("LocationSideA").Value));
  164. foreach (XElement disc in discs)
  165. {
  166. DirectoryInfo folder = new DirectoryInfo(disc.Element("LocationSideA").Value);
  167. if (folder.Exists)
  168. {
  169. DirectoryInfo dvdFolder = folder.GetDirectories("VIDEO_TS").FirstOrDefault();
  170. if (dvdFolder != null)
  171. {
  172. movie.Files.Add(dvdFolder.FullName);
  173. }
  174. foreach (FileInfo file in folder.GetFiles().OrderBy(f => f.Name))
  175. {
  176. if (Path.GetFileNameWithoutExtension(file.Name) == "trailer" && extensions.Contains(file.Extension))
  177. {
  178. movie.Trailer = file.FullName;
  179. continue;
  180. }
  181. else if (extensions.Contains(file.Extension))
  182. {
  183. movie.Files.Add(file.FullName);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. #region IMoviesBusiness Members
  190. public List<Movie> GetMovies()
  191. {
  192. return movies;
  193. }
  194. public List<CommandItem> GetFilterItems()
  195. {
  196. List<CommandItem> filters = new List<CommandItem>();
  197. filters.Add(new CommandItem("Title", (int)FilterType.Title));
  198. filters.Add(new CommandItem("Last Added", (int)FilterType.LastAdded));
  199. filters.Add(new CommandItem("MPAA Rating", (int)FilterType.MPAARating));
  200. filters.Add(new CommandItem("Genre", (int)FilterType.Genre));
  201. filters.Add(new CommandItem("Release Date", (int)FilterType.ReleaseDate));
  202. filters.Add(new CommandItem("Runtime", (int)FilterType.Runtime));
  203. return filters;
  204. }
  205. public List<CommandItem> GetCommandItems()
  206. {
  207. List<CommandItem> commandItems = new List<CommandItem>();
  208. commandItems.Add(new CommandItem("All Titles", 0));
  209. commandItems.Add(new CommandItem("Categories", 1));
  210. commandItems.Add(new CommandItem("Genres", 2));
  211. commandItems.Add(new CommandItem("MPAA Ratings", 3));
  212. return commandItems;
  213. }
  214. public List<CommandItem> GetCommandItems(int id)
  215. {
  216. List<CommandItem> commandItems = new List<CommandItem>();
  217. switch (id)
  218. {
  219. case 1:
  220. foreach (Generic category in categories.OrderBy(g=>g.Name))
  221. {
  222. commandItems.Add(new CommandItem(string.Format("{0} ({1})", category.Name, category.Movies.Count), category));
  223. }
  224. break;
  225. case 2:
  226. foreach (Genre genre in genres.OrderBy(g => g.GenreDesc))
  227. {
  228. commandItems.Add(new CommandItem(string.Format("{0} ({1})", genre.GenreDesc, genre.Movies.Count), genre));
  229. }
  230. break;
  231. case 3:
  232. foreach (Generic rating in mpaaRatings.OrderBy(g => g.Name))
  233. {
  234. commandItems.Add(new CommandItem(string.Format("{0} ({1})", rating.Name, rating.Movies.Count), rating));
  235. }
  236. break;
  237. default:
  238. break;
  239. }
  240. return commandItems;
  241. }
  242. public List<Movie> GetMoviesFromCommand(int commandId, object data)
  243. {
  244. switch (commandId)
  245. {
  246. case 0:
  247. return movies;
  248. case 1:
  249. Generic category = data as Generic;
  250. return category.Movies;
  251. case 2:
  252. Genre genre = data as Genre;
  253. return genre.Movies;
  254. case 3:
  255. Generic rating = data as Generic;
  256. return rating.Movies;
  257. }
  258. return new List<Movie>();
  259. }
  260. public bool SupportsWatched()
  261. {
  262. return false;
  263. }
  264. #endregion
  265. }
  266. }