maincpp.cpp

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

CPP
1,441
字号
                //
                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);
            }

            /*
            * From the filter logic, if bodysize == -1 the attachment are consider in this filter,
            * otherwise not. BTW In the inclusive filter they are always consider
            */
            if (sc.getBodySize() == -1) {
                mailSource.setIncludeAttachment(true);
            } else {
                mailSource.setIncludeAttachment(false);
            }
            mailSource.setPath(path);
            mailSource.setFolderToSync(t);
            mailSource.setFilter(filter);

            syncSources.add(mailSource);
        }
        
        else if (wcscmp(sources[i], CONFIG_NAME) == 0) {

            ConfigSyncSource s(CONFIG_NAME, config.getSyncSourceConfig(CONFIG_NAME_A));            
            s.setMimeType(config.getSyncSourceConfig(CONFIG_NAME_A)->getType());
            if (mode) {
                s.setSyncMode(syncModeCode(mode));
            }
            syncSources.add(s);
        }

    }

    // only to cleanup all the items of the sourcess
    if (cleanup) {

        int l = syncSources.size();
        for (int i=0; i<l; i++) {
            int group = 0;
            const wchar_t *name = ((BaseSyncSource*)syncSources[i])->getName();                       
            
            if (wcscmp(name, TEXT("mail")) == 0) {
                mailSource.beginSync();
                wchar_t t[12];
                // set all the folders to clear
                t[0] = 'I'; t[1] = 'O'; t[2] = 'D'; t[3] = 'S'; t[4] = 'T'; t[5] = NULL;
                mailSource.setFolderToSync(t);
                mailSource.clearAll();
                config.getWinSyncSourceConfig("mail")->setLast(0);
                config.saveWinSourceLastTimeStamp("mail");  // save timestamp
            } else {
                char* nn = toMultibyte(name);
                ((BaseSyncSource*)syncSources[i])->getFirstItemKey();
                if(nn) {
                    config.getWinSyncSourceConfig(nn)->setLast(0);
                    config.saveWinSourceLastTimeStamp(nn); // save timestamp
                    delete [] nn;
                }

            }
        }
        //config.save(); // only other stuffs, not the timestamp
        
        return 0;
    }

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

        s_array[i] = NULL;

        // ------------------------------------
        ret = syncClient.sync(config, s_array);
        // ------------------------------------

        if (ret == 0) {
            LOG.debug("Saving the config in the registry");
            //config.save();
            saveLastTimes(config, s_array); // saving only timestamps
        }
        else if (ret > 299 && ret < 599) {
            // try to discover if there are sources that are ok...
            SyncSource* s = NULL;
            bool otherSource404  = false;
            bool configSource404 = false;

            for (int i = 0; (s = s_array[i]) != NULL; i++) {
                SyncSourceReport* sr = s->getReport();
                if (sr->checkState()) {
                    // config.saveSyncSourceConfig(s->getConfig().getName());
                    config.saveWinSourceLastTimeStamp(s->getConfig().getName());  // save timestamp
                }
                else if (ret == 404 && sr->getLastErrorCode() == 404) {
                    if (strcmp(sr->getSourceName(), CONFIG_NAME_A)) {
                        // Found a source (not "config") that has 404 code.
                        // We have to display a UI msg for this 404.
                        otherSource404 = true;
                    }
                    else { 
                        configSource404 = true; 
                    }
                }
                else {
                    //this is to check if there's a problem with the email account
                    //if the email account on the server is not set we find it and
                    //trasform the error code from 506 to 588 so we can catch this
                    //error in uidlg.cpp and popup the right allert message to the
                    //user
                    const char* errorToCheck = "Error Setting Preliminary Info: Error getting Account information " ;
                    if( sr->getLastErrorCode() == 506 &&\
                        strcmp(sr->getLastErrorMsg(),errorToCheck) == 0 &&\
                        strcmp(sr->getSourceName(), "mail") == 0){
                            ret = 588;
                    }
                }
            }
            if (ret == 404 && configSource404 && !otherSource404) {
                // To catch 'code 404' only of source "config".
                // It happens when "configuration" is not found (old server), 
                // we don't want to display UI messages. So we change the error code.
                ret = ERR_CONFIG_SOURCE_NOT_FOUND;  // 2100

                // in this case it should save also the device configuration
                // mostly for the devinf cache
                config.saveConfig();
            }
        }

        // check source states and signal them to UI
        SyncSource* s = NULL;
        for (int i = 0; (s = s_array[i]) != NULL; i++) {
            SyncSourceReport* sr = s->getReport();
            int sourceID = sourceNameToId(sr->getSourceName());

            if (sourceID != SOURCE_CONFIG) {    // Source "config" does not have UI panel...
                if (sr->checkState())
                    SendMessage(HwndFunctions::getWindowHandle(), ID_MYMSG_SOURCE_STATE, sourceID, SYNCSOURCE_STATE_OK);
                else
                    SendMessage(HwndFunctions::getWindowHandle(), ID_MYMSG_SOURCE_STATE, sourceID, SYNCSOURCE_STATE_NOT_SYNCED);
            }
        }

        if (mailSource.getIsMailInOutbox() && ret == 0) {
             LOG.debug("performSync: there are mails in outbox");
        }

        if (mailSource.getIsMailInInbox()) {
            LOG.info("performSync: new mail in inbox.");
            /*
            // temporary removed default funambol sound. Use the usual
            // set in the ppc
            LOG.info("performSync: new mail in inbox, playing sound.");
            wstring s(path);
            s += TEXT("\\newmessage.wav");

            if (!PlaySound(s.c_str(), NULL, SND_SYNC | SND_NOSTOP)) {
                LOG.info("Could not play sound: %s", s.c_str());
                MessageBeep(MB_ICONASTERISK);
            }
            */
        }

        if (mailSource.getFailedSendMailInOutbox()) {
            LOG.error("performSync: there was an error sending an email, alerting the user.");
            wstring message = getMailUnsent(mailSource);
            MessageBox(NULL, message.c_str(), getLocalizationUtils()->getLocalizationString(IDS_FUNAMBOL_ALERT),
                    MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND);
        }

        LOG.debug("Sync ended multiple sources.");
    }
    return ret;
}

/*
 * Entry point to kickoff the synchronization process, both specifying the
 * sources or not. The default is to make all the configured sources.
 *
 * @param path    : the install path of the SyncClient PIM application on
 *                  the device
 * @param sources : NULL terminated array of strings containing the source
 *                  name of the sources to synchronize
 * @param ids :     NULL terminated array of strings containing the id of the mail
 *                  that must be synched with a INCLUSIVE filter. Note that this parameter
 *                  could be valorized only when there is only a source and this source is mail
 *
 * To avoid a server bug and send every time the capabilities to the server
 * in order to receive the NumberOfChanges tag
 * RESET the hashDevInfo flag in the registry
 */

DWORD WINAPI synchronize (
                const wchar_t* path,
                const wchar_t** sources,
                const wchar_t** ids,
                const char* mode)
{
    //LOG.reset();
    int ret = 0;

    HwndFunctions::getWindowHandle();

    SyncItemListenerClient* itemListener = new SyncItemListenerClient();
    SyncListenerClient* syncListener = new SyncListenerClient();
    syncSourceListener  = new SyncSourceListenerClient();
    SyncStatusListenerClient* sstl = new SyncStatusListenerClient();
    TransportListenerClient* tl = new TransportListenerClient();

    setSyncItemListener(itemListener);
    setSyncListener(syncListener);
    setSyncSourceListener(syncSourceListener);
    setSyncStatusListener(sstl);
    setTransportListener(tl);

    ClientSettings* cs = ClientSettings::getInstance();
    //cs->read(); NO! (read is already done by getInstance)
    // TODO: investigate why 2 read() can cause memory issues...
    
    LOG.setLevel(getLOGLevel());

    string logTitle = cs->getAccessConfig().getUserAgent();
    logTitle += " Log";

    if (LOG.getLogSize() > 2000000) { // 2.5 mb
        LOG.reset(logTitle.c_str());
    } else {
        LOG.info("------------------------------------------------------");
        LOG.info("------------------------------------------------------");
        LOG.info("%s %s", createCurrentTime(true), logTitle.c_str());          
    }

    LOG.info("DeviceID: %s", cs->getDeviceConfig().getDevID());
    LOG.info("IP address: %s", cs->getIp().c_str());


    // Get the sources to sync
    const wchar_t** srcList = sources ? sources : defaultSources;
    vector<const wchar_t*> activeList = getActiveSources(*(cs), srcList);

    // If PUSH is not active, we can't know if the config on Server has changed.
    // So we always add the "config" source to the list of sources to sync.
    if (cs->getPush() == "0") {
        if (!findSourceInVector(activeList, TEXT("config"))) {
            LOG.debug("Adding source 'config' to the list of sources to sync");
            activeList.push_back(TEXT("config"));

            WindowsSyncSourceConfig* wcs = cs->getWinSyncSourceConfig("config");
            if ( wcs && (!strcmp(wcs->getSync(), "none")) ) {
                wcs->setSync("two-way");
            }
        }
    }
    
    // ******* Start the sync *******
    ret = performSync(*(ClientSettings::getInstance()), path, activeList, ids, mode, false);

    if (ret == -20) {
        LOG.debug("The user stops the sync to avoid slow sync of PIM");
        return ret;
    }

    if (ret == -10) {
        LOG.debug("Mail not set to be synched");
        LOG.debug("No source to sync");
        return -10;
    }

    // release listeners
    if(itemListener)
        { delete itemListener; itemListener=NULL; }
    if(syncListener)
        { delete syncListener; syncListener=NULL; }
    if(syncSourceListener)
        { delete syncSourceListener; syncSourceListener=NULL; }
    if(itemListener)
        { delete itemListener; itemListener=NULL; }
    if(itemListener)
        { delete itemListener; itemListener=NULL; }

    LOG.debug("Synchronize is returning: %d.", ret);
    return ret;
}


/*
 * NOTE: USED ONLY TEMPORANEALLY TO GET A MAIL PARAMETER THAT MUST
 * BE FIXED IN A PROPER WAY
 */
static TCHAR* getMailMaxMsgSize() {
    HKEY key = NULL;
    DWORD res;
    long err = 0;
    TCHAR *p = TEXT("mailMaxMsgSize");
    ULONG dim = 0;
    TCHAR *buf = NULL;

    RegCreateKeyEx(
            HKEY_LOCAL_MACHINE,
            TEXT("\\Software\\") ROOT_CONTEXT_W TEXT("\\spds\\sources\\mails"),
            0,

⌨️ 快捷键说明

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