1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Text;
7 using System.Windows.Forms;
8 using KeePassLib;
9 using KeePassLib.Security;
10 using KeePass.UI;
11 using KeePassRPC.DataExchangeModel;
12 using KeePass.Forms;
13 using KeePassLib.Collections;
14 
15 namespace KeePassRPC.Forms
16 {
17     /// <summary>
18     /// We read and write to the GUI in the Advanced tab of the standard entry editing dialog. This allows us to cancel and commit changes when the user presses OK or cancel.
19     /// </summary>
20     public partial class KeeEntryUserControl : UserControl
21     {
22         private PwEntry _entry;
23         KeePassRPCExt KeePassRPCPlugin;
24         PwEntryForm _pwEntryForm;
25         ProtectedStringDictionary _strings;
26         EntryConfig _conf;
27         DatabaseConfig _dbConf;
28 
KeeEntryUserControl(KeePassRPCExt keePassRPCPlugin, PwEntry entry, CustomListViewEx advancedListView, PwEntryForm pwEntryForm, ProtectedStringDictionary strings)29         public KeeEntryUserControl(KeePassRPCExt keePassRPCPlugin, PwEntry entry,
30             CustomListViewEx advancedListView, PwEntryForm pwEntryForm, ProtectedStringDictionary strings)
31         {
32             KeePassRPCPlugin = keePassRPCPlugin;
33             _entry = entry;
34             InitializeComponent();
35             _pwEntryForm = pwEntryForm;
36             _strings = strings;
37             _dbConf = KeePassRPCPlugin._host.Database.GetKPRPCConfig();
38             _conf = entry.GetKPRPCConfig(strings, _dbConf.DefaultMatchAccuracy);
39         }
40 
changeAdvancedString(string name, string value, bool protect)41         private void changeAdvancedString(string name, string value, bool protect)
42         {
43             _strings.Set(name, new ProtectedString(protect, value));
44         }
45 
UpdateKPRPCJSON(EntryConfig _conf)46         private void UpdateKPRPCJSON(EntryConfig _conf)
47         {
48             // if the config is identical to an empty (default) config, only update if a JSON string already exists
49             if (!_conf.Equals(new EntryConfig(KeePassRPCPlugin._host.Database.GetKPRPCConfig().DefaultMatchAccuracy)) || _strings.GetKeys().Contains("KPRPC JSON"))
50                 changeAdvancedString("KPRPC JSON", Jayrock.Json.Conversion.JsonConvert.ExportToString(_conf), true);
51         }
52 
checkBoxHideFromKee_CheckedChanged(object sender, EventArgs e)53         private void checkBoxHideFromKee_CheckedChanged(object sender, EventArgs e)
54         {
55             if (checkBoxHideFromKee.Checked)
56             {
57                 _conf.Hide = true;
58                 textBoxKeePriority.Enabled = false;
59                 label1.Enabled = false;
60                 groupBox1.Enabled = false;
61                 groupBox2.Enabled = false;
62                 groupBox3.Enabled = false;
63                 groupBox4.Enabled = false;
64                 labelRealm.Enabled = false;
65                 textBoxKeeRealm.Enabled = false;
66             }
67             else
68             {
69                 _conf.Hide = false;
70                 textBoxKeePriority.Enabled = true;
71                 label1.Enabled = true;
72                 groupBox1.Enabled = true;
73                 groupBox2.Enabled = true;
74                 groupBox3.Enabled = true;
75                 groupBox4.Enabled = true;
76                 labelRealm.Enabled = true;
77                 textBoxKeeRealm.Enabled = true;
78             }
79             UpdateKPRPCJSON(_conf);
80         }
81 
KeeEntryUserControl_Load(object sender, EventArgs e)82         private void KeeEntryUserControl_Load(object sender, EventArgs e)
83         {
84             bool kfNeverAutoFill = false;
85             bool kfAlwaysAutoFill = false;
86             bool kfNeverAutoSubmit = false;
87             bool kfAlwaysAutoSubmit = false;
88 
89             if (_conf == null)
90                 return;
91 
92             this.checkBoxHideFromKee.CheckedChanged += new System.EventHandler(this.checkBoxHideFromKee_CheckedChanged);
93 
94             if (_conf.Hide) { checkBoxHideFromKee.Checked = true; }
95             if (_conf.GetMatchAccuracyMethod() == MatchAccuracyMethod.Exact)
96             {
97                 radioButton3.Checked = true;
98             }
99             else if (_conf.GetMatchAccuracyMethod() == MatchAccuracyMethod.Hostname)
100             {
101                 radioButton2.Checked = true;
102             }
103             else
104             {
105                 radioButton1.Checked = true;
106             }
107             RadioButton defaultRadioButton = null;
108             switch (_dbConf.DefaultMatchAccuracy)
109             {
110                 case MatchAccuracyMethod.Exact: defaultRadioButton = radioButton3; break;
111                 case MatchAccuracyMethod.Hostname: defaultRadioButton = radioButton2; break;
112                 case MatchAccuracyMethod.Domain: defaultRadioButton = radioButton1; break;
113             }
114             toolTipRealm.SetToolTip(defaultRadioButton, "This is the default behaviour for new entries. Change in the Database Settings dialog.");
115 
116             if (_conf.NeverAutoFill) { kfNeverAutoFill = true; }
117             if (_conf.AlwaysAutoFill) { kfAlwaysAutoFill = true; }
118             if (_conf.NeverAutoSubmit) { kfNeverAutoSubmit = true; }
119             if (_conf.AlwaysAutoSubmit) { kfAlwaysAutoSubmit = true; }
120             if (_conf.Priority != 0)
121                 textBoxKeePriority.Text = _conf.Priority.ToString();
122 
123             listNormalURLs = new List<string>();
124             listNormalBlockedURLs = new List<string>();
125             listRegExURLs = new List<string>();
126             listRegExBlockedURLs = new List<string>();
127 
128             if (_conf.AltURLs != null) listNormalURLs.AddRange(_conf.AltURLs);
129             if (_conf.BlockedURLs != null) listNormalBlockedURLs.AddRange(_conf.BlockedURLs);
130             if (_conf.RegExURLs != null) listRegExURLs.AddRange(_conf.RegExURLs);
131             if (_conf.RegExBlockedURLs != null) listRegExBlockedURLs.AddRange(_conf.RegExBlockedURLs);
132 
133             textBoxKeeRealm.Text = _conf.HTTPRealm;
134 
135             bool standardPasswordFound = false;
136             bool standardUsernameFound = false;
137 
138             // Read the list of form field objects and create ListViewItems for display to the user
139             if (_conf.FormFieldList != null)
140             {
141                 foreach (FormField ff in _conf.FormFieldList)
142                 {
143                     string type = Utilities.FormFieldTypeToDisplay(ff.Type, false);
144 
145                     // Override display of standard variables
146                     string value = ff.Value;
147                     if (ff.Type == FormFieldType.FFTpassword)
148                         value = "********";
149                     if (ff.DisplayName == "KeePass username")
150                     {
151                         standardUsernameFound = true;
152                         value = ff.DisplayName;
153                     }
154                     if (ff.DisplayName == "KeePass password")
155                     {
156                         standardPasswordFound = true;
157                         value = ff.DisplayName;
158                     }
159                     if (ff.Type == FormFieldType.FFTcheckbox)
160                     {
161                         value = ff.Value == "KEEFOX_CHECKED_FLAG_TRUE" ? "Enabled" : "Disabled";
162                     }
163                     ListViewItem lvi = new ListViewItem(new string[] { ff.Name, value, ff.Id, type, ff.Page.ToString() });
164                     lvi.Tag = ff;
165                     AddFieldListItem(lvi);
166                 }
167             }
168 
169             // if we didn't find specific details about the username and
170             // password, we'll pre-populate the standard KeePass ones so
171             // users can easily change things like page number and ID
172 
173             // we don't add them to the list of actual fields though - just the display list.
174             if (!standardPasswordFound)
175             {
176                 ListViewItem lvi = new ListViewItem(new string[] { "", "KeePass password", "", "password", "1" });
177                 AddFieldListItem(lvi);
178             }
179             if (!standardUsernameFound)
180             {
181                 ListViewItem lvi = new ListViewItem(new string[] { "", "KeePass username", "", "username", "1" });
182                 AddFieldListItem(lvi);
183             }
184 
185             ReadURLStrings();
186 
187             comboBoxAutoSubmit.Text = "Use Kee setting";
188             comboBoxAutoFill.Text = "Use Kee setting";
189 
190             // There are implicit behaviours based on single option choices so we'll make them explicit now so that the GUI accurately reflects the
191             // strings stored in the advanced tab
192             if (kfNeverAutoFill)
193                 currentBehaviour = EntryBehaviour.NeverAutoFillNeverAutoSubmit;
194             else if (kfAlwaysAutoSubmit)
195                 currentBehaviour = EntryBehaviour.AlwaysAutoFillAlwaysAutoSubmit;
196             else if (kfAlwaysAutoFill && kfNeverAutoSubmit)
197                 currentBehaviour = EntryBehaviour.AlwaysAutoFillNeverAutoSubmit;
198             else if (kfNeverAutoSubmit)
199                 currentBehaviour = EntryBehaviour.NeverAutoSubmit;
200             else if (kfAlwaysAutoFill)
201                 currentBehaviour = EntryBehaviour.AlwaysAutoFill;
202             else
203                 currentBehaviour = EntryBehaviour.Default;
204             changeBehaviourState(currentBehaviour);
205 
206             this.comboBoxAutoSubmit.SelectedIndexChanged += new System.EventHandler(this.comboBoxAutoSubmit_SelectedIndexChanged);
207             this.comboBoxAutoFill.SelectedIndexChanged += new System.EventHandler(this.comboBoxAutoFill_SelectedIndexChanged);
208             this.textBoxKeePriority.TextChanged += new System.EventHandler(this.textBoxKeePriority_TextChanged);
209 
210             string realmTooltip = "Set this to the realm (what the \"site says\") in the HTTP authentication popup dialog box for a more accurate match";
211             toolTipRealm.SetToolTip(this.textBoxKeeRealm, realmTooltip);
212             toolTipRealm.SetToolTip(this.labelRealm, realmTooltip);
213         }
214 
textBoxKeePriority_TextChanged(object sender, EventArgs e)215         private void textBoxKeePriority_TextChanged(object sender, EventArgs e)
216         {
217             string priority = ((System.Windows.Forms.TextBoxBase)sender).Text;
218 
219             if (!string.IsNullOrEmpty(priority))
220             {
221                 try
222                 {
223                     _conf.Priority = int.Parse(priority);
224                     UpdateKPRPCJSON(_conf);
225                     return;
226                 }
227                 catch (Exception)
228                 {
229                 }
230             }
231             _conf.Priority = 0;
232             UpdateKPRPCJSON(_conf);
233             return;
234         }
235 
textBoxKeeRealm_TextChanged(object sender, EventArgs e)236         private void textBoxKeeRealm_TextChanged(object sender, EventArgs e)
237         {
238             string realm = ((System.Windows.Forms.TextBoxBase)sender).Text;
239 
240             if (!string.IsNullOrEmpty(realm))
241                 _conf.HTTPRealm = realm;
242             else
243                 _conf.HTTPRealm = null;
244             UpdateKPRPCJSON(_conf);
245             return;
246         }
247 
comboBoxAutoFill_SelectedIndexChanged(object sender, EventArgs e)248         private void comboBoxAutoFill_SelectedIndexChanged(object sender, EventArgs e)
249         {
250             switch (comboBoxAutoFill.Text)
251             {
252                 case "Use Kee setting":
253                     if (comboBoxAutoSubmit.Text == "Never")
254                         changeBehaviourState(EntryBehaviour.NeverAutoSubmit);
255                     else
256                         changeBehaviourState(EntryBehaviour.Default);
257                     break;
258                 case "Never":
259                     changeBehaviourState(EntryBehaviour.NeverAutoFillNeverAutoSubmit);
260                     break;
261                 case "Always":
262                     if (comboBoxAutoSubmit.Text == "Never")
263                         changeBehaviourState(EntryBehaviour.AlwaysAutoFillNeverAutoSubmit);
264                     else if (comboBoxAutoSubmit.Text == "Always")
265                         changeBehaviourState(EntryBehaviour.AlwaysAutoFillAlwaysAutoSubmit);
266                     else
267                         changeBehaviourState(EntryBehaviour.AlwaysAutoFill);
268                     break;
269             }
270         }
271 
comboBoxAutoSubmit_SelectedIndexChanged(object sender, EventArgs e)272         private void comboBoxAutoSubmit_SelectedIndexChanged(object sender, EventArgs e)
273         {
274             switch (comboBoxAutoSubmit.Text)
275             {
276                 case "Use Kee setting":
277                     if (comboBoxAutoFill.Text == "Always")
278                         changeBehaviourState(EntryBehaviour.AlwaysAutoFill);
279                     else
280                         changeBehaviourState(EntryBehaviour.Default);
281                     break;
282                 case "Never":
283                     if (comboBoxAutoFill.Text == "Always")
284                         changeBehaviourState(EntryBehaviour.AlwaysAutoFillNeverAutoSubmit);
285                     else if (comboBoxAutoFill.Text == "Never")
286                         changeBehaviourState(EntryBehaviour.NeverAutoFillNeverAutoSubmit);
287                     else
288                         changeBehaviourState(EntryBehaviour.NeverAutoSubmit);
289                     break;
290                 case "Always":
291                     changeBehaviourState(EntryBehaviour.AlwaysAutoFillAlwaysAutoSubmit);
292                     break;
293             }
294         }
295 
296         enum EntryBehaviour
297         {
298             Default,
299             NeverAutoFillNeverAutoSubmit,
300             NeverAutoSubmit,
301             AlwaysAutoFillAlwaysAutoSubmit,
302             AlwaysAutoFill,
303             AlwaysAutoFillNeverAutoSubmit
304         }
305 
306         EntryBehaviour currentBehaviour = EntryBehaviour.Default;
307 
changeBehaviourState(EntryBehaviour behav)308         private void changeBehaviourState(EntryBehaviour behav)
309         {
310             switch (behav)
311             {
312                 case EntryBehaviour.AlwaysAutoFill:
313                     _conf.AlwaysAutoFill = true;
314                     _conf.AlwaysAutoSubmit = false;
315                     _conf.NeverAutoFill = false;
316                     _conf.NeverAutoSubmit = false;
317                     comboBoxAutoFill.Text = "Always";
318                     comboBoxAutoSubmit.Text = "Use Kee setting";
319                     comboBoxAutoFill.Enabled = true;
320                     comboBoxAutoSubmit.Enabled = true;
321                     break;
322                 case EntryBehaviour.NeverAutoSubmit:
323                     _conf.AlwaysAutoFill = false;
324                     _conf.AlwaysAutoSubmit = false;
325                     _conf.NeverAutoFill = false;
326                     _conf.NeverAutoSubmit = true;
327                     comboBoxAutoFill.Text = "Use Kee setting";
328                     comboBoxAutoSubmit.Text = "Never";
329                     comboBoxAutoFill.Enabled = true;
330                     comboBoxAutoSubmit.Enabled = true;
331                     break;
332                 case EntryBehaviour.AlwaysAutoFillAlwaysAutoSubmit:
333                     _conf.AlwaysAutoFill = true;
334                     _conf.AlwaysAutoSubmit = true;
335                     _conf.NeverAutoFill = false;
336                     _conf.NeverAutoSubmit = false;
337                     comboBoxAutoSubmit.Text = "Always";
338                     comboBoxAutoFill.Text = "Always";
339                     comboBoxAutoFill.Enabled = false;
340                     comboBoxAutoSubmit.Enabled = true;
341                     break;
342                 case EntryBehaviour.NeverAutoFillNeverAutoSubmit:
343                     _conf.AlwaysAutoFill = false;
344                     _conf.AlwaysAutoSubmit = false;
345                     _conf.NeverAutoFill = true;
346                     _conf.NeverAutoSubmit = true;
347                     comboBoxAutoFill.Text = "Never";
348                     comboBoxAutoSubmit.Text = "Never";
349                     comboBoxAutoSubmit.Enabled = false;
350                     comboBoxAutoFill.Enabled = true;
351                     break;
352                 case EntryBehaviour.AlwaysAutoFillNeverAutoSubmit:
353                     _conf.AlwaysAutoFill = true;
354                     _conf.AlwaysAutoSubmit = false;
355                     _conf.NeverAutoFill = false;
356                     _conf.NeverAutoSubmit = true;
357                     comboBoxAutoFill.Text = "Always";
358                     comboBoxAutoSubmit.Text = "Never";
359                     comboBoxAutoSubmit.Enabled = true;
360                     comboBoxAutoFill.Enabled = true;
361                     break;
362                 case EntryBehaviour.Default:
363                     _conf.AlwaysAutoFill = false;
364                     _conf.AlwaysAutoSubmit = false;
365                     _conf.NeverAutoFill = false;
366                     _conf.NeverAutoSubmit = false;
367                     comboBoxAutoFill.Text = "Use Kee setting";
368                     comboBoxAutoSubmit.Text = "Use Kee setting";
369                     comboBoxAutoSubmit.Enabled = true;
370                     comboBoxAutoFill.Enabled = true;
371                     break;
372             }
373             UpdateKPRPCJSON(_conf);
374             currentBehaviour = behav;
375         }
376 
377         List<string> listNormalURLs = new List<string>();
378         List<string> listRegExURLs = new List<string>();
379         List<string> listNormalBlockedURLs = new List<string>();
380         List<string> listRegExBlockedURLs = new List<string>();
381 
buttonURLAdd_Click(object sender, EventArgs e)382         private void buttonURLAdd_Click(object sender, EventArgs e)
383         {
384             List<string> all = new List<string>();
385             for (int i = 0; i < listView1.Items.Count; ++i)
386                 all.Add(listView1.Items[i].Text);
387 
388             using (KeeURLForm kfurlf = new KeeURLForm(false, false, null, null, all))
389             {
390                 if (kfurlf.ShowDialog() == DialogResult.OK)
391                 {
392                     //UpdateEntryStrings(false, false);
393                     //ResizeColumnHeaders();
394 
395                     if (kfurlf.Match && !kfurlf.RegEx)
396                     {
397                         listNormalURLs.Add(kfurlf.URL);
398                         ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "Normal", "Match" });
399                         AddURLListItem(lvi);
400                     }
401                     if (kfurlf.Block && !kfurlf.RegEx)
402                     {
403                         listNormalBlockedURLs.Add(kfurlf.URL);
404                         ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "Normal", "Block" });
405                         AddURLListItem(lvi);
406                     }
407                     if (kfurlf.Match && kfurlf.RegEx)
408                     {
409                         listRegExURLs.Add(kfurlf.URL);
410                         ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "RegEx", "Match" });
411                         AddURLListItem(lvi);
412                     }
413                     if (kfurlf.Block && kfurlf.RegEx)
414                     {
415                         listRegExBlockedURLs.Add(kfurlf.URL);
416                         ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "RegEx", "Block" });
417                         AddURLListItem(lvi);
418                     }
419                     UpdateURLStrings();
420                 }
421             }
422         }
423 
buttonURLEdit_Click(object sender, EventArgs e)424         private void buttonURLEdit_Click(object sender, EventArgs e)
425         {
426             ListView.SelectedListViewItemCollection lvsicSel = listView1.SelectedItems;
427 
428             List<string> all = new List<string>();
429             for (int i = 0; i < listView1.Items.Count; ++i)
430                 all.Add(listView1.Items[i].Text);
431 
432             for (int i = 0; i < lvsicSel.Count; ++i)
433             {
434                 List<string> others = all.GetRange(0, all.Count);
435                 others.Remove(lvsicSel[i].Text);
436 
437                 // find the current data
438                 using (KeeURLForm kfurlf = URLFormForEditing(lvsicSel, i, others))
439                 {
440                     if (kfurlf.ShowDialog() == DialogResult.OK)
441                     {
442                         // remove the old URL data
443                         if (lvsicSel[i].SubItems[1].Text == "Normal" && lvsicSel[i].SubItems[2].Text == "Match")
444                             listNormalURLs.Remove(lvsicSel[i].Text);
445                         else if (lvsicSel[i].SubItems[1].Text == "Normal" && lvsicSel[i].SubItems[2].Text == "Block")
446                             listNormalBlockedURLs.Remove(lvsicSel[i].Text);
447                         else if (lvsicSel[i].SubItems[1].Text == "RegEx" && lvsicSel[i].SubItems[2].Text == "Match")
448                             listRegExURLs.Remove(lvsicSel[i].Text);
449                         else if (lvsicSel[i].SubItems[1].Text == "RegEx" && lvsicSel[i].SubItems[2].Text == "Block")
450                             listRegExBlockedURLs.Remove(lvsicSel[i].Text);
451                         RemoveURLListItem(lvsicSel[i]);
452 
453                         //UpdateEntryStrings(false, false);
454                         //ResizeColumnHeaders();
455 
456                         if (kfurlf.Match && !kfurlf.RegEx)
457                         {
458                             listNormalURLs.Add(kfurlf.URL);
459                             ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "Normal", "Match" });
460                             AddURLListItem(lvi);
461                         }
462                         if (kfurlf.Block && !kfurlf.RegEx)
463                         {
464                             listNormalBlockedURLs.Add(kfurlf.URL);
465                             ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "Normal", "Block" });
466                             AddURLListItem(lvi);
467                         }
468                         if (kfurlf.Match && kfurlf.RegEx)
469                         {
470                             listRegExURLs.Add(kfurlf.URL);
471                             ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "RegEx", "Match" });
472                             AddURLListItem(lvi);
473                         }
474                         if (kfurlf.Block && kfurlf.RegEx)
475                         {
476                             listRegExBlockedURLs.Add(kfurlf.URL);
477                             ListViewItem lvi = new ListViewItem(new string[] { kfurlf.URL, "RegEx", "Block" });
478                             AddURLListItem(lvi);
479                         }
480                         UpdateURLStrings();
481                     }
482                 }
483             }
484         }
485 
URLFormForEditing(ListView.SelectedListViewItemCollection lvsicSel, int i, List<string> others)486         private static KeeURLForm URLFormForEditing(ListView.SelectedListViewItemCollection lvsicSel, int i, List<string> others)
487         {
488             KeeURLForm kfurlf = null;
489             if (lvsicSel[i].SubItems[1].Text == "Normal" && lvsicSel[i].SubItems[2].Text == "Match")
490                 kfurlf = new KeeURLForm(true, false, null, lvsicSel[i].Text, others);
491             else if (lvsicSel[i].SubItems[1].Text == "Normal" && lvsicSel[i].SubItems[2].Text == "Block")
492                 kfurlf = new KeeURLForm(false, true, null, lvsicSel[i].Text, others);
493             else if (lvsicSel[i].SubItems[1].Text == "RegEx" && lvsicSel[i].SubItems[2].Text == "Match")
494                 kfurlf = new KeeURLForm(true, false, lvsicSel[i].Text, null, others);
495             else if (lvsicSel[i].SubItems[1].Text == "RegEx" && lvsicSel[i].SubItems[2].Text == "Block")
496                 kfurlf = new KeeURLForm(false, true, lvsicSel[i].Text, null, others);
497             return kfurlf;
498         }
499 
buttonURLDelete_Click(object sender, EventArgs e)500         private void buttonURLDelete_Click(object sender, EventArgs e)
501         {
502             ListView.SelectedListViewItemCollection lvsicSel = listView1.SelectedItems;
503             for (int i = 0; i < lvsicSel.Count; ++i)
504             {
505                 if (lvsicSel[i].SubItems[1].Text == "Normal" && lvsicSel[i].SubItems[2].Text == "Match")
506                     listNormalURLs.Remove(lvsicSel[i].Text);
507                 else if (lvsicSel[i].SubItems[1].Text == "Normal" && lvsicSel[i].SubItems[2].Text == "Block")
508                     listNormalBlockedURLs.Remove(lvsicSel[i].Text);
509                 else if (lvsicSel[i].SubItems[1].Text == "RegEx" && lvsicSel[i].SubItems[2].Text == "Match")
510                     listRegExURLs.Remove(lvsicSel[i].Text);
511                 else if (lvsicSel[i].SubItems[1].Text == "RegEx" && lvsicSel[i].SubItems[2].Text == "Block")
512                     listRegExBlockedURLs.Remove(lvsicSel[i].Text);
513                 RemoveURLListItem(lvsicSel[i]);
514             }
515 
516             UpdateURLStrings();
517         }
518 
UpdateURLStrings()519         private void UpdateURLStrings()
520         {
521             _conf.AltURLs = listNormalURLs.ToArray();
522             _conf.BlockedURLs = listNormalBlockedURLs.ToArray();
523             _conf.RegExURLs = listRegExURLs.ToArray();
524             _conf.RegExBlockedURLs = listRegExBlockedURLs.ToArray();
525             UpdateKPRPCJSON(_conf);
526         }
527 
528 
UpdateFieldStrings()529         private void UpdateFieldStrings()
530         {
531             List<FormField> ffs = new List<FormField>();
532             foreach (ListViewItem lvi in listView2.Items)
533             {
534                 if (lvi.Tag != null)
535                     ffs.Add((FormField)lvi.Tag);
536             }
537             _conf.FormFieldList = ffs.ToArray();
538             UpdateKPRPCJSON(_conf);
539         }
540 
541 
ReadURLStrings()542         private void ReadURLStrings()
543         {
544             foreach (string url in listNormalURLs)
545             {
546                 ListViewItem lvi = new ListViewItem(new string[] { url, "Normal", "Match" });
547                 AddURLListItem(lvi);
548             }
549             foreach (string url in listNormalBlockedURLs)
550             {
551                 ListViewItem lvi = new ListViewItem(new string[] { url, "Normal", "Block" });
552                 AddURLListItem(lvi);
553             }
554             foreach (string url in listRegExURLs)
555             {
556                 ListViewItem lvi = new ListViewItem(new string[] { url, "RegEx", "Match" });
557                 AddURLListItem(lvi);
558             }
559             foreach (string url in listRegExBlockedURLs)
560             {
561                 ListViewItem lvi = new ListViewItem(new string[] { url, "RegEx", "Block" });
562                 AddURLListItem(lvi);
563             }
564         }
565 
AddURLListItem(ListViewItem lvi)566         private void AddURLListItem(ListViewItem lvi)
567         {
568             listView1.Items.Add(lvi);
569             //buttonURLEdit.Enabled = true;
570             //buttonURLDelete.Enabled = true;
571         }
572 
RemoveURLListItem(ListViewItem lvi)573         private void RemoveURLListItem(ListViewItem lvi)
574         {
575             listView1.Items.Remove(lvi);
576             if (listView1.Items.Count == 0)
577             {
578                 buttonURLEdit.Enabled = false;
579                 buttonURLDelete.Enabled = false;
580             }
581         }
582 
listView1_SelectedIndexChanged(object sender, EventArgs e)583         private void listView1_SelectedIndexChanged(object sender, EventArgs e)
584         {
585             if (listView1.SelectedItems.Count > 0)
586             {
587                 buttonURLEdit.Enabled = true;
588                 buttonURLDelete.Enabled = true;
589             } else
590             {
591                 buttonURLEdit.Enabled = false;
592                 buttonURLDelete.Enabled = false;
593             }
594         }
595 
buttonFieldAdd_Click(object sender, EventArgs e)596         private void buttonFieldAdd_Click(object sender, EventArgs e)
597         {
598             using (KeeFieldForm kff = new KeeFieldForm(null, null, null, FormFieldType.FFTtext, 1, PlaceholderHandling.Default))
599             {
600                 if (kff.ShowDialog() == DialogResult.OK)
601                 {
602                     FormField ff = new FormField(kff.Name, kff.Name, kff.Value, kff.Type, kff.Id, kff.Page, kff.PlaceholderHandling);
603 
604                     string type = Utilities.FormFieldTypeToDisplay(kff.Type, false);
605                     int page = kff.Page;
606 
607                     // We know any new passwords are not the main Entry password
608                     // Also know that the display name can be same as main name
609                     string displayValue = kff.Value;
610                     if (kff.Type == FormFieldType.FFTpassword)
611                     {
612                         displayValue = "********";
613                     }
614                     if (kff.Type == FormFieldType.FFTcheckbox)
615                     {
616                         displayValue = kff.Value == "KEEFOX_CHECKED_FLAG_TRUE" ? "Enabled" : "Disabled";
617                     }
618 
619                     ListViewItem lvi = new ListViewItem(new string[]
620                     {
621                         kff.Name, displayValue, kff.Id, type, page.ToString()
622                     });
623                     lvi.Tag = ff;
624                     AddFieldListItem(lvi);
625                     UpdateFieldStrings();
626                 }
627             }
628         }
629 
buttonFieldEdit_Click(object sender, EventArgs e)630         private void buttonFieldEdit_Click(object sender, EventArgs e)
631         {
632             ListView.SelectedListViewItemCollection lvsicSel = listView2.SelectedItems;
633 
634             FormField tag = (FormField)lvsicSel[0].Tag;
635             using (KeeFieldForm kff = FormFieldForEditing(lvsicSel, tag))
636             {
637                 if (kff.ShowDialog() == DialogResult.OK)
638                 {
639                     string displayValue = kff.Value;
640                     if (kff.Type == FormFieldType.FFTpassword)
641                     {
642                         displayValue = "********";
643                     }
644 
645                     string displayName = kff.Name;
646                     if (kff.Value == "{PASSWORD}")
647                     {
648                         displayName = "KeePass password";
649                         displayValue = "KeePass password";
650                     }
651                     else if (kff.Value == "{USERNAME}")
652                     {
653                         displayName = "KeePass username";
654                         displayValue = "KeePass username";
655                     }
656 
657                     if (kff.Type == FormFieldType.FFTcheckbox)
658                     {
659                         displayValue = kff.Value == "KEEFOX_CHECKED_FLAG_TRUE" ? "Enabled" : "Disabled";
660                     }
661 
662                     string type = Utilities.FormFieldTypeToDisplay(kff.Type, false);
663                     int page = kff.Page;
664 
665                     ListViewItem lvi = new ListViewItem(new string[] { kff.Name, displayValue, kff.Id, type, page.ToString() });
666                     lvi.Tag = new FormField(kff.Name, displayName, kff.Value, kff.Type, kff.Id, page, kff.PlaceholderHandling);
667                     RemoveFieldListItem(lvsicSel[0]);
668                     AddFieldListItem(lvi);
669                     UpdateFieldStrings();
670                 }
671             }
672         }
673 
FormFieldForEditing(ListView.SelectedListViewItemCollection lvsicSel, FormField tag)674         private static KeeFieldForm FormFieldForEditing(ListView.SelectedListViewItemCollection lvsicSel, FormField tag)
675         {
676             KeeFieldForm kfff = null;
677             if (tag != null)
678                 kfff = new KeeFieldForm(tag);
679             else if (lvsicSel[0].SubItems[1].Text == "KeePass password")
680                 kfff = new KeeFieldForm(lvsicSel[0].SubItems[0].Text, "{PASSWORD}", lvsicSel[0].SubItems[2].Text, FormFieldType.FFTpassword, int.Parse(lvsicSel[0].SubItems[4].Text), PlaceholderHandling.Default);
681             else if (lvsicSel[0].SubItems[1].Text == "KeePass username")
682                 kfff = new KeeFieldForm(lvsicSel[0].SubItems[0].Text, "{USERNAME}", lvsicSel[0].SubItems[2].Text, FormFieldType.FFTusername, int.Parse(lvsicSel[0].SubItems[4].Text), PlaceholderHandling.Default);
683             else
684                 throw new Exception("Corrupt Entry found!");
685             return kfff;
686         }
687 
buttonFieldDelete_Click(object sender, EventArgs e)688         private void buttonFieldDelete_Click(object sender, EventArgs e)
689         {
690             ListView.SelectedListViewItemCollection lvsicSel = listView2.SelectedItems;
691             // remove the old field data
692             RemoveFieldListItem(lvsicSel[0]);
693             UpdateFieldStrings();
694         }
695 
696         //ReadFields function????
697 
AddFieldListItem(ListViewItem lvi)698         private void AddFieldListItem(ListViewItem lvi)
699         {
700             listView2.Items.Add(lvi);
701         }
702 
RemoveFieldListItem(ListViewItem lvi)703         private void RemoveFieldListItem(ListViewItem lvi)
704         {
705             listView2.Items.Remove(lvi);
706             if (listView2.Items.Count == 0)
707             {
708                 buttonFieldEdit.Enabled = false;
709                 buttonFieldDelete.Enabled = false;
710             }
711         }
712 
listView2_SelectedIndexChanged(object sender, EventArgs e)713         private void listView2_SelectedIndexChanged(object sender, EventArgs e)
714         {
715             if (listView2.SelectedItems.Count > 0)
716             {
717                 buttonFieldEdit.Enabled = true;
718                 if (listView2.SelectedItems[0].SubItems[1].Text != "KeePass username"
719                     && listView2.SelectedItems[0].SubItems[1].Text != "KeePass password")
720                     buttonFieldDelete.Enabled = true;
721             }
722             else
723             {
724                 buttonFieldEdit.Enabled = false;
725                 buttonFieldDelete.Enabled = false;
726             }
727         }
728 
listView1_MouseDoubleClick(object sender, MouseEventArgs e)729         private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
730         {
731             if (listView1.SelectedItems.Count > 0)
732             {
733                 buttonURLEdit_Click(sender, e);
734             }
735         }
736 
listView2_MouseDoubleClick(object sender, MouseEventArgs e)737         private void listView2_MouseDoubleClick(object sender, MouseEventArgs e)
738         {
739             if (listView2.SelectedItems.Count > 0)
740             {
741                 buttonFieldEdit_Click(sender, e);
742             }
743         }
744 
radioButton1_CheckedChanged(object sender, EventArgs e)745         private void radioButton1_CheckedChanged(object sender, EventArgs e)
746         {
747             if (radioButton1.Checked)
748             {
749                 _conf.SetMatchAccuracyMethod(MatchAccuracyMethod.Domain);
750             }
751             UpdateKPRPCJSON(_conf);
752         }
753 
radioButton2_CheckedChanged(object sender, EventArgs e)754         private void radioButton2_CheckedChanged(object sender, EventArgs e)
755         {
756             if (radioButton2.Checked)
757             {
758                 _conf.SetMatchAccuracyMethod(MatchAccuracyMethod.Hostname);
759             }
760             UpdateKPRPCJSON(_conf);
761         }
762 
radioButton3_CheckedChanged(object sender, EventArgs e)763         private void radioButton3_CheckedChanged(object sender, EventArgs e)
764         {
765             if (radioButton3.Checked)
766             {
767                 _conf.SetMatchAccuracyMethod(MatchAccuracyMethod.Exact);
768             }
769             UpdateKPRPCJSON(_conf);
770         }
771 
listView1_KeyUp(object sender, KeyEventArgs e)772         private void listView1_KeyUp(object sender, KeyEventArgs e)
773         {
774             if (e.KeyCode == Keys.Delete)
775             {
776                 buttonURLDelete_Click(sender, e);
777             }
778         }
779 
listView2_KeyUp(object sender, KeyEventArgs e)780         private void listView2_KeyUp(object sender, KeyEventArgs e)
781         {
782             if (e.KeyCode == Keys.Delete)
783             {
784                 buttonFieldDelete_Click(sender, e);
785             }
786         }
787     }
788 }
789