mailclient2server.cpp

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

CPP
1,974
字号
            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);
            LOG.debug("Found the account info name: %S; accountMail: %S", accName, accountMail);
            // 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);
            LOG.debug("Create the list to store the info");
            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++) {
                LOG.debug("Loop on every folder: number %i", 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;
                }
                LOG.debug("Reset the cvalues counters");
                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], true);                                                
                        break;
                    }
                    case CLEAR_ALL: // it remove all the mail in the folder of the folderToSync...
                    {
                        ENTRYLIST entry;
                        entry.cValues = 0;
                        hr = GetAllMessages(entry, pfldrInbox, path, folderToSync[i]);
                        hr = pfldrInbox->DeleteMessages (&entry, 0, NULL, 0); 
                        MAPIFreeBuffer(entry.lpbin);
                        break;
                    }
                    case SYNC_SLOW:
                    {

                        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);
                            MAPIFreeBuffer(entry.lpbin);

                        } 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();
                        
                        allTotalUpdatedAndDeleted += updatedMessagesArray[i].cValues;
                        allTotalUpdatedAndDeleted += deletedMessagesArray[i]->size();

                        allTotalNew += newMessagesArray[i].cValues;
                        
                        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);
    LOG.debug("Exiting from setSync function");

    return hr;
}

/*
* Write all the current mail in the message store to get the id, the last, the read
*/

void MailClient2Server::writeCurrentMails(const wchar_t* path) {

    SYSTEMTIME local;
    DATE last;
    IMAPISession *    pSession    = NULL;
    HRESULT           hr          = NULL;
    IMessage*         pmsg        = NULL;
    ULONG             returned    = 0;
    ULONG             cmsgValue   = 0;
    SPropValue*       msgprops    = NULL;
    int j = 0;
    char filename [128];
    char* id = NULL;

    BOOL read = FALSE;

    wchar_t lastTime[64];
    GetSystemTime (&local);
    SystemTimeToVariantTime (&local, &last);
    doubleToSystemTime(lastTime, last);
    //wsprintf(lastTime, TEXT("%i-%i-%iT%i-%i-%i"), local.wYear, local.wMonth, local.wDay, local.wHour, local.wMinute, local.wSecond);

    for (int i = 0; i < folderNumber; i++) {

        //this permits to do not write the mails_O file cache
        if (folderToSync[i] == 'O')
            continue;

        int mailNum = entryListArray[i].cValues;

        FILE* f = NULL;
        if (wcscmp(path, TEXT("\\")) == 0)
            sprintf(filename, "%ls%s_%c",path, "mails", folderToSync[i]);
        else
            sprintf(filename, "%ls\\%s_%c",path, "mails", folderToSync[i]);

        f = fopen(filename, "w+");
        fprintf(f, "<mails>\n");

        fwprintf(f, TEXT("<num>%i</num>\n"), mailNum);
        fwprintf(f, TEXT("<last>%f</last>\n"), last);
        fwprintf(f, TEXT("<lastTime>%s</lastTime>\n"), lastTime);

        pSession = logOn();

        if (pSession == NULL)
            goto FuncExit;

        for (j=0; j<mailNum; j++) {

            hr = pSession->OpenEntry(entryListArray[i].lpbin[j].cb,
                (ENTRYID*)entryListArray[i].lpbin[j].lpb,
                NULL,
                MAPI_BEST_ACCESS,
                &returned,
                (IUnknown **) &pmsg);
            EXIT_ON_FAILED (hr);
            ASSERT (pmsg);

            hr = pmsg->GetProps ((LPSPropTagArray)&endSync, MAPI_UNICODE, &cmsgValue,
                &msgprops);
            fwprintf(f, TEXT("<item>\n"));

            if (msgprops[eeePR_ENTRYID].ulPropTag == PR_ENTRYID) {
                id = convertBinaryToChar(msgprops[eeePR_ENTRYID].Value.bin, folderToSync[i]);
            }
            fprintf(f, "<id>%s</id>\n", id);

            if (msgprops[eeePR_MESSAGE_FLAGS].ulPropTag == PR_MESSAGE_FLAGS) {
                if (msgprops[eeePR_MESSAGE_FLAGS].Value.ul & MSGFLAG_READ) {
                    read = TRUE;
                }
                else {
                    read = FALSE;
                }
            }
            fwprintf(f, TEXT("<read>%i</read>\n"), read);

            fwprintf(f, TEXT("</item>\n"));

            if (id) {
                delete [] id; id = NULL;
            }
        }
        fwprintf(f, TEXT("</mails>\n"));
        fflush(f);
        fclose(f);
    }

FuncExit:

    logOff(pSession);
}

/**
* <mails>
*  <num>2</num>
*  <last>2334.4656</last>
*  <item>
*   <id>AAAAABBRD==</id>
*   <read>0</read>    // if the message is read (1) or not (0)
*  </item>
*  <item>
*   <id>AAAAABBRDFFJJJF==</id>
*   <read>1</read>
*  </item>
* </mails>
*/

MailInfo* MailClient2Server::readPreviousMail(const wchar_t* path,
                                              wchar_t folderId) {

    char filename [128];
    wchar_t* element = NULL;
    wchar_t line[512];
    FILE* f = NULL;
    MailItem mailItem;
    StringBuffer s = "";
    int num = -1;

    if (wcscmp(path, TEXT("\\")) == 0)
        sprintf(filename, "%ls%s_%c",path, "mails", folderId);
    else
        sprintf(filename, "%ls\\%s_%c",path, "mails", folderId);

    f = fopen(filename, "r");

    // no file so a slow sync is perfomed
    if (f == NULL)
        return NULL;

    MailInfo* mailInfo = new MailInfo();
    
    while(fgetws(line, 511, f) != NULL) {

        if ((element = getElementContent(line, TEXT("num"), NULL)) != NULL) {
            mailInfo->setNum(element);
        }
        else if ((element = getElementContent(line, TEXT("last"), NULL)) != NULL) {
            mailInfo->setLast(element);
        }
        else if (wcsstr(line, TEXT("<item>")) != NULL) {
            mailItem = MailItem();
        }
        else if ((element = getElementContent(line, TEXT("id"), NULL)) != NULL) {
            char *e = toMultibyte(element);
            mailItem.setId(e);            
            delete [] e;
        }
        else if ((element = getElementContent(line, TEXT("read"), NULL)) != NULL) {
            mailItem.setRead(_wcc(element));
        }        
        else if (wcsstr(line, TEXT("</item>")) != NULL) {
            mailInfo->addMailItem(mailItem);

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

    fclose(f);

    return mailInfo;
}

HRESULT MailClient2Server::moveMessageFromOutboxToSentItemsFolder(const wchar_t* key) {
    LOG.debug("Enter into moveMessageFromOutboxToSentItemsFolder 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;

    rgTags = new ULONG[3];
    rgTags[0] = 2;
    rgTags[1] = PR_IPM_OUTBOX_ENTRYID;
    rgTags[2] = PR_IPM_SENTMAIL_ENTRYID;

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

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

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

    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

⌨️ 快捷键说明

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