forked from gavinkendall/autoscreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormMain.cs
2844 lines (2370 loc) · 116 KB
/
FormMain.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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-----------------------------------------------------------------------
// <copyright file="FormMain.cs" company="Gavin Kendall">
// Copyright (c) Gavin Kendall. All rights reserved.
// </copyright>
// <author>Gavin Kendall</author>
// <summary></summary>
//-----------------------------------------------------------------------
namespace AutoScreenCapture
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections.Generic;
using AutoScreenCapture.Properties;
/// <summary>
/// The application's main window.
/// </summary>
public partial class FormMain : Form
{
private FormEditor formEditor = new FormEditor();
private FormTrigger formTrigger = new FormTrigger();
private FormRegion formRegion = new FormRegion();
private FormScreen formScreen = new FormScreen();
private FormEnterPassphrase formEnterPassphrase = new FormEnterPassphrase();
private ScreenCapture _screenCapture;
private ImageFormatCollection _imageFormatCollection;
private MacroTagCollection _macroTagCollection;
private ScreenshotCollection _screenshotCollection;
/// <summary>
/// Threads for background operations.
/// </summary>
private BackgroundWorker runScreenshotSearchThread = null;
private BackgroundWorker runDateSearchThread = null;
private BackgroundWorker runDeleteSlidesThread = null;
private BackgroundWorker runFilterSearchThread = null;
private BackgroundWorker runSaveScreenshotsThread = null;
/// <summary>
/// Delegates for the threads.
/// </summary>
private delegate void RunSlideSearchDelegate(DoWorkEventArgs e);
private delegate void RunDateSearchDelegate(DoWorkEventArgs e);
private delegate void RunTitleSearchDelegate(DoWorkEventArgs e);
/// <summary>
/// Default settings used by the command line parser.
/// </summary>
private const int CAPTURE_LIMIT_MIN = 0;
private const int CAPTURE_LIMIT_MAX = 9999;
private const int CAPTURE_INTERVAL_DEFAULT_IN_MINUTES = 1;
/// <summary>
/// The various regular expressions used in the parsing of the command line arguments.
/// </summary>
private const string REGEX_COMMAND_LINE_INITIAL = "^-initial$";
private const string REGEX_COMMAND_LINE_LIMIT = @"^-limit=(?<Limit>\d{1,7})$";
private const string REGEX_COMMAND_LINE_STOPAT = @"^-stopat=(?<Hours>\d{2}):(?<Minutes>\d{2}):(?<Seconds>\d{2})$";
private const string REGEX_COMMAND_LINE_STARTAT = @"^-startat=(?<Hours>\d{2}):(?<Minutes>\d{2}):(?<Seconds>\d{2})$";
private const string REGEX_COMMAND_LINE_INTERVAL = @"^-interval=(?<Hours>\d{2}):(?<Minutes>\d{2}):(?<Seconds>\d{2})\.(?<Milliseconds>\d{3})$";
private const string REGEX_COMMAND_LINE_PASSPHRASE = "^-passphrase=(?<Passphrase>.+)$";
private const string REGEX_COMMAND_LINE_HIDE_SYSTEM_TRAY_ICON = "^-hideSystemTrayIcon$";
/// <summary>
/// Constructor for the main form. Arguments from the command line can be passed to it.
/// </summary>
/// <param name="args">Arguments from the command line</param>
public FormMain(string[] args)
{
InitializeComponent();
if (!Directory.Exists(FileSystem.ApplicationFolder))
{
Directory.CreateDirectory(FileSystem.ApplicationFolder);
}
Settings.Initialize();
Log.Enabled = Convert.ToBoolean(Settings.Application.GetByKey("DebugMode", defaultValue: false).Value);
Log.Write("*** Welcome to " + Settings.ApplicationName + " " + Settings.ApplicationVersion + " ***");
Log.Write("Starting application");
LoadSettings();
Text = (string) Settings.Application.GetByKey("Name", defaultValue: Settings.ApplicationName).Value;
InitializeThreads();
// Get rid of the old "slides" directory that may still remain from an old version of the application.
DeleteSlides();
if (args.Length > 0)
{
ParseCommandLineArguments(args);
}
}
/// <summary>
/// When this form loads we'll need to delete slides and then search for dates and slides.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormMain_Load(object sender, EventArgs e)
{
SearchFilterValues();
SearchDates();
SearchScreenshots();
Log.Write("Running triggers of condition type ApplicationStartup");
RunTriggersOfConditionType(TriggerConditionType.ApplicationStartup);
}
private void InitializeThreads()
{
Log.Write("Initializing threads");
runDeleteSlidesThread = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
runDeleteSlidesThread.DoWork += new DoWorkEventHandler(DoWork_runDeleteSlidesThread);
runDateSearchThread = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
runDateSearchThread.DoWork += new DoWorkEventHandler(DoWork_runDateSearchThread);
runScreenshotSearchThread = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
runScreenshotSearchThread.DoWork += new DoWorkEventHandler(DoWork_runScreenshotSearchThread);
runFilterSearchThread = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
runFilterSearchThread.DoWork += new DoWorkEventHandler(DoWork_runFilterSearchThread);
runSaveScreenshotsThread = new BackgroundWorker
{
WorkerReportsProgress = false,
WorkerSupportsCancellation = true
};
runSaveScreenshotsThread.DoWork += new DoWorkEventHandler(DoWork_runSaveScreenshotsThread);
}
/// <summary>
/// Loads the user's saved settings.
/// </summary>
private void LoadSettings()
{
try
{
Log.Write("Loading user settings");
Settings.User.Load();
Log.Write("User settings loaded");
Log.Write("Attempting upgrade of user settings from old version of application (if needed)");
Settings.User.Upgrade();
Log.Write("Initializing screen capture");
_screenCapture = new ScreenCapture();
Log.Write("Initializing image format collection");
_imageFormatCollection = new ImageFormatCollection();
Log.Write("Initializing macro tag collection");
_macroTagCollection = new MacroTagCollection();
Log.Write("Initializing editor collection");
formEditor.EditorCollection.Load();
Log.Write("Number of editors loaded = " + formEditor.EditorCollection.Count);
Log.Write("Initializing trigger collection");
formTrigger.TriggerCollection.Load();
Log.Write("Number of triggers loaded = " + formTrigger.TriggerCollection.Count);
Log.Write("Initializing region collection");
formRegion.RegionCollection.Load(_imageFormatCollection);
Log.Write("Number of regions loaded = " + formRegion.RegionCollection.Count);
Log.Write("Initializing screen collection");
formScreen.ScreenCollection.Load(_imageFormatCollection);
Log.Write("Number of screens loaded = " + formScreen.ScreenCollection.Count);
Log.Write("Building screens module");
BuildScreensModule();
Log.Write("Building editors module");
BuildEditorsModule();
Log.Write("Building triggers module");
BuildTriggersModule();
Log.Write("Building regions module");
BuildRegionsModule();
Log.Write("Building screenshot preview context menu");
BuildScreenshotPreviewContextualMenu();
Log.Write("Building view tab pages");
BuildViewTabPages();
Log.Write("Initializing screenshot collection");
_screenshotCollection = new ScreenshotCollection();
Log.Write("Loading screenshots into the screenshot collection to generate a history of what was captured");
_screenshotCollection.Load(_imageFormatCollection, formScreen.ScreenCollection, formRegion.RegionCollection);
int screenCaptureInterval = Convert.ToInt32(Settings.User.GetByKey("IntScreenCaptureInterval", defaultValue: 60000).Value);
Log.Write("IntScreenCaptureInterval = " + screenCaptureInterval);
if (screenCaptureInterval == 0)
{
screenCaptureInterval = 60000;
Log.Write("WARNING: Screen capture interval was found to be 0 so 60,000 milliseconds (or 1 minute) is being used as the default value");
}
Log.Write("Assigning screen capture interval value to its appropriate hour, minute, second, and millisecond variables");
decimal screenCaptureIntervalHours = Convert.ToDecimal(TimeSpan.FromMilliseconds(Convert.ToDouble(screenCaptureInterval)).Hours);
Log.Write("Hours = " + screenCaptureIntervalHours);
decimal screenCaptureIntervalMinutes = Convert.ToDecimal(TimeSpan.FromMilliseconds(Convert.ToDouble(screenCaptureInterval)).Minutes);
Log.Write("Minutes = " + screenCaptureIntervalMinutes);
decimal screenCaptureIntervalSeconds = Convert.ToDecimal(TimeSpan.FromMilliseconds(Convert.ToDouble(screenCaptureInterval)).Seconds);
Log.Write("Seconds = " + screenCaptureIntervalSeconds);
decimal screenCaptureIntervalMilliseconds = Convert.ToDecimal(TimeSpan.FromMilliseconds(Convert.ToDouble(screenCaptureInterval)).Milliseconds);
Log.Write("Milliseconds = " + screenCaptureIntervalMilliseconds);
numericUpDownHoursInterval.Value = screenCaptureIntervalHours;
numericUpDownMinutesInterval.Value = screenCaptureIntervalMinutes;
numericUpDownSecondsInterval.Value = screenCaptureIntervalSeconds;
numericUpDownMillisecondsInterval.Value = screenCaptureIntervalMilliseconds;
numericUpDownCaptureLimit.Value = Convert.ToInt32(Settings.User.GetByKey("IntCaptureLimit", defaultValue: 0).Value);
Log.Write("IntCaptureLimit = " + numericUpDownCaptureLimit.Value);
checkBoxCaptureLimit.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureLimit", defaultValue: false).Value);
Log.Write("BoolCaptureLimit = " + checkBoxCaptureLimit.Checked);
checkBoxInitialScreenshot.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolTakeInitialScreenshot", defaultValue: false).Value);
Log.Write("BoolTakeInitialScreenshot = " + checkBoxInitialScreenshot.Checked);
toolStripMenuItemShowSystemTrayIcon.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolShowSystemTrayIcon", defaultValue: true).Value);
Log.Write("BoolShowSystemTrayIcon = " + toolStripMenuItemShowSystemTrayIcon.Checked);
checkBoxScheduleStopAt.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureStopAt", defaultValue: false).Value);
Log.Write("BoolCaptureStopAt = " + checkBoxScheduleStopAt.Checked);
checkBoxScheduleStartAt.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureStartAt", defaultValue: false).Value);
Log.Write("BoolCaptureStartAt = " + checkBoxScheduleStartAt.Checked);
checkBoxSunday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnSunday", defaultValue: false).Value);
Log.Write("BoolCaptureOnSunday = " + checkBoxSunday.Checked);
checkBoxMonday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnMonday", defaultValue: false).Value);
Log.Write("BoolCaptureOnMonday = " + checkBoxMonday.Checked);
checkBoxTuesday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnTuesday", defaultValue: false).Value);
Log.Write("BoolCaptureOnTuesday = " + checkBoxTuesday.Checked);
checkBoxWednesday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnWednesday", defaultValue: false).Value);
Log.Write("BoolCaptureOnWednesday = " + checkBoxWednesday.Checked);
checkBoxThursday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnThursday", defaultValue: false).Value);
Log.Write("BoolCaptureOnThursday = " + checkBoxThursday.Checked);
checkBoxFriday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnFriday", defaultValue: false).Value);
Log.Write("BoolCaptureOnFriday = " + checkBoxFriday.Checked);
checkBoxSaturday.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnSaturday", defaultValue: false).Value);
Log.Write("BoolCaptureOnSaturday = " + checkBoxSaturday.Checked);
checkBoxScheduleOnTheseDays.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolCaptureOnTheseDays", defaultValue: false).Value);
Log.Write("BoolCaptureOnTheseDays = " + checkBoxScheduleOnTheseDays.Checked);
dateTimePickerScheduleStartAt.Value = DateTime.Parse(Settings.User.GetByKey("DateTimeCaptureStartAt", defaultValue: new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0)).Value.ToString());
Log.Write("DateTimeCaptureStartAt = " + dateTimePickerScheduleStartAt.Value);
dateTimePickerScheduleStopAt.Value = DateTime.Parse(Settings.User.GetByKey("DateTimeCaptureStopAt", defaultValue: new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 0, 0)).Value.ToString());
Log.Write("DateTimeCaptureStopAt = " + dateTimePickerScheduleStopAt.Value);
numericUpDownKeepScreenshotsForDays.Value = Convert.ToDecimal(Settings.User.GetByKey("IntKeepScreenshotsForDays", defaultValue: 30).Value);
Log.Write("IntKeepScreenshotsForDays = " + numericUpDownKeepScreenshotsForDays.Value);
comboBoxScreenshotLabel.Text = Settings.User.GetByKey("StringScreenshotLabel", defaultValue: string.Empty).Value.ToString();
Log.Write("StringScreenshotLabel = " + comboBoxScreenshotLabel.Text);
checkBoxScreenshotLabel.Checked = Convert.ToBoolean(Settings.User.GetByKey("BoolApplyScreenshotLabel", defaultValue: false).Value);
EnableStartCapture();
CaptureLimitCheck();
}
catch (Exception ex)
{
Log.Write("FormMain::LoadSettings", ex);
}
}
/// <summary>
/// When this form is closing we can either exit the application or just close this window.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FormViewer_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{
DisableStopCapture();
EnableStartCapture();
_screenCapture.Count = 0;
_screenCapture.Running = false;
// Hide the system tray icon.
notifyIcon.Visible = false;
Log.Write("Hiding interface on forced application exit because Windows is shutting down");
HideInterface();
Log.Write("Saving screenshots on forced application exit because Windows is shutting down");
_screenshotCollection.Save((int)numericUpDownKeepScreenshotsForDays.Value);
if (runDateSearchThread != null && runDateSearchThread.IsBusy)
{
runDateSearchThread.CancelAsync();
}
if (runScreenshotSearchThread != null && runScreenshotSearchThread.IsBusy)
{
runScreenshotSearchThread.CancelAsync();
}
// Exit.
Environment.Exit(0);
}
else
{
Log.Write("Running triggers of condition type InterfaceClosing");
RunTriggersOfConditionType(TriggerConditionType.InterfaceClosing);
// If there isn't a Trigger for "InterfaceClosing" that performs an action
// then make sure we cancel this event so that nothing happens. We want the user
// to use a Trigger, and decide what they want to do, when closing the interface window.
e.Cancel = true;
}
}
/// <summary>
/// Searches for dates. They should be in the format yyyy-mm-dd.
/// </summary>
private void SearchDates()
{
Log.Write("Searching for dates");
if (runDateSearchThread != null && !runDateSearchThread.IsBusy)
{
runDateSearchThread.RunWorkerAsync();
}
}
private void DeleteSlides()
{
Log.Write("Deleting slides directory from old version of application (if needed)");
if (runDeleteSlidesThread != null && !runDeleteSlidesThread.IsBusy)
{
runDeleteSlidesThread.RunWorkerAsync();
}
}
private void SaveScreenshots()
{
if (runSaveScreenshotsThread != null && !runSaveScreenshotsThread.IsBusy)
{
runSaveScreenshotsThread.RunWorkerAsync();
}
}
/// <summary>
/// Searches for screenshots.
/// </summary>
private void SearchScreenshots()
{
Log.Write("Searching for screenshots");
Slideshow.Index = 0;
Slideshow.Count = 0;
listBoxScreenshots.BeginUpdate();
listBoxScreenshots.DataSource = null;
if (runScreenshotSearchThread != null && !runScreenshotSearchThread.IsBusy)
{
runScreenshotSearchThread.RunWorkerAsync();
}
listBoxScreenshots.EndUpdate();
}
private void SearchFilterValues()
{
comboBoxFilterValue.BeginUpdate();
if (runFilterSearchThread != null && !runFilterSearchThread.IsBusy)
{
runFilterSearchThread.RunWorkerAsync();
}
comboBoxFilterValue.EndUpdate();
}
/// <summary>
/// This thread is responsible for finding slides.
/// </summary>
/// <param name="e"></param>
private void RunScreenshotSearch(DoWorkEventArgs e)
{
if (listBoxScreenshots.InvokeRequired)
{
listBoxScreenshots.Invoke(new RunSlideSearchDelegate(RunScreenshotSearch), new object[] {e});
}
else
{
listBoxScreenshots.DisplayMember = "Value";
listBoxScreenshots.ValueMember = "Name";
listBoxScreenshots.DataSource = _screenshotCollection.GetSlides(comboBoxFilterType.Text, comboBoxFilterValue.Text, monthCalendar.SelectionStart.ToString(MacroParser.DateFormat));
if (listBoxScreenshots.Items.Count > 0)
{
listBoxScreenshots.SelectedIndex = listBoxScreenshots.Items.Count - 1;
}
}
}
/// <summary>
/// This thread is responsible for figuring out what days screenshots were taken.
/// </summary>
/// <param name="e"></param>
private void RunDateSearch(DoWorkEventArgs e)
{
if (monthCalendar.InvokeRequired)
{
monthCalendar.Invoke(new RunDateSearchDelegate(RunDateSearch), new object[] {e});
}
else
{
List<string> dates = _screenshotCollection.GetDates(comboBoxFilterType.Text, comboBoxFilterValue.Text);
DateTime[] boldedDates = new DateTime[dates.Count];
for (int i = 0; i < dates.Count; i++)
{
boldedDates.SetValue(ConvertDateStringToDateTime(dates[i].ToString()), i);
}
monthCalendar.BoldedDates = boldedDates;
}
}
/// <summary>
/// This thread is responsible for deleting all the slides remaining from an old version of the application
/// since we no longer use slides or support the Slideshow module going forward.
/// </summary>
/// <param name="e"></param>
private void RunDeleteSlides(DoWorkEventArgs e)
{
FileSystem.DeleteFilesInDirectory(FileSystem.SlidesFolder);
}
private void RunSaveScreenshots(DoWorkEventArgs e)
{
_screenshotCollection.Save((int)numericUpDownKeepScreenshotsForDays.Value);
}
private void RunFilterSearch(DoWorkEventArgs e)
{
if (comboBoxFilterValue.InvokeRequired)
{
comboBoxFilterValue.Invoke(new RunTitleSearchDelegate(RunFilterSearch), new object[] {e});
}
else
{
if (comboBoxFilterType.SelectedItem != null && !string.IsNullOrEmpty(comboBoxFilterType.Text))
{
List<string> filterValueList = _screenshotCollection.GetFilterValueList(comboBoxFilterType.Text);
filterValueList.Add(string.Empty);
filterValueList.Sort();
comboBoxFilterValue.DataSource = filterValueList;
}
}
}
/// <summary>
/// Saves the user's settings.
/// </summary>
/// <param name="e"></param>
private void SaveSettings()
{
try
{
Log.Write("Saving settings");
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Settings.User.GetByKey("IntScreenCaptureInterval", defaultValue: 60000).Value = GetScreenCaptureInterval();
Settings.User.GetByKey("IntCaptureLimit", defaultValue: 0).Value = numericUpDownCaptureLimit.Value;
Settings.User.GetByKey("BoolCaptureLimit", defaultValue: false).Value = checkBoxCaptureLimit.Checked;
Settings.User.GetByKey("BoolTakeInitialScreenshot", defaultValue: false).Value = checkBoxInitialScreenshot.Checked;
Settings.User.GetByKey("BoolShowSystemTrayIcon", defaultValue: true).Value = toolStripMenuItemShowSystemTrayIcon.Checked;
Settings.User.GetByKey("BoolCaptureStopAt", defaultValue: false).Value = checkBoxScheduleStopAt.Checked;
Settings.User.GetByKey("BoolCaptureStartAt", defaultValue: false).Value = checkBoxScheduleStartAt.Checked;
Settings.User.GetByKey("BoolCaptureOnSunday", defaultValue: false).Value = checkBoxSunday.Checked;
Settings.User.GetByKey("BoolCaptureOnMonday", defaultValue: false).Value = checkBoxMonday.Checked;
Settings.User.GetByKey("BoolCaptureOnTuesday", defaultValue: false).Value = checkBoxTuesday.Checked;
Settings.User.GetByKey("BoolCaptureOnWednesday", defaultValue: false).Value = checkBoxWednesday.Checked;
Settings.User.GetByKey("BoolCaptureOnThursday", defaultValue: false).Value = checkBoxThursday.Checked;
Settings.User.GetByKey("BoolCaptureOnFriday", defaultValue: false).Value = checkBoxFriday.Checked;
Settings.User.GetByKey("BoolCaptureOnSaturday", defaultValue: false).Value = checkBoxSaturday.Checked;
Settings.User.GetByKey("BoolCaptureOnTheseDays", defaultValue: false).Value = checkBoxScheduleOnTheseDays.Checked;
Settings.User.GetByKey("DateTimeCaptureStopAt", defaultValue: new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 0, 0)).Value = dateTimePickerScheduleStopAt.Value;
Settings.User.GetByKey("DateTimeCaptureStartAt", defaultValue: new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 0, 0)).Value = dateTimePickerScheduleStartAt.Value;
Settings.User.GetByKey("IntKeepScreenshotsForDays", defaultValue: 30).Value = numericUpDownKeepScreenshotsForDays.Value;
Settings.User.GetByKey("StringScreenshotLabel", defaultValue: string.Empty).Value = comboBoxScreenshotLabel.Text;
Settings.User.GetByKey("BoolApplyScreenshotLabel", defaultValue: false).Value = checkBoxScreenshotLabel.Checked;
Settings.User.Save();
Log.Write("Settings saved");
stopwatch.Stop();
Log.Write("It took " + stopwatch.ElapsedMilliseconds + " milliseconds to save user settings");
}
catch (Exception ex)
{
Log.Write("FormMain::SaveSettings", ex);
}
}
/// <summary>
/// Converts the string representation of a date into a DateTime object. Used by the RunDateSearch thread so we can set bolded dates in the calendar.
/// </summary>
/// <param name="date">A string representation of a date (such as "2019-02-06").</param>
/// <returns>A DateTime object based on the provided date string.</returns>
private DateTime ConvertDateStringToDateTime(string date)
{
return new DateTime(Convert.ToInt32(date.Substring(0, 4)), Convert.ToInt32(date.Substring(5, 2)), Convert.ToInt32(date.Substring(8, 2)));
}
/// <summary>
/// Shows the interface.
/// </summary>
private void ShowInterface()
{
Log.Write("Showing interface");
if (ScreenCapture.LockScreenCaptureSession && !formEnterPassphrase.Visible)
{
Log.Write("Screen capture session is locked. Challenging user to enter correct passphrase to unlock");
formEnterPassphrase.ShowDialog(this);
}
// This is intentional. Do not rewrite these statements as an if/else
// because as soon as lockScreenCaptureSession is set to false we want
// to continue with normal functionality.
if (!ScreenCapture.LockScreenCaptureSession)
{
Settings.User.GetByKey("StringPassphrase", defaultValue: false).Value = string.Empty;
SaveSettings();
Opacity = 100;
toolStripMenuItemShowInterface.Enabled = false;
toolStripMenuItemHideInterface.Enabled = true;
SearchDates();
SearchScreenshots();
List<string> labels = _screenshotCollection.GetLabels();
labels.Sort();
comboBoxScreenshotLabel.DataSource = labels;
comboBoxScreenshotLabel.Text = Settings.User.GetByKey("StringScreenshotLabel", defaultValue: string.Empty).Value.ToString();
Show();
Visible = true;
ShowInTaskbar = true;
// If the window is mimimized then show it when the user wants to open the window.
if (WindowState == FormWindowState.Minimized)
{
WindowState = FormWindowState.Normal;
}
Focus();
Log.Write("Running triggers of condition type InterfaceShowing");
RunTriggersOfConditionType(TriggerConditionType.InterfaceShowing);
}
}
/// <summary>
/// Hides the interface.
/// </summary>
private void HideInterface()
{
Log.Write("Hiding interface");
Opacity = 0;
toolStripMenuItemShowInterface.Enabled = true;
toolStripMenuItemHideInterface.Enabled = false;
Hide();
Visible = false;
ShowInTaskbar = false;
Log.Write("Running triggers of condition type InterfaceHiding");
RunTriggersOfConditionType(TriggerConditionType.InterfaceHiding);
}
/// <summary>
/// Stops the screen capture session that's currently running.
/// </summary>
private void StopScreenCapture()
{
if (_screenCapture.Running)
{
Log.Write("Stopping screen capture");
if (ScreenCapture.LockScreenCaptureSession && !formEnterPassphrase.Visible)
{
Log.Write("Screen capture session is locked. Challenging user to enter correct passphrase to unlock");
formEnterPassphrase.ShowDialog(this);
}
// This is intentional. Do not rewrite these statements as an if/else
// because as soon as lockScreenCaptureSession is set to false we want
// to continue with normal functionality.
if (!ScreenCapture.LockScreenCaptureSession)
{
Settings.User.GetByKey("StringPassphrase", defaultValue: false).Value = string.Empty;
SaveSettings();
DisableStopCapture();
EnableStartCapture();
_screenCapture.Count = 0;
_screenCapture.Running = false;
SearchFilterValues();
SearchDates();
Log.Write("Running triggers of condition type ScreenCaptureStopped");
RunTriggersOfConditionType(TriggerConditionType.ScreenCaptureStopped);
}
}
}
/// <summary>
/// Starts a screen capture session.
/// </summary>
private void StartScreenCapture()
{
int screenCaptureInterval = GetScreenCaptureInterval();
if (!_screenCapture.Running && screenCaptureInterval > 0)
{
SaveSettings();
// Stop the date search thread if it's busy.
if (runDateSearchThread != null && runDateSearchThread.IsBusy)
{
runDateSearchThread.CancelAsync();
}
// Stop the slide search thread if it's busy.
if (runScreenshotSearchThread != null && runScreenshotSearchThread.IsBusy)
{
runScreenshotSearchThread.CancelAsync();
}
DisableStartCapture();
EnableStopScreenCapture();
// Setup the properties for the screen capture class.
_screenCapture.Delay = screenCaptureInterval;
_screenCapture.Limit = checkBoxCaptureLimit.Checked ? (int) numericUpDownCaptureLimit.Value : 0;
if (Settings.User.GetByKey("StringPassphrase", defaultValue: string.Empty).Value.ToString().Length > 0)
{
ScreenCapture.LockScreenCaptureSession = true;
}
else
{
ScreenCapture.LockScreenCaptureSession = false;
}
Log.Write("Starting screen capture");
_screenCapture.Running = true;
_screenCapture.DateTimeStartCapture = DateTime.Now;
if (checkBoxInitialScreenshot.Checked)
{
Log.Write("Taking initial screenshots");
TakeScreenshot();
}
// Start taking screenshots.
timerScreenCapture.Interval = screenCaptureInterval;
Log.Write("Running triggers of condition type ScreenCaptureStarted");
RunTriggersOfConditionType(TriggerConditionType.ScreenCaptureStarted);
}
}
/// <summary>
/// Whenever the user clicks on a screenshot in the list of screenshots then make sure to update the appropriate image.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SelectedIndexChanged_listBoxScreenshots(object sender, EventArgs e)
{
Slideshow.Index = listBoxScreenshots.SelectedIndex;
Slideshow.Count = listBoxScreenshots.Items.Count;
ShowScreenshotBySlideIndex();
}
private void ShowScreenshotBySlideIndex()
{
textBoxScreenshotTitle.Text = string.Empty;
textBoxScreenshotFormat.Text = string.Empty;
textBoxScreenshotWidth.Text = string.Empty;
textBoxScreenshotHeight.Text = string.Empty;
textBoxScreenshotDate.Text = string.Empty;
textBoxScreenshotTime.Text = string.Empty;
TabPage selectedTabPage = tabControlViews.SelectedTab;
if (selectedTabPage != null)
{
ToolStrip toolStrip = (ToolStrip) selectedTabPage.Controls[selectedTabPage.Name + "toolStrip"];
ToolStripTextBox toolStripTextBox = (ToolStripTextBox) toolStrip.Items[selectedTabPage.Name + "toolStripTextBoxFilename"];
PictureBox pictureBox = (PictureBox) selectedTabPage.Controls[selectedTabPage.Name + "pictureBox"];
Screenshot selectedScreenshot = new Screenshot();
if (Slideshow.Index >= 0 && Slideshow.Index <= (Slideshow.Count - 1))
{
Slideshow.SelectedSlide = (Slide) listBoxScreenshots.Items[Slideshow.Index];
if (selectedTabPage.Tag.GetType() == typeof(Screen))
{
Screen screen = (Screen) selectedTabPage.Tag;
selectedScreenshot = _screenshotCollection.GetScreenshot(Slideshow.SelectedSlide.Name, screen.ViewId);
}
if (selectedTabPage.Tag.GetType() == typeof(Region))
{
Region region = (Region) selectedTabPage.Tag;
selectedScreenshot = _screenshotCollection.GetScreenshot(Slideshow.SelectedSlide.Name, region.ViewId);
}
}
string path = selectedScreenshot.Path;
if (!string.IsNullOrEmpty(path))
{
toolStripTextBox.Text = Path.GetFileName(path);
toolStripTextBox.ToolTipText = path;
FileInfo fileInfo = new FileInfo(path);
if (fileInfo.Directory != null && fileInfo.Directory.Root.Exists)
{
DriveInfo driveInfo = new DriveInfo(fileInfo.Directory.Root.FullName);
if (driveInfo.IsReady)
{
string dirName = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dirName))
{
if (Directory.Exists(dirName) && File.Exists(path))
{
toolStripTextBox.BackColor = Color.PaleGreen;
}
else
{
toolStripTextBox.BackColor = Color.PaleVioletRed;
toolStripTextBox.ToolTipText = $"Could not find or access image file at path \"{path}\"";
}
}
}
}
pictureBox.Image = _screenCapture.GetImageByPath(path);
if (pictureBox.Image != null)
{
textBoxScreenshotTitle.Text = selectedScreenshot.WindowTitle;
textBoxScreenshotFormat.Text = selectedScreenshot.Format.Name;
textBoxScreenshotWidth.Text = pictureBox.Image.Width.ToString();
textBoxScreenshotHeight.Text = pictureBox.Image.Height.ToString();
textBoxScreenshotDate.Text = selectedScreenshot.Date;
textBoxScreenshotTime.Text = selectedScreenshot.Time;
}
}
else
{
toolStripTextBox.Text = string.Empty;
toolStripTextBox.BackColor = Color.PaleVioletRed;
toolStripTextBox.ToolTipText = "Could not find or access image file";
pictureBox.Image = null;
}
}
}
/// <summary>
/// Converts the given hours, minutes, seconds, and milliseconds into an aggregate milliseconds value.
/// </summary>
/// <param name="hours">The number of hours to be converted.</param>
/// <param name="minutes">The number of minutes to be converted.</param>
/// <param name="seconds">The number of seconds to be converted.</param>
/// <param name="milliseconds">The number of milliseconds to be converted.</param>
/// <returns></returns>
private int ConvertIntoMilliseconds(int hours, int minutes, int seconds, int milliseconds)
{
return 1000 * (hours * 3600 + minutes * 60 + seconds) + milliseconds;
}
/// <summary>
/// Returns the screen capture interval. This value will be used as the screen capture timer's interval property.
/// </summary>
/// <returns></returns>
private int GetScreenCaptureInterval()
{
return ConvertIntoMilliseconds((int) numericUpDownHoursInterval.Value,
(int) numericUpDownMinutesInterval.Value, (int) numericUpDownSecondsInterval.Value,
(int) numericUpDownMillisecondsInterval.Value);
}
/// <summary>
/// Shows the list of screenshots when a date on the calendar has been selected.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DateSelected_monthCalendar(object sender, DateRangeEventArgs e)
{
ShowScreenshots();
}
/// <summary>
/// Shows the list of screenshots.
/// </summary>
private void ShowScreenshots()
{
SearchScreenshots();
if (!tabControlModules.SelectedTab.Name.Equals("tabPageScreenshots"))
{
tabControlModules.SelectedTab = tabControlModules.TabPages["tabPageScreenshots"];
}
ShowScreenshotBySlideIndex();
}
/// <summary>
/// Starts a screen capture session.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Click_toolStripMenuItemStartScreenCapture(object sender, EventArgs e)
{
StartScreenCapture();
}
/// <summary>
/// Stops the currently running screen capture session.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Click_toolStripMenuItemStopScreenCapture(object sender, EventArgs e)
{
StopScreenCapture();
}
/// <summary>
/// Exits the application from the system tray icon menu.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Click_toolStripMenuItemExit(object sender, EventArgs e)
{
ExitApplication();
}
/// <summary>
/// Exits the application.
/// </summary>
private void ExitApplication()
{
Log.Write("Exiting application");
if (ScreenCapture.LockScreenCaptureSession && !formEnterPassphrase.Visible)
{
Log.Write("Screen capture session is locked. Challenging user to enter correct passphrase to unlock");
formEnterPassphrase.ShowDialog(this);
}
// This is intentional. Do not rewrite these statements as an if/else
// because as soon as lockScreenCaptureSession is set to false we want
// to continue with normal functionality.
if (!ScreenCapture.LockScreenCaptureSession)
{
Log.Write("Running triggers of condition type ApplicationExit");
RunTriggersOfConditionType(TriggerConditionType.ApplicationExit);
Settings.User.GetByKey("StringPassphrase", defaultValue: false).Value = string.Empty;
SaveSettings();
DisableStopCapture();
EnableStartCapture();
_screenCapture.Count = 0;
_screenCapture.Running = false;
// Hide the system tray icon.
notifyIcon.Visible = false;
Log.Write("Hiding interface on clean application exit");
HideInterface();
Log.Write("Saving screenshots on clean application exit");
_screenshotCollection.Save((int)numericUpDownKeepScreenshotsForDays.Value);
if (runDateSearchThread != null && runDateSearchThread.IsBusy)
{
runDateSearchThread.CancelAsync();
}
if (runScreenshotSearchThread != null && runScreenshotSearchThread.IsBusy)
{
runScreenshotSearchThread.CancelAsync();
}
// Exit.
Environment.Exit(0);
}
}
/// <summary>
/// Runs the slide search thread.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>