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

/MySpaceSilverlight/MySpaceSilverlightKit/AppData.cs

#
C# | 297 lines | 182 code | 51 blank | 64 comment | 25 complexity | 7d893fa6c3bd7c1fbd953d2258d0b61c MD5 | raw file
  1. // <copyright file="AppData.cs" company="Microsoft Corporation">
  2. // Copyright (c) 2009 Microsoft Corporation All Rights Reserved
  3. // </copyright>
  4. // <author>Michael S. Scherotter</author>
  5. // <email>mischero@microsoft.com</email>
  6. // <date>2009-05-28</date>
  7. // <summary>Application Data</summary>
  8. namespace MyOpenSpace
  9. {
  10. using System.ComponentModel;
  11. using System.Globalization;
  12. using System.Windows.Browser;
  13. using opensocial;
  14. using Synergist;
  15. /// <summary>
  16. /// User Application data
  17. /// </summary>
  18. /// <remarks>Uses the <a target="_blank"
  19. /// href="http://wiki.opensocial.org/index.php?title=The_Persistence_API">
  20. /// OpenSocial Persistance API</a></remarks>
  21. public class AppData : INotifyPropertyChanged
  22. {
  23. #region Fields
  24. /// <summary>
  25. /// the app value stored
  26. /// </summary>
  27. private string appValue;
  28. /// <summary>
  29. /// the key to the app value
  30. /// </summary>
  31. private string key;
  32. /// <summary>
  33. /// the ID of the user (default is "opensocial.IdSpec.PersonId.VIEWER")
  34. /// </summary>
  35. private string id;
  36. /// <summary>
  37. /// the opensocial object
  38. /// </summary>
  39. private ScriptObject opensocial;
  40. #endregion
  41. #region Constructors
  42. /// <summary>
  43. /// Initializes a new instance of the AppData class.
  44. /// </summary>
  45. public AppData()
  46. {
  47. this.id = "opensocial.IdSpec.PersonId.VIEWER";
  48. }
  49. #endregion
  50. #region INotifyPropertyChanged Members
  51. /// <summary>
  52. /// Property changed event handler
  53. /// </summary>
  54. public event PropertyChangedEventHandler PropertyChanged;
  55. #endregion
  56. #region Properties
  57. /// <summary>
  58. /// Gets the opensocial object
  59. /// </summary>
  60. public ScriptObject OpenSocial
  61. {
  62. get
  63. {
  64. if (this.opensocial == null)
  65. {
  66. try
  67. {
  68. this.opensocial = HtmlPage.Window.Eval("opensocial") as ScriptObject;
  69. }
  70. catch (System.InvalidOperationException ioe)
  71. {
  72. System.Diagnostics.Debug.WriteLine("Not running in an OpenSocial container: {0}", ioe.Message);
  73. return null;
  74. }
  75. }
  76. return this.opensocial;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets or sets the user to read/write persistent data for
  81. /// </summary>
  82. public string Id
  83. {
  84. get
  85. {
  86. return this.id;
  87. }
  88. set
  89. {
  90. if (this.id != value)
  91. {
  92. this.id = value;
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Gets or sets the app data key
  98. /// </summary>
  99. public string Key
  100. {
  101. get
  102. {
  103. return this.key;
  104. }
  105. set
  106. {
  107. if (this.key != value)
  108. {
  109. this.key = value;
  110. this.SetValue();
  111. }
  112. }
  113. }
  114. /// <summary>
  115. /// Gets or sets the app data value
  116. /// </summary>
  117. public string Value
  118. {
  119. get
  120. {
  121. if (string.IsNullOrEmpty(this.appValue))
  122. {
  123. this.FetchValue();
  124. }
  125. return this.appValue;
  126. }
  127. set
  128. {
  129. if (this.appValue != value)
  130. {
  131. this.appValue = value;
  132. this.SetValue();
  133. }
  134. }
  135. }
  136. #endregion
  137. #region Methods
  138. /// <summary>
  139. /// remove the key value
  140. /// </summary>
  141. public void RemoveValue()
  142. {
  143. if (this.OpenSocial == null)
  144. {
  145. return;
  146. }
  147. var dataRequest = new DataRequest(this.OpenSocial.Invoke("newDataRequest", null) as ScriptObject);
  148. var request = dataRequest.newRemovePersonAppDataRequest(this.Id, this.Key);
  149. dataRequest.add(request, this.Id.Eval());
  150. dataRequest.send(new opensocial.DataReceived(this.OnDataRemoved));
  151. }
  152. #endregion
  153. #region Implementation
  154. /// <summary>
  155. /// Fetch the value from persistent storage
  156. /// </summary>
  157. private void FetchValue()
  158. {
  159. if (this.OpenSocial == null)
  160. {
  161. return;
  162. }
  163. var dataRequest = new DataRequest(this.OpenSocial.Invoke("newDataRequest", null) as ScriptObject);
  164. var idSpec = new IdSpec()
  165. {
  166. UserId = this.Id.Eval()
  167. };
  168. var request = dataRequest.newFetchPersonAppDataRequest(idSpec, this.Key, null);
  169. dataRequest.add(request, "get_data");
  170. dataRequest.send(new opensocial.DataReceived(this.OnDataRetrieved));
  171. }
  172. /// <summary>
  173. /// Set the value to persistent storage
  174. /// </summary>
  175. private void SetValue()
  176. {
  177. if (string.IsNullOrEmpty(this.Key) || string.IsNullOrEmpty(this.Value))
  178. {
  179. return;
  180. }
  181. if (this.OpenSocial == null)
  182. {
  183. return;
  184. }
  185. var dataRequest = new DataRequest(this.OpenSocial.Invoke("newDataRequest", null) as ScriptObject);
  186. string jsonValue = "{" + string.Format(
  187. CultureInfo.InvariantCulture,
  188. "\"{0}\" : \"{1}\"",
  189. this.Key,
  190. this.Value) + "}";
  191. var request = dataRequest.newUpdatePersonAppDataRequest(this.Id, this.Key, jsonValue);
  192. dataRequest.add(request, "set_data");
  193. dataRequest.send(new opensocial.DataReceived(this.OnDataUpdated));
  194. }
  195. /// <summary>
  196. /// Data updated event handler
  197. /// </summary>
  198. /// <param name="response">the data response</param>
  199. private void OnDataUpdated(DataResponse response)
  200. {
  201. if (response.HadError)
  202. {
  203. }
  204. else
  205. {
  206. var data = response.get("set_data").getData();
  207. }
  208. }
  209. /// <summary>
  210. /// Data retrieved event handler
  211. /// </summary>
  212. /// <param name="response">the data response</param>
  213. private void OnDataRetrieved(DataResponse response)
  214. {
  215. if (response.HadError)
  216. {
  217. }
  218. else
  219. {
  220. var data = response.get("get_data").getData();
  221. var viewerId = HtmlPage.Window.Eval("gadgets.views.getParams().viewerId") as string;
  222. var viewerData = data.GetProperty(viewerId) as ScriptObject;
  223. if (viewerData != null)
  224. {
  225. var property = viewerData.GetProperty(this.Key) as ScriptObject;
  226. if (property != null)
  227. {
  228. this.appValue = property.GetProperty(this.Key) as string;
  229. if (this.PropertyChanged != null)
  230. {
  231. this.PropertyChanged(this, new PropertyChangedEventArgs("Value"));
  232. }
  233. }
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// Data removed event handler
  239. /// </summary>
  240. /// <param name="response">the data response</param>
  241. private void OnDataRemoved(DataResponse response)
  242. {
  243. }
  244. #endregion
  245. }
  246. }