This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poller.cs
346 lines (285 loc) · 11.5 KB
/
Poller.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Configuration;
/**********************************Simple Tray Icon sample DOTNET 2.0***********************************************
* This class creates the notification icon that dotnet 2.0 offers.
* It will be displaying the status of the application with appropiate icons.
* It will have a contextmenu that enables the user to open the form or exit the application.
* The form could be used to change settings of the app which in turn are saved in the app.config or some other file.
* This formless, useless, notification sample does only chane the icon and balloontext.
* NOTE:Chacker is a Singleton class so it will only allow to be instantiated once, and therefore only one instance.
* I have done this to prevent more then one icon on the tray and to share data with the form (if any)
*
******************************************************************************************************************/
namespace EwfUtil
{
class Poller : IDisposable
{
//Checker is a singleton
private static readonly Poller checker = new Poller();
String drive = ConfigurationManager.AppSettings["drive"];
int threshold = Int32.Parse(ConfigurationManager.AppSettings["memoryThreshold"]);
String startup = ConfigurationManager.AppSettings["onStartup"];
bool enabled = false;
String boot_cmd;
int usage;
ToolStripMenuItem discardMenuItem;
ToolStripMenuItem commitMenuItem;
ToolStripMenuItem disableMenuItem;
ToolStripMenuItem enableMenuItem;
//timer
bool IsDisposing = false;
int timeout = Int32.Parse(ConfigurationManager.AppSettings["pollingInterval"]); //30 sec
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
//notify icon: prepare the icons we may use in the notification
NotifyIcon notify;
System.Drawing.Icon iconok = System.Drawing.Icon.FromHandle((new System.Drawing.Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("EwfUtil.Images.drive.png"))).GetHicon());
System.Drawing.Icon iconwarn = System.Drawing.Icon.FromHandle((new System.Drawing.Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("EwfUtil.Images.drive_error.png"))).GetHicon());
System.Drawing.Icon iconoff = System.Drawing.Icon.FromHandle((new System.Drawing.Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("EwfUtil.Images.drive_off.png"))).GetHicon());
//GUI: the form is not loaded into memory before it used, after use it is removed from memory
//The notification icon has a contextmenu
Config form;
bool formPresent=false;
ContextMenuStrip contextmenu = new ContextMenuStrip();
/**************************** Singleton *****************************************************************
* Make the constructor private and create a public method that returns the object reference.
* The method must be static to be able to be called from different classes at any given moment.
* The object is created when the first reference is ask for.
* After that no more instances are created.
********************************************************************************************************/
public static Poller GetCheckerObject()
{
return checker;
}
public int Interval
{
get
{
return this.timeout;
}
set
{
this.timeout = value;
}
}
public String Drive
{
get
{
return this.drive;
}
set
{
this.drive = value;
}
}
public int Threshold
{
get
{
return this.threshold;
}
set
{
this.threshold = value;
}
}
public String Startup
{
get
{
return this.startup;
}
set
{
this.startup = value;
}
}
private Poller() //singleton so private constructor!
{
//create menu
//popup contextmenu when doubleclicked or when clicked in the menu
enableMenuItem = new ToolStripMenuItem("Enable on Reboot");
enableMenuItem.Click += new EventHandler(notify_setEnable);
contextmenu.Items.Add(enableMenuItem);
discardMenuItem = new ToolStripMenuItem("Discard on Reboot");
discardMenuItem.Click += new EventHandler(notify_setDiscard);
contextmenu.Items.Add(discardMenuItem);
commitMenuItem = new ToolStripMenuItem("Commit on Reboot");
commitMenuItem.Click += new EventHandler(notify_setCommit);
contextmenu.Items.Add(commitMenuItem);
disableMenuItem = new ToolStripMenuItem("Commit && Disable on Reboot");
disableMenuItem.Click += new EventHandler(notify_setDisable);
contextmenu.Items.Add(disableMenuItem);
ToolStripSeparator sep = new ToolStripSeparator();
contextmenu.Items.Add(sep);
ToolStripMenuItem item = new ToolStripMenuItem("Commit && Disable NOW");
item.Click += new EventHandler(notify_setDisableNow);
contextmenu.Items.Add(item);
sep = new ToolStripSeparator();
contextmenu.Items.Add(sep);
item = new ToolStripMenuItem("Config");
item.Click += new EventHandler(notify_config);
contextmenu.Items.Add(item);
sep = new ToolStripSeparator();
contextmenu.Items.Add(sep);
item = new ToolStripMenuItem("Exit");
item.Click += new EventHandler(Menu_OnExit);
contextmenu.Items.Add(item);
//notifyicon
notify = new NotifyIcon();
notify.Icon = iconoff;
notify.Text = "";
notify.BalloonTipTitle = "EwfUtil Warning";
notify.BalloonTipText = "Your EWF overlay is getting full. Commit and/or reboot now to secure your data.";
notify.ContextMenuStrip = contextmenu;
//notify.DoubleClick += new EventHandler(notify_DoubleClick); //show form when double clicked
notify.Visible = true;
//timer for sample actions
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = this.timeout * 1000;
timer.Start();
if (startup == "Commit")
UpdateStatus("-commit");
else if (startup == "Discard")
UpdateStatus("-nocmd");
else
UpdateStatus("");
}
//you could do some real checking here
void timer_Tick(object sender, EventArgs e)
{
UpdateStatus("");
}
public void restartTimer()
{
timer.Stop();
timer.Interval = this.timeout * 1000;
timer.Start();
}
private void UpdateStatus(String args)
{
enabled = false;
ProcessStartInfo psi = new ProcessStartInfo("ewfmgr.exe");
psi.Arguments = String.Format("{0} {1}", args, drive);
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
Process p = Process.Start(psi);
String line;
usage = 0;
char[] chars = new char[] { 'b', 'y', 't', 'e', 's', ' ' };
while ((line = p.StandardOutput.ReadLine()) != null)
{
if (line.Trim().ToLower().StartsWith("boot command"))
boot_cmd = line.Substring(line.IndexOf("ommand") + 7).Trim();
if (line.Trim().ToLower().StartsWith("state"))
enabled = line.Substring(line.IndexOf("state") + 8).Trim().ToLower().Equals("enabled");
//System.Windows.Forms.MessageBox.Show(line.Substring(line.IndexOf("state") + 6));
if (line.Trim().ToLower().StartsWith("memory used for data"))
usage += Int32.Parse(line.Substring(line.IndexOf("data") + 5).Trim(chars));
if (line.Trim().ToLower().StartsWith("memory used for mapping"))
usage += Int32.Parse(line.Substring(line.IndexOf("ping") + 5).Trim(chars));
}
discardMenuItem.Checked = false;
commitMenuItem.Checked = false;
disableMenuItem.Checked = false;
enableMenuItem.Checked = false;
if (boot_cmd == "NO_CMD")
{
discardMenuItem.Checked = true;
}
else if (boot_cmd == "COMMIT")
{
commitMenuItem.Checked = true;
}
else if (boot_cmd == "DISABLE")
{
disableMenuItem.Checked = true;
}
else if (boot_cmd == "ENABLE")
{
enableMenuItem.Checked = true;
}
if (enabled)
{
float u = usage / (1024 * 1024);
notify.Text = String.Format("EWF Usage: {0:##0.0} MB", u);
if (u > threshold)
{
notify.Icon = iconwarn;
notify.ShowBalloonTip(5000);
}
else
notify.Icon = iconok;
}
else
{
notify.Icon = iconoff;
notify.Text = "EWF disabled";
}
}
void notify_config(object sender, EventArgs e)
{
//show the form for settings
//prevent user from creating more then one form
if (!formPresent)
{
formPresent = true;
form = new Config();
//use close event to reset formpresent boolean
form.FormClosed += new FormClosedEventHandler(form_FormClosed);
form.Show();
}
}
void form_FormClosed(object sender, FormClosedEventArgs e)
{
formPresent = false;
form = null;
}
~Poller()
{
Dispose();
}
public void Dispose()
{
if (!IsDisposing)
{
timer.Stop();
IsDisposing = true;
}
}
void Menu_OnExit(Object sender, EventArgs e)
{
//be sure to call Application.Exit
notify.Visible = false;
Dispose();
Application.Exit();
}
void notify_setDiscard(Object sender, EventArgs e)
{
UpdateStatus("-nocmd");
}
void notify_setCommit(Object sender, EventArgs e)
{
UpdateStatus("-commit");
}
void notify_setDisable(Object sender, EventArgs e)
{
UpdateStatus("-commitanddisable");
}
void notify_setDisableNow(Object sender, EventArgs e)
{
UpdateStatus("-commitanddisable -live");
}
void notify_setEnable(Object sender, EventArgs e)
{
UpdateStatus("-enable");
}
}
}