-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FrmEditFileData.cs
2046 lines (1845 loc) · 89 KB
/
FrmEditFileData.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
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using GeoTagNinja.Helpers;
using GeoTagNinja.Model;
using GeoTagNinja.View.ListView;
using Microsoft.WindowsAPICodePack.Taskbar;
using NLog;
using TimeZoneConverter;
using static GeoTagNinja.FrmMainApp;
using static GeoTagNinja.Helpers.HelperControlAndMessageBoxHandling;
using static GeoTagNinja.Helpers.HelperGenericAncillaryListsArrays;
using static GeoTagNinja.Model.SourcesAndAttributes;
using HelperControlAndMessageBoxCustomMessageBoxManager =
GeoTagNinja.Helpers.HelperControlAndMessageBoxCustomMessageBoxManager;
using KeyEventArgs = System.Windows.Forms.KeyEventArgs;
namespace GeoTagNinja;
public partial class FrmEditFileData : Form
{
private static bool _tzChangedByApi;
#region Variables
private static bool _frmEditFileDataNowLoadingFileData;
#endregion
/// <summary>
/// This Form provides an interface for the user to edit various bits of Exif data in images.
/// </summary>
public FrmEditFileData()
{
Logger log = Log;
Log.Info(message: "Starting");
KeyPreview = true; // send keypress to the Form first
InitializeComponent();
// the custom logic is ugly af so no need to be pushy about it in light mode.
if (!HelperVariables.UserSettingUseDarkMode)
{
tcr_EditData.DrawMode = TabDrawMode.Normal;
lvw_FileListEditImages.OwnerDraw = false;
}
Log.Trace(message: "InitializeComponent OK");
HelperControlThemeManager.SetThemeColour(
themeColour: HelperVariables.UserSettingUseDarkMode
? ThemeColour.Dark
: ThemeColour.Light, parentControl: this);
}
/// <summary>
/// Fires when loading the form. Sets defaults for the listview and makes sure the app is ready to read the file data
/// ...w/o marking changes to textboxes (aka when a value changes the textbox formatting will generally turn to bold
/// but
/// ...when going from "nothing" to "something" that's obviously a change and we don't want that.)
/// </summary>
/// <param name="sender">Unused</param>
/// <param name="e">Unused</param>
private void FrmEditFileData_Load(object sender,
EventArgs e)
{
Logger log = Log;
Log.Info(message: "Starting");
Log.Trace(message: "Defaults Starting");
_frmEditFileDataNowLoadingFileData = true;
Log.Trace(
message:
"Emptying FrmMainApp.Stage1EditFormIntraTabTransferQueue + Stage2EditFormReadyToSaveAndMoveToWriteQueue");
foreach (DirectoryElement dirElemFileToModify in DirectoryElements)
{
{
foreach (ElementAttribute attribute in (ElementAttribute[])Enum.GetValues(
enumType: typeof(ElementAttribute)))
{
// empty queue
dirElemFileToModify.RemoveAttributeValue(
attribute: attribute,
version: DirectoryElement.AttributeVersion
.Stage1EditFormIntraTabTransferQueue);
// also empty the "original data" table
dirElemFileToModify.RemoveAttributeValue(
attribute: attribute,
version: DirectoryElement.AttributeVersion
.Stage2EditFormReadyToSaveAndMoveToWriteQueue);
}
}
}
Log.Trace(
message:
"Emptying FrmMainApp.Stage1EditFormIntraTabTransferQueue + Stage2EditFormReadyToSaveAndMoveToWriteQueue - Done");
Log.Trace(message: "Setting Dropdown defaults");
// Deal with Dates
// TakenDate
dtp_TakenDate.Enabled = true;
nud_TakenDateDaysShift.Enabled = false;
nud_TakenDateHoursShift.Enabled = false;
nud_TakenDateMinutesShift.Enabled = false;
nud_TakenDateSecondsShift.Enabled = false;
dtp_TakenDate.CustomFormat =
$"{CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern} {CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern}";
// CreateDate
dtp_CreateDate.Enabled = true;
nud_CreateDateDaysShift.Enabled = false;
nud_CreateDateHoursShift.Enabled = false;
nud_CreateDateMinutesShift.Enabled = false;
nud_CreateDateSecondsShift.Enabled = false;
dtp_CreateDate.CustomFormat =
$"{CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern} {CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern}";
ReturnControlText(
cItem: this, senderForm: this);
// fills the countries box
foreach (string country in GetCountries())
{
cbx_Country.Items.Add(item: country);
}
// fills the country codes box
foreach (string countryCode in
GetCountryCodes())
{
cbx_CountryCode.Items.Add(item: countryCode);
}
// load TZ-CBX
foreach (string timezone in GetTimeZones())
{
cbx_OffsetTime.Items.Add(item: timezone);
}
Log.Trace(message: "Setting Dropdown defaults - Done");
// this updates the listview itself
if (lvw_FileListEditImages.Items.Count > 0)
{
Log.Trace(message: "ListViewSelect Start");
lvw_FileListEditImages.Items[index: 0]
.Selected = true;
// actually if it's just one file i don't want this to be actively selectable
// also don't enable the "next" button.
if (lvw_FileListEditImages.Items.Count == 1)
{
lvw_FileListEditImages.Enabled = false;
btn_ApplyAndNext.Enabled = false;
}
Log.Trace(message: "ListViewSelect Done");
}
_frmEditFileDataNowLoadingFileData = false; // techinically this is redundant here
Log.Info(message: "Done");
}
/// <summary>
/// This method is responsible for retrieving the text values for the Controls in the Form.
/// For labels, buttons, etc., this is their "language" label (e.g., "Latitude").
/// For textboxes and similar controls, this is the value (e.g., "51.002").
/// The method also handles the assignment of these values to the corresponding controls.
/// </summary>
private void lvw_EditorFileListImagesGetData()
{
Logger log = Log;
Log.Info(message: "Starting");
_frmEditFileDataNowLoadingFileData = true;
ListView lvw = lvw_FileListEditImages;
ListViewItem lvi = lvw.SelectedItems[index: 0];
lvw.Columns[index: 0]
.Width = lvw.Width;
string fileNameWithoutPath = lvi.Text;
DirectoryElement dirElemFileToModify =
lvi.Tag as DirectoryElement;
FrmMainApp frmMainAppInstance =
(FrmMainApp)Application.OpenForms[name: "FrmMainApp"];
btn_InsertFromTakenDate.Enabled = false;
Log.Trace(message: "Assinging Labels Start");
HelperNonStatic helperNonstatic = new();
List<Type> lstControlTypesNoDEValue = new()
{
typeof(Label),
typeof(GroupBox),
typeof(Button),
typeof(CheckBox),
typeof(TabPage),
typeof(RadioButton)
};
List<Type> lstControlTypesWithDEValue = new()
{
typeof(TextBox),
typeof(ComboBox),
typeof(DateTimePicker),
typeof(NumericUpDown)
};
IEnumerable<Control> c = helperNonstatic.GetAllControls(control: this);
foreach (Control cItem in c)
{
if (lstControlTypesNoDEValue.Contains(item: cItem.GetType()) ||
lstControlTypesWithDEValue.Contains(item: cItem.GetType()))
{
string
debugItemName =
cItem.Name; // this is for debugging only (particularly here, that is.)
string controlNameWithoutTypeIdentifier;
try
{
Log.Trace(message: $"cItem: {debugItemName} (Type: {cItem.GetType().Name}) - Starting.");
if (lstControlTypesNoDEValue.Contains(item: cItem.GetType()))
{
// gets logged inside.
ReturnControlText(cItem: cItem, senderForm: this);
}
else if (lstControlTypesWithDEValue.Contains(item: cItem.GetType()))
{
// this gets the name of the Control without the type (e.g. tbx_SomethingAttrib becomes SomethingAttrib)
controlNameWithoutTypeIdentifier =
cItem.Name.Substring(startIndex: 4);
// get the ElementAttribute
ElementAttribute attribute =
GetElementAttributesElementAttribute(
attributeToFind: controlNameWithoutTypeIdentifier);
// get the AttributeVersion maxAttributeVersion
string stringValueOfCItem = NullStringEquivalentBlank;
DirectoryElement.AttributeVersion maxAttributeVersion =
GetDEAttributeMaxAttributeVersion(
directoryElement: dirElemFileToModify,
attribute: attribute);
// get the data in orig-type format
IConvertible dataInDirectoryElement = GetDataInDEForAttribute(
directoryElement: dirElemFileToModify,
attribute: attribute,
maxAttributeVersion: maxAttributeVersion);
// if we have data for the Attribute we convert it to string
if (dataInDirectoryElement != null &&
!string.IsNullOrWhiteSpace(
value: dataInDirectoryElement.ToString()))
{
stringValueOfCItem =
dataInDirectoryElement.ToString(
provider: CultureInfo.InvariantCulture);
cItem.Font = new Font(prototype: cItem.Font,
newStyle: FontStyle.Regular);
// technically this doesn't belong here as an overall-enabler but the code inside checks for details
EnableDateTimeItems(cItem: cItem);
}
else
{
// if it's none of the further below then make the cItem be just blank.
cItem.Text = NullStringEquivalentBlank;
// okay for gbx_*Date && !NUD -> those are DateTimePickers.
// If they are NULL then there is either no TakenDate or no CreateDate so the actual controls will have to be disabled.
// technically this doesn't belong here as an overall-enabler but the code inside checks for details
DisableDateTimeItems(cItem: cItem);
}
// wrangle the data
if (dataInDirectoryElement != null)
{
// stick into DE2 ("pending save") - this is to see if the data has changed later.
if (cItem is not NumericUpDown nud)
{
// this is related to storing the default DateTimes for TakenDate and CreateDate
// what we also need to do here is account for any copy-paste shifts.
if (cItem is DateTimePicker dtp)
{
HandleDateTimePickerTimeShift(dtp: dtp,
directoryElement:
dirElemFileToModify,
cItem: cItem,
maxAttributeVersion:
maxAttributeVersion);
}
else
{
cItem.Text = stringValueOfCItem;
}
Log.Trace(message:
$"cItem: {cItem.Name} - Adding to Stage2EditFormReadyToSaveAndMoveToWriteQueue");
dirElemFileToModify.SetAttributeValueAnyType(
attribute: attribute,
value: stringValueOfCItem,
version: DirectoryElement.AttributeVersion
.Stage2EditFormReadyToSaveAndMoveToWriteQueue,
isMarkedForDeletion: false);
}
else // if (cItem is NumericUpDown nud)
{
nud.Value = Convert.ToDecimal(
value: stringValueOfCItem,
provider: CultureInfo.InvariantCulture);
if (nud.Name.EndsWith(value: "Shift") &&
nud.Value != 0)
{
if (nud.Name.Substring(startIndex: 4)
.StartsWith(
value: TakenOrCreated.Taken.ToString()))
{
rbt_TakenDateTimeShift.Checked = true;
}
else if (nud.Name.Substring(startIndex: 4)
.StartsWith(
value: TakenOrCreated.Created
.ToString()))
{
rbt_CreateDateTimeShift.Checked = true;
}
}
dirElemFileToModify.SetAttributeValueAnyType(
attribute: attribute,
value: nud.Value.ToString(
provider: CultureInfo.InvariantCulture),
version: DirectoryElement.AttributeVersion
.Stage2EditFormReadyToSaveAndMoveToWriteQueue,
isMarkedForDeletion: false);
nud.Text = nud.Value.ToString();
}
// these don't have a "simple" text solution
if (cItem.Name ==
"cbx_CountryCode") // this will also fill in Country
{
string countryCodeInDirectoryElement =
stringValueOfCItem;
if (countryCodeInDirectoryElement != null &&
!string.IsNullOrEmpty(
value: countryCodeInDirectoryElement.ToString(
provider: CultureInfo.InvariantCulture)))
{
cbx_CountryCode.Text =
countryCodeInDirectoryElement.ToString(
provider: CultureInfo
.InvariantCulture);
string sqliteText = HelperDataLanguageTZ
.DataReadDTCountryCodesNames(
queryWhat: LanguageMappingQueryOrReturnWhat.ISO_3166_1A3,
inputVal: cbx_CountryCode.Text,
returnWhat: LanguageMappingQueryOrReturnWhat.Country);
cbx_Country.Text = sqliteText;
}
}
// Leaving this commented out on purpose...
// While the code works fine it's a source of confusion as Uses-DST isn't currently stored...
// anywhere and so upon reopening the Form this could lead to undesired results. ...
// I'll ponder on some reasonable ways to handle it if there's interest.
//else if (cItem.Name == "cbx_OffsetTime")
//{
// // attempt to convert offset to a member of the list
// string offsetTimeInDirectoryElement =
// dirElemFileToModify.GetAttributeValueString(
// attribute: ElementAttribute.OffsetTime,
// version: maxAttributeVersion);
// if (offsetTimeInDirectoryElement != null &&
// !string.IsNullOrEmpty(
// value: offsetTimeInDirectoryElement.ToString(
// provider: CultureInfo.InvariantCulture)))
// {
// cbx_OffsetTime.Text =
// GetFirstMatchingTzData(
// offsetTimeToMatch:
// offsetTimeInDirectoryElement);
// }
//}
}
if (maxAttributeVersion !=
DirectoryElement.AttributeVersion.Original)
{
cItem.Font =
new Font(prototype: cItem.Font,
newStyle: FontStyle.Bold);
}
}
}
catch
{
// ignored
}
Log.Trace(message: $"cItem: {cItem.Name} (Type: {cItem.GetType()
.Name}) - Done.");
}
}
Log.Trace(message: "Assinging Labels Done");
// done load
Log.Debug(message: "Done");
_frmEditFileDataNowLoadingFileData = false;
return;
void DisableDateTimeItems(Control cItem)
{
if (cItem.Parent.Name.StartsWith(value: "gbx_") &&
cItem.Parent.Name.EndsWith(value: "Date"))
{
if (cItem is not NumericUpDown nud)
{
if (cItem.Parent.Name == "gbx_TakenDate")
{
EnableSpecificControlAndDisableOthers(
parentControl: gbx_TakenDate,
controlsToEnable: new List<Control>
{ btn_InsertTakenDate },
controlsToDisable: helperNonstatic
.GetAllControls(control: gbx_TakenDate)
.Where(predicate: c =>
c != btn_InsertTakenDate)
.ToList());
}
else if (cItem.Parent.Name == "gbx_CreateDate")
{
EnableSpecificControlAndDisableOthers(
parentControl: gbx_CreateDate,
controlsToEnable: new List<Control>
{ btn_InsertCreateDate },
controlsToDisable: helperNonstatic
.GetAllControls(control: gbx_CreateDate)
.Where(predicate: c =>
c != btn_InsertCreateDate)
.ToList());
}
}
else // cItem is nud
{
nud.Value = NullIntEquivalent;
nud.Text = NullStringEquivalentZero;
}
}
}
void EnableDateTimeItems(Control cItem)
{
// if this is a TakenDate or CreateDate -related
if (cItem.Parent.Name.StartsWith(value: "gbx_") &&
cItem.Parent.Name.EndsWith(value: "Date") &&
cItem is not NumericUpDown)
{
// this code block deals with enabling and disabling the Controls on whether there is data behind.
if (cItem.Parent.Name == "gbx_TakenDate")
{
IEnumerable<Control> cGbx_TakenDate =
helperNonstatic.GetAllControls(
control: gbx_TakenDate);
List<Control> controlsToEnable = new();
List<Control> controlsToDisable = new()
{ btn_InsertTakenDate };
foreach (Control cItemGbx_TakenDate in cGbx_TakenDate)
{
if (cItemGbx_TakenDate != btn_InsertTakenDate)
{
controlsToEnable.Add(
item: cItemGbx_TakenDate);
}
}
EnableSpecificControlAndDisableOthers(
parentControl: gbx_TakenDate,
controlsToEnable: controlsToEnable,
controlsToDisable: controlsToDisable);
}
else if (cItem.Parent.Name == "gbx_CreateDate")
{
IEnumerable<Control> cGbx_CreateDate =
helperNonstatic.GetAllControls(
control: gbx_CreateDate);
List<Control> controlsToEnable = new();
List<Control> controlsToDisable = new()
{ btn_InsertCreateDate };
foreach (Control cItemGbx_CreateDate in
cGbx_CreateDate)
{
if (cItemGbx_CreateDate != btn_InsertCreateDate)
{
controlsToEnable.Add(
item: cItemGbx_CreateDate);
}
}
EnableSpecificControlAndDisableOthers(
parentControl: gbx_CreateDate,
controlsToEnable: controlsToEnable,
controlsToDisable: controlsToDisable);
}
}
}
void HandleDateTimePickerTimeShift(DateTimePicker dtp,
DirectoryElement directoryElement,
Control cItem,
DirectoryElement.AttributeVersion
maxAttributeVersion)
{
DateTime DECreateDate = default;
DateTime DETakenDate = default;
int totalShiftedSeconds = 0;
if (dtp == dtp_TakenDate &&
directoryElement.GetAttributeValue<DateTime>(
attribute: ElementAttribute.TakenDate,
version: DirectoryElement.AttributeVersion
.Original,
notFoundValue: null) !=
null)
{
DETakenDate = (DateTime)directoryElement.GetAttributeValue<DateTime>(
attribute: ElementAttribute.TakenDate,
version: DirectoryElement.AttributeVersion
.Original,
notFoundValue: null);
totalShiftedSeconds =
ShiftTimeForDateTimePicker(
whatToShift: TimeShiftTypes.TakenDate,
dirElemFileToModify: directoryElement);
dtp.Value =
DETakenDate.AddSeconds(value: totalShiftedSeconds);
}
else if (dtp == dtp_CreateDate &&
directoryElement.GetAttributeValue<DateTime>(
attribute: ElementAttribute.CreateDate,
version: DirectoryElement.AttributeVersion
.Original,
notFoundValue: null) !=
null)
{
DECreateDate = (DateTime)directoryElement.GetAttributeValue<DateTime>(
attribute: ElementAttribute.CreateDate,
version: DirectoryElement.AttributeVersion
.Original,
notFoundValue: null);
totalShiftedSeconds =
ShiftTimeForDateTimePicker(
whatToShift: TimeShiftTypes.CreateDate,
dirElemFileToModify: directoryElement);
dtp.Value =
DECreateDate.AddSeconds(value: totalShiftedSeconds);
}
Log.Trace(message: $"cItem: {cItem.Name} - Updating DateTimePicker");
if (maxAttributeVersion !=
DirectoryElement.AttributeVersion.Original ||
totalShiftedSeconds != 0)
{
dtp.Font =
new Font(prototype: dtp.Font,
newStyle: FontStyle.Bold);
}
}
}
/// <summary>
/// Retrieves the value of a specified attribute from a given directory element.
/// </summary>
/// <param name="directoryElement">The directory element from which to retrieve the attribute value.</param>
/// <param name="attribute">The attribute whose value is to be retrieved.</param>
/// <param name="maxAttributeVersion">The maximum version of the attribute to consider when retrieving the value.</param>
/// <returns>
/// The value of the specified attribute from the given directory element as an IConvertible, or null if the
/// attribute value is equivalent to the null equivalent for its type.
/// </returns>
// ReSharper disable once InconsistentNaming
private static IConvertible GetDataInDEForAttribute(DirectoryElement directoryElement,
ElementAttribute attribute,
DirectoryElement.AttributeVersion
maxAttributeVersion)
{
IConvertible returnDataInDirectoryElement = null;
Type typeOfAttribute = GetElementAttributesType(attributeToFind: attribute);
if (typeOfAttribute == typeof(string))
{
returnDataInDirectoryElement = directoryElement.GetAttributeValueString(
attribute: attribute,
version: maxAttributeVersion, nowSavingExif: false);
if (returnDataInDirectoryElement != null &&
string.IsNullOrEmpty(value: returnDataInDirectoryElement.ToString()))
{
returnDataInDirectoryElement = null;
}
}
else if (typeOfAttribute == typeof(int))
{
returnDataInDirectoryElement = directoryElement.GetAttributeValue<int>(
attribute: attribute,
version: maxAttributeVersion);
try
{
if ((int)returnDataInDirectoryElement == NullIntEquivalent)
{
returnDataInDirectoryElement = null;
}
}
catch
{
returnDataInDirectoryElement = null;
}
}
else if (typeOfAttribute == typeof(double))
{
returnDataInDirectoryElement = directoryElement.GetAttributeValue<double>(
attribute: attribute,
version: maxAttributeVersion);
try
{
if (returnDataInDirectoryElement != null &&
(double)returnDataInDirectoryElement == NullDoubleEquivalent)
{
returnDataInDirectoryElement = null;
}
}
catch
{
returnDataInDirectoryElement = null;
}
}
else if (typeOfAttribute == typeof(DateTime))
{
returnDataInDirectoryElement = directoryElement.GetAttributeValue<DateTime>(
attribute: attribute,
version: maxAttributeVersion);
try
{
if (returnDataInDirectoryElement != null &&
(DateTime)returnDataInDirectoryElement == NullDateTimeEquivalent)
{
returnDataInDirectoryElement = null;
}
}
catch
{
returnDataInDirectoryElement = null;
}
}
return returnDataInDirectoryElement;
}
/// <summary>
/// Enables specific controls and disables others within a given parent control.
/// </summary>
/// <param name="parentControl">The parent control that contains the controls to be enabled or disabled.</param>
/// <param name="controlsToEnable">A list of controls to be enabled.</param>
/// <param name="controlsToDisable">A list of controls to be disabled.</param>
/// <remarks>
/// This method iterates over all controls within the given parent control. If a control is in the list of controls to
/// be disabled, it is disabled. If a control is in the list of controls to be enabled, it is enabled.
/// </remarks>
private void EnableSpecificControlAndDisableOthers(Control parentControl,
List<Control> controlsToEnable,
List<Control> controlsToDisable)
{
HelperNonStatic helperNonstatic = new();
IEnumerable<Control> controls =
helperNonstatic.GetAllControls(control: parentControl);
foreach (Control item in controls)
{
if (controlsToDisable.Contains(item: item))
{
item.Enabled = false;
}
if (controlsToEnable.Contains(item: item))
{
item.Enabled = true;
}
}
}
/// <summary>
/// Calculates the total time shift in seconds for a DateTimePicker control.
/// </summary>
/// <param name="whatToShift">Specifies whether the CreateDate or TakenDate should be shifted.</param>
/// <param name="dirElemFileToModify">The DirectoryElement object that contains the attribute values for the time shift.</param>
/// <returns>Returns the total time shift in seconds.</returns>
[SuppressMessage(category: "ReSharper", checkId: "PossibleInvalidOperationException")]
private static int ShiftTimeForDateTimePicker(TimeShiftTypes whatToShift,
DirectoryElement dirElemFileToModify)
{
DirectoryElement.AttributeVersion attributeVersion =
DirectoryElement.AttributeVersion.Stage1EditFormIntraTabTransferQueue;
int shiftedDays = (int)dirElemFileToModify.GetAttributeValue<int>(
attribute: whatToShift == TimeShiftTypes.CreateDate
? ElementAttribute.CreateDateDaysShift
: ElementAttribute.TakenDateDaysShift,
version: attributeVersion,
notFoundValue: 0);
int shiftedHours = (int)dirElemFileToModify.GetAttributeValue<int>(
attribute: whatToShift == TimeShiftTypes.CreateDate
? ElementAttribute.CreateDateHoursShift
: ElementAttribute.TakenDateHoursShift,
version: attributeVersion,
notFoundValue: 0);
int shiftedMinutes = (int)dirElemFileToModify.GetAttributeValue<int>(
attribute: whatToShift == TimeShiftTypes.CreateDate
? ElementAttribute.CreateDateMinutesShift
: ElementAttribute.TakenDateMinutesShift,
version: attributeVersion,
notFoundValue: 0);
int shiftedSeconds = (int)dirElemFileToModify.GetAttributeValue<int>(
attribute: whatToShift == TimeShiftTypes.CreateDate
? ElementAttribute.CreateDateSecondsShift
: ElementAttribute.TakenDateSecondsShift,
version: attributeVersion,
notFoundValue: 0);
int totalShiftedSeconds = shiftedSeconds +
shiftedMinutes * 60 +
shiftedHours * 60 * 60 +
shiftedDays * 60 * 60 * 24;
return totalShiftedSeconds;
}
/// <summary>
/// Retrieves the highest version of a specific attribute in a given directory element.
/// </summary>
/// <param name="directoryElement">The directory element to inspect.</param>
/// <param name="attribute">The attribute to check for versions.</param>
/// <returns>
/// The highest version of the specified attribute in the directory element. Returns null if no version of the
/// attribute is found.
/// </returns>
// ReSharper disable once InconsistentNaming
private static DirectoryElement.AttributeVersion GetDEAttributeMaxAttributeVersion(
DirectoryElement directoryElement,
ElementAttribute attribute)
{
List<DirectoryElement.AttributeVersion> relevantAttributeVersions = new()
{
// DO NOT reorder!
DirectoryElement.AttributeVersion.Stage1EditFormIntraTabTransferQueue,
DirectoryElement.AttributeVersion.Stage3ReadyToWrite,
DirectoryElement.AttributeVersion.Original
};
DirectoryElement.AttributeVersion maxAttributeVersion =
relevantAttributeVersions.FirstOrDefault(
predicate: attributeVersion =>
directoryElement.HasSpecificAttributeWithVersion(attribute: attribute,
version: attributeVersion));
return maxAttributeVersion;
}
#region Themeing
// this is entirely the same as in FrmMainApp.
// via https://stackoverflow.com/a/75716080/3968494
private void ListView_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
Color foreColor = HelperVariables.UserSettingUseDarkMode
? Color.FromArgb(red: 241, green: 241, blue: 241)
: Color.Black;
Color backColor = HelperVariables.UserSettingUseDarkMode
? Color.FromArgb(red: 101, green: 151, blue: 151)
: SystemColors.Control;
//Fills one solid background for each cell.
using (SolidBrush backColorkBrush = new(color: backColor))
{
e.Graphics.FillRectangle(brush: backColorkBrush, rect: e.Bounds);
}
//Draw the borders for the header around each cell.
using (Pen foreColorPen = new(color: foreColor))
{
e.Graphics.DrawRectangle(pen: foreColorPen, rect: e.Bounds);
}
using (SolidBrush foreColorBrush = new(color: foreColor))
{
StringFormat stringFormat = GetStringFormat();
//Do some padding, since these draws right up next to the border for Left/Near. Will need to change this if you use Right/Far
Rectangle rect = e.Bounds;
rect.X += 2;
e.Graphics.DrawString(s: e.Header.Text, font: e.Font, brush: foreColorBrush,
layoutRectangle: rect, format: stringFormat);
}
}
private StringFormat GetStringFormat()
{
return new StringFormat
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Center
};
}
private void ListView_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
}
private void ListView_DrawSubItem(object sender,
DrawListViewSubItemEventArgs e)
{
e.DrawDefault = true;
}
#endregion
#region object events
/// <summary>
/// Pulls data for the various "Get (All) From Web" buttons depending which actual button has been pressed.
/// The TLDR logic is that if it's not the "All" button then we only read the currently active file else we read all
/// ...but ofc the currently not visible files' data doesn't show to the user so that goes into the holding tables.
/// </summary>
/// <param name="sender">The object that has been interacted with</param>
/// <param name="e">Unused</param>
private void btn_getFromWeb_Click(object sender,
EventArgs e)
{
DataTable dtToponomy = new();
FrmMainApp frmMainAppInstance =
(FrmMainApp)Application.OpenForms[name: "FrmMainApp"];
//reset this just in case.
HelperVariables.OperationAPIReturnedOKResponse = true;
switch (((Button)sender).Name)
{
case "btn_getFromWeb_Toponomy":
getFromWeb_Toponomy(fileNameWithoutPath: "");
break;
case "btn_getAllFromWeb_Toponomy":
foreach (ListViewItem lvi in lvw_FileListEditImages.Items)
{
string fileName = lvi.Text;
// for "this" file do the same as "normal" getfromweb
if (fileName ==
lvw_FileListEditImages.SelectedItems[index: 0]
.Text)
{
getFromWeb_Toponomy(fileNameWithoutPath: "");
// no need to write back to sql because it's done automatically on textboxChange
}
else
{
getFromWeb_Toponomy(fileNameWithoutPath: lvi.Text);
// get lat/long from main listview
lvi.ForeColor = Color.Red;
}
}
break;
default:
// took me a while to understand my own code. what we are doing here is that we are trying to tell the user (and by proxy, the developer) that something other than the two buttons defined above have been pressed.
HelperControlAndMessageBoxCustomMessageBoxManager.ShowMessageBox(
controlName: "mbx_FrmEditFileData_ErrorInvalidSender", captionType: MessageBoxCaption.Error,
buttons: MessageBoxButtons.OK, extraMessage: ((Button)sender).Name);
break;
}
string messageBoxName = HelperVariables.OperationAPIReturnedOKResponse
? "mbx_FrmEditFileData_InfoDataUpdated"
: "mbx_FrmEditFileData_ErrorAPIError";
MessageBoxCaption messageBoxCaption = HelperVariables.OperationAPIReturnedOKResponse
? MessageBoxCaption.Information
: MessageBoxCaption.Error;
HelperControlAndMessageBoxCustomMessageBoxManager.ShowMessageBox(controlName: messageBoxName,
captionType: messageBoxCaption,
buttons: MessageBoxButtons.OK);
}
/// <summary>
/// Pulls data from the various APIs and fills up the listView and fills the TextBoxes and/or SQLite.
/// </summary>
/// <param name="fileNameWithoutPath">Blank if used as "pull one file" otherwise the name of the file w/o Path</param>
private void getFromWeb_Toponomy(string fileNameWithoutPath = "")
{
FrmMainApp frmMainAppInstance =
(FrmMainApp)Application.OpenForms[name: "FrmMainApp"];
double parsedLat;
double parsedLng;
DateTime
createDate =
NullDateTimeEquivalent; // can't leave it null because it's updated in various IFs and C# perceives it as uninitialised.
string strGpsLatitude = null;
string strGpsLongitude = null;
DataTable dtToponomy = new();
// this is "current file"
if (fileNameWithoutPath == "")
{
if (nud_GPSLatitude.Text != "" &&
nud_GPSLongitude.Text != "")
{
strGpsLatitude =
nud_GPSLatitude.Value.ToString(
provider: CultureInfo.InvariantCulture);
strGpsLongitude =
nud_GPSLongitude.Value.ToString(
provider: CultureInfo.InvariantCulture);
HelperVariables.CurrentAltitude = null;
HelperVariables.CurrentAltitude =
nud_GPSAltitude.Text.ToString(provider: CultureInfo.InvariantCulture);
dtToponomy = HelperExifReadExifData.DTFromAPIExifGetToponomyFromWebOrSQL(
lat: strGpsLatitude,
lng: strGpsLongitude,
fileNameWithoutPath: fileNameWithoutPath);
}
}
// this is all the other files
else
{
if (frmMainAppInstance != null)
{
HelperVariables.CurrentAltitude = null;
HelperVariables.CurrentAltitude = frmMainAppInstance.lvw_FileList
.FindItemWithText(text: fileNameWithoutPath)
.SubItems[index: frmMainAppInstance
.lvw_FileList
.Columns[
key: FileListView.COL_NAME_PREFIX +
FileListView.FileListColumns.GPS_ALTITUDE]
.Index]
.Text.ToString(
provider: CultureInfo.InvariantCulture);
strGpsLatitude = frmMainAppInstance
.lvw_FileList.FindItemWithText(text: fileNameWithoutPath)
.SubItems[index: frmMainAppInstance.lvw_FileList
.Columns[
key: FileListView.COL_NAME_PREFIX +
FileListView.FileListColumns.GPS_LATITUDE]
.Index]
.Text.ToString(provider: CultureInfo.InvariantCulture);
strGpsLongitude = frmMainAppInstance
.lvw_FileList.FindItemWithText(text: fileNameWithoutPath)
.SubItems[index: frmMainAppInstance.lvw_FileList
.Columns[
key: FileListView.COL_NAME_PREFIX +
FileListView.FileListColumns
.GPS_LONGITUDE]
.Index]
.Text.ToString(provider: CultureInfo.InvariantCulture);