maincpp.cpp

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

CPP
1,441
字号
/*
 * 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 "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/ConfigSyncSource.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 "processUtils.h"
#include "localizationUtils.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>
#include <list>
using namespace std;

#ifdef WIN32_PLATFORM_WFSP
#include "funresourcesp.h"
#endif
#ifdef WIN32_PLATFORM_PSPC
#include "funresourceppc.h"
#endif

//
// 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 char* createCurrentTime(BOOL complete);
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();

wstring getEmailErrorMessage(wstring error);

/**
 * 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,
        CONFIG_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]);
                SyncSourceConfig *sc = cs.getSyncSourceConfig(name);

                if (sc) {
                    if (strcmp(sc->getSync(), "none") != 0) {
                        ret.push_back(sources[i]);

                        if (!foundFirstSource) {
                            if ( wcscmp(sources[i], CONFIG_NAME) ) {     // Don't send the msg for "config"
                                HWND hwnd = HwndFunctions::getWindowHandle();
                                if (hwnd) {
                                     int idSpecifiedSource = sourceNameToId(name);
                                     SendMessage(hwnd, ID_MYMSG_STARTING_SYNC, (WPARAM) 1, (LPARAM) idSpecifiedSource );
                                }
                                foundFirstSource = true;
                            }
                        }
                    }
                    else if (!wcscmp(sources[i], CONFIG_NAME)) {
                        // Force the sync of config, even if it was with syncmode "none".
                        LOG.debug("Config syncmode was 'none', force it to 'two-way'.");
                        sc->setSync("two-way");
                        ret.push_back(sources[i]);
                    }
                }
                else {
                    LOG.error("No config for source: %s", name);
                }
                delete [] name;
                break;
            }
        }
    }

    return ret;
}

int saveLastTimes(ClientSettings& config, SyncSource** s_array) {

    SyncSource* s = NULL;
    for (int i = 0; (s = s_array[i]) != NULL; i++) {
        config.saveWinSourceLastTimeStamp(s->getConfig().getName());                
    }
    return 0;
}

/* 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,
                                BOOL cleanup)
{

    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;

    /*
    * 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) {

⌨️ 快捷键说明

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