PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/InfoService/InfoService/RecentlyAddedWatched/Providers/ProviderMovingPictures.cs

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