syncmltransport.cpp

来自「funambol windows mobile plugin source co」· C++ 代码 · 共 1,886 行 · 第 1/4 页

CPP
1,886
字号
/*
 * 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
 */

#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"

#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;

/////////////////////////////////////////////////////////////////////////////
//
// 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
        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
    // message creates/deletes/modifications/moves.
    // [ Omitted ]

    advise = new CAdviseSink();
    m_advise = advise;
    advise->AddRef();

    ULONG m_ulAdviseConnection = 0;
    pMsgStore->Advise(
        0,
        NULL,//notify on entire store
        fnevObjectModified,
        (IMAPIAdviseSink *)advise,
        &m_ulAdviseConnection);

    // Some transports may need to know the Entry IDs for
    // system folders(Inbox, Outbox, Drafts, Sent Items,
    // Deleted Items(Wastebasket)).  This is a good place
    // to query the IMsgStore* to get those.
    // [ Omitted ]

Exit:
    return hr;
}


/////////////////////////////////////////////////////////////////////////////
//
//  CTransportSyncHandler::Synchronize()
//
//  Description:
//      This DLL entry point is called by Inbox to synchronize the
//      current service
//
//  Parameters:
//      pRqst   IN  Sync options requested by Inbox
//
//  Returns:    HRESULT
//
/////////////////////////////////////////////////////////////////////////////

HRESULT CTransportSyncHandler::Synchronize(
    MAILSYNCREQUEST * pRqst
    )
{
    HRESULT hr = MAPI_E_INVALID_PARAMETER;

    //SetCursor(LoadCursor(NULL, IDC_WAIT));

    //Get sources from registry
    
   // MessageBox(NULL,  TEXT("Inside"), TEXT("Alert"), MB_ICONWARNING);
    
    wchar_t* sources = NULL;

    sources = getValueFromReg(TEXT("transportSources"));
    unsigned long pid;
    if(!sources) {
        //pid = startcmd(SYNCAPP, TEXT("transport mail"));
        pid = startProgram(SYNCAPP, TEXT("transport mail"));  // start sync process
    } else {        
        wchar_t* value = NULL;
        int sizew = 0;
        wchar_t* transportCommand = getValueFromReg(TEXT("transportCommand"));
        
        if (!transportCommand) {
            transportCommand = wstrdup(TEXT("transport"));
        }
        sizew = wcslen(transportCommand) + 1 + wcslen(sources); // +1 is the space between "command values"
        value = new wchar_t[sizew + 1];
        wcscpy(value, transportCommand);
        wcscat(value, TEXT(" "));
        wcscat(value, sources);         
        pid = startProgram(SYNCAPP, value);	 // start sync process
        //pid = startcmd(SYNCAPP, value);   
        delete [] value; value = NULL;
        if (sources) { delete [] sources; sources = NULL;}

        
    }
    if (pid) {
        SetStatusText(IDS_STRING_SYNC_IN_PROGRESS);
        waitProcess(pid, FIVE_MINUTES_MSEC);
    }
    else {
        //SetStatusText(IDS_STRING_PLUG_IN_NOT_INSTALLED);
    }
    //SetCursor(NULL);
   
    if(NULL == pRqst)
    {
        // Inbox is requesting a full sync.
        // Synchronize all folders and messages.
        // [ Omitted ]
        // [ Assumed to return hr ]
    }
    else
    {
        switch(pRqst->ffFlags)
        {
        case SYNC_NORMAL:
            // Inbox is requesting a full sync.
            // Synchronize all folders and messages.
            // [ Omitted ]
            // [ Assumed to return hr ]
            break;

        case SYNC_FOLDER:
            // Inbox is requesting to synchronize a specific folder.
            // [ Omitted ]
            // [ Assumed to return hr ]
            break;

        case SYNC_RESETHIERARCHY:
            // Inbox is requesting to synchronize the folder hierarchy.
            // [ Omitted ]
            // [ Assumed to return hr ]
            break;

        case SYNC_CREATE_FOLDER:
            // The user created a folder, and Inbox is asking the
            // transport to create the local and server folders for it.
            // [ Omitted ]
            // [ Assumed to return hr ]
            break;

        case SYNC_RENAME_FOLDER:
            // The user renamed a folder, and Inbox is asking the
            // transport to rename the local and server folders for it.
            // [ Omitted ]
            // [ Assumed to return hr ]
            break;

        case SYNC_DELETE_FOLDER:
            // The user deleted a folder, and Inbox is asking the
            // transport to delete the local and server folders for it.
            // [ Omitted ]

⌨️ 快捷键说明

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