// DlgLicenseInfo.cpp : implementation file // #include "stdafx.h" #include "DlgLicenseInfo.h" #include "Resource.h" #include "afxdialogex.h" #include "OTSHelper.h" #include "COTSUtilityDllFunExport.h" namespace OTSMODEL { // show license info edit dialog // return true if true if DoModel return IDOK // "Edit" -- ShowEditDialog( oMyLicenseInfo) // "Add" -- ShowEditDialog( oMyLicenseInfo, TRUE) // "Request" -- ShowEditDialog( oMyLicenseInfo, FALSE, TRUE) BOOL ShowEditDialog(COTSLicenseInfo& a_oLicenseInfo, BOOL a_bAdd /*= FALSE*/, BOOL a_bRequest /*= FALSE*/) { COTSLicenseInfoPtr pLicenseInfo(new COTSLicenseInfo(a_oLicenseInfo)); AFX_MANAGE_STATE(AfxGetStaticModuleState()); CDlgLicenseInfo dlg; dlg.SetLicenseInfo(pLicenseInfo); if (a_bAdd) { dlg.SetAddSwitch(); } else if (a_bRequest) { dlg.SetRequestSwitch(); } if (dlg.DoModal() == IDOK) { a_oLicenseInfo = *(pLicenseInfo.get()); return TRUE; } return FALSE; } // CDlgLicenseInfo dialog IMPLEMENT_DYNAMIC(CDlgLicenseInfo, CDialog) CDlgLicenseInfo::CDlgLicenseInfo(CWnd* pParent /*=NULL*/) : CDialog(IDD_LICENSE_INFO, pParent) , m_strEditComputerNickName(_T("")) , m_strEditMachineId(_T("")) , m_nComboSoftPackId(0) , m_nCombeLicenseType(0) , m_oExpireData( COleDateTime::GetCurrentTime()) , m_strEditLicenseKey(_T("")) , m_bAdd(FALSE) , m_bRequest(FALSE) { m_pLicenseInfo = COTSLicenseInfoPtr(new COTSLicenseInfo()); } CDlgLicenseInfo::~CDlgLicenseInfo() { } void CDlgLicenseInfo::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDC_EDIT_NICKNAME, m_ctrlEditComputerNickName); DDX_Text(pDX, IDC_EDIT_NICKNAME, m_strEditComputerNickName); DDX_Control(pDX, IDC_EDIT_MACHEINEID, m_ctrlEditMachineId); DDX_Text(pDX, IDC_EDIT_MACHEINEID, m_strEditMachineId); DDX_Control(pDX, IDC_COMBO_PACKID, m_ctrlComboSoftPackId); DDX_CBIndex(pDX, IDC_COMBO_PACKID, m_nComboSoftPackId); DDX_Control(pDX, IDC_COMBO_LICESETYPE, m_ctrlComboLicenseType); DDX_CBIndex(pDX, IDC_COMBO_LICESETYPE, m_nCombeLicenseType); DDX_Control(pDX, IDC_DATETIME_EXPIREDATE, m_ctrlDateTimeExpireData); DDX_DateTimeCtrl(pDX, IDC_DATETIME_EXPIREDATE, m_oExpireData); DDX_Control(pDX, IDC_EDIT_KEY, m_ctrlEditLicenseKey); DDX_Text(pDX, IDC_EDIT_KEY, m_strEditLicenseKey); DDX_Control(pDX, IDC_BUTTON_LOAD, m_ctrlButtonLoad); DDX_Control(pDX, IDOK, m_ctrlButtonSave); } BEGIN_MESSAGE_MAP(CDlgLicenseInfo, CDialog) ON_BN_CLICKED(IDC_BUTTON_LOAD, &CDlgLicenseInfo::OnBnClickedButtonLoad) ON_EN_CHANGE(IDC_EDIT_NICKNAME, &CDlgLicenseInfo::OnEnChangeEditNickname) ON_EN_CHANGE(IDC_EDIT_MACHEINEID, &CDlgLicenseInfo::OnEnChangeEditMacheineid) ON_CBN_SELCHANGE(IDC_COMBO_PACKID, &CDlgLicenseInfo::OnCbnSelchangeComboPackid) ON_CBN_SELCHANGE(IDC_COMBO_LICESETYPE, &CDlgLicenseInfo::OnCbnSelchangeComboLicensetype) ON_NOTIFY(DTN_DATETIMECHANGE, IDC_DATETIME_EXPIREDATE, &CDlgLicenseInfo::OnDtnDatetimechangeDatetimeExpiredate) ON_BN_CLICKED(IDOK, &CDlgLicenseInfo::OnBnClickedOk) END_MESSAGE_MAP() // CDlgLicenseInfo message handlers BOOL CDlgLicenseInfo::OnInitDialog() { CDialog::OnInitDialog(); // set data time string format m_ctrlDateTimeExpireData.SetFormat(EXPIRE_DATA_CTRL_FORMAT); // set combo boxes for (int i = (int)OTS_SOFT_PACKAGE_ID::MIN; i <= (int)OTS_SOFT_PACKAGE_ID::MAX; ++i) { CString strItem = COTSLicMgr::GetSoftwarePackIdString((OTS_SOFT_PACKAGE_ID)i); m_ctrlComboSoftPackId.AddString(strItem); } for (int i = (int)OTS_LICENSE_TYPE::MIN; i <= (int)OTS_LICENSE_TYPE::MAX; ++i) { CString strItem = COTSLicMgr::GetLicenseTypeIdString((OTS_LICENSE_TYPE)i); m_ctrlComboLicenseType.AddString(strItem); } // control data with license info SetControlData(m_pLicenseInfo); // if this is called by request if (m_bRequest) { // change control appearance ChangeControlAppearance(); } // set control status UpdateControlStatus(FALSE || m_bAdd || m_bRequest); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } // nick name change event void CDlgLicenseInfo::OnEnChangeEditNickname() { UpdateControlStatus(); UpdateLicenseKeyStr(); } // machine name change event void CDlgLicenseInfo::OnEnChangeEditMacheineid() { UpdateControlStatus(); UpdateLicenseKeyStr(); } // packed id change event void CDlgLicenseInfo::OnCbnSelchangeComboPackid() { UpdateControlStatus(); UpdateLicenseKeyStr(); } // license type change event void CDlgLicenseInfo::OnCbnSelchangeComboLicensetype() { UpdateControlStatus(); UpdateLicenseKeyStr(); } // expire data change event void CDlgLicenseInfo::OnDtnDatetimechangeDatetimeExpiredate(NMHDR *pNMHDR, LRESULT *pResult) { LPNMDATETIMECHANGE pDTChange = reinterpret_cast(pNMHDR); UpdateControlStatus(); UpdateLicenseKeyStr(); *pResult = 0; } // load button click event void CDlgLicenseInfo::OnBnClickedButtonLoad() { // dialog is from request call if (m_bRequest) { // get control data from dialog UpdateData(); // generate license info string COTSLicenseInfoPtr a_pOTSLicenseInfo(new COTSLicenseInfo()); a_pOTSLicenseInfo->SetComputerNickName(m_strEditComputerNickName); a_pOTSLicenseInfo->SetMachineId(m_strEditMachineId); a_pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId); a_pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType); a_pOTSLicenseInfo->SetExpireDate(m_oExpireData); CString strEmail = COTSLicMgr::GetLicenseInfoTextBody(a_pOTSLicenseInfo); strEmail.Trim(); if (strEmail.IsEmpty()) { //invalid email body LogErrorTrace(__FILE__, __LINE__, _T("OnBnClickedButtonLoad: email body is empty.")); return; } // email license info string if (!COTSHelper::SendEmail(SUPPORT_EMAIL_ADDRESS, SUPPORT_EMAIL_TITLE, strEmail)) { // email license info string, return if send email fails LogErrorTrace(__FILE__, __LINE__, _T("OnBnClickedButtonLoad: Send email fails.")); return; } // get control data for the license info GetControlData(FALSE); // close dialog, DoModel() return IDOK CDialog::OnOK(); } else { // load a license info text file CFileDialog dlg(TRUE, NULL, NULL, OFN_FILEMUSTEXIST, TEXTFILE_FILTER); if (dlg.DoModal() == IDOK) { // read all string lines from the file CString strPathName = dlg.GetPathName(); // try to load license info from a text file COTSLicenseInfoPtr pOTSLicenseInfo = COTSLicMgr::LoadLicenseInfoFromTextFile(strPathName); // set control data with the license info if (pOTSLicenseInfo) { SetControlData(pOTSLicenseInfo); UpdateControlStatus(); } } } } // save button click event void CDlgLicenseInfo::OnBnClickedOk() { // dialog is from request call if (m_bRequest) { // get control data from dialog UpdateData(); // create a license info COTSLicenseInfoPtr a_pOTSLicenseInfo (new COTSLicenseInfo()); a_pOTSLicenseInfo->SetComputerNickName(m_strEditComputerNickName); a_pOTSLicenseInfo->SetMachineId(m_strEditMachineId); a_pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId); a_pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType); a_pOTSLicenseInfo->SetExpireDate(m_oExpireData); //create license info text file if (!COTSLicMgr::CreateLicenseInfoFileText(a_pOTSLicenseInfo)) { return; } } // get control data for the license info GetControlData(!m_bRequest); // close dialog, DoModel() return IDOK CDialog::OnOK(); } // set license info void CDlgLicenseInfo::SetLicenseInfo(COTSLicenseInfoPtr a_pLicenseInfo) { // safety check ASSERT(a_pLicenseInfo); if (!a_pLicenseInfo) { LogErrorTrace(__FILE__, __LINE__, _T("SetLicenseInfo: invalid license info pointer")); return; } if (a_pLicenseInfo) { m_pLicenseInfo = a_pLicenseInfo; } } // get control data for the license info void CDlgLicenseInfo::GetControlData(BOOL m_bUpdate /*= TRUE*/) { // get control data from dialog if (m_bUpdate) { UpdateData(); } // set license info with control data m_pLicenseInfo->SetComputerNickName(m_strEditComputerNickName); m_pLicenseInfo->SetMachineId(m_strEditMachineId); m_pLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId); m_pLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType); m_pLicenseInfo->SetExpireDate(m_oExpireData); } // set control data void CDlgLicenseInfo::SetControlData(COTSLicenseInfoPtr a_pLicenseInfo) { // safety check ASSERT(a_pLicenseInfo); if (!a_pLicenseInfo) { LogErrorTrace(__FILE__, __LINE__, _T("SetControlData: invalid license info pointer")); return; } // set control data with the license info m_strEditComputerNickName = a_pLicenseInfo->GetComputerNickName(); m_strEditMachineId = a_pLicenseInfo->GetMachineId(); m_nComboSoftPackId = (int)a_pLicenseInfo->GetPackId(); m_nCombeLicenseType = (int)a_pLicenseInfo->GetLicType(); m_oExpireData = a_pLicenseInfo->GetExpireDate(); UpdateLicenseKeyStr(FALSE); // send control data to dialog UpdateData(FALSE); } // change control appearance void CDlgLicenseInfo::ChangeControlAppearance() { // change button string for CString strButton; strButton.LoadString(IDS_BUTTON_EMAIL); m_ctrlButtonLoad.SetWindowText(strButton); // disable machine id edit control m_ctrlEditMachineId.EnableWindow(FALSE); } // update control status void CDlgLicenseInfo::UpdateControlStatus(BOOL a_bStatus /*= TRUE*/) { m_ctrlButtonSave.EnableWindow(a_bStatus); } // update license key string void CDlgLicenseInfo::UpdateLicenseKeyStr(BOOL a_bUpdateData /*= TRUE*/) { // get control data from dialog if (a_bUpdateData) { UpdateData(); } // get license key string ========================= CString strEditLicenseKey = m_strEditLicenseKey; // create a license info with dialog parameters COTSLicenseInfoPtr pOTSLicenseInfo(new COTSLicenseInfo()); pOTSLicenseInfo->SetComputerNickName(m_strEditComputerNickName); pOTSLicenseInfo->SetMachineId(m_strEditMachineId); pOTSLicenseInfo->SetPackId((OTS_SOFT_PACKAGE_ID)m_nComboSoftPackId); pOTSLicenseInfo->SetLicType((OTS_LICENSE_TYPE)m_nCombeLicenseType); // test the license info OTS_LICENSE_STATUS nLicStatus; COTSLicMgr::IsValidLicense(pOTSLicenseInfo->GetPackId(), pOTSLicenseInfo, nLicStatus, FALSE, FALSE); if (nLicStatus >= OTS_LICENSE_STATUS::OTS_LICENSE_STATUS_CAN_HAVEKEY && nLicStatus <= OTS_LICENSE_STATUS::MAX) { strEditLicenseKey = COTSLicMgr::EncryptLicenseInfo(pOTSLicenseInfo); } // get license key string ========================= // license key string changed? if (m_strEditLicenseKey.Compare(strEditLicenseKey) != 0) { // yes, update m_strEditLicenseKey = strEditLicenseKey; if (a_bUpdateData) { // send control data to dialog UpdateData(FALSE); } } } }