RSSCache.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright © 2011-2012 Nokia Corporation. All rights reserved.
  3. * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation.
  4. * Other product and company names mentioned herein may be trademarks
  5. * or trade names of their respective owners.
  6. * See LICENSE.TXT for license information.
  7. */
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.IO.IsolatedStorage;
  11. using System.Runtime.Serialization;
  12. namespace RSSReader.Model
  13. {
  14. /// <summary>
  15. /// The RSSCache class. The cache is stored in an
  16. /// ObservableCollection that is displayed in the
  17. /// MainView's ListBox.
  18. /// </summary>
  19. [DataContract]
  20. public class RSSCache
  21. {
  22. /// <summary>
  23. /// Class member for cache of RSS pages
  24. /// </summary>
  25. private ObservableCollection<RSSPage> cache;
  26. /// <summary>
  27. /// Property for cache of RSS pages
  28. /// </summary>
  29. [DataMember]
  30. public ObservableCollection<RSSPage> Cache
  31. {
  32. get
  33. {
  34. if (cache == null)
  35. {
  36. cache = new ObservableCollection<RSSPage>();
  37. }
  38. return cache;
  39. }
  40. set
  41. {
  42. if (cache != value)
  43. {
  44. cache = value;
  45. }
  46. }
  47. }
  48. /// <summary>
  49. /// Property for first RSS page
  50. /// </summary>
  51. [DataMember]
  52. public RSSPage FirstPage
  53. {
  54. get
  55. {
  56. return cache[0];
  57. }
  58. set {}
  59. }
  60. /// <summary>
  61. /// Property for second RSS page
  62. /// </summary>
  63. [DataMember]
  64. public RSSPage SecondPage
  65. {
  66. get
  67. {
  68. return cache[1];
  69. }
  70. set { }
  71. }
  72. /// <summary>
  73. /// Property for third RSS page
  74. /// </summary>
  75. [DataMember]
  76. public RSSPage ThirdPage
  77. {
  78. get
  79. {
  80. return cache[2];
  81. }
  82. set { }
  83. }
  84. /// <summary>
  85. /// Property for fourth RSS page
  86. /// </summary>
  87. [DataMember]
  88. public RSSPage FourthPage
  89. {
  90. get
  91. {
  92. return cache[3];
  93. }
  94. set { }
  95. }
  96. /// <summary>
  97. /// Constructor
  98. /// </summary>
  99. public RSSCache()
  100. {
  101. }
  102. /// <summary>
  103. /// Method to a new RSS page to cache
  104. /// </summary>
  105. /// <param name="pages">Page to add</param>
  106. public void AddPages(List<RSSPage> pages)
  107. {
  108. foreach (RSSPage p in pages)
  109. {
  110. Cache.Add(p);
  111. }
  112. }
  113. /// <summary>
  114. /// Loads the cache from disk
  115. /// </summary>
  116. /// <returns>RSSCache as it was persisted</returns>
  117. public static RSSCache Load()
  118. {
  119. RSSCache cache = null;
  120. using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
  121. {
  122. using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("rssCache.dat", System.IO.FileMode.OpenOrCreate, file))
  123. {
  124. if (stream.Length > 0)
  125. {
  126. App.Log("Reading cache from file");
  127. DataContractSerializer serializer = new DataContractSerializer(typeof(RSSCache));
  128. cache = serializer.ReadObject(stream) as RSSCache;
  129. }
  130. }
  131. }
  132. // File was not found, create a new cache
  133. if (cache == null)
  134. {
  135. App.Log("Creating a new cache");
  136. cache = new RSSCache();
  137. }
  138. return cache;
  139. }
  140. /// <summary>
  141. /// Persists the cache to disk
  142. /// </summary>
  143. public void Save()
  144. {
  145. App.Log("Persisting cache to file");
  146. using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
  147. {
  148. using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("rssCache.dat", System.IO.FileMode.Create, file))
  149. {
  150. DataContractSerializer serializer = new DataContractSerializer(typeof(RSSCache));
  151. stream.Flush();
  152. serializer.WriteObject(stream, this);
  153. }
  154. }
  155. }
  156. }
  157. }