-
Notifications
You must be signed in to change notification settings - Fork 5
/
IconStatsHandler.cs
322 lines (260 loc) · 9.33 KB
/
IconStatsHandler.cs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
CustomIconDashboarder - KeePass Plugin to get some information and
manage custom icons
Copyright (C) 2023 Jareth Lomson <jareth.lomson@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using KeePassLib.Delegates;
using KeePassLib;
using LomsonLib.Utility;
namespace CustomIconDashboarderPlugin
{
/// <summary>
/// Description of Class1.
/// </summary>
public class IconStatsHandler
{
private Dictionary<PwUuid, IconStats> m_dicCustomIconsStats = null;
private Dictionary<PwIcon, IconStats> m_dicStandardIconsStats = null;
private bool m_isInitialized;
public IconStatsHandler()
{
m_dicCustomIconsStats = new Dictionary<PwUuid, IconStats>();
m_dicStandardIconsStats = new Dictionary<PwIcon, IconStats>();
}
public void Initialize( PwDatabase db) {
Debug.Assert( m_dicCustomIconsStats != null ); if (m_dicCustomIconsStats == null) throw new InvalidOperationException();
Debug.Assert( m_dicStandardIconsStats != null ); if (m_dicStandardIconsStats == null) throw new InvalidOperationException();
GroupHandler gh = delegate(PwGroup pg)
{
if ( !(pg.CustomIconUuid.Equals(PwUuid.Zero) ) ){
if (!(m_dicCustomIconsStats.ContainsKey( pg.CustomIconUuid)) ){
m_dicCustomIconsStats.Add( pg.CustomIconUuid, new IconStats());
}
m_dicCustomIconsStats[ pg.CustomIconUuid ].nbInGroups += 1;
m_dicCustomIconsStats[ pg.CustomIconUuid ].listGroups.Add( pg);
}
else {
if (!(m_dicStandardIconsStats.ContainsKey( pg.IconId)) ){
m_dicStandardIconsStats.Add( pg.IconId, new IconStats());
}
m_dicStandardIconsStats[ pg.IconId ].nbInGroups += 1;
m_dicStandardIconsStats[ pg.IconId ].listGroups.Add( pg);
}
return true;
};
String urlFieldValue;
Uri siteUri;
List<Uri> uriList;
EntryHandler eh = delegate(PwEntry pe)
{
if (!(pe.CustomIconUuid.Equals(PwUuid.Zero))) {
if (!(m_dicCustomIconsStats.ContainsKey(pe.CustomIconUuid))) {
m_dicCustomIconsStats.Add(pe.CustomIconUuid, new IconStats());
}
m_dicCustomIconsStats[pe.CustomIconUuid].nbInEntries += 1;
m_dicCustomIconsStats[pe.CustomIconUuid].listEntries.Add(pe);
uriList = m_dicCustomIconsStats[pe.CustomIconUuid].listUris;
}
else {
if (!(m_dicStandardIconsStats.ContainsKey(pe.IconId))) {
m_dicStandardIconsStats.Add(pe.IconId, new IconStats());
}
m_dicStandardIconsStats[pe.IconId].nbInEntries += 1;
m_dicStandardIconsStats[pe.IconId].listEntries.Add(pe);
uriList = m_dicStandardIconsStats[pe.IconId].listUris;
}
// Get Uri
urlFieldValue = pe.Strings.ReadSafe(PwDefs.UrlField);
if (!string.IsNullOrEmpty(urlFieldValue)) {
try {
urlFieldValue = URLUtility.addUrlHttpPrefix(urlFieldValue);
bool urlWellFFormatted = Uri.TryCreate(urlFieldValue, UriKind.Absolute, out siteUri);
if (urlWellFFormatted) {
if ((siteUri.Scheme.ToLower() == "http") ||
(siteUri.Scheme.ToLower() == "https"))
{
uriList.Add(siteUri);
}
}
}
catch (UriFormatException e)
{
Debug.Write(String.Format("URIFormatException during parsing uri of entry = {0}", e.Message));
}
catch (Exception) { };
}
return true;
};
gh( db.RootGroup );
db.RootGroup.TraverseTree(TraversalMethod.PreOrder, gh, eh);
m_isInitialized = true;
}
/// <summary>
/// Get number of usage of pci in groups for Custom Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>number of usage of pci in groups</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Nb")]
public int GetNbUsageInGroups(PwCustomIcon pci) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( pci.Uuid ) ) {
return m_dicCustomIconsStats[pci.Uuid].nbInGroups;
}
else {
return 0;
}
}
/// <summary>
/// Get number of usage of pci in entries for Custom Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>number of usage of pci in entries</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Nb")]
public int GetNbUsageInEntries(PwCustomIcon pci) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( pci.Uuid ) ) {
return m_dicCustomIconsStats[pci.Uuid].nbInEntries;
}
else {
return 0;
}
}
/// <summary>
/// Get List of entries for Custom Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>List of entries</returns>
public ICollection<PwEntry> GetListEntriesForPci( PwCustomIcon pci) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( pci.Uuid ) ) {
return m_dicCustomIconsStats[pci.Uuid].listEntries;
}
else {
return new List<PwEntry>();
}
}
/// <summary>
/// Get List of entries for Standard Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>List of entries</returns>
public ICollection<PwEntry> GetListEntriesForStandardIcon( PwIcon stdIcon) {
Debug.Assert( m_isInitialized);
if ( m_dicStandardIconsStats.ContainsKey( stdIcon ) ) {
return m_dicStandardIconsStats[stdIcon].listEntries;
}
else {
return new List<PwEntry>();
}
}
// <summary>
/// Get List of entries for Custom Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>List of entries</returns>
public ICollection<PwEntry> GetListEntriesForUuid( PwUuid puuid) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( puuid ) ) {
return m_dicCustomIconsStats[puuid].listEntries;
}
else {
return new List<PwEntry>();
}
}
/// <summary>
/// Get List of groups for Custom Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>List of groups</returns>
public ICollection<PwGroup> GetListGroupsForPci( PwCustomIcon pci) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( pci.Uuid ) ) {
return m_dicCustomIconsStats[pci.Uuid].listGroups;
}
else {
return new List<PwGroup>();
}
}
/// <summary>
/// Get List of groups for Standard Icon
/// </summary>
/// <param name="pci"></param>
/// <returns>List of groups</returns>
public ICollection<PwGroup> GetListGroupsForStandardIcon( PwIcon stdIcon) {
Debug.Assert( m_isInitialized);
if ( m_dicStandardIconsStats.ContainsKey( stdIcon ) ) {
return m_dicStandardIconsStats[stdIcon].listGroups;
}
else {
return new List<PwGroup>();
}
}
public ICollection<PwGroup> GetListGroupsFromUuid( PwUuid puuid) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( puuid ) ) {
return m_dicCustomIconsStats[puuid].listGroups;
}
else {
return new List<PwGroup>();
}
}
/// <summary>
/// Get List of Uris
/// </summary>
/// <param name="pci"></param>
/// <returns>List of groups</return>
public ICollection<Uri> GetListUris( PwCustomIcon pci) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( pci.Uuid ) ) {
return m_dicCustomIconsStats[pci.Uuid].listUris;
}
else {
return new List<Uri>();
}
}
/// <summary>
/// Get number of urls of pci in entries
/// </summary>
/// <param name="pci"></param>
/// <returns>number of urls of pci in entries</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Nb")]
public int GetNbUrlsInEntries(PwCustomIcon pci) {
Debug.Assert( m_isInitialized);
if ( m_dicCustomIconsStats.ContainsKey( pci.Uuid ) ) {
return m_dicCustomIconsStats[pci.Uuid].listUris.Count;
}
else {
return 0;
}
}
private class IconStats {
public int nbInGroups { get; set ;}
public int nbInEntries { get; set ;}
public List<PwEntry> listEntries {get; set;}
public List<PwGroup> listGroups {get; set; }
public List<Uri> listUris {get; set; }
public IconStats () {
nbInGroups = 0;
nbInEntries = 0;
listEntries = new List<PwEntry>();
listGroups = new List<PwGroup>();
listUris = new List<Uri>();
}
}
}
}