DlgWOLWait.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/Commando/DlgWOLWait.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * Denzil E. Long, Jr.
  27. * $Author: Bhayes $
  28. *
  29. * VERSION INFO
  30. * $Revision: 21 $
  31. * $Modtime: 2/18/02 7:57p $
  32. *
  33. ******************************************************************************/
  34. #include "DlgWOLWait.h"
  35. #include "DlgMessageBox.h"
  36. #include "Resource.h"
  37. #include "String_ids.h"
  38. #include <WWOnline\WOLSession.h>
  39. #include <WWUI\MouseMgr.h>
  40. #include <WWTranslateDB\TranslateDB.h>
  41. #include <WWDebug\WWDebug.h>
  42. #include "gamespyadmin.h"
  43. #ifdef _MSC_VER
  44. #pragma warning (push,3)
  45. #endif
  46. #include "systimer.h"
  47. #ifdef _MSC_VER
  48. #pragma warning (pop)
  49. #endif
  50. using namespace WWOnline;
  51. DlgWOLWait* DlgWOLWait::mTheInstance = NULL;
  52. /******************************************************************************
  53. *
  54. * NAME
  55. * DlgWOLWait::DoDialog
  56. *
  57. * DESCRIPTION
  58. *
  59. * INPUTS
  60. * Title - Title of dialog
  61. * Wait - Wait condition to process.
  62. * Observer - DlgWOLWaitEvent observer
  63. *
  64. * RESULT
  65. * Success - True if dialog successfully started.
  66. *
  67. ******************************************************************************/
  68. bool DlgWOLWait::DoDialog(const WCHAR* title, RefPtr<WaitCondition>& wait,
  69. Observer<DlgWOLWaitEvent>* observer, unsigned long timeout, unsigned long dialog_timeout)
  70. {
  71. if (wait.IsValid())
  72. {
  73. DlgWOLWait* popup = new DlgWOLWait(wait, timeout, dialog_timeout);
  74. if (popup)
  75. {
  76. WWDEBUG_SAY(("DlgWOLWait: Starting dialog '%S'\n", title));
  77. popup->Start_Dialog();
  78. popup->Set_Title(title);
  79. if (observer)
  80. {
  81. popup->AddObserver(*observer);
  82. }
  83. popup->Release_Ref();
  84. }
  85. return (popup != NULL);
  86. }
  87. return false;
  88. }
  89. /******************************************************************************
  90. *
  91. * NAME
  92. * DlgWOLWait::DoDialog
  93. *
  94. * DESCRIPTION
  95. *
  96. * INPUTS
  97. * Title - Title of dialog
  98. * Wait - Wait condition to process.
  99. * Observer - DlgWOLWaitEvent observer
  100. *
  101. * RESULT
  102. * Success - True if dialog successfully started.
  103. *
  104. ******************************************************************************/
  105. bool DlgWOLWait::DoDialog(const WCHAR* title, const WCHAR* button_text, RefPtr<WaitCondition>& wait,
  106. Observer<DlgWOLWaitEvent>* observer, unsigned long timeout, unsigned long dialog_timeout)
  107. {
  108. if (wait.IsValid())
  109. {
  110. DlgWOLWait* popup = new DlgWOLWait(wait, timeout, dialog_timeout);
  111. if (popup)
  112. {
  113. WWDEBUG_SAY(("DlgWOLWait: Starting dialog '%S'\n", title));
  114. popup->Start_Dialog();
  115. popup->Set_Dlg_Item_Text(IDCANCEL, button_text);
  116. popup->Set_Title(title);
  117. if (observer)
  118. {
  119. popup->AddObserver(*observer);
  120. }
  121. popup->Release_Ref();
  122. }
  123. return (popup != NULL);
  124. }
  125. return false;
  126. }
  127. /******************************************************************************
  128. *
  129. * NAME
  130. * DlgWOLWait::DoDialog
  131. *
  132. * DESCRIPTION
  133. *
  134. * INPUTS
  135. * Title - Title ID of dialog
  136. * Wait - Wait condition to process.
  137. * Observer - DlgWOLWaitEvent observer
  138. *
  139. * RESULT
  140. * Success - True if dialog successfully started.
  141. *
  142. ******************************************************************************/
  143. bool DlgWOLWait::DoDialog(int titleID, RefPtr<WaitCondition>& wait,
  144. Observer<DlgWOLWaitEvent>* observer, unsigned long timeout, unsigned long dialog_timeout)
  145. {
  146. const WCHAR* title = TranslateDBClass::Get_String(titleID);
  147. return DoDialog(title, wait, observer, timeout, dialog_timeout);
  148. }
  149. /******************************************************************************
  150. *
  151. * NAME
  152. * DlgWOLWait::DlgWOLWait
  153. *
  154. * DESCRIPTION
  155. * Constructor
  156. *
  157. * INPUTS
  158. * Wait - Wait condition to process.
  159. *
  160. * RESULT
  161. * NONE
  162. *
  163. ******************************************************************************/
  164. DlgWOLWait::DlgWOLWait(RefPtr<WaitCondition>& wait, unsigned long timeout, unsigned long dialog_timeout) :
  165. PopupDialogClass(IDD_WOL_WAIT),
  166. mWait(wait),
  167. mStartTime(0),
  168. mTimeout(timeout),
  169. mDialogTimeout(dialog_timeout),
  170. mShowDialog(false)
  171. {
  172. mTheInstance = this;
  173. }
  174. /******************************************************************************
  175. *
  176. * NAME
  177. * DlgWOLWait::~DlgWOLWait
  178. *
  179. * DESCRIPTION
  180. * Destructor
  181. *
  182. * INPUTS
  183. * NONE
  184. *
  185. * RESULT
  186. * NONE
  187. *
  188. ******************************************************************************/
  189. DlgWOLWait::~DlgWOLWait()
  190. {
  191. mTheInstance = NULL;
  192. }
  193. /******************************************************************************
  194. *
  195. * NAME
  196. * DlgWOLWait::On_Init_Dialog
  197. *
  198. * DESCRIPTION
  199. * One time dialog initialization.
  200. *
  201. * INPUTS
  202. * NONE
  203. *
  204. * RESULT
  205. * NONE
  206. *
  207. ******************************************************************************/
  208. void DlgWOLWait::On_Init_Dialog(void)
  209. {
  210. if (mWait.IsValid())
  211. {
  212. if (!cGameSpyAdmin::Is_Gamespy_Game()) {
  213. mWOLSession = Session::GetInstance(false);
  214. WWASSERT(mWOLSession.IsValid());
  215. }
  216. // Start the wait condition
  217. mWait->WaitBeginning();
  218. // Set the dialogs wait text
  219. Set_Dlg_Item_Text(IDC_WAITTEXT, mWait->GetWaitText());
  220. mStartTime = TIMEGETTIME();
  221. if (mDialogTimeout != SHOW_NEVER)
  222. {
  223. // Initially the dialog is not shown so bring up the wait cursor.
  224. mShowDialog = false;
  225. MouseMgrClass::Begin_Wait_Cursor();
  226. }
  227. }
  228. PopupDialogClass::On_Init_Dialog();
  229. }
  230. /******************************************************************************
  231. *
  232. * NAME
  233. * DlgWOLWait::On_Destroy
  234. *
  235. * DESCRIPTION
  236. *
  237. * INPUTS
  238. * NONE
  239. *
  240. * RESULT
  241. * NONE
  242. *
  243. ******************************************************************************/
  244. void DlgWOLWait::On_Destroy(void)
  245. {
  246. WWDEBUG_SAY(("DlgWOLWait: Ending dialog '%S'\n", (const WCHAR*)Title));
  247. if (!mShowDialog && mDialogTimeout != SHOW_NEVER)
  248. {
  249. MouseMgrClass::End_Wait_Cursor();
  250. }
  251. }
  252. /******************************************************************************
  253. *
  254. * NAME
  255. * DlgWOLWait::On_Periodic
  256. *
  257. * DESCRIPTION
  258. *
  259. * INPUTS
  260. * NONE
  261. *
  262. * RESULT
  263. * NONE
  264. *
  265. ******************************************************************************/
  266. void DlgWOLWait::On_Periodic(void)
  267. {
  268. Add_Ref();
  269. CheckCondition();
  270. PopupDialogClass::On_Periodic();
  271. Release_Ref();
  272. }
  273. /******************************************************************************
  274. *
  275. * NAME
  276. * DlgWOLWait::CheckCondition
  277. *
  278. * DESCRIPTION
  279. * Check the condition of the wait.
  280. *
  281. * INPUTS
  282. * NONE
  283. *
  284. * RESULT
  285. * NONE
  286. *
  287. ******************************************************************************/
  288. void DlgWOLWait::CheckCondition(void)
  289. {
  290. // Quit if there is nothing to wait on or there is not WWOnline session.
  291. if (!mWait.IsValid() || (!mWOLSession.IsValid() && !cGameSpyAdmin::Is_Gamespy_Game()))
  292. {
  293. End_Dialog();
  294. return;
  295. }
  296. // Check the status of the wait condition
  297. WaitCondition::WaitResult waitStatus = mWait->GetResult();
  298. // If we are no longer waiting then process the result.
  299. if (waitStatus != WaitCondition::Waiting)
  300. {
  301. // If there are no observers and the wait timed out or errored then
  302. // show a message dialog describing the failure.
  303. if ((Notifier<DlgWOLWaitEvent>::HasObservers() == false)
  304. && ((waitStatus == WaitCondition::TimeOut) || (waitStatus == WaitCondition::Error)))
  305. {
  306. DlgMsgBox::DoDialog(mWait->GetWaitText(), mWait->GetResultText());
  307. }
  308. // Notify the observers about the status of the wait.
  309. Add_Ref();
  310. DlgWOLWaitEvent event(waitStatus, mWait.ReferencedObject());
  311. NotifyObservers(event);
  312. Release_Ref();
  313. End_Dialog();
  314. return;
  315. }
  316. // Change the waiting text if necessary.
  317. const WCHAR* text = Get_Dlg_Item_Text(IDC_WAITTEXT);
  318. const WideStringClass& waitText = mWait->GetWaitText();
  319. if (waitText.Compare_No_Case(text) != 0)
  320. {
  321. Set_Dlg_Item_Text(IDC_WAITTEXT, waitText);
  322. PopupDialogClass::Build_Background_Renderers();
  323. }
  324. // Watch for timeout
  325. unsigned long currTime = TIMEGETTIME();
  326. unsigned long timeout = mTimeout;
  327. unsigned long dialog_timeout = mDialogTimeout;
  328. if (dialog_timeout == 0)
  329. {
  330. dialog_timeout = 2000;
  331. }
  332. // If the dialog timeout is 0 then use the wait conditions timeout.
  333. if (timeout == 0)
  334. {
  335. timeout = mWait->GetTimeout();
  336. }
  337. if ((currTime - mStartTime) > timeout)
  338. {
  339. mWait->EndWait(WaitCondition::TimeOut, TRANSLATE(IDS_WOL_TIMEDOUT));
  340. }
  341. // Do not render until sufficient time has elapsed. This will prevent the
  342. // dialog from flashing in and out when the wait condition is satisfied quickly.
  343. if (!mShowDialog && mStartTime && dialog_timeout != SHOW_NEVER && ((currTime - mStartTime) > 2000))
  344. {
  345. mShowDialog = true;
  346. MouseMgrClass::End_Wait_Cursor();
  347. }
  348. // Allow time for WWOnline processing.
  349. if (mWOLSession.IsValid()) mWOLSession->Process();
  350. }
  351. /******************************************************************************
  352. *
  353. * NAME
  354. * DlgWOLWait::On_Command
  355. *
  356. * DESCRIPTION
  357. * Process command message
  358. *
  359. * INPUTS
  360. * Ctrl - ID of control
  361. * Message -
  362. * Param -
  363. *
  364. * RESULT
  365. * NONE
  366. *
  367. ******************************************************************************/
  368. void DlgWOLWait::On_Command(int ctrl, int message, DWORD param)
  369. {
  370. if ((ctrl == IDCANCEL) && mWait.IsValid())
  371. {
  372. WWDEBUG_SAY(("DlgWOLWait: UserAborted '%S'\n", (const WCHAR*)mWait->GetWaitText()));
  373. mWait->EndWait(WaitCondition::UserCancel, TRANSLATE(IDS_WOL_CANCELED));
  374. }
  375. }
  376. /******************************************************************************
  377. *
  378. * NAME
  379. * DlgWOLWait::Render
  380. *
  381. * DESCRIPTION
  382. *
  383. * INPUTS
  384. * NONE
  385. *
  386. * RESULT
  387. * NONE
  388. *
  389. ******************************************************************************/
  390. void DlgWOLWait::Render(void)
  391. {
  392. // Do not render until sufficient time has elapsed. This will prevent the
  393. // dialog from flashing in and out when the wait condition is satisfied quickly.
  394. if (mShowDialog)
  395. {
  396. PopupDialogClass::Render();
  397. }
  398. }