-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeeStatsExt.cs
162 lines (138 loc) · 5.15 KB
/
KeeStatsExt.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
/*
KeeStats - A plugin for Keepass Password Manager
Copyright (C) 2014 Andrea Decorte
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.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using KeePass.Plugins;
using KeePassLib;
namespace KeeStats
{
/// <summary>
/// This is the main plugin class
/// </summary>
public sealed class KeeStatsExt : Plugin
{
private IPluginHost m_host = null;
// Menus
private ToolStripSeparator m_tsSeparator = null;
private ToolStripSeparator m_tsSeparator2 = null;
private ToolStripMenuItem m_tsmiStats = null;
private ToolStripMenuItem m_GroupStats = null;
/// <summary>
/// The <c>Initialize</c> function is called by KeePass when
/// you should initialize your plugin (create menu items, etc.).
/// </summary>
/// <param name="host">Plugin host interface. By using this
/// interface, you can access the KeePass main window and the
/// currently opened database.</param>
/// <returns>You must return <c>true</c> in order to signal
/// successful initialization. If you return <c>false</c>,
/// KeePass unloads your plugin (without calling the
/// <c>Terminate</c> function of your plugin).</returns>
public override bool Initialize(IPluginHost host)
{
Debug.Assert(host != null);
if(host == null) return false;
m_host = host;
// Get a reference to the 'Tools' menu item container
ToolStripItemCollection tsMenu = m_host.MainWindow.ToolsMenu.DropDownItems;
// Add a separator at the bottom
m_tsSeparator = new ToolStripSeparator();
tsMenu.Add(m_tsSeparator);
m_tsmiStats = new ToolStripMenuItem();
m_tsmiStats.Text = "&View stats...";
tsMenu.Add(m_tsmiStats);
m_tsmiStats.Click += OnMenuViewStats;
// Add a seperator and menu item to the group context menu
ContextMenuStrip contextMenu = m_host.MainWindow.GroupContextMenu;
m_tsSeparator2 = new ToolStripSeparator();
contextMenu.Items.Add(m_tsSeparator2);
m_GroupStats = new ToolStripMenuItem();
m_GroupStats.Text = "View stats for this group...";
m_GroupStats.Click += OnMenuGroupStats;
contextMenu.Items.Add(m_GroupStats);
return true; // Initialization successful
}
/// <summary>
/// The <c>Terminate</c> function is called by KeePass when
/// you should free all resources, close open files/streams,
/// etc. It is also recommended that you remove all your
/// plugin menu items from the KeePass menu.
/// </summary>
public override void Terminate()
{
// Remove all of our menu items
ToolStripItemCollection tsMenu = m_host.MainWindow.ToolsMenu.DropDownItems;
tsMenu.Remove(m_tsSeparator);
tsMenu.Remove(m_tsmiStats);
// Remove group context menu items
ContextMenuStrip contextMenu = m_host.MainWindow.GroupContextMenu;
contextMenu.Items.Remove(m_tsSeparator2);
contextMenu.Items.Remove(m_GroupStats);
}
private void OnMenuViewStats(object sender, EventArgs e)
{
if(!m_host.Database.IsOpen)
{
MessageBox.Show("You first need to open a database!", "KeeStats");
return;
}
ComputeStats(m_host.Database.RootGroup);
}
private void OnMenuGroupStats(object sender, EventArgs e)
{
if(!m_host.Database.IsOpen)
{
MessageBox.Show("You first need to open a database!", "KeeStats");
return;
}
PwGroup theGroup = m_host.MainWindow.GetSelectedGroup();
Debug.Assert(theGroup != null); if (theGroup == null) return;
ComputeStats(theGroup);
}
/// <summary>
/// Computes the statistics
/// </summary>
/// <param name="group">The group on which to calculate the statistics</param>
private void ComputeStats(PwGroup group)
{
List<StatItem> statsList = new List<StatItem>();
List<ExtendedStatItem> extended_stats = new List<ExtendedStatItem>();
// By default is recursive
if (!StatComputer.ComputeStats(group, ref statsList, ref extended_stats, true)) {
// exit if no password
MessageBox.Show("No passwords in this group", "KeeStats", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
// Show the window
StatsSummaryWindow theWindow = new StatsSummaryWindow(statsList, extended_stats);
theWindow.Database = m_host.Database;
theWindow.Group = group;
// We need the icons for the Edit Entry Form
theWindow.Icons = m_host.MainWindow.ClientIcons;
theWindow.Show();
}
// Set this link to where the version update file is located
public override string UpdateUrl
{
get
{
return "https://github.com/klenje/KeeStats/blob/master/version.txt";
}
}
}
}