MakeUniqueSessionId() public static method

Generate a unique session ID to prevent collisions in the in-application data store, used when importing and merging sessions from another application or an older versin of SuperPuTTY
public static MakeUniqueSessionId ( string sessionId ) : string
sessionId string A string containing the sessionID of the session being imported
return string
Exemplo n.º 1
0
        /// <summary>
        /// Create/Update a session entry
        /// </summary>
        /// <param name="sender">The toolstripmenuitem control that was clicked</param>
        /// <param name="e">An Empty EventArgs object</param>
        private void CreateOrEditSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SessionData session = null;
            TreeNode    node    = null;
            TreeNode    nodeRef = this.nodeRoot;
            bool        isEdit  = false;
            string      title   = null;

            if (sender is ToolStripMenuItem)
            {
                ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
                bool isFolderNode          = IsFolderNode(treeView1.SelectedNode);
                if (menuItem.Text.ToLower().Equals("new") || isFolderNode)
                {
                    session = new SessionData();
                    nodeRef = isFolderNode ? treeView1.SelectedNode : treeView1.SelectedNode.Parent;
                    title   = "Create New Session";
                }
                else if (menuItem == this.createLikeToolStripMenuItem)
                {
                    // copy as
                    session             = (SessionData)((SessionData)treeView1.SelectedNode.Tag).Clone();
                    session.SessionId   = SuperPuTTY.MakeUniqueSessionId(session.SessionId);
                    session.SessionName = SessionData.GetSessionNameFromId(session.SessionId);
                    nodeRef             = treeView1.SelectedNode.Parent;
                    title = "Create New Session Like " + session.OldName;
                }
                else
                {
                    // edit, session node selected
                    session = (SessionData)treeView1.SelectedNode.Tag;
                    node    = treeView1.SelectedNode;
                    nodeRef = node.Parent;
                    isEdit  = true;
                    title   = "Edit Session: " + session.SessionName;
                }
            }

            dlgEditSession form = new dlgEditSession(session, this.treeView1.ImageList);

            form.Text = title;
            form.SessionNameValidator += delegate(string txt, out string error)
            {
                error = String.Empty;
                bool isDupeNode = isEdit ? txt != node.Text && nodeRef.Nodes.ContainsKey(txt) : nodeRef.Nodes.ContainsKey(txt);
                if (isDupeNode)
                {
                    error = "Session with same name exists";
                }
                else if (txt.Contains(SessionIdDelim))
                {
                    error = "Invalid character ( " + SessionIdDelim + " ) in name";
                }
                else if (string.IsNullOrEmpty(txt) || txt.Trim() == String.Empty)
                {
                    error = "Empty name";
                }
                return(string.IsNullOrEmpty(error));
            };

            if (form.ShowDialog(this) == DialogResult.OK)
            {
                /* "node" will only be assigned if we're editing an existing session entry */
                if (node == null)
                {
                    // get the path up to the ref (parent) node
                    if (nodeRoot != nodeRef)
                    {
                        UpdateSessionId(nodeRef, session);
                        session.SessionId = SessionData.CombineSessionIds(session.SessionId, session.SessionName);
                    }
                    SuperPuTTY.AddSession(session);

                    // find new node and select it
                    TreeNode nodeNew = nodeRef.Nodes[session.SessionName];
                    if (nodeNew != null)
                    {
                        this.treeView1.SelectedNode = nodeNew;
                    }
                }
                else
                {
                    // handle renames
                    node.Text             = session.SessionName;
                    node.Name             = session.SessionName;
                    node.ImageKey         = session.ImageKey;
                    node.SelectedImageKey = session.ImageKey;
                    if (session.SessionId != session.OldSessionId)
                    {
                        try
                        {
                            this.isRenamingNode = true;
                            SuperPuTTY.RemoveSession(session.OldSessionId);
                            SuperPuTTY.AddSession(session);
                        }
                        finally
                        {
                            this.isRenamingNode = false;
                        }
                    }
                    ResortNodes();
                    this.treeView1.SelectedNode = node;
                }

                //treeView1.ExpandAll();
                SuperPuTTY.SaveSessions();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create/Update a session entry
        /// </summary>
        /// <param name="sender">The toolstripmenuitem control that was clicked</param>
        /// <param name="e">An Empty EventArgs object</param>
        private void CreateOrEditSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SessionData session = null;
            TreeNode    node    = null;
            TreeNode    nodeRef = this.nodeRoot;
            string      title   = null;

            if (sender is ToolStripMenuItem)
            {
                ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
                bool isFolderNode          = IsFolderNode(treeView1.SelectedNode);
                if (menuItem.Text.ToLower().Equals("new") || isFolderNode)
                {
                    session = new SessionData();
                    nodeRef = isFolderNode ? treeView1.SelectedNode : treeView1.SelectedNode.Parent;
                    title   = "Create New Session";
                }
                else if (menuItem == this.createLikeToolStripMenuItem)
                {
                    // copy as
                    session             = (SessionData)((SessionData)treeView1.SelectedNode.Tag).Clone();
                    session.SessionId   = SuperPuTTY.MakeUniqueSessionId(session.SessionId);
                    session.SessionName = SessionData.GetSessionNameFromId(session.SessionId);
                    nodeRef             = treeView1.SelectedNode.Parent;
                    title = "Create New Session Like " + session.OldName;
                }
                else
                {
                    // edit, session node selected
                    // We make a clone of the session since we do not want to directly edit the real object.
                    session = (SessionData)((SessionData)treeView1.SelectedNode.Tag).Clone();
                    node    = treeView1.SelectedNode;
                    nodeRef = node.Parent;
                    title   = "Edit Session: " + session.SessionName;
                }
            }

            dlgEditSession form = new dlgEditSession(session, this.treeView1.ImageList)
            {
                Text = title
            };

            form.SessionNameValidator += delegate(string txt, out string error)
            {
                bool IsValid = ValidateSessionNameChange(nodeRef, node, txt, out error);
                return(IsValid);
            };

            if (form.ShowDialog(this) == DialogResult.OK)
            {
                /* "node" will only be assigned if we're editing an existing session entry */
                if (node == null)
                {
                    // get the path up to the ref (parent) node
                    if (nodeRoot != nodeRef)
                    {
                        UpdateSessionId(nodeRef, session);
                        session.SessionId = SessionData.CombineSessionIds(session.SessionId, session.SessionName);
                    }
                    SuperPuTTY.AddSession(session);

                    // find new node and select it
                    TreeNode nodeNew = nodeRef.Nodes[session.SessionName];
                    if (nodeNew != null)
                    {
                        this.treeView1.SelectedNode = nodeNew;
                    }
                }
                else
                {
                    SessionData RealSession = (SessionData)treeView1.SelectedNode.Tag;
                    RealSession.CopyFrom(session);
                    RealSession.SessionName     = session.SessionName;
                    this.treeView1.SelectedNode = node;
                }

                //treeView1.ExpandAll();
                SuperPuTTY.SaveSessions();
            }
        }