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

📄 mailclient2server.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    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;

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

            ASSERT (rgprops);

            //
            // open the folder Outbox and Sent
            //

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

            hr = pStore->OpenEntry (rgprops[1].Value.bin.cb,
                        (LPENTRYID)rgprops[1].Value.bin.lpb,
                        NULL,
                        MAPI_MODIFY,
                        NULL,
                        (IUnknown **) &pfldrSent);
            EXIT_ON_FAILED (hr);
            ASSERT (pfldrSent);

             // Decode the id
            char* eid = NULL;
            size_t len = decodeEntryId(key, &eid);
            if(!eid){
                LOG.error("updateMessage: error decoding entryId");
                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;
            }

            //
            // Add the read flag to the existing
            //
            SPropValue rgprops[3] = {0};
            ULONG    cProps   = 0;
            ULONG    cmsgValue =  0;
            SPropValue* msgprops = NULL;
            ULONG messageFlag = (ULONG) 0x00000000;
            FILETIME ft;
            char* msgId = NULL;

            hr = pmsg->GetProps ((LPSPropTagArray)&properties, MAPI_UNICODE, &cmsgValue,
               &msgprops);
            EXIT_ON_FAILED (hr);

            if (msgprops[ePR_MESSAGE_FLAGS].ulPropTag == PR_MESSAGE_FLAGS) {
                messageFlag = msgprops[ePR_MESSAGE_FLAGS].Value.ul;
            }

            if (msgprops[ePR_LAST_MODIFICATION_TIME].ulPropTag == PR_LAST_MODIFICATION_TIME) {
                ft = msgprops[ePR_LAST_MODIFICATION_TIME].Value.ft;
            }

            // Set the flags
            rgprops[cProps].ulPropTag = PR_MESSAGE_FLAGS;
            rgprops[cProps].Value.ul = messageFlag | MSGFLAG_READ | MSGFLAG_FROMME;
            ++cProps;

            rgprops[cProps].ulPropTag = PR_CLIENT_SUBMIT_TIME;
            rgprops[cProps].Value.ft = ft;
            ++cProps;

            rgprops[cProps].ulPropTag = PR_MESSAGE_DELIVERY_TIME;
            rgprops[cProps].Value.ft = ft;
            ++cProps;

            hr = pmsg->SetProps(cProps, rgprops, NULL);
            EXIT_ON_FAILED(hr);

            HRESULT hr = pfldrOutbox->CopyMessages(
                            &entryList,
                            NULL,
                            pfldrSent,
                            0,
                            NULL,
                            MESSAGE_MOVE);

            delete [] eid;

            MAPIFreeBuffer (entryList.lpbin);


            msgset = true;
            break;

        }

        // Clean up
        MAPIFreeBuffer (rgprops);

        MAPIFreeBuffer (rgpropsMsg);
        FreeProws   (psrs);

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

        RELEASE_OBJ (pmsg);
        RELEASE_OBJ (pfldrOutbox);
        RELEASE_OBJ (pfldrSent);
        RELEASE_OBJ (pStore);

    }
    if (accountFound == FALSE) {
        LOG.error("No Funambol account found");
    }
FuncExit:

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

    RELEASE_OBJ (pmsg);
    RELEASE_OBJ (pfldrOutbox);
    RELEASE_OBJ (pfldrSent);
    RELEASE_OBJ (pStore);
    RELEASE_OBJ (pTable);
    RELEASE_OBJ (pSession);

    if (rgTags)
        delete rgTags;
    LOG.debug("Exiting from moveMessageFromOutboxToSentItemsFolder function");

    return hr;
}

⌨️ 快捷键说明

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