/// <summary> /// Executes the current extension. /// </summary> /// <param name="shell">The <see cref="IShell" /> object on which the current extension will be executed.</param> protected async override void DoExecute(IShell shell) { if (shell.HasActiveDocument) { await SafeExecutionContext.ExecuteAsync((Form)shell.Owner, async() => { var blogSetting = this.SettingProvider.GetExtensionSetting <BlogSetting>(); if (string.IsNullOrEmpty(blogSetting.MetaWeblogAddress) || string.IsNullOrEmpty(blogSetting.UserName) || string.IsNullOrEmpty(blogSetting.Password)) { MessageBox.Show(Resources.MissingBlogConfigurationMsg, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } var gateway = new BlogGateway(blogSetting.MetaWeblogAddress, blogSetting.UserName, blogSetting.Password); if (await gateway.TestConnectionAsync()) { var blogPublishDialog = new FrmBlogPublish(gateway); if (blogPublishDialog.ShowDialog() == DialogResult.OK) { var selectedCategories = blogPublishDialog.SelectedCategories.Select(s => s.Title).ToList(); var postInfo = new PostInfo { Categories = selectedCategories, DateCreated = DateTime.Now, Description = HtmlUtilities.Tidy(HtmlUtilities.ReplaceFileSystemImages(shell.Note.Content)), Title = shell.Note.Title }; await gateway.PublishBlog(postInfo, selectedCategories); shell.StatusText = Resources.PublishSucceeded; } } else { MessageBox.Show(Resources.BlogExtensionCannotConnectToBlogService, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }); } else { MessageBox.Show(Resources.NoActiveNoteOpened, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private async void btnTestConnection_Click(object sender, EventArgs e) { bool hasError = false; errorProvider.Clear(); if (string.IsNullOrEmpty(txtMetaWeblogAddress.Text)) { errorProvider.SetError(txtMetaWeblogAddress, "Please input the MetaWeblog address."); hasError = true; } if (string.IsNullOrEmpty(txtUserName.Text)) { errorProvider.SetError(txtUserName, "Please input the user name."); hasError = true; } if (string.IsNullOrEmpty(txtPassword.Text)) { errorProvider.SetError(txtPassword, "Please input the password."); hasError = true; } if (hasError) { return; } await SafeExecutionContext.ExecuteAsync(this.ParentForm, async() => { var gateway = new BlogGateway(txtMetaWeblogAddress.Text, txtUserName.Text, txtPassword.Text); if (await gateway.TestConnectionAsync()) { MessageBox.Show(Resources.TestConnectionSucceeded, Resources.Information, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show(Resources.TestConnectionFailed, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }); }