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

📄 clientutil.cpp

📁 funambol window mobile客户端源代码
💻 CPP
字号:
/*
 * 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 "stdafx.h"
#include "pim/maincpp.h"
#include "pim/SettingFunctions.h"
#include "base/startcmd.h"
#include "notify/timed_msgbox.h"
#include "HwndFunctions.h"
#include "spds/constants.h"
#include "Winuser.h"

#include "ui.h"
#include "ClientUtil.h"
#include "base/util/utils.h"
#include "processUtils.h"
#include "pim/ClientSettings.h"



// items in listboxes MUST be load in this order
int SynctypeReg2UI(const char* sSyncType)
{
	int pos = 0;

	if(! strcmp(sSyncType,"two-way"))
		pos=0;
	if(! strcmp(sSyncType, "one-way-server"))
		pos=1;
	if(! strcmp(sSyncType,"one-way-client"))
		pos=2;

	return pos;
}


const char* SynctypeUI2Reg(int nPos)
{
	switch(nPos)
	{
	case 0:
           return "two-way";
	case 1:
           return "one-way-server";
	case 2:
           return "one-way-client";
	}

    // should not get here
    return "";
}


/**
 * @return -1= NO, 0:YES, 1=Not running
 */
int EndAppQuery(HWND wndUIWindow)
{
    int ret = -1;
    CString s1;
    int processID = 0;
    HWND wnd = NULL;
    wnd = ::FindWindow(TEXT("funambolstartsync"), NULL);
    if ((processID = checkStartSync()) != 0)
    {
        // sync is in progress
        HwndFunctions::closePreviousMsgBox();
        s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_INTERRUPT_SYNC);
        //s1 = "Do you really want to interrupt the synchronization?";
        int retMsgBox = TimedMessageBox(wndUIWindow, s1, getLocalizationUtils()->getLocalizationString(IDS_FUNAMBOL_ALERT),
            MB_YESNO | MB_ICONQUESTION | MB_SETFOREGROUND, 10 * 1000);

        if(retMsgBox == IDYES)
        {
            // send msg to startsync to stop
            if(wnd){
                ::PostMessage(wnd, WM_COMMAND, (WPARAM)10, NULL);
            }
            Sleep(500);
            HANDLE h = OpenProcess(0, FALSE, processID);
            if (h) {
            DWORD code = 0;
                GetExitCodeProcess(h, &code);
            if (code == STILL_ACTIVE) {
                    startProgram(SYNCAPP, TEXT("removeNotif"));
                    TerminateProcess(h, -6); //-6 code for the user stop sync.
                    CloseHandle(h);
                    setClientConfiguration(NULL, PROPERTY_SPDM_PID, TEXT("0"), NULL);
                    CString s; s.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_CODE_SYNC_STOPPED);
                    LOG.info(_wcc(s.GetString()));
                }
            }
            // The user answered YES
            return 0;
        }
    }
    else {
        // send msg to startsync to remove the notif Icon if there is again
        startProgram(SYNCAPP, TEXT("removeNotif"));
        // The process is not running
        return 1;
    }
    // The user answered no
    return -1;
}


int getSyncModeCode(const char* syncMode){
    int code = SYNC_NONE;

    if(strcmp(syncMode, "none") == 0)
        code = SYNC_NONE;
    if(strcmp(syncMode, "two-way") == 0)
        code = SYNC_TWO_WAY;
    if(strcmp(syncMode, "slow") == 0)
        code = SYNC_SLOW;
    if(strcmp(syncMode, "one-way-from-client") == 0)
        code = SYNC_ONE_WAY_FROM_CLIENT;
    if(strcmp(syncMode, "refresh-from-client") == 0)
        code = SYNC_REFRESH_FROM_CLIENT;
    if(strcmp(syncMode, "one-way-from-server") == 0)
        code = SYNC_ONE_WAY_FROM_SERVER;
    if(strcmp(syncMode, "refresh-from-server") == 0)
        code = SYNC_REFRESH_FROM_SERVER;

    return code;
}

const char* getSyncModeName(int code) {
    switch (code) {
        case SYNC_NONE:
            return "none";
            break;
        case SYNC_TWO_WAY:
        case SYNC_TWO_WAY_BY_SERVER:
            return "two-way";
            break;
        case SYNC_SLOW:
            return "slow";
            break;
        case SYNC_ONE_WAY_FROM_CLIENT:
        case SYNC_ONE_WAY_FROM_CLIENT_BY_SERVER:
            return "one-way-from-client";
            break;
        case SYNC_REFRESH_FROM_CLIENT:
        case SYNC_REFRESH_FROM_CLIENT_BY_SERVER:
            return "refresh-from-client";
            break;
        case SYNC_ONE_WAY_FROM_SERVER:
        case SYNC_ONE_WAY_FROM_SERVER_BY_SERVER:
            return "one-way-from-server";
            break;
        case SYNC_REFRESH_FROM_SERVER:
        case SYNC_REFRESH_FROM_SERVER_BY_SERVER:
            return "refresh-from-server";
            break;
        default:
            return NULL;
            break;
    }
}

BOOL existsDirectory(wchar_t* dir) {

    WIN32_FIND_DATA FileData;
    BOOL ret = TRUE;
    HANDLE hFind;

    hFind = FindFirstFile(dir, &FileData);

    if (hFind == INVALID_HANDLE_VALUE) {
        ret = FALSE;
    } else {
        ret = TRUE;
    }
    if (hFind) {
        FindClose(hFind);
    }

    return ret;
}

/*
 * Modify the window position and size of the specified offset.
 * 
 * See ClientUtil.h for the doc.
 */
void modifyWindow(CWnd *wnd, int x, int y, int cx, int cy) {
    RECT rc;
    wnd->GetWindowRect(&rc);
    rc.left   += x;
    rc.top    += y;
    rc.right  += x + cx;
    rc.bottom += y + cy;
    
    wnd->MoveWindow(&rc);
}

/*
 * Modify the window size to fit the parent one.
 * 
 * See ClientUtil.h for the doc.
 */
void adjustToParent(CWnd *wnd, int opt, int lx, int rx, int ty, int by) {
    RECT rc, parentRect;

    wnd->GetWindowRect(&rc);
    wnd->GetParent()->GetWindowRect(&parentRect);

    if (opt & ADJUST_WIDTH) {
        rc.left   = parentRect.left + lx;
        rc.right  = parentRect.right - rx;
    }
    if (opt & ADJUST_HEIGHT) {
        rc.top    = parentRect.top + ty;
        rc.bottom = parentRect.bottom - by;
    }
    wnd->MoveWindow(&rc);
}

CString escapeAmpString(CString s) {
    
    CString current = s;
    int pos = 0;
    
    while(pos != -1 && pos <= current.GetLength()) {
        pos = current.Find('&', pos);
        if (pos != -1) {
            current.Insert(pos, '&');
            pos += 2;
        }
    }
    return current;
}

void drawLine(CDC *dc, CPoint& start, CPoint& end, COLORREF color, int width, int penStyle) {

    // Set the pen and save tho old one
	CPen pen(penStyle, width, color);
    CPen* oldPen = dc->SelectObject(&pen);

	dc->MoveTo(start.x, start.y);
	dc->LineTo(end.x,   end.y);

    // Restore the old pen
	dc->SelectObject(oldPen);
}


void EnableButton(HWND dialog, int buttonID, bool bEnable) {

    HWND hwndMB = SHFindMenuBar(dialog);
    TBBUTTONINFO tbi;
    if (!hwndMB)
        return;

    memset (&tbi, 0, sizeof (tbi));
    tbi.cbSize = sizeof (tbi);
    tbi.dwMask = TBIF_STATE;
    SendMessage(hwndMB, TB_GETBUTTONINFO, buttonID, (LPARAM)&tbi);

    if(bEnable)  tbi.fsState = TBSTATE_ENABLED;
    else         tbi.fsState &= ~TBSTATE_ENABLED;

    SendMessage(hwndMB, TB_SETBUTTONINFO, buttonID, (LPARAM)&tbi);
}




DWORD WINAPI configWorker(LPVOID lpv)
{
    LOG.debug("Starting configWorker thread");

    // The dialog HWND that called this function.
    HWND dialogWnd = (HWND)lpv;
    CString s1;

    //
    // Start the config sync, in a process "startsync.exe config".
    //
    PROCESS_INFORMATION procinfo;
    DWORD exitcode = -2;
    wchar_t *path = NULL;
    wstring cmd;
    ClientSettings* cs = getRegConfig();
    path = toWideChar(cs->getAppPath().c_str());
    
    if (path) {
        cmd += path;
        delete [] path;
    }
    cmd += TEXT("\\startsync.exe");

    //
    // SYNC TYPE: keep 'refresh-from-server' if it's set from registry.
    //            Otherwise it's a two-way.
    wstring configSyncType;
    SyncSourceConfig* ssc = cs->getSyncSourceConfig("config");
    if (!ssc) {
        LOG.error("Config Syncsource not existing, aborting config sync.");
        goto finally;
    }
    if (!strcmp(ssc->getSync(), "refresh-from-server")) { 
        configSyncType = TEXT("refresh-config"); 
    }
    else { 
        // default
        configSyncType = TEXT("two-way-config"); 
    }

    configSyncType += TEXT(" config");


    //-----------------------------------------------------------
    if (!CreateProcess(cmd.c_str(), configSyncType.c_str(),
                       NULL, NULL, FALSE, 0,
                       NULL, NULL, NULL, &procinfo ) ) {
        LOG.error("%s process not created: error", cmd.c_str());
        goto finally;
    }
    //-----------------------------------------------------------
    
    int timeout = 120;
    DWORD waitResult = WaitForSingleObject(procinfo.hProcess, timeout*1000);
    switch (waitResult) {
        // Thread exited, get exit code.        
        case WAIT_ABANDONED:
            LOG.debug("refresh-config abandoned");
            exitcode = -1;
            break;

        case WAIT_OBJECT_0: {
            ::SetForegroundWindow(dialogWnd);
            GetExitCodeProcess(procinfo.hProcess, &exitcode);
            LOG.debug("configSync ended with code %d", exitcode);            
            break;
        }
        
        // Thread is still running after timeout -> kill it and exit.
        case WAIT_TIMEOUT: {
            LOG.debug("Timeout - configSync will now be terminated");
            TerminateProcess(procinfo.hProcess, 1);            
            s1.LoadString(getLocalizationUtils()->getLocaleResource(), IDS_NETWORK_ERROR);
            AfxMessageBox(s1);
            exitcode = -3;
            break;
        }

        // Some error occurred (case WAIT_FAILED)
        default: {
            LOG.debug("Wait error on configSync");
            TerminateProcess(procinfo.hProcess, 1);
            exitcode = -4;
            break;
        }                     
    }

finally:
    // Close the dialog
    SendMessage(dialogWnd, ID_MYMSG_CONFIG_SYNC_ENDED, exitcode, NULL);
    return 0;
}

⌨️ 快捷键说明

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