1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Diagnostics;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9 using KeePassRPC.DataExchangeModel;
10 
11 namespace KeePassRPC.Forms
12 {
13     public partial class KeeFieldForm : Form
14     {
15         public new string Name;
16         public string Value;
17         public string Id;
18         public FormFieldType Type;
19         public int Page;
20         public PlaceholderHandling PlaceholderHandling;
21 
KeeFieldForm(FormField ff)22         public KeeFieldForm(FormField ff) : this(ff.Name, ff.Value, ff.Id, ff.Type, ff.Page, ff.PlaceholderHandling)
23         {
24         }
25 
KeeFieldForm(string name, string value, string id, FormFieldType type, int page, PlaceholderHandling phh)26         public KeeFieldForm(string name, string value, string id, FormFieldType type, int page, PlaceholderHandling phh)
27         {
28             InitializeComponent();
29             Icon = global::KeePassRPC.Properties.Resources.kee;
30             if (string.IsNullOrEmpty(name))
31                 this.Text = "Add a form field";
32             else
33                 this.Text = "Edit a form field";
34 
35             if (value == "{USERNAME}")
36             {
37                 textBox2.Text = Value = value;
38                 comboBox1.Text = "Username";
39                 comboBox1.Enabled = false;
40                 label6.Visible = true;
41             } else
42             if (value == "{PASSWORD}")
43             {
44                 textBox2.Text = Value = value;
45                 comboBox1.Text = "Password";
46                 comboBox1.Enabled = false;
47                 label7.Visible = true;
48             }
49             else
50             {
51                 if (type == FormFieldType.FFTpassword)
52                     comboBox1.Text = "Password";
53                 else if (type == FormFieldType.FFTselect)
54                     comboBox1.Text = "Select";
55                 else if (type == FormFieldType.FFTradio)
56                     comboBox1.Text = "Radio";
57                 else if (type == FormFieldType.FFTtext)
58                     comboBox1.Text = "Text";
59                 else if (type == FormFieldType.FFTusername)
60                     comboBox1.Text = "Username";
61                 else if (type == FormFieldType.FFTcheckbox)
62                     comboBox1.Text = "Checkbox";
63 
64                 if (type == FormFieldType.FFTcheckbox)
65                 {
66                     checkBox1.Visible = true;
67                     Value = value;
68                     checkBox1.Checked = Value == "KEEFOX_CHECKED_FLAG_TRUE" ? true : false;
69                 } else
70                 {
71                     textBox2.Text = Value = value;
72                     textBox2.Visible = true;
73                     label2.Visible = true;
74                 }
75             }
76             textBox1.Text = Name = name;
77             textBox3.Text = Id = id;
78             Page = page;
79             textBox4.Text = Page.ToString();
80 
81             switch (phh)
82             {
83                 case PlaceholderHandling.Default: radioButton1.Checked = true; break;
84                 case PlaceholderHandling.Enabled: radioButton2.Checked = true; break;
85                 case PlaceholderHandling.Disabled: radioButton3.Checked = true; break;
86             }
87 
88             comboBox1.SelectedIndexChanged += new System.EventHandler(comboBox1_SelectedIndexChanged);
89         }
90 
button1_Click(object sender, EventArgs e)91         private void button1_Click(object sender, EventArgs e)
92         {
93             if (textBox2.Visible && textBox2.Text.Length <= 0)
94             {
95                 MessageBox.Show(this, "Please specify a value");
96                 this.DialogResult = DialogResult.None;
97                 return;
98             }
99 
100             if (textBox2.Visible && (textBox2.Text == "{USERNAME}" || textBox2.Text == "{PASSWORD}"))
101             {
102                 MessageBox.Show(this, "Please change the value of this form field - it is currently set to a value that Kee needs to reserve for internal use. Sorry, please report this on the support forums if you are inconvenienced by this choice of reserved phrase.");
103                 this.DialogResult = DialogResult.None;
104                 return;
105             }
106 
107             Name = textBox1.Text;
108             Id = textBox3.Text;
109             if (!int.TryParse(textBox4.Text, out Page)) Page = 1;
110             if (comboBox1.Text == "Password")
111                 Type = FormFieldType.FFTpassword;
112             else if (comboBox1.Text == "Select")
113                 Type = FormFieldType.FFTselect;
114             else if (comboBox1.Text == "Radio")
115                 Type = FormFieldType.FFTradio;
116             else if (comboBox1.Text == "Text")
117                 Type = FormFieldType.FFTtext;
118             else if (comboBox1.Text == "Username")
119                 Type = FormFieldType.FFTusername;
120             else if (comboBox1.Text == "Checkbox")
121                 Type = FormFieldType.FFTcheckbox;
122 
123             if (comboBox1.Text == "Checkbox")
124             {
125                 Value = checkBox1.Checked ? "KEEFOX_CHECKED_FLAG_TRUE" : "KEEFOX_CHECKED_FLAG_FALSE";
126             } else
127             {
128                 Value = textBox2.Text;
129             }
130 
131             if (radioButton1.Checked) PlaceholderHandling = PlaceholderHandling.Default;
132             if (radioButton2.Checked) PlaceholderHandling = PlaceholderHandling.Enabled;
133             if (radioButton3.Checked) PlaceholderHandling = PlaceholderHandling.Disabled;
134         }
135 
comboBox1_SelectedIndexChanged(object sender, EventArgs e)136         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
137         {
138             if (comboBox1.Text == "Checkbox")
139             {
140                 textBox2.Visible = false;
141                 label2.Visible = false;
142                 checkBox1.Visible = true;
143                 checkBox1.Checked = false;
144             }
145             else
146             {
147                 textBox2.Visible = true;
148                 label2.Visible = true;
149                 checkBox1.Visible = false;
150                 checkBox1.Checked = false;
151             }
152         }
153 
linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)154         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
155         {
156             Process p = new Process();
157             p = Process.Start("https://forum.kee.pm/t/placeholder-handling/1100");
158         }
159     }
160 }
161