mailsyncsource.cpp

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

CPP
863
字号
/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2003 - 2007 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#include "pim/mail/MailSyncSource.h"
#include "spds/SyncItemStatus.h"
#include "spds/MailMessage.h"
#include "spds/constants.h"
#include "base/util/utils.h"
#include "base/util/StringBuffer.h"
#include "pim/utils.h"
#include "spds/FolderData.h"
#include "notify/timed_msgbox.h"
#include "spds/SyncReport.h"
#include "spds/MailSyncSourceConfig.h"

#include "HwndFunctions.h"
#include "OutOfMemoryException.h"

#define ASK_REFRESH_MESSAGE TEXT("A sync to replace your local email with your mail server email has been requested. All the local mails will be deleted before adding the server items. Are you sure you want to continue?")
#define ASK_REFRESH_TIMEOUT 10

static int call;
static int cnew;
static int cupdated;
static int cdeleted;

static SyncItem *buildSyncItem(MailClientData* m)
{
    MailMessage& msg = m->getEmailData()->getEmailItem();

    SyncItem *syncItem = new SyncItem(m->getEntryId());

    char* toSend = m->getEmailData()->format();
    syncItem->setData(toSend, strlen(toSend) * sizeof(char));
    syncItem->setSourceParent(m->getParent());
    if (toSend)
        delete [] toSend;

    return syncItem;
}

static bool setMailData(SyncItem &item, const wchar_t *parent, MailClientData &m, bool isUpdate)
{
    int errorCode;

    char *msg = new char[item.getDataSize() + 1];
    memset(msg, 0, item.getDataSize() + 1);
    memcpy(msg, item.getData(), item.getDataSize());

    errorCode = m.getEmailData()->parse(msg);

    //errorCode = m.getEmailData()->parse((char*)item.getData(), item.getDataSize());
    //
    // -2 is a real error
    // -1 is an error only if it is a new item but not if it is an update.
    //
    if (errorCode == 0 || (errorCode == -1 && isUpdate)) {
        // nothing to do.
        // it continue usually
    } else {
        LOG.error("Error parsing message!");
        return false;
    }
    m.setParent(parent);

    delete [] msg;
    return true;
}

static bool setFolderData(SyncItem &item, /*const wchar_t *parent*/ FolderData &folder)
{
    int errorCode;

    char *msg = new char[item.getDataSize() + 1];
    memset(msg, 0, item.getDataSize() + 1);
    memcpy(msg, item.getData(), item.getDataSize());

    errorCode = folder.parse(msg);

    if (errorCode != 0) {
        LOG.error("Error parsing message!");
        return false;
    }

    delete [] msg;
    return true;
}



MailSyncSource::MailSyncSource(const wchar_t* name, AbstractSyncSourceConfig *sc) : 
                BaseSyncSource(name, sc)
{
    path = wstrdup(TEXT("\\"));
    s_folderToSync = NULL;
    syncItem = NULL;
    isMailInOutbox = false;
    isMailInInbox = false;
    mailClient2Server = NULL;
    mailServer2Client = NULL;
    failedSendMailInOutbox = false;
    maxMailMessageSize = 0;
    isSyncInclusive = false;
}

MailSyncSource::MailSyncSource(MailSyncSource& s) : BaseSyncSource(s)
{
    path = NULL;
    s_folderToSync = NULL;
    setLastSync(s.getLastSync());
    setNextSync(s.getNextSync());
    setMaxMailMessageSize(s.getMaxMailMessageSize());
    setLastAnchor(s.getLastAnchor());
    setNextAnchor(s.getNextAnchor());
    //setErrorHandler(s.getErrorHandler());
    setFilter(s.getFilter());

    setPath(s.path);
    setFolderToSync(s.s_folderToSync);

    // These are not copied from the other instance
    isMailInOutbox = false;
    isMailInInbox = false;
    failedSendMailInOutbox = false;
    mailServer2Client=NULL;
    mailClient2Server=NULL;
    

}

MailSyncSource::~MailSyncSource() {
    if (syncItem) {
        syncItem = NULL;
    }
    if (s_folderToSync) {
        delete [] s_folderToSync; s_folderToSync = NULL;
    }
    if (path) {
        delete [] path; path = NULL;
    }
    if (mailServer2Client)
        delete mailServer2Client;
    if (mailClient2Server)
        delete mailClient2Server;
}

void MailSyncSource::setPath(const wchar_t* p) {
    if (path)
        delete [] path;

    if (p == NULL)
        path = wstrdup(TEXT(""));
    else
        path = wstrdup(p);
}

const wchar_t* MailSyncSource::getPath() {
    return path;
}

void MailSyncSource::setFolderToSync(const wchar_t* t){
    if(s_folderToSync)
        delete [] s_folderToSync;
    s_folderToSync = wstrdup(t);
}

const wchar_t* MailSyncSource::getFolderToSync() {
    return s_folderToSync;
}

bool MailSyncSource::isInFolderToSync(const wchar_t* toCheck) {
    return wcsstr(s_folderToSync, toCheck) != NULL ? true : false;
}

void MailSyncSource::setIsMailInOutbox(bool value){
    isMailInOutbox = value;

}

bool MailSyncSource::getIsMailInOutbox() {
    return isMailInOutbox;
}

void MailSyncSource::setIsMailInInbox(bool value){
    isMailInInbox = value;
}

bool MailSyncSource::getIsMailInInbox() {
    return isMailInInbox;
}

void MailSyncSource::setFailedSendMailInOutbox(bool value){
    failedSendMailInOutbox = value;
}

bool MailSyncSource::getFailedSendMailInOutbox() {
    return failedSendMailInOutbox;
}

/*
 * Return the first SyncItem of all. It is used in case of slow
 * sync and retrieve the entire data source content.
 */
SyncItem* MailSyncSource::getFirstItem()
{
    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }
    //
    // initialize the flag
    //
    isMailInInbox = false;

    if ( mailClient2Server->setSync(SYNC_SLOW, getPath()) != S_OK ){
        LOG.error("getFirstItem: error in MailClient2Server::setSync");
        return NULL;
    }

    MailClientData* m = mailClient2Server->getFirstItem();

    if (m) {
        LOG.debug("Subject first all mail: -%s-", m->getEmailData()->getEmailItem().getSubject());
        syncItem = buildSyncItem(m);
        syncItem->setState(SYNC_STATE_NONE);
        if (m->getParent()[0] == 'O')
            isMailInOutbox = true;

        delete m; m = NULL;

    } else {
        LOG.debug("No mail get first");
    }

    return syncItem;

}

/*
* Return the next SyncItem of all. It is used in case of slow or refresh sync
* and retrieve the entire data source content.
*/

SyncItem* MailSyncSource::getNextItem() {
    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }

    MailClientData* m = mailClient2Server->getNextItem();
    if (m) {
        LOG.info("Subject other all mail: -%s-", m->getEmailData()->getEmailItem().getSubject());
        syncItem = buildSyncItem(m);
        syncItem->setState(SYNC_STATE_NONE);
        if (m->getParent()[0] == 'O')
            isMailInOutbox = true;

        delete m; m = NULL;

    } else {
        LOG.info("No mail get next");
    }

    return syncItem;


}

SyncItem* MailSyncSource::getFirstItemKey() {

    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }
    //
    // initialize the flag
    //
    setIsMailInOutbox(false);

    mailClient2Server->setFolderToSync(s_folderToSync);

    if ( mailClient2Server->setSync(SYNC_SLOW, getPath()) != S_OK ){
        LOG.error("getFirstItemKey: error in MailClient2Server::setSync");
        return NULL;
    }

    MailClientData* m = mailClient2Server->getFirstItem(TRUE);

    if (m) {
        mailServer2Client->setFolderToSync(s_folderToSync);
        mailServer2Client->deleteMessage(m);
        delete m; m = NULL;

        while((m = mailClient2Server->getNextItem(TRUE)) != NULL) {
            mailServer2Client->deleteMessage(m);
            LOG.debug("Delete all items: one record deleted");
            delete m; m = NULL;
        }
    }

    return NULL;
}

SyncItem* MailSyncSource::getNextItemKey() {

    LOG.debug("MailSyncSource::getNextItemKey");
    return NULL;
}

/*
* This method is always called by the sync manager. So the setModifiedItemsPOOM is always called
*/

SyncItem* MailSyncSource::getFirstNewItem() {

    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }
    //
    // initialize the flag
    //
    setIsMailInOutbox(false);

    mailClient2Server->setFolderToSync(s_folderToSync);
   
    if ( mailClient2Server->setSync(SYNC_TWO_WAY, getPath()) != S_OK ){
        LOG.error("getFirstNewItem: error in MailClient2Server::setSync");
        return NULL;
    }    
    MailClientData* m = mailClient2Server->getFirstNewItem();
        
    // this is the call to inform the main windows about
    // the modification only for new items
    if(HwndFunctions::wnd) {
        SendMessage(HwndFunctions::wnd, ID_MYMSG_TOTAL_NEW, SOURCE_MAIL, 
            (LPARAM)mailClient2Server->allTotalNew);
    }
    
    if (m) {
        LOG.info("Subject first new mail: -%s-", m->getEmailData()->getEmailItem().getSubject());
        syncItem = buildSyncItem(m);
        syncItem->setState(SYNC_STATE_NEW);
        if (m->getParent()[0] == 'O')
        {
            isMailInOutbox = true;
        }

        delete m; m = NULL;

    } else {
        LOG.info("No new mail");
    }

    return syncItem;
}

SyncItem* MailSyncSource::getNextNewItem() {
    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }

    MailClientData* m = mailClient2Server->getNextNewItem();

    if (m) {
        LOG.debug("Subject other new mail: -%s-", m->getEmailData()->getEmailItem().getSubject());
        syncItem = buildSyncItem(m);
        syncItem->setState(SYNC_STATE_NEW);
        //syncItem->setSourceParent(m->getParent());
        if (m->getParent()[0] == 'O')
               isMailInOutbox = true;

        delete m; m = NULL;

    } else {
        LOG.debug("No other new mail");
    }
    //-- check if syncItem is NULL
    return syncItem;
}

SyncItem* MailSyncSource::getFirstUpdatedItem() {
    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }

    MailClientData* m = mailClient2Server->getFirstUpdatedItem();

    
    // this is the call to inform the main windows about
    // the modification only for mods items (updated and deleted)
    if(HwndFunctions::wnd) {
        SendMessage(HwndFunctions::wnd, ID_MYMSG_TOTAL_MOD, SOURCE_MAIL, 
            (LPARAM)mailClient2Server->allTotalUpdatedAndDeleted);
    }
    
    if (m) {
        if(!m->getEmailData()->getEmailItem().empty()){

⌨️ 快捷键说明

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