DlgLicenseInfo.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // DlgLicenseInfo.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "DlgLicenseInfo.h"
  5. #include "Resource.h"
  6. #include "afxdialogex.h"
  7. #include "OTSHelper.h"
  8. #include "COTSUtilityDllFunExport.h"
  9. namespace OTSMODEL {
  10. // show license info edit dialog
  11. // return true if true if DoModel return IDOK
  12. // "Edit" -- ShowEditDialog( oMyLicenseInfo)
  13. // "Add" -- ShowEditDialog( oMyLicenseInfo, TRUE)
  14. // "Request" -- ShowEditDialog( oMyLicenseInfo, FALSE, TRUE)
  15. BOOL ShowEditDialog(COTSLicenseInfo& a_oLicenseInfo, BOOL a_bAdd /*= FALSE*/, BOOL a_bRequest /*= FALSE*/)
  16. {
  17. COTSLicenseInfoPtr pLicenseInfo(new COTSLicenseInfo(a_oLicenseInfo));
  18. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  19. CDlgLicenseInfo dlg;
  20. dlg.SetLicenseInfo(pLicenseInfo);
  21. if (a_bAdd)
  22. {
  23. dlg.SetAddSwitch();
  24. }
  25. else if (a_bRequest)
  26. {
  27. dlg.SetRequestSwitch();
  28. }
  29. if (dlg.DoModal() == IDOK)
  30. {
  31. a_oLicenseInfo = *(pLicenseInfo.get());
  32. return TRUE;
  33. }
  34. return FALSE;
  35. }
  36. // CDlgLicenseInfo dialog
  37. IMPLEMENT_DYNAMIC(CDlgLicenseInfo, CDialog)
  38. CDlgLicenseInfo::CDlgLicenseInfo(CWnd* pParent /*=NULL*/)
  39. : CDialog(IDD_LICENSE_INFO, pParent)
  40. , m_strEditComputerNickName(_T(""))
  41. , m_strEditMachineId(_T(""))
  42. , m_nComboSoftPackId(0)
  43. , m_nCombeLicenseType(0)
  44. , m_oExpireData(
  45. COleDateTime::GetCurrentTime())
  46. , m_strEditLicenseKey(_T(""))
  47. , m_bAdd(FALSE)
  48. , m_bRequest(FALSE)
  49. {
  50. m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo());
  51. }
  52. CDlgLicenseInfo::~CDlgLicenseInfo()
  53. {
  54. }
  55. void CDlgLicenseInfo::DoDataExchange(CDataExchange* pDX)
  56. {
  57. CDialog::DoDataExchange(pDX);
  58. DDX_Control(pDX, IDC_EDIT_NICKNAME, m_ctrlEditComputerNickName);
  59. DDX_Text(pDX, IDC_EDIT_NICKNAME, m_strEditComputerNickName);
  60. DDX_Control(pDX, IDC_EDIT_MACHEINEID, m_ctrlEditMachineId);
  61. DDX_Text(pDX, IDC_EDIT_MACHEINEID, m_strEditMachineId);
  62. DDX_Control(pDX, IDC_COMBO_PACKID, m_ctrlComboSoftPackId);
  63. DDX_CBIndex(pDX, IDC_COMBO_PACKID, m_nComboSoftPackId);
  64. DDX_Control(pDX, IDC_COMBO_LICESETYPE, m_ctrlComboLicenseType);
  65. DDX_CBIndex(pDX, IDC_COMBO_LICESETYPE, m_nCombeLicenseType);
  66. DDX_Control(pDX, IDC_DATETIME_EXPIREDATE, m_ctrlDateTimeExpireData);
  67. DDX_DateTimeCtrl(pDX, IDC_DATETIME_EXPIREDATE, m_oExpireData);
  68. DDX_Control(pDX, IDC_EDIT_KEY, m_ctrlEditLicenseKey);
  69. DDX_Text(pDX, IDC_EDIT_KEY, m_strEditLicenseKey);
  70. DDX_Control(pDX, IDC_BUTTON_LOAD, m_ctrlButtonLoad);
  71. DDX_Control(pDX, IDOK, m_ctrlButtonSave);
  72. }
  73. BEGIN_MESSAGE_MAP(CDlgLicenseInfo, CDialog)
  74. ON_BN_CLICKED(IDC_BUTTON_LOAD, &CDlgLicenseInfo::OnBnClickedButtonLoad)
  75. ON_EN_CHANGE(IDC_EDIT_NICKNAME, &CDlgLicenseInfo::OnEnChangeEditNickname)
  76. ON_EN_CHANGE(IDC_EDIT_MACHEINEID, &CDlgLicenseInfo::OnEnChangeEditMacheineid)
  77. ON_CBN_SELCHANGE(IDC_COMBO_PACKID, &CDlgLicenseInfo::OnCbnSelchangeComboPackid)
  78. ON_CBN_SELCHANGE(IDC_COMBO_LICESETYPE, &CDlgLicenseInfo::OnCbnSelchangeComboLicensetype)
  79. ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIME_EXPIREDATE, &CDlgLicenseInfo::OnDtnDatetimechangeDatetimeExpiredate)
  80. ON_BN_CLICKED(IDOK, &CDlgLicenseInfo::OnBnClickedOk)
  81. END_MESSAGE_MAP()
  82. // CDlgLicenseInfo message handlers
  83. BOOL CDlgLicenseInfo::OnInitDialog()
  84. {
  85. CDialog::OnInitDialog();
  86. // set data time string format
  87. m_ctrlDateTimeExpireData.SetFormat(EXPIRE_DATA_CTRL_FORMAT);
  88. // set combo boxes
  89. for (int i = (int)OTS_SOFT_PACKAGE_ID::MIN; i <= (int)OTS_SOFT_PACKAGE_ID::MAX; ++i)
  90. {
  91. CString strItem = COTSLicMgr::GetSoftwarePackIdString((OTS_SOFT_PACKAGE_ID)i);
  92. m_ctrlComboSoftPackId.AddString(strItem);
  93. }
  94. for (int i = (int)OTS_LICENSE_TYPE::MIN; i <= (int)OTS_LICENSE_TYPE::MAX; ++i)
  95. {
  96. CString strItem = COTSLicMgr::GetLicenseTypeIdString((OTS_LICENSE_TYPE)i);
  97. m_ctrlComboLicenseType.AddString(strItem);
  98. }
  99. // control data with license info
  100. SetControlData(m_pLicenseInfo);
  101. // if this is called by request
  102. if (m_bRequest)
  103. {
  104. // change control appearance
  105. ChangeControlAppearance();
  106. }
  107. // set control status
  108. UpdateControlStatus(FALSE || m_bAdd || m_bRequest);
  109. return TRUE; // return TRUE unless you set the focus to a control
  110. // EXCEPTION: OCX Property Pages should return FALSE
  111. }
  112. // nick name change event
  113. void CDlgLicenseInfo::OnEnChangeEditNickname()
  114. {
  115. UpdateControlStatus();
  116. UpdateLicenseKeyStr();
  117. }
  118. // machine name change event
  119. void CDlgLicenseInfo::OnEnChangeEditMacheineid()
  120. {
  121. UpdateControlStatus();
  122. UpdateLicenseKeyStr();
  123. }
  124. // packed id change event
  125. void CDlgLicenseInfo::OnCbnSelchangeComboPackid()
  126. {
  127. UpdateControlStatus();
  128. UpdateLicenseKeyStr();
  129. }
  130. // license type change event
  131. void CDlgLicenseInfo::OnCbnSelchangeComboLicensetype()
  132. {
  133. UpdateControlStatus();
  134. UpdateLicenseKeyStr();
  135. }
  136. // expire data change event
  137. void CDlgLicenseInfo::OnDtnDatetimechangeDatetimeExpiredate(NMHDR *pNMHDR, LRESULT *pResult)
  138. {
  139. LPNMDATETIMECHANGE pDTChange = reinterpret_cast<LPNMDATETIMECHANGE>(pNMHDR);
  140. UpdateControlStatus();
  141. UpdateLicenseKeyStr();
  142. *pResult = 0;
  143. }
  144. // load button click event
  145. void CDlgLicenseInfo::OnBnClickedButtonLoad()
  146. {
  147. // dialog is from request call
  148. if (m_bRequest)
  149. {
  150. // get control data from dialog
  151. UpdateData();
  152. // generate license info string
  153. COTSLicenseInfoPtr a_pOTSLicenseInfo(new COTSLicenseInfo());
  154. a_pOTSLicenseInfo->SetComputerNickName(m_strEditComputerNickName);
  155. a_pOTSLicenseInfo->SetMachineId(m_strEditMachineId);
  156. a_pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId);
  157. a_pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType);
  158. a_pOTSLicenseInfo->SetExpireDate(m_oExpireData);
  159. CString strEmail = COTSLicMgr::GetLicenseInfoTextBody(a_pOTSLicenseInfo);
  160. strEmail.Trim();
  161. if (strEmail.IsEmpty())
  162. {
  163. //invalid email body
  164. LogErrorTrace(__FILE__, __LINE__, _T("OnBnClickedButtonLoad: email body is empty."));
  165. return;
  166. }
  167. // email license info string
  168. if (!COTSHelper::SendEmail(SUPPORT_EMAIL_ADDRESS, SUPPORT_EMAIL_TITLE, strEmail))
  169. {
  170. // email license info string, return if send email fails
  171. LogErrorTrace(__FILE__, __LINE__, _T("OnBnClickedButtonLoad: Send email fails."));
  172. return;
  173. }
  174. // get control data for the license info
  175. GetControlData(FALSE);
  176. // close dialog, DoModel() return IDOK
  177. CDialog::OnOK();
  178. }
  179. else
  180. {
  181. // load a license info text file
  182. CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, TEXTFILE_FILTER);
  183. if (dlg.DoModal() == IDOK)
  184. {
  185. // read all string lines from the file
  186. CString strPathName = dlg.GetPathName();
  187. // try to load license info from a text file
  188. COTSLicenseInfoPtr pOTSLicenseInfo = COTSLicMgr::LoadLicenseInfoFromTextFile(strPathName);
  189. // set control data with the license info
  190. if (pOTSLicenseInfo)
  191. {
  192. SetControlData(pOTSLicenseInfo);
  193. UpdateControlStatus();
  194. }
  195. }
  196. }
  197. }
  198. // save button click event
  199. void CDlgLicenseInfo::OnBnClickedOk()
  200. {
  201. // dialog is from request call
  202. if (m_bRequest)
  203. {
  204. // get control data from dialog
  205. UpdateData();
  206. // create a license info
  207. COTSLicenseInfoPtr a_pOTSLicenseInfo (new COTSLicenseInfo());
  208. a_pOTSLicenseInfo->SetComputerNickName(m_strEditComputerNickName);
  209. a_pOTSLicenseInfo->SetMachineId(m_strEditMachineId);
  210. a_pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId);
  211. a_pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType);
  212. a_pOTSLicenseInfo->SetExpireDate(m_oExpireData);
  213. //create license info text file
  214. if (!COTSLicMgr::CreateLicenseInfoFileText(a_pOTSLicenseInfo))
  215. {
  216. return;
  217. }
  218. }
  219. // get control data for the license info
  220. GetControlData(!m_bRequest);
  221. // close dialog, DoModel() return IDOK
  222. CDialog::OnOK();
  223. }
  224. // set license info
  225. void CDlgLicenseInfo::SetLicenseInfo(COTSLicenseInfoPtr a_pLicenseInfo)
  226. {
  227. // safety check
  228. ASSERT(a_pLicenseInfo);
  229. if (!a_pLicenseInfo)
  230. {
  231. LogErrorTrace(__FILE__, __LINE__, _T("SetLicenseInfo: invalid license info pointer"));
  232. return;
  233. }
  234. if (a_pLicenseInfo)
  235. {
  236. m_pLicenseInfo = a_pLicenseInfo;
  237. }
  238. }
  239. // get control data for the license info
  240. void CDlgLicenseInfo::GetControlData(BOOL m_bUpdate /*= TRUE*/)
  241. {
  242. // get control data from dialog
  243. if (m_bUpdate)
  244. {
  245. UpdateData();
  246. }
  247. // set license info with control data
  248. m_pLicenseInfo->SetComputerNickName(m_strEditComputerNickName);
  249. m_pLicenseInfo->SetMachineId(m_strEditMachineId);
  250. m_pLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId);
  251. m_pLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType);
  252. m_pLicenseInfo->SetExpireDate(m_oExpireData);
  253. }
  254. // set control data
  255. void CDlgLicenseInfo::SetControlData(COTSLicenseInfoPtr a_pLicenseInfo)
  256. {
  257. // safety check
  258. ASSERT(a_pLicenseInfo);
  259. if (!a_pLicenseInfo)
  260. {
  261. LogErrorTrace(__FILE__, __LINE__, _T("SetControlData: invalid license info pointer"));
  262. return;
  263. }
  264. // set control data with the license info
  265. m_strEditComputerNickName = a_pLicenseInfo->GetComputerNickName();
  266. m_strEditMachineId = a_pLicenseInfo->GetMachineId();
  267. m_nComboSoftPackId = (int)a_pLicenseInfo->GetPackId();
  268. m_nCombeLicenseType = (int)a_pLicenseInfo->GetLicType();
  269. m_oExpireData = a_pLicenseInfo->GetExpireDate();
  270. UpdateLicenseKeyStr(FALSE);
  271. // send control data to dialog
  272. UpdateData(FALSE);
  273. }
  274. // change control appearance
  275. void CDlgLicenseInfo::ChangeControlAppearance()
  276. {
  277. // change button string for
  278. CString strButton;
  279. strButton.LoadString(IDS_BUTTON_EMAIL);
  280. m_ctrlButtonLoad.SetWindowText(strButton);
  281. // disable machine id edit control
  282. m_ctrlEditMachineId.EnableWindow(FALSE);
  283. }
  284. // update control status
  285. void CDlgLicenseInfo::UpdateControlStatus(BOOL a_bStatus /*= TRUE*/)
  286. {
  287. m_ctrlButtonSave.EnableWindow(a_bStatus);
  288. }
  289. // update license key string
  290. void CDlgLicenseInfo::UpdateLicenseKeyStr(BOOL a_bUpdateData /*= TRUE*/)
  291. {
  292. // get control data from dialog
  293. if (a_bUpdateData)
  294. {
  295. UpdateData();
  296. }
  297. // get license key string =========================
  298. CString strEditLicenseKey = m_strEditLicenseKey;
  299. // create a license info with dialog parameters
  300. COTSLicenseInfoPtr pOTSLicenseInfo(new COTSLicenseInfo());
  301. pOTSLicenseInfo->SetComputerNickName(m_strEditComputerNickName);
  302. pOTSLicenseInfo->SetMachineId(m_strEditMachineId);
  303. pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId);
  304. pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType);
  305. // test the license info
  306. OTS_LICENSE_STATUS nLicStatus;
  307. COTSLicMgr::IsValidLicense(pOTSLicenseInfo->GetPackId(), pOTSLicenseInfo, nLicStatus, FALSE, FALSE);
  308. if (nLicStatus >= OTS_LICENSE_STATUS::OTS_LICENSE_STATUS_CAN_HAVEKEY && nLicStatus <= OTS_LICENSE_STATUS::MAX)
  309. {
  310. strEditLicenseKey = COTSLicMgr::EncryptLicenseInfo(pOTSLicenseInfo);
  311. }
  312. // get license key string =========================
  313. // license key string changed?
  314. if (m_strEditLicenseKey.Compare(strEditLicenseKey) != 0)
  315. {
  316. // yes, update
  317. m_strEditLicenseKey = strEditLicenseKey;
  318. if (a_bUpdateData)
  319. {
  320. // send control data to dialog
  321. UpdateData(FALSE);
  322. }
  323. }
  324. }
  325. }