account.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 334 行

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

/***********************************************************************
 * Email account handling functions
 ***********************************************************************/

#include <windows.h>
#include <stdio.h>
#include <cfgmgrapi.h>
#include "pim/account.h"
#include "base/Log.h"
#include "customization.h"


#define EXIT_ON_FAILED(_hr)   \
      if (FAILED(_hr))  {  \
        goto FuncExit; \
      }

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


// #define ACCOUNT_KEY L"F9043C85-F6F2-101A-A3C9-08002B2F49FB" // no more used

#define ACCOUNT_QUERY L"<wap-provisioningdoc>\
                            <characteristic type=\"EMAIL2\">\
                                <characteristic-query type=\"%s\"/>\
                            </characteristic>\
                        </wap-provisioningdoc>"

#define MODIFY_ACCOUNT L"<wap-provisioningdoc>\
                           <characteristic type=\"EMAIL2\">\
                             <characteristic type=\"%s\">\
                               <parm name=\"REPLYADDR\" value=\"%s\"/>\
                               <parm name=\"NAME\" value=\"%s\"/>\
                             </characteristic>\
                           </characteristic>\
                         </wap-provisioningdoc>"

/*
 * Modify visible name and email address on Funambol Account
 */
HRESULT modifyAccount(const wchar_t* name, const wchar_t* replyAddress) {
    LPWSTR out = NULL;
    wchar_t* guid = getFunambolGUID();
    wchar_t account[512];
    wsprintf(account, MODIFY_ACCOUNT, guid, replyAddress, name);
    HRESULT ret = DMProcessConfigXML(account, CFGFLAG_PROCESS, &out);

    if (out) delete [] out;
    if (guid) { delete [] guid; guid = NULL; }
    return ret;
}

static HRESULT getParameter(const wchar_t *xml, const wchar_t *parname, wchar_t *value)
{
    wchar_t pname[ACCOUNT_PARAM_SIZE];
    wsprintf(pname, L"\"%s\"", parname);
    const wchar_t VAL[] = L"value=\"";

    wchar_t *p1 = wcsstr(xml, pname);
    if(!p1){
        LOG.error("getAccountInfo: param '%s' not found.", parname);
        return -1;
    }
    p1 = wcsstr(p1, VAL);
    if(!pname){
        LOG.error("getAccountInfo: error parsing '%s'.", parname);
        return -1;
    }
    p1 += wcslen(VAL);
    wchar_t *end = wcsstr(p1, L"\"");
    if(!end){
        LOG.error("getAccountInfo: error parsing '%s'.", parname);
        return -1;
    }
    size_t len = end-p1;
    if(len > ACCOUNT_PARAM_SIZE-1){
        LOG.info("getAccountInfo: '%s' truncated.", parname);
        len = ACCOUNT_PARAM_SIZE-1;
    }
    wcsncpy(value, p1, len);
    value[len]=0;

    return 0;
}

HRESULT getAccountInfo(wchar_t* name, wchar_t* replyaddress) {
    return getAccountInfoSession(name, replyaddress, NULL);
}
BOOL doesFunambolAccountExist() {
    wchar_t* guid = getFunambolGUID(NULL);
    if (guid == NULL) // funambol account not found
        return FALSE;
    else {
        delete [] guid;
        return TRUE;
    }
}


/*
 * Get Funambol account information
 */
HRESULT getAccountInfoSession(wchar_t* name, wchar_t* replyaddress, IMAPISession* pSession)
{
    LPWSTR out = NULL;
    wchar_t* guid = getFunambolGUID(pSession);
    if (guid == NULL) // funambol account not found
        return -1;
    wchar_t account[512];
    wsprintf(account, ACCOUNT_QUERY, guid);

    HRESULT ret = DMProcessConfigXML(account, CFGFLAG_PROCESS, &out);
    if (ret) {
        LOG.error("getAccountInfo: error %s in DMProcessConfigXML.", ret);
        return ret;
    }
    if (!out) {
        LOG.error("getAccountInfo: DMProcessConfigXML returned null.");
        return -1;
    }

#if 0
    //////////////////////////////////////////DEBUG
    FILE *f = fopen("/AccountLog.txt", "w");
    if(f){
        fprintf(f, "%ls", out);
        fclose(f);
    }
    else{
        LOG.error("Error opening file");
    }
    //////////////////////////////////////////DEBUG
#endif

    // Get Name ----------------------------------------------
    ret = getParameter(out, L"NAME", name);
    if (ret){
        goto finally;
    }
    // Get Address -------------------------------------------
    ret = getParameter(out, L"REPLYADDR", replyaddress);
    if(ret){
        goto finally;
    }

finally:
    if (guid) {delete [] guid; guid = NULL; }
    delete [] out;
    return ret;
}


enum {
    ePR_ENTRYID,
    ePR_DISPLAY_NAME,
    ePR_CE_UNIQUE_STORE_ID,
    NUM_COLS
    };

// These tags represent the message information we would like to pick up
static SizedSPropTagArray(NUM_COLS, Columns) =
{
    NUM_COLS,
    PR_ENTRYID,
    PR_DISPLAY_NAME,
    PR_CE_UNIQUE_STORE_ID
};

/*
* It always return a valid GUID. At the beginning it look for the existing one created in the prorammatically
* creation of the account. If non exists, maybe because the user deleted the old and created a new one manually,
* the procedure set a new GUID and return it to delete the account properly.
* the same GUID is {A3D64F8C-1BDC-4ac6-92A4-4D85213F96CD}.
* In the same way there is a similar procedure written in setup.cpp to always modify the account.
* If the account was generated manually, this same guid is inserted.
*/

wchar_t* getFunambolGUID(IMAPISession* pSession) {

    HRESULT           hr;
    IMAPITable*       pTable      =  NULL;
    SRowSet*          psrs        =  NULL;
    IMsgStore*        pStore      =  NULL;
    BOOL accountFound             =  FALSE;
    wchar_t* ret                  =  NULL;
    BOOL isSessionCreated         =  FALSE;

    if (!pSession) {
        isSessionCreated = TRUE;
        hr = MAPILogonEx (NULL, NULL, NULL, NULL, &pSession);
        EXIT_ON_FAILED (hr);
    }

    // Get the message stores table
    hr = pSession->GetMsgStoresTable (MAPI_UNICODE, &pTable);
    EXIT_ON_FAILED (hr);

    hr = pTable->SetColumns((LPSPropTagArray)&Columns, 0);
    EXIT_ON_FAILED(hr);

    while (1) {
        // Get a row
        hr = pTable->QueryRows (1, 0, &psrs);
        EXIT_ON_FAILED (hr);

        // Did we hit the end of the table?
        if (psrs->cRows != 1) {
          break;
        }

        if (psrs->aRow[0].lpProps[ePR_DISPLAY_NAME].ulPropTag == PR_DISPLAY_NAME) {

            wchar_t* displayName = psrs->aRow[0].lpProps[ePR_DISPLAY_NAME].Value.lpszW;
            //MessageBox (NULL, displayName, TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);

            if (wcscmp(displayName, ACCOUNT_NAME) == 0) {
                accountFound = TRUE;
                // Now we got the GUID
                LPGUID pGuid = NULL;

                ret = new wchar_t[64];
                memset(ret, 0, 64*sizeof(wchar_t));

                if (psrs->aRow[0].lpProps[ePR_CE_UNIQUE_STORE_ID].ulPropTag == PR_CE_UNIQUE_STORE_ID) {
                    //MessageBox (NULL, TEXT("GUID ok"), TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);
                    pGuid = psrs->aRow[0].lpProps[ePR_CE_UNIQUE_STORE_ID].Value.lpguid;

                }
                else {
                    //MessageBox (NULL, TEXT("GUID NULL"), TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);

                    // Open this message store
                    hr = pSession->OpenMsgStore (NULL,
                                   psrs->aRow[0].lpProps[0].Value.bin.cb,
                                  (ENTRYID *) psrs->aRow[0].lpProps[0].Value.
                                   bin.lpb, NULL, 0, &pStore);

                    SPropValue rgprops[1] = {0};
                    ULONG    cProps   = 0;

                    rgprops[cProps].ulPropTag = PR_CE_UNIQUE_STORE_ID;

                    pGuid = new GUID();
                    pGuid->Data1 = 0xA3D64F8C;
                    pGuid->Data2 = 0x1BDC;
                    pGuid->Data3 = 0x4ac6;
                    pGuid->Data4[0] = (char)0x92;
                    pGuid->Data4[1] = (char)0xA4;
                    pGuid->Data4[2] = (char)0x4D;
                    pGuid->Data4[3] = (char)0x85;
                    pGuid->Data4[4] = (char)0x21;
                    pGuid->Data4[5] = (char)0x3F;
                    pGuid->Data4[6] = (char)0x96;
                    pGuid->Data4[7] = (char)0xCD;

                    rgprops[cProps].Value.lpguid = pGuid;
                    ++cProps;

                    hr = pStore->SetProps(cProps, rgprops, NULL);

                }
                wsprintf(ret,
                    TEXT("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}"),
                    pGuid->Data1, pGuid->Data2, pGuid->Data3,
                    pGuid->Data4[0], pGuid->Data4[1],
                    pGuid->Data4[2], pGuid->Data4[3],
                    pGuid->Data4[4], pGuid->Data4[5],
                    pGuid->Data4[6], pGuid->Data4[7]);
                    //MessageBox (NULL, ret, TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);
            }
        }

        FreeProws   (psrs);
        psrs    =  NULL;
        RELEASE_OBJ (pStore);
        if (accountFound) {
            break;
        }
    }

FuncExit:

    FreeProws   (psrs);
    RELEASE_OBJ (pStore);
    RELEASE_OBJ (pTable);

    if (isSessionCreated)
        RELEASE_OBJ (pSession);

    return ret;
}

⌨️ 快捷键说明

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