-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
659 lines (557 loc) · 23.6 KB
/
MainForm.cs
File metadata and controls
659 lines (557 loc) · 23.6 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Stealth_Compressor.Properties;
namespace Stealth_Compressor
{
public partial class MainForm : Form
{
// Variable declaration
public static string tempPath = Environment.GetEnvironmentVariable("TEMP") + "\\";
private ListViewColumnSorter columnSorter = new ListViewColumnSorter();
private bool isCanceling; // <- for stopping the archival
#region Main Form
// Form Initializer (Runs at startup)
public MainForm()
{
InitializeComponent();
// Link our sorting class to our listview object
lst_Files.ListViewItemSorter = columnSorter;
// Set up the form
// Load the last toolbar settings
ToolStripManager.LoadSettings(this);
tsc_Main.TopToolStripPanel.RowMargin = new Padding(0);
tlb_Main.Renderer = new ToolStripRendererFix();
tlb_Archive.Renderer = new ToolStripRendererFix();
if (isCanceling) isCanceling = false; // <- To fix later
lbl_ImgSize.Text = string.Empty;
lst_Files_Size.Text = string.Format("File Size ({0})", Settings.Default.UNITS);
lbl_TotalSize.Text = string.Format("Total File Size ({0}): 0.00", Settings.Default.UNITS);
sts_Main_Seperator.Text = string.Empty;
lbl_Status.Text = "Add files to begin...";
tlb_Archive_Stop.Enabled = false;
tlb_Archive_Start.Enabled = readyCheck();
btn_Start.Enabled = readyCheck();
GetValuesFromSettings();
}
private void GetValuesFromSettings()
{
chk_Encryption.Checked = Settings.Default.ENCRYPT_ARCHIVE;
chk_OpenComplete.Checked = Settings.Default.OPEN_ON_COMPLETION;
}
// Start button is pressed
private void tlb_Main_Start_Click(object sender, EventArgs e) { Main(); }
private void btn_Start_Click(object sender, EventArgs e) { Main(); }
// Browse save button is pressed
private void btn_Output_Click(object sender, EventArgs e)
{
sav_Output.CheckPathExists = true;
if (!lbl_Image.Visible)
{
sav_Output.DefaultExt = img_Image.ImageLocation.Substring(img_Image.ImageLocation.Length - 3, 3);
}
else { sav_Output.DefaultExt = "*.*"; }
sav_Output.Filter = "Image Files|*.bmp;*.jpg;*.png|" +
"BitMap (*.bmp)|*.bmp|" +
"JPEG (*.jpg)|*.jpg|" +
"PNG (*.png)|*.png|" +
"All Files (*.*)|*.*";
sav_Output.FilterIndex = 1;
sav_Output.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
sav_Output.Title = "Save Compressed Archive As...";
if (sav_Output.ShowDialog() == DialogResult.OK)
{
txt_Output.Text = sav_Output.FileName;
}
tlb_Archive_Start.Enabled = readyCheck();
btn_Start.Enabled = readyCheck();
}
// Exit button is pressed
private void mnu_File_Exit_Click(object sender, EventArgs e) { this.Close(); }
// Archival is canceled
private void tlb_Main_Stop_Click(object sender, EventArgs e) { isCanceling = true; }
private void btn_Stop_Click(object sender, EventArgs e) { isCanceling = true; }
// Load archive configuration settings
private void mnu_Tools_Configuration_Click(object sender, EventArgs e)
{
ArchiveSettingsForm settings = new ArchiveSettingsForm();
settings.StartPosition = FormStartPosition.CenterParent;
settings.ShowDialog(this);
}
#endregion
#region Application Methods
// Main Routine
private void Main()
{
// Disable the form during archival
btn_Start.SendToBack();
enableForm(false);
// Copy the image file to the output directory
File.Copy(img_Image.ImageLocation, txt_Output.Text, true);
// Create the path to the file
string outputPath = txt_Output.Text;
// Try creating an archive
if (CreateStealthArchive(outputPath))
{
// Open file if the option is selected
if (chk_OpenComplete.Checked) System.Diagnostics.Process.Start(outputPath);
lbl_Status.Text = "Merge Complete!";
}
else // on failure, clean-up
{
MessageBox.Show("Failed to create stealth archive!");
File.Delete(outputPath);
}
// Re-enable the form after archival
btn_Stop.SendToBack();
enableForm(true);
}
// Prompts the user for password entry
private string getPassword(bool validate)
{
// Update the status label
lbl_Status.Text = "Setting archive password...";
tsc_Main.Update();
PassForm pass = new PassForm(validate);
pass.StartPosition = FormStartPosition.CenterParent;
pass.ShowDialog(this);
return pass.Password;
}
// Create Zip archive using the DotNetZip library
private bool CreateStealthArchive(string outputFile)
{
using (StealthArchive stealth = new StealthArchive())
{
// Set Output Path
stealth.CloakingFile = txt_Output.Text;
// Get archive settings
stealth.CompressionLevel = ArchiveSettingsForm.GetLevel(Settings.Default.COMPRESSION_LEVEL);
stealth.CompressionMethod = ArchiveSettingsForm.GetMethod(Settings.Default.COMPRESSION_METHOD);
stealth.Strategy = ArchiveSettingsForm.GetStrategy(Settings.Default.COMPRESSION_STRATEGY);
stealth.BufferSize = Settings.Default.IO_BUFFER_SIZE;
stealth.CodecBufferSize = Settings.Default.CODEC_BUFFER_SIZE;
stealth.UseMemory = Settings.Default.MEMORY_ARCHIVE;
stealth.UseZip64WhenSaving = ArchiveSettingsForm.Get64Option(Settings.Default.USE_ZIP64);
// Set an optional password
if (Settings.Default.ENCRYPT_ARCHIVE)
{
// Prompt the user for a password
stealth.Password = getPassword(true);
stealth.Encryption = ArchiveSettingsForm.GetEncryption(Settings.Default.ENCRYPTION_ALGORITHM);
}
try
{
// Iterate through each checked file in the file list
foreach (ListViewItem item in lst_Files.CheckedItems)
{
// Update status label
lbl_Status.Text = string.Format("Adding {0} to archive...", item.Text);
tsc_Main.Update();
// Add entry to archive
stealth.AddFile(item.Name, string.Empty);
}
// Cloak the archive
stealth.Cloak();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return false;
}
}
return true;
}
// Decloaking form initializer
private void tlb_Archive_Decloak_Click(object sender, EventArgs e)
{
DecloakForm deCloak = new DecloakForm();
deCloak.StartPosition = FormStartPosition.CenterParent;
deCloak.ShowDialog(this);
}
#endregion
#region Image File
// Routines for adding the main image file
private void addImage()
{
opn_Image.CheckFileExists = true;
opn_Image.CheckPathExists = true;
opn_Image.Multiselect = false;
opn_Image.Filter = "Image Files|*.bmp;*.jpg;*.png|" +
"BitMap (*.bmp)|*.bmp|" +
"JPEG (*.jpg *.jpeg)|*.jpg;*.jpeg|" +
"PNG (*.png)|*.png|" +
"All Files (*.*)|*.*";
opn_Image.FilterIndex = 1;
opn_Image.InitialDirectory = Environment.SpecialFolder.MyPictures.ToString();
if (opn_Image.ShowDialog() == DialogResult.OK)
{
formatImage(opn_Image.FileName);
}
tlb_Archive_Start.Enabled = readyCheck();
btn_Start.Enabled = readyCheck();
}
private void formatImage(string imgPath)
{
// Hide the image controls
lbl_Image.Visible = false;
img_Image.ImageLocation = imgPath;
// Get the measurements of the selected image
Size origSize = new Size();
using (Image curImage = Image.FromFile(opn_Image.FileName))
{
origSize.Width = curImage.Width;
origSize.Height = curImage.Height;
}
//Update the Image group label
FileInfo imgFile = new FileInfo(imgPath);
grp_Image.Text = String.Format("Image: {0}", imgFile.Name);
// Update the size label
lbl_ImgSize.Text = string.Format("{0} x {1}", origSize.Width, origSize.Height);
lbl_ImgSize.Left = (grp_Image.Width - lbl_ImgSize.Width) / 2;
// Set the size of the image
int maxSize = 156; // (Image container is 156x156)
double aspectRatio;
Size newSize = new Size();
// Build the new size based on the aspect ratio of the image
if (origSize.Width > origSize.Height)
{
aspectRatio = Convert.ToDouble(origSize.Width) / Convert.ToDouble(origSize.Height);
newSize.Width = maxSize;
newSize.Height = Convert.ToInt16(maxSize / aspectRatio);
}
else
{
aspectRatio = Convert.ToDouble(origSize.Height) / Convert.ToDouble(origSize.Width);
newSize.Width = Convert.ToInt16(maxSize / aspectRatio);
newSize.Height = maxSize;
}
// Apply the new size to the image, and maximize it within the container
img_Image.Size = newSize;
img_Image.SizeMode = PictureBoxSizeMode.StretchImage;
// Center the image in the container
img_Image.Left = (grp_Image.Width - img_Image.Width) / 2;
img_Image.Top = (grp_Image.Height - img_Image.Height) / 2;
}
private void tlb_Main_AddImage_Click(object sender, EventArgs e) { addImage(); }
private void ctx_Image_AddImage_Click(object sender, EventArgs e){ addImage(); }
#endregion
#region File List
// Routines for adding files to the list
private void AddFiles()
{
opn_Files.CheckFileExists = true;
opn_Files.CheckPathExists = true;
opn_Files.Multiselect = true;
opn_Files.Filter = "All Files (*.*)|*.*";
opn_Files.FilterIndex = 1;
opn_Files.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
if (opn_Files.ShowDialog() == DialogResult.OK)
{
foreach (string file in opn_Files.FileNames)
{
FileInfo curFile = new FileInfo(file);
ListViewItem curItem = new ListViewItem(curFile.Name);
curItem.Name = curFile.FullName;
if (!lst_Files.Items.ContainsKey(curItem.Name))
{
curItem.SubItems.Add(curFile.DirectoryName);
curItem.SubItems.Add(formatFileSize(curFile.Length, Settings.Default.UNITS));
curItem.Checked = true;
lst_Files.Items.Add(curItem);
}
}
}
updateTotal();
tlb_Archive_Start.Enabled = readyCheck();
btn_Start.Enabled = readyCheck();
}
private void tlb_Main_AddFiles_Click(object sender, EventArgs e) { AddFiles(); }
private void ctx_Files_AddFiles_Click(object sender, EventArgs e){ AddFiles(); }
// Routines for removing selected files from the list
private void RemoveFiles()
{
foreach (ListViewItem item in lst_Files.SelectedItems)
{
item.Remove();
}
updateTotal();
tlb_Archive_Start.Enabled = readyCheck();
btn_Start.Enabled = readyCheck();
}
private void tlb_Main_Remove_Click(object sender, EventArgs e) { RemoveFiles(); }
private void ctx_Files_Remove_Click(object sender, EventArgs e){ RemoveFiles(); }
// Routines for removing all files from the list
private void tlb_Main_RemoveAll_Click(object sender, EventArgs e)
{
lst_Files.Items.Clear();
updateTotal();
tlb_Archive_Start.Enabled = readyCheck();
btn_Start.Enabled = readyCheck();
}
private void ctx_Files_RemoveAll_Click(object sender, EventArgs e)
{
lst_Files.Items.Clear();
updateTotal();
}
//Routines for checking/unchecking items in the file list
private void checkItem(bool isChecked)
{
foreach (ListViewItem item in lst_Files.SelectedItems)
{
if (isChecked) { item.Checked = true; }
else { item.Checked = false; }
}
}
private void tlb_Main_Check_Click(object sender, EventArgs e) { checkItem(true); }
private void ctx_Files_Check_Click(object sender, EventArgs e) { checkItem(true); }
private void tlb_Main_Uncheck_Click(object sender, EventArgs e) { checkItem(false); }
private void ctx_Files_Uncheck_Click(object sender, EventArgs e){ checkItem(false); }
private void lst_Files_ItemChecked(object sender, ItemCheckedEventArgs e)
{
if (e.Item.Checked) { e.Item.ForeColor = Color.Black; }
else { e.Item.ForeColor = Color.Gray; }
updateTotal();
}
// Routines for inverting the current file list selection
private void invertSelection()
{
foreach (ListViewItem item in lst_Files.Items)
{
if (item.Selected) { item.Selected = false; }
else { item.Selected = true; }
}
}
private void tlb_Main_Invert_Click(object sender, EventArgs e) { invertSelection(); }
private void ctx_Files_Invert_Click(object sender, EventArgs e){ invertSelection(); }
// Handles the column sorting when clicked
private void lst_Files_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (e.Column == columnSorter.SortColumn)
{
// Reverse the current sort direction for this column.
if (columnSorter.Order == SortOrder.Ascending)
{
columnSorter.Order = SortOrder.Descending;
}
else
{
columnSorter.Order = SortOrder.Ascending;
}
}
else
{
// Set the column number that is to be sorted; default to ascending.
columnSorter.SortColumn = e.Column;
columnSorter.Order = SortOrder.Ascending;
}
// Perform the sort with these new sort options.
this.lst_Files.Sort();
}
// Routines that keep the filesize column anchored to the size of the form
private void MainForm_SizeChanged(object sender, System.EventArgs e)
{
lst_Files.Columns[2].Width = -2;
}
private void lst_Files_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
{
if (e.ColumnIndex != 2)
{
lst_Files.Columns[2].Width = -2;
}
}
#endregion
#region Tools
// Creates the desktop shortcut
private void mnu_Tools_Shortcut_Click(object sender, EventArgs e)
{
// The shortcut will only be generated properly when run from the exe, not a VS debugging session
string exePath = AppDomain.CurrentDomain.BaseDirectory + AppDomain.CurrentDomain.FriendlyName;
string lnkPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Stealth Compressor.lnk";
ShellLink shortcut = new ShellLink();
shortcut.Description = "Stealth Compressor - Archive Cloaking";
shortcut.DisplayMode = ShellLink.LinkDisplayMode.edmNormal;
shortcut.Target = exePath;
shortcut.IconPath = exePath;
shortcut.IconIndex = 0;
if (File.Exists(lnkPath)) File.Delete(lnkPath);
shortcut.Save(lnkPath);
}
#endregion
#region Application Utilities
// Formats the file size to the desired setting
public static string formatFileSize(double size, string format)
{
double divisor;
switch (format)
{
case "KB":
divisor = Math.Pow(2, 10);
break;
case "MB":
divisor = Math.Pow(2, 20);
break;
case "GB":
divisor = Math.Pow(2, 30);
break;
default:
divisor = Math.Pow(2, 10);
break;
}
return Math.Round(size / divisor, 2).ToString("n2"); // <-- Composite formatting
}
// Updates the total size label
private void updateTotal()
{
double total = 0;
// Iterate through each file in the list
foreach (ListViewItem item in lst_Files.Items)
{
if (item.Checked)
{
// Add the file size to the total if it's checked
total += Convert.ToDouble(item.SubItems[2].Text);
}
}
// Format the total file size label
lbl_TotalSize.Text = string.Format("Total Size ({0}): {1}", Settings.Default.UNITS, total.ToString("n2"));
//// Estimated archive size formatting...
//string archiveTotal = "Est. Archive Size ({0}): {1}";
//lbl_ArchiveSize.Text = string.Format(archiveTotal, units, "Calculating...");
//if (!lbl_TotalSize.Text.Contains("0.00"))
//{
// //long imageSize = 0;
// // We can use this to get the size of everything, including the image used
// //if (img_Image.Image != null)
// //{
// // FileInfo image = new FileInfo(img_Image.ImageLocation);
// //}
// //Thread archiveThread = new Thread(estimateArchiveThread);
// //archiveThread.IsBackground = true;
// //archiveThread.Start();
//}
//else lbl_ArchiveSize.Text = string.Format(archiveTotal, units, "0.00");
}
// Checks that everything is ready for archival
private bool readyCheck()
{
// Check if image is loaded
if (img_Image.ImageLocation == null)
{
return false;
}
// Check if files are loaded
if (lst_Files.CheckedItems.Count == 0)
{
return false;
}
// Check if a save path exists
if (txt_Output.Text == string.Empty)
{
return false;
}
return true;
}
// Disables/Enables form controls during archival
private void enableForm(bool choice)
{
// Form buttons
btn_Start.Enabled = choice;
btn_Stop.Enabled = !choice;
// Form groups
grp_Output.Enabled = choice;
grp_Image.Enabled = choice;
grp_Files.Enabled = choice;
grp_QuickSettings.Enabled = choice;
// Main menu
mnu_File_New.Enabled = choice;
mnu_File_Open.Enabled = choice;
mnu_File_Save.Enabled = choice;
mnu_Tools.Enabled = choice;
mnu_Help.Enabled = choice;
// Main toolbar
tlb_Main_New.Enabled = choice;
tlb_Main_Open.Enabled = choice;
tlb_Main_Save.Enabled = choice;
tlb_Main_AddImage.Enabled = choice;
tlb_Main_AddFiles.Enabled = choice;
tlb_Main_Remove.Enabled = choice;
tlb_Main_RemoveAll.Enabled = choice;
tlb_Main_Check.Enabled = choice;
tlb_Main_Uncheck.Enabled = choice;
tlb_Main_Invert.Enabled = choice;
tlb_Archive_Start.Enabled = choice;
tlb_Archive_Stop.Enabled = !choice;
this.Update();
}
#endregion
//-----------\\
#region To Do Items
private void mnu_File_Open_Click(object sender, EventArgs e)
{
MessageBox.Show("Need to add open session method");
}
private void mnu_File_Save_Click(object sender, EventArgs e)
{
MessageBox.Show("Need to add save session method");
}
private void mnu_File_New_Click(object sender, EventArgs e)
{
MessageBox.Show("Need to add new session method");
}
private void tlb_Main_Open_Click(object sender, EventArgs e)
{
}
private void tlb_Main_Save_Click(object sender, EventArgs e)
{
}
private void tlb_Main_New_Click(object sender, EventArgs e)
{
}
private void mnu_Tools_Settings_Click(object sender, EventArgs e)
{
MessageBox.Show("Need to add settings form");
}
private void mnu_Help_ViewHelp_Click(object sender, EventArgs e)
{
MessageBox.Show("Need to create help chm file");
}
private void mnu_Help_About_Click(object sender, EventArgs e)
{
MessageBox.Show("Need to add about splashscreen");
}
#endregion
//-----------\\
// Toolbar controls
private void ctx_Toolbars_Main_Click(object sender, EventArgs e)
{
ctx_Toolbars_Main.Checked = !ctx_Toolbars_Main.Checked;
}
private void ctx_Toolbars_Main_CheckedChanged(object sender, System.EventArgs e)
{
tlb_Main.Visible = ctx_Toolbars_Main.Checked;
}
private void ctx_Toolbars_Archive_Click(object sender, EventArgs e)
{
ctx_Toolbars_Archive.Checked = !ctx_Toolbars_Archive.Checked;
}
private void ctx_Toolbars_Archive_CheckedChanged(object sender, System.EventArgs e)
{
tlb_Archive.Visible = ctx_Toolbars_Archive.Checked;
}
// On exit
private void MainForm_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
ToolStripManager.SaveSettings(this);
}
// Quick settings
private void chk_Encryption_CheckedChanged(object sender, EventArgs e)
{
Settings.Default.ENCRYPT_ARCHIVE = chk_Encryption.Checked;
}
private void chk_OpenComplete_CheckedChanged(object sender, EventArgs e)
{
Settings.Default.OPEN_ON_COMPLETION = chk_OpenComplete.Checked;
}
}
}