startsync.cpp

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

CPP
1,301
字号
        // If sync ended succesfully or if it's NOT a network error, 
        // we reset the PIMList ONLY for the sources just synced.
        if (retValue == 0 || retValue < ERR_TRANSPORT_BASE) {
            resetPIMModifications((const wchar_t**)allList);
        }

        if (sList) {
            if (sList == allList) { allList = NULL; }
            for (int i=0; sList[i]  ; i++) { delete [] sList[i];   sList[i] = NULL;  }
            delete [] sList; sList = NULL;
        }
        if (cpList) {
            if (cpList == allList) { allList = NULL; }
            for (int i=0; cpList[i] ; i++) { delete [] cpList[i] ; cpList[i] = NULL; }
            delete [] cpList; cpList = NULL;
        }
        if (allList) {
            for (int i=0; allList[i]; i++) { delete [] allList[i]; allList[i] = NULL; }
            delete [] allList; allList = NULL;
        }
    }
    if (retValue != NOT_RETURN_TO_MAIN_FORM && retValue >= 0 && retValue < ERR_TRANSPORT_BASE) {
        int check = checkUpdate();
        getRegConfig()->getUpdateParams().setIsAnUpdateAvailable(check == 0 ? "0" : "1");
        getRegConfig()->storeUpdateParams();        
    }

    resetProcessID();

    CloseHandle( hMutex );
    LOG.debug("Mutex released.");

    return retValue;
}


/**
 * Parse the command line
 *
 * @param cmdline - the command line (syntax: command [source,source,...])
 *
 * @param command (out) - the command name
 * @param sources (out) - a NULL terminated array of source names
 */
static int parseCmdLine(const LPTSTR cmdline,
                        wchar_t **command,
                        wchar_t ***sources)
{

    *command = NULL;
    *sources = NULL;

    if (cmdline == NULL || wcscmp(cmdline, TEXT("")) == 0) {
        return 1;
    }

    wstring token(TEXT(" ,"));
    list<wstring> elements;
    list<wstring>::iterator it;
    wstring all = cmdline;
    size_t previous = 0;

    size_t found = all.find_first_of(token);    

    while (found != wstring::npos) {        
        wstring data = all.substr(previous, found - previous);
        if (data != TEXT("")) {
            elements.push_back(data);
        }
        previous = found + 1;
        found = all.find_first_of(token, previous);        
    }    
    if (previous < all.size()) {
        wstring data = all.substr(previous, all.size());
        trim(data);
        elements.push_back(data);
    }

    wchar_t **srclist = NULL;

    if (elements.size() == 0) {
        // nothing to do

    } else if (elements.size() == 1) {
        it = elements.begin();
        *command = wstrdup((*it).c_str()); 

    } else { // size > 1
        int size = elements.size();
        srclist = new wchar_t *[size];
        unsigned int i = 0;
        for (it = elements.begin(); it != elements.end(); it++) {
            wstring line = *it;       
            if (i == 0) { 
                *command = wstrdup(line.c_str()); 
            } else { 
                srclist[i-1] = wstrdup(line.c_str()); 
            }
            i++;
        }
        srclist[size - 1] = 0;
        *sources=srclist;
    } 

    return 0;
}

static void exitHandler() {
    if (retValue != ERR_ANOTHER_SYNC) {
        removeNotif();
        HwndFunctions::closePreviousMsgBox();
        HWND hwnd = HwndFunctions::getWindowHandle();

        wchar_t title[64];
        wcscpy(title, getLocalizationUtils()->getLocalizationString(IDS_FUNAMBOL_ALERT)); 
        if (hwnd && retValue != NOT_RETURN_TO_MAIN_FORM) {
            SendMessage(hwnd, ID_MYMSG_STARTSYNC_ENDED, NULL, (LPARAM)retValue ); // send msg startsync ended
        }          
        else if (!hwnd && retValue != NOT_RETURN_TO_MAIN_FORM && retValue >= 0 && retValue < ERR_TRANSPORT_BASE) {
            // if there is no window of the UI, starts the update procedure
            // no windows could be due by a schedule or push where the UI was closed with Close command
            updateProcedure(NULL);
        }        
        else if (retValue == -40) {
            MessageBox (NULL, getLocalizationUtils()->getLocalizationString(IDS_ERR_OUT_OF_MEMORY), 
                title, MB_SETFOREGROUND | MB_ICONHAND |MB_OK);
            
        } else if (retValue == -50) {
            MessageBox (NULL, getLocalizationUtils()->getLocalizationString(IDS_ERR_LOW_MEM), 
                title, MB_SETFOREGROUND | MB_ICONHAND |MB_OK);
            
        }
        
    }
    if (retValue == -6) { // sync stopped by the user
        // reset the pid property
        resetProcessID();
    }
}

//------------------------------------------------------------ Public functions
void quit(int value) {
    retValue = value;
    exit(retValue);
}




//------------------------------------------------------------------------ MAIN
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow) {       
    
    _set_new_mode(1);
    _set_new_handler(operatorNewHandler);
    
    try {
        
        //MessageBox (NULL, lpCmdLine, TEXT("title"), MB_SETFOREGROUND |MB_OK);
        wchar_t *command, **sources;

        HRESULT         hr              = NOERROR;
        HANDLE          hevtMsgAvailable= NULL;
        HPUSHROUTER     hPushRouter     = NULL;
        PUSHMSG         PushMsg         = {0};

        PowerPolicyNotify(PPN_UNATTENDEDMODE, TRUE);
        //SetThreadPriority(GetCurrentThread(),THREAD_PRIORITY_BELOW_NORMAL);
        
        if(g_debug){
            Alert(lpCmdLine);
        }

        if (parseCmdLine(lpCmdLine, &command, &sources)) {
            Alert(TEXT("Syntax error in command line"), TEXT("StartSync"));
            return 1;
        }
        
        if (command == NULL) {
            Alert(TEXT("No command line. Exit"), TEXT("StartSync"));
            return 1;
        }

        if (wcscmp(command, TEXT("addresschange")) == 0) {
            LogLevel l = getLOGLevel();
            LOG.setLevel(l);
            return notify();            
        }

        // Set the exit handler to remove the icon
        atexit(exitHandler);
        
        if(!(wcscmp(command,PARAM_AVAILABLE) == 0 ||
            wcscmp(command, PARAM_REMOVENOTIF) == 0 ||
            wcscmp(command, PARAM_SCHEDULE) == 0 ||
            wcscmp(command, PARAM_CRADLE) == 0 ||
            wcscmp(command, PARAM_REGISTER) == 0 ||
            wcscmp(command, PARAM_DEREGISTER) == 0 ||
            wcscmp(command, PARAM_NOTIFY) == 0 ||
            wcscmp(command, PARAM_TRANSPORT) == 0 ||
            wcscmp(command, PARAM_INCLUSIVE) == 0 ||
            wcscmp(command, PARAM_MANUAL) == 0 ||
            wcscmp(command, PARAM_REFRESHSERVER) == 0 ||
            wcscmp(command, PARAM_REFRESHCLIENT ) == 0 ||
            wcscmp(command, PARAM_T_REFRESHSERVER) == 0 ||
            wcscmp(command, PARAM_T_REFRESHCLIENT ) == 0 ||
            wcscmp(command, c_szParams) == 0 ||
            wcscmp(command, PARAM_TCPIP) == 0 ||
            wcscmp(command, APP_RUN_AT_TIME) == 0  ||
            wcscmp(command, PARAM_CLEANUP) == 0 ||
            wcscmp(command, PARAM_SEND_LOG) == 0  ||
            wcscmp(command, PARAM_REMOVE_SCHEDULE) == 0 ||// used by setup to close the schedule for client push          
            wcscmp(command, PARAM_REFRESH_CONFIG) == 0 ||
            wcscmp(command, PARAM_TWO_WAY_CONFIG) == 0
            ) ) {

                quit(0);
        }


        // If APP_RUN_AT_TIME, we set the Schedule and then we start 
        // the sync at the end of the WinMain (inside the 'else' statement)
        if(wcscmp(command, PARAM_REMOVE_SCHEDULE) == 0 ){
            WCHAR appName[MAX_PATH];
            getAppName(appName);
            CeRunAppAtTime(appName, 0);
            resetPIMModifications(NULL);
            retValue = NOT_RETURN_TO_MAIN_FORM;
            return retValue;
        }

        // If APP_RUN_AT_TIME, we set the Schedule and then we start 
        // the sync at the end of the WinMain (inside the 'else' statement)
        if(wcscmp(command, APP_RUN_AT_TIME) == 0 ){
            LogLevel l = getLOGLevel();
            LOG.setLevel(l);

            int pushValue = 0;
            string push = getRegConfig()->getPush();
            if (push.size() > 0) {
                pushValue = atoi(push.c_str());
            }
            if (pushValue > 0) {
                //
                // We have PUSH activated: it's a ClientPush sync.
                //
                sources = readPIMModifications(); 

                // Remove any schedule of startsync. It will be rescheduled at the
                // end of the sync, in case of failure.
                WCHAR appName[MAX_PATH];
                getAppName(appName);
                CeRunAppAtTime(appName, 0);    
                LOG.debug("startsync schedule removed after APP_RUN_AT_TIME");
                if (!sources) {
                    retValue = NOT_RETURN_TO_MAIN_FORM;
                    return retValue;
                }

                delete [] command;
                command = wstrdup(TEXT("clientPush"));
            }
            else {
                // It's a normal scheduled sync. 
                // Schedule now the next sync.
                schedule();
            }
        }
        /*
        * This is to sync the configuration parameter. 
        */
        else if(wcscmp(command, PARAM_REFRESH_CONFIG) == 0) {    
            getRegConfig()->setConfigSourcesParameter("config", "sync", "refresh-from-server");
            getRegConfig()->setDirty(CS_DIRTY_SOURCE_ENABLED);
            getRegConfig()->saveDirty();            
            delete [] command;
            command = wstrdup(TEXT("manual"));            
            
        } else if(wcscmp(command, PARAM_TWO_WAY_CONFIG) == 0) {   
            getRegConfig()->setConfigSourcesParameter("config", "sync", "two-way");
            getRegConfig()->setDirty(CS_DIRTY_SOURCE_ENABLED);
            getRegConfig()->saveDirty();            
            command = wstrdup(TEXT("manual"));                
        }           

        /**
        * This function is to send the log to the Technical Support
        * It changes the parameter as a normal manual sync of the email.
        * It is not to put in else as the other
        */
        if(wcscmp(command, PARAM_SEND_LOG) == 0) {            
            int m = sendLog();
            if (m == 0) {                
                delete [] command;
                command = wstrdup(TEXT("manual"));                        
            } else {
                LOG.error("The log cannot be sent");
            }
        }  

        //-------------------------

        /*
        * These two following test are only to understand and make right 
        * modification. So the Cradle action is normalized and the RunAppAtTime
        * currently only schedules the next time sync and leaves to the flow
        * the right choice.
        */
        if(wcscmp(command, PARAM_CLEANUP) == 0) {            
            vector<const wchar_t*> ss;
            for (int i = 0; sources[i] != NULL; i++) {
                ss.push_back(sources[i]);
            }
            cleanupAllItems(ss);
            // reset the pim flag after a cleanup because it writes the registry
            resetPIMModifications((const wchar_t**)sources);
            retValue = NOT_RETURN_TO_MAIN_FORM;
        } else if (wcsicmp(command, PARAM_NOTIFY) == 0 ){
            //This if for the on cradle notification
            delete [] command;
            command = wstrdup(TEXT("cradle"));
            //command = TEXT("cradle");
            if (sources) { for (int k = 0; sources[k]; k++) { delete [] sources[k]; sources[k] = NULL; } sources = NULL;}
    
            // if (sources){ delete [] sources;}  sources = NULL;
        } else if(wcscmp(command, PARAM_AVAILABLE) == 0) {
            getAvailability();
        } else if(wcscmp(command, PARAM_REMOVENOTIF) == 0) {
            removeNotif();
            retValue = NOT_RETURN_TO_MAIN_FORM;            
        } else if(wcscmp(command, PARAM_SCHEDULE) == 0) {
            LogLevel l = getLOGLevel();
            LOG.setLevel(l);
            schedule();                     
        } else if(wcscmp(command, PARAM_REGISTER) == 0) {            
            WCHAR c_szPath[MAX_PATH];
            getAppNameForSMS(c_szPath); // /Program files/Funambol/startsync.exe
            LogLevel l = getLOGLevel();
            LOG.setLevel(l);
            hr = PushRouter_RegisterClient(c_szContentType, c_szAppId, c_szPath, c_szParams);
            if (hr == S_OK) {
                LOG.debug("Wap push client registered successfully");
            } else if (hr == PUSHRTR_E_ALREADYREGISTERED) {
                LOG.debug("Wap push client already registered");
            } else {
                LOG.error("Error to register wap push client");
            }
            retValue = NOT_RETURN_TO_MAIN_FORM;
            
        } else if(wcscmp(command, PARAM_DEREGISTER) == 0) {
            //
            // unregister the startsync from the pushrouter
            //
            hr = PushRouter_UnRegisterClient(c_szContentType, c_szAppId);
            LogLevel l = getLOGLevel();
            LOG.setLevel(l);
            if (hr == S_OK) {
                LOG.debug("Wap push client unregistered successfully");
            } else if (hr == PUSHRTR_E_NOTFOUND) {
                LOG.debug("Wap push client not found. Arleady unregistered");
            } else {
                LOG.error("Error to unregister wap push client");
            }
            retValue = NOT_RETURN_TO_MAIN_FORM;
        } else if(wcscmp(command, c_szParams) == 0) {
            LogLevel l = getLOGLevel();
            LOG.setLevel(l);
            memset(&PushMsg, 0, sizeof(PUSHMSG));
            PushMsg.cbSize = sizeof(PUSHMSG);

            hr = PushRouter_Open(c_szContentType, c_szAppId, &hevtMsgAvailable, &hPushRouter);
            if (hr != S_OK) {
                LOG.error("Wap push: PushRouter_Open - it is not possible to register the client.");
            }
            while(WaitForSingleObject(hevtMsgAvailable, 10000) != WAIT_TIMEOUT) {
                hr = PushRouter_GetMessage(hPushRouter, &PushMsg);
                if (hr != S_OK) {
                    LOG.error("Error in PushRouter_GetMessage wap push client");
                    break;
                }
                wchar_t** ss;
                wchar_t** ws = NULL;
                int ret = handleWapPushMessage(&PushMsg, &ws);

                int i = 0;
                int a = 0;
                while(ws[i]){
                    char* srctocmp = toMultibyte(ws[i]);
                    if (!(strcmp(getRegConfig()->getConfigSourcesParameter(srctocmp, "sync"), "none") == 0)){
                        a++;
                    }
                    i++;
                    if (srctocmp){
                        delete [] srctocmp;
                    }
                }

                if(a != 0){
                    ss = new wchar_t*[a+1];

                    i = 0;
                    a = 0;
                    int sizews = 0;
                    while(ws[i]){

                        char* srctocmp = toMultibyte(ws[i]);
                        if (!(strcmp(getRegConfig()->getConfigSourcesParameter(srctocmp, "sync"), "none") == 0)){
                            sizews = wcslen(ws[i])+ 1;
                            ss[a] = new wchar_t[sizews];
                            wcscpy(ss[a], ws[i]);
                            a++;
                        }
                        if ( srctocmp ){
                            delete [] srctocmp;
                        }
                        i++;
                    }
                    ss[a] = 0;
                }
                else{
                    ss = NULL;
                }
                if (ret != 0) {
                    LOG.error("Wap push: error in handleWapPushMessage message");
                    break;
                }
                if (ss) {
                    if (askConfirm()) {
                                                
                        bool found = false;
                        int i = 0;
                        for (i = 0; ss[i]; i++) {

⌨️ 快捷键说明

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