/// <summary>Open a new putty window with its settings being passed in a <seealso cref="SessionData"/> object</summary> /// <param name="session">The <seealso cref="SessionData"/> object containing the settings</param> public static CtlPuttyPanel OpenPuttySession(SessionData session) { Log.InfoFormat("Opening putty session, id={0}", session == null ? "" : session.SessionId); CtlPuttyPanel panel = null; if (session != null) { String Executable = PuttyStartInfo.GetExecutable(session); if (String.IsNullOrEmpty(Executable)) { MessageBox.Show( string.Format(LocalizedText.SuperPuTTY_Error_trying_to_create_session_Executable_not_set, session, session.Proto), LocalizedText.SuperPuTTY_OpenPuttySession_Failed_to_create_a_session, MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } if (!File.Exists(Executable)) { MessageBox.Show(string.Format(LocalizedText.SuperPuTTY_Error_trying_to_create_session_Executable_not_found, session, session.Proto, Executable) , LocalizedText.SuperPuTTY_OpenPuttySession_Failed_to_create_a_session, MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } // This is the callback fired when the panel containing the terminal is closed // We use this to save the last docking location and to close the panel void ClosedCallback(bool error) { if (panel != null) { // save the last dockstate (if it has been changed) if (session.LastDockstate != panel.DockState && panel.DockState != DockState.Unknown && panel.DockState != DockState.Hidden) { session.LastDockstate = panel.DockState; SaveSessions(); } if (panel.InvokeRequired) { panel.BeginInvoke((MethodInvoker) delegate { panel.Close(); }); } else { panel.Close(); } } } try { panel = new CtlPuttyPanel(session, ClosedCallback); ApplyDockRestrictions(panel); ApplyIconForWindow(panel, session); panel.Show(MainForm.DockPanel, session.LastDockstate); ReportStatus("Opened session: {0} [{1}]", session.SessionId, session.Proto); if (!String.IsNullOrEmpty(session.SPSLFileName) && File.Exists(session.SPSLFileName)) { ExecuteScriptEventArgs scriptArgs = new ExecuteScriptEventArgs() { Script = File.ReadAllText(session.SPSLFileName), Handle = panel.AppPanel.AppWindowHandle }; if (!String.IsNullOrEmpty(scriptArgs.Script)) { SPSL.BeginExecuteScript(scriptArgs); } } } catch (InvalidOperationException ex) { MessageBox.Show( string.Format(LocalizedText.SuperPuTTY_Error_trying_to_create_session, ex.Message), LocalizedText.SuperPuTTY_OpenPuttySession_Failed_to_create_session_panel, MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(panel); }
/// <summary>Open a new putty window with its settings being passed in a <seealso cref="SessionData"/> object</summary> /// <param name="session">The <seealso cref="SessionData"/> object containing the settings</param> public static ctlPuttyPanel OpenProtoSession(SessionData session) { Log.InfoFormat("Opening putty session, id={0}", session == null ? "" : session.SessionId); ctlPuttyPanel panel = null; if (session != null) { String Executable = PuttyStartInfo.GetExecutable(session); if (String.IsNullOrEmpty(Executable)) { MessageBox.Show("Error trying to create session: " + session.ToString() + "\nExecutable not set for " + session.Proto.ToString() + " protocol." + "\nGo to tools->options->General tab to set the path to the executable." , "Failed to create a session", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } if (!File.Exists(Executable)) { MessageBox.Show("Error trying to create session: " + session.ToString() + "\nExecutable not found for " + session.Proto.ToString() + " protocol." + "\nThe path for the executable was set as \"" + Executable + "\"." + "\nGo to tools->options->General tab to set the path to the executable." , "Failed to create a session", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } // This is the callback fired when the panel containing the terminal is closed // We use this to save the last docking location and to close the panel PuttyClosedCallback callback = delegate { if (panel != null) { // save the last dockstate (if it has been changed) if (session.LastDockstate != panel.DockState && panel.DockState != DockState.Unknown && panel.DockState != DockState.Hidden) { session.LastDockstate = panel.DockState; SuperPuTTY.SaveSessions(); } if (panel.InvokeRequired) { panel.BeginInvoke((MethodInvoker) delegate { panel.Close(); }); } else { panel.Close(); } } }; try { panel = new ctlPuttyPanel(session, callback); ApplyDockRestrictions(panel); ApplyIconForWindow(panel, session); panel.Show(MainForm.DockPanel, session.LastDockstate); ReportStatus("Opened session: {0} [{1}]", session.SessionId, session.Proto); if (!String.IsNullOrEmpty(session.SPSLFileName) && File.Exists(session.SPSLFileName)) { ExecuteScriptEventArgs scriptArgs = new ExecuteScriptEventArgs() { Script = File.ReadAllText(session.SPSLFileName), Handle = panel.AppPanel.AppWindowHandle }; if (!String.IsNullOrEmpty(scriptArgs.Script)) { SPSL.BeginExecuteScript(scriptArgs); } } } catch (InvalidOperationException ex) { MessageBox.Show("Error trying to create session " + ex.Message, "Failed to create session panel", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(panel); }
/// <summary>Open a new putty window with its settings being passed in a <seealso cref="SessionData"/> object</summary> /// <param name="session">The <seealso cref="SessionData"/> object containing the settings</param> public static ctlPuttyPanel OpenProtoSession(SessionData session) { Log.InfoFormat("Opening putty session, id={0}", session == null ? "" : session.SessionId); ctlPuttyPanel panel = null; if (session != null) { String Executable = PuttyStartInfo.GetExecutable(session); if (String.IsNullOrEmpty(Executable)) { MessageBox.Show("Error trying to create session: " + session.ToString() + "\nExecutable not set for " + session.Proto.ToString() + " protocol." + "\nGo to tools->options->General tab to set the path to the executable." , "Failed to create a session", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } if (!File.Exists(Executable)) { MessageBox.Show("Error trying to create session: " + session.ToString() + "\nExecutable not found for " + session.Proto.ToString() + " protocol." + "\nThe path for the executable was set as \"" + Executable + "\"." + "\nGo to tools->options->General tab to set the path to the executable." , "Failed to create a session", MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } // This is the callback fired when the panel containing the terminal is closed // We use this to save the last docking location and to close the panel PuttyClosedCallback callback = delegate { if (panel != null) { // save the last dockstate (if it has been changed) if (session.LastDockstate != panel.DockState && panel.DockState != DockState.Unknown && panel.DockState != DockState.Hidden) { session.LastDockstate = panel.DockState; SuperPuTTY.SaveSessions(); } if (panel.InvokeRequired) { panel.BeginInvoke((MethodInvoker) delegate { panel.Close(); }); } else { panel.Close(); } } }; try { panel = new ctlPuttyPanel(session, callback); ApplyDockRestrictions(panel); ApplyIconForWindow(panel, session); panel.Show(MainForm.DockPanel, session.LastDockstate); ReportStatus("Opened session: {0} [{1}]", session.SessionId, session.Proto); if (!String.IsNullOrWhiteSpace(session.SPSLFileName)) { String fileName = session.SPSLFileName; String script = String.Empty; if (Regex.IsMatch(fileName, @"^https?:\/\/", RegexOptions.IgnoreCase)) { try { HttpWebRequest req = WebRequest.CreateHttp(fileName); var response = req.GetResponse(); using (var stream = new StreamReader(response.GetResponseStream())) { script = stream.ReadToEnd(); } } catch (Exception) { script = String.Empty; } } else { if (fileName.StartsWith("file://", StringComparison.OrdinalIgnoreCase)) { fileName = fileName.Substring("file://".Length); } if (File.Exists(fileName)) { script = File.ReadAllText(fileName); } } if (!String.IsNullOrEmpty(script)) { ExecuteScriptEventArgs scriptArgs = new ExecuteScriptEventArgs() { Script = script, Handle = panel.AppPanel.AppWindowHandle }; SPSL.BeginExecuteScript(scriptArgs); } } } catch (InvalidOperationException ex) { MessageBox.Show("Error trying to create session " + ex.Message, "Failed to create session panel", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(panel); }