/// Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Globalization;
using System.IO;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;
namespace Microsoft.VisualStudio.Project
{
internal partial class SecurityWarningDialog : Form
{
#region fields
///
/// The associated service provider
///
private IServiceProvider serviceProvider;
///
/// The dialog message to be presented when the 'More' button is pressed.
///
private string dialogMessage;
///
/// Teh full path to teh project.
///
private string projectFullPath;
///
/// The value of the ask again check box.
///
private bool askAgainCheckBoxValue;
///
/// The project load option the userwill choose on this form.
///
private ProjectLoadOption projectLoadOption = ProjectLoadOption.DonNotLoad;
#endregion
#region properties
///
/// The value of the ask again check box.
///
internal bool AskAgainCheckBoxValue
{
get
{
return this.askAgainCheckBoxValue;
}
}
///
/// The project load option the user has chosen to perform.
///
internal ProjectLoadOption ProjectLoadOption
{
get
{
return this.projectLoadOption;
}
}
#endregion
#region ctors
///
/// Overloaded ctor.
///
/// The associated service provider.
/// The message that will be shown when the 'More' button is pressed.
/// The full path of the project.
public SecurityWarningDialog(IServiceProvider serviceProvider, string dialogMessage, string projectFullpath)
{
if(serviceProvider == null)
{
throw new ArgumentNullException("serviceProvider");
}
if(String.IsNullOrEmpty(projectFullpath))
{
throw new ArgumentException(SR.GetString(SR.ParameterCannotBeNullOrEmpty, CultureInfo.CurrentUICulture), "projectFullpath");
}
this.serviceProvider = serviceProvider;
this.projectFullPath = projectFullpath;
this.dialogMessage = dialogMessage;
this.InitializeComponent();
this.SetupComponents();
}
#endregion
#region helpers
///
/// Shows the dialog if possible hosted by the IUIService.
///
/// A DialogResult
internal new DialogResult ShowDialog()
{
IUIService uiService = this.serviceProvider.GetService(typeof(IUIService)) as IUIService;
if(uiService == null)
{
return this.ShowDialog();
}
return uiService.ShowDialog(this);
}
///
/// Sets up the different UI elements.
///
private void SetupComponents()
{
// Get the project name.
string projectName = Path.GetFileNameWithoutExtension(this.projectFullPath);
IVsUIShell shell = this.serviceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell;
if(shell == null)
{
throw new InvalidOperationException();
}
String applicationName;
// Get the name of the SKU.
ErrorHandler.ThrowOnFailure(shell.GetAppName(out applicationName));
// Set the dialog box caption (title).
this.Text = String.Format(CultureInfo.CurrentCulture, this.Text, projectName);
// Set the text at the top of the dialog that gives a brief description of the security
// implications of loading this project.
this.warningText.Text = String.Format(CultureInfo.CurrentCulture, this.warningText.Text, projectName, applicationName);
// Set the text that describes the "Browse" option.
this.browseText.Text = String.Format(CultureInfo.CurrentCulture, this.browseText.Text, applicationName);
// Set the text that describes the "Load" option.
this.loadText.Text = String.Format(CultureInfo.CurrentCulture, this.loadText.Text, applicationName);
// The default selection is "Browse" so select that radio button.
this.browseButton.Checked = true;
// Turn on the "Ask me always" checkbox by default.
this.askAgainCheckBox.Checked = true;
// Set the focus to the Browse button, so hitting Enter will press the OK button
this.browseButton.Focus();
this.CenterToScreen();
}
///
/// The Cancel button was clicked.
///
/// The sender of the event
/// An event arg Associated to the event.
private void cancelButton_Click(object sender, EventArgs e)
{
// In case the user presses the Cancel button, we assume he never wants to see this dialog again
// and pretend the "Ask me every time" checkbox is unchecked even if it is really checked.
this.askAgainCheckBoxValue = false;
this.projectLoadOption = ProjectLoadOption.DonNotLoad;
}
///
/// The OK button was clicked.
///
/// The sender of the event
/// An event arg Associated to the event.
private void okButton_Click(object sender, EventArgs e)
{
if(this.browseButton.Checked && !this.loadButton.Checked)
{
this.projectLoadOption = ProjectLoadOption.LoadOnlyForBrowsing;
}
else
{
this.projectLoadOption = ProjectLoadOption.LoadNormally;
}
this.askAgainCheckBoxValue = this.askAgainCheckBox.Checked;
this.Close();
}
///
/// Loads a messagebox explaining in detail the security problem
///
/// The sender of the event
/// An event arg Associated to the event.
private void detailsButton_Click(object sender, EventArgs e)
{
string title = null;
OLEMSGICON icon = OLEMSGICON.OLEMSGICON_INFO;
OLEMSGBUTTON buttons = OLEMSGBUTTON.OLEMSGBUTTON_OK;
OLEMSGDEFBUTTON defaultButton = OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST;
VsShellUtilities.ShowMessageBox(this.serviceProvider, title, this.dialogMessage, icon, buttons, defaultButton);
}
#endregion
}
}