Menu

[r1586]: / trunk / GoogleContactsSync / SettingsForm.cs  Maximize  Restore  History

Download this file

2229 lines (1922 with data), 96.5 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
 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
using Google.Apis.Util.Store;
using Google.Apis.Requests;
using Microsoft.Win32;
using Serilog;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace GoContactSyncMod
{
internal partial class SettingsForm : Form
{
#region IdleTime, see https://blog.aaronlenoir.com/2016/02/16/detect-system-idle-in-windows-applications-2/
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public UInt32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public UInt32 dwTime;
}
private TimeSpan RetrieveIdleTime()
{
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = (uint)LASTINPUTINFO.SizeOf;
GetLastInputInfo(ref lastInputInfo);
int elapsedTicks = Environment.TickCount - (int)lastInputInfo.dwTime;
if (elapsedTicks > 0) { return new TimeSpan(0, 0, 0, 0, elapsedTicks); }
else { return new TimeSpan(0); }
}
#endregion IdleTime
//Singleton-Object
#region Singleton Definition
private static volatile SettingsForm instance;
private static readonly object syncRoot = new object();
public static SettingsForm Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
instance = new SettingsForm();
}
}
}
return instance;
}
}
#endregion
internal Synchronizer sync;
private SyncOption syncOption;
private DateTime lastSync;
private bool requestClose = false;
private bool boolShowBalloonTip = true;
private readonly CancellationTokenSource cancellationTokenSource;
private string prevUserName;
public const string AppRootKey = @"Software\GoContactSyncMOD";
public const string RegistrySyncOption = "SyncOption";
public const string RegistryUsername = "Username";
public const string RegistryAutoSync = "AutoSync";
public const string RegistryAutoSyncInterval = "AutoSyncInterval";
public const string RegistryAutoStart = "AutoStart";
public const string RegistryReportSyncResult = "ReportSyncResult";
public const string RegistrySyncDeletion = "SyncDeletion";
public const string RegistryPromptDeletion = "PromptDeletion";
public const string RegistrySyncAppointmentsMonthsInPast = "SyncAppointmentsMonthsInPast";
public const string RegistrySyncAppointmentsMonthsInFuture = "SyncAppointmentsMonthsInFuture";
public const string RegistrySyncAppointmentsMonthsInPastFlag = "SyncAppointmentsMonthsInPastFlag";
public const string RegistrySyncAppointmentsMonthsInFutureFlag = "SyncAppointmentsMonthsInFutureFlag";
public const string RegistrySyncAppointmentsTimezone = "SyncAppointmentsTimezone";
public const string RegistrySyncAppointments = "SyncAppointments";
public const string RegistrySyncAppointmentsForceRTF = "SyncAppointmentsForceRTF";
public const string RegistrySyncAppointmentsPrivate = "SyncAppointmentsPrivate";
public const string RegistrySyncOnlyIdle = "SyncOnlyIdle";
public const string RegistrySyncReminders = "SyncReminders";
public const string RegistryIncludePastReminders = "IncludePastReminders";
public const string RegistrySyncContacts = "SyncContacts";
public const string RegistrySyncContactsForceRTF = "SyncContactsForceRTF";
public const string RegistrySyncPhotos = "SyncPhotos";
public const string RegistryUseFileAs = "UseFileAs";
public const string RegistryLastSync = "LastSync";
public const string RegistrySyncContactsFolder = "SyncContactsFolder";
public const string RegistrySyncAppointmentsFolder = "SyncAppointmentsFolder";
public const string RegistrySyncAppointmentsGoogleFolder = "SyncAppointmentsGoogleFolder";
public const string RegistrySyncProfile = "SyncProfile";
private const int WaitingMinutesBeforeSync = 5;
private readonly ProxySettingsForm _proxy = new ProxySettingsForm();
private string syncContactsFolder = "";
private string syncAppointmentsFolder = "";
private string syncAppointmentsGoogleFolder = "";
private string Timezone = "";
private int cmbSyncProfile_PreviouslySelectedIndex = -1;
//private string _syncProfile;
private static string SyncProfile
{
get
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey);
return (regKeyAppRoot.GetValue(RegistrySyncProfile) != null) ?
(string)regKeyAppRoot.GetValue(RegistrySyncProfile) : null;
}
set
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey);
if (value != null)
{
regKeyAppRoot.SetValue(RegistrySyncProfile, value);
}
}
}
private readonly string ProfileRegistry;
private static bool OutlookFoldersLoaded = false;
private int executing; // make this static if you want this one-caller-only to
// all objects instead of a single object
private Thread syncThread;
//register window for lock/unlock messages of workstation
//private bool registered = false;
private delegate void TextHandler(string text);
private delegate void SwitchHandler(bool value);
private delegate void IconHandler();
private delegate DialogResult DialogHandler(string text);
private delegate void OnTimeZoneChangesCallback(string timeZone);
public DialogResult ShowDialog(string text)
{
return InvokeRequired
? (DialogResult)Invoke(new DialogHandler(ShowDialog), new object[] { text })
: MessageBox.Show(this, text, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
}
private readonly Icon IconError = Properties.Resources.sync_error;
private readonly Icon Icon0 = Properties.Resources.sync;
private readonly Icon Icon30 = Properties.Resources.sync_30;
private readonly Icon Icon60 = Properties.Resources.sync_60;
private readonly Icon Icon90 = Properties.Resources.sync_90;
private readonly Icon Icon120 = Properties.Resources.sync_120;
private readonly Icon Icon150 = Properties.Resources.sync_150;
private readonly Icon Icon180 = Properties.Resources.sync_180;
private readonly Icon Icon210 = Properties.Resources.sync_210;
private readonly Icon Icon240 = Properties.Resources.sync_240;
private readonly Icon Icon270 = Properties.Resources.sync_270;
private readonly Icon Icon300 = Properties.Resources.sync_300;
private readonly Icon Icon330 = Properties.Resources.sync_330;
private SettingsForm()
{
cmbSyncProfile_PreviouslySelectedIndex = -1;
/* Cannot set Font in designer as there is automatic sorting and Font will be set after AutoScaleDimensions
* This will prevent application to work correctly with high DPI systems. */
Font = new Font("Verdana", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
cancellationTokenSource = new CancellationTokenSource();
InitializeComponent();
Text = Text + " - " + Application.ProductVersion;
Program.EnableLogHandler(LogUpdatedHandler);
Log.Information($"Started application {Application.ProductName} ({Application.ProductVersion}) on {VersionInformation.GetWindowsVersion()} and {OutlookRegistryUtils.GetOutlookVersion()}");
var Folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GoContactSyncMOD\\";
Log.Information($"Detailed log file created in directory: {Folder}");
Log.Debug($"Total allocated memory at application start: {GC.GetTotalMemory(false):N0}");
if (IsAdministrator())
{
Log.Information("Started with Administrator priviliges");
}
ContactsMatcher.NotificationReceived += new ContactsMatcher.NotificationHandler(OnNotificationReceived);
AppointmentsMatcher.NotificationReceived += new AppointmentsMatcher.NotificationHandler(OnNotificationReceived);
PopulateSyncOptionBox();
//temporary remove the listener to avoid to load the settings twice, because it is set from SettingsForm.Designer.cs
cmbSyncProfile.SelectedIndexChanged -= new EventHandler(CmbSyncProfile_SelectedIndexChanged);
ProfileRegistry = FillSyncProfileItems() ? cmbSyncProfile.Text : null;
LoadSettings(ProfileRegistry);
//enable the listener
cmbSyncProfile.SelectedIndexChanged += new EventHandler(CmbSyncProfile_SelectedIndexChanged);
TimerSwitch(true);
lastSyncLabel.Text = "Not synced";
ValidateSyncButton();
//Register Session Lock Event
SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
//Register Power Mode Event
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeSwitch);
}
public static bool IsAdministrator()
{
return new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
}
private void PopulateSyncOptionBox()
{
string str;
for (var i = 0; i < 20; i++)
{
str = ((SyncOption)i).ToString();
if (str == i.ToString())
{
break;
}
// format (to add space before capital)
var matches = Regex.Matches(str, "[A-Z]");
for (var k = 0; k < matches.Count; k++)
{
str = str.Replace(str[matches[k].Index].ToString(), " " + str[matches[k].Index]);
matches = Regex.Matches(str, "[A-Z]");
}
str = str.Replace(" ", " ");
// fix start
str = str.Substring(1);
syncOptionBox.Items.Add(str);
}
}
private void FillSyncFolderItems()
{
if (InvokeRequired)
{
Invoke(new InvokeCallback(FillSyncFolderItems));
}
else
{
lock (syncRoot)
{
if (OutlookFoldersLoaded)
{
return;
}
Log.Debug("FillSyncFolderItems - start");
if (contactFoldersComboBox.DataSource == null || appointmentFoldersComboBox.DataSource == null ||
(appointmentGoogleFoldersComboBox.DataSource == null && btSyncAppointments.Checked) ||
contactFoldersComboBox.Items.Count == 0 || appointmentFoldersComboBox.Items.Count == 0 ||
(appointmentGoogleFoldersComboBox.Items.Count == 0 && btSyncAppointments.Checked))
{
Log.Information("Loading Outlook folders...");
contactFoldersComboBox.Visible = btSyncContactsForceRTF.Visible = SyncPhotosCheckBox.Visible = btSyncContacts.Checked;
labelTimezone.Visible = btMonthsPast.Visible = btMonthsFuture.Visible = btSyncAppointments.Checked;
appointmentFoldersComboBox.Visible = appointmentGoogleFoldersComboBox.Visible = appointmentTimezonesComboBox.Visible = btSyncAppointmentsForceRTF.Visible = btSyncAppointmentsPrivate.Visible = btSyncAppointments.Checked;
futureMonthInterval.Visible = btSyncAppointments.Checked && btMonthsFuture.Checked;
pastMonthInterval.Visible = btSyncAppointments.Checked && btMonthsPast.Checked;
cmbSyncProfile.Visible = true;
var defaultText = " --- Select an Outlook folder ---";
var outlookContactFolders = new ArrayList();
var outlookAppointmentFolders = new ArrayList();
try
{
Cursor = Cursors.WaitCursor;
SuspendLayout();
contactFoldersComboBox.BeginUpdate();
appointmentFoldersComboBox.BeginUpdate();
contactFoldersComboBox.DataSource = null;
appointmentFoldersComboBox.DataSource = null;
try
{ //Add Default Contacts Folder
var defaultFolder = Synchronizer.OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
outlookContactFolders.Add(new OutlookFolder(defaultFolder.FolderPath, defaultFolder.EntryID, true));
}
catch (Exception e)
{
Log.Debug(e, "Exception");
Log.Warning("Error adding OlDefaultFolders.olFolderContacts: " + e.Message);
}
try
{//Add Default Calendar/Appointment folder
var defaultFolder = Synchronizer.OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
outlookAppointmentFolders.Add(new OutlookFolder(defaultFolder.FolderPath, defaultFolder.EntryID, true));
}
catch (Exception e)
{
Log.Debug(e, "Exception");
Log.Warning("Error adding OlDefaultFolders.olFolderCalendar: " + e.Message);
}
var folders = Synchronizer.OutlookNameSpace.Folders;
for (var i = 1; i <= folders.Count; i++)
{
try
{
var folder = folders[i];
GetOutlookMAPIFolders(outlookContactFolders, outlookAppointmentFolders, folder);
}
catch (System.Exception e)
{
Log.Debug(e, "Exception");
Log.Warning("Error getting available Outlook folders: " + e.Message);
}
}
if (outlookContactFolders != null)
{
outlookContactFolders.Sort();
outlookContactFolders.Insert(0, new OutlookFolder(defaultText, defaultText, false));
contactFoldersComboBox.DataSource = outlookContactFolders;
contactFoldersComboBox.DisplayMember = "DisplayName";
contactFoldersComboBox.ValueMember = "FolderID";
}
if (outlookAppointmentFolders != null)
{
outlookAppointmentFolders.Sort();
outlookAppointmentFolders.Insert(0, new OutlookFolder(defaultText, defaultText, false));
appointmentFoldersComboBox.DataSource = outlookAppointmentFolders;
appointmentFoldersComboBox.DisplayMember = "DisplayName";
appointmentFoldersComboBox.ValueMember = "FolderID";
}
contactFoldersComboBox.EndUpdate();
appointmentFoldersComboBox.EndUpdate();
contactFoldersComboBox.SelectedValue = defaultText;
appointmentFoldersComboBox.SelectedValue = defaultText;
//If user has not yet selected any folder, select one based on Outlook default folder
if (contactFoldersComboBox.SelectedIndex < 1)
{
foreach (OutlookFolder folder in contactFoldersComboBox.Items)
{
if (folder.IsDefaultFolder)
{
contactFoldersComboBox.SelectedValue = folder.FolderID;
break;
}
}
}
//If user has not yet selected any folder, select one based on Outlook default folder
if (appointmentFoldersComboBox.SelectedIndex < 1)
{
foreach (OutlookFolder folder in appointmentFoldersComboBox.Items)
{
if (folder.IsDefaultFolder)
{
appointmentFoldersComboBox.SelectedItem = folder;
break;
}
}
}
Log.Information("Loaded Outlook folders.");
}
catch (NotSupportedException e)
{
//Log.Warning(e.Message);
ErrorHandler.Handle(e);
}
catch (Exception e)
{
Log.Debug(e, "Exception");
Log.Warning("Error getting available Outlook and Google folders: " + e.Message);
}
finally
{
Cursor = Cursors.Default;
ResumeLayout();
}
}
LoadSettingsFolders(ProfileRegistry);
if ((contactFoldersComboBox.SelectedIndex == -1) && (contactFoldersComboBox.Items.Count > 0))
{
contactFoldersComboBox.SelectedIndex = 0;
}
if ((appointmentFoldersComboBox.SelectedIndex == -1) && (appointmentFoldersComboBox.Items.Count > 0))
{
appointmentFoldersComboBox.SelectedIndex = 0;
}
OutlookFoldersLoaded = true;
Log.Debug("FillSyncFolderItems - finish");
}
if (!ValidSyncContactFolders)
{
Log.Debug(@"contactFoldersComboBox.SelectedIndex: " + contactFoldersComboBox.SelectedIndex);
Log.Debug(@"contactFoldersComboBox.Items.Count: " + contactFoldersComboBox.Items.Count);
throw new Exception("Outlook contact folder is not selected or invalid!");
}
if (!ValidSyncAppointmentFolders)
{
Log.Debug($"appointmentFoldersComboBox.SelectedIndex: {appointmentFoldersComboBox.SelectedIndex}");
Log.Debug($"appointmentFoldersComboBox.Items.Count: {appointmentFoldersComboBox.Items.Count}");
Log.Debug($"appointmentGoogleFoldersComboBox.SelectedIndex: {appointmentGoogleFoldersComboBox.SelectedIndex}");
Log.Debug($"appointmentGoogleFoldersComboBox.Items.Count: {appointmentGoogleFoldersComboBox.Items.Count}");
if (appointmentGoogleFoldersComboBox.SelectedIndex <= 0)
{
throw new ApplicationException("Please select in Settings GUI a Google appointment folder to be synchronized");
}
else if (appointmentFoldersComboBox.SelectedIndex <= 0)
{
throw new ApplicationException("Please select in Settings GUI an Outlook appointment folder to be synchronized");
}
else
{
throw new ApplicationException("At least one Outlook or Google appointment folder is not selected or invalid!");
}
}
}
}
/// <summary>
/// recursively scan thru all Outlook MapiFolders to search for existing Contact and Appointment folders
/// </summary>
/// <param name="outlookContactFolders">to be filled array with contact folders found</param>
/// <param name="outlookAppointmentFolders">to be filled array with appointment folders found</param>
/// <param name="folder">parent folder to scan through child folders</param>
public static void GetOutlookMAPIFolders(ArrayList outlookContactFolders, ArrayList outlookAppointmentFolders, Outlook.MAPIFolder folder)
{
for (var i = 1; i <= folder.Folders.Count; i++)
{
Outlook.MAPIFolder mapi = null;
try
{
mapi = folder.Folders[i];
if (mapi.DefaultItemType == Outlook.OlItemType.olContactItem)
{
var isDefaultFolder = mapi.EntryID.Equals(Synchronizer.OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts).EntryID);
if (!isDefaultFolder) //only add, if not Default Folder, because DefaultFolder already added before calling this method
outlookContactFolders.Add(new OutlookFolder(mapi.FolderPath, mapi.EntryID, isDefaultFolder));
}
if (mapi.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
{
var isDefaultFolder = mapi.EntryID.Equals(Synchronizer.OutlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar).EntryID);
if (!isDefaultFolder) //only add, if not Default Folder, because DefaultFolder already added before calling this method
outlookAppointmentFolders.Add(new OutlookFolder(mapi.FolderPath, mapi.EntryID, isDefaultFolder));
}
//Cannot be fixed by patch provided in bug #1363: https://sourceforge.net/p/googlesyncmod/bugs/1363/, because otherwise we will redundantly search ALL folders, even sub-sub-sub-... folders of Emails. Please use a Contact or Calendar root parent folder to keep your contacts and Calendars subfolders in
if (mapi.DefaultItemType == Outlook.OlItemType.olContactItem ||
mapi.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
{
GetOutlookMAPIFolders(outlookContactFolders, outlookAppointmentFolders, mapi);
}
}
catch (COMException e)
{
Log.Debug(e, "Exception");
}
finally
{
if (mapi != null)
{
Marshal.ReleaseComObject(mapi);
}
}
}
}
private void ClearSettings()
{
SetSyncOption(0);
autoSyncCheckBox.Checked = runAtStartupCheckBox.Checked = reportSyncResultCheckBox.Checked = false;
autoSyncInterval.Value = 2;
_proxy.ClearSettings();
}
// Fill lists of sync profiles
private bool FillSyncProfileItems()
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey);
var vReturn = false;
cmbSyncProfile.Items.Clear();
cmbSyncProfile.Items.Add("[Add new profile...]");
foreach (var subKeyName in regKeyAppRoot.GetSubKeyNames())
{
if (!string.IsNullOrEmpty(subKeyName))
{
cmbSyncProfile.Items.Add(subKeyName);
}
}
if (SyncProfile == null)
{
SyncProfile = "Default_" + Environment.MachineName;
}
if (cmbSyncProfile.Items.Count == 1)
{
cmbSyncProfile.Items.Add(SyncProfile);
}
else
{
vReturn = true;
}
cmbSyncProfile.Items.Add("[Configuration manager...]");
cmbSyncProfile.Text = SyncProfile;
cmbSyncProfile_PreviouslySelectedIndex = cmbSyncProfile.SelectedIndex;
return vReturn;
}
private void LoadSettings(string _profile)
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey + (_profile != null ? ('\\' + _profile) : ""));
if (regKeyAppRoot.GetValue(RegistrySyncOption) != null)
{
syncOption = (SyncOption)regKeyAppRoot.GetValue(RegistrySyncOption);
SetSyncOption((int)syncOption);
}
if (regKeyAppRoot.GetValue(RegistryUsername) != null)
{
prevUserName = regKeyAppRoot.GetValue(RegistryUsername) as string;
UserName.Text = prevUserName;
}
//temporary remove listener
autoSyncCheckBox.CheckedChanged -= new EventHandler(AutoSyncCheckBox_CheckedChanged);
btSyncContacts.CheckedChanged -= new EventHandler(BtSyncContacts_CheckedChanged);
btSyncAppointments.CheckedChanged -= new EventHandler(BtSyncAppointments_CheckedChanged);
SyncRemindersCheckBox.CheckedChanged -= new EventHandler(SyncReminders_CheckedChanged);
ReadRegistryIntoCheckBox(autoSyncCheckBox, regKeyAppRoot.GetValue(RegistryAutoSync));
ReadRegistryIntoNumber(autoSyncInterval, regKeyAppRoot.GetValue(RegistryAutoSyncInterval));
ReadRegistryIntoCheckBox(runAtStartupCheckBox, regKeyAppRoot.GetValue(RegistryAutoStart));
ReadRegistryIntoCheckBox(reportSyncResultCheckBox, regKeyAppRoot.GetValue(RegistryReportSyncResult));
ReadRegistryIntoCheckBox(btSyncDelete, regKeyAppRoot.GetValue(RegistrySyncDeletion));
ReadRegistryIntoCheckBox(btPromptDelete, regKeyAppRoot.GetValue(RegistryPromptDeletion));
ReadRegistryIntoNumber(pastMonthInterval, regKeyAppRoot.GetValue(RegistrySyncAppointmentsMonthsInPast));
ReadRegistryIntoNumber(futureMonthInterval, regKeyAppRoot.GetValue(RegistrySyncAppointmentsMonthsInFuture));
ReadRegistryIntoCheckBox(btMonthsPast, regKeyAppRoot.GetValue(RegistrySyncAppointmentsMonthsInPastFlag));
ReadRegistryIntoCheckBox(btMonthsFuture, regKeyAppRoot.GetValue(RegistrySyncAppointmentsMonthsInFutureFlag));
if (regKeyAppRoot.GetValue(RegistrySyncAppointmentsTimezone) != null)
{
appointmentTimezonesComboBox.Text = regKeyAppRoot.GetValue(RegistrySyncAppointmentsTimezone) as string;
}
ReadRegistryIntoCheckBox(btSyncAppointments, regKeyAppRoot.GetValue(RegistrySyncAppointments));
ReadRegistryIntoCheckBox(btSyncContacts, regKeyAppRoot.GetValue(RegistrySyncContacts));
ReadRegistryIntoCheckBox(chkUseFileAs, regKeyAppRoot.GetValue(RegistryUseFileAs));
ReadRegistryIntoCheckBox(btSyncContactsForceRTF, regKeyAppRoot.GetValue(RegistrySyncContactsForceRTF));
ReadRegistryIntoCheckBox(SyncPhotosCheckBox, regKeyAppRoot.GetValue(RegistrySyncPhotos));
ReadRegistryIntoCheckBox(btSyncAppointmentsForceRTF, regKeyAppRoot.GetValue(RegistrySyncAppointmentsForceRTF));
ReadRegistryIntoCheckBox(btSyncAppointmentsPrivate, regKeyAppRoot.GetValue(RegistrySyncAppointmentsPrivate));
ReadRegistryIntoCheckBox(btSyncOnlyIdle, regKeyAppRoot.GetValue(RegistrySyncOnlyIdle));
ReadRegistryIntoCheckBox(SyncRemindersCheckBox, regKeyAppRoot.GetValue(RegistrySyncReminders));
if (! ReadRegistryIntoCheckBox(IncludePastRemindersCheckBox, regKeyAppRoot.GetValue(RegistryIncludePastReminders)))
IncludePastRemindersCheckBox.Checked = false;
if (regKeyAppRoot.GetValue(RegistryLastSync) != null)
{
try
{
lastSync = new DateTime(Convert.ToInt64(regKeyAppRoot.GetValue(RegistryLastSync)));
SetLastSyncText(lastSync.ToString());
}
catch (FormatException ex)
{
Log.Warning("LastSyncDate couldn't be read from registry (" + regKeyAppRoot.GetValue(RegistryLastSync) + "): " + ex);
}
}
//autoSyncCheckBox_CheckedChanged(null, null);
BtSyncContacts_CheckedChanged(null, null);
_proxy.LoadSettings(_profile);
contactFoldersComboBox.Visible = btSyncContacts.Checked;
btSyncContactsForceRTF.Visible = btSyncContacts.Checked;
SyncPhotosCheckBox.Visible = btSyncContacts.Checked;
appointmentFoldersComboBox.Visible = appointmentGoogleFoldersComboBox.Visible = btSyncAppointments.Checked;
labelTimezone.Visible = btMonthsPast.Visible = btMonthsFuture.Visible = btSyncAppointments.Checked;
appointmentTimezonesComboBox.Visible = btSyncAppointments.Checked;
pastMonthInterval.Visible = btSyncAppointments.Checked && btMonthsPast.Checked;
futureMonthInterval.Visible = btSyncAppointments.Checked && btMonthsFuture.Checked;
btSyncAppointmentsForceRTF.Visible = btSyncAppointments.Checked;
btSyncAppointmentsPrivate.Visible = SyncRemindersCheckBox.Visible = SyncRemindersCheckBox.Visible = btSyncAppointments.Checked;
IncludePastRemindersCheckBox.Visible = btSyncAppointments.Checked && SyncRemindersCheckBox.Checked;
//enable temporary disabled listener
autoSyncCheckBox.CheckedChanged += new EventHandler(AutoSyncCheckBox_CheckedChanged);
btSyncContacts.CheckedChanged += new EventHandler(BtSyncContacts_CheckedChanged);
btSyncAppointments.CheckedChanged += new EventHandler(BtSyncAppointments_CheckedChanged);
SyncRemindersCheckBox.CheckedChanged += new EventHandler(SyncReminders_CheckedChanged);
}
private static bool ReadRegistryIntoCheckBox(CheckBox checkbox, object registryEntry)
{
if (registryEntry == null)
return false;
try
{
checkbox.Checked = Convert.ToBoolean(registryEntry);
return true;
}
catch (FormatException ex)
{
Log.Warning(checkbox.Name + " couldn't be read from registry (" + registryEntry + "), was kept at default (" + checkbox.Checked + "): " + ex);
return false;
}
}
private static void ReadRegistryIntoNumber(NumericUpDown numericUpDown, object registryEntry)
{
if (registryEntry != null)
{
var interval = Convert.ToDecimal(registryEntry);
if (interval < numericUpDown.Minimum)
{
numericUpDown.Value = numericUpDown.Minimum;
Log.Warning(numericUpDown.Name + " read from registry was below range (" + interval + "), was set to minimum (" + numericUpDown.Minimum + ")");
}
else if (interval > numericUpDown.Maximum)
{
numericUpDown.Value = numericUpDown.Maximum;
Log.Warning(numericUpDown.Name + " read from registry was above range (" + interval + "), was set to maximum (" + numericUpDown.Maximum + ")");
}
else
{
numericUpDown.Value = interval;
}
}
}
private void LoadSettingsFolders(string _profile)
{
Log.Debug("Loading settings folders...");
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey + (_profile != null ? ('\\' + _profile) : ""));
var regKeyValueStr = regKeyAppRoot.GetValue(RegistrySyncContactsFolder) as string;
if (!string.IsNullOrEmpty(regKeyValueStr))
{
foreach (OutlookFolder i in contactFoldersComboBox.Items)
{
if (i.FolderID == regKeyValueStr)
{
contactFoldersComboBox.SelectedValue = regKeyValueStr;
break;
}
}
}
regKeyValueStr = regKeyAppRoot.GetValue(RegistrySyncAppointmentsFolder) as string;
if (!string.IsNullOrEmpty(regKeyValueStr))
{
foreach (OutlookFolder i in appointmentFoldersComboBox.Items)
{
if (i.FolderID == regKeyValueStr)
{
appointmentFoldersComboBox.SelectedValue = regKeyValueStr;
break;
}
}
}
if (appointmentGoogleFoldersComboBox.DataSource == null)
{
LoadAppointmentGoogleFoldersComboBox();
}
regKeyValueStr = regKeyAppRoot.GetValue(RegistrySyncAppointmentsGoogleFolder) as string;
if (!string.IsNullOrEmpty(regKeyValueStr))
{
appointmentGoogleFoldersComboBox.SelectedValue = regKeyValueStr;
if ((appointmentGoogleFoldersComboBox.SelectedIndex == -1) && (appointmentGoogleFoldersComboBox.Items.Count > 0))
{
appointmentGoogleFoldersComboBox.SelectedIndex = 0;
}
}
Log.Debug("Loaded settings folders...");
}
private void SaveSettings()
{
SaveSettings(cmbSyncProfile.Text);
}
private void SaveSettings(string profile)
{
if (!string.IsNullOrEmpty(profile))
{
SyncProfile = cmbSyncProfile.Text;
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey + "\\" + profile);
regKeyAppRoot.SetValue(RegistrySyncOption, (int)syncOption);
if (!string.IsNullOrEmpty(UserName.Text))
{
regKeyAppRoot.SetValue(RegistryUsername, UserName.Text);
}
regKeyAppRoot.SetValue(RegistryAutoSync, autoSyncCheckBox.Checked.ToString());
regKeyAppRoot.SetValue(RegistryAutoSyncInterval, autoSyncInterval.Value.ToString());
regKeyAppRoot.SetValue(RegistryAutoStart, runAtStartupCheckBox.Checked);
regKeyAppRoot.SetValue(RegistryReportSyncResult, reportSyncResultCheckBox.Checked);
regKeyAppRoot.SetValue(RegistrySyncDeletion, btSyncDelete.Checked);
regKeyAppRoot.SetValue(RegistryPromptDeletion, btPromptDelete.Checked);
regKeyAppRoot.SetValue(RegistrySyncAppointmentsMonthsInPast, pastMonthInterval.Value.ToString());
regKeyAppRoot.SetValue(RegistrySyncAppointmentsMonthsInFuture, futureMonthInterval.Value.ToString());
regKeyAppRoot.SetValue(RegistrySyncAppointmentsMonthsInPastFlag, btMonthsPast.Checked.ToString());
regKeyAppRoot.SetValue(RegistrySyncAppointmentsMonthsInFutureFlag, btMonthsFuture.Checked.ToString());
regKeyAppRoot.SetValue(RegistrySyncAppointmentsTimezone, appointmentTimezonesComboBox.Text);
regKeyAppRoot.SetValue(RegistrySyncAppointments, btSyncAppointments.Checked);
regKeyAppRoot.SetValue(RegistrySyncAppointmentsForceRTF, btSyncAppointmentsForceRTF.Checked);
regKeyAppRoot.SetValue(RegistrySyncAppointmentsPrivate, btSyncAppointmentsPrivate.Checked);
regKeyAppRoot.SetValue(RegistrySyncOnlyIdle, btSyncOnlyIdle.Checked);
regKeyAppRoot.SetValue(RegistrySyncReminders, SyncRemindersCheckBox.Checked);
regKeyAppRoot.SetValue(RegistryIncludePastReminders, IncludePastRemindersCheckBox.Checked);
regKeyAppRoot.SetValue(RegistrySyncContacts, btSyncContacts.Checked);
regKeyAppRoot.SetValue(RegistrySyncContactsForceRTF, btSyncContactsForceRTF.Checked);
regKeyAppRoot.SetValue(RegistrySyncPhotos, SyncPhotosCheckBox.Checked);
regKeyAppRoot.SetValue(RegistryUseFileAs, chkUseFileAs.Checked);
regKeyAppRoot.SetValue(RegistryLastSync, lastSync.Ticks);
_proxy.SaveSettings(cmbSyncProfile.Text);
}
}
private bool ValidSyncFolders => ValidSyncContactFolders && ValidSyncAppointmentFolders;
private bool ValidSyncContactFolders => (contactFoldersComboBox.SelectedIndex >= 1 && contactFoldersComboBox.SelectedIndex < contactFoldersComboBox.Items.Count)
|| !btSyncContacts.Checked;
private bool ValidSyncAppointmentFolders => ValidSyncOutlookAppointmentFolders && ValidSyncGoogleAppointmentFolders || !btSyncAppointments.Checked;
private bool ValidSyncOutlookAppointmentFolders => appointmentFoldersComboBox.SelectedIndex >= 1 && appointmentFoldersComboBox.SelectedIndex < appointmentFoldersComboBox.Items.Count;
private bool ValidSyncGoogleAppointmentFolders => appointmentGoogleFoldersComboBox.SelectedIndex >= 1 && appointmentGoogleFoldersComboBox.SelectedIndex <appointmentGoogleFoldersComboBox.Items.Count;
private bool ValidUserName
{
get
{
var userNameIsValid = Regex.IsMatch(UserName.Text, @"^(?'id'[a-z0-9\'\%\._\+\-]+)@(?'domain'[a-z0-9\'\%\._\+\-]+)\.(?'ext'[a-z]{2,6})$", RegexOptions.IgnoreCase);
SetBgColor(UserName, userNameIsValid);
if (!userNameIsValid)
{
toolTip.SetToolTip(UserName, "User is of wrong format, should be full Google Mail address, e.g. user@gmail.com");
}
else
{
toolTip.SetToolTip(UserName, string.Empty);
}
return userNameIsValid;
}
}
private bool ValidProfile
{
get
{
var syncProfileIsValid = cmbSyncProfile.SelectedIndex > 0 && cmbSyncProfile.SelectedIndex < cmbSyncProfile.Items.Count - 1;
SetBgColor(cmbSyncProfile, syncProfileIsValid);
return syncProfileIsValid;
}
}
private static void SetBgColor(Control box, bool isValid)
{
box.BackColor = !isValid ? Color.LightPink : (box is ListBox)? ListBox.DefaultBackColor: (box is CheckBox) ? CheckBox.DefaultBackColor: Color.LightGreen;
}
private void SyncButton_Click(object sender, EventArgs e)
{
Sync();
}
private async void Sync()
{
try
{
if (syncThread != null && syncThread.IsAlive)
{
TimerSwitch(false);
Log.Information("The sync thread is already running and alive, waiting for sync thread to finish");
await Task.Delay(15 * 3600 * 1000, this.cancellationTokenSource.Token); //Wait for 15 minutes
if (syncThread != null && syncThread.IsAlive) //Check if still alive after 15 minutes
{
Log.Information("The sync thread is still running and alive after waiting, aborting the running thread and try to start a new thread after next sync interval");
//cancel current and continue starting a new sync thread
CancelButton_Click(this, EventArgs.Empty);
}
return; //return and wait for next sync interval
}
if (!ValidUserName)
{
//return;
throw new Exception("E-Mail address is incomplete or incorrect - Maybe a typo...");
}
if (!ValidProfile)
{
//return;
throw new Exception("Please select a Sync Profile...");
}
FillSyncFolderItems();
//IconTimerSwitch(true);
var starter = new ThreadStart(Sync_ThreadStarter);
syncThread = new Thread(starter)
{
CurrentCulture = CultureInfo.CreateSpecificCulture("en-US"),
CurrentUICulture = new CultureInfo("en-US")
};
syncThread.Start();
//if new version on sourceforge.net website than print an information to the log
Log.Debug("Check version from Sync");
CheckVersion();
// wait for thread to start
for (var i = 0; !syncThread.IsAlive && i < 10; i++)
{
Thread.Sleep(1000);//Do nothing, until the thread was started, but only wait maximum 10 seconds
}
}
catch (Exception ex)
{
TimerSwitch(false);
ShowForm();
ErrorHandler.Handle(ex);
}
}
[STAThread]
private async void Sync_ThreadStarter()
{
//==>Instead of lock, use Interlocked to exit the code, if already another thread is calling the same
var won = false;
try
{
won = Interlocked.CompareExchange(ref executing, 1, 0) == 0;
if (won)
{
TimerSwitch(false);
//if the contacts folder has changed ==> Reset matches (to not delete contacts on the one or other side)
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(AppRootKey + "\\" + SyncProfile);
var oldSyncContactsFolder = regKeyAppRoot.GetValue(RegistrySyncContactsFolder) as string;
var oldSyncAppointmentsFolder = regKeyAppRoot.GetValue(RegistrySyncAppointmentsFolder) as string;
var oldSyncAppointmentsGoogleFolder = regKeyAppRoot.GetValue(RegistrySyncAppointmentsGoogleFolder) as string;
//only reset contacts if ContactsFolder changed
//and only reset appointments, if either OutlookAppointmentsFolder changed (without changing Google at the same time) or GoogleAppointmentsFolder changed (without changing Outlook at the same time) (not chosen before means not changed)
var syncContacts = !string.IsNullOrEmpty(oldSyncContactsFolder) && !oldSyncContactsFolder.Equals(syncContactsFolder) && btSyncContacts.Checked;
var syncAppointments = !string.IsNullOrEmpty(oldSyncAppointmentsFolder) && !oldSyncAppointmentsFolder.Equals(syncAppointmentsFolder) && btSyncAppointments.Checked;
var syncGoogleAppointments = !string.IsNullOrEmpty(syncAppointmentsGoogleFolder) && !syncAppointmentsGoogleFolder.Equals(oldSyncAppointmentsGoogleFolder) && btSyncAppointments.Checked;
if (syncContacts || (syncAppointments && !syncGoogleAppointments) || (!syncAppointments && syncGoogleAppointments))
{
var response = ShowDialog("One or more Outlook or Google folder(s) changed, do you want to reset the matches first?");
switch (response)
{
case DialogResult.Yes:
var r = await ResetMatches(syncContacts,syncAppointments||syncGoogleAppointments);
if (!r)
{
throw new Exception("Reset required but cancelled by user");
}
break;
case DialogResult.Cancel:
return;
//default: //or DialogResult.No: Continue without Reset matches
}
}
//Then save the Contacts Folders used at last sync
if (btSyncContacts.Checked)
{
regKeyAppRoot.SetValue(RegistrySyncContactsFolder, syncContactsFolder);
}
if (btSyncAppointments.Checked)
{
regKeyAppRoot.SetValue(RegistrySyncAppointmentsFolder, syncAppointmentsFolder);
if (string.IsNullOrEmpty(syncAppointmentsGoogleFolder) && !string.IsNullOrEmpty(oldSyncAppointmentsGoogleFolder))
{
syncAppointmentsGoogleFolder = oldSyncAppointmentsGoogleFolder;
}
if (!string.IsNullOrEmpty(syncAppointmentsGoogleFolder))
{
regKeyAppRoot.SetValue(RegistrySyncAppointmentsGoogleFolder, syncAppointmentsGoogleFolder);
}
}
SetLastSyncText("Syncing...");
notifyIcon.Text = Application.ProductName + "\nSyncing...";
IconTimerSwitch(true);
SetFormEnabled(false);
if (sync == null)
{
sync = new Synchronizer();
sync.DuplicatesFound += new Synchronizer.DuplicatesFoundHandler(OnDuplicatesFound);
sync.ErrorEncountered += new Synchronizer.ErrorNotificationHandler(OnErrorEncountered);
sync.TimeZoneChanges += new Synchronizer.TimeZoneNotificationHandler(OnTimeZoneChanges);
}
/* Log Logger.ClearLog(); */
SetSyncConsoleText("");
Log.Information("Sync started (" + SyncProfile + ").");
/*SetSyncConsoleText(Logger.GetText());*/
Synchronizer.SyncProfile = SyncProfile;
Synchronizer.SyncContactsFolder = syncContactsFolder;
Synchronizer.SyncAppointmentsFolder = syncAppointmentsFolder;
Synchronizer.SyncAppointmentsGoogleFolder = syncAppointmentsGoogleFolder;
Synchronizer.RestrictMonthsInPast = btMonthsPast.Checked;
Synchronizer.MonthsInPast = Convert.ToUInt16(pastMonthInterval.Value);
Synchronizer.RestrictMonthsInFuture = btMonthsFuture.Checked;
Synchronizer.MonthsInFuture = Convert.ToInt16(futureMonthInterval.Value);
Synchronizer.SyncAppointmentsPrivate = btSyncAppointmentsPrivate.Checked;
Synchronizer.SyncReminders = SyncRemindersCheckBox.Checked;
Synchronizer.IncludePastReminders = IncludePastRemindersCheckBox.Checked;
Synchronizer.Timezone = Timezone;
sync.SyncOption = syncOption;
sync.SyncDelete = btSyncDelete.Checked;
sync.PromptDelete = btPromptDelete.Checked && btSyncDelete.Checked;
sync.UseFileAs = chkUseFileAs.Checked;
sync.SyncContacts = btSyncContacts.Checked;
sync.SyncAppointments = btSyncAppointments.Checked;
Synchronizer.SyncAppointmentsForceRTF = btSyncAppointmentsForceRTF.Checked;
Synchronizer.SyncContactsForceRTF = btSyncContactsForceRTF.Checked;
Synchronizer.SyncPhotos = SyncPhotosCheckBox.Checked;
if (!sync.SyncContacts && !sync.SyncAppointments)
{
SetLastSyncText("Sync failed.");
notifyIcon.Text = Application.ProductName + "\nSync failed";
var messageText = "Neither contacts nor appointments are switched on for syncing. Please choose at least one option. Sync aborted!";
Log.Error(messageText);
ShowForm();
ShowBalloonToolTip("Error", messageText, ToolTipIcon.Error, 5000, true);
return;
}
sync.LoginToGoogle(UserName.Text);
sync.LoginToOutlook();
sync.Sync();
lastSync = DateTime.Now;
SetLastSyncText("Last synced at " + lastSync.ToString());
var message = $"Sync complete. Synced: {sync.SyncedCount} out of {sync.TotalCount}. Deleted: {sync.DeletedCount}. Skipped: {sync.SkippedCount}. Errors: {sync.ErrorCount}.";
Log.Information(message);
if (reportSyncResultCheckBox.Checked)
{
var icon = sync.ErrorCount > 0 ? ToolTipIcon.Error : sync.SkippedCount > 0 ? ToolTipIcon.Warning : ToolTipIcon.Info;
ShowBalloonToolTip(Application.ProductName,
$"{DateTime.Now}. {message}",
icon,
5000, false);
}
var s = DateTime.Now.ToString("dd.MM. HH:mm");
var toolTip = $"{Application.ProductName}\nLast sync: {s}";
if (sync.ErrorCount + sync.SkippedCount > 0)
{
toolTip += $"\nWarnings: {sync.ErrorCount + sync.SkippedCount}.";
}
if (toolTip.Length >= 64)
{
toolTip = toolTip.Substring(0, 63);
}
notifyIcon.Text = toolTip;
}
}
catch (Google.GoogleApiException ex)
{
SetLastSyncText("Sync failed.");
notifyIcon.Text = Application.ProductName + "\nSync failed";
if (ex.InnerException is WebException)
{
var message = "Cannot connect to Google, please check for available internet connection and proxy settings if applicable: " + ex.Message + "\r\n" + ex.InnerException.Message + "\r\n" + ex.HelpLink;
Log.Warning(message);
ShowBalloonToolTip("Error", message, ToolTipIcon.Error, 5000, true);
}
else
{
ErrorHandler.Handle(ex);
}
}
//catch (Google.GData.Client.InvalidCredentialsException) //ToDo: Check counterpart in new Google Api
//{
// SetLastSyncText("Sync failed.");
// notifyIcon.Text = Application.ProductName + "\nSync failed";
// var message = "The credentials (Google Account username and/or password) are invalid, please correct them in the settings form before you sync again";
// Log.Error(message);
// ShowForm();
// ShowBalloonToolTip("Error", message, ToolTipIcon.Error, 5000, true);
//}
catch (Exception ex)
{
SetLastSyncText("Sync failed.");
notifyIcon.Text = Application.ProductName + "\nSync failed";
Log.Debug(ex, "Exception");
if (ex is COMException)
{
var message = "Outlook exception, please assure that Outlook is running and not closed when syncing";
Log.Warning(message + ": " + ex.Message + "\r\n" + ex.StackTrace);
ShowBalloonToolTip("Error", message, ToolTipIcon.Error, 5000, true);
}
else
{
ErrorHandler.Handle(ex);
}
}
finally
{
if (won)
{
Interlocked.Exchange(ref executing, 0);
lastSync = DateTime.Now;
TimerSwitch(true);
SetFormEnabled(true);
if (sync != null)
{
sync.LogoffOutlook();
sync.LogoffGoogle();
sync = null;
}
IconTimerSwitch(false);
}
}
}
public void ShowBalloonToolTip(string title, string message, ToolTipIcon icon, int timeout, bool error)
{
//if user is active on workstation
if (boolShowBalloonTip)
{
notifyIcon.BalloonTipTitle = title;
notifyIcon.BalloonTipText = message;
notifyIcon.BalloonTipIcon = icon;
notifyIcon.ShowBalloonTip(timeout);
}
var iconText = title + ": " + message;
if (!string.IsNullOrEmpty(iconText))
{
notifyIcon.Text = iconText.Substring(0, iconText.Length >= 63 ? 63 : iconText.Length);
}
if (error)
{
notifyIcon.Icon = IconError;
}
}
private void LogUpdatedHandler(string Message)
{
AppendSyncConsoleText(Message);
}
private void OnErrorEncountered(string title, Exception ex)
{
// do not show ErrorHandler, as there may be multiple exceptions that would nag the user
Log.Error(ex.Message);
Log.Debug(ex, "Exception");
var message = $"Error Saving Person: {ex.Message}.\nPlease report complete ErrorMessage from Log to the Tracker\nat https://sourceforge.net/tracker/?group_id=369321";
ShowBalloonToolTip(title, message, ToolTipIcon.Error, 5000, true);
}
private void OnTimeZoneChanges(string timeZone)
{
if (appointmentTimezonesComboBox.InvokeRequired)
{
var d = new OnTimeZoneChangesCallback(OnTimeZoneChanges);
Invoke(d, new object[] { timeZone });
}
else
{
appointmentTimezonesComboBox.Text = timeZone;
}
Timezone = timeZone;
Synchronizer.Timezone = timeZone;
}
private void OnDuplicatesFound(string title, string message)
{
Log.Warning(message);
ShowBalloonToolTip(title, message, ToolTipIcon.Warning, 5000, true);
}
private void OnNotificationReceived(string message)
{
SetLastSyncText(message);
}
public void SetFormEnabled(bool enabled)
{
if (InvokeRequired)
{
var h = new SwitchHandler(SetFormEnabled);
Invoke(h, new object[] { enabled });
}
else
{
resetMatchesLinkLabel.Enabled = enabled;
settingsGroupBox.Enabled = enabled;
syncButton.Enabled = enabled;
cancelButton.Enabled = !enabled;
}
}
public void SetLastSyncText(string text)
{
if (InvokeRequired)
{
var h = new TextHandler(SetLastSyncText);
Invoke(h, new object[] { text });
}
else
{
lastSyncLabel.Text = text;
}
}
public void SetSyncConsoleText(string text)
{
if (InvokeRequired)
{
var h = new TextHandler(SetSyncConsoleText);
Invoke(h, new object[] { text });
}
else
{
syncConsole.Text = text;
//Scroll to bottom to always see the last log entry
syncConsole.SelectionStart = syncConsole.TextLength;
syncConsole.ScrollToCaret();
}
}
public void AppendSyncConsoleText(string text)
{
if (InvokeRequired)
{
var h = new TextHandler(AppendSyncConsoleText);
Invoke(h, new object[] { text });
}
else
{
syncConsole.Text += text;
//Scroll to bottom to always see the last log entry
syncConsole.SelectionStart = syncConsole.TextLength;
syncConsole.ScrollToCaret();
}
}
public void TimerSwitch(bool value)
{
if (InvokeRequired)
{
var h = new SwitchHandler(TimerSwitch);
Invoke(h, new object[] { value });
}
else
{
//If PC resumes or unlocks or is started, give him 5 minutes to recover everything before the sync starts
if (lastSync < DateTime.Now.AddMinutes(WaitingMinutesBeforeSync) - new TimeSpan((int)autoSyncInterval.Value, 0, 0))
lastSync = DateTime.Now.AddMinutes(WaitingMinutesBeforeSync) - new TimeSpan((int)autoSyncInterval.Value, 0, 0);
var v = autoSyncCheckBox.Checked && value;
autoSyncInterval.Enabled = v;
syncTimer.Enabled = v;
btSyncOnlyIdle.Enabled = v;
nextSyncLabel.Visible = v;
}
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
//System shutdown
case NativeMethods.WM_QUERYENDSESSION:
requestClose = true;
break;
default:
break;
}
//Show Window from Tray
if (m.Msg == NativeMethods.WM_GCSM_SHOWME)
{
Log.Debug("WM_GCSM_SHOWME");
ShowForm();
}
base.WndProc(ref m);
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!requestClose)
{
SaveSettings();
e.Cancel = true;
}
HideForm();
}
private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
{
try
{
cancellationTokenSource.Cancel();
if (sync != null)
{
sync.LogoffOutlook();
}
Log.Information("Closed application.");
Log.CloseAndFlush();
SaveSettings();
//unregister event handler
SystemEvents.SessionSwitch -= SystemEvents_SessionSwitch;
SystemEvents.PowerModeChanged -= SystemEvents_PowerModeSwitch;
Program.DisableLogHandler(LogUpdatedHandler);
notifyIcon.Dispose();
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}
private void SyncOptionBox_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Application.DoEvents();
var index = syncOptionBox.SelectedIndex;
if (index == -1)
{
return;
}
SetSyncOption(index);
}
catch (Exception ex)
{
TimerSwitch(false);
ShowForm();
ErrorHandler.Handle(ex);
}
}
private void SetSyncOption(int index)
{
syncOption = (SyncOption)index;
for (var i = 0; i < syncOptionBox.Items.Count; i++)
{
if (i == index)
{
syncOptionBox.SetItemCheckState(i, CheckState.Checked);
}
else
{
syncOptionBox.SetItemCheckState(i, CheckState.Unchecked);
}
}
}
private void SettingsForm_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
Hide();
}
}
private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowForm();
}
private void AutoSyncCheckBox_CheckedChanged(object sender, EventArgs e)
{
lastSync = DateTime.Now.AddMinutes(WaitingMinutesBeforeSync) - new TimeSpan((int)autoSyncInterval.Value, 0, 0);
TimerSwitch(true);
}
private void SyncTimer_Tick(object sender, EventArgs e)
{
var syncTime = DateTime.Now - lastSync;
var limit = new TimeSpan((int)autoSyncInterval.Value, 0, 0);
string str = "Next sync in";
TimeSpan diff = TimeSpan.Zero;
TimeSpan diff2 = TimeSpan.Zero;
if (btSyncOnlyIdle.Checked && RetrieveIdleTime() < TimeSpan.FromMinutes(WaitingMinutesBeforeSync))
//only sync, if idleTime longer than 5 minutes
{
diff = TimeSpan.FromMinutes(WaitingMinutesBeforeSync).Subtract(RetrieveIdleTime());
}
if (syncTime < limit)
{
diff2 = limit.Subtract(syncTime);
}
if (diff2 > diff)
diff = diff2; //use the biggest difference to countdown
if (diff != TimeSpan.Zero)
{
if (diff.Hours != 0)
str += " " + diff.Hours + " h";
if (diff.Minutes != 0 || diff.Hours != 0)
str += " " + diff.Minutes + " min";
if (diff.Seconds != 0)
str += " " + diff.Seconds + " s";
nextSyncLabel.Text = str;
}
else
{
Sync();
}
}
private async void ResetMatchesLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
// force deactivation to show up
Application.DoEvents();
try
{
cancelButton.Enabled = false; //Cancel is only working for sync currently, not for reset
await ResetMatches(btSyncContacts.Checked, btSyncAppointments.Checked);
}
catch (Exception ex)
{
SetLastSyncText("Reset Matches failed");
Log.Error("Reset Matches failed");
ErrorHandler.Handle(ex);
}
finally
{
lastSync = DateTime.Now;
TimerSwitch(true);
SetFormEnabled(true);
hideButton.Enabled = true;
if (sync != null)
{
sync.LogoffOutlook();
sync.LogoffGoogle();
sync = null;
}
}
}
private async Task<bool> ResetMatches(bool syncContacts, bool syncAppointments)
{
TimerSwitch(false);
SetLastSyncText("Resetting matches...");
notifyIcon.Text = Application.ProductName + "\nResetting matches...";
SetFormEnabled(false);
if (sync == null)
{
sync = new Synchronizer();
}
/* Log Logger.ClearLog(); */
SetSyncConsoleText("");
Log.Information("Reset Matches started (" + SyncProfile + ").");
sync.SyncContacts = syncContacts;
sync.SyncAppointments = syncAppointments;
Synchronizer.SyncContactsFolder = syncContactsFolder;
Synchronizer.SyncAppointmentsFolder = syncAppointmentsFolder;
Synchronizer.SyncAppointmentsGoogleFolder = syncAppointmentsGoogleFolder;
Synchronizer.SyncProfile = SyncProfile;
sync.LoginToGoogle(UserName.Text);
sync.LoginToOutlook();
if (sync.SyncAppointments)
{
bool deleteOutlookAppointments;
switch (ShowDialog("Do you want to delete all Outlook Calendar entries?"))
{
case DialogResult.Yes: deleteOutlookAppointments = true; break;
case DialogResult.No: deleteOutlookAppointments = false; break;
default: return false;
}
bool deleteGoogleAppointments;
switch (ShowDialog("Do you want to delete all Google Calendar entries?"))
{
case DialogResult.Yes: deleteGoogleAppointments = true; break;
case DialogResult.No: deleteGoogleAppointments = false; break;
default: return false;
}
Log.Information("Resetting Google appointment matches...");
try
{
await sync.ResetGoogleAppointmentMatches(deleteGoogleAppointments, cancellationTokenSource.Token);
sync.LoadAppointments();
sync.ResetOutlookAppointmentMatches(deleteOutlookAppointments);
}
catch (TaskCanceledException)
{
Log.Information("Task cancelled by user.");
sync.LoadAppointments();
}
}
if (sync.SyncContacts)
{
sync.LoadContacts();
sync.ResetContactMatches();
}
lastSync = DateTime.Now;
SetLastSyncText("Matches reset at " + lastSync.ToString());
Log.Information("Matches reset.");
return true;
}
public delegate DialogResult InvokeConflict(ConflictResolverForm conflictResolverForm);
public DialogResult ShowConflictDialog(ConflictResolverForm conflictResolverForm)
{
if (InvokeRequired)
{
return (DialogResult)Invoke(new InvokeConflict(ShowConflictDialog), new object[] { conflictResolverForm });
}
else
{
var res = conflictResolverForm.ShowDialog(this);
notifyIcon.Icon = Icon0;
return res;
}
}
public delegate DialogResult InvokeDeleteTooManyPropertiesForm(DeleteTooManyPropertiesForm form);
public DialogResult ShowDeleteTooManyPropertiesForm(DeleteTooManyPropertiesForm form)
{
return InvokeRequired
? (DialogResult)Invoke(new InvokeDeleteTooManyPropertiesForm(ShowDeleteTooManyPropertiesForm), new object[] { form })
: form.ShowDialog(this);
}
public delegate DialogResult InvokeDeleteTooBigPropertiesForm(DeleteTooBigPropertiesForm form);
public DialogResult ShowDeleteTooBigPropertiesForm(DeleteTooBigPropertiesForm form)
{
return InvokeRequired
? (DialogResult)Invoke(new InvokeDeleteTooBigPropertiesForm(ShowDeleteTooBigPropertiesForm), new object[] { form })
: form.ShowDialog(this);
}
public delegate DialogResult InvokeDeleteDuplicatedPropertiesForm(DeleteDuplicatedPropertiesForm form);
public DialogResult ShowDeleteDuplicatedPropertiesForm(DeleteDuplicatedPropertiesForm form)
{
return InvokeRequired
? (DialogResult)Invoke(new InvokeDeleteDuplicatedPropertiesForm(ShowDeleteDuplicatedPropertiesForm), new object[] { form })
: form.ShowDialog(this);
}
private delegate void InvokeCallback();
private void ShowForm()
{
if (InvokeRequired)
{
Invoke(new InvokeCallback(ShowForm));
}
else
{
var oldState = WindowState;
Show();
Activate();
WindowState = FormWindowState.Normal;
using (var filter = new OleMessageFilter())
{
try
{
FillSyncFolderItems();
}
catch (ApplicationException ex)
{
TimerSwitch(false);
Log.Error(ex.Message);
}
catch (Exception ex)
{
TimerSwitch(false);
ErrorHandler.Handle(ex);
}
}
if (oldState != WindowState)
{
Log.Debug("Check version from ShowForm, oldState: " + oldState + ", currentState: " + WindowState);
CheckVersion();
}
}
}
private async void CheckVersion()
{
if (!NewVersionLinkLabel.Visible)
{//Only check once, if new version is available
try
{
Cursor = Cursors.WaitCursor;
SuspendLayout();
//check for new version
if (NewVersionLinkLabel.LinkColor != Color.Red && await VersionInformation.IsNewVersionAvailable(cancellationTokenSource.Token))
{
NewVersionLinkLabel.Visible = true;
NewVersionLinkLabel.LinkColor = Color.Red;
NewVersionLinkLabel.Text = "New Version of GCSM available on sf.net!";
notifyIcon.BalloonTipClicked += NotifyIcon_BalloonTipClickedDownloadNewVersion;
ShowBalloonToolTip("New version available", "Click here to download", ToolTipIcon.Info, 20000, false);
}
NewVersionLinkLabel.Visible = true;
}
finally
{
Cursor = Cursors.Default;
ResumeLayout();
}
}
}
private void NotifyIcon_BalloonTipClickedDownloadNewVersion(object sender, EventArgs e)
{
Process.Start("https://sourceforge.net/projects/googlesyncmod/files/latest/download");
notifyIcon.BalloonTipClicked -= NotifyIcon_BalloonTipClickedDownloadNewVersion;
}
private void HideForm()
{
WindowState = FormWindowState.Minimized;
Hide();
}
private void ToolStripMenuItem1_Click(object sender, EventArgs e)
{
ShowForm();
Activate();
}
private void ToolStripMenuItem3_Click(object sender, EventArgs e)
{
HideForm();
}
private void ToolStripMenuItem2_Click(object sender, EventArgs e)
{
requestClose = true;
Close();
}
private void ToolStripMenuItem5_Click(object sender, EventArgs e)
{
using (var about = new AboutBox())
{
about.Show();
}
}
private void ToolStripMenuItem4_Click(object sender, EventArgs e)
{
Sync();
}
private void SettingsForm_Load(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(UserName.Text) ||
string.IsNullOrEmpty(cmbSyncProfile.Text))
{
// this is the first load, show form
ShowForm();
UserName.Focus();
ShowBalloonToolTip(Application.ProductName,
"Application started and visible in your PC's system tray, click on this balloon or the icon below to open the settings form and enter your Google credentials there.",
ToolTipIcon.Info,
5000, false);
}
else
{
HideForm();
}
}
private void RunAtStartupCheckBox_CheckedChanged(object sender, EventArgs e)
{
var regKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
try
{
var regKeyAppRoot = Registry.CurrentUser.CreateSubKey(regKey);
if (runAtStartupCheckBox.Checked)
{
// add to registry
regKeyAppRoot.SetValue("GoogleContactSync", "\"" + Application.ExecutablePath + "\"");
}
else
{
// remove from registry
regKeyAppRoot.DeleteValue("GoogleContactSync");
}
}
catch (Exception ex)
{
//if we can't write to that key, disable it...
runAtStartupCheckBox.Checked = false;
runAtStartupCheckBox.Enabled = false;
TimerSwitch(false);
ShowForm();
ErrorHandler.Handle(new Exception("Error saving 'Run program at startup' settings into Registry key '" + regKey + "' Error: " + ex.Message, ex));
}
}
private void ValidateSyncButton()
{
syncButton.Enabled = ValidUserName && ValidProfile && ValidSyncFolders;
SetBgColor(contactFoldersComboBox, ValidSyncContactFolders);
SetBgColor(btSyncContacts, ValidSyncContactFolders);
SetBgColor(appointmentFoldersComboBox, ValidSyncOutlookAppointmentFolders);
SetBgColor(appointmentGoogleFoldersComboBox, ValidSyncGoogleAppointmentFolders);
SetBgColor(btSyncAppointments, ValidSyncAppointmentFolders);
}
private void Donate_Click(object sender, EventArgs e)
{
Process.Start("https://sourceforge.net/project/project_donations.php?group_id=369321");
}
private void Donate_MouseEnter(object sender, EventArgs e)
{
Donate.BackColor = Color.LightGray;
}
private void Donate_MouseLeave(object sender, EventArgs e)
{
Donate.BackColor = Color.Transparent;
}
private void HideButton_Click(object sender, EventArgs e)
{
Close();
}
private void ProxySettingsLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (_proxy != null)
{
_proxy.ShowDialog(this);
}
}
private void SettingsForm_HelpButtonClicked(object sender, CancelEventArgs e)
{
ShowHelp();
}
private void SettingsForm_HelpRequested(object sender, HelpEventArgs hlpevent)
{
ShowHelp();
}
private static void ShowHelp()
{
// go to the page showing the help and howto instructions
Process.Start("https://googlesyncmod.sourceforge.io/");
}
private void BtSyncContacts_CheckedChanged(object sender, EventArgs e)
{
if (!btSyncContacts.Checked && !btSyncAppointments.Checked)
{
MessageBox.Show("Neither contacts nor appointments are switched on for syncing. Please choose at least one option (automatically switched on appointments for syncing now).", "No sync switched on");
btSyncAppointments.Checked = true;
}
contactFoldersComboBox.Visible = btSyncContacts.Checked;
btSyncContactsForceRTF.Visible = btSyncContacts.Checked;
SyncPhotosCheckBox.Visible = btSyncContacts.Checked;
ValidateSyncButton();
}
private void BtSyncAppointments_CheckedChanged(object sender, EventArgs e)
{
if (!btSyncContacts.Checked && !btSyncAppointments.Checked)
{
MessageBox.Show("Neither contacts nor appointments are switched on for syncing. Please choose at least one option (automatically switched on contacts for syncing now).", "No sync switched on");
btSyncContacts.Checked = true;
}
appointmentFoldersComboBox.Visible = appointmentGoogleFoldersComboBox.Visible = btSyncAppointments.Checked;
labelTimezone.Visible = btMonthsPast.Visible = btMonthsFuture.Visible = btSyncAppointments.Checked;
appointmentTimezonesComboBox.Visible = btSyncAppointments.Checked;
pastMonthInterval.Visible = btSyncAppointments.Checked && btMonthsPast.Checked;
futureMonthInterval.Visible = btSyncAppointments.Checked && btMonthsFuture.Checked;
btSyncAppointmentsForceRTF.Visible = btSyncAppointments.Checked;
btSyncAppointmentsPrivate.Visible = SyncRemindersCheckBox.Visible = btSyncAppointments.Checked;
IncludePastRemindersCheckBox.Visible = btSyncAppointments.Checked && SyncRemindersCheckBox.Checked;
ValidateSyncButton();
}
private void SyncReminders_CheckedChanged(object sender, EventArgs e)
{
IncludePastRemindersCheckBox.Visible = btSyncAppointments.Checked && SyncRemindersCheckBox.Checked;
}
private void CmbSyncProfile_SelectedIndexChanged(object sender, EventArgs e)
{
var comboBox = (ComboBox)sender;
if ((0 == comboBox.SelectedIndex) || (comboBox.SelectedIndex == (comboBox.Items.Count - 1)))
{
using (var _configs = new ConfigurationManagerForm())
{
if (0 == comboBox.SelectedIndex && _configs != null)
{
SyncProfile = ConfigurationManagerForm.AddProfile();
ClearSettings();
}
if (comboBox.SelectedIndex == (comboBox.Items.Count - 1) && _configs != null)
{
_configs.CurrentSyncProfile = SyncProfile;
_configs.Synchronizer = sync;
_configs.ShowDialog(this);
}
}
FillSyncProfileItems();
comboBox.Text = SyncProfile;
SaveSettings();
}
if (comboBox.SelectedIndex < 0)
{
MessageBox.Show("Please select Sync Profile.", "No sync switched on");
}
else
{
if (cmbSyncProfile_PreviouslySelectedIndex != -1)
{
SaveSettings(comboBox.Items[cmbSyncProfile_PreviouslySelectedIndex].ToString());
}
LoadSettings(comboBox.Text);
LoadSettingsFolders(comboBox.Text);
SyncProfile = comboBox.Text;
cmbSyncProfile_PreviouslySelectedIndex = comboBox.SelectedIndex;
}
ValidateSyncButton();
}
private void ContacFoldersComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
folderComboBox_SelectedIndexChanged(sender, ref syncContactsFolder, "Outlook Contacts");
}
private void AppointmentFoldersComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
folderComboBox_SelectedIndexChanged(sender, ref syncAppointmentsFolder, "Outlook Appointments");
}
private void AppointmentGoogleFoldersComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var message = "Select the Google Calendar you want to sync";
var comboBox = sender as ComboBox;
if (comboBox.SelectedIndex >= 0 && comboBox.SelectedIndex < comboBox.Items.Count && comboBox.SelectedItem is GoogleCalendar calendar)
{
syncAppointmentsGoogleFolder = comboBox.SelectedValue.ToString();
toolTip.SetToolTip(comboBox, message + ":\r\n" + calendar.DisplayName);
}
else
{
syncAppointmentsGoogleFolder = "";
toolTip.SetToolTip(comboBox, message);
}
ValidateSyncButton();
}
private void folderComboBox_SelectedIndexChanged(object sender, ref string folder, string content)
{
string message = $"Select the {content} folder you want to sync to";
var comboBox = sender as ComboBox;
if (comboBox.SelectedIndex >= 0 && comboBox.SelectedIndex < comboBox.Items.Count && comboBox.SelectedItem is OutlookFolder)
{
folder = (sender as ComboBox).SelectedValue.ToString();
toolTip.SetToolTip((sender as ComboBox), message + ":\r\n" + ((OutlookFolder)(sender as ComboBox).SelectedItem).DisplayName);
}
else
{
folder = "";
toolTip.SetToolTip((sender as ComboBox), message);
}
ValidateSyncButton();
}
private void BtSyncDelete_CheckedChanged(object sender, EventArgs e)
{
btPromptDelete.Visible = btSyncDelete.Checked;
btPromptDelete.Checked = btSyncDelete.Checked;
}
private void PictureBoxExit_Click(object sender, EventArgs e)
{
if (DialogResult.Yes == MessageBox.Show("Do you really want to exit " + Application.ProductName + "? This will also stop the service performing automatic synchronizaton in the background. If you only want to hide the settings form, use the 'Hide' Button instead.", "Exit " + Application.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
CancelButton_Click(sender, EventArgs.Empty); //Close running thread
requestClose = true;
Close();
}
}
private void SystemEvents_PowerModeSwitch(object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Suspend)
{
TimerSwitch(false);
}
else if (e.Mode == PowerModes.Resume)
{
TimerSwitch(true);
}
}
private void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
if (e.Reason == SessionSwitchReason.SessionLock)
{
boolShowBalloonTip = false;
}
else if (e.Reason == SessionSwitchReason.SessionUnlock)
{
boolShowBalloonTip = true;
TimerSwitch(true);
}
}
private void AutoSyncInterval_ValueChanged(object sender, EventArgs e)
{
TimerSwitch(true);
}
private void CancelButton_Click(object sender, EventArgs e)
{
cancellationTokenSource.Cancel();
KillSyncThread();
}
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, ControlThread = true)]
private void KillSyncThread()
{
if (syncThread != null && syncThread.IsAlive)
{
syncThread.Abort();
}
}
#region syncing icon
public void IconTimerSwitch(bool value)
{
if (InvokeRequired)
{
var h = new SwitchHandler(IconTimerSwitch);
Invoke(h, new object[] { value });
}
else
{
if (value) //Reset Icon to default icon as starting point for the syncing icon
{
notifyIcon.Icon = Icon0;
}
iconTimer.Enabled = value;
}
}
private void IconTimer_Tick(object sender, EventArgs e)
{
ShowNextIcon();
}
private void ShowNextIcon()
{
if (InvokeRequired)
{
var h = new IconHandler(ShowNextIcon);
Invoke(h, new object[] { });
}
else
{
notifyIcon.Icon = GetNextIcon(notifyIcon.Icon);
};
}
private Icon GetNextIcon(Icon currentIcon)
{
#pragma warning disable IDE0046 // Convert to conditional expression
if (currentIcon == IconError) //Don't change the icon anymore, once an error occurred
{
return IconError;
}
if (currentIcon == Icon30)
{
return Icon60;
}
if (currentIcon == Icon60)
{
return Icon90;
}
if (currentIcon == Icon90)
{
return Icon120;
}
if (currentIcon == Icon120)
{
return Icon150;
}
if (currentIcon == Icon150)
{
return Icon180;
}
if (currentIcon == Icon180)
{
return Icon210;
}
if (currentIcon == Icon210)
{
return Icon240;
}
if (currentIcon == Icon240)
{
return Icon270;
}
if (currentIcon == Icon270)
{
return Icon300;
}
if (currentIcon == Icon300)
{
return Icon330;
}
if (currentIcon == Icon330)
{
return Icon0;
}
return Icon30;
#pragma warning restore IDE0046 // Convert to conditional expression
}
#endregion
private void AppointmentTimezonesComboBox_TextChanged(object sender, EventArgs e)
{
Timezone = appointmentTimezonesComboBox.Text;
}
private void LinkLabelRevokeAuthentication_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
RevokeAuthentication();
}
public static void RevokeAuthentication()
{
try
{
Log.Information("Trying to remove Authentication...");
var Folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\GoContactSyncMOD\\";
var AuthFolder = Folder + "\\Auth\\";
var fDS = new FileDataStore(AuthFolder, true);
fDS.ClearAsync();
Log.Information("Removed Authentication...");
}
catch (Exception ex)
{
Log.Error($"Error revoking Google Authentication: {ex.Message}");
Log.Debug(ex, "Exception");
}
}
private void LoadAppointmentGoogleFoldersComboBox()
{
Log.Debug("Loading Google Appointments folders...");
var googleAppointmentFolders = new ArrayList();
appointmentGoogleFoldersComboBox.BeginUpdate();
var defaultText = " --- Select a Google Appointment folder ---";
if (sync == null)
{
sync = new Synchronizer();
}
sync.SyncAppointments = btSyncAppointments.Checked;
sync.LoginToGoogle(UserName.Text);
if (sync.CalendarList != null)
{
foreach (var calendar in sync.CalendarList)
{
googleAppointmentFolders.Add(new GoogleCalendar(calendar.Summary, calendar.Id, calendar.Primary ?? false));
}
}
if (googleAppointmentFolders != null)
{
googleAppointmentFolders.Sort();
googleAppointmentFolders.Insert(0, new GoogleCalendar(defaultText, defaultText, false));
appointmentGoogleFoldersComboBox.DataSource = googleAppointmentFolders;
appointmentGoogleFoldersComboBox.DisplayMember = "DisplayName";
appointmentGoogleFoldersComboBox.ValueMember = "FolderID";
}
appointmentGoogleFoldersComboBox.EndUpdate();
appointmentGoogleFoldersComboBox.SelectedValue = defaultText;
//Select Default Folder per Default
foreach (GoogleCalendar folder in appointmentGoogleFoldersComboBox.Items)
{
if (folder.IsDefaultFolder)
{
appointmentGoogleFoldersComboBox.SelectedValue = folder.FolderID;
break;
}
}
Log.Debug("Loaded Google Appointments folders");
}
private void AppointmentGoogleFoldersComboBox_Enter(object sender, EventArgs e)
{
if (appointmentGoogleFoldersComboBox.DataSource == null ||
appointmentGoogleFoldersComboBox.Items.Count <= 1)
{
LoadAppointmentGoogleFoldersComboBox();
}
}
private void AutoSyncInterval_Enter(object sender, EventArgs e)
{
syncTimer.Enabled = false;
}
private void AutoSyncInterval_Leave(object sender, EventArgs e)
{
syncTimer.Enabled = true;
}
private void NewVersionLinkLabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (((LinkLabel)sender).LinkColor == Color.Red)
{
Log.Debug("Process Start for https://sourceforge.net/projects/googlesyncmod/files/latest/download");
Process.Start("https://sourceforge.net/projects/googlesyncmod/files/latest/download");
}
else
{
Log.Debug("Process Start for https://sourceforge.net/projects/googlesyncmod/");
Process.Start("https://sourceforge.net/projects/googlesyncmod/");
}
}
private void UserName_Validating(object sender, CancelEventArgs e)
{
var isValid = ValidUserName;//Regex.IsMatch(UserName.Text, @"^(?'id'[a-z0-9\'\%\._\+\-]+)@(?'domain'[a-z0-9\'\%\._\+\-]+)\.(?'ext'[a-z]{2,6})$", RegexOptions.IgnoreCase);
//SetBgColor(UserName, isValid);
syncButton.Enabled = isValid;
if (isValid)
{
//toolTip.SetToolTip(UserName, string.Empty);
}
else
{
//toolTip.SetToolTip(UserName, "User is of wrong format, should be full Google Mail address, e.g. user@gmail.com");
Log.Warning("User is of wrong format, should be full Google Mail address, e.g. user@gmail.com");
e.Cancel = true;
}
}
private void UserName_Validated(object sender, EventArgs e)
{
var _profile = cmbSyncProfile.Text;
if (!string.IsNullOrEmpty(_profile))
{
if (prevUserName != UserName.Text)
{
prevUserName = UserName.Text;
LoadAppointmentGoogleFoldersComboBox();
}
}
}
private void btMonthsPast_CheckedChanged(object sender, EventArgs e)
{
pastMonthInterval.Visible = btMonthsPast.Checked;
futureMonthInterval.Visible = btMonthsFuture.Checked;
}
private void btMonthsFuture_CheckedChanged(object sender, EventArgs e)
{
pastMonthInterval.Visible = btMonthsPast.Checked;
futureMonthInterval.Visible = btMonthsFuture.Checked;
}
private void futureMonthInterval_ValueChanged(object sender, EventArgs e)
{
//if negative futureMonthInterval selected (i.e. sync not until present), the pastMonthInterval must be more in the past
if (futureMonthInterval.Value < 0 && futureMonthInterval.Value < -pastMonthInterval.Value)
pastMonthInterval.Value = -futureMonthInterval.Value;
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.