SettingsDialog.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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));
  46. }
  47. private void OkButton_Click(object sender, RoutedEventArgs e)
  48. {
  49. if (Apply())
  50. DialogResult = true;
  51. }
  52. private void ApplyButton_Click(object sender, RoutedEventArgs e)
  53. {
  54. Apply();
  55. }
  56. private bool Apply()
  57. {
  58. var args = new List<string>();
  59. args.Add(Assembly.GetEntryAssembly().Location);
  60. foreach (GroupBox box in SettingsPanel.Children)
  61. if (((RadioButton)box.Header).IsChecked.Value)
  62. {
  63. if (box is HandlerSettingsBox)
  64. try
  65. {
  66. args.AddRange(((HandlerSettingsBox)box).Options);
  67. }
  68. catch (Exception exception)
  69. {
  70. MessageBox.Show(this, exception.Message, "SSH Handler Settings Error", MessageBoxButton.OK, MessageBoxImage.Error);
  71. return false;
  72. }
  73. break;
  74. }
  75. args.Add("\"%1\"");
  76. Debug.WriteLine("{0}", string.Join(" ", args), null);
  77. return true;
  78. }
  79. }