-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmMain.cs
203 lines (163 loc) · 5.69 KB
/
frmMain.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace OpenPGPExplorer
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
OpenPGP.StatusUpdate += StatusUpdateCallback;
Worker.StatusUpdate += StatusUpdateCallback;
Worker.AddComplete += AddComplete;
Worker.ProcessComplete += ProcessComplete;
Worker.Error += WorkerError;
}
private void ResetStatus()
{
pbStatus.Value = 0;
pbStatus.Visible = false;
lblStatus.Text = "";
}
private void ProcessComplete(ByteBlock BB, TreeNode tn)
{
if (InvokeRequired)
{
BeginInvoke(new ProcessCompleteDelegate(ProcessComplete), new object[] { BB, tn });
return;
}
ResetStatus();
if (tn.Nodes.Count == 0)
{
CreateBlockTree(tn, BB.ChildBlock);
RefreshNodes(tn.Nodes);
}
}
private void StatusUpdateCallback(string Message, int Percent)
{
if (InvokeRequired)
{
BeginInvoke(new StatusUpdates(StatusUpdateCallback), new object[] { Message, Percent });
return;
}
lblStatus.Text = Message;
if (Percent >= 0 && Percent <=100)
{
pbStatus.Visible = true;
pbStatus.Value = Percent;
}
}
private void AddComplete(string FileName, ByteBlock StartBlock)
{
if (InvokeRequired)
{
BeginInvoke(new AddCompleteDelegate(AddComplete), new object[] { FileName, StartBlock });
return;
}
ResetStatus();
var tn = treeView1.Nodes.Add(FileName);
tn.ImageKey = "file";
tn.NodeFont = new Font(treeView1.Font, FontStyle.Bold);
tn.Text = FileName;
CreateBlockTree(tn, StartBlock);
RefreshNodes(treeView1.Nodes);
}
private void WorkerError(string Message, bool Reset)
{
if (InvokeRequired)
{
BeginInvoke(new ErrorDelegate(WorkerError), new object[] { Message, Reset });
return;
}
if (Reset)
ResetStatus();
MessageBox.Show(this, Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void CreateBlockTree(TreeNode tn, ByteBlock bt)
{
while (bt != null)
{
var newNode = tn.Nodes.Add(bt.Label + (string.IsNullOrEmpty(bt.Description) ? "" : ": " + bt.Description));
newNode.Tag = bt;
if (bt.ChildBlock != null)
CreateBlockTree(newNode, bt.ChildBlock);
bt = bt.NextBlock;
}
}
private void RefreshNodes(TreeNodeCollection col)
{
if (col == null)
return;
foreach(TreeNode n in col)
{
if (n.Tag is ByteBlock bt)
{
if (bt.Type == ByteBlockType.Calculated)
n.ForeColor = Color.Blue;
else if (bt.Type == ByteBlockType.CalculatedError)
n.ForeColor = Color.Red;
else if (bt.Type == ByteBlockType.CalculatedSuccess)
n.ForeColor = Color.Green;
if (bt is PacketBlock pb)
{
n.ToolTipText = pb.Message;
if (pb.PGPPacket is PublicKeyPacket)
n.ImageKey = "key";
else if (pb.PGPPacket is UserIDPacket)
n.ImageKey = "user";
else if (pb.PGPPacket is SignaturePacket)
if (pb.Type == ByteBlockType.CalculatedSuccess)
n.ImageKey = "tick";
else
n.ImageKey = "cross";
else
n.ImageKey = "packet";
}
}
RefreshNodes(n.Nodes);
}
}
private void Clear()
{
treeView1.Nodes.Clear();
txtValue.Text = "";
txtValueText.Text = "";
OpenPGP.Reset();
}
private void DoNodeAction(TreeNode tn)
{
if (tn.Tag != null && tn.Nodes.Count == 0)
{
var BB = (ByteBlock)tn.Tag;
txtValue.Text = BitConverter.ToString(BB.RawBytes);
txtValueText.Text = Encoding.UTF8.GetString(BB.RawBytes);
if (BB.ProcessBlock != null)
Worker.ProcessBlock(BB, tn);
}
}
private void cmdBrowse_Click(object sender, EventArgs e)
{
FileDialog fd = new OpenFileDialog();
if (fd.ShowDialog(this) == DialogResult.OK)
{
Worker.AddFile(fd.FileName);
}
}
private void cmdClear_Click(object sender, EventArgs e)
{
Clear();
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
DoNodeAction(e.Node);
}
}
}