From 406a63cc82836df87a084efcb597ce1404e6a90a Mon Sep 17 00:00:00 2001 From: Nick Vella Date: Fri, 2 Aug 2019 02:03:03 +1000 Subject: [PATCH] ssg: advanced config saving --- .../Preferences/PreferencesForm.cs | 4 +- .../StaticSiteAdvanced/AuthoringPanel.cs | 35 ++++- .../StaticSiteAdvanced/BuildPublishPanel.cs | 52 ++++-- .../StaticSiteAdvanced/FrontMatterPanel.cs | 18 ++- .../StaticSiteAdvanced/GeneralPanel.cs | 22 ++- .../StaticSitePreferencesController.cs | 148 ++++++++---------- .../StaticSitePreferencesForm.cs | 27 ++++ .../StaticSitePreferencesPanel.cs | 25 ++- 8 files changed, 220 insertions(+), 111 deletions(-) diff --git a/src/managed/OpenLiveWriter.ApplicationFramework/Preferences/PreferencesForm.cs b/src/managed/OpenLiveWriter.ApplicationFramework/Preferences/PreferencesForm.cs index ff074e1c..b1efc0c8 100644 --- a/src/managed/OpenLiveWriter.ApplicationFramework/Preferences/PreferencesForm.cs +++ b/src/managed/OpenLiveWriter.ApplicationFramework/Preferences/PreferencesForm.cs @@ -33,7 +33,7 @@ namespace OpenLiveWriter.ApplicationFramework.Preferences /// /// The PreferencesPanel list. /// - private ArrayList preferencesPanelList = new ArrayList(); + protected ArrayList preferencesPanelList = new ArrayList(); /// /// A value which indicates whether the form is initialized. @@ -391,7 +391,7 @@ namespace OpenLiveWriter.ApplicationFramework.Preferences /// Helper method to save Preferences. /// Returns true if saved successfully. /// - private bool SavePreferences() + protected virtual bool SavePreferences() { TabSwitcher tabSwitcher = new TabSwitcher(sideBarControl); diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/AuthoringPanel.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/AuthoringPanel.cs index 1998cfee..643a0482 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/AuthoringPanel.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/AuthoringPanel.cs @@ -17,6 +17,7 @@ using OpenLiveWriter.BlogClient; using OpenLiveWriter.PostEditor; using OpenLiveWriter.ApplicationFramework.Preferences; using OpenLiveWriter.PostEditor.Configuration.Wizard; +using OpenLiveWriter.BlogClient.Clients.StaticSite; namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced { @@ -94,12 +95,42 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced set => textBoxImagesPath.Text = value; } - public AuthoringPanel(StaticSitePreferencesController controller, TemporaryBlogSettings blogSettings) - : base(controller, blogSettings) + public AuthoringPanel(StaticSitePreferencesController controller) + : base(controller) { InitializeComponent(); } + public override void LoadConfig() + { + PostsPath = _controller.Config.PostsPath; + DraftsEnabled = _controller.Config.DraftsEnabled; + DraftsPath = _controller.Config.DraftsPath; + PagesEnabled = _controller.Config.PagesEnabled; + PagesPath = _controller.Config.PagesPath; + PagesStoredInRoot = _controller.Config.PagesPath == "."; + ImagesEnabled = _controller.Config.ImagesEnabled; + ImagesPath = _controller.Config.ImagesPath; + } + + public override void ValidateConfig() + => _controller.Config.Validator + .ValidatePostsPath() + .ValidateDraftsPath() + .ValidatePagesPath() + .ValidateImagesPath(); + + public override void Save() + { + _controller.Config.PostsPath = PostsPath; + _controller.Config.DraftsEnabled = DraftsEnabled; + _controller.Config.DraftsPath = DraftsPath; + _controller.Config.PagesEnabled = PagesEnabled; + _controller.Config.PagesPath = PagesStoredInRoot ? "." : PagesPath; + _controller.Config.ImagesEnabled = ImagesEnabled; + _controller.Config.ImagesPath = ImagesPath; + } + protected override void OnLoad(EventArgs e) { base.OnLoad(e); diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/BuildPublishPanel.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/BuildPublishPanel.cs index fb6069fd..1b16261e 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/BuildPublishPanel.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/BuildPublishPanel.cs @@ -46,19 +46,6 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced /// // private System.ComponentModel.Container components = null; - public BuildPublishPanel(StaticSitePreferencesController controller, TemporaryBlogSettings blogSettings) - : base(controller, blogSettings) - { - InitializeComponent(); - numericUpDownCmdTimeout.Maximum = int.MaxValue; - } - - protected override void OnLoad(EventArgs e) - { - base.OnLoad(e); - RecomputeEnabledStates(); - } - public bool ShowCmdWindows { get => checkBoxShowCommandWindows.Checked; @@ -113,6 +100,45 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced set => textBoxPublishCommand.Text = value; } + public BuildPublishPanel(StaticSitePreferencesController controller) + : base(controller) + { + InitializeComponent(); + numericUpDownCmdTimeout.Maximum = int.MaxValue; + } + + public override void LoadConfig() + { + ShowCmdWindows = _controller.Config.ShowCmdWindows; + CmdTimeoutMs = _controller.Config.CmdTimeoutMs; + BuildingEnabled = _controller.Config.BuildingEnabled; + BuildCommand = _controller.Config.BuildCommand; + OutputPath = _controller.Config.OutputPath; + PublishCommand = _controller.Config.PublishCommand; + } + + public override void ValidateConfig() + => _controller.Config.Validator + .ValidateBuildCommand() + .ValidateOutputPath() + .ValidatePublishCommand(); + + public override void Save() + { + _controller.Config.ShowCmdWindows = ShowCmdWindows; + _controller.Config.CmdTimeoutMs = CmdTimeoutMs; + _controller.Config.BuildingEnabled = BuildingEnabled; + _controller.Config.BuildCommand = BuildCommand; + _controller.Config.OutputPath = OutputPath; + _controller.Config.PublishCommand = PublishCommand; + } + + protected override void OnLoad(EventArgs e) + { + base.OnLoad(e); + RecomputeEnabledStates(); + } + #region Component Designer generated code /// /// Required method for Designer support - do not modify diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/FrontMatterPanel.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/FrontMatterPanel.cs index f54b58cf..3c05509e 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/FrontMatterPanel.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/FrontMatterPanel.cs @@ -40,12 +40,26 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced private Label labelSubtitle; private Dictionary _keyRowMap = new Dictionary(); - public FrontMatterPanel(StaticSitePreferencesController controller, TemporaryBlogSettings blogSettings) - : base(controller, blogSettings) + public FrontMatterPanel(StaticSitePreferencesController controller) + : base(controller) { InitializeComponent(); } + public override void LoadConfig() + { + Keys = _controller.Config.FrontMatterKeys; + } + + public override void ValidateConfig() + { + // No validator for FrontMatterKeys yet + } + + public override void Save() + { + _controller.Config.FrontMatterKeys = Keys; + } protected override void OnLayout(LayoutEventArgs e) { diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/GeneralPanel.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/GeneralPanel.cs index a961cc31..376522fc 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/GeneralPanel.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/GeneralPanel.cs @@ -17,6 +17,7 @@ using OpenLiveWriter.BlogClient; using OpenLiveWriter.PostEditor; using OpenLiveWriter.ApplicationFramework.Preferences; using OpenLiveWriter.PostEditor.Configuration.Wizard; +using OpenLiveWriter.BlogClient.Clients.StaticSite; namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced { @@ -62,12 +63,29 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced set => textBoxLocalSitePath.Text = value; } - public GeneralPanel(StaticSitePreferencesController controller, TemporaryBlogSettings blogSettings) - : base(controller, blogSettings) + public GeneralPanel(StaticSitePreferencesController controller) + : base(controller) { InitializeComponent(); } + public override void LoadConfig() + { + SiteTitle = _controller.Config.SiteTitle; + SiteUrl = _controller.Config.SiteUrl; + LocalSitePath = _controller.Config.LocalSitePath; + } + + public override void ValidateConfig() + => _controller.Config.Validator.ValidateLocalSitePath(); + + public override void Save() + { + _controller.Config.SiteTitle = SiteTitle; + _controller.Config.SiteUrl = SiteUrl; + _controller.Config.LocalSitePath = LocalSitePath; + } + protected override void OnLoad(EventArgs e) { base.OnLoad(e); diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesController.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesController.cs index 85ef68b8..7eaeee91 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesController.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesController.cs @@ -17,9 +17,11 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced { public class StaticSitePreferencesController { - private TemporaryBlogSettings _temporarySettings; - private PreferencesForm _form; + public StaticSiteConfig Config { get; set; } + private TemporaryBlogSettings _temporarySettings; + + private StaticSitePreferencesForm formPreferences; private GeneralPanel panelGeneral; private AuthoringPanel panelAuthoring; private FrontMatterPanel panelFrontMatter; @@ -28,46 +30,69 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced public StaticSitePreferencesController(TemporaryBlogSettings blogSettings) { _temporarySettings = blogSettings; - _form = new PreferencesForm(); + LoadConfigFromBlogSettings(); - panelGeneral = new GeneralPanel(this, blogSettings); - panelAuthoring = new AuthoringPanel(this, blogSettings); - panelFrontMatter = new FrontMatterPanel(this, blogSettings); - panelBuildPublish = new BuildPublishPanel(this, blogSettings); + panelGeneral = new GeneralPanel(this); + panelAuthoring = new AuthoringPanel(this); + panelFrontMatter = new FrontMatterPanel(this); + panelBuildPublish = new BuildPublishPanel(this); + + LoadConfigIntoPanels(); + } + + public void LoadConfigFromBlogSettings() + => Config = StaticSiteConfig.LoadConfigFromBlogSettings(_temporarySettings); + + public void LoadConfigIntoPanels() + { + panelGeneral.LoadConfig(); + panelAuthoring.LoadConfig(); + panelFrontMatter.LoadConfig(); + panelBuildPublish.LoadConfig(); + } + + /// + /// Save the StaticSiteConfig to an arbitrary TemporaryBlogSettings + /// + /// the TemporaryBlogSettings object + private void SaveConfigToBlogSettings(TemporaryBlogSettings settings) + { + Config.SaveToCredentials(settings.Credentials); + settings.BlogName = Config.SiteTitle; + settings.HomepageUrl = Config.SiteUrl; } private bool EditWeblogTemporarySettings(IWin32Window owner) { - LoadFromStaticSiteConfig(StaticSiteConfig.LoadConfigFromBlogSettings(_temporarySettings)); + LoadConfigFromBlogSettings(); + LoadConfigIntoPanels(); // Show form - using (var preferencesForm = new StaticSitePreferencesForm()) + using (formPreferences = new StaticSitePreferencesForm(this)) { - using (BlogClientUIContextScope uiContextScope = new BlogClientUIContextScope(preferencesForm)) + using (BlogClientUIContextScope uiContextScope = new BlogClientUIContextScope(formPreferences)) { // Customize form title and behavior - preferencesForm.Text = $"Static Site Configuration for '{_temporarySettings.BlogName}'"; //TODO use strings - preferencesForm.HideApplyButton(); + formPreferences.Text = $"Static Site Configuration for '{_temporarySettings.BlogName}'"; //TODO use strings + formPreferences.HideApplyButton(); // Add panels int iPanel = 0; - preferencesForm.SetEntry(iPanel++, panelGeneral); - preferencesForm.SetEntry(iPanel++, panelAuthoring); - preferencesForm.SetEntry(iPanel++, panelFrontMatter); - preferencesForm.SetEntry(iPanel++, panelBuildPublish); + formPreferences.SetEntry(iPanel++, panelGeneral); + formPreferences.SetEntry(iPanel++, panelAuthoring); + formPreferences.SetEntry(iPanel++, panelFrontMatter); + formPreferences.SetEntry(iPanel++, panelBuildPublish); - preferencesForm.SelectedIndex = 0; + formPreferences.SelectedIndex = 0; // Show the dialog - var result = preferencesForm.ShowDialog(owner); + var result = formPreferences.ShowDialog(owner); if(result == DialogResult.OK) { - // Create a static site config - var ssgConfig = StaticSiteConfig.LoadConfigFromBlogSettings(_temporarySettings); - SaveToStaticSiteConfig(ssgConfig); // All panels should be validated by this point, so save the settings - + + SaveConfigToBlogSettings(_temporarySettings); return true; } @@ -77,75 +102,26 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced return false; } - private void LoadFromStaticSiteConfig(StaticSiteConfig config) - { - // General - panelGeneral.SiteTitle = config.SiteTitle; - panelGeneral.SiteUrl = config.SiteUrl; - panelGeneral.LocalSitePath = config.LocalSitePath; - - // Authoring - panelAuthoring.PostsPath = config.PostsPath; - panelAuthoring.DraftsEnabled = config.DraftsEnabled; - panelAuthoring.DraftsPath = config.DraftsPath; - panelAuthoring.PagesEnabled = config.PagesEnabled; - panelAuthoring.PagesPath = config.PagesPath; - panelAuthoring.PagesStoredInRoot = config.PagesPath == "."; - panelAuthoring.ImagesEnabled = config.ImagesEnabled; - panelAuthoring.ImagesPath = config.ImagesPath; - - // Front Matter - panelFrontMatter.Keys = config.FrontMatterKeys; - - // Building and Publishing - panelBuildPublish.ShowCmdWindows = config.ShowCmdWindows; - panelBuildPublish.CmdTimeoutMs = config.CmdTimeoutMs; - panelBuildPublish.BuildingEnabled = config.BuildingEnabled; - panelBuildPublish.BuildCommand = config.BuildCommand; - panelBuildPublish.OutputPath = config.OutputPath; - panelBuildPublish.PublishCommand = config.PublishCommand; - } - - private void SaveToStaticSiteConfig(StaticSiteConfig config) - { - // General - config.SiteTitle = panelGeneral.SiteTitle; - config.SiteUrl = panelGeneral.SiteUrl; - config.LocalSitePath = panelGeneral.LocalSitePath; - - // Authoring - config.PostsPath = panelAuthoring.PostsPath; - config.DraftsEnabled = panelAuthoring.DraftsEnabled; - config.DraftsPath = panelAuthoring.DraftsPath; - config.PagesEnabled = panelAuthoring.PagesEnabled; - config.PagesPath = panelAuthoring.PagesStoredInRoot ? "." : panelAuthoring.PagesPath; - config.ImagesEnabled = panelAuthoring.ImagesEnabled; - config.ImagesPath = panelAuthoring.ImagesPath; - - // Front Matter - config.FrontMatterKeys = panelFrontMatter.Keys; - - // Building and Publishing - config.ShowCmdWindows = panelBuildPublish.ShowCmdWindows; - config.CmdTimeoutMs = panelBuildPublish.CmdTimeoutMs; - config.BuildingEnabled = panelBuildPublish.BuildingEnabled; - config.BuildCommand = panelBuildPublish.BuildCommand; - config.OutputPath = panelBuildPublish.OutputPath; - config.PublishCommand = panelBuildPublish.PublishCommand; - } - - public void GeneralPanel_RunAccountWizard() { - WeblogConfigurationWizardController.EditTemporarySettings(_form, _temporarySettings); - // Reload the settings into the form - LoadFromStaticSiteConfig(StaticSiteConfig.LoadConfigFromBlogSettings(_temporarySettings)); + var settingsCopy = TemporaryBlogSettings.CreateNew(); + settingsCopy.CopyFrom(_temporarySettings); + SaveConfigToBlogSettings(settingsCopy); + + var result = WeblogConfigurationWizardController.EditTemporarySettings(formPreferences, settingsCopy); + + // If wizard is successful, load the new settings back into the form. + if(result) + { + _temporarySettings.CopyFrom(settingsCopy); + Config = StaticSiteConfig.LoadConfigFromBlogSettings(_temporarySettings); + LoadConfigIntoPanels(); + } } public void GeneralPanel_RunAutoDetect() { - var ssgConfig = StaticSiteConfig.LoadConfigFromBlogSettings(_temporarySettings); - var result = StaticSiteConfigDetector.AttmeptAutoDetect(ssgConfig); + var result = StaticSiteConfigDetector.AttmeptAutoDetect(Config); if(result) { @@ -156,7 +132,7 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced MessageBoxButtons.OK, MessageBoxIcon.Information); - LoadFromStaticSiteConfig(ssgConfig); + LoadConfigIntoPanels(); } } diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesForm.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesForm.cs index ff59eac1..792d548f 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesForm.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesForm.cs @@ -10,5 +10,32 @@ namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced { public class StaticSitePreferencesForm : PreferencesForm { + private StaticSitePreferencesController _controller; + + public StaticSitePreferencesForm(StaticSitePreferencesController controller) + { + _controller = controller; + } + + + protected override bool SavePreferences() + { + // We need to save all the results before validating them, as the validity of some settings are dependant on others, + // eg. PostPath is dependant on LocalSitePath + + // Clone the existing config in-case of validation failure + var originalConfig = _controller.Config.Clone(); + + foreach (StaticSitePreferencesPanel preferencesPanel in preferencesPanelList) + { + preferencesPanel.Save(); + } + + // On succesful validation, hand control back to controller + if (base.SavePreferences()) return true; + // Otherwise, reset settings to pre-validation state and return false + _controller.Config = originalConfig; + return false; + } } } diff --git a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesPanel.cs b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesPanel.cs index 163eec71..22a4e5ff 100644 --- a/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesPanel.cs +++ b/src/managed/OpenLiveWriter.PostEditor/Configuration/StaticSiteAdvanced/StaticSitePreferencesPanel.cs @@ -3,20 +3,37 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; - +using System.Windows.Forms; using OpenLiveWriter.ApplicationFramework.Preferences; +using OpenLiveWriter.BlogClient.Clients.StaticSite; namespace OpenLiveWriter.PostEditor.Configuration.StaticSiteAdvanced { public abstract class StaticSitePreferencesPanel : PreferencesPanel { protected StaticSitePreferencesController _controller; - protected TemporaryBlogSettings _blogSettings; - public StaticSitePreferencesPanel(StaticSitePreferencesController controller, TemporaryBlogSettings blogSettings) : base() + public StaticSitePreferencesPanel(StaticSitePreferencesController controller) : base() { _controller = controller; - _blogSettings = blogSettings; + } + + public abstract void LoadConfig(); + public abstract void ValidateConfig(); + + public override bool PrepareSave(SwitchToPanel switchToPanel) + { + try + { + ValidateConfig(); + } + catch (StaticSiteConfigValidationException ex) + { + MessageBox.Show(ex.Text, ex.Title, MessageBoxButtons.OK, MessageBoxIcon.Warning); + switchToPanel(); + return false; + } + return true; } } }