diff --git a/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj b/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj index 14f49e6182..f988c58921 100644 --- a/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj +++ b/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj @@ -274,6 +274,10 @@ ApplicationSettings.xaml Code + + CreateKeyXaml.xaml + Code + DebugOptions.xaml Code @@ -908,6 +912,7 @@ + ProjectOptionPanel.cs diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/CreateKeyXaml.xaml b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/CreateKeyXaml.xaml new file mode 100644 index 0000000000..2b5fea2ff0 --- /dev/null +++ b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/CreateKeyXaml.xaml @@ -0,0 +1,74 @@ + + + + 25 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/CreateKeyXaml.xaml.cs b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/CreateKeyXaml.xaml.cs new file mode 100644 index 0000000000..a73fb06df5 --- /dev/null +++ b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/CreateKeyXaml.xaml.cs @@ -0,0 +1,170 @@ +/* + * Created by SharpDevelop. + * User: Peter Forstmeier + * Date: 01.06.2012 + * Time: 20:20 + * To change this template use Tools | Options | Coding | Edit Standard Headers. + */ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.IO; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; + +using ICSharpCode.Core; + +namespace ICSharpCode.SharpDevelop.Gui.OptionPanels +{ + /// + /// Interaction logic for CreateKeyXaml.xaml + /// + public partial class CreateKeyXaml : Window,INotifyPropertyChanged + { + + private bool checkBoxChecked; + private string keyFile; + private string baseDirectory; + + public CreateKeyXaml() + { + InitializeComponent(); + DataContext = this; + } + + public CreateKeyXaml (string baseDirectory):this() + { + this.baseDirectory = baseDirectory; + } + + + public bool CheckBoxChecked { + get { return checkBoxChecked; } + set { checkBoxChecked = value; + OnPropertyChange("CheckBoxChecked"); + } + } + + + public string KeyFile { + get { return keyFile; } + set { keyFile = value; + OnPropertyChange("KeyFile"); } + } + + /// + /// Gets the path of the "strong named" executable. This is used to create keys for strongly signing + /// .NET assemblies. + /// + public static string StrongNameTool { + get { + return FileUtility.GetSdkPath("sn.exe"); + } + } + + + public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; + + private void OnPropertyChange (string propertyName) + { + if (PropertyChanged != null) { + PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName)); + } + } + + void okButtonClick(object sender, RoutedEventArgs e) + { + KeyFile = KeyFile.Trim(); + if (KeyFile.Length == 0) { + MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.EnterKeyName}"); + return; + } + + if (CheckBoxChecked) { + if (!CheckPassword(this.passwordTextBox.Text.Trim(),this.confirmPasswordTextBox.Text.Trim())) + { + return; + } + MessageService.ShowMessage("Creating a key file with a password is currently not supported."); + return; + } + if (!KeyFile.EndsWith(".snk") && !KeyFile.EndsWith(".pfx")) + KeyFile += ".snk"; + if (CreateKey(Path.Combine(baseDirectory, KeyFile))) { + this.DialogResult = true; + Close(); + } + } + + /// + /// Creates a key with the sn.exe utility. + /// + /// The path of the key to create. + /// True if the key was created correctly. + private static bool CreateKey(string keyPath) + { + if (File.Exists(keyPath)) { + string question = "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion}"; + question = StringParser.Parse(question, new StringTagPair("fileNames", keyPath)); + if (!MessageService.AskQuestion(question, "${res:ICSharpCode.SharpDevelop.Internal.Templates.ProjectDescriptor.OverwriteQuestion.InfoName}")) { + return false; + } + } + Process p = Process.Start(StrongNameTool, "-k \"" + keyPath + "\""); + p.WaitForExit(); + if (p.ExitCode != 0) { + MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.ErrorCreatingKey}"); + return false; + } + return true; + } + + + public static bool CheckPassword(string password, string confirm) + { + +// if (password.Text.Length < 6) { +// MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.PasswordTooShort}"); +// password.Focus(); +// return false; +// } +// if (password.Text != confirm.Text) { +// MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.PasswordsDontMatch}"); +// return false; +// } + return true; + } + + +// private static bool CheckPassword(Control password, Control confirm) +// { +// password.Text = password.Text.Trim(); +// confirm.Text = confirm.Text.Trim(); +// if (password.Text.Length < 6) { +// MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.PasswordTooShort}"); +// password.Focus(); +// return false; +// } +// if (password.Text != confirm.Text) { +// MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.PasswordsDontMatch}"); +// return false; +// } +// return true; +// } + + + void cancelButtonClick(object sender, RoutedEventArgs e) + { + this.DialogResult = false; + Close(); + } + + + } +} \ No newline at end of file diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml index e3f04bfa11..e1cf3b8802 100644 --- a/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml +++ b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml @@ -15,8 +15,7 @@ Orientation="Vertical"> - + @@ -37,19 +36,13 @@ - - + + + diff --git a/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml.cs b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml.cs index a3e3c0c767..f089bc55e1 100644 --- a/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml.cs +++ b/src/Main/Base/Project/Src/Gui/Dialogs/OptionPanels/ProjectOptions/SigningXaml.xaml.cs @@ -8,14 +8,17 @@ using System; using System.Collections.Generic; using System.ComponentModel; +using System.IO; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; + using System.Windows.Input; using System.Windows.Media; +using ICSharpCode.Core; using ICSharpCode.SharpDevelop.Project; using ICSharpCode.SharpDevelop.Widgets; @@ -26,13 +29,29 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels /// public partial class SigningXaml : ProjectOptionPanel { -// private const string KeyFileExtensions = "*.snk;*.pfx;*.key"; - // private MSBuildBasedProject project; - + private const string KeyFileExtensions = "*.snk;*.pfx;*.key"; + private List keyFile = new List(); + private MSBuildBasedProject project; + + public SigningXaml() { InitializeComponent(); - + } + + + private void Initialize() + { + FindKeys(base.BaseDirectory); + SelectedKey = AssemblyOriginatorKeyFile.Value; + if (SelectedKey != null) { + if (!KeyFile.Contains(SelectedKey)) { + keyFile.Add(SelectedKey); + } + } + + keyFile.Add(StringParser.Parse("<${res:Global.CreateButtonText}...>")); + keyFile.Add(StringParser.Parse("<${res:Global.BrowseText}...>")); } public ProjectProperty SignAssembly { @@ -44,5 +63,115 @@ namespace ICSharpCode.SharpDevelop.Gui.OptionPanels get { return GetProperty("AssemblyOriginatorKeyFile","", TextBoxEditMode.EditEvaluatedProperty); } } + + public ProjectProperty DelaySign { + get { return GetProperty("DelaySign","", TextBoxEditMode.EditEvaluatedProperty); } + } + + + public ProjectProperty AssemblyOriginatorKeyMode { + get { return GetProperty("AssemblyOriginatorKeyMode","", TextBoxEditMode.EditEvaluatedProperty); } + } + + #region overrides + + protected override void Load(MSBuildBasedProject project, string configuration, string platform) + { + base.Load(project, configuration, platform); + this.project = project; + Initialize(); + } + + protected override bool Save(MSBuildBasedProject project, string configuration, string platform) + { + if (signAssemblyCheckBox.IsChecked) { + this.AssemblyOriginatorKeyFile.Value = "File"; + } + return base.Save(project, configuration, platform); + } + #endregion + + + #region keyFile + + public List KeyFile { + get { return keyFile; } + set { + keyFile = value; + base.RaisePropertyChanged(() => KeyFile); + } + } + + public string SelectedKey {get;set;} + + void FindKeys(string directory) + { + directory = FileUtility.NormalizePath(directory); + while (true) { + try { +// var files = from file in new DirectoryInfo(@"C:\").GetFiles() +// where file..Name.StartsWith("_") +// select file; + + foreach (string fileName in Directory.GetFiles(directory, "*.snk")) { + keyFile.Add(MSBuildInternals.Escape(FileUtility.GetRelativePath(base.BaseDirectory, fileName))); + } + foreach (string fileName in Directory.GetFiles(directory, "*.pfx")) { + keyFile.Add(MSBuildInternals.Escape(FileUtility.GetRelativePath(base.BaseDirectory, fileName))); + } + foreach (string fileName in Directory.GetFiles(directory, "*.key")) { + keyFile.Add(MSBuildInternals.Escape(FileUtility.GetRelativePath(base.BaseDirectory, fileName))); + } + } catch { + // can happen for networked drives / network locations + break; + } + int pos = directory.LastIndexOf(Path.DirectorySeparatorChar); + if (pos < 0) + break; + directory = directory.Substring(0, pos); + } + } + + void KeyFileComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + var cbo = (ComboBox) sender; + + if (cbo.SelectedIndex == keyFile.Count - 1) { + BrowseKeyFile(); + } + if (cbo.SelectedIndex == keyFile.Count - 2) { + CreateKeyFile(); + } + } + + + void BrowseKeyFile() + { + string str = OptionsHelper.OpenFile("${res:SharpDevelop.FileFilter.KeyFiles} (" + KeyFileExtensions + ")|" + KeyFileExtensions + "|${res:SharpDevelop.FileFilter.AllFiles}|*.*"); + if (!String.IsNullOrEmpty(str)) { + this.AssemblyOriginatorKeyFile.Value = str; + } + } + + + private void CreateKeyFile() + { + if (File.Exists(CreateKeyXaml.StrongNameTool)) { + CreateKeyXaml createKey = new CreateKeyXaml(base.BaseDirectory); + createKey.KeyFile = project.Name; + createKey.ShowDialog(); + if (createKey.DialogResult.HasValue && createKey.DialogResult.Value) + { + SelectedKey = MSBuildInternals.Escape(createKey.KeyFile); + } else{ + SelectedKey = String.Empty; + } + } else { + MessageService.ShowMessage("${res:Dialog.ProjectOptions.Signing.SNnotFound}"); + } + } + + #endregion } } \ No newline at end of file