Пример #1
0
        public void ShowFileOpenDialog()
        {
            string[] fileNames;
            List <Agent.KeyConstraint> constraints = new List <Agent.KeyConstraint>();

            if (mAgent is PageantClient)
            {
                // Client Mode with Pageant - Show standard file dialog since we don't
                // need / can't use constraints

                using (var openFileDialog = new OpenFileDialog()) {
                    openFileDialog.Multiselect = true;
                    openFileDialog.Filter      = string.Join("|",
                                                             Strings.filterPuttyPrivateKeyFiles, "*.ppk",
                                                             Strings.filterAllFiles, "*.*");

                    var result = openFileDialog.ShowDialog();
                    if (result != DialogResult.OK)
                    {
                        return;
                    }
                    fileNames = openFileDialog.FileNames;
                }
            }
            else if (CommonOpenFileDialog.IsPlatformSupported)
            {
                // Windows Vista/7/8 has new style file open dialog that can be extended
                // using the Windows API via the WindowsAPICodepack library

                var win7OpenFileDialog = new CommonOpenFileDialog();
                win7OpenFileDialog.Multiselect      = true;
                win7OpenFileDialog.EnsureFileExists = true;

                var confirmConstraintCheckBox =
                    new CommonFileDialogCheckBox(cConfirmConstraintCheckBox,
                                                 "Require user confirmation");
                var lifetimeConstraintTextBox =
                    new CommonFileDialogTextBox(cLifetimeConstraintTextBox, string.Empty);
                lifetimeConstraintTextBox.Visible = false;
                var lifetimeConstraintCheckBox =
                    new CommonFileDialogCheckBox(cLifetimeConstraintCheckBox,
                                                 "Set lifetime (in seconds)");
                lifetimeConstraintCheckBox.CheckedChanged += (s, e) => {
                    lifetimeConstraintTextBox.Visible =
                        lifetimeConstraintCheckBox.IsChecked;
                };

                var confirmConstraintGroupBox  = new CommonFileDialogGroupBox();
                var lifetimeConstraintGroupBox = new CommonFileDialogGroupBox();

                confirmConstraintGroupBox.Items.Add(confirmConstraintCheckBox);
                lifetimeConstraintGroupBox.Items.Add(lifetimeConstraintCheckBox);
                lifetimeConstraintGroupBox.Items.Add(lifetimeConstraintTextBox);

                win7OpenFileDialog.Controls.Add(confirmConstraintGroupBox);
                win7OpenFileDialog.Controls.Add(lifetimeConstraintGroupBox);

                var filter = new CommonFileDialogFilter(
                    Strings.filterPuttyPrivateKeyFiles, "*.ppk");
                win7OpenFileDialog.Filters.Add(filter);
                filter = new CommonFileDialogFilter(Strings.filterAllFiles, "*.*");
                win7OpenFileDialog.Filters.Add(filter);

                win7OpenFileDialog.FileOk += win7OpenFileDialog_FileOk;

                /* add help listeners to win7OpenFileDialog */

                // declare variables here so that the GC does not eat them.
                WndProcDelegate newWndProc, oldWndProc = null;
                win7OpenFileDialog.DialogOpening += (sender, e) =>
                {
                    var hwnd = win7OpenFileDialog.GetWindowHandle();

                    // hook into WndProc to catch WM_HELP, i.e. user pressed F1
                    newWndProc = (hWnd, msg, wParam, lParam) =>
                    {
                        const short shellHelpCommand = 0x7091;

                        var win32Msg = (Win32Types.Msg)msg;
                        switch (win32Msg)
                        {
                        case Win32Types.Msg.WM_HELP:
                            var helpInfo = (HELPINFO)Marshal.PtrToStructure(lParam, typeof(HELPINFO));
                            // Ignore if we are on an unknown control or control 100.
                            // These are the windows shell control. The help command is
                            // issued by these controls so by not ignoring, we would call
                            // the help method twice.
                            if (helpInfo.iCtrlId != 0 && helpInfo.iCtrlId != 100)
                            {
                                OnAddFromFileHelpRequested(win7OpenFileDialog, EventArgs.Empty);
                            }
                            return((IntPtr)1); // TRUE

                        case Win32Types.Msg.WM_COMMAND:
                            var wParamBytes = BitConverter.GetBytes(wParam.ToInt32());
                            var highWord    = BitConverter.ToInt16(wParamBytes, 0);
                            var lowWord     = BitConverter.ToInt16(wParamBytes, 2);
                            if (lowWord == 0 && highWord == shellHelpCommand)
                            {
                                OnAddFromFileHelpRequested(win7OpenFileDialog, EventArgs.Empty);
                                return((IntPtr)0);
                            }
                            break;
                        }
                        return(CallWindowProc(oldWndProc, hwnd, msg, wParam, lParam));
                    };
                    var newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);
                    var oldWndProcPtr = SetWindowLongPtr(hwnd, WindowLongFlags.GWL_WNDPROC, newWndProcPtr);
                    oldWndProc = (WndProcDelegate)
                                 Marshal.GetDelegateForFunctionPointer(oldWndProcPtr, typeof(WndProcDelegate));
                };

                var result = win7OpenFileDialog.ShowDialog();
                if (result != CommonFileDialogResult.Ok)
                {
                    return;
                }
                if (confirmConstraintCheckBox.IsChecked)
                {
                    var constraint = new Agent.KeyConstraint();
                    constraint.Type = Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM;
                    constraints.Add(constraint);
                }
                if (lifetimeConstraintCheckBox.IsChecked)
                {
                    // error checking for parse done in fileOK event handler
                    uint lifetime   = uint.Parse(lifetimeConstraintTextBox.Text);
                    var  constraint = new Agent.KeyConstraint();
                    constraint.Type = Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME;
                    constraint.Data = lifetime;
                    constraints.Add(constraint);
                }
                fileNames = win7OpenFileDialog.FileNames.ToArray();
            }
            else
            {
                using (var openFileDialog = new OpenFileDialog())
                {
                    openFileDialog.Multiselect = true;
                    openFileDialog.Filter      = string.Join("|",
                                                             Strings.filterPuttyPrivateKeyFiles, "*.ppk",
                                                             Strings.filterAllFiles, "*.*");

                    openFileDialog.FileOk += xpOpenFileDialog_FileOk;

                    // Windows XP uses old style file open dialog that can be extended
                    // using the Windows API via FileDlgExtenders library
                    XPOpenFileDialog xpOpenFileDialog = null;
                    if (Type.GetType("Mono.Runtime") == null)
                    {
                        xpOpenFileDialog = new XPOpenFileDialog();
                        xpOpenFileDialog.FileDlgStartLocation = AddonWindowLocation.Bottom;
                        mOpenFileDialogMap.Add(openFileDialog, xpOpenFileDialog);
                    }

                    openFileDialog.HelpRequest += OnAddFromFileHelpRequested;
                    // TODO: technically, a listener could be added after this
                    openFileDialog.ShowHelp = AddFromFileHelpRequested != null;

                    var result = xpOpenFileDialog == null?
                                 openFileDialog.ShowDialog() :
                                     openFileDialog.ShowDialog(xpOpenFileDialog, null);

                    if (result != DialogResult.OK)
                    {
                        return;
                    }

                    if (xpOpenFileDialog == null)
                    {
                        // If dialog could not be extended, then we add constraints by holding
                        // down the control key when clicking the Open button.
                        if (Control.ModifierKeys.HasFlag(Keys.Control))
                        {
                            var constraintDialog = new ConstraintsInputDialog();
                            constraintDialog.ShowDialog();
                            if (constraintDialog.DialogResult == DialogResult.OK)
                            {
                                if (constraintDialog.ConfirmConstraintChecked)
                                {
                                    constraints.AddConfirmConstraint();
                                }
                                if (constraintDialog.LifetimeConstraintChecked)
                                {
                                    constraints.AddLifetimeConstraint(constraintDialog.LifetimeDuration);
                                }
                            }
                        }
                    }
                    else
                    {
                        mOpenFileDialogMap.Remove(openFileDialog);

                        if (xpOpenFileDialog.UseConfirmConstraintChecked)
                        {
                            constraints.AddConfirmConstraint();
                        }
                        if (xpOpenFileDialog.UseLifetimeConstraintChecked)
                        {
                            constraints.AddLifetimeConstraint
                                (xpOpenFileDialog.LifetimeConstraintDuration);
                        }
                    }
                    fileNames = openFileDialog.FileNames;
                }
            }
            UseWaitCursor = true;
            mAgent.AddKeysFromFiles(fileNames, constraints);
            if (!(mAgent is Agent))
            {
                ReloadKeyListView();
            }
            UseWaitCursor = false;
        }
Пример #2
0
        public void ShowFileOpenDialog()
        {
            string[] fileNames;
              List<Agent.KeyConstraint> constraints = new List<Agent.KeyConstraint>();
              if (mAgent is PageantClient) {
            // Client Mode with Pageant - Show standard file dialog since we don't
            // need / can't use constraints

            using (var openFileDialog = new OpenFileDialog()) {
              openFileDialog.Multiselect = true;
              openFileDialog.Filter = string.Join ("|",
            Strings.filterPuttyPrivateKeyFiles, "*.ppk",
            Strings.filterAllFiles, "*.*");

              var result = openFileDialog.ShowDialog ();
              if (result != DialogResult.OK) {
            return;
              }
              fileNames = openFileDialog.FileNames;
            }
              } else if (CommonOpenFileDialog.IsPlatformSupported) {
            // Windows Vista/7/8 has new style file open dialog that can be extended
            // using the Windows API via the WindowsAPICodepack library

            var win7OpenFileDialog = new CommonOpenFileDialog ();
            win7OpenFileDialog.Multiselect = true;
            win7OpenFileDialog.EnsureFileExists = true;

            var confirmConstraintCheckBox =
              new CommonFileDialogCheckBox (cConfirmConstraintCheckBox,
              "Require user confirmation");
            var lifetimeConstraintTextBox =
              new CommonFileDialogTextBox (cLifetimeConstraintTextBox, string.Empty);
            lifetimeConstraintTextBox.Visible = false;
            var lifetimeConstraintCheckBox =
              new CommonFileDialogCheckBox (cLifetimeConstraintCheckBox,
              "Set lifetime (in seconds)");
            lifetimeConstraintCheckBox.CheckedChanged +=
              delegate(object aSender, EventArgs aEventArgs) {
              lifetimeConstraintTextBox.Visible =
              lifetimeConstraintCheckBox.IsChecked;
            };

            var confirmConstraintGroupBox = new CommonFileDialogGroupBox ();
            var lifetimeConstraintGroupBox = new CommonFileDialogGroupBox ();

            confirmConstraintGroupBox.Items.Add (confirmConstraintCheckBox);
            lifetimeConstraintGroupBox.Items.Add (lifetimeConstraintCheckBox);
            lifetimeConstraintGroupBox.Items.Add (lifetimeConstraintTextBox);

            win7OpenFileDialog.Controls.Add (confirmConstraintGroupBox);
            win7OpenFileDialog.Controls.Add (lifetimeConstraintGroupBox);

            var filter = new CommonFileDialogFilter (
              Strings.filterPuttyPrivateKeyFiles, "*.ppk");
            win7OpenFileDialog.Filters.Add (filter);
            filter = new CommonFileDialogFilter (Strings.filterAllFiles, "*.*");
            win7OpenFileDialog.Filters.Add (filter);

            win7OpenFileDialog.FileOk += win7OpenFileDialog_FileOk;

            /* add help listeners to win7OpenFileDialog */

            // declare variables here so that the GC does not eat them.
            WndProcDelegate newWndProc, oldWndProc = null;
            win7OpenFileDialog.DialogOpening += (sender, e) =>
            {
              var hwnd = win7OpenFileDialog.GetWindowHandle();

              // hook into WndProc to catch WM_HELP, i.e. user pressed F1
              newWndProc = (hWnd, msg, wParam, lParam) =>
              {
            const short shellHelpCommand = 0x7091;

            var win32Msg = (Win32Types.Msg)msg;
            switch (win32Msg) {
              case Win32Types.Msg.WM_HELP:
                var helpInfo = (HELPINFO)Marshal.PtrToStructure(lParam, typeof(HELPINFO));
                // Ignore if we are on an unknown control or control 100.
                // These are the windows shell control. The help command is
                // issued by these controls so by not ignoring, we would call
                // the help method twice.
                if (helpInfo.iCtrlId != 0 && helpInfo.iCtrlId != 100)
                  OnAddFromFileHelpRequested(win7OpenFileDialog, EventArgs.Empty);
                return (IntPtr)1; // TRUE
              case Win32Types.Msg.WM_COMMAND:
                var wParamBytes = BitConverter.GetBytes(wParam.ToInt32());
                var highWord = BitConverter.ToInt16(wParamBytes, 0);
                var lowWord = BitConverter.ToInt16(wParamBytes, 2);
                if (lowWord == 0 && highWord == shellHelpCommand) {
                  OnAddFromFileHelpRequested(win7OpenFileDialog, EventArgs.Empty);
                  return (IntPtr)0;
                }
                break;
            }
            return CallWindowProc(oldWndProc, hwnd, msg, wParam, lParam);
              };
              var newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);
              var oldWndProcPtr = SetWindowLongPtr(hwnd, WindowLongFlags.GWL_WNDPROC, newWndProcPtr);
              oldWndProc = (WndProcDelegate)
              Marshal.GetDelegateForFunctionPointer(oldWndProcPtr, typeof(WndProcDelegate));
            };

            var result = win7OpenFileDialog.ShowDialog ();
            if (result != CommonFileDialogResult.Ok) {
              return;
            }
            if (confirmConstraintCheckBox.IsChecked) {
              var constraint = new Agent.KeyConstraint ();
              constraint.Type = Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_CONFIRM;
              constraints.Add (constraint);
            }
            if (lifetimeConstraintCheckBox.IsChecked) {
              // error checking for parse done in fileOK event handler
              uint lifetime = uint.Parse (lifetimeConstraintTextBox.Text);
              var constraint = new Agent.KeyConstraint ();
              constraint.Type = Agent.KeyConstraintType.SSH_AGENT_CONSTRAIN_LIFETIME;
              constraint.Data = lifetime;
              constraints.Add (constraint);
            }
            fileNames = win7OpenFileDialog.FileNames.ToArray ();
              } else {
            using (var openFileDialog = new OpenFileDialog())
            {
              openFileDialog.Multiselect = true;
              openFileDialog.Filter = string.Join ("|",
            Strings.filterPuttyPrivateKeyFiles, "*.ppk",
             Strings.filterAllFiles, "*.*");

              openFileDialog.FileOk += xpOpenFileDialog_FileOk;

              // Windows XP uses old style file open dialog that can be extended
              // using the Windows API via FileDlgExtenders library
              XPOpenFileDialog xpOpenFileDialog = null;
              if (Type.GetType("Mono.Runtime") == null) {
            xpOpenFileDialog = new XPOpenFileDialog ();
            xpOpenFileDialog.FileDlgStartLocation = AddonWindowLocation.Bottom;
            mOpenFileDialogMap.Add (openFileDialog, xpOpenFileDialog);
              }

              openFileDialog.HelpRequest += OnAddFromFileHelpRequested;
              // TODO: technically, a listener could be added after this
              openFileDialog.ShowHelp = AddFromFileHelpRequested != null;

              var result = xpOpenFileDialog == null ?
            openFileDialog.ShowDialog() :
            openFileDialog.ShowDialog(xpOpenFileDialog, null);
              if (result != DialogResult.OK)
            return;

              if (xpOpenFileDialog == null) {
            // If dialog could not be extended, then we add constraints by holding
            // down the control key when clicking the Open button.
            if (Control.ModifierKeys.HasFlag(Keys.Control)) {
              var constraintDialog = new ConstraintsInputDialog ();
              constraintDialog.ShowDialog ();
              if (constraintDialog.DialogResult == DialogResult.OK) {
                if (constraintDialog.ConfirmConstraintChecked) {
                  constraints.addConfirmConstraint ();
                }
                if (constraintDialog.LifetimeConstraintChecked) {
                  constraints.addLifetimeConstraint (constraintDialog.LifetimeDuration);
                }
              }
            }
              } else {
            mOpenFileDialogMap.Remove (openFileDialog);

            if (xpOpenFileDialog.UseConfirmConstraintChecked) {
              constraints.addConfirmConstraint ();
            }
            if (xpOpenFileDialog.UseLifetimeConstraintChecked) {
              constraints.addLifetimeConstraint
                (xpOpenFileDialog.LifetimeConstraintDuration);
            }
              }
              fileNames = openFileDialog.FileNames;
            }
              }
              UseWaitCursor = true;
              mAgent.AddKeysFromFiles(fileNames, constraints);
              if (!(mAgent is Agent))
              {
            ReloadKeyListView();
              }
              UseWaitCursor = false;
        }