SettingsDialog.xaml.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Windows;
  26. using System.Windows.Controls;
  27. using Microsoft.Win32;
  28. public partial class SettingsDialog : Window
  29. {
  30. public SettingsDialog(IList<Handler> handlers)
  31. {
  32. InitializeComponent();
  33. IEnumerable<string> options = new string[0];
  34. using (RegistryKey baseKey = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Default), key = baseKey.OpenSubKey(@"ssh\shell\open\command"))
  35. if (key != null)
  36. {
  37. string command = (string)key.GetValue(null);
  38. if (!string.IsNullOrWhiteSpace(command))
  39. {
  40. var args = Shell32.CommandLineToArgv(command);
  41. options = args.Skip(1).TakeWhile(arg => arg != "%1");
  42. }
  43. }
  44. foreach (Handler handler in handlers)
  45. SettingsPanel.Children.Add(new HandlerSettingsBox(handler, options, ApplyButton));
  46. ApplyButton.IsEnabled = false;
  47. }
  48. private void OkButton_Click(object sender, RoutedEventArgs e)
  49. {
  50. if (Apply())
  51. DialogResult = true;
  52. }
  53. private void ApplyButton_Click(object sender, RoutedEventArgs e)
  54. {
  55. if (Apply())
  56. ApplyButton.IsEnabled = false;
  57. }
  58. private void RadioButton_Unchecked(object sender, RoutedEventArgs e)
  59. {
  60. ApplyButton.IsEnabled = true;
  61. }
  62. private bool Apply()
  63. {
  64. var args = new List<string>();
  65. args.Add(Assembly.GetEntryAssembly().Location);
  66. foreach (GroupBox box in SettingsPanel.Children)
  67. if (((RadioButton)box.Header).IsChecked.Value)
  68. {
  69. if (box is HandlerSettingsBox)
  70. try
  71. {
  72. args.AddRange(((HandlerSettingsBox)box).Options);
  73. }
  74. catch (Exception exception)
  75. {
  76. MessageBox.Show(this, exception.Message, "SSH Handler Settings Error", MessageBoxButton.OK, MessageBoxImage.Error);
  77. return false;
  78. }
  79. break;
  80. }
  81. args.Add("\"%1\"");
  82. Debug.WriteLine("{0}", string.Join(" ", args), null);
  83. return true;
  84. }
  85. }