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

📄 maincpp.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * 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 General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */
#include "Winbase.h"
#include <windows.h>
#include <oleauto.h>
#include "initguid.h"
#include "pimstore.h"
#include <stdio.h>

#include "base/startcmd.h"
#include "pim/maincpp.h"
#include "pim/Utils.h"
#include "pim/SettingFunctions.h"
#include "client/SyncClient.h"
#include "pim/ContactSyncSource.h"
#include "pim/CalendarSyncSource.h"
#include "pim/NoteSyncSource.h"
#include "pim/TaskSyncSource.h"
#include "pim/BriefcaseSyncSource.h"

#include "pim/mail/MailSyncSource.h"
#include "client/MailSourceManagementNode.h"
#include "client/DMTClientConfig.h"
#include "filter/SourceFilter.h"
#include "filter/ClauseUtil.h"

#include "spdm/DMTree.h"
#include "spdm/DMTreeFactory.h"
#include "syncml/formatter/Formatter.h"
#include "spds/spdsutils.h"
#include "Notify.h"
#include "notify/timed_msgbox.h"
#include "pim/account.h"
#include "OutOfMemoryException.h"

#include "pim/difftime.h"

#include "events/SyncItemListenerClient.h"
#include "events/SyncListenerClient.h"
#include "events/SyncSourceListenerClient.h"
#include "events/SyncStatusListenerClient.h"
#include "events/TransportListenerClient.h"
#include "event/SetListener.h"

#include "HwndFunctions.h"
#include "customization.h"

#include "pim/CustomSyncClient.h"
#include <new.h>
#include <algorithm>
#include <string>

using namespace std;

//
// USE the pim/ClientSettings.h. THis must be shared with the same ClientSettings of the
// FunambolClient
//
#include "pim/ClientSettings.h"

#define OL_MAIL 610

static SyncSourceListenerClient* syncSourceListener ;

// it could be a string. it is a vector because better should be divide the sources
// and not get a string
// TODO: do we need this as a global???
//static vector<wstring> sourceNameForSlow;

/** Prototypes for local functions */
static wstring getMailUnsent(MailSyncSource& mailSource);
static wchar_t* completeMailId(const wchar_t* path, const wchar_t* partialID);
static wchar_t* findMailIdInFile(const wchar_t* fileName, const wchar_t* partialID);
//static int checkRefreshFromServer(int type);

static TCHAR* getMailMaxMsgSize();

/**
 * List of the sources to sync when no subset is specified,
 * in the default order. The list must be NULL terminated.
 */
const static wchar_t *defaultSources[] = {
        MAILS_NAME,
        CONTACTS_NAME,
        APPOINTMENTS_NAME,
        TASKS_NAME,
        NOTES_NAME,
        FAVORITES_NAME,
        FILES_NAME,
        NULL
};


/**
 * Function to handle the memory failure. It is set to be called at new failure
 * using the _set_new_handler() call in the main of the calling program.
 *
 * @param size the size of the memory required
 *
 * @throws an OutOfMemoryException with error code -1
 */
int operatorNewHandler(size_t size)
{
    throw OutOfMemoryException(size, -1);
}


/*
 * Return the sources to sync from the current config.
 */
static vector<const wchar_t*> getActiveSources(ClientSettings &cs,
                                               const wchar_t **sources)
{
    vector<const wchar_t*> ret;
    bool foundFirstSource = false;

    for (int j=0; defaultSources[j]; j++) {
        for (int i=0; sources[i]; i++){
            if (wcscmp(defaultSources[j], sources[i]) == 0) {

                char *name = toMultibyte(sources[i]);               //XXX
                SyncSourceConfig *sc = cs.getSyncSourceConfig(name);

                if (!sc) {
                    LOG.error("No config for source: %s", name);
                }
                else if(strcmp(sc->getSync(), "none") != 0) {
                    ret.push_back(sources[i]);

                    if (!foundFirstSource) {
                        HWND hwnd = HwndFunctions::getWindowHandle();
                        if (hwnd) {
                             int idSpecifiedSource = sourceNameToId(name);
                             SendMessage(hwnd, (WM_APP+4), (WPARAM) 1, (LPARAM) idSpecifiedSource );
                        }
                        foundFirstSource = true;
                    }
                }
                delete [] name;                                     //XXX
                break;
            }
        }
    }

    return ret;
}

/* Perform the synchronization */
static DWORD WINAPI performSync(ClientSettings& config,
                                const wchar_t* path,
                                vector<const wchar_t*> &sources,
                                const wchar_t** ids,
                                const char* mode)
{

    if (sources.empty()) {
        LOG.debug("No sources to sync");
        return 0;
    }

    int ret = 0;
    int mailSourceIndex = -1, result = 0;
    int days = 0, attachSize = 0;
    BOOL blockedByUser = FALSE;

    TCHAR* msxmessagesize = getMailMaxMsgSize();
    long maxMailMessageSize = (wcstol(msxmessagesize, NULL, 10) - 16384) / 1024;
    wchar_t* downloadAge = NULL;
    SourceFilter* filter = NULL;

    ArrayList syncSources;
    char* value = NULL;

    ManagementNode* node = NULL;

    LOG.setLevel(getLOGLevel());

    /*
    * To understand if the briefcase and note directory exist. If no and there are
    * no other source to sync the message is specific
    */
    BOOL briefcaseDirExists = TRUE;
    BOOL noteDirExists = TRUE;

    //
    // Mail configuration read now because it has to have a scope until the end
    //
    MailSourceManagementNode m = MailSourceManagementNode(SOURCES_CONTEXT, "mails");
    MailSyncSourceConfig sc;
    sc = m.getMailSourceConfig(true);
    MailSyncSource mailSource = MailSyncSource (TEXT("mail"), &sc);

    for (int i=0, l=sources.size(); i<l; i++) {
        if (wcscmp(sources[i], CONTACTS_NAME) == 0) {

            ContactSyncSource s(CONTACTS_NAME, OL_CONTACTS, config.getSyncSourceConfig(CONTACTS_NAME_A));
            s.setPath(path);
            s.setMimeType(config.getSyncSourceConfig(CONTACTS_NAME_A)->getType());
            if (mode) {
                s.setSyncMode(syncModeCode(mode));
            }
            syncSources.add(s);
        }
        else if (wcscmp(sources[i], APPOINTMENTS_NAME) == 0) {

            CalendarSyncSource s(APPOINTMENTS_NAME, OL_CALENDAR,
                                 config.getSyncSourceConfig(APPOINTMENTS_NAME_A));
            s.setPath(path);
            s.setMimeType(config.getSyncSourceConfig(APPOINTMENTS_NAME_A)->getType());
            if (mode) {
                s.setSyncMode(syncModeCode(mode));
            }
            syncSources.add(s);
        }
        else if (wcscmp(sources[i], TASKS_NAME)        == 0) {

            TaskSyncSource  s(TASKS_NAME, OL_TASK, config.getSyncSourceConfig(TASKS_NAME_A));
            s.setPath(path);
            s.setMimeType(config.getSyncSourceConfig(TASKS_NAME_A)->getType());
            if (mode) {
                s.setSyncMode(syncModeCode(mode));
            }
            syncSources.add(s); //XXX
        }
        else if (wcscmp(sources[i], FAVORITES_NAME)    == 0) {
/*
            FavoritesSyncSource s(FAVORITES_NAME, OL_FAVORITES);
            s.setPath(path);
            syncSources.add(s);
*/
        }
        else if (wcscmp(sources[i], FILES_NAME)        == 0) {
            //config.readConfigSources(FILES_NAME_A);
            BriefcaseSyncSource  s(FILES_NAME, OL_BRIEFCASE, config.getSyncSourceConfig(FILES_NAME_A));
            s.setPath(path);
            if (mode) {
                s.setSyncMode(syncModeCode(mode));
            }
            wchar_t* dir = toWideChar((config.getWinSyncSourceConfig(FILES_NAME_A))->getSyncDir().c_str());
            if (existsDirectory(dir)) {
                s.setDir(dir);
                syncSources.add(s);

            } else {
                LOG.info("Briefcase dir %S doesn't exists!", dir);
                briefcaseDirExists = FALSE;
            }
            if (dir) { delete [] dir; dir = NULL; }
        }
        else if (wcscmp(sources[i], NOTES_NAME) == 0) {
            //config.readConfigSources(NOTES_NAME_A);
            NoteSyncSource s(NOTES_NAME, OL_NOTES, config.getSyncSourceConfig(NOTES_NAME_A));
            s.setPath(path);
            if (mode) {
                s.setSyncMode(syncModeCode(mode));
            }
            wchar_t* dir = toWideChar((config.getWinSyncSourceConfig(NOTES_NAME_A))->getSyncDir().c_str());
            if (existsDirectory(dir)) {
                s.setDir(dir);
                syncSources.add(s);
            } else {
                LOG.info("Note dir %S doesn't exists!", dir);
                noteDirExists = FALSE;
            }
            if (dir) { delete [] dir; dir = NULL; }
        }
        else if (wcscmp(sources[i], MAILS_NAME) == 0 && doesFunambolAccountExist()) {

            if (mode) {
                mailSource.setSyncMode(syncModeCode(mode));
            }
            wchar_t t[12];
            getFolderToSync(sc, t);

            attachSize = sc.getAttachSize();

            // we want to disable the message size filter for the inclusive items
            // but we need to use the default as for donwload all message
            if (ids)
                attachSize = -1;

            if (attachSize == -1) {
                //reserved value for the remain part of syncml msg
                // 1024 because the filter multiply for 1024
                attachSize = maxMailMessageSize;
            }
            //
            // Set the size from the filter.
            //
            sc.setAttachSize(attachSize*1024);

            mailSource.setMaxMailMessageSize(maxMailMessageSize*1024);
            //
            // the ids is an array of mail id
            //
            if (ids) {
                //
                mailSource.setIsSyncInclusive(true);
                mailSource.setSyncMode(SYNC_ONE_WAY_FROM_SERVER);
                wchar_t* idComplete = completeMailId(path, ids[0]);
                if (idComplete) {
                    char* ss = toMultibyte(idComplete);
                    filter = ClauseUtil::createSourceFilterInclusive(ss, attachSize);
                    delete [] ss;
                    delete [] idComplete;
                }
            } else {
                mailSource.setIsSyncInclusive(false);
                days = sc.getDownloadAge();
                downloadAge = getDayBefore(days);

                filter =
                    ClauseUtil::createSourceFilter(downloadAge, sc.getBodySize(), attachSize);
            }

            mailSource.setPath(path);
            mailSource.setFolderToSync(t);
            mailSource.setFilter(filter);

            syncSources.add(mailSource);
        }
    }

    if (syncSources.size() == 0) {
            LOG.debug("The source array to sync is empty");
        if (blockedByUser) {
            LOG.debug("%s", "No sources to sync: blockedByUser");
            ret = -20;
        } else if (briefcaseDirExists == FALSE) {
            LOG.debug("%s", "No sources to sync: briefcaseDirExists false");
            ret = -25;
        } else if (noteDirExists == FALSE) {
            LOG.debug("%s", "No sources to sync: noteDirExists false");
            ret = -26;
        } else {
            LOG.debug("%s", "No sources to sync");
            ret = -10;
        }

    } else {
        //
        // Now we can create the Sync4jClient object and kick off the sync
        //
        // FIXME: adjust CustomSyncClient to make the hard work
        // the main should be as light as possible.
        CustomSyncClient syncClient;

        if (ids) {
            syncClient.isMailInclusive = TRUE;
        }
        else {
            syncClient.isMailInclusive = FALSE;
        }

        syncSourceListener->sc = &syncClient;

        int i=0;
        int l = syncSources.size();

        const int ml = 10;
        SyncSource* s_array[ml];
        for (i=0; (i<l) && (i<ml-1); ++i) {
            const wchar_t *name = ((SyncSource*)syncSources[i])->getName();
            if (wcscmp(name, TEXT("mail")) == 0) {
                s_array[i] = &mailSource;
            } else {
                s_array[i] = (SyncSource*)syncSources[i];
            }
            syncClient.namesOfSources.push_back(name); //XXX
        }
        s_array[i] = NULL;

        ret = syncClient.sync(config, s_array);

⌨️ 快捷键说明

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