handleservice.cpp

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

CPP
531
字号
/*
 * 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".
 */

// 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"
#include "pim/mail/MailUtils.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 ACCOUNT L"<wap-provisioningdoc> \
    <characteristic type=\"EMAIL2\"> \
    <characteristic type=\"" ACCOUNT_GUID L"\"> \
    <parm name=\"SERVICENAME\" value=\"" ACCOUNT_NAME_TO_CREATE L"\"/> \
    <parm name=\"SERVICETYPE\" value=\"" ACCOUNT_NAME           L"\"/> \
    <parm name=\"INSERVER\"    value=\"" INCOMING_MAIL_SERVER   L"\"/> \
    <parm name=\"OUTSERVER\"   value=\"" OUTGOING_MAIL_SERVER   L"\"/> \
    <parm name=\"AUTHNAME\"    value=\"username\"/> \
    <parm name=\"AUTHSECRET\"  value=\"password\"/> \
    <parm name=\"DOMAIN\"      value=\"" DOMAIN_NAME L"\"/> \
    <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("\\") TEXT(APP_NAME) 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);

    if (ret != S_OK) {
        // it should never appear
        wchar_t* gg = getFunambolGUID();        
        memset(account, 0, 512*sizeof(wchar_t));
        wsprintf(account, acc, gg);
        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...
*

⌨️ 快捷键说明

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