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

📄 handleservice.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
字号:
/*
 * Copyright (C) 2006-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
 */

// handleservice.cpp - module used to deregister push setting by setup.dll
//
//

// Windows Header Files:
#include <windows.h>
#include "notify/s4n_service.h"
#include "Tlhelp32.h"
#include "http/GPRSConnection.h"
#include "pim/difftime.h"
#include <Notify.h>
#include "pim/SettingFunctions.h"
#include "pim/account.h"
#include "base/startcmd.h"
#include "base/Log.h"
#include "base/util/utils.h"
#include "pim/ClientSettings.h"
#include "customization.h"

#if _WIN32_WCE > 0x500
    #include <snapi.h>
#endif

#include <mapidefs.h>
#include <mapicode.h>
#include <mapitags.h>
#include <mapix.h>
#include <mapiutil.h>
#include <cemapi.h>

#include "Cfgmgrapi.h" //-- for DMProcessConfigXML

#if _WIN32_WCE > 0x500
#include "Regext.h"
#endif

#define PROGRAM_DIR PROVIDER_W

#define ACCOUNT L"<wap-provisioningdoc> \
    <characteristic type=\"EMAIL2\"> \
    <characteristic type=\"{F9043C85-F6F2-101A-A3C9-08002B2F49FB}\"> \
    <parm name=\"SERVICENAME\" value=\"" PROVIDER_W L"\"/> \
    <parm name=\"SERVICETYPE\" value=\"" PROVIDER_W L"\"/> \
    <parm name=\"INSERVER\"    value=\"sync.funambol.com\"/> \
    <parm name=\"OUTSERVER\"   value=\"sync.funambol.com\"/> \
    <parm name=\"AUTHNAME\"    value=\"username\"/> \
    <parm name=\"AUTHSECRET\"  value=\"password\"/> \
    <parm name=\"DOMAIN\"      value=\"funambol.com\"/> \
    <parm name=\"REPLYADDR\"   value=\"default@email\"/> \
    <parm name=\"NAME\"        value=\"\"/> \
    <parm name=\"LINGER\"      value=\"0\"/> \
    </characteristic> \
    </characteristic> \
    </wap-provisioningdoc>"

const wchar_t* acc  = TEXT("<wap-provisioningdoc><characteristic type=\"EMAIL2\"><nocharacteristic type=\"%s\"/></characteristic></wap-provisioningdoc>");


#define CHECK_TIME 5  // check GPRS every CHECK_TIME minutes
#define HANDLE_SERVICE_APP  TEXT("handleservice.exe")
#define FUNAMBOL_LNK TEXT("\\") PROVIDER_W TEXT(".lnk")


void createAccount() {
    LPWSTR out = NULL;
    wchar_t* ret = NULL;
    ret = getFunambolGUID();
    HRESULT hr;
    if (!ret) {
        hr = DMProcessConfigXML(ACCOUNT, CFGFLAG_PROCESS, &out);
    }

}

void deleteAccount(wchar_t* guid) {
    LPWSTR out = NULL;
    wchar_t account[512];

    memset(account, 0, 512*sizeof(wchar_t));
    wsprintf(account, acc, guid);
    HRESULT ret = DMProcessConfigXML(account, CFGFLAG_PROCESS, &out);
}

int deregister() {
    return deregisterService();
}

int terminateStartsyncProcess() {
    DWORD out = 0;
    PROCESSENTRY32 lppe;
    BOOL next = TRUE;
    HANDLE hProcess = 0;

    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); // 0 is however ignored

    if (hSnapshot == INVALID_HANDLE_VALUE)
        return 0;

    lppe.dwSize = sizeof( PROCESSENTRY32 );

    if (!Process32First(hSnapshot, &lppe))
        return 1;

    if (wcsstr(lppe.szExeFile, TEXT("startsync.exe")) != NULL) {
        hProcess = OpenProcess(0, FALSE, lppe.th32ProcessID);
        TerminateProcess(hProcess, -1);
        goto exit;
    }

    do {
        next = Process32Next(hSnapshot, &lppe);

        if (!next)
            break;

        if (wcsstr(lppe.szExeFile, TEXT("startsync.exe")) != NULL ) {
            hProcess = OpenProcess(0, FALSE, lppe.th32ProcessID);
            TerminateProcess(hProcess, -1);
            goto exit;
        }

    } while(next);

exit:
    if (hSnapshot)
        CloseToolhelp32Snapshot(hSnapshot);
    CloseHandle(hProcess);
    return 0;

}

static void getAppName(wchar_t* appName) {

    wchar_t path[256];
    getClientConfigurationInternal (NULL, PROPERTY_SPDM_PATH_INFO, path, NULL);
    wcscpy(appName, path);
    wcscat(appName, TEXT("\\"));
    wcscat(appName, HANDLE_SERVICE_APP);    
}
/*
* This function check if GPRS is active and if not try to activate it.
*/
BOOL checkGPRS() {
    return EstablishConnection();
}

/*
* Needed to create the link in the automatic execution to start automatically if
* the service is registered
*/

void createLink() {
    wchar_t pathRetrieved[512];
    SHGetSpecialFolderPath(NULL, pathRetrieved, CSIDL_STARTUP, 0);
    wcscat(pathRetrieved, FUNAMBOL_LNK);
    wchar_t applicationName[512];
    getAppName(applicationName);

    wstring s;
    s.append(TEXT("\""));
    s.append(applicationName);
    s.append(TEXT("\" registerByLink"));
    SHCreateShortcut(pathRetrieved, (wchar_t*)s.c_str());

}

void deleteLink() {

    wchar_t pathRetrieved[512];
    SHGetSpecialFolderPath(NULL, pathRetrieved, CSIDL_STARTUP, 0);
    wcscat(pathRetrieved, FUNAMBOL_LNK);
    DeleteFile(pathRetrieved);
}

/*
*
*/
void scheduleNextGPRSCheck(int time) {


    SYSTEMTIME lpTime;
    SYSTEMTIME pollTime;

    wchar_t appName[256];
    getAppName(appName);
  
    GetLocalTime(&lpTime);
    pollTime = DT_AddDiff(nano100SecInMin, time, &lpTime);
    
    /*
    CE_NOTIFICATION_TRIGGER ce;
    ce.dwSize = sizeof(CE_NOTIFICATION_TRIGGER);
    ce.dwType = CNT_TIME;
    ce.dwEvent = NULL;
    ce.lpszApplication = appName;
    ce.lpszArguments = TEXT("after");
    ce.stStartTime = pollTime;
    pollTime.wYear += 20;
    ce.stEndTime = pollTime;

    HANDLE handle = NULL;
    handle = CeSetUserNotificationEx(NULL, &ce, 0);

    */
                

    if (time == 0) {
        CeRunAppAtTime(appName, 0);
    } else {
        GetLocalTime(&lpTime);
        pollTime = DT_AddDiff(nano100SecInMin, time, &lpTime);
        CeRunAppAtTime(appName, &pollTime);
    }
    

}

/*
* The way to identify the cradle event. If the device is cradled it is possible
* to get the event and start the sync automatically. Remember to modify the uninstall
* procedure too...
*
*/

#if _WIN32_WCE > 0x500

TCHAR szNotificationName[] = TEXT("FunambolPluginCradled");
TCHAR registryName[] = TEXT("System\\State\\Hardware");
TCHAR registryKey[]  = TEXT("Cradled");

HRESULT NotifyCradleEvent() {

    NOTIFICATIONCONDITION nc;
    HRESULT hr = S_OK;

    //const wchar_t* programPath= getProgramPath();
    const wchar_t* programPath = toWideChar(getRegConfig()->getAppPath().c_str());
    if (wcscmp(programPath, L"") == 0) {
        LOG.info("Notification cralde event: it is not possible to register the service: path not found: %s", programPath);
        return S_FALSE;
    }

    wstring szAppName(programPath);
    szAppName.insert(0,L"\"");
    szAppName += L"\\";
    szAppName += SYNCAPP;
    szAppName += L"\"";


    // Receive notifications whenever the Windows Mobile-based device is cradled.
    nc.ctComparisonType = REG_CT_EQUAL;
    nc.TargetValue.dw   = 1;

    // Since you are not masking, dwMask must be non-zero for dwords.
    nc.dwMask = 0xFFFFFFFF;

    // The application is launched from the command line with: myApp.exe param1 param2 /notify "myAppCradled".
    hr = RegistryNotifyApp(HKEY_LOCAL_MACHINE,
                           registryName,
                           registryKey,
                           szNotificationName,
                           szAppName.c_str(),
                           NULL,
                           NULL,
                           0,
                           0,
                           &nc);

    if (hr == S_OK) {
        LOG.debug("Cradle notification succesfully registered");
    } else if (hr == E_ALREADY_REGISTERED) {
        LOG.debug("Cradle notification already registered");
    } else {
        LOG.debug("Cradle notification generic error");
    }
    
    if (programPath) {
        delete [] programPath;
    }
    return hr;
}

HRESULT RemoveNotifyCradleEvent() {
    HRESULT hr = RegistryStopNotification(szNotificationName);

    if (hr == S_OK) {
        LOG.debug("Cradle notification succesfully removed");
    } else {
        LOG.debug("Cradle notification generic error in removing: it cannot be found");
    }

    return hr;
}

#endif

//-------------------------------------------------------------------------- MAIN

int WINAPI WinMain(	HINSTANCE hInstance,
					HINSTANCE hPrevInstance,
					LPTSTR    lpCmdLine,
					int       nCmdShow)
{
    int ret = 0;
    //MessageBox(NULL,  TEXT("Inside"), TEXT("Alert"), MB_OK);

    if(wcscmp(lpCmdLine,_T("registerByLink")) == 0) {
        
        scheduleNextGPRSCheck(0);
        scheduleNextGPRSCheck(CHECK_TIME);
        if (checkGPRS()) {
            ret =  registerService();
        }

    } else if(wcscmp(lpCmdLine,_T("register")) == 0) {
        terminateStartsyncProcess();
        ret =  registerService();
        checkGPRS();
        scheduleNextGPRSCheck(CHECK_TIME);
        createLink();
    } else if(wcscmp(lpCmdLine,_T("deregister")) == 0) {
        terminateStartsyncProcess();
        ret = deregister();
        scheduleNextGPRSCheck(0);
        deleteLink();
    } else if(wcscmp(lpCmdLine,_T("createAccount")) == 0) {
        terminateStartsyncProcess();
        createAccount();
    } else if(wcscmp(lpCmdLine,_T("deleteAccount")) == 0) {
        terminateStartsyncProcess();
        wchar_t* xx = getFunambolGUID();
        deleteAccount(xx);
    } else if (wcscmp(lpCmdLine, APP_RUN_AT_TIME) == 0) {
        
        /*
        HANDLE hMutex;//Handle to mutex...

        // Create mutex here
        hMutex = CreateMutex(NULL, TRUE, TEXT("checkConnectionInProgress") );

        switch(GetLastError()) {
            case ERROR_SUCCESS:
                break;
            case ERROR_ALREADY_EXISTS:                                    
                return -2;
            default:                
                return -1;
        }
        */
        if (checkGPRS()) {
            registerService();
        }
        scheduleNextGPRSCheck(CHECK_TIME);            
        //CloseHandle( hMutex );
        
    }  


#if _WIN32_WCE > 0x500
    //
    // try to get the cradle event only in WM5
    //
    else if (wcscmp(lpCmdLine, L"registerCradle") == 0) {
        NotifyCradleEvent();
    } else if (wcscmp(lpCmdLine, L"deregisterCradle") == 0) {
        RemoveNotifyCradleEvent();
    }   

#endif
	return ret;
}

⌨️ 快捷键说明

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