mailserver2client.cpp

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

CPP
1,844
字号
        rgTags = new ULONG[2];
        rgTags[0] = 1;
        rgTags[1] = PR_CE_IPM_INBOX_ENTRYID;
        folderNumber = 1;
    }
    else {
        folderNumber = wcslen(server_folderToSync);
        rgTags = new ULONG[folderNumber + 1];
        rgTags[0] = folderNumber;
        for (int i = 1; i <= folderNumber; i++)
            rgTags[i] = getFolderId(server_folderToSync[i-1]);    // PR_CE_IPM_INBOX_ENTRYID;
                                                      // PR_CE_IPM_DRAFTS_ENTRYID;
    }

    ULONG             rgMsgTags[] =  { 1, PR_ENTRYID };
    ULONG             cValues     =  0;
    ULONG             returned    =  0;

    SPropValue*       rgprops     =  NULL;
    SPropValue*       rgpropsMsg  =  NULL;
    LPMAPIFOLDER      pfldrInbox  =  NULL;
    LPMAPIFOLDER      pfldrOutbox =  NULL;

    IMessage*         pmsg        =  NULL;
    ULONG             cmsgValue   =  0;
    SPropValue*       msgprops    =  NULL;

    LPSRowSet    pRows = NULL;
    SPropValue*       rgprops2    =  NULL;
    ULONG             cValues2    =  0;
    wchar_t* ret                  = NULL;
    bool msgset = false;

    // First log on to the store.
    hr = MAPILogonEx (NULL, NULL, NULL, NULL, &pSession);
    EXIT_ON_FAILED (hr);

    // Get the message stores table
    hr = pSession->GetMsgStoresTable (MAPI_UNICODE, &pTable);
    EXIT_ON_FAILED (hr);

    hr = pTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);


    while (!msgset){
        // Get a row
        hr = pTable->QueryRows (1, 0, &psrs);
        EXIT_ON_FAILED (hr);

        // Did we hit the end of the table?
        if (psrs->cRows != 1) {
          break;
        }
        //
        // the display name is the name of the account.
        //
        wchar_t* displayName = psrs->aRow[0].lpProps[eePR_DISPLAY_NAME].Value.lpszW;

        //
        // The name of the MessageStore that we use for the own Transport
        //
        if (wcscmp(displayName, ACCOUNT_NAME) == 0) {
            accountFound = TRUE;
            // Make sure we got the props we need
            if ((psrs->aRow[0].cValues < 1) || (psrs->aRow[0].lpProps[0].ulPropTag != PR_ENTRYID))
            {
              LOG.error("Store missing PR_ENTRYID!");
              hr = E_FAIL;
              break;
            }

            // Open this message store
            hr = pSession->OpenMsgStore (NULL,
                           psrs->aRow[0].lpProps[0].Value.bin.cb,
                          (ENTRYID *) psrs->aRow[0].lpProps[0].Value.
                           bin.lpb, NULL, 0, &pStore);
            EXIT_ON_FAILED (hr);

            // Now get the Inbox folder.
            hr = pStore->GetProps ((SPropTagArray *) rgTags, MAPI_UNICODE, &cValues, &rgprops);
            EXIT_ON_FAILED (hr);

            if(!rgprops){
                LOG.error("Error getting folder from message store.");
                goto FuncExit;
            }

            //
            // watch at the folder that we want to sync
            //
            for (i = 0; i < folderNumber; i++) {
                wchar_t* tmp = c2wc(server_folderToSync[i]);

                if (wcscmp(tmp, m->getParent()) != 0) {
                    if (tmp) delete [] tmp; tmp = NULL;
                    continue;
                }

                // open the message store
                hr = pStore->OpenEntry (rgprops[i].Value.bin.cb,
                            (LPENTRYID)rgprops[i].Value.bin.lpb,
                            NULL,
                            MAPI_MODIFY,
                            NULL,
                            (IUnknown **) &pfldrInbox);
                EXIT_ON_FAILED (hr);

                if (!pfldrInbox){
                    LOG.error("MailServer2Client::addMessage: pfldrInbox NULL!");
                    goto FuncExit;
                }

                ret = doesMessageExist(m->getEmailData()->getEmailItem().getMessageId(),
                                     pfldrInbox, server_folderToSync[i]);

                if (ret != NULL ) {
                    //ret = new wchar_t[64];
                    //wcscpy(ret, ITEM_ARLEADY_EXISTS);

                }
                else {
                    hr = pfldrInbox->CreateMessage (NULL, 0, &pmsg);
                    EXIT_ON_FAILED (hr);

                    if (!pmsg){
                        LOG.error("MailServer2Client::addMessage: pmsg NULL!");
                        goto FuncExit;
                    }

                    // populate the array with the binary ID
                    hr = pmsg->GetProps ((SPropTagArray *) rgMsgTags, MAPI_UNICODE, &cValues2, &rgprops2);
                    EXIT_ON_FAILED(hr);
                                                            
                    try {                       
                    // Now set the props on the message....
                    hr = SetMessageProps (pSession, pmsg, m);

                    } catch (OutOfMemoryException&) {
                        deleteGhostEmail(pfldrInbox, rgprops2);
                        throw OutOfMemoryException(0, -50); 
                    }
                    if (hr != S_OK) {
                        deleteGhostEmail(pfldrInbox, rgprops2);
                        LOG.error("Error in insert new mail message.");
                        goto FuncExit;
                    }

                    LOG.debug("Message props set.");

                    //
                    // Get the ID of the new message
                    //
                    if (rgprops2[eePR_ENTRYID].ulPropTag == PR_ENTRYID) {
                        ret = convertBinaryToWChar(rgprops2[eePR_ENTRYID].Value.bin, server_folderToSync[i]);
                        LOG.debug("Message enry id: %ls", ret);
                    }
                    else {
                        LOG.error("Error getting msg entry id.");
                    }

                }
                //
                // If there is a mail in Outbox with the same messageId of a mail just inserted in Sent,
                // then delete the Outbox one.
                //
                /*
                if (server_folderToSync[i] == 'S') {
                    if (messageExistInOubox_deleteIt(pStore, m->getEmailData()->getEmailItem().getMessageId()) ){
                        LOG.debug("Message in outbox succesfully deleted. Message id: %s", m->getEmailData()->getEmailItem().getMessageId());
                    } else {
                        LOG.debug("No message in outbox to delete. Message id: %s", m->getEmailData()->getEmailItem().getMessageId());
                    }

                }
                */

                msgset = true;
                break;  // found.
            }
        }

        // Clean up
        MAPIFreeBuffer (rgprops);
        MAPIFreeBuffer (rgprops2);
        MAPIFreeBuffer (rgpropsMsg);
        FreeProws   (psrs);

        rgprops   =  NULL;
        rgpropsMsg =  NULL;
        psrs    =  NULL;

        RELEASE_OBJ (pmsg);
        RELEASE_OBJ (pfldrInbox);
        RELEASE_OBJ (pStore);
    }
    if (accountFound == FALSE) {
        LOG.error("No " PROVIDER " account found");
    }
FuncExit:

    MAPIFreeBuffer (rgprops);
    MAPIFreeBuffer (rgpropsMsg);
    FreeProws   (psrs);

    RELEASE_OBJ (pmsg);
    RELEASE_OBJ (pfldrInbox);
    RELEASE_OBJ (pStore);
    RELEASE_OBJ (pTable);
    RELEASE_OBJ (pSession);

    if (rgTags)
        delete rgTags;
    LOG.debug("Exiting from addMessage function");
    return ret;
}


/*
* Return the ID of the inserted message
*/
HRESULT MailServer2Client::updateMessage(MailClientData* m) {
    LOG.debug("Enter into updateMessage function");
    if (!m) {
        LOG.error("Update message: mail client data is NULL.");
        return -1;
    }

    HRESULT           hr;
    IMAPISession *    pSession    =  NULL;
    ULONG             returned    =  0;
    IMessage*         pmsg        =  NULL;

    // First log on to the store.
    hr = MAPILogonEx (NULL, NULL, NULL, NULL, &pSession);
    EXIT_ON_FAILED (hr);

   // Decode the id
    char* eid = NULL;
    size_t len = decodeEntryId(m->getEntryId(), &eid);
    if(!eid){
        LOG.error("UpdateMessage: it is not possible to update the mail with id %S. Continue sync...", m->getEntryId());        
        return -1;
    }

    ENTRYLIST entryList;

    entryList.cValues = 0;
    MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(entryList.lpbin));
    entryList.lpbin[entryList.cValues].cb = len;
    MAPIAllocateBuffer(len, (LPVOID*)&(entryList.lpbin[entryList.cValues].lpb) );
    memmove(entryList.lpbin[entryList.cValues].lpb, eid, len);
    entryList.cValues++;

    hr = pSession->OpenEntry(entryList.lpbin[0].cb,
        (ENTRYID*)entryList.lpbin[0].lpb,
        NULL,
        MAPI_BEST_ACCESS,
        &returned,
        (IUnknown **) &pmsg);

    EXIT_ON_FAILED (hr);
    if(!pmsg){
        LOG.error("Pmsg is null.");
        goto FuncExit;
    }

    delete [] eid;

    // Now set the props on the message....
    //hr = SetMessageProps (pSession, pmsg, m, true); // update only the flags
    hr = SetMessageProps (pSession, pmsg, m); // the decision in done by the MailMessage
    EXIT_ON_FAILED(hr);

    MAPIFreeBuffer (entryList.lpbin);

FuncExit:

    RELEASE_OBJ (pmsg);
    RELEASE_OBJ (pSession);

    LOG.debug("Exiting from updateMessage function");
    return hr;
}

/*
* Return the ID of the inserted message
*/

HRESULT MailServer2Client::deleteMessage(MailClientData* m) {
    LOG.debug("Enter into deleteMessage function");
    HRESULT           hr;
    IMAPISession *    pSession    =  NULL;
    IMAPITable*       pTable      =  NULL;
    SRowSet*          psrs        =  NULL;
    IMsgStore*        pStore      =  NULL;
    int               i           =  0;
    ULONG*   rgTags;
    folderNumber = 0;
    BOOL accountFound             = FALSE;

    if (server_folderToSync == NULL) {
        rgTags = new ULONG[2];
        rgTags[0] = 1;
        rgTags[1] = PR_CE_IPM_INBOX_ENTRYID;
        folderNumber = 1;
    }
    else {
        folderNumber = wcslen(server_folderToSync);
        rgTags = new ULONG[folderNumber + 1];
        rgTags[0] = folderNumber;
        for (int i = 1; i <= folderNumber; i++)
            rgTags[i] = getFolderId(server_folderToSync[i-1]);    // PR_CE_IPM_INBOX_ENTRYID;
                                                      // PR_CE_IPM_DRAFTS_ENTRYID;
    }

    ULONG             rgMsgTags[] =  { 1, PR_ENTRYID };
    ULONG             cValues     =  0;
    ULONG             returned    =  0;

    SPropValue*       rgprops     =  NULL;
    SPropValue*       rgpropsMsg  =  NULL;
    LPMAPIFOLDER      pfldrInbox  =  NULL;

    IMessage*         pmsg        =  NULL;
    ULONG             cmsgValue   =  0;
    SPropValue*       msgprops    =  NULL;

    LPSRowSet          pRows        = NULL;
    SPropValue*       rgprops2     =  NULL;
    ULONG             cValues2     =  0;
    wchar_t*          ret         = NULL;
    wchar_t*          displayName  = NULL;
    bool msgset = false;

    // First log on to the store.
    hr = MAPILogonEx (NULL, NULL, NULL, NULL, &pSession);
    EXIT_ON_FAILED (hr);

    // Get the message stores table
    hr = pSession->GetMsgStoresTable (MAPI_UNICODE, &pTable);
    EXIT_ON_FAILED (hr);

    hr = pTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);

    while (!msgset){
        // Get a row
        hr = pTable->QueryRows (1, 0, &psrs);
        EXIT_ON_FAILED (hr);

        // Did we hit the end of the table?
        if (psrs->cRows != 1) {
            break;
        }
        //
        // the display name is the name of the account.
        //
        displayName = psrs->aRow[0].lpProps[eePR_DISPLAY_NAME].Value.lpszW;

        //
        // The name of the MessageStore that we use for the own Transport
        //
        if (wcscmp(displayName, ACCOUNT_NAME) == 0) {
            accountFound = TRUE;
            // Make sure we got the props we need
            if ((psrs->aRow[0].cValues < 1) || (psrs->aRow[0].lpProps[0].ulPropTag != PR_ENTRYID))
            {
              LOG.error("Store missing PR_ENTRYID!");

⌨️ 快捷键说明

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