Menu

[r1147]: / trunk / src / HiddenWindow.cpp  Maximize  Restore  History

Download this file

1509 lines (1429 with data), 68.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
// CommitMonitor - simple checker for new commits in svn repositories
// Copyright (C) 2007-2017 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "stdafx.h"
#include <fstream>
#include <sstream>
#include <Urlmon.h>
#pragma comment(lib, "Urlmon.lib")
#include "HiddenWindow.h"
#include "resource.h"
#include "MainDlg.h"
#include "SCCS.h"
#include "SVN.h"
#include "Accurev.h"
#include "Callback.h"
#include "AppUtils.h"
#include "StatusBarMsgWnd.h"
#include "OptionsDlg.h"
#include "version.h"
#include "SnarlInterface.h"
#include <cctype>
#include <regex>
#include <set>
// for Vista
#define MSGFLT_ADD 1
CHiddenWindow *hiddenWindowPointer = NULL;
DWORD WINAPI MonitorThread(LPVOID lpParam);
CHiddenWindow::PFNCHANGEWINDOWMESSAGEFILTER CHiddenWindow::m_pChangeWindowMessageFilter = NULL;
CHiddenWindow::CHiddenWindow(HINSTANCE hInst, const WNDCLASSEX* wcx /* = NULL*/)
: CWindow(hInst, wcx)
, m_ThreadRunning(0)
, m_bRun(true)
, m_hMonitorThread(NULL)
, m_bMainDlgShown(false)
, m_bMainDlgRemovedItems(false)
, m_hMainDlg(NULL)
, m_nIcon(0)
, regShowTaskbarIcon(_T("Software\\CommitMonitor\\TaskBarIcon"), TRUE)
, m_regLastSelectedProject(_T("Software\\CommitMonitor\\LastSelectedProject"))
, m_bIsTask(false)
, m_bNewerVersionAvailable(false)
, COMMITMONITOR_SHOWDLGMSG(0)
, WM_TASKBARCREATED(0)
, snarlGlobalMsg(0)
{
m_hIconNew0 = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTIFYNEW0));
m_hIconNew1 = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTIFYNEW1));
m_hIconNew2 = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTIFYNEW2));
m_hIconNew3 = LoadIcon(hInst, MAKEINTRESOURCE(IDI_NOTIFYNEW3));
m_hIconNormal = LoadIcon(hInst, MAKEINTRESOURCE(IDI_COMMITMONITOR));
SecureZeroMemory(&m_SystemTray, sizeof(m_SystemTray));
hiddenWindowPointer = this;
}
CHiddenWindow::~CHiddenWindow(void)
{
DestroyIcon(m_hIconNew0);
DestroyIcon(m_hIconNew1);
DestroyIcon(m_hIconNew2);
DestroyIcon(m_hIconNew3);
DestroyIcon(m_hIconNormal);
Shell_NotifyIcon(NIM_DELETE, &m_SystemTray);
}
void CHiddenWindow::RemoveTrayIcon()
{
Shell_NotifyIcon(NIM_DELETE, &m_SystemTray);
}
bool CHiddenWindow::RegisterAndCreateWindow()
{
WNDCLASSEX wcx;
// Fill in the window class structure with default parameters
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = CWindow::stWinMsgHandler;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hResource;
wcx.hCursor = LoadCursor(NULL, IDC_SIZEWE);
ResString clsname(hResource, IDS_APP_TITLE);
wcx.lpszClassName = clsname;
wcx.hIcon = LoadIcon(hResource, MAKEINTRESOURCE(IDI_COMMITMONITOR));
wcx.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wcx.lpszMenuName = MAKEINTRESOURCE(IDC_COMMITMONITOR);
wcx.hIconSm = LoadIcon(wcx.hInstance, MAKEINTRESOURCE(IDI_COMMITMONITOR));
if (RegisterWindow(&wcx))
{
if (Create(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, NULL))
{
COMMITMONITOR_SHOWDLGMSG = RegisterWindowMessage(_T("CommitMonitor_ShowDlgMsg"));
WM_TASKBARCREATED = RegisterWindowMessage(_T("TaskbarCreated"));
// On Vista, the message TasbarCreated may be blocked by the message filter.
// We try to change the filter here to get this message through. If even that
// fails, then we can't do much about it and the task bar icon won't show up again.
HMODULE hLib = LoadLibrary(_T("user32.dll"));
if (hLib)
{
m_pChangeWindowMessageFilter = (CHiddenWindow::PFNCHANGEWINDOWMESSAGEFILTER)GetProcAddress(hLib, "ChangeWindowMessageFilter");
if (m_pChangeWindowMessageFilter)
{
(*m_pChangeWindowMessageFilter)(WM_TASKBARCREATED, MSGFLT_ADD);
}
}
ShowWindow(m_hwnd, SW_HIDE);
ShowTrayIcon(false);
m_UrlInfos.Load();
Snarl::SnarlInterface snarlIface;
snarlGlobalMsg = snarlIface.GetGlobalMsg();
if (hLib) FreeLibrary(hLib);
return true;
}
}
return false;
}
INT_PTR CHiddenWindow::ShowDialog()
{
return ::SendMessage(*this, COMMITMONITOR_SHOWDLGMSG, 0, 0);
}
LRESULT CHiddenWindow::HandleCustomMessages(HWND /*hwnd*/, UINT uMsg, WPARAM wParam, LPARAM /*lParam*/)
{
if (uMsg == COMMITMONITOR_SHOWDLGMSG)
{
if (m_bMainDlgShown)
{
// bring the dialog to front
if (m_hMainDlg == NULL)
m_hMainDlg = FindWindow(NULL, _T("Commit Monitor"));
if (!CAppUtils::IsWindowCovered(m_hMainDlg))
SendMessage(m_hMainDlg, WM_CLOSE, 0, 0);
else
{
if (IsIconic(m_hMainDlg))
ShowWindow(m_hMainDlg, SW_RESTORE);
else
SetWindowPos(m_hMainDlg, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_SHOWWINDOW);
SetForegroundWindow(m_hMainDlg);
}
return TRUE;
}
m_bMainDlgShown = true;
m_bMainDlgRemovedItems = false;
CMainDlg dlg(*this);
dlg.SetLastSelectedProject(m_regLastSelectedProject);
dlg.SetUrlInfos(&m_UrlInfos);
dlg.SetUpdateAvailable(m_bNewerVersionAvailable);
dlg.DoModal(hResource, IDD_MAINDLG, NULL, IDC_COMMITMONITOR);
m_regLastSelectedProject = dlg.GetLastSelectedProject();
m_bNewerVersionAvailable = false;
ShowTrayIcon(false);
m_hMainDlg = NULL;
m_bMainDlgShown = false;
return TRUE;
}
else if (uMsg == WM_TASKBARCREATED)
{
bool bNew = m_SystemTray.hIcon == m_hIconNew1;
m_SystemTray.hIcon = NULL;
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(" Taskbar created!\n"));
ShowTrayIcon(bNew);
}
else if (uMsg == (UINT)snarlGlobalMsg)
{
if (wParam == Snarl::SNARL_LAUNCHED)
{
Snarl::SnarlInterface snarlIface;
if ((snarlIface.GetVersionEx() != Snarl::M_FAILED)&&(Snarl::SnarlInterface::GetSnarlWindow() != NULL))
{
std::wstring imgPath = CAppUtils::GetAppDataDir()+L"\\CM.png";
if (CAppUtils::ExtractBinResource(_T("PNG"), IDB_COMMITMONITOR, imgPath))
{
// register with Snarl
snarlIface.RegisterApp(_T("CommitMonitor"), imgPath.c_str(), imgPath.c_str(), *this);
snarlIface.RegisterAlert(_T("CommitMonitor"), ALERTTYPE_NEWPROJECTS);
snarlIface.RegisterAlert(_T("CommitMonitor"), ALERTTYPE_NEWCOMMITS);
snarlIface.RegisterAlert(_T("CommitMonitor"), ALERTTYPE_FAILEDCONNECT);
return TRUE;
}
}
}
if (wParam == Snarl::SNARL_SHOW_APP_UI)
{
SendMessage(*this, COMMITMONITOR_SHOWDLGMSG, 0, 0);
return TRUE;
}
}
return 0L;
}
LRESULT CALLBACK CHiddenWindow::WinMsgHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// the custom messages are not constant, therefore we can't handle them in the
// switch-case below
HandleCustomMessages(hwnd, uMsg, wParam, lParam);
switch (uMsg)
{
case WM_CREATE:
{
m_hwnd = hwnd;
// set the timers we use to start the monitoring threads
// we wait a minute before starting the initial monitoring
// to avoid problems after booting up
// See issue #63 for details:
// https://sourceforge.net/p/commitmonitor/tickets/63/
::SetTimer(*this, IDT_MONITOR, 60000, NULL);
}
break;
case WM_TIMER:
{
switch (wParam)
{
case IDT_MONITOR:
DoTimer(false);
break;
case IDT_ANIMATE:
DoAnimate();
break;
case IDT_POPUP:
{
if (CAppUtils::IsFullscreenWindowActive())
{
// restart the timer and wait until no fullscreen app is active
SetTimer(hwnd, IDT_POPUP, 5000, NULL);
}
else
{
CStatusBarMsgWnd * popup = new CStatusBarMsgWnd(hResource);
if (m_popupData.size() == 1)
{
popup->Show(m_popupData[0].sTitle.c_str(), m_popupData[0].sText.c_str(), IDI_COMMITMONITOR, *this, COMMITMONITOR_POPUPCLICK);
}
else
{
// only show one popup for all the notifications
TCHAR sTitle[1024] = {0};
_stprintf_s(sTitle, _countof(sTitle), _T("%Iu projects have updates"), m_popupData.size());
std::wstring sText;
for (std::vector<popupData>::const_iterator it = m_popupData.begin(); it != m_popupData.end(); ++it)
{
if (!sText.empty())
sText += _T(", ");
sText += it->sProject;
}
popup->Show(sTitle, sText.c_str(), IDI_COMMITMONITOR, *this, COMMITMONITOR_POPUPCLICK);
}
m_popupData.clear();
KillTimer(hwnd, IDT_POPUP);
}
}
break;
}
}
break;
case WM_POWERBROADCAST:
{
switch (wParam)
{
case PBT_APMRESUMEAUTOMATIC:
case PBT_APMRESUMESUSPEND:
case PBT_APMRESUMECRITICAL:
// waking up again
// we wait a minute before starting the initial monitoring
// to avoid problems after booting up
// See issue #63 for details:
// https://sourceforge.net/p/commitmonitor/tickets/63/
::SetTimer(*this, IDT_MONITOR, 60000, NULL);
break;
case PBT_APMSUSPEND:
// going to sleep
::KillTimer(*this, IDT_MONITOR);
break;
}
}
break;
case COMMITMONITOR_GETALL:
if (lParam)
m_UrlToWorkOn = std::wstring((wchar_t*)lParam);
DoTimer(true);
break;
case COMMITMONITOR_POPUP:
{
popupData * pData = (popupData*)lParam;
if (pData)
{
Snarl::SnarlInterface snarlIface;
if ((snarlIface.GetVersionEx() == Snarl::M_FAILED)||(Snarl::SnarlInterface::GetSnarlWindow() == NULL))
{
if (CRegStdDWORD(L"Software\\CommitMonitor\\ShowPopups", TRUE))
{
m_popupData.push_back(*pData);
SetTimer(hwnd, IDT_POPUP, 5000, NULL);
}
}
else
{
std::wstring iconPath = CAppUtils::GetAppDataDir()+L"\\CM.png";
if (!PathFileExists(iconPath.c_str()))
iconPath = _T("");
snarlIface.ShowNotification(pData->sAlertType.c_str(), pData->sTitle.c_str(), pData->sText.c_str(), 5, iconPath.c_str(), *this, COMMITMONITOR_POPUPCLICK);
}
ShowTrayIcon(true);
}
}
break;
case COMMITMONITOR_POPUPCLICK:
if ((wParam != Snarl::SNARL_NOTIFICATION_TIMED_OUT)&&(wParam != Snarl::SNARL_NOTIFICATION_CLOSED))
ShowDialog();
break;
case COMMITMONITOR_SAVEINFO:
{
m_UrlInfos.Save(false);
return TRUE;
}
break;
case COMMITMONITOR_REMOVEDURL:
{
m_bMainDlgRemovedItems = true;
}
break;
case COMMITMONITOR_CHANGEDINFO:
ShowTrayIcon(!!wParam);
return TRUE;
case COMMITMONITOR_TASKBARCALLBACK:
{
switch (lParam)
{
case WM_MOUSEMOVE:
{
// find the number of unread items
int nNewCommits = 0;
const std::map<std::wstring, CUrlInfo> * pRead = m_UrlInfos.GetReadOnlyData();
for (auto it = pRead->cbegin(); it != pRead->cend(); ++it)
{
for (auto logit = it->second.logentries.cbegin(); logit != it->second.logentries.cend(); ++logit)
{
if (!logit->second.read)
nNewCommits++;
}
}
m_UrlInfos.ReleaseReadOnlyData();
// update the tool tip data
m_SystemTray.cbSize = sizeof(NOTIFYICONDATA);
m_SystemTray.hWnd = *this;
m_SystemTray.uFlags = NIF_MESSAGE | NIF_TIP;
m_SystemTray.uCallbackMessage = COMMITMONITOR_TASKBARCALLBACK;
if (nNewCommits)
{
if (nNewCommits == 1)
_stprintf_s(m_SystemTray.szTip, _countof(m_SystemTray.szTip), _T("CommitMonitor - %d new commit"), nNewCommits);
else
_stprintf_s(m_SystemTray.szTip, _countof(m_SystemTray.szTip), _T("CommitMonitor - %d new commits"), nNewCommits);
}
else
_tcscpy_s(m_SystemTray.szTip, _countof(m_SystemTray.szTip), _T("CommitMonitor"));
if (Shell_NotifyIcon(NIM_MODIFY, &m_SystemTray) == FALSE)
{
Shell_NotifyIcon(NIM_DELETE, &m_SystemTray);
Shell_NotifyIcon(NIM_ADD, &m_SystemTray);
}
}
break;
case WM_LBUTTONDBLCLK:
{
if (CRegStdDWORD(_T("Software\\CommitMonitor\\LeftClickMenu"), FALSE) != 0)
{
// show the main dialog
ShowDialog();
}
}
break;
case WM_LBUTTONDOWN:
if (CRegStdDWORD(_T("Software\\CommitMonitor\\LeftClickMenu"), FALSE) == 0)
{
// show the main dialog
ShowDialog();
return TRUE;
}
break;
case WM_LBUTTONUP:
case NIN_KEYSELECT:
case NIN_SELECT:
case WM_RBUTTONUP:
case WM_CONTEXTMENU:
{
if (lParam == WM_LBUTTONUP)
{
if (CRegStdDWORD(_T("Software\\CommitMonitor\\LeftClickMenu"), FALSE) == 0)
{
return TRUE;
}
}
POINT pt;
GetCursorPos( &pt );
HMENU hMenu = ::LoadMenu(hResource, MAKEINTRESOURCE(IDC_COMMITMONITOR));
hMenu = ::GetSubMenu(hMenu, 0);
// set the default entry
MENUITEMINFO iinfo = {0};
iinfo.cbSize = sizeof(MENUITEMINFO);
iinfo.fMask = MIIM_STATE;
GetMenuItemInfo(hMenu, 0, MF_BYPOSITION, &iinfo);
iinfo.fState |= MFS_DEFAULT;
SetMenuItemInfo(hMenu, 0, MF_BYPOSITION, &iinfo);
// destroy all popup windows
HWND hPopup = FindWindow(L"StatusBarMsgWnd_{BAB03407-CF65-4942-A1D5-063FA1CA8530}", NULL);
while (hPopup)
{
DestroyWindow(hPopup);
hPopup = FindWindow(L"StatusBarMsgWnd_{BAB03407-CF65-4942-A1D5-063FA1CA8530}", NULL);
}
// show the menu
::SetForegroundWindow(*this);
int cmd = ::TrackPopupMenu(hMenu, TPM_RETURNCMD | TPM_LEFTALIGN | TPM_NONOTIFY , pt.x, pt.y, NULL, *this, NULL);
::PostMessage(*this, WM_NULL, 0, 0);
switch( cmd )
{
case IDM_EXIT:
Save();
::PostQuitMessage(0);
break;
case ID_POPUP_OPENCOMMITMONITOR:
ShowDialog();
break;
case ID_POPUP_CHECKNOW:
DoTimer(true);
break;
case ID_POPUP_OPTIONS:
{
COptionsDlg dlg(*this);
dlg.SetHiddenWnd(*this);
dlg.SetUrlInfos(&m_UrlInfos);
dlg.DoModal(hResource, IDD_OPTIONS, *this);
}
break;
case ID_POPUP_MARKALLASREAD:
{
std::map<std::wstring,CUrlInfo> * pWrite = m_UrlInfos.GetWriteData();
for (auto it = pWrite->begin(); it != pWrite->end(); ++it)
{
CUrlInfo * pInfo = &it->second;
for (auto logit = pInfo->logentries.begin(); logit != pInfo->logentries.end(); ++logit)
{
logit->second.read = true;
}
}
m_UrlInfos.ReleaseWriteData();
ShowTrayIcon(false);
Save();
}
break;
}
}
break;
}
return TRUE;
}
break;
case COMMITMONITOR_SETWINDOWHANDLE:
m_hMainDlg = (HWND)wParam;
break;
case COMMITMONITOR_INFOTEXT:
if (m_hMainDlg)
SendMessage(m_hMainDlg, COMMITMONITOR_INFOTEXT, 0, lParam);
break;
case WM_DESTROY:
if (!StopThread(4000))
{
TerminateProcess(GetCurrentProcess(), 0);
}
else
PostQuitMessage(0);
break;
case WM_CLOSE:
::DestroyWindow(m_hwnd);
break;
case WM_QUERYENDSESSION:
if (!StopThread(4000))
{
TerminateProcess(GetCurrentProcess(), 0);
}
else
PostQuitMessage(0);
return TRUE;
break;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
};
void CHiddenWindow::DoTimer(bool bForce)
{
CTraceToOutputDebugString::Instance()(_T(__FUNCTION__) _T(" timer fired!\n"));
// Restart the timer with 60 seconds
::SetTimer(*this, IDT_MONITOR, TIMER_ELAPSE, NULL);
if (m_ThreadRunning)
{
if (m_hMainDlg)
SendMessage(m_hMainDlg, COMMITMONITOR_INFOTEXT, 0, (LPARAM)_T("Repositories are currently being checked, please wait..."));
return;
}
// go through all url infos and check if
// we need to refresh them
if (m_UrlInfos.IsEmpty())
{
if (m_bIsTask)
::PostQuitMessage(0);
return;
}
bool bStartThread = false;
__time64_t currenttime = NULL;
_time64(&currenttime);
if (bForce)
{
// reset the 'last checked times' of all urls
std::map<std::wstring,CUrlInfo> * pInfos = m_UrlInfos.GetWriteData();
for (auto it = pInfos->begin(); it != pInfos->end(); ++it)
{
if (!m_UrlToWorkOn.empty())
{
if (it->second.url.compare(m_UrlToWorkOn) == 0)
it->second.lastchecked = 0;
}
else
it->second.lastchecked = 0;
}
m_UrlInfos.ReleaseWriteData();
}
bool bAllRead = true;
bool bHasErrors = false;
const std::map<std::wstring,CUrlInfo> * pInfos = m_UrlInfos.GetReadOnlyData();
for (auto it = pInfos->cbegin(); it != pInfos->cend(); ++it)
{
CUrlInfo inf = it->second;
if ((!it->second.error.empty())&&(!it->second.parentpath))
bHasErrors = true;
// go through the log entries and find unread items
for (auto rit = it->second.logentries.cbegin(); rit != it->second.logentries.cend(); ++rit)
{
if (!rit->second.read)
{
bAllRead = false;
break;
}
}
if ((it->second.lastchecked + (it->second.minutesinterval*60)) < currenttime)
bStartThread = true;
}
m_UrlInfos.ReleaseReadOnlyData();
if (bAllRead && !bHasErrors && DWORD(CRegStdDWORD(_T("Software\\CommitMonitor\\IndicateConnectErrors"), TRUE)))
{
// no errors (anymore) and all items are marked as read:
// stop the animated icon
KillTimer(*this, IDT_ANIMATE);
ShowTrayIcon(false);
}
if ((bStartThread)&&(m_ThreadRunning == 0))
{
// start the monitoring thread to update the infos
if (m_hMonitorThread)
{
CloseHandle(m_hMonitorThread);
m_hMonitorThread = NULL;
}
DWORD dwThreadId = 0;
m_hMonitorThread = CreateThread(
NULL, // no security attribute
0, // default stack size
MonitorThread,
(LPVOID)this, // thread parameter
0, // not suspended
&dwThreadId); // returns thread ID
if (m_hMonitorThread == NULL)
return;
}
SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);
}
void CHiddenWindow::ShowTrayIcon(bool newCommits)
{
CTraceToOutputDebugString::Instance()(_T("changing tray icon to %s\n"), (newCommits ? _T("\"new commits\"") : _T("\"normal\"")));
DWORD msg = m_SystemTray.hIcon ? NIM_MODIFY : NIM_ADD;
regShowTaskbarIcon.read();
bool bClearIcon = ((!newCommits)&&(regShowTaskbarIcon == FALSE));
if (bClearIcon)
{
msg = NIM_DELETE;
}
m_SystemTray.cbSize = sizeof(NOTIFYICONDATA);
m_SystemTray.hWnd = *this;
if (bClearIcon)
m_SystemTray.hIcon = NULL;
else
m_SystemTray.hIcon = newCommits ? m_hIconNew1 : m_hIconNormal;
m_SystemTray.uFlags = NIF_MESSAGE | NIF_ICON;
m_SystemTray.uCallbackMessage = COMMITMONITOR_TASKBARCALLBACK;
if (Shell_NotifyIcon(msg, &m_SystemTray) == FALSE)
{
if (msg == NIM_MODIFY)
{
Shell_NotifyIcon(NIM_DELETE, &m_SystemTray);
Shell_NotifyIcon(NIM_ADD, &m_SystemTray);
}
}
m_nIcon = 2;
if ((newCommits)&&(m_SystemTray.hIcon)&&(DWORD(CRegStdDWORD(_T("Software\\CommitMonitor\\Animate"), TRUE))))
SetTimer(*this, IDT_ANIMATE, TIMER_ANIMATE, NULL);
else
KillTimer(*this, IDT_ANIMATE);
}
void CHiddenWindow::DoAnimate()
{
m_SystemTray.cbSize = sizeof(NOTIFYICONDATA);
m_SystemTray.hWnd = *this;
switch (m_nIcon)
{
case 0:
m_SystemTray.hIcon = m_hIconNew0;
break;
default:
case 1:
case 5:
m_SystemTray.hIcon = m_hIconNew1;
break;
case 2:
case 4:
m_SystemTray.hIcon = m_hIconNew2;
break;
case 3:
m_SystemTray.hIcon = m_hIconNew3;
break;
}
m_SystemTray.uFlags = NIF_MESSAGE | NIF_ICON;
m_SystemTray.uCallbackMessage = COMMITMONITOR_TASKBARCALLBACK;
if (Shell_NotifyIcon(NIM_MODIFY, &m_SystemTray) == FALSE)
{
Shell_NotifyIcon(NIM_DELETE, &m_SystemTray);
Shell_NotifyIcon(NIM_ADD, &m_SystemTray);
}
m_nIcon++;
if (m_nIcon >= 6)
m_nIcon = 0;
}
DWORD CHiddenWindow::RunThread()
{
m_ThreadRunning = TRUE;
m_bRun = true;
__time64_t currenttime = NULL;
_time64(&currenttime);
bool bNewEntries = false;
bool bNeedsSaving = false;
if (::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)!=S_OK)
{
SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);
return 1;
}
// to avoid blocking access to the urls info for the whole time this thread is running,
// create a copy of it here. Copying the whole structure is relatively fast and therefore
// won't block anything for too long.
std::map<std::wstring,CUrlInfo> urlinfoReadOnly = *m_UrlInfos.GetReadOnlyData();
m_UrlInfos.ReleaseReadOnlyData();
TCHAR infotextbuf[1024];
CTraceToOutputDebugString::Instance()(_T("monitor thread started\n"));
const std::map<std::wstring,CUrlInfo> * pUrlInfoReadOnly = &urlinfoReadOnly;
std::map<std::wstring,CUrlInfo>::const_iterator it = pUrlInfoReadOnly->begin();
for (; (it != pUrlInfoReadOnly->end()) && m_bRun; ++it)
{
int mit = max(it->second.minutesinterval, it->second.minminutesinterval);
SendMessage(*this, COMMITMONITOR_INFOTEXT, 0, (LPARAM)_T(""));
if (!m_UrlToWorkOn.empty())
{
if (it->second.url.compare(m_UrlToWorkOn))
continue;
}
if (((it->second.monitored)&&((it->second.lastchecked + (mit*60)) < currenttime))||(!m_UrlToWorkOn.empty()))
{
m_UrlToWorkOn.clear();
if ((it->second.errNr == SVN_ERR_RA_NOT_AUTHORIZED)&&(!it->second.error.empty()))
continue; // don't check if the last error was 'not authorized'
CTraceToOutputDebugString::Instance()(_T("checking %s for updates\n"), it->first.c_str());
// get the highest revision of the repository
SCCS *pSCCS;
SVN svnAccess;
ACCUREV accurevAccess;
switch (it->second.sccs) {
default:
case CUrlInfo::SCCS_SVN:
pSCCS = (SCCS *)&svnAccess;
break;
case CUrlInfo::SCCS_ACCUREV:
pSCCS = (SCCS *)&accurevAccess;
break;
}
pSCCS->SetAuthInfo(it->second.username, it->second.password);
if (m_hMainDlg)
{
_stprintf_s(infotextbuf, _countof(infotextbuf), _T("checking %s ..."), it->first.c_str());
SendMessage(*this, COMMITMONITOR_INFOTEXT, 0, (LPARAM)infotextbuf);
}
svn_revnum_t headrev = pSCCS->GetHEADRevision(it->second.accurevRepo, it->first);
if ((pSCCS->Err)&&(pSCCS->Err->apr_err == SVN_ERR_RA_NOT_AUTHORIZED))
{
// only block the object for a short time
std::map<std::wstring,CUrlInfo> * pWrite = m_UrlInfos.GetWriteData();
std::map<std::wstring,CUrlInfo>::iterator writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.lastchecked = currenttime;
writeIt->second.error = pSCCS->GetLastErrorMsg();
writeIt->second.errNr = pSCCS->Err->apr_err;
// no need to save here just for this
}
m_UrlInfos.ReleaseWriteData();
continue;
}
if (!m_bRun)
continue;
std::set<std::wstring> authors;
if (headrev > it->second.lastcheckedrev)
{
CTraceToOutputDebugString::Instance()(_T("%s has updates! Last checked revision was %ld, HEAD revision is %ld\n"), it->first.c_str(), it->second.lastcheckedrev, headrev);
if (m_hMainDlg)
{
_stprintf_s(infotextbuf, _countof(infotextbuf), _T("getting log for %s"), it->first.c_str());
SendMessage(*this, COMMITMONITOR_INFOTEXT, 0, (LPARAM)infotextbuf);
}
int nNewCommits = 0; // commits without ignored ones
int nTotalNewCommits = 0; // all commits, including ignored ones
if (pSCCS->GetLog(it->second.accurevRepo, it->first, it->second.startfromrev ? it->second.startfromrev : headrev, it->second.lastcheckedrev + 1))
{
CTraceToOutputDebugString::Instance()(_T("log fetched for %s\n"), it->first.c_str());
if (!m_bRun)
continue;
// only block the object for a short time
std::map<std::wstring,CUrlInfo> * pWrite = m_UrlInfos.GetWriteData();
std::map<std::wstring,CUrlInfo>::iterator writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.lastcheckedrev = headrev;
writeIt->second.lastchecked = currenttime;
writeIt->second.startfromrev = 0;
// no need to save just for this
}
m_UrlInfos.ReleaseWriteData();
if (!m_bRun)
continue;
std::wstring sPopupText;
bool hadError = !it->second.error.empty();
for (auto logit = pSCCS->m_logs.begin(); logit != pSCCS->m_logs.end(); ++logit)
{
// again, only block for a short time
pWrite = m_UrlInfos.GetWriteData();
writeIt = pWrite->find(it->first);
bool bIgnore = false;
bool bEntryExists = false;
if (writeIt != pWrite->end())
{
std::map<svn_revnum_t,SCCSLogEntry>::iterator existIt = writeIt->second.logentries.find(logit->first);
bEntryExists = existIt != writeIt->second.logentries.end();
bool readState = false;
if (bEntryExists)
readState = existIt->second.read;
logit->second.read = readState;
writeIt->second.logentries[logit->first] = logit->second;
if (!bEntryExists)
{
std::wstring author1 = logit->second.author;
std::transform(author1.begin(), author1.end(), author1.begin(), ::towlower);
authors.insert(author1);
if (!writeIt->second.includeUsers.empty())
{
std::wstring s1 = writeIt->second.includeUsers;
std::transform(s1.begin(), s1.end(), s1.begin(), ::towlower);
CAppUtils::SearchReplace(s1, _T("\r\n"), _T("\n"));
std::vector<std::wstring> includeVector = CAppUtils::tokenize_str(s1, _T("\n"));
bool bInclude = false;
for (auto inclIt = includeVector.begin(); inclIt != includeVector.end(); ++inclIt)
{
if (author1.compare(*inclIt) == 0)
{
bInclude = true;
break;
}
}
bIgnore = !bInclude;
}
if (!writeIt->second.ignoreUsers.empty())
{
std::wstring s1 = writeIt->second.ignoreUsers;
std::transform(s1.begin(), s1.end(), s1.begin(), ::towlower);
CAppUtils::SearchReplace(s1, _T("\r\n"), _T("\n"));
std::vector<std::wstring> ignoreVector = CAppUtils::tokenize_str(s1, _T("\n"));
for (auto ignoreIt = ignoreVector.begin(); ignoreIt != ignoreVector.end(); ++ignoreIt)
{
if (author1.compare(*ignoreIt) == 0)
{
bIgnore = true;
break;
}
}
}
if (!writeIt->second.ignoreCommitLog.empty())
{
try
{
const std::wregex ignex(writeIt->second.ignoreCommitLog.c_str(), std::regex_constants::icase | std::regex_constants::ECMAScript);
if (std::regex_search(logit->second.message.begin(), logit->second.message.end(), ignex, std::regex_constants::match_default))
{
bIgnore = true;
}
}
catch (std::exception)
{
}
}
nTotalNewCommits++;
if (!bIgnore)
{
bNewEntries = true;
nNewCommits++;
}
else
// set own commit as already read
writeIt->second.logentries[logit->first].read = true;
}
writeIt->second.error.clear();
}
bNeedsSaving = true;
m_UrlInfos.ReleaseWriteData();
if (!m_bRun)
continue;
// popup info text
if ((!bIgnore)&&(!bEntryExists))
{
if (!sPopupText.empty())
sPopupText += _T(", ");
sPopupText += logit->second.author;
}
if ((!it->second.disallowdiffs)&&(it->second.fetchdiffs))
{
TCHAR buf[4096];
// first, find a name where to store the diff for that revision
_stprintf_s(buf, _countof(buf), _T("%s_%ld.diff"), it->second.name.c_str(), logit->first);
std::wstring diffFileName = CAppUtils::GetAppDataDir();
diffFileName += _T("/");
diffFileName += std::wstring(buf);
// do we already have that diff?
if (!PathFileExists(diffFileName.c_str()))
{
// get the diff
if (m_hMainDlg)
{
_stprintf_s(infotextbuf, _countof(infotextbuf), _T("getting diff for %s, revision %ld"), it->first.c_str(), logit->first);
SendMessage(*this, COMMITMONITOR_INFOTEXT, 0, (LPARAM)infotextbuf);
}
if (!pSCCS->Diff(it->first, logit->first, logit->first-1, logit->first, true, true, false, std::wstring(), false, diffFileName, std::wstring()))
{
CTraceToOutputDebugString::Instance()(_T("Diff not fetched for %s, revision %ld because of an error\n"), it->first.c_str(), logit->first);
DeleteFile(diffFileName.c_str());
}
else
CTraceToOutputDebugString::Instance()(_T("Diff fetched for %s, revision %ld\n"), it->first.c_str(), logit->first);
if (!m_bRun)
break;
}
}
}
if ((it->second.lastcheckedrobots + (60*60*24*2)) < currenttime)
{
std::wstring sRobotsURL = it->first;
sRobotsURL += _T("/svnrobots.txt");
std::wstring sRootRobotsURL;
std::wstring sDomainRobotsURL = sRobotsURL.substr(0, sRobotsURL.find('/', sRobotsURL.find(':')+3))+ _T("/svnrobots.txt");
sRootRobotsURL = pSCCS->GetRootUrl(it->first);
if (!sRootRobotsURL.empty())
sRootRobotsURL += _T("/svnrobots.txt");
std::wstring sFile = CAppUtils::GetTempFilePath();
std::string in;
std::unique_ptr<CCallback> callback(new CCallback);
callback->SetAuthData(it->second.username, it->second.password);
if ((!sDomainRobotsURL.empty())&&(URLDownloadToFile(NULL, sDomainRobotsURL.c_str(), sFile.c_str(), 0, callback.get()) == S_OK))
{
if (!m_bRun)
continue;
std::ifstream fs(sFile.c_str());
if (!fs.bad())
{
in.reserve((unsigned int)fs.rdbuf()->in_avail());
char c;
while (fs.get(c))
{
if (in.capacity() == in.size())
in.reserve(in.capacity() * 3);
in.append(1, c);
}
}
}
else if ((!sRootRobotsURL.empty())&&(pSCCS->GetFile(sRootRobotsURL, sFile)))
{
std::ifstream fs(sFile.c_str());
if (!fs.bad())
{
in.reserve((unsigned int)fs.rdbuf()->in_avail());
char c;
while (fs.get(c))
{
if (in.capacity() == in.size())
in.reserve(in.capacity() * 3);
in.append(1, c);
}
fs.close();
}
}
else if (pSCCS->GetFile(sRobotsURL, sFile))
{
std::ifstream fs(sFile.c_str());
if (!fs.bad())
{
in.reserve((unsigned int)fs.rdbuf()->in_avail());
char c;
while (fs.get(c))
{
if (in.capacity() == in.size())
in.reserve(in.capacity() * 3);
in.append(1, c);
}
fs.close();
}
}
DeleteFile(sFile.c_str());
// the format of the svnrobots.txt file is as follows:
// # comment
// disallowautodiff
// checkinterval = XXX
//
// with 'checkinterval' being the minimum amount of time to wait
// between checks in minutes.
std::istringstream iss(in);
std::string line;
int minutes = 0;
bool disallowdiffs = false;
while (getline(iss, line))
{
if (line.length())
{
if (line.at(0) != '#')
{
if (line.compare("disallowautodiff") == 0)
{
disallowdiffs = true;
}
else if ((line.length() > 13) && (line.substr(0, 13).compare("checkinterval") == 0))
{
std::string num = line.substr(line.find('=')+1);
minutes = atoi(num.c_str());
}
}
}
}
// again, only block for a short time
pWrite = m_UrlInfos.GetWriteData();
writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.lastcheckedrobots = currenttime;
writeIt->second.disallowdiffs = disallowdiffs;
writeIt->second.minminutesinterval = minutes;
// no need to save just for this
}
m_UrlInfos.ReleaseWriteData();
}
// prepare notification strings
if ((bNewEntries)||(!hadError && !it->second.error.empty()))
{
popupData data;
TCHAR sTitle[1024] = {0};
if (!it->second.error.empty() && DWORD(CRegStdDWORD(_T("Software\\CommitMonitor\\IndicateConnectErrors"), TRUE)))
{
_stprintf_s(sTitle, _countof(sTitle), _T("%s\nfailed to connect!"), it->second.name.c_str());
sPopupText = it->second.error;
}
else
{
data.sProject = it->second.name;
if (nNewCommits == 1)
_stprintf_s(sTitle, _countof(sTitle), _T("%s\nhas %d new commit"), it->second.name.c_str(), nNewCommits);
else
_stprintf_s(sTitle, _countof(sTitle), _T("%s\nhas %d new commits"), it->second.name.c_str(), nNewCommits);
}
data.sText = sPopupText;
data.sTitle = std::wstring(sTitle);
data.sAlertType = ALERTTYPE_NEWCOMMITS;
// check if there still are unread items
bool bUnread = false;
pWrite = m_UrlInfos.GetWriteData();
writeIt = pWrite->find(it->first);
for (auto lit = writeIt->second.logentries.cbegin(); lit != writeIt->second.logentries.cend(); ++lit)
{
if (!lit->second.read)
{
bUnread = true;
break;
}
}
// no need to save just for this
m_UrlInfos.ReleaseWriteData();
if (bUnread)
::SendMessage(*this, COMMITMONITOR_POPUP, 0, (LPARAM)&data);
}
bNewEntries = false;
}
else
{
// only block the object for a short time
std::map<std::wstring,CUrlInfo> * pWrite = m_UrlInfos.GetWriteData();
std::map<std::wstring,CUrlInfo>::iterator writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.lastchecked = currenttime;
writeIt->second.error = pSCCS->GetLastErrorMsg();
if (pSCCS->Err)
writeIt->second.errNr = pSCCS->Err->apr_err;
// no need to save just for this
}
m_UrlInfos.ReleaseWriteData();
}
// call the custom script
if ((nTotalNewCommits > 0)&&(!it->second.callcommand.empty()))
{
if ((!it->second.noexecuteignored)||(nNewCommits > 0))
{
// replace "%revision" with the new HEAD revision
std::wstring tag(_T("%revision"));
std::wstring commandline = it->second.callcommand;
// prepare the revision
TCHAR revBuf[40] = {0};
_stprintf_s(revBuf, _countof(revBuf), _T("%ld"), headrev);
std::wstring srev = revBuf;
CAppUtils::SearchReplace(commandline, tag, srev);
// replace "%url" with the repository url
tag = _T("%url");
CAppUtils::SearchReplace(commandline, tag, it->second.url);
// replace "%project" with the project name
tag = _T("%project");
CAppUtils::SearchReplace(commandline, tag, it->second.name);
// replace "%usernames" with a list of usernames for all the commits,
// separated by ";" (in case a username contains that char, you're out of luck, sorry)
tag = _T("%usernames");
std::wstring a;
for (auto autit = authors.cbegin(); autit != authors.cend(); ++autit)
{
if (!a.empty())
a += L";";
a += *autit;
}
CAppUtils::SearchReplace(commandline, tag, a);
CAppUtils::LaunchApplication(commandline);
}
}
}
else if (headrev > 0)
{
// only block the object for a short time
std::map<std::wstring,CUrlInfo> * pWrite = m_UrlInfos.GetWriteData();
std::map<std::wstring,CUrlInfo>::iterator writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.lastchecked = currenttime;
bool hadError = !writeIt->second.error.empty();
writeIt->second.error = pSCCS->GetLastErrorMsg();
if (pSCCS->Err)
writeIt->second.errNr = pSCCS->Err->apr_err;
if (!writeIt->second.error.empty() && DWORD(CRegStdDWORD(_T("Software\\CommitMonitor\\IndicateConnectErrors"), TRUE)))
{
if (!hadError)
{
TCHAR sTitle[1024] = {0};
_stprintf_s(sTitle, _countof(sTitle), _T("%s\nfailed to connect!"), it->second.name.c_str());
popupData data;
data.sText = pSCCS->GetLastErrorMsg();
data.sTitle = std::wstring(sTitle);
data.sAlertType = ALERTTYPE_FAILEDCONNECT;
::SendMessage(*this, COMMITMONITOR_POPUP, 0, (LPARAM)&data);
}
}
// no need to save just for this
}
m_UrlInfos.ReleaseWriteData();
if (m_hMainDlg)
{
_stprintf_s(infotextbuf, _countof(infotextbuf), _T("no new commits for %s"), it->first.c_str());
SendMessage(*this, COMMITMONITOR_INFOTEXT, 0, (LPARAM)infotextbuf);
}
}
else
{
// only block the object for a short time
std::map<std::wstring,CUrlInfo> * pWrite = m_UrlInfos.GetWriteData();
std::map<std::wstring,CUrlInfo>::iterator writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.lastchecked = currenttime;
bool hadError = !writeIt->second.error.empty();
writeIt->second.error = pSCCS->GetLastErrorMsg();
if (pSCCS->Err)
writeIt->second.errNr = pSCCS->Err->apr_err;
if (!writeIt->second.error.empty() && DWORD(CRegStdDWORD(_T("Software\\CommitMonitor\\IndicateConnectErrors"), TRUE)))
{
if (!hadError)
{
TCHAR sTitle[1024] = {0};
_stprintf_s(sTitle, _countof(sTitle), _T("%s\nfailed to connect!"), it->second.name.c_str());
popupData data;
data.sText = pSCCS->GetLastErrorMsg();
data.sTitle = std::wstring(sTitle);
data.sAlertType = ALERTTYPE_FAILEDCONNECT;
::SendMessage(*this, COMMITMONITOR_POPUP, 0, (LPARAM)&data);
}
}
// no need to save just for this
}
m_UrlInfos.ReleaseWriteData();
// if we already have log entries, then there's no need to
// check whether the url points to an SVNParentPath: it points
// to a repository, but we got an error for some reason when
// trying to find the HEAD revision
if (pSCCS->Err && it->second.logentries.empty() &&(it->second.lastcheckedrev == 0)&&((pSCCS->Err->apr_err == SVN_ERR_RA_DAV_RELOCATED)||(pSCCS->Err->apr_err == SVN_ERR_RA_DAV_REQUEST_FAILED)||(pSCCS->Err->apr_err == SVN_ERR_RA_DAV_MALFORMED_DATA) || (pSCCS->Err->apr_err == SVN_ERR_RA_CANNOT_CREATE_SESSION)))
{
// if we can't fetch the HEAD revision, it might be because the URL points to an SVNParentPath
// instead of pointing to an actual repository.
// we have to include the authentication in the URL itself
std::wstring tempfile = CAppUtils::GetTempFilePath();
std::unique_ptr<CCallback> callback(new CCallback);
callback->SetAuthData(it->second.username, it->second.password);
DeleteFile(tempfile.c_str());
std::wstring projName = it->second.name;
std::wstring parentpathurl = it->first;
std::wstring parentpathurl2 = parentpathurl + _T("/");
HRESULT hResUDL = URLDownloadToFile(NULL, parentpathurl2.c_str(), tempfile.c_str(), 0, callback.get());
if (!m_bRun)
continue;
if (hResUDL != S_OK)
{
hResUDL = URLDownloadToFile(NULL, parentpathurl.c_str(), tempfile.c_str(), 0, callback.get());
}
if (!m_bRun)
continue;
if (hResUDL == S_OK)
{
// we got a web page! But we can't be sure that it's the page from SVNParentPath.
// Use a regex to parse the website and find out...
std::ifstream fs(tempfile.c_str());
std::string in;
if (!fs.bad())
{
in.reserve((unsigned int)fs.rdbuf()->in_avail());
char c;
while (fs.get(c))
{
if (in.capacity() == in.size())
in.reserve(in.capacity() * 3);
in.append(1, c);
}
fs.close();
DeleteFile(tempfile.c_str());
// make sure this is a html page from an SVNParentPathList
// we do this by checking for header titles looking like
// "<h2>Revision XX: /</h2> - if we find that, it's a html
// page from inside a repository
// some repositories show
// "<h2>projectname - Revision XX: /trunk</h2>
const char * reTitle = "<\\s*h2\\s*>[^/]+Revision\\s*\\d+:[^<]+<\\s*/\\s*h2\\s*>";
// xsl transformed pages don't have an easy way to determine
// the inside from outside of a repository.
// We therefore check for <index rev="0" to make sure it's either
// an empty repository or really an SVNParentPathList
const char * reTitle2 = "<\\s*index\\s*rev\\s*=\\s*\"0\"";
const std::regex titex(reTitle, std::regex_constants::icase | std::regex_constants::ECMAScript);
const std::regex titex2(reTitle2, std::regex_constants::icase | std::regex_constants::ECMAScript);
if (std::regex_search(in.begin(), in.end(), titex, std::regex_constants::match_default))
{
CTraceToOutputDebugString::Instance()(_T("found repository url instead of SVNParentPathList\n"));
continue;
}
const char * re = "<\\s*LI\\s*>\\s*<\\s*A\\s+[^>]*HREF\\s*=\\s*\"([^\"]*)\"\\s*>([^<]+)<\\s*/\\s*A\\s*>\\s*<\\s*/\\s*LI\\s*>";
const char * re2 = "<\\s*DIR\\s*name\\s*=\\s*\"([^\"]*)\"\\s*HREF\\s*=\\s*\"([^\"]*)\"\\s*/\\s*>";
const std::regex expression(re, std::regex_constants::icase | std::regex_constants::ECMAScript);
const std::regex expression2(re2, std::regex_constants::icase | std::regex_constants::ECMAScript);
bool hasNewEntries = false;
int nCountNewEntries = 0;
std::wstring popupText;
const std::sregex_iterator end;
for (std::sregex_iterator i(in.begin(), in.end(), expression); i != end && m_bRun; ++i)
{
const std::smatch match = *i;
// what[0] contains the whole string
// what[1] contains the url part.
// what[2] contains the name
std::wstring url = CUnicodeUtils::StdGetUnicode(std::string(match[1]));
url = it->first + _T("/") + url;
url = pSCCS->CanonicalizeURL(url);
pWrite = m_UrlInfos.GetWriteData();
writeIt = pWrite->find(url);
if ((!m_bMainDlgRemovedItems)&&(writeIt == pWrite->end()))
{
// we found a new URL, add it to our list
CUrlInfo newinfo;
newinfo.url = url;
newinfo.name = CUnicodeUtils::StdGetUnicode(std::string(match[2]));
newinfo.name.erase(newinfo.name.find_last_not_of(_T("/ ")) + 1);
newinfo.username = it->second.username;
newinfo.password = it->second.password;
newinfo.fetchdiffs = it->second.fetchdiffs;
newinfo.minutesinterval = it->second.minutesinterval;
newinfo.ignoreUsers = it->second.ignoreUsers;
newinfo.callcommand = it->second.callcommand;
newinfo.webviewer = it->second.webviewer;
(*pWrite)[url] = newinfo;
hasNewEntries = true;
nCountNewEntries++;
if (!popupText.empty())
popupText += _T(", ");
popupText += newinfo.name;
bNeedsSaving = true;
}
writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.parentpath = true;
}
m_UrlInfos.ReleaseWriteData();
}
if (!regex_search(in.begin(), in.end(), titex2))
{
CTraceToOutputDebugString::Instance()(_T("found repository url instead of SVNParentPathList\n"));
continue;
}
for (std::sregex_iterator i(in.begin(), in.end(), expression2); i != end && m_bRun; ++i)
{
const std::smatch match = *i;
// what[0] contains the whole string
// what[1] contains the url part.
// what[2] contains the name
std::wstring url = CUnicodeUtils::StdGetUnicode(std::string(match[1]));
url = it->first + _T("/") + url;
url = pSCCS->CanonicalizeURL(url);
pWrite = m_UrlInfos.GetWriteData();
writeIt = pWrite->find(url);
if ((!m_bMainDlgRemovedItems)&&(writeIt == pWrite->end()))
{
// we found a new URL, add it to our list
CUrlInfo newinfo;
newinfo.url = url;
newinfo.name = CUnicodeUtils::StdGetUnicode(std::string(match[2]));
newinfo.name.erase(newinfo.name.find_last_not_of(_T("/ ")) + 1);
newinfo.username = it->second.username;
newinfo.password = it->second.password;
newinfo.fetchdiffs = it->second.fetchdiffs;
newinfo.minutesinterval = it->second.minutesinterval;
newinfo.ignoreUsers = it->second.ignoreUsers;
newinfo.callcommand = it->second.callcommand;
newinfo.webviewer = it->second.webviewer;
(*pWrite)[url] = newinfo;
hasNewEntries = true;
nCountNewEntries++;
if (!popupText.empty())
popupText += _T(", ");
popupText += newinfo.name;
bNeedsSaving = true;
}
writeIt = pWrite->find(it->first);
if (writeIt != pWrite->end())
{
writeIt->second.parentpath = true;
}
m_UrlInfos.ReleaseWriteData();
}
if (hasNewEntries)
{
it = pUrlInfoReadOnly->begin();
TCHAR popupTitle[1024] = {0};
_stprintf_s(popupTitle, _countof(popupTitle), _T("%s\nhas %d new projects"), projName.c_str(), nCountNewEntries);
popupData data;
data.sText = popupText;
data.sTitle = std::wstring(popupTitle);
data.sAlertType = ALERTTYPE_NEWPROJECTS;
::SendMessage(*this, COMMITMONITOR_POPUP, 0, (LPARAM)&data);
bNewEntries = false;
}
}
}
DeleteFile(tempfile.c_str());
}
}
}
}
// check for newer versions
if (m_bRun && (CRegStdDWORD(_T("Software\\CommitMonitor\\CheckNewer"), TRUE) != FALSE))
{
time_t now;
struct tm ptm;
time(&now);
if ((now != 0) && (localtime_s(&ptm, &now)==0))
{
int week = 0;
// we don't calculate the real 'week of the year' here
// because just to decide if we should check for an update
// that's not needed.
week = ptm.tm_yday / 7;
CRegStdDWORD oldweek = CRegStdDWORD(_T("Software\\CommitMonitor\\CheckNewerWeek"), (DWORD)-1);
if (((DWORD)oldweek) == -1)
oldweek = week; // first start of CommitMonitor, no update check needed
else
{
if ((DWORD)week != oldweek)
{
oldweek = week;
//
std::wstring tempfile = CAppUtils::GetTempFilePath();
CRegStdString checkurluser = CRegStdString(_T("Software\\CommitMonitor\\UpdateCheckURL"), _T(""));
CRegStdString checkurlmachine = CRegStdString(_T("Software\\CommitMonitor\\UpdateCheckURL"), _T(""), FALSE, HKEY_LOCAL_MACHINE);
std::wstring sCheckURL = (std::wstring)checkurluser;
if (sCheckURL.empty())
{
sCheckURL = (std::wstring)checkurlmachine;
if (sCheckURL.empty())
sCheckURL = L"https://svn.code.sf.net/p/commitmonitor/code/trunk/version.txt";
}
HRESULT res = URLDownloadToFile(NULL, sCheckURL.c_str(), tempfile.c_str(), 0, NULL);
if (res == S_OK)
{
std::ifstream File;
File.open(tempfile.c_str());
if (File.good())
{
char line[1024];
char * pLine = line;
File.getline(line, sizeof(line));
int major = 0;
int minor = 0;
int micro = 0;
int build = 0;
major = atoi(pLine);
pLine = strchr(pLine, '.');
if (pLine)
{
pLine++;
minor = atoi(pLine);
pLine = strchr(pLine, '.');
if (pLine)
{
pLine++;
micro = atoi(pLine);
pLine = strchr(pLine, '.');
if (pLine)
{
pLine++;
build = atoi(pLine);
}
}
}
if (major > CM_VERMAJOR)
m_bNewerVersionAvailable = true;
else if ((minor > CM_VERMINOR)&&(major == CM_VERMAJOR))
m_bNewerVersionAvailable = true;
else if ((micro > CM_VERMICRO)&&(minor == CM_VERMINOR)&&(major == CM_VERMAJOR))
m_bNewerVersionAvailable = true;
else if ((build > CM_VERBUILD)&&(micro == CM_VERMICRO)&&(minor == CM_VERMINOR)&&(major == CM_VERMAJOR))
m_bNewerVersionAvailable = true;
}
File.close();
}
}
}
}
}
SendMessage(*this, COMMITMONITOR_INFOTEXT, 0, (LPARAM)_T(""));
if (bNeedsSaving)
{
// save the changed entries
::PostMessage(*this, COMMITMONITOR_SAVEINFO, (WPARAM)true, (LPARAM)0);
}
if (bNewEntries)
::PostMessage(*this, COMMITMONITOR_CHANGEDINFO, (WPARAM)true, (LPARAM)0);
if ((!bNewEntries)&&(m_bIsTask))
::PostQuitMessage(0);
CTraceToOutputDebugString::Instance()(_T("monitor thread ended\n"));
m_bMainDlgRemovedItems = false;
m_ThreadRunning = FALSE;
SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);
::CoUninitialize();
return 0L;
}
DWORD WINAPI MonitorThread(LPVOID lpParam)
{
CHiddenWindow * pThis = (CHiddenWindow*)lpParam;
if (pThis)
return pThis->RunThread();
SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);
return 0L;
}
bool CHiddenWindow::StopThread(DWORD wait)
{
bool bRet = true;
m_bRun = false;
if (m_hMonitorThread)
{
WaitForSingleObject(m_hMonitorThread, wait);
if (m_ThreadRunning)
{
bRet = false;
}
CloseHandle(m_hMonitorThread);
m_hMonitorThread = NULL;
}
return bRet;
}
void CHiddenWindow::ShowPopup(std::wstring& title, std::wstring& text, const wchar_t *alertType)
{
popupData data;
data.sText = text;
data.sTitle = title;
data.sAlertType = alertType;
::SendMessage(*this, COMMITMONITOR_POPUP, 0, (LPARAM)&data);
}
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.