Writing Plugins For Windows Live Writer – Using Settings In Your Plugin

So far I’ve shown you how to create basic plugins for Windows Live Writer, in this next guide I’m going to show you how you can use settings in your plugin to make things easier for your end user, you can create default options (that they can customise if you allow them). I’m going to show you how to do exactly that, have the user be able to change the default settings that are applied everytime the plugin is called. I will be carrying this on from my last guide so if you haven’t got that already, download the source now.

First thing’s first, what options does your plugin need? This is still going to be a simple plugin, so anything you put in can’t be changed once inserted, so the options and settings need to be something that the user would want. The simple plugin we have been using so far in this series has been to insert some simple text into the blog entry, so the option I’m going to use is to make it default to use a bold style for the text.

For the settings, we need to create a new Class that will contain all the settings’ properties. In this example, I have called it PluginSettings.cs. Before we call the constructor, we need to just declare an instance of the IProperties Interface:


        private IProperties m_properties;
        private const string DEFAULTBOLD = "DEFAULTBOLD";

At this point, I also like to declare constants for the setting properties, these will be used in a bit, I will note that you don’t need to put these in, this is more a personal preference for me.

The settings class needs to be passed an IProperties instance so that it has a point of reference, then you need to set the IProperties instance you just declared to be the one that gets passed to the class. So your constructor should look like this:


        public PluginSettings(IProperties properties)
            m_properties = properties;

Next we write the properties for the settings class, you will need one of these for each of the settings you use in your plugin and they can be one of five different types: int, string, boolean, decimal or float. For this plugin, I’m going to use a boolean type as I want it to either be yes (true) or no (false) for whether the text should be bold or not. This plugin only requires one property, and mine looks like this:


        public bool DefaultBoldOption
            get { return m_properties.GetBoolean(DEFAULTBOLD, true); }
            set { m_properties.SetBoolean(DEFAULTBOLD, value); }

You’ll see that this is where we used the constant value I mentioned earlier, this is the property name that tells the plugin which setting to get, and you’ll see that there are two sections, a get section, and a set section. The get section must return a value of whatever type you’re using, in this case, a boolean value, so we use the IProperties’ GetBoolean method and put in the property name that we want. The set section is where we assign what the property’s value is going to be. This will all become clearer a bit further on. And that’s the settings class done with.

Now we move back to the plugin’s main class. To start off with, declare a PluginSettings object (I’ve called it m_defaultoptions), this will be set next. To do this, we need to override the Initialize method so that we can get the IProperties object for that plugin. Declaring the settings originally outside of this method means that the default options are available to the rest of the plugin. It should look like this:


        public override void Initialize(IProperties pluginOptions)
            m_defaultsettings = new PluginSettings(pluginOptions);

Notice how we pass the PluginSettings class the IProperties object so that it knows where it’s reading/writing from.

You might remember in the previous parts to this guide, in the WriterPlugin section we had HasEditbleOptions set to false, well now we’re going to set it to true, this will enable the Options button in the plugins part of the preferences. Before we tell the plugin about the options window, let’s create the form first. So add a new Windows form to the project (I called mine frmOptions.cs) and make your form look something like this:

Now, hit F7 and go into the code for the form and you will need to add the using WindowsLive.Writer.Api; namespace again. At the top, declare a new instance of the PluginSettings class, and in the constructor we need to then set that to be the PluginSettings that is being passed to the form, then apply the default settings. So your code should look like this:

        public frmOptions(PluginSettings settings)
            m_settings = settings;
            // This sets the checkbox to be whatever the default option was
            chBoldOption.Checked = m_settings.DefaultBoldOption;

Now these settings are only basic, so when we press the save button, there’s only one setting that needs to be saved, which we do with the following line:

        private void btnSave_Click(object sender, EventArgs e)
            // This actually sets the option to be whatever the checkbox is currently at
            m_settings.DefaultBoldOption = chBoldOption.Checked;

Right, back to the main plugin class. Now that we have our options form, we need to tell the plugin what to do when the options button is pressed; so we need to override the EditOptions method. In this method, we are literally just going to create a new instance of the PluginSettings and use the stored options for that plugin, then pass that to the form, as follows:

        public override void EditOptions(IWin32Window dialogOwner)
            PluginSettings options = new PluginSettings(this.Options);
            frmOptions op = new frmOptions(options);

Now, the final thing to do is actually apply these settings to the text that is put into the blog entry. For this plugin, this is simple as there is only one setting. So in the CreateContent method, we had the if statement to say that if (result == DialogResult.OK) then we make newContent = form.getText. All we will do for this plugin is add an extra if statement after that which checks whether the setting is to be applied or not, and as we have made this setting a boolean value, it makes it even easier to check. So the if statement we want is basically this:

                    if (m_defaultsettings.DefaultBoldOption)
                        newContent = string.Format("<b>{0}</b>", newContent);

And that’s it, you should be done smile_regular

As always, the source code that I have used can be downloaded here.

Any problems, please leave a comment.