syncmltransport.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 1,990 行 · 第 1/5 页

CPP
1,990
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * 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 Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#include "StdAfx.h"
#include "SyncMLTransport.h"
#include "base/Log.h"
#include "base/startcmd.h"
#include "base/util/utils.h"
#include "processUtils.h"
#include "customization.h"
#include "localizationUtils.h"
#include "HwndFunctions.h"

#include "pim/maincpp.h"
#include "pim/SettingFunctions.h"

#ifdef WIN32_PLATFORM_WFSP
#include "funresourcesp.h"
#endif
#ifdef WIN32_PLATFORM_PSPC
#include "funresourceppc.h"
#endif

//#include "MAPIHelper.h"

CAdviseSink* advise = NULL;

HRESULT Transport_OneStopFactory(
    IMailSyncHandler ** ppSyncHandler
    );

static HINSTANCE g_hInst = NULL;

IMailSyncCallBack* refToPCallBack;
LPWSTR             refToPProfile;

/**
* Function to abort the sync from stop send/receive.
*/
void abortSync() {
    
    CString s1;
    int     processID = 0;
    HWND    wnd       = NULL;

    wnd = ::FindWindow(TEXT("funambolstartsync"), NULL);
    
    if ((processID = checkStartSync()) != 0) {
       
            // send msg to startsync to stop
            if(wnd){
                ::PostMessage(wnd, WM_COMMAND, (WPARAM)10, NULL);
            }
            Sleep(500);
            HANDLE h = OpenProcess(0, FALSE, processID);
    
            if (h) {
                DWORD code = 0;
                GetExitCodeProcess(h, &code);
                if (code == STILL_ACTIVE) {                                        
                    startProgram(SYNCAPP, TEXT("removeNotif"));
                    TerminateProcess(h, -6); //-6 code for the user stop sync.
                    CloseHandle(h);
                    setClientConfiguration(NULL, PROPERTY_SPDM_PID, TEXT("0"), NULL);
                    CString s; s.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_CODE_SYNC_STOPPED);
                    LOG.info(_wcc(s.GetString()));                    
                }
            }
    }  
    startProgram(SYNCAPP, TEXT("removeNotif"));           
}
/////////////////////////////////////////////////////////////////////////////
//
// DllMain()
//
// DLL entry point.  Perform module initialization and clean-up.
//
/////////////////////////////////////////////////////////////////////////////

BOOL WINAPI DllMain(
    HANDLE hinstDLL,
    DWORD dwReason,
    LPVOID lpvReserved
    )
{
    switch(dwReason)
    {
    case DLL_PROCESS_ATTACH:
        // Save the DLL's handle
        g_hInst =(HINSTANCE) hinstDLL;

        // We don't need DLL_THREAD_ATTACH/DLL_THREAD_DETACH notifications here
        // so we can disable them.
        DisableThreadLibraryCalls((HMODULE) g_hInst);

        // If you use a resource DLL, load it here.
        // g_hInstRes = LoadLibrary(g_kszResDll);
        break;

    case DLL_PROCESS_DETACH:
        // Perform any clean-up here

        // Must release the localization DLL if we used it
        if (localizationUtils::isInstantiated()) {
            localizationUtils* language = getLocalizationUtils();
            if (language) {
                delete language; language = NULL;
            }
        }
        break;

    default:
        break;
    }

    return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
//
// OneStopFactory()
//
//  Description:
//      This DLL entry point is used by the Inbox to create an instance of
//      the transport object and to return the IMailSyncHandler interface
//      to it.
//
//  Parameters:
//      pszType         IN      String identifying the name of the transport
//                              to create(e.g. L"IMAP4", L"POP3", ...)
//                              This allows multiple transports to reside
//                              in a single DLL for efficiency.
//      ppSyncHandler   OUT     Pointer to the IMailSyncHandler interface
//                              for the requested transport.
//
//  Returns:    HRESULT
//
/////////////////////////////////////////////////////////////////////////////

extern "C" HRESULT WINAPI OneStopFactory(
    LPCWSTR pszType,
    IMailSyncHandler ** ppSyncHandler
    )
{
    return Transport_OneStopFactory(ppSyncHandler);
}


/////////////////////////////////////////////////////////////////////////////
//
// Transport_OneStopFactory()
//
//  Description:
//      Creates an instance of the transport class and returns a pointer
//      to its IMailSyncHandler interface.
//
//  Parameters:
//      ppSyncHandler   OUT     Pointer to the IMailSyncHandler interface
//                              for the requested transport.
//
//  Returns:    HRESULT
//
/////////////////////////////////////////////////////////////////////////////

HRESULT Transport_OneStopFactory(
    IMailSyncHandler ** ppSyncHandler
    )
{
    if(NULL == ppSyncHandler)
    {
        return E_INVALIDARG;
    }

    *ppSyncHandler = new CTransportSyncHandler;
    if(NULL == *ppSyncHandler)
    {
        return E_OUTOFMEMORY;
    }

    return S_OK;
}


/////////////////////////////////////////////////////////////////////////////
//
// CTransportSyncHandler::CTransportSyncHandler
//
//  Description:
//      Constructor
//
//  Parameters:
//
//  Returns:
//
/////////////////////////////////////////////////////////////////////////////

CTransportSyncHandler::CTransportSyncHandler() :
    m_cRef(1),
    m_pCallback(NULL),
    m_pMsgStore(NULL),
    m_fShutDown(FALSE),
    m_hEventShutdown(NULL),
    m_pszProfile(NULL),
    m_advise(NULL)
{
    ZeroMemory(&m_SyncProgressItemPending, sizeof(m_SyncProgressItemPending));
    CoInitializeEx(NULL, COINIT_MULTITHREADED);
    refToPCallBack = m_pCallback;
    refToPProfile = m_pszProfile;
}


/////////////////////////////////////////////////////////////////////////////
//
//  CTransportSyncHandler::~CTransportSyncHandler()
//
//  Description:
//      Destructor
//
//  Parameters:
//
//  Returns:
//
/////////////////////////////////////////////////////////////////////////////

CTransportSyncHandler::~CTransportSyncHandler()
{
    if(NULL != m_hEventShutdown)
    {
        CloseHandle(m_hEventShutdown);
    }

    MAPIFreeBuffer(m_pszProfile);

    if(NULL != m_advise)
    {
        m_advise->Release();
    }

    if(NULL != m_pMsgStore)
    {
        m_pMsgStore->Release();
    }

    if(NULL != m_pCallback)
    {
        m_pCallback->Release();
    }

    if (advise) {
        advise = NULL;
    }
}


/////////////////////////////////////////////////////////////////////////////
//
//  CTransportSyncHandler::Initialize()
//
//  Description:
//      Called by Inbox to initializes settings for the sync handler.
//
//  Parameters:
//      pCallback       IN  Pointer to callback interface used to retrieve
//                          configuration information from and pass status
//                          information back to Inbox during mail synchronization.
//      pszProfileName  IN  Name of active profile in Inbox.  Used in calls to
//                          some methods of IMailSyncCallBack.
//      pMsgStore       IN  Pointer to message store for this service
//
//  Returns:    HRESULT
//
/////////////////////////////////////////////////////////////////////////////

HRESULT CTransportSyncHandler::Initialize(
    IMailSyncCallBack* pCallback,
    LPCWSTR pszProfileName,
    IMsgStore*   pMsgStore
    )
{
    HRESULT         hr          =   S_OK;
    int             iProfileLen;
    //SizedSPropTagArray(1, spta) = { 1, PR_CE_SYNC_MANUALLY_WHEN_ROAMING }; 

    //LOG.setLevel(LOG_LEVEL_DEBUG);

    // Save a copy of the IMailSyncCallBack function pointer.
    // We'll use this to retrieve user credentials for the
    // account and to pass status info back to Inbox later.
    m_pCallback = pCallback;
    m_pCallback->AddRef();


    // Perform any transport initialization here
    // e.g.  creation of thread syncronization objects,
    // data structures used during mail syncronization, etc.
    // [ Omitted ]


    // Save a copy of the profile name for use later.
    iProfileLen =(lstrlen(pszProfileName) + 1 ) * sizeof(TCHAR);
    hr = MAPIAllocateBuffer(iProfileLen,(LPVOID *) &m_pszProfile);
    if(FAILED(hr))
    {
        goto Exit;
    }

    memcpy(m_pszProfile, pszProfileName, iProfileLen);

    // Save a copy of the message store pointer for this service
    if(NULL == pMsgStore)
    {
        hr = E_INVALIDARG;
        goto Exit;
    }
    /*
    MessageBox(NULL,  TEXT("Inside"), TEXT("Alert"), MB_ICONWARNING);

    SPropValue *pspv = NULL;
    ULONG cValues = 0;
    
    hr = pMsgStore->GetProps((SPropTagArray *) &spta, MAPI_UNICODE, &cValues, &pspv);
    long ll = pspv[0].Value.l;

    MAPIFreeBuffer(pspv);

    //
    // Set property to messagestore
    //
    LPSPropValue rgpMsgProperties = NULL;  // A structure for holding a MAPI property. This is treated as an array of MAPI properties.
    int          cbMsgProperties  = 0;     // The size of the array of properties (measured as a count of bytes).
    int          cMsgProperties   = 1;     // The number of properties for the message (for example, one)
    
    cbMsgProperties = sizeof(SPropTagArray) + 
                        cMsgProperties * (sizeof(SPropValue) );

    hr = MAPIAllocateBuffer(cbMsgProperties, (LPVOID FAR *)&rgpMsgProperties);  // Allocate memory for the properties.
    memset(rgpMsgProperties, 0, cbMsgProperties);                               // Erase the allocated memory.

    rgpMsgProperties[0].ulPropTag   = PR_CE_SYNC_MANUALLY_WHEN_ROAMING;   // Set values for the properties.
    rgpMsgProperties[0].Value.l = 1;
        
    hr = pMsgStore->SetProps(cMsgProperties, rgpMsgProperties, NULL);  // Add the properties to the message.
    hr = MAPIFreeBuffer((void *)rgpMsgProperties);                // Free resources.
     
    hr = pMsgStore->GetProps((SPropTagArray *) &spta, MAPI_UNICODE, &cValues, &pspv);
    ll = 0;
    ll = pspv[0].Value.l;

    MAPIFreeBuffer(pspv);
    */

    m_pMsgStore = pMsgStore; //set to the mail store.
    pMsgStore->AddRef();     //add one to the ref counter.


    // Load settings for the specified profile name.  These may
    // be stored in the registry, or as named properties on the
    // message store, or wherever is convenient.
    // [ Omitted ]


    // Most transports will want to register for an advise
    // in order to receive CEMAPI notifications to track

⌨️ 快捷键说明

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