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
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Navigator.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corp.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Sean Su <ssu@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <windows.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
#include "extern.h"
#include "extra.h"
#include "dialogs.h"
#include "xpnetHook.h"
#ifdef __cplusplus
}
#endif /* __cplusplus */
#include "nsFTPConn.h"
#include "nsHTTPConn.h"
#include "nsSocket.h"
#define UPDATE_INTERVAL_STATUS 1
#define UPDATE_INTERVAL_PROGRESS_BAR 1
/* Cancel Status */
#define CS_NONE 0x00000000
#define CS_CANCEL 0x00000001
#define CS_PAUSE 0x00000002
#define CS_RESUME 0x00000003
const int kProxySrvrLen = 1024;
const char kHTTP[8] = "http://";
const char kFTP[7] = "ftp://";
const char kLoclFile[7] = "zzzFTP";
const int kModTimeOutValue = 3;
static nsHTTPConn *connHTTP = NULL;
static nsFTPConn *connFTP = NULL;
static long glLastBytesSoFar;
static long glAbsoluteBytesSoFar;
static long glBytesResumedFrom;
static long glTotalKb;
char gszStrCopyingFile[MAX_BUF_MEDIUM];
char gszCurrentDownloadPath[MAX_BUF];
char gszCurrentDownloadFilename[MAX_BUF_TINY];
char gszCurrentDownloadFileDescription[MAX_BUF_TINY];
char gszUrl[MAX_BUF];
char gszTo[MAX_BUF];
char gszFileInfo[MAX_BUF];
char *gszConfigIniFile;
BOOL gbDlgDownloadMinimized;
BOOL gbDlgDownloadJustMinimized;
BOOL gbUrlChanged;
BOOL gbShowDownloadRetryMsg;
DWORD gdwDownloadDialogStatus;
int giIndex;
int giTotalArchivesToDownload;
DWORD gdwTickStart;
BOOL gbStartTickCounter;
double GetPercentSoFar(void);
static void UpdateGaugeFileProgressBar(double value);
int ProgressCB(int aBytesSoFar, int aTotalFinalSize);
void InitDownloadDlg(void);
void DeInitDownloadDlg();
/* local prototypes */
siC *GetObjectFromArchiveName(char *szArchiveName);
struct DownloadFileInfo
{
char szUrl[MAX_BUF];
char szFile[MAX_BUF_TINY];
} dlFileInfo;
struct ExtractFilesDlgInfo
{
HWND hWndDlg;
int nMaxFileBars; // maximum number of bars that can be displayed
int nMaxArchiveBars; // maximum number of bars that can be displayed
int nFileBars; // current number of bars to display
int nArchiveBars; // current number of bars to display
} dlgInfo;
struct TickInfo
{
DWORD dwTickBegin;
DWORD dwTickEnd;
DWORD dwTickDif;
BOOL bTickStarted;
BOOL bTickDownloadResumed;
} gtiPaused;
BOOL CheckInterval(long *lModLastValue, int iInterval)
{
BOOL bRv = FALSE;
long lModCurrentValue;
if(iInterval == 1)
{
lModCurrentValue = time(NULL);
if(lModCurrentValue != *lModLastValue)
bRv = TRUE;
}
else
{
lModCurrentValue = time(NULL) % iInterval;
if((lModCurrentValue == 0) && (*lModLastValue != 0))
bRv = TRUE;
}
*lModLastValue = lModCurrentValue;
return(bRv);
}
char *GetTimeLeft(DWORD dwTimeLeft,
char *szTimeString,
DWORD dwTimeStringBufSize)
{
DWORD dwTimeLeftPP;
SYSTEMTIME stTime;
ZeroMemory(&stTime, sizeof(stTime));
dwTimeLeftPP = dwTimeLeft + 1;
stTime.wHour = (unsigned)(dwTimeLeftPP / 60 / 60);
stTime.wMinute = (unsigned)((dwTimeLeftPP / 60) % 60);
stTime.wSecond = (unsigned)(dwTimeLeftPP % 60);
ZeroMemory(szTimeString, dwTimeStringBufSize);
/* format time string using user's local time format information */
GetTimeFormat(LOCALE_USER_DEFAULT,
TIME_NOTIMEMARKER|TIME_FORCE24HOURFORMAT,
&stTime,
NULL,
szTimeString,
dwTimeStringBufSize);
return(szTimeString);
}
DWORD AddToTick(DWORD dwTick, DWORD dwTickMoreToAdd)
{
DWORD dwTickLeftTillWrap = 0;
/* Since GetTickCount() is the number of milliseconds since the system
* has been on and the return value is a DWORD, this value will wrap
* every 49.71 days or 0xFFFFFFFF milliseconds. */
dwTickLeftTillWrap = 0xFFFFFFFF - dwTick;
if(dwTickMoreToAdd > dwTickLeftTillWrap)
dwTick = dwTickMoreToAdd - dwTickLeftTillWrap;
else
dwTick = dwTick + dwTickMoreToAdd;
return(dwTick);
}
DWORD GetTickDif(DWORD dwTickEnd, DWORD dwTickStart)
{
DWORD dwTickDif;
/* Since GetTickCount() is the number of milliseconds since the system
* has been on and the return value is a DWORD, this value will wrap
* every 49.71 days or 0xFFFFFFFF milliseconds.
*
* Assumption: dwTickEnd has not wrapped _and_ passed dwTickStart */
if(dwTickEnd < dwTickStart)
dwTickDif = 0xFFFFFFFF - dwTickStart + dwTickEnd;
else
dwTickDif = dwTickEnd - dwTickStart;
return(dwTickDif);
}
void InitTickInfo(void)
{
gtiPaused.dwTickBegin = 0;
gtiPaused.dwTickEnd = 0;
gtiPaused.dwTickDif = 0;
gtiPaused.bTickStarted = FALSE;
gtiPaused.bTickDownloadResumed = FALSE;
}
DWORD RoundDouble(double dValue)
{
if(0.5 <= (dValue - (DWORD)dValue))
return((DWORD)dValue + 1);
else
return((DWORD)dValue);
}
void SetStatusStatus(void)
{
char szStatusStatusLine[MAX_BUF_MEDIUM];
char szCurrentStatusInfo[MAX_BUF_MEDIUM];
char szPercentString[MAX_BUF_MEDIUM];
char szPercentageCompleted[MAX_BUF_MEDIUM];
static long lModLastValue = 0;
double dRate;
static double dRateCounter;
DWORD dwTickNow;
DWORD dwTickDif;
DWORD dwKBytesSoFar;
DWORD dwRoundedRate;
char szTimeLeft[MAX_BUF_TINY];
/* If the user just clicked on the Resume button, then the time lapsed
* between gdwTickStart and when the Resume button was clicked needs to
* be subtracted taken into account when calculating dwTickDif. So
* "this" lapsed time needs to be added to gdwTickStart. */
if(gtiPaused.bTickDownloadResumed)
{
gdwTickStart = AddToTick(gdwTickStart, gtiPaused.dwTickDif);
InitTickInfo();
}
/* GetTickCount() returns time in milliseconds. This is more accurate,
* which will allow us to get at a 2 decimal precision value for the
* download rate. */
dwTickNow = GetTickCount();
if((gdwTickStart == 0) && gbStartTickCounter)
dwTickNow = gdwTickStart = GetTickCount();
dwTickDif = GetTickDif(dwTickNow, gdwTickStart);
/* Only update the UI every UPDATE_INTERVAL_STATUS interval,
* which is currently set to 1 sec. */
if(!CheckInterval(&lModLastValue, UPDATE_INTERVAL_STATUS))
return;
if(glAbsoluteBytesSoFar == 0)
dRateCounter = 0.0;
else
dRateCounter = dwTickDif / 1000;
if(dRateCounter == 0.0)
dRate = 0.0;
else
dRate = (glAbsoluteBytesSoFar - glBytesResumedFrom) / dRateCounter / 1024;
dwKBytesSoFar = glAbsoluteBytesSoFar / 1024;
/* Use a rate that is rounded to the nearest integer. If dRate used directly,
* the "Time Left" will jump around quite a bit due to the rate usually
* varying up and down by quite a bit. The rounded rate give a "more linear"
* count down of the "Time Left". */
dwRoundedRate = RoundDouble(dRate);
if(dwRoundedRate > 0)
GetTimeLeft((glTotalKb - dwKBytesSoFar) / dwRoundedRate,
szTimeLeft,
sizeof(szTimeLeft));
else
lstrcpy(szTimeLeft, "00:00:00");
if(!gbShowDownloadRetryMsg)
{
GetPrivateProfileString("Strings",
"Status Download",
"",
szStatusStatusLine,
sizeof(szStatusStatusLine),
szFileIniConfig);
if(*szStatusStatusLine != '\0')
sprintf(szCurrentStatusInfo,
szStatusStatusLine,
szTimeLeft,
dRate,
dwKBytesSoFar,
glTotalKb);
else
sprintf(szCurrentStatusInfo,
"%s at %.2fKB/sec (%uKB of %uKB downloaded)",
szTimeLeft,
dRate,
dwKBytesSoFar,
glTotalKb);
}
else
{
GetPrivateProfileString("Strings",
"Status Retry",
"",
szStatusStatusLine,
sizeof(szStatusStatusLine),
szFileIniConfig);
if(*szStatusStatusLine != '\0')
sprintf(szCurrentStatusInfo,
szStatusStatusLine,
szTimeLeft,
dRate,
dwKBytesSoFar,
glTotalKb);
else
sprintf(szCurrentStatusInfo,
"%s at %.2KB/sec (%uKB of %uKB downloaded)",
szTimeLeft,
dRate,
dwKBytesSoFar,
glTotalKb);
}
GetPrivateProfileString("Strings",
"Status Percentage Completed",
"",
szPercentageCompleted,
sizeof(szPercentageCompleted),
szFileIniConfig);
wsprintf(szPercentString, szPercentageCompleted, (int)GetPercentSoFar());
/* Set the download dialog title */
SetDlgItemText(dlgInfo.hWndDlg, IDC_STATUS_STATUS, szCurrentStatusInfo);
SetDlgItemText(dlgInfo.hWndDlg, IDC_PERCENTAGE, szPercentString);
}
void SetStatusFile(void)
{
char szString[MAX_BUF];
/* Set the download dialog status*/
wsprintf(szString, gszFileInfo, gszCurrentDownloadFileDescription);
SetDlgItemText(dlgInfo.hWndDlg, IDC_STATUS_FILE, szString);
SetStatusStatus();
}
void SetStatusUrl(void)
{
/* display the current url being processed */
if(gbUrlChanged)
{
char szUrlPathBuf[MAX_BUF];
char szToPathBuf[MAX_BUF];
HWND hStatusUrl = NULL;
HWND hStatusTo = NULL;
hStatusUrl = GetDlgItem(dlgInfo.hWndDlg, IDC_STATUS_URL);
if(hStatusUrl)
TruncateString(hStatusUrl, gszUrl, szUrlPathBuf, sizeof(szUrlPathBuf));
else
lstrcpy(szUrlPathBuf, gszUrl);
hStatusTo = GetDlgItem(dlgInfo.hWndDlg, IDC_STATUS_TO);
if(hStatusTo)
TruncateString(hStatusTo, gszTo, szToPathBuf, sizeof(szToPathBuf));
else
lstrcpy(szToPathBuf, gszTo);
SetDlgItemText(dlgInfo.hWndDlg, IDC_STATUS_URL, szUrlPathBuf);
SetDlgItemText(dlgInfo.hWndDlg, IDC_STATUS_TO, szToPathBuf);
SetStatusFile();
gbUrlChanged = FALSE;
}
}
double GetPercentSoFar(void)
{
return((double)(((double)(glAbsoluteBytesSoFar / 1024) / (double)glTotalKb) * (double)100));
}
void SetMinimizedDownloadTitle(DWORD dwPercentSoFar)
{
static DWORD dwLastPercentSoFar = 0;
char szDownloadTitle[MAX_BUF_MEDIUM];
char gszCurrentDownloadInfo[MAX_BUF_MEDIUM];
GetPrivateProfileString("Strings", "Dialog Download Title Minimized", "", szDownloadTitle, sizeof(szDownloadTitle), szFileIniConfig);
if(*szDownloadTitle != '\0')
wsprintf(gszCurrentDownloadInfo, szDownloadTitle, dwPercentSoFar, gszCurrentDownloadFilename);
else
wsprintf(gszCurrentDownloadInfo, "%d%% for all files", dwPercentSoFar);
/* check and save the current percent so far to minimize flickering */
if((dwLastPercentSoFar != dwPercentSoFar) || gbDlgDownloadJustMinimized)
{
/* When the dialog is has just been minimized, the title is not set
* until the percentage changes, which when downloading via modem,
* could take several seconds. This variable allows us to tell when
* the dialog had *just* been minimized regardless if the percentage
* had changed or not. */
gbDlgDownloadJustMinimized = FALSE;
/* Set the download dialog title */
SetWindowText(dlgInfo.hWndDlg, gszCurrentDownloadInfo);
dwLastPercentSoFar = dwPercentSoFar;
}
}
void SetRestoredDownloadTitle(void)
{
/* Set the download dialog title */
SetWindowText(dlgInfo.hWndDlg, diDownload.szTitle);
}
void GetTotalArchivesToDownload(int *iTotalArchivesToDownload, DWORD *dwTotalEstDownloadSize)
{
int iIndex = 0;
char szUrl[MAX_BUF];
char szSection[MAX_INI_SK];
char szDownloadSize[MAX_ITOA];
*dwTotalEstDownloadSize = 0;
iIndex = 0;
wsprintf(szSection, "File%d", iIndex);
GetPrivateProfileString(szSection,
"url0",
"",
szUrl,
sizeof(szUrl),
gszConfigIniFile);
while(*szUrl != '\0')
{
GetPrivateProfileString(szSection, "size", "", szDownloadSize, sizeof(szDownloadSize), gszConfigIniFile);
if((lstrlen(szDownloadSize) < 32) && (*szDownloadSize != '\0'))
/* size will be in kb. 31 bits (int minus the signed bit) of precision should sufice */
*dwTotalEstDownloadSize += atoi(szDownloadSize);
++iIndex;
wsprintf(szSection, "File%d", iIndex);
GetPrivateProfileString(szSection,
"url0",
"",
szUrl,
sizeof(szUrl),
gszConfigIniFile);
}
*iTotalArchivesToDownload = iIndex;
}
/*
* Name: ProcessWndMsgCB
*
* Arguments: None
*
* Description: Callback function invoked by socket code and by FTP and HTTP layers
* to give the UI a chance to breath while we are in a look processing
* incoming data, or looping in select()
*
* Author: syd@netscape.com 5/11/2001
*
*/
int
ProcessWndMsgCB()
{
int iRv = nsFTPConn::OK;
ProcessWindowsMessages();
if((gdwDownloadDialogStatus == CS_CANCEL) ||
(gdwDownloadDialogStatus == CS_PAUSE))
iRv = nsFTPConn::E_USER_CANCEL;
return(iRv);
}
/* Function used only to send the message stream error */
int WGet(char *szUrl,
char *szFile,
char *szProxyServer,
char *szProxyPort,
char *szProxyUser,
char *szProxyPasswd)
{
int rv;
char proxyURL[MAX_BUF];
nsHTTPConn *conn = NULL;
if((szProxyServer != NULL) && (szProxyPort != NULL) &&
(*szProxyServer != '\0') && (*szProxyPort != '\0'))
{
/* detected proxy information, let's use it */
memset(proxyURL, 0, sizeof(proxyURL));
wsprintf(proxyURL, "http://%s:%s", szProxyServer, szProxyPort);
conn = new nsHTTPConn(proxyURL);
if(conn == NULL)
return(WIZ_OUT_OF_MEMORY);
if((szProxyUser != NULL) && (*szProxyUser != '\0') &&
(szProxyPasswd != NULL) && (*szProxyPasswd != '\0'))
/* detected user and password info */
conn->SetProxyInfo(szUrl, szProxyUser, szProxyPasswd);
else
conn->SetProxyInfo(szUrl, NULL, NULL);
}
else
{
/* no proxy information supplied. set up normal http object */
conn = new nsHTTPConn(szUrl, ProcessWndMsgCB);
if(conn == NULL)
return(WIZ_OUT_OF_MEMORY);
}
rv = conn->Open();
if(rv == WIZ_OK)
{
rv = conn->Get(NULL, szFile);
conn->Close();
}
if(conn)
delete(conn);
return(rv);
}
int DownloadViaProxyOpen(char *szUrl, char *szProxyServer, char *szProxyPort, char *szProxyUser, char *szProxyPasswd)
{
int rv;
char proxyURL[kProxySrvrLen];
if((!szUrl) || (*szUrl == '\0'))
return nsHTTPConn::E_PARAM;
rv = nsHTTPConn::OK;
memset(proxyURL, 0, kProxySrvrLen);
wsprintf(proxyURL, "http://%s:%s", szProxyServer, szProxyPort);
connHTTP = new nsHTTPConn(proxyURL, ProcessWndMsgCB);
if(connHTTP == NULL)
{
char szBuf[MAX_BUF_TINY];
GetPrivateProfileString("Strings", "Error Out Of Memory", "", szBuf, sizeof(szBuf), szFileIniConfig);
PrintError(szBuf, ERROR_CODE_HIDE);
return(WIZ_OUT_OF_MEMORY);
}
if((szProxyUser != NULL) && (*szProxyUser != '\0') &&
(szProxyPasswd != NULL) && (*szProxyPasswd != '\0'))
connHTTP->SetProxyInfo(szUrl, szProxyUser, szProxyPasswd);
else
connHTTP->SetProxyInfo(szUrl, NULL, NULL);
rv = connHTTP->Open();
return(rv);
}
void DownloadViaProxyClose(void)
{
gbStartTickCounter = FALSE;
if(connHTTP)
{
connHTTP->Close();
delete(connHTTP);
connHTTP = NULL;
}
}
int DownloadViaProxy(char *szUrl, char *szProxyServer, char *szProxyPort, char *szProxyUser, char *szProxyPasswd)
{
int rv;
char *file = NULL;
rv = nsHTTPConn::OK;
if((!szUrl) || (*szUrl == '\0'))
return nsHTTPConn::E_PARAM;
if(connHTTP == NULL)
{
rv = DownloadViaProxyOpen(szUrl,
szProxyServer,
szProxyPort,
szProxyUser,
szProxyPasswd);
if(rv != nsHTTPConn::OK)
{
DownloadViaProxyClose();
return(rv);
}
}
if(connHTTP == NULL)
{
char szBuf[MAX_BUF_TINY];
GetPrivateProfileString("Strings", "Error Out Of Memory", "", szBuf, sizeof(szBuf), szFileIniConfig);
PrintError(szBuf, ERROR_CODE_HIDE);
return(WIZ_OUT_OF_MEMORY);
}
if(strrchr(szUrl, '/') != (szUrl + strlen(szUrl)))
file = strrchr(szUrl, '/') + 1; // set to leaf name
gbStartTickCounter = TRUE;
rv = connHTTP->Get(ProgressCB, file); // use leaf from URL
DownloadViaProxyClose();
return(rv);
}
int DownloadViaHTTPOpen(char *szUrl)
{
int rv;
if((!szUrl) || (*szUrl == '\0'))
return nsHTTPConn::E_PARAM;
rv = nsHTTPConn::OK;
connHTTP = new nsHTTPConn(szUrl, ProcessWndMsgCB);
if(connHTTP == NULL)
{
char szBuf[MAX_BUF_TINY];
GetPrivateProfileString("Strings", "Error Out Of Memory", "", szBuf, sizeof(szBuf), gszConfigIniFile);
PrintError(szBuf, ERROR_CODE_HIDE);
return(WIZ_OUT_OF_MEMORY);
}
rv = connHTTP->Open();
return(rv);
}
void DownloadViaHTTPClose(void)
{
gbStartTickCounter = FALSE;
if(connHTTP)
{
connHTTP->Close();
delete(connHTTP);
connHTTP = NULL;
}
}
int DownloadViaHTTP(char *szUrl)
{
int rv;
char *file = NULL;
if((!szUrl) || (*szUrl == '\0'))
return nsHTTPConn::E_PARAM;
if(connHTTP == NULL)
{
rv = DownloadViaHTTPOpen(szUrl);
if(rv != nsHTTPConn::OK)
{
DownloadViaHTTPClose();
return(rv);
}
}
if(connHTTP == NULL)
{
char szBuf[MAX_BUF_TINY];
GetPrivateProfileString("Strings", "Error Out Of Memory", "", szBuf, sizeof(szBuf), gszConfigIniFile);
PrintError(szBuf, ERROR_CODE_HIDE);
return(WIZ_OUT_OF_MEMORY);
}
rv = nsHTTPConn::OK;
if(strrchr(szUrl, '/') != (szUrl + strlen(szUrl)))
file = strrchr(szUrl, '/') + 1; // set to leaf name
gbStartTickCounter = TRUE;
rv = connHTTP->Get(ProgressCB, file);
DownloadViaHTTPClose();
return(rv);
}
int DownloadViaFTPOpen(char *szUrl)
{
char *host = 0, *path = 0, *file = (char*) kLoclFile;
int port = 21;
int rv;
if((!szUrl) || (*szUrl == '\0'))
return nsFTPConn::E_PARAM;
rv = nsHTTPConn::ParseURL(kFTP, szUrl, &host, &port, &path);
connFTP = new nsFTPConn(host, ProcessWndMsgCB);
if(connFTP == NULL)
{
char szBuf[MAX_BUF_TINY];
GetPrivateProfileString("Strings", "Error Out Of Memory", "", szBuf, sizeof(szBuf), szFileIniConfig);
PrintError(szBuf, ERROR_CODE_HIDE);
return(WIZ_OUT_OF_MEMORY);
}
rv = connFTP->Open();
if(host)
free(host);
if(path)
free(path);
return(rv);
}
void DownloadViaFTPClose(void)
{
gbStartTickCounter = FALSE;
if(connFTP)
{
connFTP->Close();
delete(connFTP);
connFTP = NULL;
}
}
int DownloadViaFTP(char *szUrl)
{
char *host = 0, *path = 0, *file = (char*) kLoclFile;
int port = 21;
int rv;
if((!szUrl) || (*szUrl == '\0'))
return nsFTPConn::E_PARAM;
if(connFTP == NULL)
{
rv = DownloadViaFTPOpen(szUrl);
if(rv != nsFTPConn::OK)
{
DownloadViaFTPClose();
return(rv);
}
}
if(connFTP == NULL)
{
char szBuf[MAX_BUF_TINY];
GetPrivateProfileString("Strings", "Error Out Of Memory", "", szBuf, sizeof(szBuf), szFileIniConfig);
PrintError(szBuf, ERROR_CODE_HIDE);
return(WIZ_OUT_OF_MEMORY);
}
rv = nsHTTPConn::ParseURL(kFTP, szUrl, &host, &port, &path);
if(strrchr(path, '/') != (path + strlen(path)))
file = strrchr(path, '/') + 1; // set to leaf name
gbStartTickCounter = TRUE;
rv = connFTP->Get(path, file, nsFTPConn::BINARY, TRUE, ProgressCB);
if(host)
free(host);
if(path)
free(path);
return(rv);
}
void PauseTheDownload(int rv, int *iFileDownloadRetries)
{
if(rv != nsFTPConn::E_USER_CANCEL)
{
SendMessage(dlgInfo.hWndDlg, WM_COMMAND, IDPAUSE, 0);
--*iFileDownloadRetries;
}
while(gdwDownloadDialogStatus == CS_PAUSE)
{
SleepEx(200, FALSE);
ProcessWindowsMessages();
}
}
void CloseSocket(char *szProxyServer, char *szProxyPort)
{
/* Close the socket connection from the first attempt. */
if((szProxyServer != NULL) && (szProxyPort != NULL) &&
(*szProxyServer != '\0') && (*szProxyPort != '\0'))
DownloadViaProxyClose();
else
{
/* is this an HTTP URL? */
if(strncmp(gszUrl, kHTTP, lstrlen(kHTTP)) == 0)
DownloadViaHTTPClose();
/* or is this an FTP URL? */
else if(strncmp(gszUrl, kFTP, lstrlen(kFTP)) == 0)
DownloadViaFTPClose();
}
}
siC *GetObjectFromArchiveName(char *szArchiveName)
{
DWORD dwIndex;
siC *siCObject = NULL;
siC *siCNode = NULL;
dwIndex = 0;
siCObject = SiCNodeGetObject(dwIndex, TRUE, AC_ALL);
while(siCObject)
{
if(lstrcmpi(szArchiveName, siCObject->szArchiveName) == 0)
{
siCNode = siCObject;
break;
}
++dwIndex;
siCObject = SiCNodeGetObject(dwIndex, TRUE, AC_ALL);
}
return(siCNode);
}
int DownloadFiles(char *szInputIniFile,
char *szDownloadDir,
char *szProxyServer,
char *szProxyPort,
char *szProxyUser,
char *szProxyPasswd,
BOOL bShowRetryMsg,
BOOL bIgnoreAllNetworkErrors,
char *szFailedFile,
DWORD dwFailedFileSize)
{
char szBuf[MAX_BUF];
char szCurrentFile[MAX_BUF];
char szSection[MAX_INI_SK];
char szKey[MAX_INI_SK];
char szSavedCwd[MAX_BUF_MEDIUM];
int iCounter;
int rv;
int iFileDownloadRetries;
int iIgnoreFileNetworkError;
int iLocalTimeOutCounter;
DWORD dwTotalEstDownloadSize;
char szPartiallyDownloadedFilename[MAX_BUF];
BOOL bDownloadInitiated;
char szTempURL[MAX_BUF];
char szWorkingURLPathOnly[MAX_BUF];
siC *siCCurrentFileObj = NULL;
ZeroMemory(szTempURL, sizeof(szTempURL));
ZeroMemory(szWorkingURLPathOnly, sizeof(szWorkingURLPathOnly));
if(szInputIniFile == NULL)
return(WIZ_ERROR_UNDEFINED);
if(szFailedFile)
ZeroMemory(szFailedFile, dwFailedFileSize);
InitTickInfo();
GetCurrentDirectory(sizeof(szSavedCwd), szSavedCwd);
SetCurrentDirectory(szDownloadDir);
rv = WIZ_OK;
dwTotalEstDownloadSize = 0;
giTotalArchivesToDownload = 0;
glLastBytesSoFar = 0;
glAbsoluteBytesSoFar = 0;
glBytesResumedFrom = 0;
gdwTickStart = 0; /* Initialize the counter used to
* calculate download rate */
gbStartTickCounter = FALSE; /* used to determine when to start
* the tick counter used to calculate
* the download rate */
gbUrlChanged = TRUE;
gbDlgDownloadMinimized = FALSE;
gbDlgDownloadJustMinimized = FALSE;
gdwDownloadDialogStatus = CS_NONE;
gbShowDownloadRetryMsg = bShowRetryMsg;
gszConfigIniFile = szInputIniFile;
bDownloadInitiated = FALSE;
GetTotalArchivesToDownload(&giTotalArchivesToDownload,
&dwTotalEstDownloadSize);
glTotalKb = dwTotalEstDownloadSize;
GetSetupCurrentDownloadFile(szPartiallyDownloadedFilename,
sizeof(szPartiallyDownloadedFilename));
ShowMessage(NULL, FALSE);
InitDownloadDlg();
for(giIndex = 0; giIndex < giTotalArchivesToDownload; giIndex++)
{
/* set (or reset) the counter to 0 in order to read the
* next files's 0'th url from the .idi file */
iCounter = 0;
gbUrlChanged = TRUE; /* Update the download dialog with new URL */
wsprintf(szSection, "File%d", giIndex);
wsprintf(szKey, "url%d", iCounter);
GetPrivateProfileString(szSection,
szKey,
"",
szTempURL,
sizeof(szTempURL),
gszConfigIniFile);
if(*szTempURL == '\0')
continue;
if(!bDownloadInitiated)
{
ParsePath(szTempURL,
szWorkingURLPathOnly,
sizeof(szWorkingURLPathOnly),
TRUE, //use '/' as the path delimiter
PP_PATH_ONLY);
}
GetPrivateProfileString(szSection,
"desc",
"",
gszCurrentDownloadFileDescription,
sizeof(gszCurrentDownloadFileDescription),
gszConfigIniFile);
iIgnoreFileNetworkError = GetPrivateProfileInt(szSection,
"Ignore File Network Error",
0,
gszConfigIniFile);
/* save the file name to be downloaded */
ParsePath(szTempURL,
szCurrentFile,
sizeof(szCurrentFile),
TRUE, //use '/' as the path delimiter
PP_FILENAME_ONLY);
RemoveSlash(szWorkingURLPathOnly);
wsprintf(gszUrl, "%s/%s", szWorkingURLPathOnly, szCurrentFile);
/* retrieve the file's data structure */
siCCurrentFileObj = GetObjectFromArchiveName(szCurrentFile);
if((*szPartiallyDownloadedFilename != 0) &&
(lstrcmpi(szPartiallyDownloadedFilename, szCurrentFile) == 0))
{
struct stat statBuf;
if(stat(szPartiallyDownloadedFilename, &statBuf) != -1)
{
glAbsoluteBytesSoFar += statBuf.st_size;
glBytesResumedFrom = statBuf.st_size;
}
}
lstrcpy(gszTo, szDownloadDir);
AppendBackSlash(gszTo, sizeof(gszTo));
lstrcat(gszTo, szCurrentFile);
if(gbDlgDownloadMinimized)
SetMinimizedDownloadTitle((int)GetPercentSoFar());
else
{
SetStatusUrl();
SetRestoredDownloadTitle();
}
SetSetupCurrentDownloadFile(szCurrentFile);
iFileDownloadRetries = 0;
iLocalTimeOutCounter = 0;
do
{
ProcessWindowsMessages();
/* Download starts here */
if((szProxyServer != NULL) && (szProxyPort != NULL) &&
(*szProxyServer != '\0') && (*szProxyPort != '\0'))
/* If proxy info is provided, use HTTP proxy */
rv = DownloadViaProxy(gszUrl,
szProxyServer,
szProxyPort,
szProxyUser,
szProxyPasswd);
else
{
/* is this an HTTP URL? */
if(strncmp(gszUrl, kHTTP, lstrlen(kHTTP)) == 0)
rv = DownloadViaHTTP(gszUrl);
/* or is this an FTP URL? */
else if(strncmp(gszUrl, kFTP, lstrlen(kFTP)) == 0)
rv = DownloadViaFTP(gszUrl);
}
bDownloadInitiated = TRUE;
if((rv == nsFTPConn::E_USER_CANCEL) ||
(gdwDownloadDialogStatus == CS_PAUSE))
{
if(gdwDownloadDialogStatus == CS_PAUSE)
{
CloseSocket(szProxyServer, szProxyPort);
/* rv needs to be set to something
* other than E_USER_CANCEL or E_OK */
rv = nsFTPConn::E_CMD_UNEXPECTED;
PauseTheDownload(rv, &iFileDownloadRetries);
bDownloadInitiated = FALSE; /* restart the download using
* new socket connection */
}
else
{
/* user canceled; break out of the do loop */
break;
}
}
else if((rv != nsFTPConn::OK) &&
(rv != nsFTPConn::E_CMD_FAIL) &&
(rv != nsSocket::E_BIND) &&
(rv != nsHTTPConn::E_HTTP_RESPONSE) &&
(gdwDownloadDialogStatus != CS_CANCEL))
{
/* We timed out. No response from the server, or
* we somehow lost connection. */
char szTitle[MAX_BUF_SMALL];
char szMsgDownloadPaused[MAX_BUF];
/* Incrememt the time out counter on E_TIMEOUT */
if(rv == nsSocket::E_TIMEOUT)
{
++siCCurrentFileObj->iNetTimeOuts;
++iLocalTimeOutCounter;
}
CloseSocket(szProxyServer, szProxyPort);
/* If the number of timeouts is %3 == 0, then let's pause
* the download process. Otherwise, just close the
* connection and open a new one to see if the download
* can be restarted automatically. */
if((rv != nsSocket::E_TIMEOUT) ||
(rv == nsSocket::E_TIMEOUT) && ((iLocalTimeOutCounter % kModTimeOutValue) == 0))
{
/* Start the pause tick counter here because we don't know how
* long before the user will dismiss the MessageBox() */
if(!gtiPaused.bTickStarted)
{
gtiPaused.dwTickBegin = GetTickCount();
gtiPaused.bTickStarted = TRUE;
gtiPaused.bTickDownloadResumed = FALSE;
}
/* The connection unexepectedly dropped for some reason, so inform
* the user that the download will be Paused, and then update the
* Download dialog to show the Paused state. */
GetPrivateProfileString("Messages",
"MB_WARNING_STR",
"",
szTitle,
sizeof(szTitle),
szFileIniInstall);
GetPrivateProfileString("Strings",
"Message Download Paused",
"",
szMsgDownloadPaused,
sizeof(szMsgDownloadPaused),
szFileIniConfig);
MessageBox(dlgInfo.hWndDlg,
szMsgDownloadPaused,
szTitle,
MB_ICONEXCLAMATION);
/* Let's make sure we're in a paused state */
gdwDownloadDialogStatus = CS_PAUSE;
PauseTheDownload(rv, &iFileDownloadRetries);
}
else
/* Let's make sure we're _not_ in a paused state */
gdwDownloadDialogStatus = CS_NONE;
}
/* We don't count time outs as normal failures. We're
* keeping track of time outs differently. */
if(rv != nsSocket::E_TIMEOUT)
++iFileDownloadRetries;
if((iFileDownloadRetries > MAX_FILE_DOWNLOAD_RETRIES) &&
(rv != nsFTPConn::E_USER_CANCEL) &&
(gdwDownloadDialogStatus != CS_CANCEL))
{
/* since the download retries maxed out, increment the counter
* to read the next url for the current file */
++iCounter;
wsprintf(szKey, "url%d", iCounter);
GetPrivateProfileString(szSection,
szKey,
"",
szTempURL,
sizeof(szTempURL),
gszConfigIniFile);
if(*szTempURL != '\0')
{
/* Found more urls to download from for the current file.
* Update the dialog to show the new url and reset the
* file download retries to 0 since it's a new url. */
gbUrlChanged = TRUE;
iFileDownloadRetries = 0;
bDownloadInitiated = FALSE; // restart the download using new socket connection
CloseSocket(szProxyServer, szProxyPort);
ParsePath(szTempURL,
szWorkingURLPathOnly,
sizeof(szWorkingURLPathOnly),
TRUE, //use '/' as the path delimiter
PP_PATH_ONLY);
RemoveSlash(szWorkingURLPathOnly);
wsprintf(gszUrl, "%s/%s", szWorkingURLPathOnly, szCurrentFile);
SetStatusUrl();
}
}
} while((rv != nsFTPConn::E_USER_CANCEL) &&
(rv != nsFTPConn::OK) &&
(gdwDownloadDialogStatus != CS_CANCEL) &&
(iFileDownloadRetries <= MAX_FILE_DOWNLOAD_RETRIES));
/* Save the number of retries for each file */
siCCurrentFileObj->iNetRetries = iFileDownloadRetries < 1 ? 0:iFileDownloadRetries - 1;
if((rv == nsFTPConn::E_USER_CANCEL) ||
(gdwDownloadDialogStatus == CS_CANCEL))
{
/* make sure rv is E_USER_CANCEL when gdwDownloadDialogStatus
* is CS_CANCEL */
rv = nsFTPConn::E_USER_CANCEL;
if(szFailedFile && ((DWORD)lstrlen(szCurrentFile) <= dwFailedFileSize))
lstrcpy(szFailedFile, gszCurrentDownloadFileDescription);
/* break out of for() loop */
break;
}
if((rv != nsFTPConn::OK) &&
(iFileDownloadRetries > MAX_FILE_DOWNLOAD_RETRIES) &&
!bIgnoreAllNetworkErrors &&
!iIgnoreFileNetworkError)
{
/* too many retries from failed downloads */
char szMsg[MAX_BUF];
if(szFailedFile && ((DWORD)lstrlen(szCurrentFile) <= dwFailedFileSize))
lstrcpy(szFailedFile, gszCurrentDownloadFileDescription);
GetPrivateProfileString("Strings",
"Error Too Many Network Errors",
"",
szMsg,
sizeof(szMsg),
szFileIniConfig);
if(*szMsg != '\0')
{
wsprintf(szBuf, szMsg, szCurrentFile);
PrintError(szBuf, ERROR_CODE_HIDE);
}
iFileDownloadRetries = 0; // reset the file download retries counter since
// we'll be restarting the download again.
bDownloadInitiated = FALSE; // restart the download using new socket connection
CloseSocket(szProxyServer, szProxyPort);
--giIndex; // Decrement the file index counter because we'll be trying to
// download the same file again. We don't want to go to the next
// file just yet.
/* Let's make sure we're in a paused state. */
/* The pause state will be unset by DownloadDlgProc(). */
gdwDownloadDialogStatus = CS_PAUSE;
PauseTheDownload(rv, &iFileDownloadRetries);
}
else if(bIgnoreAllNetworkErrors || iIgnoreFileNetworkError)
rv = nsFTPConn::OK;
UnsetSetupCurrentDownloadFile();
}
CloseSocket(szProxyServer, szProxyPort);
DeInitDownloadDlg();
SetCurrentDirectory(szSavedCwd);
return(rv);
}
int ProgressCB(int aBytesSoFar, int aTotalFinalSize)
{
long lBytesDiffSoFar;
double dPercentSoFar;
int iRv = nsFTPConn::OK;
if(sgProduct.mode != SILENT)
{
SetStatusUrl();
if(glTotalKb == 0)
glTotalKb = aTotalFinalSize;
/* Calculate the difference between the last set of bytes read against
* the current set of bytes read. If the value is negative, that means
* that it started a new file, so reset lBytesDiffSoFar to aBytesSoFar */
lBytesDiffSoFar = ((aBytesSoFar - glLastBytesSoFar) < 1) ? aBytesSoFar : (aBytesSoFar - glLastBytesSoFar);
/* Save the current bytes read as the last set of bytes read */
glLastBytesSoFar = aBytesSoFar;
glAbsoluteBytesSoFar += lBytesDiffSoFar;
dPercentSoFar = GetPercentSoFar();
if(gbDlgDownloadMinimized)
SetMinimizedDownloadTitle((int)dPercentSoFar);
UpdateGaugeFileProgressBar(dPercentSoFar);
SetStatusStatus();
if((gdwDownloadDialogStatus == CS_CANCEL) ||
(gdwDownloadDialogStatus == CS_PAUSE))
iRv = nsFTPConn::E_USER_CANCEL;
}
ProcessWindowsMessages();
return(iRv);
}
// Window proc for dialog
LRESULT CALLBACK
DownloadDlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_INITDIALOG:
GetPrivateProfileString("Strings",
"Status File Info",
"",
gszFileInfo,
sizeof(gszFileInfo),
szFileIniConfig);
DisableSystemMenuItems(hWndDlg, FALSE);
if(gbShowDownloadRetryMsg)
SetDlgItemText(hWndDlg, IDC_MESSAGE0, diDownload.szMessageRetry0);
else
SetDlgItemText(hWndDlg, IDC_MESSAGE0, diDownload.szMessageDownload0);
EnableWindow(GetDlgItem(hWndDlg, IDRESUME), FALSE);
SetDlgItemText(hWndDlg, IDC_STATIC1, sgInstallGui.szStatus);
SetDlgItemText(hWndDlg, IDC_STATIC2, sgInstallGui.szFile);
SetDlgItemText(hWndDlg, IDC_STATIC4, sgInstallGui.szTo);
SetDlgItemText(hWndDlg, IDC_STATIC3, sgInstallGui.szUrl);
SetDlgItemText(hWndDlg, IDCANCEL, sgInstallGui.szCancel_);
SetDlgItemText(hWndDlg, IDPAUSE, sgInstallGui.szPause_);
SetDlgItemText(hWndDlg, IDRESUME, sgInstallGui.szResume_);
SendDlgItemMessage (hWndDlg, IDC_STATIC1, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_STATIC2, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_STATIC3, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDCANCEL, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDPAUSE, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDRESUME, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_MESSAGE0, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_STATUS_STATUS, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_STATUS_FILE, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_STATUS_URL, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
SendDlgItemMessage (hWndDlg, IDC_STATUS_TO, WM_SETFONT, (WPARAM)sgInstallGui.definedFont, 0L);
RepositionWindow(hWndDlg, BANNER_IMAGE_DOWNLOAD);
ClosePreviousDialog();
return FALSE;
case WM_SIZE:
switch(wParam)
{
case SIZE_MINIMIZED:
SetMinimizedDownloadTitle((int)GetPercentSoFar());
gbDlgDownloadMinimized = TRUE;
gbDlgDownloadJustMinimized = TRUE;
break;
case SIZE_RESTORED:
SetStatusUrl();
SetRestoredDownloadTitle();
gbDlgDownloadMinimized = FALSE;
break;
}
return(FALSE);
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDCANCEL:
if(AskCancelDlg(hWndDlg))
gdwDownloadDialogStatus = CS_CANCEL;
break;
case IDPAUSE:
if(!gtiPaused.bTickStarted)
{
gtiPaused.dwTickBegin = GetTickCount();
gtiPaused.bTickStarted = TRUE;
gtiPaused.bTickDownloadResumed = FALSE;
}
EnableWindow(GetDlgItem(hWndDlg, IDPAUSE), FALSE);
EnableWindow(GetDlgItem(hWndDlg, IDRESUME), TRUE);
gdwDownloadDialogStatus = CS_PAUSE;
break;
case IDRESUME:
gtiPaused.dwTickEnd = GetTickCount();
gtiPaused.dwTickDif = GetTickDif(gtiPaused.dwTickEnd,
gtiPaused.dwTickBegin);
gtiPaused.bTickDownloadResumed = TRUE;
EnableWindow(GetDlgItem(hWndDlg, IDRESUME), FALSE);
EnableWindow(GetDlgItem(hWndDlg, IDPAUSE), TRUE);
gdwDownloadDialogStatus = CS_NONE;
break;
default:
break;
}
return(TRUE);
}
return(FALSE); // didn't handle the message
}
// This routine will update the File Gauge progress bar to the specified percentage
// (value between 0 and 100)
static void
UpdateGaugeFileProgressBar(double value)
{
int nBars;
static long lModLastValue = 0;
if(sgProduct.mode != SILENT)
{
if(!CheckInterval(&lModLastValue, UPDATE_INTERVAL_PROGRESS_BAR))
return;
// Figure out how many bars should be displayed
nBars = (int)(dlgInfo.nMaxFileBars * value / 100);
// Only paint if we need to display more bars
if((nBars > dlgInfo.nFileBars) || (dlgInfo.nFileBars == 0))
{
HWND hWndGauge = GetDlgItem(dlgInfo.hWndDlg, IDC_GAUGE_FILE);
RECT rect;
// Update the gauge state before painting
dlgInfo.nFileBars = nBars;
// Only invalidate the part that needs updating
GetClientRect(hWndGauge, &rect);
InvalidateRect(hWndGauge, &rect, FALSE);
// Update the whole extracting dialog. We do this because we don't
// have a message loop to process WM_PAINT messages in case the
// extracting dialog was exposed
UpdateWindow(dlgInfo.hWndDlg);
}
}
}
// Draws a recessed border around the gauge
static void
DrawGaugeBorder(HWND hWnd)
{
HDC hDC = GetWindowDC(hWnd);
RECT rect;
int cx, cy;
HPEN hShadowPen = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_BTNSHADOW));
HGDIOBJ hOldPen;
GetWindowRect(hWnd, &rect);
cx = rect.right - rect.left;
cy = rect.bottom - rect.top;
// Draw a dark gray line segment
hOldPen = SelectObject(hDC, (HGDIOBJ)hShadowPen);
MoveToEx(hDC, 0, cy - 1, NULL);
LineTo(hDC, 0, 0);
LineTo(hDC, cx - 1, 0);
// Draw a white line segment
SelectObject(hDC, GetStockObject(WHITE_PEN));
MoveToEx(hDC, 0, cy - 1, NULL);
LineTo(hDC, cx - 1, cy - 1);
LineTo(hDC, cx - 1, 0);
SelectObject(hDC, hOldPen);
DeleteObject(hShadowPen);
ReleaseDC(hWnd, hDC);
}
// Draws the progress bar
static void
DrawProgressBar(HWND hWnd, int nBars)
{
int i;
PAINTSTRUCT ps;
HDC hDC;
RECT rect;
HBRUSH hBrush;
hDC = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
if(nBars <= 0)
{
/* clear the bars */
hBrush = CreateSolidBrush(GetSysColor(COLOR_MENU));
FillRect(hDC, &rect, hBrush);
}
else
{
// Draw the bars
hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
rect.left = rect.top = BAR_LIBXPNET_MARGIN;
rect.bottom -= BAR_LIBXPNET_MARGIN;
rect.right = rect.left + BAR_LIBXPNET_WIDTH;
for(i = 0; i < nBars; i++)
{
RECT dest;
if(IntersectRect(&dest, &ps.rcPaint, &rect))
FillRect(hDC, &rect, hBrush);
OffsetRect(&rect, BAR_LIBXPNET_WIDTH + BAR_LIBXPNET_SPACING, 0);
}
}
DeleteObject(hBrush);
EndPaint(hWnd, &ps);
}
// Adjusts the width of the gauge based on the maximum number of bars
static void
SizeToFitGauge(HWND hWnd, int nMaxBars)
{
RECT rect;
int cx;
// Get the window size in pixels
GetWindowRect(hWnd, &rect);
// Size the width to fit
cx = 2 * GetSystemMetrics(SM_CXBORDER) + 2 * BAR_LIBXPNET_MARGIN +
nMaxBars * BAR_LIBXPNET_WIDTH + (nMaxBars - 1) * BAR_LIBXPNET_SPACING;
SetWindowPos(hWnd, NULL, -1, -1, cx, rect.bottom - rect.top,
SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}
// Window proc for Download gauge
BOOL CALLBACK
GaugeDownloadWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
DWORD dwStyle;
RECT rect;
switch(msg)
{
case WM_NCCREATE:
dwStyle = GetWindowLong(hWnd, GWL_STYLE);
SetWindowLong(hWnd, GWL_STYLE, dwStyle | WS_BORDER);
return(TRUE);
case WM_CREATE:
// Figure out the maximum number of bars that can be displayed
GetClientRect(hWnd, &rect);
dlgInfo.nFileBars = 0;
dlgInfo.nMaxFileBars = (rect.right - rect.left - 2 * BAR_LIBXPNET_MARGIN + BAR_LIBXPNET_SPACING) / (BAR_LIBXPNET_WIDTH + BAR_LIBXPNET_SPACING);
// Size the gauge to exactly fit the maximum number of bars
SizeToFitGauge(hWnd, dlgInfo.nMaxFileBars);
return(FALSE);
case WM_NCPAINT:
DrawGaugeBorder(hWnd);
return(FALSE);
case WM_PAINT:
DrawProgressBar(hWnd, dlgInfo.nFileBars);
return(FALSE);
}
return(DefWindowProc(hWnd, msg, wParam, lParam));
}
void InitDownloadDlg(void)
{
WNDCLASS wc;
if(sgProduct.mode != SILENT)
{
memset(&wc, 0, sizeof(wc));
wc.style = CS_GLOBALCLASS;
wc.hInstance = hInst;
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpfnWndProc = (WNDPROC)GaugeDownloadWndProc;
wc.lpszClassName = "GaugeFile";
RegisterClass(&wc);
// Display the dialog box
dlgInfo.hWndDlg = CreateDialog(hSetupRscInst, MAKEINTRESOURCE(DLG_DOWNLOADING), hWndMain, (DLGPROC)DownloadDlgProc);
UpdateWindow(dlgInfo.hWndDlg);
UpdateGaugeFileProgressBar(0);
}
}
void DeInitDownloadDlg()
{
if(sgProduct.mode != SILENT)
{
SaveWindowPosition(dlgInfo.hWndDlg);
DestroyWindow(dlgInfo.hWndDlg);
UnregisterClass("GaugeFile", hInst);
}
}
|