PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/1.6.6 - MePo1.2 Beta/InfoService/InfoService/RecentlyAddedWatched/Providers/ProviderMovingPictures.cs

#
C# | 221 lines | 171 code | 36 blank | 14 comment | 27 complexity | 9e006cc0d1d275d7fd80d94f888f398a MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Cornerstone.Tools;
  5. using Cornerstone.Database;
  6. using Cornerstone.Database.Tables;
  7. using InfoService.Enums;
  8. using InfoService.RecentlyAddedWatched.Data;
  9. using InfoService.RecentlyAddedWatched.Interfaces;
  10. using InfoService.Utils;
  11. using MediaPortal.Plugins.MovingPictures;
  12. using MediaPortal.Plugins.MovingPictures.Database;
  13. namespace InfoService.RecentlyAddedWatched.Providers
  14. {
  15. public class ProviderMovingPictures : IRecentlyAddedWatchedProvider<RecentlyMovieItem>
  16. {
  17. private static readonly Logger logger = Logger.GetInstance();
  18. private static List<RecentlyMovieItem> MoviesAdded;
  19. private static List<RecentlyMovieItem> MoviesWatched;
  20. private const int MaxRecentlyItems = 3;
  21. public event EventHandler<NewItemHandler<RecentlyMovieItem>> OnNewItem;
  22. public ProviderMovingPictures()
  23. {
  24. logger.WriteLog("MovingPictures: Init MovingPictures RecentlyAddedWatchedProvider", LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  25. MovingPicturesCore.DatabaseManager.ObjectInserted += new DatabaseManager.ObjectAffectedDelegate(DatabaseManager_ObjectInserted);
  26. MoviesWatched = new List<RecentlyMovieItem>();
  27. MoviesAdded = new List<RecentlyMovieItem>();
  28. foreach (RecentlyMovieItem movie in RetrieveRecentlyAddedFromPlugin())
  29. {
  30. AddNewItem(movie);
  31. }
  32. foreach (RecentlyMovieItem movie in RetrieveRecentlyWatchedFromPlugin())
  33. {
  34. AddNewItem(movie);
  35. }
  36. }
  37. public List<RecentlyMovieItem> GetRecentlyAdded()
  38. {
  39. return MoviesAdded;
  40. }
  41. public List<RecentlyMovieItem> GetRecentlyWatched()
  42. {
  43. return MoviesWatched;
  44. }
  45. private void AddNewItem(RecentlyMovieItem item)
  46. {
  47. if (item != null)
  48. {
  49. if (item.Mode == RecentlyAddedWatchedMode.Watched)
  50. {
  51. logger.WriteLog(string.Format("MovingPictures: New movie watched: {0}", item.Movie.Title), LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  52. MoviesWatched.Insert(0, item);
  53. if (MoviesWatched.Count > MaxRecentlyItems) MoviesWatched.RemoveAt(MaxRecentlyItems);
  54. }
  55. else if (item.Mode == RecentlyAddedWatchedMode.Added)
  56. {
  57. logger.WriteLog(string.Format("MovingPictures: New movie added: {0}", item.Movie.Title), LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  58. MoviesAdded.Insert(0, item);
  59. if (MoviesAdded.Count > MaxRecentlyItems) MoviesAdded.RemoveAt(MaxRecentlyItems);
  60. }
  61. }
  62. }
  63. private void DatabaseManager_ObjectInserted(DatabaseTable obj)
  64. {
  65. if (obj.GetType() == typeof(DBMovieInfo))
  66. {
  67. DBMovieInfo movie = (DBMovieInfo) obj;
  68. RecentlyMovieItem ra = new RecentlyMovieItem();
  69. ra = GetRecentlyMovieItemFromEpisode(movie);
  70. ra.Mode = RecentlyAddedWatchedMode.Added;
  71. AddNewItem(ra);
  72. if (OnNewItem != null) OnNewItem(this, new NewItemHandler<RecentlyMovieItem>(ra));
  73. }
  74. else if(obj.GetType() == typeof(DBWatchedHistory))
  75. {
  76. DBWatchedHistory movie = (DBWatchedHistory) obj;
  77. RecentlyMovieItem ra = new RecentlyMovieItem();
  78. ra = GetRecentlyMovieItemFromEpisode(movie.Movie);
  79. ra.Mode = RecentlyAddedWatchedMode.Watched;
  80. AddNewItem(ra);
  81. if (OnNewItem != null) OnNewItem(this, new NewItemHandler<RecentlyMovieItem>(ra));
  82. }
  83. }
  84. private List<RecentlyMovieItem> RetrieveRecentlyAddedFromPlugin()
  85. {
  86. logger.WriteLog("MovingPictures: Get Most Recent Added Movies", LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  87. // get list of movies in database
  88. List<DBMovieInfo> movies = DBMovieInfo.GetAll();
  89. // get filter criteria of movies protected by parental conrols
  90. bool pcFilterEnabled = MovingPicturesCore.Settings.ParentalControlsEnabled;
  91. DBFilter<DBMovieInfo> pcFilter = MovingPicturesCore.Settings.ParentalControlsFilter;
  92. // apply parental control filter to movie list
  93. List<DBMovieInfo> filteredMovies = pcFilterEnabled ? pcFilter.Filter(movies).ToList() : movies;
  94. logger.WriteLog(string.Format("MovingPictures: {0} Movies found in database", movies.Count.ToString()), LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  95. logger.WriteLog(string.Format("MovingPictures: {0} Movies filtered by parental controls", movies.Count - filteredMovies.Count), LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  96. // Sort list in to most recent first
  97. filteredMovies.Sort((m1, m2) =>
  98. {
  99. return m2.DateAdded.CompareTo(m1.DateAdded);
  100. });
  101. // Update properties
  102. int i = 1;
  103. List<RecentlyMovieItem> items = new List<RecentlyMovieItem>();
  104. foreach (DBMovieInfo movie in filteredMovies)
  105. {
  106. RecentlyMovieItem ra = new RecentlyMovieItem();
  107. ra = GetRecentlyMovieItemFromEpisode(movie);
  108. ra.Mode = RecentlyAddedWatchedMode.Added;
  109. logger.WriteLog(string.Format("MovingPictures: Recently Added Movie {0} is {1}", i, movie.Title), LogLevel.Debug, InfoServiceModul.RecentlyAddedWatched);
  110. items.Add(ra);
  111. i++;
  112. if (i == MaxRecentlyItems + 1) break;
  113. }
  114. return items;
  115. }
  116. private List<RecentlyMovieItem> RetrieveRecentlyWatchedFromPlugin()
  117. {
  118. logger.WriteLog("Moving Pictures: Get Most Recent Watched Movies", LogLevel.Info, InfoServiceModul.RecentlyAddedWatched);
  119. // get list of movies in database
  120. List<DBMovieInfo> movies = DBMovieInfo.GetAll();
  121. // get filter criteria of movies protected by parental conrols
  122. bool pcFilterEnabled = MovingPicturesCore.Settings.ParentalControlsEnabled;
  123. DBFilter<DBMovieInfo> pcFilter = MovingPicturesCore.Settings.ParentalControlsFilter;
  124. // apply parental control filter to movie list
  125. List<DBMovieInfo> filteredMovies = pcFilterEnabled ? pcFilter.Filter(movies).ToList() : movies;
  126. // sort based on most recently watched
  127. filteredMovies.Sort((m1, m2) =>
  128. {
  129. // get watched count for each movie
  130. int watchCount1 = m1.WatchedHistory.Count;
  131. int watchCount2 = m2.WatchedHistory.Count;
  132. // compare most recently watched dates
  133. // WatchedHistory stores a list of dates each time the movie was watched
  134. DateTime lastWatchDate1 = watchCount1 > 0 ? m1.WatchedHistory[watchCount1 - 1].DateWatched : DateTime.MinValue;
  135. DateTime lastWatchDate2 = watchCount2 > 0 ? m2.WatchedHistory[watchCount2 - 1].DateWatched : DateTime.MinValue;
  136. return lastWatchDate2.CompareTo(lastWatchDate1);
  137. });
  138. int i = 1;
  139. List<RecentlyMovieItem> items = new List<RecentlyMovieItem>();
  140. foreach (DBMovieInfo movie in filteredMovies)
  141. {
  142. if (movie.WatchedHistory.Count > 0)
  143. {
  144. RecentlyMovieItem ra = new RecentlyMovieItem();
  145. ra = GetRecentlyMovieItemFromEpisode(movie);
  146. ra.Mode = RecentlyAddedWatchedMode.Watched;
  147. items.Add(ra);
  148. logger.WriteLog(string.Format("Moving Pictures: Recently Watched Movie {0} is {1}", i, movie.Title), LogLevel.Debug, InfoServiceModul.RecentlyAddedWatched);
  149. i++;
  150. if (i == MaxRecentlyItems + 1) break;
  151. }
  152. }
  153. return items;
  154. }
  155. private RecentlyMovieItem GetRecentlyMovieItemFromEpisode(DBMovieInfo movie)
  156. {
  157. RecentlyMovieItem ra = new RecentlyMovieItem();
  158. ra.Movie = movie;
  159. ra.Thumb = movie.CoverThumbFullPath;
  160. ra.Fanart = movie.BackdropFullPath;
  161. ra.DateAdded = movie.DateAdded;
  162. ra.Runtime = GetMovieRuntime(movie);
  163. ra.Certification = movie.Certification;
  164. ra.Score = movie.Score.ToString();
  165. ra.RoundedScore = Math.Round(movie.Score, MidpointRounding.AwayFromZero).ToString();
  166. ra.Mode = RecentlyAddedWatchedMode.Watched;
  167. ra.Type = RecentlyAddedWatchedType.Movie;
  168. return ra;
  169. }
  170. private static TimeSpan GetMovieRuntime(DBMovieInfo movie)
  171. {
  172. int minutes = 0;
  173. if (movie == null) return new TimeSpan();
  174. if (MovingPicturesCore.Settings.DisplayActualRuntime && movie.ActualRuntime > 0)
  175. {
  176. // Actual Runtime or (MediaInfo result) is in milliseconds
  177. // convert to minutes
  178. minutes = ((movie.ActualRuntime/1000)/60);
  179. }
  180. else
  181. minutes = movie.Runtime;
  182. return new TimeSpan(0, 0, minutes, 0, 0);
  183. }
  184. }
  185. }