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

/src/MarkPad/Settings/UI/BlogSettingsViewModel.cs

https://github.com/bcott/DownmarkerWPF
C# | 225 lines | 185 code | 40 blank | 0 comment | 31 complexity | 86b4250e7719fed8c650e4fceff07c2d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Caliburn.Micro;
  7. using MarkPad.DocumentSources.GitHub;
  8. using MarkPad.DocumentSources.MetaWeblog.Service;
  9. using MarkPad.DocumentSources.MetaWeblog.Service.Rsd;
  10. using MarkPad.Helpers;
  11. using MarkPad.Infrastructure.DialogService;
  12. using MarkPad.Settings.Models;
  13. namespace MarkPad.Settings.UI
  14. {
  15. public class BlogSettingsViewModel : Screen
  16. {
  17. readonly IDialogService dialogService;
  18. readonly Func<string, IMetaWeblogService> getMetaWeblog;
  19. private readonly IRsdService discoveryService;
  20. public BlogSettingsViewModel(IDialogService dialogService, Func<string, IMetaWeblogService> getMetaWeblog, IRsdService discoveryService)
  21. {
  22. this.dialogService = dialogService;
  23. this.getMetaWeblog = getMetaWeblog;
  24. this.discoveryService = discoveryService;
  25. BlogLanguages = new List<string> { "HTML", "Markdown" };
  26. }
  27. public override string DisplayName
  28. {
  29. get { return "Blog Settings"; }
  30. set { }
  31. }
  32. public void InitializeBlog(BlogSetting blog)
  33. {
  34. CurrentBlog = blog;
  35. }
  36. public List<string> BlogLanguages { get; set; }
  37. public string SelectedBlogLanguage
  38. {
  39. get
  40. {
  41. if (CurrentBlog == null)
  42. return "";
  43. return CurrentBlog.Language ?? "HTML";
  44. }
  45. set { CurrentBlog.Language = value; }
  46. }
  47. public BlogSetting CurrentBlog { get; set; }
  48. public bool MetaWebLog
  49. {
  50. get { return CurrentBlog.WebSourceType == WebSourceType.MetaWebLog; }
  51. set
  52. {
  53. if (value)
  54. CurrentBlog.WebSourceType = WebSourceType.MetaWebLog;
  55. }
  56. }
  57. public bool GitHub
  58. {
  59. get { return CurrentBlog.WebSourceType == WebSourceType.GitHub; }
  60. set
  61. {
  62. if (value)
  63. CurrentBlog.WebSourceType = WebSourceType.GitHub;
  64. }
  65. }
  66. public ObservableCollection<FetchedBlogInfo> APIBlogs { get; set; }
  67. public FetchedBlogInfo SelectedAPIBlog
  68. {
  69. get
  70. {
  71. if (CurrentBlog == null)
  72. return null;
  73. var bi = new FetchedBlogInfo
  74. {
  75. BlogInfo = CurrentBlog.BlogInfo,
  76. Name = CurrentBlog.BlogInfo.blogName
  77. };
  78. if (APIBlogs == null) APIBlogs = new ObservableCollection<FetchedBlogInfo>();
  79. var listEntry = APIBlogs.SingleOrDefault(b => b.Name == bi.Name);
  80. if (listEntry == null)
  81. {
  82. APIBlogs.Add(bi);
  83. return bi;
  84. }
  85. return listEntry;
  86. }
  87. set
  88. {
  89. if (CurrentBlog == null)
  90. return;
  91. CurrentBlog.BlogInfo = value == null ? new BlogInfo() : value.BlogInfo;
  92. }
  93. }
  94. public void SetCurrentBlogPassword(object password)
  95. {
  96. if (CurrentBlog == null)
  97. return;
  98. CurrentBlog.Password = password.ToString();
  99. }
  100. public void FetchBlogs()
  101. {
  102. SelectedAPIBlog = null;
  103. APIBlogs = new ObservableCollection<FetchedBlogInfo>();
  104. IsFetching = true;
  105. var fetchingTask = CurrentBlog.WebSourceType == WebSourceType.MetaWebLog ? FetchMetaWeblogApi() : FetchGithubBranches();
  106. fetchingTask
  107. .ContinueWith(UpdateBlogList, TaskScheduler.FromCurrentSynchronizationContext())
  108. .ContinueWith(HandleFetchError)
  109. .ContinueWith(t=>IsFetching = false);
  110. }
  111. async Task<BlogInfo[]> FetchGithubBranches()
  112. {
  113. var githubApi = new GithubApi();
  114. if (string.IsNullOrEmpty(CurrentBlog.Token))
  115. {
  116. var githubLogin = new GithubLogin();
  117. githubLogin.ShowDialog();
  118. CurrentBlog.Token = await githubApi.GetToken(githubLogin.Code);
  119. }
  120. return await githubApi.FetchBranches(CurrentBlog.Token, CurrentBlog.Username, CurrentBlog.WebAPI);
  121. }
  122. Task<BlogInfo[]> FetchMetaWeblogApi()
  123. {
  124. var proxy = getMetaWeblog(CurrentBlog.WebAPI);
  125. var fetchingTask = proxy
  126. .GetUsersBlogsAsync(CurrentBlog);
  127. return fetchingTask;
  128. }
  129. public bool Wiki
  130. {
  131. get { return CurrentBlog.WebAPI != null && CurrentBlog.WebAPI.EndsWith(".wiki"); }
  132. set
  133. {
  134. if (value && !Wiki)
  135. CurrentBlog.WebAPI = CurrentBlog.WebAPI + ".wiki";
  136. else if (!value && Wiki)
  137. CurrentBlog.WebAPI = CurrentBlog.WebAPI.Replace(".wiki", string.Empty);
  138. }
  139. }
  140. private void UpdateBlogList(Task<BlogInfo[]> t)
  141. {
  142. t.PropagateExceptions();
  143. var newAPIBlogs = new ObservableCollection<FetchedBlogInfo>();
  144. foreach (var blogInfo in t.Result)
  145. {
  146. newAPIBlogs.Add(new FetchedBlogInfo { Name = blogInfo.blogName, BlogInfo = blogInfo });
  147. }
  148. APIBlogs = newAPIBlogs;
  149. }
  150. private void HandleFetchError(Task t)
  151. {
  152. if (!t.IsFaulted)
  153. return;
  154. dialogService.ShowError("Markpad", "There was a problem contacting the website. Check the settings and try again.", t.Exception.GetErrorMessage());
  155. }
  156. public void DiscoverAddress()
  157. {
  158. var startAddress = CurrentBlog.WebAPI ?? string.Empty;
  159. if (!startAddress.StartsWith("http://"))
  160. startAddress = "http://" + startAddress;
  161. if (Uri.IsWellFormedUriString(startAddress, UriKind.Absolute))
  162. {
  163. DiscoveringAddress = true;
  164. discoveryService.DiscoverAddress(startAddress)
  165. .ContinueWith(t =>
  166. {
  167. if (t.Result.Success)
  168. CurrentBlog.WebAPI = t.Result.MetaWebLogApiLink;
  169. else
  170. {
  171. const string errorText = "Make sure you have a rsd.xml in the root of your blog, or put a link to it on your blog homepage head";
  172. dialogService.ShowError("Discovery failed", errorText, t.Result.FailMessage);
  173. }
  174. DiscoveringAddress = false;
  175. }, TaskScheduler.FromCurrentSynchronizationContext());
  176. }
  177. else
  178. {
  179. dialogService.ShowWarning("Enter blog address", "Enter your blog address to discover the MetaWeblog Uri", null);
  180. }
  181. }
  182. public bool DiscoveringAddress { get; private set; }
  183. public bool IsFetching { get; private set; }
  184. }
  185. }