PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MarkPad/DocumentSources/MetaWeblog/BlogService.cs

https://github.com/bcott/DownmarkerWPF
C# | 138 lines | 115 code | 23 blank | 0 comment | 14 complexity | 7a3010d609612562d2228b140f73a012 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.Serialization.Json;
  6. using System.Text;
  7. using Caliburn.Micro;
  8. using MarkPad.Infrastructure.DialogService;
  9. using MarkPad.Settings;
  10. using MarkPad.Settings.Models;
  11. using MarkPad.Settings.UI;
  12. using Ookii.Dialogs.Wpf;
  13. namespace MarkPad.DocumentSources.MetaWeblog
  14. {
  15. public class BlogService : IBlogService
  16. {
  17. readonly IDialogService dialogService;
  18. readonly IWindowManager windowManager;
  19. readonly Func<BlogSettingsViewModel> blogSettingsCreator;
  20. readonly ISettingsProvider settingsProvider;
  21. public BlogService(
  22. IDialogService dialogService,
  23. IWindowManager windowManager,
  24. Func<BlogSettingsViewModel> blogSettingsCreator,
  25. ISettingsProvider settingsProvider)
  26. {
  27. this.dialogService = dialogService;
  28. this.windowManager = windowManager;
  29. this.blogSettingsCreator = blogSettingsCreator;
  30. this.settingsProvider = settingsProvider;
  31. }
  32. public bool ConfigureNewBlog(string featureName)
  33. {
  34. var extra = string.Format(
  35. "The '{0}' feature requires a blog to be configured. A window will be displayed which will allow you to configure a blog.",
  36. featureName);
  37. var setupBlog = dialogService.ShowConfirmation(
  38. "No blogs are configured",
  39. "Do you want to configure a blog?",
  40. extra,
  41. new ButtonExtras(ButtonType.Yes, "Yes", "Configure a blog"),
  42. new ButtonExtras(ButtonType.No, "No", "Don't configure a blog"));
  43. if (!setupBlog)
  44. return false;
  45. return AddBlog() != null;
  46. }
  47. public BlogSetting AddBlog()
  48. {
  49. var blog = new BlogSetting { BlogName = "New", Language = "HTML" };
  50. blog.BeginEdit();
  51. var blogSettings = blogSettingsCreator();
  52. blogSettings.InitializeBlog(blog);
  53. var result = windowManager.ShowDialog(blogSettings);
  54. if (result != true)
  55. {
  56. blog.CancelEdit();
  57. return null;
  58. }
  59. blog.EndEdit();
  60. var blogs = GetBlogs();
  61. blogs.Add(blog);
  62. SaveBlogs(blogs);
  63. return blog;
  64. }
  65. public bool EditBlog(BlogSetting currentBlog)
  66. {
  67. var blogs = GetBlogs();
  68. var blogToUpdate = blogs.SingleOrDefault(b => SameBlog(currentBlog, b));
  69. currentBlog.BeginEdit();
  70. var blogSettings = blogSettingsCreator();
  71. blogSettings.InitializeBlog(currentBlog);
  72. var result = windowManager.ShowDialog(blogSettings);
  73. if (result != true)
  74. {
  75. currentBlog.CancelEdit();
  76. return false;
  77. }
  78. var index = blogs.IndexOf(blogToUpdate);
  79. blogs[index] = currentBlog;
  80. currentBlog.EndEdit();
  81. SaveBlogs(blogs);
  82. return true;
  83. }
  84. public void Remove(BlogSetting currentBlog)
  85. {
  86. var blogs = GetBlogs();
  87. var blogToRemove = blogs.SingleOrDefault(b => SameBlog(currentBlog, b));
  88. blogs.Remove(blogToRemove);
  89. SaveBlogs(blogs);
  90. }
  91. static bool SameBlog(BlogSetting b1, BlogSetting b2)
  92. {
  93. return b2.BlogInfo.blogName == b1.BlogInfo.blogName && b2.BlogInfo.blogid == b1.BlogInfo.blogid
  94. && b2.BlogName == b1.BlogName && b2.WebAPI == b1.WebAPI;
  95. }
  96. public List<BlogSetting> GetBlogs()
  97. {
  98. var settings = settingsProvider.GetSettings<MarkPadSettings>();
  99. if (string.IsNullOrEmpty(settings.BlogsJson))
  100. return new List<BlogSetting>();
  101. var serializer = new DataContractJsonSerializer(typeof (List<BlogSetting>));
  102. return (List<BlogSetting>)serializer.ReadObject(new MemoryStream(Encoding.Default.GetBytes(settings.BlogsJson)));
  103. }
  104. void SaveBlogs(List<BlogSetting> blogs)
  105. {
  106. var settings = settingsProvider.GetSettings<MarkPadSettings>();
  107. var ms = new MemoryStream();
  108. var writer = JsonReaderWriterFactory.CreateJsonWriter(ms);
  109. new DataContractJsonSerializer(typeof(List<BlogSetting>)).WriteObject(ms, blogs);
  110. writer.Flush();
  111. settings.BlogsJson = Encoding.Default.GetString(ms.ToArray());
  112. settingsProvider.SaveSettings(settings);
  113. }
  114. }
  115. }