Browse Source

Parse options into settings dialog and ouput options back.

Douglas Thrift 9 years ago
parent
commit
18e3e1d75d

+ 21 - 0
ssh-handler/HandlerSettingsBox.xaml.cs

@@ -20,6 +20,7 @@
  */
 
 using System.Collections.Generic;
+using System.Text.RegularExpressions;
 using System.Windows.Controls;
 
 public partial class HandlerSettingsBox : GroupBox
@@ -46,6 +47,26 @@ public partial class HandlerSettingsBox : GroupBox
                 SettingsPanel.Children.Add(new OptionalYesNoDirectoryPanel(setting, options));
                 break;
             }
+
+        Regex regex = new Regex(@"^(?:/|--?)" + handler.Setting.option.Substring(1) + @"(?:[:=].*)?$");
+
+        foreach (string option in options)
+            if (regex.IsMatch(option))
+                HandlerRadioButton.IsChecked = true;
+    }
+
+    public IEnumerable<string> Options
+    {
+        get
+        {
+            var options = new List<string>();
+
+            foreach (SettingPanel panel in SettingsPanel.Children)
+                if (panel.IsSelected)
+                    options.Add(panel.Option);
+
+            return options;
+        }
     }
 
     private void HandlerRadioButton_Checked(object sender, System.Windows.RoutedEventArgs e)

+ 46 - 1
ssh-handler/OptionalExecutablePanel.xaml.cs

@@ -19,10 +19,13 @@
  *  limitations under the License.
  */
 
+using System;
 using System.Collections.Generic;
+using System.IO;
+using System.Text.RegularExpressions;
 using System.Windows.Controls;
 
-public partial class OptionalExecutablePanel : StackPanel
+public partial class OptionalExecutablePanel : StackPanel, SettingPanel
 {
     private Setting setting;
 
@@ -39,6 +42,48 @@ public partial class OptionalExecutablePanel : StackPanel
             SettingCheckBox.Content = setting.name + " Executable:";
             SettingUsage.Text = setting.usage + ":";
         }
+
+        Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<executable>.*))?$");
+
+        foreach (string option in options)
+        {
+            Match match = regex.Match(option);
+            if (match.Success)
+            {
+                Group group = match.Groups["executable"];
+                if (group.Success)
+                {
+                    SettingCheckBox.IsChecked = true;
+                    SettingExecutableBox.Text = group.Value;
+                }
+            }
+        }
+    }
+
+    public bool IsSelected
+    {
+        get
+        {
+            return setting.handler || SettingCheckBox.IsChecked.Value;
+        }
+    }
+
+    public string Option
+    {
+        get
+        {
+            if (SettingCheckBox.IsChecked.Value)
+            {
+                string executable = SettingExecutableBox.Text;
+
+                if (File.GetAttributes(executable).HasFlag(FileAttributes.Directory))
+                    throw new Exception("'" + executable + "' is not a file.");
+
+                return setting.option + ":" + executable;
+            }
+            else
+                return setting.option;
+        }
     }
 
     private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)

+ 59 - 1
ssh-handler/OptionalYesNoDirectoryPanel.xaml.cs

@@ -19,10 +19,13 @@
  *  limitations under the License.
  */
 
+using System;
 using System.Collections.Generic;
+using System.IO;
+using System.Text.RegularExpressions;
 using System.Windows.Controls;
 
-public partial class OptionalYesNoDirectoryPanel : StackPanel
+public partial class OptionalYesNoDirectoryPanel : StackPanel, SettingPanel
 {
     private Setting setting;
 
@@ -33,6 +36,61 @@ public partial class OptionalYesNoDirectoryPanel : StackPanel
         this.setting = setting;
         SettingCheckBox.Content = setting.name + ":";
         SettingUsage.Text = setting.usage + ":";
+
+        Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<directory>.*))?$");
+
+        foreach (string option in options)
+        {
+            Match match = regex.Match(option);
+            if (match.Success)
+            {
+                Group group = match.Groups["directory"];
+                if (group.Success)
+                    switch (group.Value)
+                    {
+                    case "yes":
+                        SettingYes.IsChecked = true;
+                        break;
+                    case "no":
+                        SettingNo.IsChecked = true;
+                        break;
+                    default:
+                        SettingDirectory.IsChecked = true;
+                        SettingDirectoryBox.Text = group.Value;
+                        break;
+                    }
+
+                SettingCheckBox.IsChecked = true;
+            }
+        }
+    }
+
+    public bool IsSelected
+    {
+        get
+        {
+            return SettingCheckBox.IsChecked.Value;
+        }
+    }
+
+    public string Option
+    {
+        get
+        {
+            if (SettingYes.IsChecked.Value)
+                return setting.option + ":yes";
+            else if (SettingNo.IsChecked.Value)
+                return setting.option + ":no";
+            else
+            {
+                string directory = SettingDirectoryBox.Text;
+
+                if (!File.GetAttributes(directory).HasFlag(FileAttributes.Directory))
+                    throw new Exception("'" + directory + "' is not a directory.");
+
+                return setting.option + ":" + directory;
+            }
+        }
     }
 
     private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)

+ 59 - 1
ssh-handler/OptionalYesNoExecutablePanel.xaml.cs

@@ -19,10 +19,13 @@
  *  limitations under the License.
  */
 
+using System;
 using System.Collections.Generic;
+using System.IO;
+using System.Text.RegularExpressions;
 using System.Windows.Controls;
 
-public partial class OptionalYesNoExecutablePanel : StackPanel
+public partial class OptionalYesNoExecutablePanel : StackPanel, SettingPanel
 {
     private Setting setting;
 
@@ -33,6 +36,61 @@ public partial class OptionalYesNoExecutablePanel : StackPanel
         this.setting = setting;
         SettingCheckBox.Content = setting.name + ":";
         SettingUsage.Text = setting.usage + ":";
+
+        Regex regex = new Regex(@"^(?:/|--?)" + setting.option.Substring(1) + @"(?:[:=](?<executable>.*))?$");
+
+        foreach (string option in options)
+        {
+            Match match = regex.Match(option);
+            if (match.Success)
+            {
+                Group group = match.Groups["executable"];
+                if (group.Success)
+                    switch (group.Value)
+                    {
+                    case "yes":
+                        SettingYes.IsChecked = true;
+                        break;
+                    case "no":
+                        SettingNo.IsChecked = true;
+                        break;
+                    default:
+                        SettingExecutable.IsChecked = true;
+                        SettingExecutableBox.Text = group.Value;
+                        break;
+                    }
+
+                SettingCheckBox.IsChecked = true;
+            }
+        }
+    }
+
+    public bool IsSelected
+    {
+        get
+        {
+            return SettingCheckBox.IsChecked.Value;
+        }
+    }
+
+    public string Option
+    {
+        get
+        {
+            if (SettingYes.IsChecked.Value)
+                return setting.option + ":yes";
+            else if (SettingNo.IsChecked.Value)
+                return setting.option + ":no";
+            else
+            {
+                string executable = SettingExecutableBox.Text;
+
+                if (File.GetAttributes(executable).HasFlag(FileAttributes.Directory))
+                    throw new Exception("'" + executable + "' is not a file.");
+
+                return setting.option + ":" + executable;
+            }
+        }
     }
 
     private void SettingCheckBox_Checked(object sender, System.Windows.RoutedEventArgs e)

+ 26 - 0
ssh-handler/SettingPanel.cs

@@ -0,0 +1,26 @@
+// Setting Panel
+//
+// Douglas Thrift
+//
+// SettingPanel.cs
+
+/*  Copyright 2014 Douglas Thrift
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+public interface SettingPanel
+{
+    bool IsSelected { get; }
+    string Option { get; }
+}

+ 28 - 6
ssh-handler/SettingsDialog.xaml.cs

@@ -19,6 +19,7 @@
  *  limitations under the License.
  */
 
+using System;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
@@ -53,8 +54,8 @@ public partial class SettingsDialog : Window
 
     private void OkButton_Click(object sender, RoutedEventArgs e)
     {
-        Apply();
-        DialogResult = true;
+        if (Apply())
+            DialogResult = true;
     }
 
     private void ApplyButton_Click(object sender, RoutedEventArgs e)
@@ -62,11 +63,32 @@ public partial class SettingsDialog : Window
         Apply();
     }
 
-    private void Apply()
+    private bool Apply()
     {
-        string program = Assembly.GetEntryAssembly().Location;
-        string[] arguments = { "/openssh", "/bash" };
+        var args = new List<string>();
+
+        args.Add(Assembly.GetEntryAssembly().Location);
+
+        foreach (GroupBox box in SettingsPanel.Children)
+            if (((RadioButton)box.Header).IsChecked.Value)
+            {
+                if (box is HandlerSettingsBox)
+                    try
+                    {
+                        args.AddRange(((HandlerSettingsBox)box).Options);
+                    }
+                    catch (Exception exception)
+                    {
+                        MessageBox.Show(this, exception.Message, "SSH Handler Settings Error", MessageBoxButton.OK, MessageBoxImage.Error);
+                        return false;
+                    }
+                break;
+            }
+
+        args.Add("\"%1\"");
+
+        Debug.WriteLine("{0}", string.Join(" ", args), null);
 
-        Debug.WriteLine("\"{0}\" {1} \"%1\"", program, string.Join(" ", arguments));
+        return true;
     }
 }

+ 1 - 0
ssh-handler/ssh-handler.csproj

@@ -93,6 +93,7 @@
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="PuttyHandler.cs" />
     <Compile Include="Setting.cs" />
+    <Compile Include="SettingPanel.cs" />
     <Compile Include="SettingType.cs" />
     <Compile Include="Shell32.cs" />
     <Compile Include="SshHandler.cs" />