SettingsDialog.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Settings Dialog
  2. //
  3. // Douglas Thrift
  4. //
  5. // SettingsDialog.xaml.cs
  6. /* Copyright 2014 Douglas Thrift
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Diagnostics;
  23. using System.Linq;
  24. using System.Reflection;
  25. using System.Security.Principal;
  26. using System.Windows;
  27. using System.Windows.Controls;
  28. using Microsoft.Win32;
  29. public partial class SettingsDialog : Window
  30. {
  31. private bool global = false;
  32. public SettingsDialog(IList<Handler> handlers, string type)
  33. {
  34. switch (type.ToLowerInvariant())
  35. {
  36. case "global":
  37. global = true;
  38. if (!new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator))
  39. Elevate();
  40. break;
  41. }
  42. InitializeComponent();
  43. Title = "SSH Handler " + (global ? "Global" : "User") + " Settings";
  44. IEnumerable<string> options;
  45. if (global)
  46. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default), key = baseKey.OpenSubKey(@"Software\Classes\ssh\shell\open\command"))
  47. options = Options(key);
  48. else
  49. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default), key = baseKey.OpenSubKey(@"ssh\shell\open\command"))
  50. options = Options(key);
  51. foreach (Handler handler in handlers)
  52. SettingsPanel.Children.Add(new HandlerSettingsBox(handler, options, ApplyButton));
  53. ApplyButton.IsEnabled = false;
  54. }
  55. private void OkButton_Click(object sender, RoutedEventArgs e)
  56. {
  57. if (Apply())
  58. DialogResult = true;
  59. }
  60. private void ApplyButton_Click(object sender, RoutedEventArgs e)
  61. {
  62. if (Apply())
  63. ApplyButton.IsEnabled = false;
  64. }
  65. private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
  66. {
  67. ApplyButton.IsEnabled = true;
  68. }
  69. private void Elevate()
  70. {
  71. ProcessStartInfo info = new ProcessStartInfo(Assembly.GetEntryAssembly().Location, "/settings:" + (global ? "global" : "user"));
  72. info.Verb = "runas";
  73. Process.Start(info);
  74. Environment.Exit(0);
  75. }
  76. private IEnumerable<string> Options(RegistryKey key)
  77. {
  78. if (key != null)
  79. {
  80. string command = (string)key.GetValue(null);
  81. if (!string.IsNullOrWhiteSpace(command))
  82. return Shell32.CommandLineToArgv(command).Skip(1).TakeWhile(arg => arg != "%1");
  83. }
  84. return new string[0];
  85. }
  86. private bool Apply()
  87. {
  88. var args = new List<string>();
  89. args.Add(Assembly.GetEntryAssembly().Location);
  90. foreach (GroupBox box in SettingsPanel.Children)
  91. if (((RadioButton)box.Header).IsChecked.Value)
  92. {
  93. if (box is HandlerSettingsBox)
  94. try
  95. {
  96. args.AddRange(((HandlerSettingsBox)box).Options);
  97. }
  98. catch (Exception exception)
  99. {
  100. MessageBox.Show(this, exception.Message, "SSH Handler Settings Error", MessageBoxButton.OK, MessageBoxImage.Error);
  101. return false;
  102. }
  103. break;
  104. }
  105. args.Add("\"%1\"");
  106. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(global ? RegistryHive.LocalMachine : RegistryHive.CurrentUser, RegistryView.Default), key = baseKey.CreateSubKey(@"Software\Classes\ssh"), commandKey = key.CreateSubKey(@"shell\open\command"))
  107. {
  108. key.SetValue(null, "URL:SSH Protocol");
  109. key.SetValue("URL Protocol", "");
  110. commandKey.SetValue(null, string.Join(" ", args));
  111. }
  112. return true;
  113. }
  114. }