File: KeeGroupUserControl.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 (191 lines) | stat: -rw-r--r-- 7,145 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
using System;
using System.Windows.Forms;
using KeePassLib;
using KeePassLib.Utility;

namespace KeePassRPC.Forms
{
    public partial class KeeGroupUserControl : UserControl
    {
        private readonly KeePassRPCExt KeePassRPCPlugin;

        private PwGroup _group;

        private KeeHomeStatus _status = KeeHomeStatus.Unknown;

        private string _location = "";

        private KeeHomeStatus Status { get {
            if (_status == KeeHomeStatus.Unknown)
            {
                PwGroup _rootGroup = KeePassRPCPlugin.RPCService.GetRootPwGroup(KeePassRPCPlugin._host.Database, _location);
                var rid = KeePassRPCPlugin._host.Database.RecycleBinUuid;

                if (_rootGroup.Uuid.Equals(_group.Uuid))
                    _status = KeeHomeStatus.Home;
                else if (rid != null && !ReferenceEquals(rid, PwUuid.Zero) && _group.IsOrIsContainedIn(KeePassRPCPlugin._host.Database.RootGroup.FindGroup(rid, true)))
                        _status = KeeHomeStatus.Rubbish;
                else if (_group.IsContainedIn(_rootGroup)) // returns true when _group is main root and custom root group has been selected.
                    _status = KeeHomeStatus.Inside;
                else
                    _status = KeeHomeStatus.Outside;

            }
            return _status;
        
        } }

        public KeeGroupUserControl(KeePassRPCExt keePassRPCPlugin, PwGroup group)
        {
            KeePassRPCPlugin = keePassRPCPlugin;
            _group = group;
            InitializeComponent();
        }

        private void KeeGroupUserControl_Load(object sender, EventArgs e)
        {
            UpdateStatus();
            
            l_homeExplanation.Text = @"Kee will only know about the groups
and entries that are inside your Home group";

            UpdateLocations();
        }

        private void UpdateLocations()
        {
            comboBoxLocation.Items.Clear();
            comboBoxLocation.Items.Add("Anywhere");
            string klconf = KeePassRPCPlugin._host.CustomConfig.GetString("KeePassRPC.knownLocations");
            if (!string.IsNullOrEmpty(klconf))
            {
                string[] knownLocations = klconf.Split(',');
                foreach (string location in knownLocations)
                {
                    comboBoxLocation.Items.Add(location);
                }
            }
            comboBoxLocation.Items.Add("Location manager...");
            comboBoxLocation.SelectedIndex = 0;
        }

        private void UpdateStatus()
        {
            switch (Status)
            {
                case KeeHomeStatus.Home:
                    l_status.Text = @"This is the Kee Home group. Kee can see and work with
this group and all groups and entries that are contained within.";
                    buttonMakeHome.Enabled = false;
                    break;
                case KeeHomeStatus.Inside:
                    l_status.Text = @"Kee can see and work with this group.";
                    buttonMakeHome.Enabled = true;
                    break;
                case KeeHomeStatus.Outside:
                    l_status.Text = @"This group is hidden from Kee. You must change your Home
group if you want Kee to work with this group.";
                    buttonMakeHome.Enabled = true;
                    break;
                case KeeHomeStatus.Rubbish:
                    l_status.Text = @"This group is hidden from Kee. You must remove it from
the recycle bin if you want Kee to work with this group.";
                    buttonMakeHome.Enabled = false;
                    break;
            }
        }

        private void buttonMakeHome_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_location))
            {
                var conf = KeePassRPCPlugin._host.Database.GetKPRPCConfig();
                conf.RootUUID = MemUtil.ByteArrayToHexString(_group.Uuid.UuidBytes);
                KeePassRPCPlugin._host.Database.SetKPRPCConfig(conf);
            }
            else
            {
                // set the root group for a particular location
                string currentLocationRoots = KeePassRPCPlugin._host.CustomConfig.GetString("KeePassRPC.knownLocations." + _location + ".RootGroups","");

                currentLocationRoots = CleanUpLocationRootGroups(currentLocationRoots);
                
                if (currentLocationRoots.Length > 0)
                    currentLocationRoots += ",";
                currentLocationRoots += MemUtil.ByteArrayToHexString(_group.Uuid.UuidBytes);

                KeePassRPCPlugin._host.CustomConfig.SetString("KeePassRPC.knownLocations." + _location + ".RootGroups",
                    currentLocationRoots);
                KeePassRPCPlugin._host.MainWindow.Invoke((MethodInvoker)delegate { KeePassRPCPlugin._host.MainWindow.SaveConfig(); });
            }
            
            _status = KeeHomeStatus.Unknown;
            UpdateStatus();
            KeePassRPCPlugin._host.MainWindow.UpdateUI(false, null, true, null, true, null, true);
        }

        private string CleanUpLocationRootGroups(string currentLocationRoots)
        {
            string[] guids = currentLocationRoots.Split(',');
            string newLocationRoots = "";
            foreach (string guid in guids)
            {
                if (guid.Length <= 0)
                    continue;

                PwUuid uuid = new PwUuid(MemUtil.HexStringToByteArray(guid)); ;

                if (KeePassRPCPlugin._host.Database.RootGroup.Uuid.Equals(uuid))
                    continue;

                PwGroup group = KeePassRPCPlugin._host.Database.RootGroup.FindGroup(uuid, true);
                
                // only keep this group UUID if it might be from a different database
                if (group == null)
                    newLocationRoots += guid + ",";
            }
            if (newLocationRoots.Length > 0)
                newLocationRoots = newLocationRoots.Substring(0, newLocationRoots.Length - 1);

            return newLocationRoots;
        }

        private void comboBoxLocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = (string)comboBoxLocation.SelectedItem;

            if (selected == "Location manager...")
            {
                // launch location manager
                using (LocationManager lm = new LocationManager(KeePassRPCPlugin))
                {
                    if (lm.ShowDialog() == DialogResult.OK)
                        UpdateLocations();
                }
            }
            else if (selected == "Anywhere")
            {
                // use default home group
                _location = "";
                _status = KeeHomeStatus.Unknown;
                UpdateStatus();
            }
            else
            {
                _location = selected;
                _status = KeeHomeStatus.Unknown;
                UpdateStatus();
            }
        }

    }

    internal enum KeeHomeStatus
    {
        Unknown,
        Rubbish,
        Home,
        Inside,
        Outside
    }
}