File: DatabaseSettingsUserControl.cs

package info (click to toggle)
keepass2-plugin-keepassrpc 2.0.2%2Bdfsg2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,304 kB
  • sloc: cs: 29,001; makefile: 14
file content (227 lines) | stat: -rw-r--r-- 7,997 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using KeePassLib;
using KeePassRPC.Models.Shared;

namespace KeePassRPC.Forms
{
    public partial class DatabaseSettingsUserControl : UserControl
    {
        private PwDatabase database;
        private DatabaseConfig config;

        public DatabaseSettingsUserControl(PwDatabase db)
        {
            database = db;
            config = db.GetKPRPCConfig();
            InitializeComponent();
            PresentDefaultMatchAccuracy();
            PresentDomains();
            PresentDefaultPlaceholderHandling();
        }
        
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (ParentForm != null)
            {
                ParentForm.FormClosing +=
                  delegate
                  {
                      if (ParentForm.DialogResult == DialogResult.OK)
                      {
                          config.DefaultMatchAccuracy = DetermineDefaultMatchAccuracy();
                          config.MatchedURLAccuracyOverrides = DetermineMatchedURLAccuracyOverrides();
                          config.DefaultPlaceholderHandling = DetermineDefaultPlaceholderHandling();
                          database.SetKPRPCConfig(config);
                      }
                  };
            }
        }

        private void PresentDefaultMatchAccuracy()
        {
            switch (config.DefaultMatchAccuracy)
            {
                case MatchAccuracyMethod.Exact: radioButton6.Checked = true; break;
                case MatchAccuracyMethod.Hostname: radioButton5.Checked = true; break;
                case MatchAccuracyMethod.Domain: radioButton4.Checked = true; break;
            }
        }

        private MatchAccuracyMethod DetermineDefaultMatchAccuracy()
        {
            if (radioButton6.Checked) return MatchAccuracyMethod.Exact;
            if (radioButton5.Checked) return MatchAccuracyMethod.Hostname;
            return MatchAccuracyMethod.Domain;
        }

        private void PresentDefaultPlaceholderHandling()
        {
            if (config.DefaultPlaceholderHandling == PlaceholderHandling.Enabled) radioButton8.Checked = true;
            else radioButton7.Checked = true;
        }

        private PlaceholderHandling DetermineDefaultPlaceholderHandling()
        {
            if (radioButton8.Checked) return PlaceholderHandling.Enabled;
            return PlaceholderHandling.Disabled;
        }

        private Dictionary<string, MatchAccuracyMethod> DetermineMatchedURLAccuracyOverrides()
        {
            Dictionary<string, MatchAccuracyMethod> matchedURLAccuracyOverrides = 
                new Dictionary<string, MatchAccuracyMethod>();

            for (int i = 0; i < listView1.Items.Count; ++i)
            {
                matchedURLAccuracyOverrides.Add(
                    listView1.Items[i].Text, 
                    MatchAccuracyMethodFromText(listView1.Items[i].SubItems[1].Text));
            }
            return matchedURLAccuracyOverrides;
        }

        private void buttonURLAdd_Click(object sender, EventArgs e)
        {
            List<string> all = new List<string>();
            for (int i = 0; i < listView1.Items.Count; ++i)
                all.Add(listView1.Items[i].Text);

            using (var mamoForm = new KeeMAMOverrideForm(null, null, all))
            {
                if (mamoForm.ShowDialog() == DialogResult.OK)
                {
                    AddURLListItem(mamoForm.Domain, mamoForm.MAM);
                }
            }
        }

        private void buttonURLEdit_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection lvsicSel = listView1.SelectedItems;

            List<string> all = new List<string>();
            for (int i = 0; i < listView1.Items.Count; ++i)
                all.Add(listView1.Items[i].Text);

            for (int i = 0; i < lvsicSel.Count; ++i)
            {
                List<string> others = all.GetRange(0, all.Count);
                others.Remove(lvsicSel[i].Text);

                // find the current data
                using (var mamoForm = MAMOFormForEditing(lvsicSel, i, others))
                {
                    if (mamoForm.ShowDialog() == DialogResult.OK)
                    {
                        RemoveURLListItem(lvsicSel[i]);
                        AddURLListItem(mamoForm.Domain, mamoForm.MAM);
                    }
                }
            }
        }

        private static KeeMAMOverrideForm MAMOFormForEditing(ListView.SelectedListViewItemCollection lvsicSel, int i, List<string> others)
        {
            return new KeeMAMOverrideForm(lvsicSel[i].Text, MatchAccuracyMethodFromText(lvsicSel[i].SubItems[1].Text), others);
        }

        private static MatchAccuracyMethod MatchAccuracyMethodFromText(string text)
        {
            switch (text)
            {
                case "Domain": return MatchAccuracyMethod.Domain;
                case "Hostname": return MatchAccuracyMethod.Hostname;
                case "Exact": return MatchAccuracyMethod.Exact;
                default: throw new Exception("Invalid data for existing entry.");
            }
        }

        private void buttonURLDelete_Click(object sender, EventArgs e)
        {
            ListView.SelectedListViewItemCollection lvsicSel = listView1.SelectedItems;
            for (int i = 0; i < lvsicSel.Count; ++i)
            {
                RemoveURLListItem(lvsicSel[i]);
            }
        }

        private void PresentDomains()
        {
            if (config.MatchedURLAccuracyOverrides == null || config.MatchedURLAccuracyOverrides.Count == 0)
            {
                return;
            }

            foreach (var ovrride in config.MatchedURLAccuracyOverrides)
            {
                AddURLListItem(ovrride.Key, ovrride.Value);
            }
        }

        private void AddURLListItem(string domain, MatchAccuracyMethod mam)
        {
            switch (mam)
            {
                case MatchAccuracyMethod.Domain:
                    listView1.Items.Add(new ListViewItem(new[] { domain, "Domain" }));
                    break;
                case MatchAccuracyMethod.Hostname:
                    listView1.Items.Add(new ListViewItem(new[] { domain, "Hostname" }));
                    break;
                case MatchAccuracyMethod.Exact:
                    listView1.Items.Add(new ListViewItem(new[] { domain, "Exact" }));
                    break;
            }
        }

        private void RemoveURLListItem(ListViewItem lvi)
        {
            listView1.Items.Remove(lvi);
            if (listView1.Items.Count == 0)
            {
                buttonURLEdit.Enabled = false;
                buttonURLDelete.Enabled = false;
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                buttonURLEdit.Enabled = true;
                buttonURLDelete.Enabled = true;
            }
            else
            {
                buttonURLEdit.Enabled = false;
                buttonURLDelete.Enabled = false;
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                buttonURLEdit_Click(sender, e);
            }
        }

        private void listView1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                buttonURLDelete_Click(sender, e);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process p = new Process();
            p = Process.Start("https://forum.kee.pm/t/placeholder-handling/1100");
        }
    }
}