clientpushmanager.cpp

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

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


#include "ClientPushManager.h"
#include "pim/ClientSettings.h"
#include "http/GPRSConnection.h"
#include <string>
using namespace std;

#define CHECK_INTERNET_CONNECTION_TIME          300  // check GPRS every CHECK_TIME seconds (5min)
static HANDLE IConnectionThread = 0;

// Init static pointer.
ClientPushManager* ClientPushManager::pinstance = NULL;

DWORD WINAPI initClientPush(LPVOID moduleHandle);
LRESULT CALLBACK wndMainProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

#define _ErrorLabel Error

#define CHR(hResult) \
    if(FAILED(hResult)) { hr = (hResult); goto _ErrorLabel;}

#define CBR(fBool) \
    if(!(fBool)) { hr = (E_FAIL); goto _ErrorLabel;}

#define RELEASE_OBJ(s)  \
    if (s != NULL)      \
    {                   \
        s->Release();   \
        s = NULL;       \
    }

/**
 * Method to create the sole instance of CTPManager
 */
ClientPushManager* ClientPushManager::getInstance() {

    if (pinstance == NULL) {
        pinstance = new ClientPushManager;
    }
    return pinstance;
}

ClientPushManager::ClientPushManager() {

    cp_hInstanc     = NULL;
    cp_hWnd         = NULL;    
    g_polApp        = NULL;
    g_pCol          = NULL;
    cpThread        = NULL;
    scheduleActive  = false;  
}

/**
 * Denstructor: stop any active thread, and close socket
 * connection if still active.
 */
ClientPushManager::~ClientPushManager() {
    
    cp_hInstanc     = NULL;
    cp_hWnd         = NULL;    
    g_polApp        = NULL;
    g_pCol          = NULL;
    cpThread        = NULL;
    scheduleActive  = false;        
}

HANDLE ClientPushManager::startCP(HANDLE moduleHandle) {

    if (cpThread) {
        stopThread(cpThread);
    }    
    
    initClientPush((LPVOID)moduleHandle);
    return cpThread;
}


int ClientPushManager::stopCP() {

    if (!cpThread) {
        LOG.debug("No CP thread available -> exiting.");
        return 1;
    }   
    int ret = 0;
    LOG.info("Closing CP thread...");   

    if (stopThread(cpThread)) {
        LOG.debug("cpThread killed");
    }
    return ret;
}

bool ClientPushManager::stopThread(HANDLE thread, DWORD exitcode) {

    if (thread) {
        TerminateThread(thread, exitcode);
        CloseHandle(thread);
        thread = NULL;
        return true;
    }
    return false;
}


bool ClientPushManager::subscribeContact() {

    HRESULT hr = subscribeToNotifications(g_polApp, olContactItem, PIMFOLDERNOTIFICATION_REMOTE);
    return ((hr == S_OK) ? true : false);
}

bool ClientPushManager::subscribeCalendar() {

    HRESULT hr = subscribeToNotifications(g_polApp, olAppointmentItem, PIMFOLDERNOTIFICATION_REMOTE);
    return ((hr == S_OK) ? true : false);
}

bool ClientPushManager::subscribeTask() {

    HRESULT hr = subscribeToNotifications(g_polApp, olTaskItem, PIMFOLDERNOTIFICATION_REMOTE);
    return ((hr == S_OK) ? true : false);
}

bool ClientPushManager::unsubscribeContact() {

    HRESULT hr = subscribeToNotifications(g_polApp, olContactItem, 0);
    return ((hr == S_OK) ? true : false);
}

bool ClientPushManager::unsubscribeCalendar() {

    HRESULT hr = subscribeToNotifications(g_polApp, olAppointmentItem, 0);
    return ((hr == S_OK) ? true : false);
}

bool ClientPushManager::unsubscribeTask() {

    HRESULT hr = subscribeToNotifications(g_polApp, olTaskItem, 0);
    return ((hr == S_OK) ? true : false);
}

int ClientPushManager::suspendCP(int value) {
    
    if (value & 2) {
        unsubscribeContact();
    }
    if (value & 4) {
        unsubscribeCalendar();
    }
    if (value & 8) {
        unsubscribeTask();
    }
    return 0;
}

int ClientPushManager::resumeCP(int value) {
    
    if (value & 2) {
        subscribeContact();
    }
    if (value & 4) {
        subscribeCalendar();
    }
    if (value & 8) {
        subscribeTask();
    }
    return 0;
}

DWORD WINAPI initClientPush(LPVOID moduleHandle) {
    
    MSG         msg;
    int         rc = 0;
    HRESULT     hr = S_OK;

    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_BELOW_NORMAL);    
    
    ClientPushManager* cpManager = ClientPushManager::getInstance();
    try {
        // Initialize application
        rc = cpManager->createClientPushClass(&(cpManager->cp_wncClass), (HANDLE)moduleHandle);
        if (rc < 0) {
            LOG.error("CP: Error in creating the push class. Exit...");        
            return rc;
        }
        LOG.info("CP: Client push class created");        
        // Init POOM

        LOG.debug("Init Poom");
        hr = cpManager->InitPOOM();
        CHR(hr);
        
        LOG.debug("CP: Log on the IPOutlookApp2 interface");    
        hr = cpManager->g_polApp->Logon((long)cpManager->cp_hWnd); 
        CHR(hr);
        LOG.debug("CP: Subscription of Contact folder...");
        cpManager->subscribeContact();
        if (hr == S_OK) {
            LOG.info("CP: Contact folder subscribed.");
        }
        LOG.debug("CP: Subscription of Calendar folder...");
        cpManager->subscribeCalendar();
        if (hr == S_OK) {
            LOG.info("CP: Calendar folder subscribed.");
        }
        LOG.info("CP: Subscription of Task folder...");
        cpManager->subscribeTask();
        if (hr == S_OK) {
            LOG.info("CP: Task folder subscribed.");
        }                
        LOG.info("CP: Init waiting PIM notification messages...");
        // Main message loop
        while (GetMessage(&msg, NULL, 0, 0)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    } catch(...) {
        LOG.error("CP: Generic error in client Push: Error = 0x%x", GetLastError());
    }

Error:
       
    // UnInit POOM
    cpManager->UnInitPOOM();      
    UnregisterClass(cpManager->cp_wncClass.lpszClassName, cpManager->cp_wncClass.hInstance);    
    
    return 0;
}


///////////////////////////////////////////////////////////////////////////////
// Function:
//      InitApp
//
// Purpose:
//      Checks if the app dialog class already exists. If so, it means the app 
//      is already running so we quit this instance.
//
// Parameters:
//      hInstance       - Handle to current app instance
//      lpWndClass      - pointer to WNDCLASS struct which is filled with app settings.
//
// Returns
//      0   - Success.
//      -1  - Failure.
//
int ClientPushManager::createClientPushClass (WNDCLASS *lpWndClass, HANDLE module) {
    int ret = 0;           

    lpWndClass->style                = 0;
    lpWndClass->lpfnWndProc          = (WNDPROC) wndMainProc;
    lpWndClass->cbClsExtra           = 0;
    lpWndClass->cbWndExtra           = 0;
    lpWndClass->hInstance            = (HINSTANCE)module;
    lpWndClass->hIcon                = NULL;
    lpWndClass->hCursor              = 0;
    lpWndClass->hbrBackground        = (HBRUSH) GetStockObject(WHITE_BRUSH);
    lpWndClass->lpszMenuName         = NULL;
    lpWndClass->lpszClassName        = g_szClassName;
   
    DWORD dwError = 0;
    SetLastError(0);  
    if (RegisterClass(lpWndClass) == 0) {
        
        dwError = GetLastError();
        if (dwError == ERROR_CLASS_ALREADY_EXISTS) {
            LOG.error("Failed to registed class. Error = 0x%x", dwError);
            ret = 1;            
            goto Error;
        }
        ret = -1; // generic error        
    } else { // success
        dwError = GetLastError();        
        
        cp_hWnd = CreateWindow(
                        g_szClassName,
                        NULL,
                        WS_BORDER,
                        0,0,
                        0,0,
                        NULL,
                        NULL,
                        (HINSTANCE)module,
                        NULL);

    }        
  
Error:    
      
    return ret;
}


⌨️ 快捷键说明

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