File: KeeURLForm.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 (113 lines) | stat: -rw-r--r-- 3,306 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
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using KeePass.UI;
using KeePassRPC.Properties;

namespace KeePassRPC.Forms
{
    public partial class KeeURLForm : Form
    {
        public bool Match;
        public bool Block;
        public bool RegEx;
        public string URL;
        public List<string> OtherKeys;

        private bool _editing;
        protected override void OnLoad(EventArgs e)
        {
            GlobalWindowManager.AddWindow(this);
            base.OnLoad(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            GlobalWindowManager.RemoveWindow(this);
            base.OnClosed(e);
        }

        public KeeURLForm(bool match, bool block, string regExURL, string url, List<string> otherKeys)
        {
            InitializeComponent();
            Icon = Resources.KPRPCico;
            Match = match;
            Block = block;
            OtherKeys = otherKeys;

            if (Block && Match)
                throw new ArgumentException("Can't block and match the same URL");

            if (!string.IsNullOrEmpty(regExURL))
            {
                RegEx = true;
                _editing = true;
                URL = regExURL;
            }
            else if (!string.IsNullOrEmpty(url))
            {
                _editing = true;
                URL = url;
            }

            textBox1.Text = URL;
            if (!Block && !Match)
                radioButtonMatch.Checked = true;
            else
            {
                radioButtonBlock.Checked = Block;
                radioButtonMatch.Checked = Match;
            }
            checkBoxRegEx.Checked = RegEx;

            if (_editing)
                Text = "Edit URL";
            else
                Text = "Add URL";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
                URL = textBox1.Text;
            else
                DialogResult = DialogResult.None;

            if (OtherKeys.Contains(URL))
            {
                MessageBox.Show(this, "A rule for '" + URL + "' has already been added.");
                DialogResult = DialogResult.None;
                return;
            }

            //TODO2: Check the URL follows an acceptable pattern

            if (!radioButtonBlock.Checked && !radioButtonMatch.Checked)
                DialogResult = DialogResult.None;

            RegEx = checkBoxRegEx.Checked;
            Block = radioButtonBlock.Checked;
            Match = radioButtonMatch.Checked;

            // Tell the user straight away if their Regex is bad
            if (RegEx)
            {
                try
                {
                    Regex test = new Regex(URL);
                }
                catch (ArgumentException ex)
                {
                    MessageBox.Show(this, "'" + URL + "' is not a valid regular expression. Details: " + ex.Message, "Invalid regular expression", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    DialogResult = DialogResult.None;
                }
            }
        }
    }
}