PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.7/WordPress.Model/Comment.cs

https://bitbucket.org/sendhil/wordpress-windowsphone
C# | 434 lines | 390 code | 40 blank | 4 comment | 68 complexity | 0e55618d0b8c721527b7a92db3b0d9fb MD5 | raw file
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using System.Xml.Serialization;
  7. using System.Text;
  8. using System.Security.Cryptography;
  9. using System.Net;
  10. namespace WordPress.Model
  11. {
  12. public class Comment : INotifyPropertyChanged
  13. {
  14. #region member variables
  15. private DateTime _dateCreatedGMT;
  16. private int _userId;
  17. private string _commentId;
  18. private string _parent;
  19. private string _status;
  20. private string _content;
  21. private string _link;
  22. private string _postId;
  23. private string _postTitle;
  24. private string _author;
  25. private string _authorUrl;
  26. private string _authorEmail;
  27. private string _authorIp;
  28. private string _commentType;
  29. private const string DATECREATEDGMT_VALUE = "date_created_gmt";
  30. private const string USERID_VALUE = "user_id";
  31. private const string COMMENTID_VALUE = "comment_id";
  32. private const string PARENT_VALUE = "parent";
  33. private const string STATUS_VALUE = "status";
  34. private const string CONTENT_VALUE = "content";
  35. private const string LINK_VALUE = "link";
  36. private const string POSTID_VALUE = "post_id";
  37. private const string POSTTITLE_VALUE = "post_title";
  38. private const string AUTHOR_VALUE = "author";
  39. private const string AUTHORURL_VALUE = "author_url";
  40. private const string AUTHOREMAIL_VALUE = "author_email";
  41. private const string AUTHORIP_VALUE = "author_ip";
  42. private const string COMMENTTYPE_VALUE = "type";
  43. #endregion
  44. #region events
  45. public event PropertyChangedEventHandler PropertyChanged;
  46. #endregion
  47. #region constructors
  48. public Comment() { }
  49. public Comment(XElement structElement)
  50. {
  51. ParseElement(structElement);
  52. }
  53. #endregion
  54. #region properties
  55. public DateTime DateCreatedGMT
  56. {
  57. get { return _dateCreatedGMT; }
  58. set
  59. {
  60. if (value != _dateCreatedGMT)
  61. {
  62. _dateCreatedGMT = value;
  63. NotifyPropertyChanged("DateCreatedGMT");
  64. }
  65. }
  66. }
  67. [XmlIgnore]
  68. public DateTime DateCreated
  69. {
  70. get { return _dateCreatedGMT.ToLocalTime(); }
  71. }
  72. public int UserId
  73. {
  74. get { return _userId; }
  75. set
  76. {
  77. if (value != _userId)
  78. {
  79. _userId = value;
  80. NotifyPropertyChanged("UserId");
  81. }
  82. }
  83. }
  84. public string CommentId
  85. {
  86. get { return _commentId; }
  87. set
  88. {
  89. if (value != _commentId)
  90. {
  91. _commentId = value;
  92. NotifyPropertyChanged("CommentId");
  93. }
  94. }
  95. }
  96. public string Parent
  97. {
  98. get { return _parent; }
  99. set
  100. {
  101. if (value != _parent)
  102. {
  103. _parent = value;
  104. NotifyPropertyChanged("Parent");
  105. }
  106. }
  107. }
  108. public string Content
  109. {
  110. get { return _content; }
  111. set
  112. {
  113. if (value != _content)
  114. {
  115. _content = value;
  116. NotifyPropertyChanged("Content");
  117. }
  118. }
  119. }
  120. public string Status
  121. {
  122. get { return _status; }
  123. set
  124. {
  125. if (value != _status)
  126. {
  127. _status = value;
  128. NotifyPropertyChanged("Status");
  129. NotifyPropertyChanged("CommentStatus");
  130. NotifyPropertyChanged("FormattedStatus");
  131. }
  132. }
  133. }
  134. public string FormattedStatus
  135. {
  136. //Format the comment status from 'approve' to 'Approved' etc
  137. get {
  138. String formattedStatus = "Approved";
  139. if (_status == "hold")
  140. formattedStatus = "Unapproved";
  141. else if (_status == "spam")
  142. formattedStatus = "Spam";
  143. return formattedStatus;
  144. }
  145. }
  146. [XmlIgnore]
  147. public eCommentStatus CommentStatus
  148. {
  149. get
  150. {
  151. //all comments are "approve" status by default, if we can't parse the status
  152. //default to that value
  153. eCommentStatus result = eCommentStatus.approve;
  154. try
  155. {
  156. if (!string.IsNullOrEmpty(_status))
  157. {
  158. result = (eCommentStatus)Enum.Parse(typeof(eCommentStatus), _status, true);
  159. }
  160. }
  161. catch (Exception) { }
  162. return result;
  163. }
  164. set
  165. {
  166. if (value != CommentStatus)
  167. {
  168. //use the Status property API to notify any interested parties of the change
  169. Status = value.ToString();
  170. NotifyPropertyChanged("CommentStatus");
  171. }
  172. }
  173. }
  174. public string Link
  175. {
  176. get { return _link; }
  177. set
  178. {
  179. if (value != _link)
  180. {
  181. _link = value;
  182. NotifyPropertyChanged("Link");
  183. }
  184. }
  185. }
  186. public string PostId
  187. {
  188. get { return _postId; }
  189. set
  190. {
  191. if (value != _postId)
  192. {
  193. _postId = value;
  194. NotifyPropertyChanged("PostId");
  195. }
  196. }
  197. }
  198. public string PostTitle
  199. {
  200. get { return _postTitle; }
  201. set
  202. {
  203. if (value != _postTitle)
  204. {
  205. _postTitle = value;
  206. NotifyPropertyChanged("PostTitle");
  207. }
  208. }
  209. }
  210. public string Author
  211. {
  212. get { return _author; }
  213. set
  214. {
  215. if (value != _author)
  216. {
  217. _author = value;
  218. NotifyPropertyChanged("Author");
  219. }
  220. }
  221. }
  222. public string AuthorUrl
  223. {
  224. get { return _authorUrl; }
  225. set
  226. {
  227. if (value != _authorUrl)
  228. {
  229. _authorUrl = value;
  230. NotifyPropertyChanged("AuthorUrl");
  231. }
  232. }
  233. }
  234. public string AuthorEmail
  235. {
  236. get { return _authorEmail; }
  237. set
  238. {
  239. if (value != _authorEmail)
  240. {
  241. _authorEmail = value;
  242. NotifyPropertyChanged("AuthorEmail");
  243. }
  244. }
  245. }
  246. public string AuthorIp
  247. {
  248. get { return _authorIp; }
  249. set
  250. {
  251. if (value != _authorIp)
  252. {
  253. _authorIp = value;
  254. NotifyPropertyChanged("AuthorIp");
  255. }
  256. }
  257. }
  258. public string CommentType
  259. {
  260. get { return _commentType; }
  261. set
  262. {
  263. if (value != _commentType)
  264. {
  265. _commentType = value;
  266. NotifyPropertyChanged("CommentType");
  267. }
  268. }
  269. }
  270. [XmlIgnore]
  271. public string GravatarUrl
  272. {
  273. get
  274. {
  275. string emailAddress = _authorEmail ?? String.Empty;
  276. Encoder encoder = Encoding.Unicode.GetEncoder();
  277. byte[] unicodeText = new byte[emailAddress.Length * 2];
  278. encoder.GetBytes(emailAddress.ToCharArray(), 0, emailAddress.Length, unicodeText, 0, true);
  279. string hash = MD5.GetMd5String(emailAddress.Trim());
  280. string uriFormat = "http://gravatar.com/avatar/{0}?s=100&d=mm";
  281. string result = string.Format(uriFormat, hash);
  282. return result;
  283. }
  284. }
  285. #endregion
  286. #region methods
  287. private void NotifyPropertyChanged(string propertyName)
  288. {
  289. if (null != PropertyChanged)
  290. {
  291. PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  292. }
  293. }
  294. private void ParseElement(XElement element)
  295. {
  296. if (!element.HasElements)
  297. {
  298. throw new XmlRPCParserException(XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_CODE, XmlRPCResponseConstants.XELEMENTMISSINGCHILDELEMENTS_MESSAGE);
  299. }
  300. string value = null;
  301. foreach (XElement member in element.Descendants(XmlRPCResponseConstants.MEMBER))
  302. {
  303. string memberName = member.Descendants(XmlRPCResponseConstants.NAME).First().Value;
  304. if (DATECREATEDGMT_VALUE.Equals(memberName))
  305. {
  306. value = member.Descendants(XmlRPCResponseConstants.DATETIMEISO8601).First().Value;
  307. if (!DateTime.TryParseExact(value, Constants.WORDPRESS_DATEFORMAT, CultureInfo.InvariantCulture, DateTimeStyles.None, out _dateCreatedGMT))
  308. {
  309. Exception detailedError = new FormatException("Unable to parse GMT date-time: " + value);
  310. throw new XmlRPCParserException(XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_CODE, XmlRPCResponseConstants.SERVER_RETURNED_INVALID_XML_RPC_MESSAGE, detailedError);
  311. }
  312. }
  313. else if (USERID_VALUE.Equals(memberName))
  314. {
  315. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  316. if (!int.TryParse(value, out _userId))
  317. {
  318. _userId = -1;
  319. }
  320. }
  321. else if (COMMENTID_VALUE.Equals(memberName))
  322. {
  323. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  324. _commentId = value;
  325. }
  326. else if (PARENT_VALUE.Equals(memberName))
  327. {
  328. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  329. _parent = value;
  330. }
  331. else if (STATUS_VALUE.Equals(memberName))
  332. {
  333. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  334. _status = value;
  335. }
  336. else if (CONTENT_VALUE.Equals(memberName))
  337. {
  338. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  339. _content = value.HtmlDecode();
  340. }
  341. else if (LINK_VALUE.Equals(memberName))
  342. {
  343. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  344. _link = value;
  345. }
  346. else if (POSTID_VALUE.Equals(memberName))
  347. {
  348. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  349. _postId = value;
  350. }
  351. else if (POSTTITLE_VALUE.Equals(memberName))
  352. {
  353. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  354. _postTitle = value.HtmlDecode();
  355. }
  356. else if (AUTHOR_VALUE.Equals(memberName))
  357. {
  358. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  359. _author = value.HtmlDecode();
  360. }
  361. else if (AUTHORURL_VALUE.Equals(memberName))
  362. {
  363. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  364. _authorUrl = value;
  365. }
  366. else if (AUTHOREMAIL_VALUE.Equals(memberName))
  367. {
  368. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  369. _authorEmail = value;
  370. }
  371. else if (AUTHORIP_VALUE.Equals(memberName))
  372. {
  373. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  374. _authorIp = value;
  375. }
  376. else if (COMMENTTYPE_VALUE.Equals(memberName))
  377. {
  378. value = member.Descendants(XmlRPCResponseConstants.STRING).First().Value;
  379. _commentType = value;
  380. }
  381. }
  382. }
  383. private class WPDateFormatProvider : IFormatProvider
  384. {
  385. public object GetFormat(Type formatType)
  386. {
  387. DateTimeFormatInfo info = new DateTimeFormatInfo();
  388. info.FullDateTimePattern = "yyyyMMddTH:mm:ss";
  389. return info;
  390. }
  391. }
  392. #endregion
  393. }
  394. }