mailclient2server.cpp

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

CPP
1,974
字号
    }

FuncExit:
    // Free each element in the pRows buffer
    FreeProws(pRows);
    RELEASE_OBJ(pRecipientTable);
    LOG.debug("Exiting from GetRecipients function");
    return hr;
}

HRESULT MailClient2Server::GetAllMessages(ENTRYLIST& entryList, LPMAPIFOLDER m_pInBoxFolder,
                                          const wchar_t* path, wchar_t folderId, bool list) {
    LOG.debug("Enter into GetAllMessages function");
    HRESULT         hr;
    UINT            nEntries = 0;
    LPMAPITABLE     pContentsTable = NULL;
    LPSRowSet       pRows = NULL;
    
    // We define a SPropTagArray array here using the SizedSPropTagArray Macro
    // This enum will allows us to access portions of the array by a name instead of a number.
    // If more tags are added to the array, appropriate constants need to be added to the enum.

    LOG.debug("Get Content table");
    hr = m_pInBoxFolder->GetContentsTable(0, &pContentsTable);
    EXIT_ON_FAILED(hr);

    LOG.debug("Set the Columns");
    hr = pContentsTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);
    int counter = 0;         
       
    while(!FAILED(hr = pContentsTable->QueryRows(1, 0, &pRows)))
    {
        if(pRows->cRows == 0)
            break;

        SBinary sbEntry = pRows->aRow[0].lpProps[eePR_ENTRYID].Value.bin;

        if(entryList.cValues)
        {
            PVOID    pOldBuffer = entryList.lpbin;
            hr = MAPIAllocateBuffer(sizeof(SBinary) * (entryList.cValues + 1), (LPVOID*)&(entryList.lpbin));
                       
            if (hr) {
                LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value1 is: %i", hr);
                MAPIFreeBuffer(pOldBuffer);
                entryList.cValues = 0;
                goto FuncExit;
            } else {
                memmove((LPVOID*)entryList.lpbin, pOldBuffer, sizeof(SBinary) * entryList.cValues);
                MAPIFreeBuffer(pOldBuffer);
            }
        }
        else {
            hr = MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(entryList.lpbin));
            if (hr) {
                LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value2 is: %i", hr);
                goto FuncExit;
            } 
        }

        entryList.lpbin[entryList.cValues].cb = sbEntry.cb;
        hr = MAPIAllocateBuffer(sbEntry.cb, (LPVOID*)&(entryList.lpbin[entryList.cValues].lpb) ); 
        if (hr) {
            LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value3 is: %i", hr);
            goto FuncExit;
        } else {
            memmove(entryList.lpbin[entryList.cValues].lpb, sbEntry.lpb, sbEntry.cb);
            entryList.cValues++;
        }

        // Free each element in the pRows buffer
        FreeProws(pRows);
        counter++;
        
    }
    hr = S_OK;

FuncExit:
    if(pRows) {
        LOG.debug("Free the row");
        // Free each element in the pRows buffer
        FreeProws(pRows);
    }
    RELEASE_OBJ(pContentsTable);
    LOG.debug("Exiting from GetAllMessages function");
    return hr;
}


HRESULT MailClient2Server::GetModificatedMessages(
                                ENTRYLIST& newMessages,
                                ENTRYLIST& updatedMessages,
                                ArrayList** deletedMessages,
                                LPMAPIFOLDER m_pInBoxFolder,
                                const wchar_t* path,
                                wchar_t folderId) {

    LOG.debug("Enter into GetModificatedMessages function");
    HRESULT        hr;
    
    SBinary        sbEntry;
    LPMAPITABLE pContentsTable = NULL;
    LPSRowSet    pRows = NULL;
    DATE        lastSync = 0;
    int         numMailItem = 0;
    int         i = 0;
    int         action = 0;
    BOOL        found = FALSE;

    BOOL        flagRead = FALSE;
    FILETIME    ft;
    SYSTEMTIME  st;

    DATE        lastSyncMailItem = 0;
    BOOL        flagReadMailItem = FALSE;

    LOG.debug("Read the previous mail of folderId %c", folderId);
    MailInfo*  mailInfo = readPreviousMail(path, folderId);

    if (mailInfo == NULL) {
        if (folderId == 'O') {
            LOG.debug("It is Outbox");
            mailInfo = new MailInfo();
        } else {
            LOG.debug("It is NULL. Exit");
            goto FuncExit;
        }
    }

    MailItem*  mailItem = NULL;

    lastSync    = mailInfo->getLast();
    numMailItem = mailInfo->getNum();


    // We define a SPropTagArray array here using the SizedSPropTagArray Macro
    // This enum will allows us to access portions of the array by a name instead of a number.
    // If more tags are added to the array, appropriate constants need to be added to the enum.

    LOG.debug("Get the content table of the mail Folder");
    hr = m_pInBoxFolder->GetContentsTable( 0, &pContentsTable);
    EXIT_ON_FAILED(hr);

    LOG.debug("Set the column of Columns");
    hr = pContentsTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);

    while(!FAILED(hr = pContentsTable->QueryRows(1, 0, &pRows))) {
        
        found = FALSE;

        if(pRows->cRows == 0) {
            break;
        }
        LOG.debug("Get the SBinary of the mail");
        // get the property of the mail in the folder
        sbEntry = pRows->aRow[0].lpProps[eePR_ENTRYID].Value.bin;
        ft = pRows->aRow[0].lpProps[eePR_LAST_MODIFICATION_TIME].Value.ft;
        FileTimeToSystemTime(&ft, &st);
        SystemTimeToVariantTime(&st, &lastSyncMailItem);

        if (pRows->aRow[0].lpProps[eePR_MESSAGE_FLAGS].Value.ul & MSGFLAG_READ) {
            flagRead = TRUE;
        } else {
            flagRead = FALSE;
        }
        LOG.debug("Get the ID of the mail...");
        char* id = convertBinaryToChar(sbEntry, folderId);
        LOG.debug("... that is %s", id);

        for (i = 0; i < mailInfo->getMailItems()->size(); i++) {

            mailItem = (MailItem*)(mailInfo->getMailItems())->get(i);

            if (strcmp(id, mailItem->getId()) != 0)
                continue;

            // if no continue then it goes on and found the item
            found = TRUE;
            flagReadMailItem = mailItem->getRead();

            if (flagReadMailItem != flagRead ||
                lastSyncMailItem > lastSync) {
                action = UPDATED_MAIL;
            } else {
                action = NO_ACTION_MAIL;
            }
            mailInfo->getMailItems()->removeElementAt(i);
            break;

        }
        LOG.debug("The action to perform is %i, (NO_ACT 0, N 1, UP 2)", action);

        if (id) { delete [] id; id = NULL;}

        if (found == FALSE)
            action = NEW_MAIL;

        switch (action) {

            case NEW_MAIL: {
                if (folderId == 'O') {
                    LOG.debug("New action only for Outbox");
                    if(newMessages.cValues)
                    {
                        PVOID    pOldBuffer = newMessages.lpbin;
                        hr = MAPIAllocateBuffer(sizeof(SBinary) * (newMessages.cValues + 1), (LPVOID*)&(newMessages.lpbin));
                        if (hr) {
                            LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value1 new: %i", hr);
                            MAPIFreeBuffer(pOldBuffer);
                            newMessages.cValues = 0;
                            goto FuncExit;
                        }
                        memmove((LPVOID*)newMessages.lpbin, pOldBuffer, sizeof(SBinary) * newMessages.cValues);
                        MAPIFreeBuffer(pOldBuffer);
                    }
                    else {
                        hr = MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(newMessages.lpbin));
                        if (hr) {
                            LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value2 new: %i", hr);                            
                            goto FuncExit;
                        }
                    }
                    newMessages.lpbin[newMessages.cValues].cb = sbEntry.cb;
                    hr = MAPIAllocateBuffer(sbEntry.cb, (LPVOID*)&(newMessages.lpbin[newMessages.cValues].lpb) );
                    if (hr) {                    
                        LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value3 new: %i", hr);
                        goto FuncExit;
                    
                    }
                    memmove(newMessages.lpbin[newMessages.cValues].lpb, sbEntry.lpb, sbEntry.cb);
                    newMessages.cValues++;

                    // Free each element in the pRows buffer
                    FreeProws(pRows);
                }

            }; break;

            case UPDATED_MAIL: {
                LOG.debug("Update action...");
                if(updatedMessages.cValues)
                {
                    PVOID    pOldBuffer = updatedMessages.lpbin;
                    hr = MAPIAllocateBuffer(sizeof(SBinary) * (updatedMessages.cValues + 1), (LPVOID*)&(updatedMessages.lpbin));
                    if (hr) {                    
                        LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value1 up: %i", hr);
                        MAPIFreeBuffer(pOldBuffer);
                        updatedMessages.cValues = 0;
                        goto FuncExit;                    
                    }
                    memmove((LPVOID*)updatedMessages.lpbin, pOldBuffer, sizeof(SBinary) * updatedMessages.cValues);
                    MAPIFreeBuffer(pOldBuffer);
                }
                else {
                    hr = MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(updatedMessages.lpbin));
                    if (hr) {                    
                        LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value2 up: %i", hr);                        
                        goto FuncExit;                    
                    }
                }

                updatedMessages.lpbin[updatedMessages.cValues].cb = sbEntry.cb;
                hr = MAPIAllocateBuffer(sbEntry.cb, (LPVOID*)&(updatedMessages.lpbin[updatedMessages.cValues].lpb) );
                if (hr) {                    
                    LOG.error("MAPIAllocateBuffer error. Try to restore. Scode value3 up: %i", hr);                        
                    goto FuncExit;                    
                }
                memmove(updatedMessages.lpbin[updatedMessages.cValues].lpb, sbEntry.lpb, sbEntry.cb);
                updatedMessages.cValues++;

                // Free each element in the pRows buffer
                FreeProws(pRows);
            }; break;

            case NO_ACTION_MAIL:
                LOG.debug("No action to be performed");
                // No action to do
                break;
        }

    }
    if (mailInfo->getMailItems()->size() > 0) {
        LOG.debug("Deleted mail");
        *deletedMessages = mailInfo->getMailItems()->clone();
    }

FuncExit:
    if(pRows) {
        LOG.debug("Removed pRows");
        // Free each element in the pRows buffer
        FreeProws(pRows);
    }
    if (mailInfo) {
        LOG.debug("Delete mailInfo");
        delete mailInfo; mailInfo = NULL;
    }

    RELEASE_OBJ(pContentsTable);
    LOG.debug("Exiting from GetModificatedMessages function");
    return hr;
}

void MailClient2Server::setFolderToSync(const wchar_t* t){
    if(folderToSync)
        delete folderToSync;
    folderToSync = wstrdup(t);
}

wchar_t* MailClient2Server::getFolderToSync() {
    return folderToSync;
}

HRESULT MailClient2Server::setSync (int mode, const wchar_t* path)
{
    LOG.debug("Enter into setSync function");
    HRESULT           hr          =  E_FAIL;
    IMAPISession *    pSession    =  NULL;
    IMAPITable*       pTable      =  NULL;
    SRowSet*          psrs        =  NULL;
    IMsgStore*        pStore      =  NULL;
    int               i           =  0;
    ULONG*   rgTags;
    folderNumber = 0;
    
    if (folderToSync == NULL) {
        LOG.debug("There is no folder to sync: create one");
        rgTags = new ULONG[2];
        rgTags[0] = 1;
        rgTags[1] = PR_CE_IPM_INBOX_ENTRYID;
        folderNumber = 1;
    }
    else {
        folderNumber = wcslen(folderToSync);
        LOG.debug("There are %i folder to sync", folderNumber);
        rgTags = new ULONG[folderNumber + 1];
        rgTags[0] = folderNumber;
        for (int i = 1; i <= folderNumber; i++) {
            rgTags[i] = getFolderId(folderToSync[i-1]);    // PR_CE_IPM_INBOX_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;

    LOG.debug("Login and create session");
    // First log on to the store.
    hr = MAPILogonEx (NULL, NULL, NULL, NULL, &pSession);
    EXIT_ON_FAILED (hr);

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

    LOG.debug("Set the column to retrieve");
    hr = pTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);

    int totalMails =0;
    bool accountFound = false;

    while (!accountFound) {
        // Get a row
        LOG.debug("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;
        LOG.debug("Found the account named %S", displayName);

        //
        // The name of the MessageStore that we use for the own Transport
        //
        if (wcscmp(displayName, ACCOUNT_NAME) == 0) {
            accountFound = true;
            LOG.debug("Check if the store is found properly");
            // Make sure we got the props we need

⌨️ 快捷键说明

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