You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.6 KiB

  1. // Copyright (c) 2017 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.ComponentModel;
  20. using System.IO;
  21. using System.Reflection;
  22. using System.Runtime.CompilerServices;
  23. using System.Windows;
  24. using System.Windows.Input;
  25. using ICSharpCode.ILSpy.AppEnv;
  26. using ICSharpCode.ILSpy.Commands;
  27. using ICSharpCode.ILSpyX.Settings;
  28. using Microsoft.Win32;
  29. namespace ICSharpCode.ILSpy.Options
  30. {
  31. public class MiscSettingsViewModel : IMiscSettings, INotifyPropertyChanged
  32. {
  33. bool allowMultipleInstances;
  34. bool loadPreviousAssemblies = true;
  35. public MiscSettingsViewModel(MiscSettings s)
  36. {
  37. AllowMultipleInstances = s.AllowMultipleInstances;
  38. LoadPreviousAssemblies = s.LoadPreviousAssemblies;
  39. AddRemoveShellIntegrationCommand = new DelegateCommand<object>(AddRemoveShellIntegration);
  40. }
  41. /// <summary>
  42. /// Allow multiple instances.
  43. /// </summary>
  44. public bool AllowMultipleInstances {
  45. get { return allowMultipleInstances; }
  46. set {
  47. if (allowMultipleInstances != value)
  48. {
  49. allowMultipleInstances = value;
  50. OnPropertyChanged();
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// Load assemblies that were loaded in the previous instance
  56. /// </summary>
  57. public bool LoadPreviousAssemblies {
  58. get { return loadPreviousAssemblies; }
  59. set {
  60. if (loadPreviousAssemblies != value)
  61. {
  62. loadPreviousAssemblies = value;
  63. OnPropertyChanged();
  64. }
  65. }
  66. }
  67. public ICommand AddRemoveShellIntegrationCommand { get; }
  68. const string rootPath = @"Software\Classes\{0}\shell";
  69. const string fullPath = @"Software\Classes\{0}\shell\Open with ILSpy\command";
  70. private void AddRemoveShellIntegration(object obj)
  71. {
  72. string commandLine = CommandLineTools.ArgumentArrayToCommandLine(Path.ChangeExtension(Assembly.GetEntryAssembly().Location, ".exe")) + " \"%L\"";
  73. if (RegistryEntriesExist())
  74. {
  75. if (MessageBox.Show(string.Format(Properties.Resources.RemoveShellIntegrationMessage, commandLine), "ILSpy", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  76. {
  77. Registry.CurrentUser.CreateSubKey(string.Format(rootPath, "dllfile")).DeleteSubKeyTree("Open with ILSpy");
  78. Registry.CurrentUser.CreateSubKey(string.Format(rootPath, "exefile")).DeleteSubKeyTree("Open with ILSpy");
  79. }
  80. }
  81. else
  82. {
  83. if (MessageBox.Show(string.Format(Properties.Resources.AddShellIntegrationMessage, commandLine), "ILSpy", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
  84. {
  85. Registry.CurrentUser.CreateSubKey(string.Format(fullPath, "dllfile"))?
  86. .SetValue("", commandLine);
  87. Registry.CurrentUser.CreateSubKey(string.Format(fullPath, "exefile"))?
  88. .SetValue("", commandLine);
  89. }
  90. }
  91. OnPropertyChanged(nameof(AddRemoveShellIntegrationText));
  92. }
  93. private static bool RegistryEntriesExist()
  94. {
  95. return Registry.CurrentUser.OpenSubKey(string.Format(fullPath, "dllfile")) != null
  96. && Registry.CurrentUser.OpenSubKey(string.Format(fullPath, "exefile")) != null;
  97. }
  98. public string AddRemoveShellIntegrationText {
  99. get {
  100. return RegistryEntriesExist() ? Properties.Resources.RemoveShellIntegration : Properties.Resources.AddShellIntegration;
  101. }
  102. }
  103. #region INotifyPropertyChanged Implementation
  104. public event PropertyChangedEventHandler PropertyChanged;
  105. protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
  106. {
  107. PropertyChanged?.Invoke(this, e);
  108. }
  109. protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
  110. {
  111. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  112. }
  113. #endregion
  114. }
  115. }