|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Xml.Linq; |
| 7 | + |
| 8 | +namespace RishWinTools.Keys |
| 9 | +{ |
| 10 | + public partial class InsertKeyForm : System.Windows.Forms.Form |
| 11 | + { |
| 12 | + private Label NameLabel; |
| 13 | + private TextBox NameInput; |
| 14 | + public string NameValue => NameInput.Text; |
| 15 | + private Label SecretKeyLabel; |
| 16 | + private TextBox SecretKeyInput; |
| 17 | + public string SecretKeyValue => SecretKeyInput.Text; |
| 18 | + private Button InsertButton; |
| 19 | + |
| 20 | + public InsertKeyForm() |
| 21 | + { |
| 22 | + // Window |
| 23 | + Name = "InsertKeyForm"; |
| 24 | + Text = "Вставка SSH ключа"; |
| 25 | + AutoScaleDimensions = new SizeF(6F, 13F); |
| 26 | + AutoScaleMode = AutoScaleMode.Font; |
| 27 | + ClientSize = new Size(640, 0); |
| 28 | + StartPosition = FormStartPosition.CenterParent; |
| 29 | + |
| 30 | + // NameLabel |
| 31 | + NameLabel = new Label(); |
| 32 | + NameLabel.Name = "NameLabel"; |
| 33 | + NameLabel.Text = "Имя ключа"; |
| 34 | + NameLabel.AutoSize = true; |
| 35 | + NameLabel.Location = new Point(10, 20); |
| 36 | + Controls.Add(NameLabel); |
| 37 | + |
| 38 | + // NameInput |
| 39 | + NameInput = new TextBox(); |
| 40 | + NameInput.Name = "NameInput"; |
| 41 | + NameInput.Text = "id_ed25519"; |
| 42 | + NameInput.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; |
| 43 | + NameInput.Size = new Size(ClientSize.Width - 20, 20); |
| 44 | + NameInput.Location = new Point(10, 40); |
| 45 | + Controls.Add(NameInput); |
| 46 | + |
| 47 | + // SecretKeyLabel |
| 48 | + SecretKeyLabel = new Label(); |
| 49 | + SecretKeyLabel.Name = "SecretKeyLabel"; |
| 50 | + SecretKeyLabel.Text = "Секретный ключ"; |
| 51 | + SecretKeyLabel.AutoSize = true; |
| 52 | + SecretKeyLabel.Location = new Point(10, NameInput.Bottom + 10); |
| 53 | + Controls.Add(SecretKeyLabel); |
| 54 | + |
| 55 | + // SecretKeyInput |
| 56 | + SecretKeyInput = new TextBox(); |
| 57 | + SecretKeyInput.Name = "SecretKeyInput"; |
| 58 | + SecretKeyInput.Text = ""; |
| 59 | + SecretKeyInput.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; |
| 60 | + SecretKeyInput.Size = new Size(ClientSize.Width - 20, 140); |
| 61 | + SecretKeyInput.Location = new Point(10, SecretKeyLabel.Bottom + 5); |
| 62 | + SecretKeyInput.Multiline = true; |
| 63 | + Controls.Add(SecretKeyInput); |
| 64 | + |
| 65 | + // InsertButton |
| 66 | + InsertButton = new Button(); |
| 67 | + InsertButton.Name = "InsertButton"; |
| 68 | + InsertButton.Text = "Вставить ключ"; |
| 69 | + InsertButton.AutoSize = true; |
| 70 | + InsertButton.AutoSizeMode = AutoSizeMode.GrowAndShrink; |
| 71 | + InsertButton.Location = new Point(10, SecretKeyInput.Bottom + 10); |
| 72 | + InsertButton.UseVisualStyleBackColor = true; |
| 73 | + InsertButton.Click += OnInsertButtonClick; |
| 74 | + Controls.Add(InsertButton); |
| 75 | + |
| 76 | + // Layout |
| 77 | + int FormHeight = 0; |
| 78 | + foreach (Control control in Controls) |
| 79 | + { |
| 80 | + int controlBottom = control.Bottom; |
| 81 | + if (controlBottom > FormHeight) |
| 82 | + { |
| 83 | + FormHeight = controlBottom; |
| 84 | + } |
| 85 | + } |
| 86 | + ClientSize = new Size(ClientSize.Width, FormHeight + 20); |
| 87 | + } |
| 88 | + |
| 89 | + protected void OnInsertButtonClick(object? sender, EventArgs e) |
| 90 | + { |
| 91 | + if (string.IsNullOrWhiteSpace(NameValue)) |
| 92 | + { |
| 93 | + MessageBox.Show("Пожалуйста заполните поле Имя", "Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 94 | + return; |
| 95 | + } |
| 96 | + if (string.IsNullOrWhiteSpace(SecretKeyValue)) |
| 97 | + { |
| 98 | + MessageBox.Show("Пожалуйста заполните поле Секретный ключ.", "Ошибка ввода", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + if (KeysManager.InsertKey(NameValue, SecretKeyValue)) |
| 103 | + { |
| 104 | + KeysManager.GetKeys(true); |
| 105 | + if (KeysManager.CreatePubKey(NameValue, false, true)) |
| 106 | + { |
| 107 | + KeysManager.GetKeys(true); |
| 108 | + } |
| 109 | + DialogResult = DialogResult.OK; |
| 110 | + Close(); |
| 111 | + } |
| 112 | + else |
| 113 | + { |
| 114 | + DialogResult = DialogResult.None; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments