public NewSessionRequest getNewSessionRequest()
 {
     NewSessionRequest nsr = new NewSessionRequest((Session)sessionComboBox.SelectedItem
                                 , sessionFolderComboBox.Text
                                 , hostnameTextBox.Text
                                 , sessionnameTextBox.Text
                                 , copyUsernameCheckBox.Checked
                                 , launchSessionCheckBox.Checked);
     return nsr;
 }
 public void setNewSessionRequest(NewSessionRequest nsr)
 {
     if (nsr.SessionTemplate != null)
     {
         // use the session controller to find the session as the objects held
         // held in the session control may not be the same
         sessionComboBox.SelectedItem = sc.findSession(nsr.SessionTemplate);
         if (nsr.SessionFolder == null)
             sessionFolderComboBox.SelectedItem = nsr.SessionTemplate.FolderName;
         else
             sessionFolderComboBox.SelectedItem = nsr.SessionFolder;
     }
     else
     {
         sessionComboBox.SelectedItem = sc.findDefaultSession(false);
         if (nsr.SessionFolder != null && !(nsr.SessionFolder.Equals("")))
         {
             sessionFolderComboBox.SelectedItem = nsr.SessionFolder;
         }
     }
     launchSessionCheckBox.Checked = nsr.LaunchSession;
     copyUsernameCheckBox.Checked = nsr.CopyDefaultUsername;
 }
        protected void newSession(List<Session> selectedSessions)
        {
            if (selectedSessions.Count > 0)
            {
                // Pick the first selected session to be the template
                Session template = selectedSessions[0];
                NewSessionRequest nsr = new NewSessionRequest(template, template.FolderName, "", "", true, false);
                nsf.setNewSessionRequest(nsr);
            }

            if (nsf.ShowDialog() == DialogResult.OK)
            {
                NewSessionRequest nsr = nsf.getNewSessionRequest();
                bool result = sc.createNewSession(nsr, this);
                if (result == false)
                    MessageBox.Show("Failed to create new session: " + nsr.SessionName
                    , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    if ( nsr.LaunchSession == true )
                    {
                         String errMsg = sc.launchSession(nsr.SessionName);
                         if (errMsg.Equals("") == false)
                         {
                             MessageBox.Show("PuTTY Failed to start.\nCheck the PuTTY location in System Tray -> Options.\n" +
                                 errMsg
                                 , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                         }
                    }
                }

            }
        }
        /// <summary>
        /// Create a new session based on an old session
        /// </summary>
        /// <param name="nsr">The new session request</param>
        /// <returns></returns>
        public bool createNewSession(NewSessionRequest nsr)
        {
            // Check the template session is still there
            RegistryKey template = Registry.CurrentUser.OpenSubKey(PUTTY_SESSIONS_REG_KEY + "\\" + nsr.SessionTemplate.SessionKey, false);
            if (template == null)
                return false;

            // Check no-one has created a new session with the same name
            RegistryKey newSession = Registry.CurrentUser.OpenSubKey(PUTTY_SESSIONS_REG_KEY + "\\" + nsr.SessionName, false);
            if (newSession != null)
            {
                newSession.Close();
                return false;
            }

            // Create the new session
            newSession = Registry.CurrentUser.CreateSubKey(PUTTY_SESSIONS_REG_KEY + "\\" + Session.convertDisplayToSessionKey(nsr.SessionName));

            // Copy the values
            bool hostnameSet = false;
            bool foldernameSet = false;
            bool protocolSet = false;
            bool portnumberSet = false;

            object value;
            foreach (string valueName in template.GetValueNames())
            {

                if (valueName.Equals(PUTTY_HOSTNAME_ATTRIB))
                {
                    hostnameSet = true;
                    value = nsr.Hostname;
                }
                else if (valueName.Equals(PUTTY_PSM_FOLDER_ATTRIB))
                {
                    foldernameSet = true;
                    value = nsr.SessionFolder;
                }
                else if (nsr.CopyDefaultUsername == false &&
                            valueName.Equals(PUTTY_USERNAME_ATTRIB))
                {
                    value = "";
                }
                else if (valueName.Equals(PUTTY_PROTOCOL_ATTRIB) &&
                         !(nsr.Protocol.Equals("")) )
                {
                    protocolSet = true;
                    value = nsr.Protocol;

                }
                else if (valueName.Equals(PUTTY_PORTNUMBER_ATTRIB) &&
                         nsr.Portnumber > 0 )
                {
                    portnumberSet = true;
                    value = nsr.Portnumber;
                }
                else
                {
                    value = template.GetValue(valueName);
                }

                newSession.SetValue(valueName, value, template.GetValueKind(valueName));
            }

            // Set the hostname if it hasn't already been set
            if (hostnameSet == false)
                newSession.SetValue(PUTTY_HOSTNAME_ATTRIB, nsr.Hostname, RegistryValueKind.String);

            // Set the foldername if it hasn't already been set
            if (foldernameSet == false)
                newSession.SetValue(PUTTY_PSM_FOLDER_ATTRIB, nsr.SessionFolder, RegistryValueKind.String);

            // Set the protocol if it hasn't already been set
            if (protocolSet == false)
                newSession.SetValue(PUTTY_PROTOCOL_ATTRIB, nsr.Protocol, RegistryValueKind.String);

            // Set the portnumber if it hasn't already been set
            if (portnumberSet == false)
                newSession.SetValue(PUTTY_PORTNUMBER_ATTRIB, nsr.Portnumber, RegistryValueKind.DWord);

            template.Close();
            newSession.Close();

            return true;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public int syncSessions(BackgroundWorker worker, SyncSessionsRequestedEventArgs e)
        {
            int modifiedCount = 0;
            List<Session> delList = new List<Session>();

            // Disable session refreshes
            BeginUpdate();

            foreach ( SessionAction sa in e.SessionActionList )
            {

                if ( sa.Action == SessionAction.ACTION.ADD )
                {
                    NewSessionRequest nsr = new NewSessionRequest(e.SessionTemplate
                                                                , sa.NewSession.FolderName
                                                                , sa.NewSession.Hostname
                                                                , sa.NewSession.SessionDisplayText
                                                                , sa.NewSession.Protocol
                                                                , sa.NewSession.Portnumber
                                                                , true, false);
                    createNewSession(nsr, worker);
                }
                else if ( sa.Action == SessionAction.ACTION.DELETE )
                {
                    delList.Add(sa.ExistingSession);
                }
                else if ( sa.Action == SessionAction.ACTION.UPDATE )
                {
                    Session existingSession = findSession(sa.NewSession);
                    if (existingSession != null)
                    {
                        sessionProvider.updateHostname(sa.NewSession);
                        sessionProvider.updateFolder(sa.NewSession);
                        sessionProvider.updateProtocol(sa.NewSession);
                        sessionProvider.updatePortnumber(sa.NewSession);
                    }
                }
                else if (sa.Action == SessionAction.ACTION.RENAME)
                {
                    Session existingSession = findSession(sa.ExistingSession);
                    sessionProvider.renameSession(existingSession, sa.NewSession.SessionDisplayText);
                    existingSession.FolderName = sa.NewSession.FolderName;
                    sessionProvider.updateFolder(existingSession);
                }
                modifiedCount++;
                worker.ReportProgress(modifiedCount);
            }

            if (delList.Count > 0)
                deleteSessions(delList, worker);

            worker.ReportProgress(e.SessionActionList.Count);

            // Re-enable session refreshes
            EndUpdate();

            return modifiedCount;
        }
        /// <summary>
        /// Create a new session based on an existing session
        /// Delegates to the sessionProvider, and then
        /// fires a session refresh event
        /// </summary>
        /// <param name="nsr">The new session request</param>
        /// <param name="sender"></param>
        /// <returns></returns>
        public bool createNewSession(NewSessionRequest nsr, Object sender)
        {
            bool result = sessionProvider.createNewSession(nsr);

            // Don't refresh the sender - this should have done it's own update
            invalidateSessionList(sender, false);

            return result;
        }
        private void saveNewSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Get the session
            Session s = getSelectedSession(true);

            // Get the foldername
            String folderName = s.FolderName;

            // Setup the session request
            NewSessionRequest nsr;

            if (s.IsFolder == false)
            {
                nsr = new NewSessionRequest(s, folderName, "", "", true, true);
            }
            else
            {
                // Try to get the default session
                s = sc.findDefaultSession();

                // If that doesn't work get the first child session, if
                // one exists
                if (s == null && getSelectedSessions().Count > 0)
                {
                    s = getSelectedSessions()[0];
                }

                // If we still don't have a session error and return
                if (s == null)
                {
                    MessageBox.Show("You must create at least one session in PuTTY first!"
                        , "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                nsr = new NewSessionRequest(s, folderName, "", "", true, true);
            }

            // Set the options in the form
            newSessionForm.setNewSessionRequest(nsr);

            // Show the dialog
            if (newSessionForm.ShowDialog() == DialogResult.OK)
            {
                nsr = newSessionForm.getNewSessionRequest();
                bool result = getSessionController().createNewSession(nsr, this);
                if (result == false)
                    MessageBox.Show("Failed to create new session: " + nsr.SessionName
                    , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                {
                    TreeNode[] ta = treeView.Nodes.Find(Session.getFolderKey(nsr.SessionFolder), true);
                    if (ta.Length > 0)
                    {
                        Session newSession = getSessionController().findSessionByName(nsr.SessionName);

                        // If we can't find the session immediately after creating it - something
                        // has gone wrong - so just refresh the sessions including the tree
                        if (newSession == null)
                        {
                            // Refresh the session list in the system tray
                            sc.invalidateSessionList(this, true);
                        }
                        else
                        {
                            TreeNode newNode = createNode(newSession);

                            // Add the new node
                            ta[0].Nodes.Add(newNode);

                            // Select the new node
                            ta[0].Expand();

                            // Refresh the session list in the system tray
                            sc.invalidateSessionList(this, false);
                        }

                    }

                    if (nsr.LaunchSession == true)
                    {
                        String errMsg = getSessionController().launchSession(nsr.SessionName);
                        if (errMsg.Equals("") == false)
                        {
                            MessageBox.Show("PuTTY Failed to start.\nCheck the PuTTY location in System Tray -> Options.\n" +
                                errMsg
                                , "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }

            }
        }