Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

250 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include <stdio.h>
  8. #include "DialogAddBan.h"
  9. #include <vgui/ISurface.h>
  10. #include <KeyValues.h>
  11. #include <vgui_controls/Button.h>
  12. #include <vgui_controls/Label.h>
  13. #include <vgui_controls/TextEntry.h>
  14. #include <vgui_controls/CheckButton.h>
  15. #include <vgui_controls/MessageBox.h>
  16. using namespace vgui;
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Constructor
  19. //-----------------------------------------------------------------------------
  20. CDialogAddBan::CDialogAddBan(vgui::Panel *parent) : Frame(parent, "DialogAddBan")
  21. {
  22. SetSize(320, 200);
  23. SetTitle("#Game_Ban_Add_Title", false);
  24. m_pIDTextEntry = new TextEntry(this, "IDTextEntry");
  25. m_pOkayButton = new Button(this, "OkayButton", "#Okay_Button");
  26. m_pPermBanRadio = new RadioButton(this, "PermBanRadio", "#Add_Ban_Time_Permanent");
  27. m_pTempBanRadio = new RadioButton(this, "TempBanRadio", "#Add_Ban_Time_Temporary");
  28. m_pPermBanRadio->SetSelected(true);
  29. m_pTimeTextEntry = new TextEntry(this, "TimeTextEntry");
  30. m_pTimeCombo = new ComboBox(this, "TimeCombo",3,false);
  31. int defaultItem = m_pTimeCombo->AddItem("#Add_Ban_Period_Minutes", NULL);
  32. m_pTimeCombo->AddItem("#Add_Ban_Period_Hours", NULL);
  33. m_pTimeCombo->AddItem("#Add_Ban_Period_Days", NULL);
  34. m_pTimeCombo->ActivateItem(defaultItem);
  35. LoadControlSettings("Admin\\DialogAddBan.res", "PLATFORM");
  36. SetTitle("#Add_Ban_Title", true);
  37. SetSizeable(false);
  38. // set our initial position in the middle of the workspace
  39. MoveToCenterOfScreen();
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Destructor
  43. //-----------------------------------------------------------------------------
  44. CDialogAddBan::~CDialogAddBan()
  45. {
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: initializes the dialog and brings it to the foreground
  49. //-----------------------------------------------------------------------------
  50. void CDialogAddBan::Activate(const char *type,const char *player,const char *authid)
  51. {
  52. m_cType=type;
  53. m_pOkayButton->SetAsDefaultButton(true);
  54. MakePopup();
  55. MoveToFront();
  56. RequestFocus();
  57. m_pIDTextEntry->RequestFocus();
  58. SetVisible(true);
  59. SetTextEntry("PlayerTextEntry",player);
  60. SetTextEntry("IDTextEntry",authid);
  61. BaseClass::Activate();
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Sets the text of a labell by name
  65. //-----------------------------------------------------------------------------
  66. void CDialogAddBan::SetLabelText(const char *textEntryName, const char *text)
  67. {
  68. Label *entry = dynamic_cast<Label *>(FindChildByName(textEntryName));
  69. if (entry)
  70. {
  71. entry->SetText(text);
  72. }
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Purpose: Sets the text of a labell by name
  76. //-----------------------------------------------------------------------------
  77. void CDialogAddBan::SetTextEntry(const char *textEntryName, const char *text)
  78. {
  79. TextEntry *entry = dynamic_cast<TextEntry *>(FindChildByName(textEntryName));
  80. if (entry)
  81. {
  82. entry->SetText(text);
  83. }
  84. }
  85. bool CDialogAddBan::IsIPCheck()
  86. {
  87. char buf[64];
  88. int dotCount=0;
  89. m_pIDTextEntry->GetText(buf, sizeof(buf)-1);
  90. for(unsigned int i=0;i<strlen(buf);i++)
  91. {
  92. if(buf[i]=='.')
  93. {
  94. dotCount++;
  95. }
  96. }
  97. if(dotCount>0)
  98. {
  99. return true;
  100. }
  101. else
  102. {
  103. return false;
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose:
  108. //-----------------------------------------------------------------------------
  109. void CDialogAddBan::OnCommand(const char *command)
  110. {
  111. bool bClose = false;
  112. if (!stricmp(command, "Okay"))
  113. {
  114. KeyValues *msg = new KeyValues("AddBanValue");
  115. char buf[64],idbuf[64];
  116. float time;
  117. m_pIDTextEntry->GetText(idbuf, sizeof(idbuf));
  118. m_pTimeTextEntry->GetText(buf, 64);
  119. if(strlen(idbuf)<=0)
  120. {
  121. MessageBox *dlg = new MessageBox("#Add_Ban_Error", "#Add_Ban_ID_Invalid");
  122. dlg->DoModal();
  123. bClose=false;
  124. }
  125. else if(strlen(buf)<=0 && !m_pPermBanRadio->IsSelected())
  126. {
  127. MessageBox *dlg = new MessageBox("#Add_Ban_Error", "#Add_Ban_Time_Empty");
  128. dlg->DoModal();
  129. bClose=false;
  130. }
  131. else
  132. {
  133. if(m_pPermBanRadio->IsSelected())
  134. {
  135. time=0;
  136. }
  137. else
  138. {
  139. sscanf(buf,"%f",&time);
  140. m_pTimeCombo->GetText(buf,64);
  141. if(strstr(buf,"hour"))
  142. {
  143. time*=60;
  144. }
  145. else if(strstr(buf,"day"))
  146. {
  147. time*=(60*24);
  148. }
  149. if(time<0)
  150. {
  151. MessageBox *dlg = new MessageBox("#Add_Ban_Error", "#Add_Ban_Time_Invalid");
  152. dlg->DoModal();
  153. bClose=false;
  154. }
  155. }
  156. if(time>=0)
  157. {
  158. msg->SetFloat("time", time);
  159. msg->SetString("id", idbuf);
  160. msg->SetString("type",m_cType);
  161. msg->SetInt("ipcheck",IsIPCheck());
  162. PostActionSignal(msg);
  163. bClose = true;
  164. }
  165. }
  166. }
  167. else if (!stricmp(command, "Close"))
  168. {
  169. bClose = true;
  170. }
  171. else
  172. {
  173. BaseClass::OnCommand(command);
  174. }
  175. if (bClose)
  176. {
  177. Close();
  178. }
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose:
  182. //-----------------------------------------------------------------------------
  183. void CDialogAddBan::PerformLayout()
  184. {
  185. BaseClass::PerformLayout();
  186. }
  187. //-----------------------------------------------------------------------------
  188. // Purpose: deletes the dialog on close
  189. //-----------------------------------------------------------------------------
  190. void CDialogAddBan::OnClose()
  191. {
  192. BaseClass::OnClose();
  193. MarkForDeletion();
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Purpose: called when the perm/temp ban time radio buttons are pressed
  197. //-----------------------------------------------------------------------------
  198. void CDialogAddBan::OnButtonToggled(Panel *panel)
  199. {
  200. if (panel == m_pPermBanRadio)
  201. {
  202. m_pTimeTextEntry->SetEnabled(false);
  203. m_pTimeCombo->SetEnabled(false);
  204. }
  205. else
  206. {
  207. m_pTimeTextEntry->SetEnabled(true);
  208. m_pTimeCombo->SetEnabled(true);
  209. }
  210. Repaint();
  211. }