startsync.cpp

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

CPP
1,301
字号
                            if (wcsstr(ss[i], L"mail") != 0) {
                                found = true;
                            }
                        }
                        if (found) {
                             setSourceOnReg(ss);
                             MailSyncMessages(ACCOUNT_NAME, MCF_ACCOUNT_IS_NAME | MCF_RUN_IN_BACKGROUND); 
                             return retValue = NOT_RETURN_TO_MAIN_FORM;                             
                        }

                        sync((const wchar_t **)ss);
                    }
                }
                PushRouter_FreeMessage(&PushMsg);
                memset(&PushMsg, 0, sizeof(PUSHMSG));
                PushMsg.cbSize = sizeof(PUSHMSG);                
            }
        } else if (wcscmp(command, PARAM_INCLUSIVE) == 0) {
            
            // just to know the number of ids
            unsigned int i = 0;
            for (i = 0; sources[i]; i++) {
                // the position start from 0 [that is mail, the name of the source]
                // [1], [2].. contains the ids
                //
            }
            // the name of the source: it should be "mail"
            wchar_t* mailSourceName = wstrdup(sources[0]);

            // the buffer for the ids
            const wchar_t ** ids = new const wchar_t*[i];

            for (unsigned j = 0; j < i; j++) {
                ids[j] =  wstrdup(sources[j + 1]);
            }
            if (sources) { for (int k = 0; sources[k]; k++) { delete [] sources[k]; sources[k] = NULL; } sources = NULL;}
            //delete [] sources;

            sources = new wchar_t* [2];
            sources[0] = mailSourceName;
            sources[1] = 0;
            sync((const wchar_t **)sources, (const wchar_t **)ids);

        } else if (wcscmp(command, PARAM_TRANSPORT) == 0 ||
                   wcscmp(command, PARAM_T_REFRESHSERVER) == 0 ||
                   wcscmp(command, PARAM_T_REFRESHCLIENT ) == 0) {

            if (strcmp(getRegConfig()->getConfigSourcesParameter("mail", "sync"), "none") == 0) {
                wchar_t title[128];
                wsprintf(title, TEXT("%s"), getLocalizationUtils()->getLocalizationString(IDS_FUNAMBOL_ALERT));
                int ret = TimedMessageBox(NULL, 
                    getLocalizationUtils()->getLocalizationString(IDS_MAIL_DISABLED),
                    title, 
                    MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND, 10 * 1000);
                if(ret == IDYES){
                    getRegConfig()->setConfigSourcesParameter( "mail" , "sync" , "two-way" );
                    getRegConfig()->setDirty(CS_DIRTY_SOURCE_ENABLED);
                    getRegConfig()->saveDirty();
                } else {
                    return 1;
                }
            }
            if( wcscmp(command, PARAM_T_REFRESHSERVER) == 0) {
                if (sources == 0) { //////****************
                    LOG.error("%S call without sources", PARAM_T_REFRESHSERVER);
                    retValue = NOT_RETURN_TO_MAIN_FORM;
                } else {
                    recover("refresh-from-server", (const wchar_t **)sources);
                }

            } else if (wcscmp(command, PARAM_T_REFRESHCLIENT ) == 0) {
                if (sources == 0 ){ //////****************
                    LOG.error("%S call without sources", PARAM_T_REFRESHCLIENT);
                    retValue = NOT_RETURN_TO_MAIN_FORM;
                } else {
                    recover("refresh-from-client", (const wchar_t **)sources);
                }

            } else if (sources) {
                sync((const wchar_t **)sources);

            } else if (!sources) {
                LOG.error("Transport call without sources");
                retValue = 0;
            }
        } else {
            if (!sources) {
                sources = getEnabledSourcesFromConfig();
                if (!sources) {
                    LOG.error("No sources in the config are selected to be synced");
                    quit(1);
                }                
            }
            bool flagmail = false;
            for(int i = 0; sources[i]; i++){
                if (wcsicmp(sources[i], TEXT("mail")) == 0){
                    flagmail = true;
                }
            }
            if(!flagmail) {
                if (wcsicmp(command, TEXT("refresh-from-server")) == 0) {
                    if (sources == 0) {
		                quit(1);
	                }
                    recover("refresh-from-server", (const wchar_t **)sources);

                } else if (wcscmp(command, TEXT("refresh-from-client")) == 0) {
                    if (sources == 0) {
		                quit(1);
                    }
                    recover("refresh-from-client", (const wchar_t **)sources);
                }
                else if (wcscmp(command, TEXT("clientPush")) == 0) {
                    int ret = sync((const wchar_t **)sources);
                    wchar_t** sourcesLeft = readPIMModifications();
                    if (sourcesLeft) {
                        // We still have sources to sync (sync failed), so schedule a new sync.
                        schedule();
                        if (sourcesLeft) { for (int k = 0; sourcesLeft[k]; k++) { delete [] sourcesLeft[k]; sourcesLeft[k] = NULL; } sourcesLeft = NULL;}
                        
                    }
                    // Force to return the real errorcode of the sync. To avoid NOT_RETURN_TO_MAIN_SCREEN.
                    retValue = ret;
                } 
                else { 
                    // manual parameter
                    sync((const wchar_t **)sources);
                }
            } else {
                if (wcscmp(command, TEXT("refresh-from-server")) == 0 || 
                    wcscmp(command, TEXT("refresh-from-client")) == 0){
                    
                    wstring value = TEXT("transport-");
                    value += command;
                    setValuesInRegistry(TEXT("transportCommand"), value.c_str());
                }
                setSourceOnReg(sources);
                MailSyncMessages(ACCOUNT_NAME, MCF_ACCOUNT_IS_NAME | MCF_RUN_IN_BACKGROUND);
                return retValue = NOT_RETURN_TO_MAIN_FORM;
            }
        }
        if (hPushRouter) {
            PushRouter_Close(hPushRouter);
            PushRouter_FreeMessage(&PushMsg);
        }

        PowerPolicyNotify(PPN_UNATTENDEDMODE, FALSE);

        if (command){
            delete [] command;
        }
        if (sources) { for (int k = 0; sources[k]; k++) { delete [] sources[k]; sources[k] = NULL; } sources = NULL;}
        
        //if (sources){
        //    delete [] sources;
        //}

    }//end try
    catch (OutOfMemoryException& e) {

        int k = e.getMemExcErrorCode();
        if (k == -50) {
            LOG.error("Low Storage Memory Exception.");
            quit(k);
        }
        else if (k == -1) { // error due to Out of memory
            LOG.error("Out of memory. Required %lu ", e.getMemExcMemoryRequired());
            quit(-40);
        } else { // error due to Out of memory. Not -1 set but supposed
            LOG.error("Out of memory");
            quit(-40);
        }
    }
    catch (...) {
        LOG.error("Generic failure");
        quit(-2);
    }


    if (retValue == 0) {
        LOG.info("Sync successful.");
    }
    else if (retValue == NOT_RETURN_TO_MAIN_FORM) {
        // nothing to do
    }
    else {
        LOG.error("Sync not successful.");
    }

    return retValue;
}

void setValuesInRegistry(const wchar_t* name, const wchar_t* value) {
    
    HKEY key;
    DWORD res;
    int sizew = 0;

    RegCreateKeyEx(
        HKEY_LOCAL_MACHINE,
        TEXT("Software\\") ROOT_CONTEXT_W,
        0,
        NULL,
        REG_OPTION_NON_VOLATILE,
        KEY_ALL_ACCESS,
        NULL,
        &key,
        &res
        );
    
    if (key == 0) {
        goto finally;
    }
        
    RegSetValueEx(
        key,
        name,
        NULL,
        REG_SZ,  // we currently support only strings
        (UCHAR*)value,
        (wcslen(value)+1)*sizeof(WCHAR)
        );

    RegFlushKey(key);

finally:

    if (key != 0) {
        RegCloseKey(key);
    }    
}


/**
* Set a list of sources that has to be synced passing through 
* the SyncMLTransport to take advantage of the mail notification.
* This is done only if the mail source is in the list of sources 
* to be synced
*/
void setSourceOnReg(wchar_t** sources) {
    
    wchar_t* value = NULL;
    int sizew = 0;
   
    for (int i = 0; sources[i]; i++ ){
        sizew += wcslen (sources[i]) + 1;
    }

    value = new wchar_t[sizew];
    
    for (int i = 0; sources[i]; i++ ){
        if (i == 0){
            wcscpy(value, sources[i]);
        } else {
            wcscat(value, TEXT(","));
            wcscat(value, sources[i]);
        }
    }
    
    setValuesInRegistry(TEXT("transportSources"), value);
   
    if (value) {
        delete [] value; value = NULL;
    }
}

/**
* Read the enabled sources from the configuration and return an array null terminated
* with the names of the sources to be synced
*/
wchar_t** getEnabledSourcesFromConfig() {
    
    int i = 0, count = 0;
    wchar_t** sourcesfr = NULL;

    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("mail", "sync"), "none") == 0)) {
        count++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("contact", "sync"), "none") == 0)) {
        count++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("calendar", "sync"), "none") == 0)) {
        count++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("task", "sync"), "none") == 0)) {
        count++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("note", "sync"), "none") == 0)) {
        count++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("briefcase", "sync"), "none") == 0)) {
        count++;
    }
    if (count == 0) {
        return sourcesfr;
    }

    sourcesfr = new wchar_t*[count + 1];                

    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("mail", "sync"), "none") == 0)){
        sourcesfr[i] = wstrdup(TEXT("mail"));
        i++;
    }

    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("contact", "sync"), "none") == 0)){
        sourcesfr[i] = wstrdup(TEXT("contact"));        
        i++;
    }

    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("calendar", "sync"), "none") == 0)){
        sourcesfr[i] = wstrdup(TEXT("calendar"));
        i++;
    }

    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("task", "sync"), "none") == 0)){
        sourcesfr[i] = wstrdup(TEXT("task"));                         
        i++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("note", "sync"), "none") == 0)){
        sourcesfr[i] = wstrdup(TEXT("note"));                          
        i++;
    }
    if(!(strcmp(getRegConfig()->getConfigSourcesParameter("briefcase", "sync"), "none") == 0)){
        sourcesfr[i] = wstrdup(TEXT("briefcase"));  
        i++;
    }
    sourcesfr[i] = 0;
    return sourcesfr;

}

/**
 * Utility function to set a desired property in Windows registry.
 * @param fullContext    the full context path of the key to set (under HKLM)
 * @param propertyName   the name of the key to set
 * @param propertyVlaue  the value to set
 */
void setProperty(const char* fullContext, const char* propertyName, const char* propertyValue) {

    if (!fullContext  || !propertyName || !propertyValue) {
        return;
    }

    DMTree* dmt          = NULL;
    ManagementNode* node = NULL;
    dmt = DMTreeFactory::getDMTree(fullContext);
    if (!dmt)   goto finally;
    node = dmt->readManagementNode(fullContext);
    if (!node)  goto finally;
    node->setPropertyValue(propertyName, propertyValue);

finally:
    if (dmt)   delete dmt;
    if (node)  delete node;
}


/**
* function to try the parsign of command line
*/

void testCommandLineParsing() {

    wchar_t *com, **sou;
    parseCmdLine(TEXT("manual"), &com, &sou);
    if (sou) { for (int i = 0; sou[i]; i++) { delete [] sou[i]; sou[i] = NULL; } }
    parseCmdLine(TEXT("manual  contact, calendar  "), &com, &sou);
    if (sou) { for (int i = 0; sou[i]; i++) { delete [] sou[i]; sou[i] = NULL; } }
    parseCmdLine(TEXT("manual contact,calendar,task"), &com, &sou);
    if (sou) { for (int i = 0; sou[i]; i++) { delete [] sou[i]; sou[i] = NULL; } }
    parseCmdLine(TEXT("manual contact, calendar,   task"), &com, &sou);
    if (sou) { for (int i = 0; sou[i]; i++) { delete [] sou[i]; sou[i] = NULL; } }
    parseCmdLine(TEXT("transport-refresh-from-client mail,   task    "), &com, &sou);
    if (sou) { for (int i = 0; sou[i]; i++) { delete [] sou[i]; sou[i] = NULL; } }
    parseCmdLine(TEXT("/notify   "), &com, &sou);
    if (sou) { for (int i = 0; sou[i]; i++) { delete [] sou[i]; sou[i] = NULL; } }
    parseCmdLine(TEXT(""), &com, &sou);
    
}


    /*
int parsingCommandLine() {
 OLD PARSING COMMANDLINE

    if( wcsstr(cmdline, TEXT("debug ")) ) {
        *command = wstrdup(cmdline+wcslen(TEXT("debug ")));
        g_debug = true;
    }
    else {
        *command = wstrdup(cmdline);
        g_debug = false;
    }
   

    *sources = NULL;

    wchar_t *args = wcschr(*command, ' ');
    if(!args) {
        // in this case comand == cmdline and sources is null
        return 0;
    }
    // terminate command and point to the start of arguments
    args[0] = 0; args++;
    if (!args[0]) {
        // empty args after a space, like before
        return 0;
    }
    int argc;
    wchar_t *arg = args;
    // Count arguments
    for(argc = 1; (arg=wcschr(arg+1, ',')) != 0; argc++);

    // Get arg list
    wchar_t **srclist = new wchar_t *[argc+1];
    arg=args;
    int i;

    for(i=0; i<argc; i++) {
        if(!arg || !arg[0])
            return -1;
        srclist[i]=arg;

        arg=wcschr(arg, ',');
        if(arg) {
            arg[0]=0; arg++;
        }
    }
    srclist[i]=0;
    *sources=srclist;
}
*/

⌨️ 快捷键说明

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