-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
272 lines (239 loc) · 9.36 KB
/
MainForm.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
using System.Numerics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Globalization;
namespace SlashUSB
{
public partial class MainForm : Form
{
private const string ActivatedGroupName = "listViewGroupAktivert";
private const string FoundGroupName = "listViewGroupFunnet";
readonly USBDetect usbdetector = new();
readonly List<string> hubList = new();
readonly List<string> activatedHubList = new();
bool mDeactivate = false;
public MainForm()
{
InitializeComponent();
usbdetector.USBinserted += C_USBInserted;
usbdetector.USBremoved += C_USBRemoved;
versionLabel.Text = strings.PreVersion + " " + typeof(MainForm).Assembly.GetName().Version?.ToString() ?? "--";
}
private UInt32 queryCancelAutoPlay = 0;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (queryCancelAutoPlay == 0)
{
queryCancelAutoPlay = Pinvoke.RegisterWindowMessage("QueryCancelAutoPlay");
// Open up filter for this message when in administrator mode (and we are)
Pinvoke.ChangeWindowMessageFilter(queryCancelAutoPlay,Pinvoke.MSGFLT_ADD);
}
//if the window message id equals the QueryCancelAutoPlay message id
if (m.Msg == queryCancelAutoPlay)
{
/* only needed if your application is using a dialog box and needs to
* respond to a "QueryCancelAutoPlay" message, it cannot simply return TRUE or FALSE.
SetWindowLong(this.Handle, 0, 1);
*/
Pinvoke.SetWindowLongPtr(this.Handle, 0, new IntPtr(1));
m.Result = (IntPtr)1;
}
}
private void AktivertCheckBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox? activated = sender as CheckBox;
if (activated != null)
{
activated.Text = activated.Checked ? strings.Deaktiver : strings.Aktiver;
UpdateEmergencyLight();
}
}
private void UpdateEmergencyLight()
{
var actGrp = usbListView.Groups[ActivatedGroupName];
emergencyLight.Visible = activatedCB.Checked && actGrp.Items.Count>0;
}
private void C_USBInserted(object? sender, EventArgs e)
{
USB_EventInfo? usbInfo = e as USB_EventInfo;
if (usbInfo != null)
this.Invoke((MethodInvoker)delegate
{
InsertDevice(usbInfo);
});
}
private void C_USBRemoved(object? sender, USB_RemovedEvent e)
{
USB_RemovedEvent? usbInfo = e as USB_RemovedEvent;
if (usbInfo != null)
this.Invoke((MethodInvoker)delegate
{
RemoveDevice(usbInfo.DeviceName);
});
}
private void InsertDevice(USB_EventInfo usbInfo)
{
DetermineHubIndex(usbInfo);
bool aktivert = (activatedHubList.FindIndex(x => x == usbInfo.HubFriendlyName) != -1);
var usb = new USBmemoryDevice(usbListView, usbInfo, aktivert ? ActivatedGroupName : FoundGroupName);
if (activatedCB.Checked && aktivert && usb.InstanceAdded)
RunJob(usb);
}
private void DetermineHubIndex(USB_EventInfo usbInfo)
{
if(usbInfo.HubID == null ) return;
var index = hubList.FindIndex(x => x == usbInfo.HubID);
if (index == -1)
{
hubList.Add(usbInfo.HubID);
index = hubList.Count - 1;
}
usbInfo.HubFriendlyName = index.ToString();
}
private void RemoveDevice(string? deviceName)
{
if (deviceName == null)
return;
var result = usbListView.Items.Find(deviceName, false);
if (result.Length > 0)
{
foreach (var item in result)
{
USBmemoryDevice? device = item.Tag as USBmemoryDevice;
if (device != null)
device.Remove();
}
}
}
private void testButton_Click(object sender, EventArgs e)
{
foreach (ListViewItem s in usbListView.SelectedItems)
{
USBmemoryDevice usb = (USBmemoryDevice)s.Tag;
s.Selected = false;
RunJob(usb, true);
}
}
bool CleanOn { get { return cleanChecked.Checked; } }
bool CleanEnabled { get { return CleanOn && activatedCB.Checked; } }
bool FormatOn { get { return formatChecked.Checked; } }
bool FormatEnabled { get { return FormatOn && activatedCB.Checked; } }
private void RunJob(USBmemoryDevice usb, bool force = false)
{
string fs = "FAT32";
if (ntfsSelect.Checked) fs = "NTFS";
if (ExFATselect.Checked) fs = "ExFAT";
if (force)
new Thread(() => { usb.RunJob(CleanOn, FormatOn, merkelappCheckBox.Checked, fs, roundUpSizeCB.Checked); }).Start();
else if(activatedCB.Checked)
new Thread(() => { usb.RunJob(CleanEnabled, FormatEnabled, merkelappCheckBox.Checked, fs, roundUpSizeCB.Checked); }).Start();
}
private void ActHubButClick(object sender, EventArgs e)
{
if (mDeactivate)
foreach (ListViewItem s in usbListView.SelectedItems)
{
s.Selected = false;
USBmemoryDevice usb = (USBmemoryDevice)s.Tag;
var hub = usb.HubFriendlyName;
RemoveHubFromActivated(hub);
}
else
{
foreach (ListViewItem s in usbListView.SelectedItems)
{
s.Selected = false;
USBmemoryDevice usb = (USBmemoryDevice)s.Tag;
var hub = usb.HubFriendlyName;
AddHub2Activated(hub);
string message = String.Format(strings.MsgBoxOnActivateText, hub);
MessageBox.Show(this, message, strings.MsgBoxOnActivateTextCaption);
}
}
UpdateAndRunActivated();
UpdateEmergencyLight();
}
private void RemoveHubFromActivated(string? hub)
{
if (hub == null) return;
var index = activatedHubList.FindIndex(x => x == hub);
if (index != -1)
activatedHubList.RemoveAt(index);
}
private void ListViewSelect_Changed(object sender, EventArgs e)
{
bool enabled = usbListView.SelectedItems.Count > 0;
activateHUBbt.Enabled = enabled;
testButton.Enabled = enabled;
bool deactivated = false;
var funGrp = usbListView.Groups[FoundGroupName];
foreach (ListViewItem s in usbListView.SelectedItems)
{
var group = s.Group;
if (group == funGrp) deactivated = true;
}
if (!deactivated)
{
activateHUBbt.Text = strings.ButtonTextDeactivate;
mDeactivate = true;
}
else
{
activateHUBbt.Text = strings.ButtonTextActivate;
mDeactivate = false;
}
}
private void UpdateAndRunActivated()
{
foreach (ListViewItem s in usbListView.Items)
{
USBmemoryDevice usb = (USBmemoryDevice)s.Tag;
if (activatedHubList.FindIndex(x => usb.HubFriendlyName == x) != -1)
{
usb.ChangeGroup(ActivatedGroupName);
RunJob(usb);
}
else
usb.ChangeGroup(FoundGroupName);
}
}
private void AddHub2Activated(string? hub)
{
if (hub == null) return;
if (activatedHubList.FindIndex(x => x == hub) == -1)
activatedHubList.Add(hub);
}
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
internal class Utils
{
static readonly string[] SizeSuffixes =
{ strings.Bytes, strings.KB, strings.MB, strings.GB, strings.TB, strings.PB, strings.EB, strings.ZB, strings.YB };
static public string SizeSuffix(UInt64 value, int decimalPlaces = 1, bool roundUpToLog2 = false)
{
int divisor = 1000;
if (roundUpToLog2)
{
var log2 = BitOperations.Log2(value);
var temp = (UInt64)1 << (log2 - 1);
log2 = BitOperations.Log2(temp+value);
value = (UInt64)1 << log2;
divisor = 1024;
decimalPlaces = 0;
}
int i = 0;
decimal dValue = (decimal)value;
while (Math.Round(dValue, decimalPlaces) >= divisor)
{
dValue /= divisor;
i++;
}
return string.Format("{0:n" + decimalPlaces + "} {1}", dValue, SizeSuffixes[i]);
}
}
}