WOLDownload.cpp 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/WWOnline/WOLDownload.cpp $
  22. *
  23. * DESCRIPTION
  24. * This class specifies patch files to be downloaded from the server.
  25. *
  26. * PROGRAMMER
  27. * $Author: Denzil_l $
  28. *
  29. * VERSION INFO
  30. * $Revision: 8 $
  31. * $Modtime: 1/12/02 9:26p $
  32. *
  33. ******************************************************************************/
  34. #include <WWLib\Always.h>
  35. #include "WOLDownload.h"
  36. #include "WOLProduct.h"
  37. #include "WOLErrorUtil.h"
  38. #include "WOLString.h"
  39. #include <WOLAPI\DownloadDefs.h>
  40. #include <WWLib\WWString.h>
  41. #include <WWDebug\WWDebug.h>
  42. namespace WWOnline {
  43. /******************************************************************************
  44. *
  45. * NAME
  46. * Download::Create
  47. *
  48. * DESCRIPTION
  49. * Create a new download.
  50. *
  51. * INPUTS
  52. * Download - IDownload object.
  53. * Update - WOL update description.
  54. *
  55. * RESULT
  56. * Download - Reference to new download instance.
  57. *
  58. ******************************************************************************/
  59. RefPtr<Download> Download::Create(const WOL::Update& update)
  60. {
  61. return new Download(update);
  62. }
  63. /******************************************************************************
  64. *
  65. * NAME
  66. * Download::Download
  67. *
  68. * DESCRIPTION
  69. * Constructor
  70. *
  71. * INPUTS
  72. * Download - IDownload object.
  73. * Update - WOL update description.
  74. *
  75. * RESULT
  76. * NONE
  77. *
  78. ******************************************************************************/
  79. Download::Download(const WOL::Update& update) :
  80. mDownloadCookie(0),
  81. mState(DLPending),
  82. mStatusCode(DOWNLOADSTATUS_DONE),
  83. mErrorCode(0),
  84. mErrorText(NULL),
  85. mBytesRead(0),
  86. mTotalSize(0),
  87. mTimeElapsed(0),
  88. mTimeRemaining(0)
  89. {
  90. memcpy(&mWOLUpdate, &update, sizeof(mWOLUpdate));
  91. mWOLUpdate.next = NULL;
  92. WWDEBUG_SAY(("WOL: Download instantiated '%s'\n", GetFilename()));
  93. }
  94. /******************************************************************************
  95. *
  96. * NAME
  97. * Download::~Download
  98. *
  99. * DESCRIPTION
  100. * Destructor
  101. *
  102. * INPUTS
  103. * NONE
  104. *
  105. * RESULT
  106. * NONE
  107. *
  108. ******************************************************************************/
  109. Download::~Download()
  110. {
  111. WWDEBUG_SAY(("WOL: Download destructing '%s'\n", GetFilename()));
  112. Stop();
  113. }
  114. /******************************************************************************
  115. *
  116. * NAME
  117. * Download::CreateDownloadObject
  118. *
  119. * DESCRIPTION
  120. *
  121. * INPUTS
  122. * NONE
  123. *
  124. * RESULT
  125. * True if successful.
  126. *
  127. ******************************************************************************/
  128. bool Download::CreateDownloadObject(void)
  129. {
  130. //---------------------------------------------------------------------------
  131. // Create Download object
  132. //---------------------------------------------------------------------------
  133. WWDEBUG_SAY(("WOL: Creating IID_IDownload object\n"));
  134. WOL::IDownload* downloadObject = NULL;
  135. HRESULT hr = CoCreateInstance(WOL::CLSID_Download, NULL, CLSCTX_INPROC_SERVER,
  136. WOL::IID_IDownload, (void **)&downloadObject);
  137. if (FAILED(hr))
  138. {
  139. WWDEBUG_SAY(("WOLERROR: Failed to create IID_IDownload\n"));
  140. return false;
  141. }
  142. mDownloadObject = downloadObject;
  143. // Register this download as an event sink for download events
  144. hr = AtlAdvise(mDownloadObject, this, WOL::IID_IDownloadEvent, &mDownloadCookie);
  145. if (FAILED(hr))
  146. {
  147. WWDEBUG_SAY(("WOLERROR: AltAdvise failed\n"));
  148. SetError(-1, "WOL_NOTINITIALIZED");
  149. mDownloadCookie = 0;
  150. return false;
  151. }
  152. return true;
  153. }
  154. /******************************************************************************
  155. *
  156. * NAME
  157. * Download::ReleaseDownloadObject
  158. *
  159. * DESCRIPTION
  160. *
  161. * INPUTS
  162. * NONE
  163. *
  164. * RESULT
  165. * NONE
  166. *
  167. ******************************************************************************/
  168. void Download::ReleaseDownloadObject(void)
  169. {
  170. // No longer listen to download events.
  171. if (mDownloadObject && mDownloadCookie != 0)
  172. {
  173. HRESULT hr = AtlUnadvise(mDownloadObject, WOL::IID_IDownloadEvent, mDownloadCookie);
  174. mDownloadCookie = 0;
  175. if (FAILED(hr))
  176. {
  177. WWDEBUG_SAY(("WOLERROR: Download AltUnadvise failed\n"));
  178. }
  179. }
  180. mDownloadObject.Release();
  181. }
  182. /******************************************************************************
  183. *
  184. * NAME
  185. * Download::Start
  186. *
  187. * DESCRIPTION
  188. * Start downloading file.
  189. *
  190. * INPUTS
  191. * NONE
  192. *
  193. * RESULT
  194. * True if download started successfully.
  195. *
  196. ******************************************************************************/
  197. bool Download::Start(void)
  198. {
  199. // If the file was already download successfully then we are done.
  200. if (DLComplete == mState)
  201. {
  202. OnEnd();
  203. return true;
  204. }
  205. // Verify product is initialized
  206. RefPtr<Product> product = Product::Current();
  207. if (!product.IsValid())
  208. {
  209. WWDEBUG_SAY(("WOLERROR: Product not initialized\n"));
  210. SetError(-1, "WOL_NOTINITIALIZED");
  211. return false;
  212. }
  213. bool downloadCreated = CreateDownloadObject();
  214. if (!downloadCreated)
  215. {
  216. WWDEBUG_SAY(("WOLERROR: IDownload object not initialized\n"));
  217. SetError(-1, "WOL_NOTINITIALIZED");
  218. return false;
  219. }
  220. // Attempt to create the target path for the download file.
  221. const char* localPath = GetLocalPath();
  222. int dirCreated = CreateDirectory(localPath, NULL);
  223. if (!dirCreated && (ERROR_ALREADY_EXISTS != GetLastError()))
  224. {
  225. WWDEBUG_SAY(("WOLERROR: Failed to create download directory '%s'\n", localPath));
  226. Print_Win32Error(GetLastError());
  227. SetError(DOWNLOADEVENT_LOCALFILEOPENFAILED, GetOnErrorText(DOWNLOADEVENT_LOCALFILEOPENFAILED));
  228. return false;
  229. }
  230. // Generate full pathname for source and target
  231. const char* downloadPath = GetDownloadPath();
  232. const char* filename = GetFilename();
  233. StringClass localFile(true);
  234. localFile.Format("%s\\%s", localPath, filename);
  235. StringClass downloadFile(true);
  236. downloadFile.Format("%s\\%s", downloadPath, filename);
  237. // Initiate download of file.
  238. const char* server = GetServerName();
  239. const char* login = GetLoginName();
  240. const char* password = GetPassword();
  241. const char* regPath = product->GetRegistryPath();
  242. WWDEBUG_SAY(("WOL: Downloading '%s' to '%s'\n", (const char*)downloadFile, (const char*)localFile));
  243. HRESULT hr = mDownloadObject->DownloadFile(server, login, password, downloadFile, localFile, regPath);
  244. if (FAILED(hr))
  245. {
  246. WWDEBUG_SAY(("WOLERROR: DownloadFile() HRESULT = %s\n", GetDownloadErrorString(hr)));
  247. AtlUnadvise(mDownloadObject, WOL::IID_IDownloadEvent, mDownloadCookie);
  248. SetError(DOWNLOADEVENT_COULDNOTCONNECT, GetOnErrorText(DOWNLOADEVENT_COULDNOTCONNECT));
  249. return false;
  250. }
  251. mState = DLDownloading;
  252. DownloadEvent event(DownloadEvent::DOWNLOAD_BEGIN, this);
  253. NotifyObservers(event);
  254. return true;
  255. }
  256. /******************************************************************************
  257. *
  258. * NAME
  259. * Download::Stop
  260. *
  261. * DESCRIPTION
  262. * Stop the download in progress.
  263. *
  264. * INPUTS
  265. * NONE
  266. *
  267. * RESULT
  268. * NONE
  269. *
  270. ******************************************************************************/
  271. void Download::Stop(void)
  272. {
  273. if (mDownloadObject)
  274. {
  275. // If the download is in progress then stop it.
  276. if (DLDownloading == mState)
  277. {
  278. mState = DLAborted;
  279. HRESULT hr = mDownloadObject->Abort();
  280. if (FAILED(hr))
  281. {
  282. WWDEBUG_SAY(("WOLERROR: WOL::IDownload::Abort() HRESULT = %s\n", GetDownloadErrorString(hr)));
  283. }
  284. DownloadEvent event(DownloadEvent::DOWNLOAD_STOPPED, this);
  285. NotifyObservers(event);
  286. }
  287. ReleaseDownloadObject();
  288. }
  289. }
  290. /******************************************************************************
  291. *
  292. * NAME
  293. * Download::IsDone
  294. *
  295. * DESCRIPTION
  296. * Check if the download has finished.
  297. *
  298. * INPUTS
  299. * NONE
  300. *
  301. * RESULT
  302. * True if download is finished.
  303. *
  304. ******************************************************************************/
  305. bool Download::IsDone(void) const
  306. {
  307. return (DLComplete == mState);
  308. }
  309. /******************************************************************************
  310. *
  311. * NAME
  312. * Download::Process
  313. *
  314. * DESCRIPTION
  315. * Yield time for the download to process.
  316. *
  317. * INPUTS
  318. * NONE
  319. *
  320. * RESULT
  321. * NONE
  322. *
  323. ******************************************************************************/
  324. void Download::Process(void)
  325. {
  326. if (mDownloadObject)
  327. {
  328. mDownloadObject->PumpMessages();
  329. }
  330. }
  331. /******************************************************************************
  332. *
  333. * NAME
  334. * Download::GetStatusText
  335. *
  336. * DESCRIPTION
  337. * Get a text description of the current download status.
  338. *
  339. * INPUTS
  340. * NONE
  341. *
  342. * RESULT
  343. * Status - Text description of status.
  344. *
  345. ******************************************************************************/
  346. const wchar_t* Download::GetStatusText(void) const
  347. {
  348. static const char* _statusText[] =
  349. {
  350. // DOWNLOADSTATUS_DONE
  351. "WOL_DOWNLOADDONE",
  352. // DOWNLOADSTATUS_GO
  353. "WOL_DOWNLOADGO",
  354. // DOWNLOADSTATUS_CONNECTING
  355. "WOL_DOWNLOADCONNECT",
  356. // DOWNLOADSTATUS_LOGGINGIN
  357. "WOL_DOWNLOADLOGIN",
  358. // DOWNLOADSTATUS_FINDINGFILE
  359. "WOL_DOWNLOADFIND",
  360. // DOWNLOADSTATUS_QUERYINGRESUME
  361. "WOL_DOWNLOADRESUME",
  362. // DOWNLOADSTATUS_DOWNLOADING
  363. "WOL_DOWNLOADING",
  364. // DOWNLOADSTATUS_DISCONNECTING
  365. "WOL_DOWNLOADDISCONNECT"
  366. };
  367. if ((mStatusCode >= 0) && (mStatusCode < ARRAY_SIZE(_statusText)))
  368. {
  369. return WOLSTRING(_statusText[mStatusCode]);
  370. }
  371. return L"";
  372. }
  373. const wchar_t* Download::GetErrorText(void) const
  374. {
  375. return WOLSTRING(mErrorText);
  376. }
  377. /******************************************************************************
  378. *
  379. * NAME
  380. * Download::GetOnErrorText
  381. *
  382. * DESCRIPTION
  383. * Get a text description of the current error condition.
  384. *
  385. * INPUTS
  386. * NONE
  387. *
  388. * RESULT
  389. * Error - Text description of error.
  390. *
  391. ******************************************************************************/
  392. const char* Download::GetOnErrorText(int onErrorCode) const
  393. {
  394. static const char* _onErrorText[] =
  395. {
  396. // DOWNLOADEVENT_NOSUCHSERVER
  397. "WOL_DOWNLOADNOSERVER",
  398. // DOWNLOADEVENT_COULDNOTCONNECT
  399. "WOL_DOWNLOADCONNECTERROR",
  400. // DOWNLOADEVENT_LOGINFAILED
  401. "WOL_DOWNLOADLOGINERROR",
  402. // DOWNLOADEVENT_NOSUCHFILE
  403. "WOL_DOWNLOADFNF",
  404. // DOWNLOADEVENT_LOCALFILEOPENFAILED
  405. "WOL_DOWNLOADOPENERROR",
  406. // DOWNLOADEVENT_TCPERROR
  407. "WOL_DOWNLOADIOERROR",
  408. // DOWNLOADEVENT_DISCONNECTERROR
  409. "WOL_DOWNLOADDISCONNECTERROR"
  410. };
  411. if ((onErrorCode >= 1) && (onErrorCode < ARRAY_SIZE(_onErrorText)))
  412. {
  413. return _onErrorText[onErrorCode - 1];
  414. }
  415. return "";
  416. }
  417. /******************************************************************************
  418. *
  419. * NAME
  420. * Download::SetError
  421. *
  422. * DESCRIPTION
  423. *
  424. * INPUTS
  425. * ErrorCode - Numerical error code.
  426. * ErrorText - Text description of error code.
  427. *
  428. * RESULT
  429. * NONE
  430. *
  431. ******************************************************************************/
  432. void Download::SetError(int errorCode, const char* errorText)
  433. {
  434. mState = DLError;
  435. mErrorCode = errorCode;
  436. mErrorText = errorText;
  437. }
  438. /******************************************************************************
  439. *
  440. * NAME
  441. * Download::GetProgress
  442. *
  443. * DESCRIPTION
  444. * Get the progress of the download.
  445. *
  446. * INPUTS
  447. * BytesRead - On Return; Number of bytes read.
  448. * TotalSize - On Return; Total number of bytes to download.
  449. * TimeElapsed - On Return; Time elapsed since download began.
  450. * TimeRemaining - On Return; Estimated time remaining for download.
  451. *
  452. * RESULT
  453. * NONE
  454. *
  455. ******************************************************************************/
  456. void Download::GetProgress(int& bytesRead, int& totalSize, int& timeElapsed, int& timeRemaining) const
  457. {
  458. bytesRead = mBytesRead;
  459. totalSize = mTotalSize;
  460. timeElapsed = mTimeElapsed;
  461. timeRemaining = mTimeRemaining;
  462. }
  463. /****************************************************************************
  464. *
  465. * NAME
  466. * IUnknown::QueryInterface
  467. *
  468. * DESCRIPTION
  469. *
  470. * INPUTS
  471. *
  472. * RESULT
  473. *
  474. ****************************************************************************/
  475. STDMETHODIMP Download::QueryInterface(const IID& iid, void** ppv)
  476. {
  477. if ((iid == IID_IUnknown) || (iid == WOL::IID_IDownloadEvent))
  478. {
  479. *ppv = static_cast<WOL::IDownloadEvent*>(this);
  480. }
  481. else
  482. {
  483. *ppv = NULL;
  484. return E_NOINTERFACE;
  485. }
  486. static_cast<IUnknown*>(*ppv)->AddRef();
  487. return S_OK;
  488. }
  489. /****************************************************************************
  490. *
  491. * NAME
  492. * IUnknown::AddRef
  493. *
  494. * DESCRIPTION
  495. *
  496. * INPUTS
  497. * NONE
  498. *
  499. * RESULT
  500. *
  501. ****************************************************************************/
  502. ULONG STDMETHODCALLTYPE Download::AddRef(void)
  503. {
  504. RefCounted::AddReference();
  505. return RefCounted::ReferenceCount();
  506. }
  507. /****************************************************************************
  508. *
  509. * NAME
  510. * IUnknown::Release
  511. *
  512. * DESCRIPTION
  513. *
  514. * INPUTS
  515. * NONE
  516. *
  517. * RESULT
  518. *
  519. ****************************************************************************/
  520. ULONG STDMETHODCALLTYPE Download::Release(void)
  521. {
  522. ULONG refCount = RefCounted::ReferenceCount();
  523. RefCounted::ReleaseReference();
  524. return --refCount;
  525. }
  526. /******************************************************************************
  527. *
  528. * NAME
  529. * Download::OnEnd
  530. *
  531. * DESCRIPTION
  532. * Notification that the download has completed.
  533. *
  534. * INPUTS
  535. * NONE
  536. *
  537. * RESULT
  538. *
  539. ******************************************************************************/
  540. STDMETHODIMP Download::OnEnd(void)
  541. {
  542. WWDEBUG_SAY(("WOL: Download End '%s'\n", GetFilename()));
  543. SetError(0, NULL);
  544. mState = DLComplete;
  545. DownloadEvent event(DownloadEvent::DOWNLOAD_END, this);
  546. NotifyObservers(event);
  547. return S_OK;
  548. }
  549. /******************************************************************************
  550. *
  551. * NAME
  552. * Download::OnError
  553. *
  554. * DESCRIPTION
  555. * Notification that the download has encountered an error.
  556. *
  557. * INPUTS
  558. *
  559. * RESULT
  560. *
  561. ******************************************************************************/
  562. STDMETHODIMP Download::OnError(int error)
  563. {
  564. WWDEBUG_SAY(("WOLERROR: Download '%s'\n", GetFilename()));
  565. #ifdef WWDEBUG
  566. static const char* _errors[] =
  567. {
  568. "DOWNLOADEVENT_NOSUCHSERVER",
  569. "DOWNLOADEVENT_COULDNOTCONNECT",
  570. "DOWNLOADEVENT_LOGINFAILED",
  571. "DOWNLOADEVENT_NOSUCHFILE",
  572. "DOWNLOADEVENT_LOCALFILEOPENFAILED",
  573. "DOWNLOADEVENT_TCPERROR",
  574. "DOWNLOADEVENT_DISCONNECTERROR"
  575. };
  576. WWDEBUG_SAY(("WOLERROR: Download Error: %s\n", _errors[error - 1]));
  577. #endif
  578. SetError(error, GetOnErrorText(error));
  579. DownloadEvent event(DownloadEvent::DOWNLOAD_ERROR, this);
  580. NotifyObservers(event);
  581. return S_OK;
  582. }
  583. /******************************************************************************
  584. *
  585. * NAME
  586. * Download::OnProgressUpdate
  587. *
  588. * DESCRIPTION
  589. * Notification of the downloads progress.
  590. *
  591. * INPUTS
  592. *
  593. * RESULT
  594. *
  595. ******************************************************************************/
  596. STDMETHODIMP Download::OnProgressUpdate(int bytesRead, int totalSize,
  597. int timeElapsed, int timeRemaining)
  598. {
  599. mBytesRead = bytesRead;
  600. mTotalSize = totalSize;
  601. mTimeElapsed = timeElapsed;
  602. mTimeRemaining = timeRemaining;
  603. DownloadEvent event(DownloadEvent::DOWNLOAD_PROGRESS, this);
  604. NotifyObservers(event);
  605. return S_OK;
  606. }
  607. /******************************************************************************
  608. *
  609. * NAME
  610. * DownloadObserver::OnQueryResume
  611. *
  612. * DESCRIPTION
  613. *
  614. * INPUTS
  615. *
  616. * RESULT
  617. *
  618. ******************************************************************************/
  619. STDMETHODIMP Download::OnQueryResume(void)
  620. {
  621. WWDEBUG_SAY(("WOL: Download QueryResume '%s'\n", GetFilename()));
  622. DownloadEvent event(DownloadEvent::DOWNLOAD_RESUME, this);
  623. NotifyObservers(event);
  624. return DOWNLOADEVENT_RESUME;
  625. }
  626. /******************************************************************************
  627. *
  628. * NAME
  629. * DownloadObserver::OnStatusUpdate
  630. *
  631. * DESCRIPTION
  632. * Notification of the downloads status change.
  633. *
  634. * INPUTS
  635. *
  636. * RESULT
  637. *
  638. ******************************************************************************/
  639. STDMETHODIMP Download::OnStatusUpdate(int status)
  640. {
  641. #ifdef WWDEBUG
  642. static const char* _status[] =
  643. {
  644. "DOWNLOADSTATUS_DONE",
  645. "DOWNLOADSTATUS_GO",
  646. "DOWNLOADSTATUS_CONNECTING",
  647. "DOWNLOADSTATUS_LOGGINGIN",
  648. "DOWNLOADSTATUS_FINDINGFILE",
  649. "DOWNLOADSTATUS_QUERYINGRESUME",
  650. "DOWNLOADSTATUS_DOWNLOADING",
  651. "DOWNLOADSTATUS_DISCONNECTING",
  652. };
  653. WWDEBUG_SAY(("WOL: Download Status: %s\n", _status[status]));
  654. #endif
  655. mStatusCode = status;
  656. DownloadEvent event(DownloadEvent::DOWNLOAD_STATUS, this);
  657. NotifyObservers(event);
  658. return S_OK;
  659. }
  660. //-----------------------------------------------------------------------------
  661. /******************************************************************************
  662. *
  663. * NAME
  664. * DownloadWait::Create
  665. *
  666. * DESCRIPTION
  667. * Create a wait condition to process downloads.
  668. *
  669. * INPUTS
  670. * List of download files.
  671. *
  672. * RESULT
  673. * Wait condition to process.
  674. *
  675. ******************************************************************************/
  676. RefPtr<DownloadWait> DownloadWait::Create(const DownloadList& files)
  677. {
  678. if (files.empty())
  679. {
  680. return NULL;
  681. }
  682. return new DownloadWait(files);
  683. }
  684. /******************************************************************************
  685. *
  686. * NAME
  687. * DownloadWait::DownloadWait
  688. *
  689. * DESCRIPTION
  690. * Constructor
  691. *
  692. * INPUTS
  693. * List of download files.
  694. *
  695. * RESULT
  696. * NONE
  697. *
  698. ******************************************************************************/
  699. DownloadWait::DownloadWait(const DownloadList& files) :
  700. SingleWait(WOLSTRING("WOL_DOWNLOADING")),
  701. mFiles(files),
  702. mFileIndex(-1),
  703. mCallback(NULL)
  704. {
  705. WWDEBUG_SAY(("WOL: DownloadWait Create\n"));
  706. }
  707. /******************************************************************************
  708. *
  709. * NAME
  710. * DownloadWait::~DownloadWait
  711. *
  712. * DESCRIPTION
  713. * Destructor
  714. *
  715. * INPUTS
  716. * NONE
  717. *
  718. * RESULT
  719. * NONE
  720. *
  721. ******************************************************************************/
  722. DownloadWait::~DownloadWait()
  723. {
  724. WWDEBUG_SAY(("WOL: DownloadWait End '%S'\n", mEndText));
  725. }
  726. /******************************************************************************
  727. *
  728. * NAME
  729. * DownloadWait::WaitBeginning
  730. *
  731. * DESCRIPTION
  732. * Begin download process.
  733. *
  734. * INPUTS
  735. * NONE
  736. *
  737. * RESULT
  738. * NONE
  739. *
  740. ******************************************************************************/
  741. void DownloadWait::WaitBeginning(void)
  742. {
  743. WWDEBUG_SAY(("WOL: DownloadWait Begin\n"));
  744. // If there are no files to download then just say we are done.
  745. if (mFiles.empty())
  746. {
  747. SingleWait::EndWait(ConditionMet, WOLSTRING("WOL_DOWNLOADDONE"));
  748. return;
  749. }
  750. // WaitBeginning should only be called once.
  751. if (mCurrentDownload.IsValid())
  752. {
  753. WWDEBUG_SAY(("WOLERROR: DownloadWait::WaitBeginning() called more that once"));
  754. WWASSERT("DownloadWait::WaitBeginning() called more that once");
  755. return;
  756. }
  757. // Start the first download
  758. mFileIndex = 0;
  759. mCurrentDownload = mFiles[0];
  760. Observer<DownloadEvent>::NotifyMe(*mCurrentDownload);
  761. bool started = mCurrentDownload->Start();
  762. if (!started)
  763. {
  764. SingleWait::EndWait(Error, mCurrentDownload->GetErrorText());
  765. }
  766. }
  767. /******************************************************************************
  768. *
  769. * NAME
  770. * DownloadWait::GetResult
  771. *
  772. * DESCRIPTION
  773. * Check the status of the download.
  774. * This must be called regularly for the wait condition to be processed.
  775. *
  776. * INPUTS
  777. * NONE
  778. *
  779. * RESULT
  780. *
  781. ******************************************************************************/
  782. WaitCondition::WaitResult DownloadWait::GetResult(void)
  783. {
  784. if (mFileIndex == -1)
  785. {
  786. return Waiting;
  787. }
  788. // Process the current download
  789. if (mEndResult == Waiting)
  790. {
  791. mCurrentDownload->Process();
  792. // If the current download is complete then start the next one.
  793. if (mCurrentDownload->IsDone())
  794. {
  795. Observer<DownloadEvent>::StopObserving();
  796. mCurrentDownload->Stop();
  797. mCurrentDownload.Release();
  798. // Advance to the next file.
  799. ++mFileIndex;
  800. // If there are more files then start the next one in sequence.
  801. if ((unsigned)mFileIndex < mFiles.size())
  802. {
  803. mCurrentDownload = mFiles[mFileIndex];
  804. WWASSERT(mCurrentDownload.IsValid());
  805. Observer<DownloadEvent>::NotifyMe(*mCurrentDownload);
  806. bool started = mCurrentDownload->Start();
  807. if (!started)
  808. {
  809. EndWait(Error, mCurrentDownload->GetErrorText());
  810. }
  811. }
  812. else
  813. {
  814. // If all of the files have downloaded successfully then the wait
  815. // condition has been met.
  816. EndWait(ConditionMet, WOLSTRING("WOL_DOWNLOADCOMPLETE"));
  817. }
  818. }
  819. }
  820. return mEndResult;
  821. }
  822. /******************************************************************************
  823. *
  824. * NAME
  825. * DownloadWait::EndWait
  826. *
  827. * DESCRIPTION
  828. * Terminate the wait condition.
  829. *
  830. * INPUTS
  831. *
  832. * RESULT
  833. * NONE
  834. *
  835. ******************************************************************************/
  836. void DownloadWait::EndWait(WaitResult endResult, const wchar_t* endText)
  837. {
  838. if ((ConditionMet != endResult) && mCurrentDownload.IsValid())
  839. {
  840. mCurrentDownload->Stop();
  841. }
  842. SingleWait::EndWait(endResult, endText);
  843. }
  844. /******************************************************************************
  845. *
  846. * NAME
  847. * DownloadWait::SetCallback
  848. *
  849. * DESCRIPTION
  850. *
  851. * INPUTS
  852. *
  853. * RESULT
  854. *
  855. ******************************************************************************/
  856. void DownloadWait::SetCallback(DownloadWaitCallback callback, unsigned long userdata)
  857. {
  858. mCallback = callback;
  859. mUserdata = userdata;
  860. }
  861. /******************************************************************************
  862. *
  863. * NAME
  864. * DownloadWait::DoCallback
  865. *
  866. * DESCRIPTION
  867. *
  868. * INPUTS
  869. *
  870. * RESULT
  871. *
  872. ******************************************************************************/
  873. void DownloadWait::DoCallback(DownloadEvent& event)
  874. {
  875. if (mCallback)
  876. {
  877. mCallback(event, mUserdata);
  878. }
  879. }
  880. /******************************************************************************
  881. *
  882. * NAME
  883. * DownloadWait::HandleNotification(DownloadEvent)
  884. *
  885. * DESCRIPTION
  886. *
  887. * INPUTS
  888. *
  889. * RESULT
  890. *
  891. ******************************************************************************/
  892. void DownloadWait::HandleNotification(DownloadEvent& event)
  893. {
  894. if (mEndResult == Waiting)
  895. {
  896. const RefPtr<Download>& download = event.GetDownload();
  897. WWASSERT(download.IsValid());
  898. WWASSERT(mCurrentDownload == download);
  899. switch (event.GetEvent())
  900. {
  901. case DownloadEvent::DOWNLOAD_ERROR:
  902. WWDEBUG_SAY(("WOLERROR: Download Error %S\n", download->GetErrorText()));
  903. EndWait(Error, download->GetErrorText());
  904. break;
  905. case DownloadEvent::DOWNLOAD_STATUS:
  906. mWaitText = download->GetStatusText();
  907. break;
  908. case DownloadEvent::DOWNLOAD_BEGIN:
  909. break;
  910. case DownloadEvent::DOWNLOAD_PROGRESS:
  911. break;
  912. case DownloadEvent::DOWNLOAD_END:
  913. WWDEBUG_SAY(("WOL: Download complete\n"));
  914. break;
  915. case DownloadEvent::DOWNLOAD_STOPPED:
  916. WWDEBUG_SAY(("WOL: Download stopped\n"));
  917. EndWait(UserCancel, WOLSTRING("WOL_DOWNLOADSTOPPED"));
  918. break;
  919. case DownloadEvent::DOWNLOAD_RESUME:
  920. WWDEBUG_SAY(("WOL: Download resumed\n"));
  921. mWaitText = WOLSTRING("WOL_DOWNLOADRESUME");
  922. break;
  923. default:
  924. break;
  925. }
  926. DoCallback(event);
  927. }
  928. }
  929. } // namespace WWOnline