⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communicationsettings.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
字号:
/*
 * Copyright (C) 2003-2007 Funambol, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */

// CommunicationSettings.cpp : implementation file
//
#include "stdafx.h"
#include "ui.h"
#include "CommunicationSettings.h"
#include "pim/ClientSettings.h"

#include "ClientUtil.h"
#include "examples/config.h"
#include "base/fscapi.h"
#include "spds/constants.h"
#include "spdm/DMTree.h"
#include "spdm/DMTreeFactory.h"
#include "spdm/ManagementNode.h"

//#include "comutil.h"
#include "pim/Maincpp.h"
#include "tpcshell.h"  // for SHSendBackToFocusWindow
#include "pim/SettingFunctions.h"
#include "base/startcmd.h"
#include "processUtils.h"


// CCommunicationSettings dialog

IMPLEMENT_DYNAMIC(CCommunicationSettings, CDialog)

CCommunicationSettings::CCommunicationSettings(CWnd* pParent /*=NULL*/)
    : CDialog(CCommunicationSettings::IDD, pParent)
{

}

CCommunicationSettings::~CCommunicationSettings()
{
  #if defined(WIN32_PLATFORM_PSPC)
    editUrl.Detach();
    editUser.Detach();
    editPassword.Detach();
  #endif
}

void CCommunicationSettings::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMM_EDIT_URL, editUrl);
    DDX_Control(pDX, IDC_COMM_EDIT_USER, editUser);
    DDX_Control(pDX, IDC_COMM_EDIT_PASS, editPassword);
}

void CCommunicationSettings::showMessage(CString message){

    alertMessage = message;

}
BOOL CCommunicationSettings::OnInitDialog()
{
    CString s1;
    s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_COMMUNICATION_SETTINGS); SetWindowText(s1);
    CDialog::OnInitDialog();

    ZeroMemory(&dlgCommandBar, sizeof(SHMENUBARINFO));
    dlgCommandBar.cbSize = sizeof(SHMENUBARINFO);
    dlgCommandBar.hwndParent = this->GetSafeHwnd();
    dlgCommandBar.dwFlags = SHCMBF_HMENU;
    dlgCommandBar.nToolBarId = IDM_OKCANCEL;
    #if defined WIN32_PLATFORM_WFSP
    #if (_WIN32_WCE < 0x500)
        dlgCommandBar.nToolBarId = IDR_MENUBAR_OKCANCEL;
    #endif
    #endif
    dlgCommandBar.hInstRes = getLocalizationUtils()->getLocaleResource();
    dlgCommandBar.nBmpId = 0;
    dlgCommandBar.cBmpImages = 0;
    if(!SHCreateMenuBar(&dlgCommandBar)){
        TRACE(_T("Cannot create command bar!"));
        return FALSE;
    }

    editUrl.SetLimitText(EDIT_MAXLENGTH);
    editUser.SetLimitText(EDIT_MAXLENGTH);
    editPassword.SetLimitText(EDIT_MAXLENGTH);

    LOGFONT lf;
    memset(&lf, 0, sizeof(LOGFONT));
    HFONT hFont = (HFONT) GetStockObject(SYSTEM_FONT);
    GetObject(hFont, sizeof lf, &lf);
    lf.lfWeight = FW_BOLD;
    lf.lfHeight +=FONT_HEIGHT_OFFSET;
    VERIFY(fontBold.CreateFontIndirect(&lf));    
  
    GetDlgItem(IDC_COMM_SERVURL)->SetFont(&fontBold);
    GetDlgItem(IDC_COMM_USER)->SetFont(&fontBold);
    GetDlgItem(IDC_COMM_PASS)->SetFont(&fontBold);
    GetDlgItem(IDC_ALERT)->SetFont(&fontBold);

    s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_SETTCOMM_SERVURL); 
    SetDlgItemText(IDC_COMM_SERVURL, s1);
    s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_SETTCOMM_USER); 
    SetDlgItemText(IDC_COMM_USER, s1);
    s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_SETTCOMM_PASS); 
    SetDlgItemText(IDC_COMM_PASS, s1);
    
    SetDlgItemText(IDC_ALERT,alertMessage);

    if(alertMessage.IsEmpty()){
        GetDlgItem(IDC_ALERT)->ShowWindow(SW_HIDE);
    }
   

    #if defined(WIN32_PLATFORM_PSPC)
        s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_COMMUNICATION_SETTINGS); 
        SetDlgItemText(IDC_HEADER_STATIC, s1);
        GetDlgItem(IDC_HEADER_STATIC)->SetFont(&fontBold);
    #endif

    // read Registry settings
    ClientSettings* cs = getRegConfig();
    CString s3, s4;

    s3 = cs->getAccessConfig().getSyncURL();     SetDlgItemText(IDC_COMM_EDIT_URL, s3);
    s3 = toWideChar( cs->getAccessConfig().getUsername());    SetDlgItemText(IDC_COMM_EDIT_USER, s3);
    s3 = toWideChar( cs->getAccessConfig().getPassword());    SetDlgItemText(IDC_COMM_EDIT_PASS, s3);

    if(checkStartSync() != 0)
    {
        // another sync is in progress, disable controls
        editUrl.EnableWindow(FALSE); editUser.EnableWindow(FALSE);
        editPassword.EnableWindow(FALSE);
    }
    else
    {
        #ifdef WIN32_PLATFORM_WFSP
         // override back key
         LPARAM lparam = MAKELPARAM(SHMBOF_NODEFAULT | SHMBOF_NOTIFY,
	            SHMBOF_NODEFAULT | SHMBOF_NOTIFY);
         ::SendMessage(dlgCommandBar.hwndMB, SHCMBM_OVERRIDEKEY, VK_TBACK, lparam);
        #endif
    }

    // set a large int as current selection, so the cursor will be at end
    editUrl.SetSel(LARGE_INT ,LARGE_INT);
    editUser.SetSel(LARGE_INT ,LARGE_INT);
    editPassword.SetSel(LARGE_INT ,LARGE_INT);

    // fix for wrong title problem, we get & set the title for the main window
    wchar_t title[256];
    AfxGetMainWnd()->GetWindowText(title,256);
    AfxGetMainWnd()->SetWindowText(title);

    editUrl.SetFocus();
    return FALSE;
}

BEGIN_MESSAGE_MAP(CCommunicationSettings, CDialog)
#ifdef WIN32_PLATFORM_WFSP
    ON_MESSAGE(WM_HOTKEY,&CCommunicationSettings::OnHotkey)
#endif
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


void CCommunicationSettings::OnOK()
{
    if(checkStartSync()){
        CDialog::OnOK();
    }

    // save values in registry
    ClientSettings* cs = getRegConfig();
    BOOL dirtyFlag=FALSE;

    CString s3;
    s3 = cs->getAccessConfig().getSyncURL();

    CString s1, url, username, password;
    //_bstr_t bst;

    GetDlgItemText(IDC_COMM_EDIT_URL, url);
    GetDlgItemText(IDC_COMM_EDIT_USER, username);
    GetDlgItemText(IDC_COMM_EDIT_PASS, password);

    if(!isInputValid(url, username, password))
        return;
    
    wstring s((LPCTSTR)url);    
    char* tmp = toMultibyte(s.c_str());  

    if(strcmp(tmp,cs->getAccessConfig().getSyncURL()) != 0){
        cs->getAccessConfig().setSyncURL(tmp);
        dirtyFlag=TRUE;
    }
    if (tmp) {
        delete [] tmp; tmp = NULL;
    }
   
    s = (LPCTSTR)username;
    tmp = toMultibyte(s.c_str());  
    if(strcmp(tmp,cs->getAccessConfig().getUsername()) != 0){
        cs->getAccessConfig().setUsername(tmp);
        dirtyFlag=TRUE;
    }
    if (tmp) {
        delete [] tmp; tmp = NULL;
    }

    s=(LPCTSTR)password;
    tmp = toMultibyte(s.c_str());  
    if(strcmp(tmp,cs->getAccessConfig().getPassword()) != 0){
        cs->getAccessConfig().setPassword(tmp);
        dirtyFlag=TRUE;
    }
    if (tmp) {
        delete [] tmp; tmp = NULL;
    }

    /*
    bst.Assign(url.AllocSysString());
    if(strcmp((char*)bst,cs->getAccessConfig().getSyncURL()) != 0){
        cs->getAccessConfig().setSyncURL(bst);
        dirtyFlag=TRUE;
    }
    bst.Detach();
    

    bst.Assign(username.AllocSysString());
    if(strcmp((char*)bst,cs->getAccessConfig().getUsername()) != 0){
        cs->getAccessConfig().setUsername(bst);
        dirtyFlag=TRUE;
    }
    bst.Detach();

    bst.Assign(password.AllocSysString());
    if(strcmp((char*)bst,cs->getAccessConfig().getPassword()) != 0){
        cs->getAccessConfig().setPassword(bst);
        dirtyFlag=TRUE;
    }
    bst.Detach();
    */
    const char* devId = cs->getDeviceConfig().getDevID();
    if(strcmp(devId, "") == 0){
        wstring s1;
        #if defined(WIN32_PLATFORM_WFSP)
            s1 = getIMEI();
        #endif
        #if defined(WIN32_PLATFORM_PSPC)
            s1 = GetSerialNumberFromKernelIoControl();
        #endif
        
        tmp = toMultibyte(s1.c_str());
        if(strcmp(tmp,cs->getDeviceConfig().getDevID()) != 0){
            cs->getDeviceConfig().setDevID(tmp);
            cs->setDirty(CS_DIRTY_DEVICE_CONFIG);
        }
        /*
        bst.Assign(s1.AllocSysString());
        if(strcmp((char*)bst,cs->getDeviceConfig().getDevID()) != 0){
            cs->getDeviceConfig().setDevID(bst);
            cs->setDirty(CS_DIRTY_DEVICE_CONFIG);
        }
        bst.Detach();
        */
    }

    if ((s3 != url) && (cs->getPush() != "0")) {
        //startcmd(SYNCAPP, TEXT("addresschange mail"));
        startProgram(SYNCAPP, TEXT("addresschange mail"));
    }

    if(dirtyFlag){
        getRegConfig()->setDirty(CS_DIRTY_ACCESS_CONFIG);
    }
    getRegConfig()->saveDirty();

    // TODO: maybe here would be needed to save it to Registry, and not only to memory
    CDialog::OnOK();
}

#ifdef WIN32_PLATFORM_WFSP
LRESULT CCommunicationSettings::OnHotkey(WPARAM wParam, LPARAM lParam)
{
    if (HIWORD(lParam) == VK_TBACK)
        SHSendBackToFocusWindow(WM_HOTKEY, wParam, lParam);
    return 0;
}
#endif

bool CCommunicationSettings::isInputValid(CString url, CString username, CString password){
    CString s1;
    CString lowerUrl(url);
    lowerUrl.MakeLower();

    if(url.IsEmpty()){
        s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_ERROR_EMPTY_URL);
        AfxMessageBox(s1);
        return false;
    };
    if(username.IsEmpty()){
        s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_ERROR_EMPTY_USERNAME);
        AfxMessageBox(s1);
        return false;
    };
    if(password.IsEmpty()){
        s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_ERROR_EMPTY_PASSWORD);
        AfxMessageBox(s1);
        return false;
    };
    if(username.Find(' ') != -1){
        s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_ERROR_USERNAME_CONTAINS_BLANKS);
        AfxMessageBox(s1);
        return false;
    }

    // check if url is valid
    if( ((lowerUrl.Left(7).Compare(_T("http://"))  != 0) &&
         (lowerUrl.Left(8).Compare(_T("https://")) != 0)   )  ||

         (lowerUrl.Compare(_T("http://")) == 0)               ||
         (lowerUrl.Compare(_T("https://")) == 0)              ||
         (lowerUrl.Left(8).Compare(_T("http:///")) == 0)      ||
         (lowerUrl.Left(9).Compare(_T("https:///"))== 0)      ||
         (lowerUrl.Mid(7).Find(_T("http:/")) != -1)        ||
         (lowerUrl.Mid(7).Find(_T("https:/")) != -1)        ||
         (lowerUrl.Find(' ') != -1)

      ) {

       // url is not valid
       s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_ERROR_INVALID_URL);
       s1+=":\n\""; s1+=url; s1+="\"";
       AfxMessageBox(s1);
       return false;
    }

    return true;
}


HBRUSH CCommunicationSettings::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // set ppc header text color
    #if defined(WIN32_PLATFORM_PSPC)
        if(pWnd->GetDlgCtrlID() == IDC_HEADER_STATIC) {
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetTextColor(PPC_HEADER_TEXT_COLOR);
        }

    #endif
        if(pWnd->GetDlgCtrlID() == IDC_ALERT) {
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetTextColor(ALERT_COLOR);
        }
    return hbr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -