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

📄 mailclient2server.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 5 页
字号:
FuncExit:
    if(pRows)
    {
        // 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;
    UINT        nEntries = 0;
    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;

    MailInfo*  mailInfo = readPreviousMail(path, folderId);

    if (mailInfo == NULL) {
        if (folderId == 'O') {
            mailInfo = new MailInfo();
        } else {
            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.

    hr = m_pInBoxFolder->GetContentsTable( 0, &pContentsTable);
    EXIT_ON_FAILED(hr);

    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;

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

        char* id = convertBinaryToChar(sbEntry, folderId);

        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;

        }

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

        if (found == FALSE)
            action = NEW_MAIL;

        switch (action) {

            case NEW_MAIL: {

                if(newMessages.cValues)
                {
                    PVOID    pOldBuffer = newMessages.lpbin;
                    MAPIAllocateBuffer(sizeof(SBinary) * (newMessages.cValues + 1), (LPVOID*)&(newMessages.lpbin));
                    memmove((LPVOID*)newMessages.lpbin, pOldBuffer, sizeof(SBinary) * newMessages.cValues);
                    MAPIFreeBuffer(pOldBuffer);
                }
                else {
                    MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(newMessages.lpbin));
                }
                newMessages.lpbin[newMessages.cValues].cb = sbEntry.cb;
                MAPIAllocateBuffer(sbEntry.cb, (LPVOID*)&(newMessages.lpbin[newMessages.cValues].lpb) );
                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: {

                if(updatedMessages.cValues)
                {
                    PVOID    pOldBuffer = updatedMessages.lpbin;
                    MAPIAllocateBuffer(sizeof(SBinary) * (updatedMessages.cValues + 1), (LPVOID*)&(updatedMessages.lpbin));
                    memmove((LPVOID*)updatedMessages.lpbin, pOldBuffer, sizeof(SBinary) * updatedMessages.cValues);
                    MAPIFreeBuffer(pOldBuffer);
                }
                else {
                    MAPIAllocateBuffer(sizeof(SBinary), (LPVOID*)&(updatedMessages.lpbin));
                }

                updatedMessages.lpbin[updatedMessages.cValues].cb = sbEntry.cb;
                MAPIAllocateBuffer(sbEntry.cb, (LPVOID*)&(updatedMessages.lpbin[updatedMessages.cValues].lpb) );
                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:
                // No action to do
                break;
        }

    }
    if (mailInfo->getMailItems()->size() > 0)
        *deletedMessages = mailInfo->getMailItems()->clone();

FuncExit:
    if(pRows)
    {
        // Free each element in the pRows buffer
        FreeProws(pRows);
    }
    if (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) {
        rgTags = new ULONG[2];
        rgTags[0] = 1;
        rgTags[1] = PR_CE_IPM_INBOX_ENTRYID;
        folderNumber = 1;
    }
    else {
        folderNumber = wcslen(folderToSync);
        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;
        // 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;


    // 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);

    int totalMails =0;

    while (1){
        // 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) {

            // 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;
            }
            wchar_t accountMail[256];
            getAccountInfoSession(accName, accountMail, pSession);
            // 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);

            ASSERT (rgprops);

            entryListArray       = new ENTRYLIST[folderNumber];
            newMessagesArray     = new ENTRYLIST[folderNumber];
            updatedMessagesArray = new ENTRYLIST[folderNumber];
            deletedMessagesArray = new ArrayList*[folderNumber];
            
            //
            // watch at the folder that we want to sync
            //
            for (i = 0; i < folderNumber; i++) {

                // 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("Null inbox folder");
                    hr = E_FAIL;
                    break;
                }

                entryListArray[i].cValues = 0;
                newMessagesArray[i].cValues = 0;
                updatedMessagesArray[i].cValues = 0;
                deletedMessagesArray[i] = new ArrayList();

                // ENTRYLIST entry;
                switch (mode) {

                    case ONLY_ALL_MESSAGE_LIST:
                    {
                        hr = GetAllMessages(entryListArray[i], pfldrInbox, path, folderToSync[i]);
                        break;
                    }
                    case SYNC_SLOW:
                    {
                        //extra.clear();

                        if (folderToSync[i] == 'I' || folderToSync[i] == 'S') {
                            ENTRYLIST entry;
                            entry.cValues = 0;
                            hr = GetAllMessages(entry, pfldrInbox, path, folderToSync[i]);
                            hr = pfldrInbox->DeleteMessages (&entry, 0, NULL, 0);

                        } else {

                            hr = GetAllMessages(entryListArray[i], pfldrInbox, path, folderToSync[i]);
                            MailInfo* mailInfo = NULL;
                            readPreviousMail(path, folderToSync[i]);
                            if (mailInfo) {
                                delete mailInfo; mailInfo = NULL;
                            }
                        }
                        /*
                        * for the slow sync of Inbox and Sent (if checked to sync) the new way to do is delete all the items
                        * in the folder.

                        if (folderToSync[i] == 'I' || folderToSync[i] == 'S') {
                            hr = pfldrInbox->DeleteMessages (&entryListArray[i], 0, NULL, 0);
                            //MAPIFreeBuffer(entryListArray[i].lpbin);
                            //entryListArray[i].cValues = 0;
                        } else {
                            readPreviousMail(path, folderToSync[i], extra);
                            if (mailInfo) {
                                delete mailInfo; mailInfo = NULL;
                            }
                        }*/
                        break;
                    }
                    case SYNC_TWO_WAY:
                    {
                        
                        hr = GetModificatedMessages(newMessagesArray[i], updatedMessagesArray[i],
                                &deletedMessagesArray[i], pfldrInbox, path, folderToSync[i]);

                        totalMails += newMessagesArray[i].cValues;
                        totalMails += updatedMessagesArray[i].cValues;
                        totalMails += deletedMessagesArray[i]->size();

                        break;
                    }
                    default:
                        break;
                }
            }
        }


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

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

        RELEASE_OBJ (pmsg);
        RELEASE_OBJ (pfldrInbox);
        RELEASE_OBJ (pStore);
    }

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;

    if(HwndFunctions::wnd)
        SendMessage(HwndFunctions::wnd, ID_MYMSG_TOTAL_ITEMS, SOURCE_MAIL, (LPARAM)totalMails);

⌨️ 快捷键说明

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