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

📄 poomserver2client.cpp

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

//
// @author Marco Magistrali
//

#include <windows.h>
#include "pimstore.h"
#include <stdio.h>

#include "base/Log.h"
#include "base/util/utils.h"
#include "spds/spdsutils.h"
#include "spds/Constants.h"

#include "pim/Utils.h"
#include "pim/maincpp.h"
#include "pim/ContactBuilder.h"
#include "pim/AppointmentBuilder.h"
#include "pim/TaskBuilder.h"
#include "pim/POOMServer2Client.h"
#include "pim/POOMFileManagement.h"

#include "vocl/VConverter.h"

#include <vector>
#include <list>
#include <map>
using namespace std;

/**
 * Create a WinContact that could be a WinConctact or WinContactSIF
 */

WinContact *createWinContact(bool isSif, wstring& ptrData, const wchar_t **fields) {
        
    if (isSif) {
        return new WinContactSIF(ptrData, fields); 
    }
    else {
        return new WinContact(ptrData); 
    }
}

/*
* Function return the code for the get_Oid function. Using PPC-WM5 the oid could have
* a value < 0. So it the value of OID is
*/

long manageNewItems(SyncItem* item, int dataType, long *oid, wchar_t* path, bool isSIF, map<long,long> &currentOidHash) {

    LOG.debug("Enter in manageNewItemsPOOM ...");    
    IP_OUTLOOK_APP* polApp = getOutlookApp();
    if (polApp == NULL)
        return -1;

    HRESULT hr = -1;

    long oidNew = 0;
    int size = 0;
    wchar_t * ptrData = NULL;
    wchar_t* t = NULL;
    char* data = NULL;

    IFolder *pFolder = NULL;
    IPOutlookItemCollection *pItems = NULL;

    size = item->getDataSize();
    data = new char[size + 1];
    memset(data, 0, size + 1);
    memcpy(data, item->getData(), size);

    ptrData = toWideChar(data);

    delete [] data; data = NULL;
    wstring ptrDataString = ptrData;
    
    // try to update the way to create the sif and vcard.
    // currently there is only for contacts
    // REMOVE AFTER THE WHOLE IPOUTLOOK_APP2
    if (dataType != olFolderContacts) {
        if (isSIF) {
            replaceAmpLGt(ptrDataString);
        }
        if (ptrData) {
            delete [] ptrData; ptrData = NULL;
        }
        ptrData = (wchar_t*)ptrDataString.data();
    } else {
        
        delete [] ptrData; ptrData = NULL;
    }

    // to create contacts
    if (dataType == olFolderContacts) {               

        WinContact *winC = createWinContact(isSIF, ptrDataString, contactFields);       
        
        IItem* ppItem     = NULL;
        IDispatch *pDisp  = NULL;
        hr = polApp->CreateItem(olContactItem, &pDisp);   

        // Get the IItem interface the newly created item.
        hr = pDisp->QueryInterface(IID_IItem, (LPVOID*)&ppItem);
        
        if (winC) {
            winContactToContact(*winC, ppItem);                    
            oidNew = getIItemOid(ppItem);              

            long hash = winC->getCRC();
            currentOidHash[oidNew] = hash;            

            delete winC;
            
        }
        ppItem->Release();
        pDisp->Release();


    } else if (dataType == olFolderCalendar) {
        IAppointment *pAppointment;

        polApp->GetDefaultFolder (olFolderCalendar, &pFolder);
        pFolder->get_Items (&pItems);
        pItems->Add((IDispatch**)&pAppointment);

        if(isSIF)
            completeAppointment(pAppointment, ptrData, polApp, path);
        else {
            VObject* vo = VConverter::parse(ptrData);
            if(vo) {
                VObjectToAppointment(pAppointment, vo);
                delete vo; vo = NULL;
            }
        }
        // Save the new appointment.
        pAppointment->Save ();
        hr = pAppointment->get_Oid(&oidNew);

        // Release objects
        pAppointment->Release ();

    } else if (dataType == olFolderTasks) {
        ITask *pTask;

        polApp->GetDefaultFolder (olFolderTasks, &pFolder);
        pFolder->get_Items (&pItems);
        pItems->Add((IDispatch**)&pTask);

        if(isSIF)
            completeTask(pTask, ptrData);
        else {
            VObject* vo = VConverter::parse(ptrData);
            if(vo) {
                VObjectToTask(pTask, vo);
                delete vo; vo = NULL;
            }
        }

        // Save the new appointment.
        pTask->Save ();
        hr = pTask->get_Oid(&oidNew);

        // Release objects
        pTask->Release ();
    }

    if (pItems) { pItems->Release (); }
    if (pFolder) { pFolder->Release ();}

    releaseOutlookApp(polApp);
    LOG.debug("Exiting manageNewItemsPOOM... ");

    if (hr == S_OK)
        *oid = oidNew;

    return hr;

}


long manageUpdatedItems(SyncItem* item, int dataType, wchar_t* path, bool isSIF, map<long,long> &currentOidHash) {

    LOG.debug("Enter in managerUpdatedItemsPOOM... ");

    int size = item->getDataSize();
    if (size <= 0){
        LOG.info("Empty data from server.");
        return 500;
    }

    IP_OUTLOOK_APP* polApp = getOutlookApp();
    if (polApp == NULL)
        return -1;

    wchar_t* dummy      = NULL;
    wchar_t* dummyEvent = NULL;
    HRESULT h           = NULL;
    wchar_t * ptrData   = NULL;
    wchar_t* t = NULL;
    char* data = NULL;

    long oidToUpdate = 0;   

    oidToUpdate = wcstol(item->getKey(), &dummy, 0);

    data = new char[size + 1];
    memset(data, 0, size + 1);
    memcpy(data, item->getData(), size);

    ptrData = toWideChar(data);
    delete [] data; data = NULL;
    
    wstring ptrDataString = ptrData;

    // try to update the way to create the sif and vcard.
    // currently there is only for contacts
    // REMOVE AFTER THE WHOLE IPOUTLOOK_APP2
    if (dataType != olFolderContacts) {
        if (isSIF) {
            replaceAmpLGt(ptrDataString);
        }
        if (ptrData) {
            delete [] ptrData; ptrData = NULL;
        }
        ptrData = (wchar_t*)ptrDataString.data();
    } else {
        
        delete [] ptrData; ptrData = NULL;
    }    

    //
    // we start to use the IPOutlookApp2 to use the IItem object
    //
    if (dataType == olFolderContacts) {
        
        IItem* ppItem = NULL;
        h = polApp->GetItemFromOidEx(oidToUpdate, 0, &ppItem); 
        if (h == S_OK) {
            WinContact *winC = createWinContact(isSIF, ptrDataString, contactFields);   

            if (winC) {
                winContactToContact(*winC, ppItem);       

                long hash = winC->getCRC();
                currentOidHash[oidToUpdate] = hash;            

                delete winC;                                 
            }
            ppItem->Release();
        } else {
            LOG.debug("Error updating contact...");
        }
    }
    else if (dataType == olFolderCalendar){
        IAppointment *pAppointment;

        h = polApp->GetItemFromOid (oidToUpdate, (IDispatch**)&pAppointment);

        if (h == S_OK) {
            if(isSIF)
                completeAppointment(pAppointment, ptrData, polApp, path);
            else {
                VObject* vo = VConverter::parse(ptrData);
                if(vo) {
                    VObjectToAppointment(pAppointment, vo);
                    delete vo; vo = NULL;
                }
            }

            pAppointment->Save();
            pAppointment->Release();
        } else {            
           LOG.debug("Error updating calendar...");
        }

    }
    else if (dataType == olFolderTasks){
        ITask *pTask;

        h = polApp->GetItemFromOid (oidToUpdate, (IDispatch**)&pTask);

        if (h == S_OK) {
            if(isSIF)
                completeTask(pTask, ptrData);
            else {
                VObject* vo = VConverter::parse(ptrData);
                if(vo) {
                    VObjectToTask(pTask, vo);
                    delete vo; vo = NULL;
                }
            }

            pTask->Save();
            pTask->Release();
        } else {
           LOG.debug("Error updating task...");
        }

    }
  
    releaseOutlookApp(polApp);
    LOG.debug("Exiting setModifiedItemsPOOM...");
    return h;

}

/*
Use to delete item from server to PPC
*/
long manageDeletedItems(SyncItem* syncItem, int dataType, map<long,long> &currentOidHash) {

    LOG.debug("Enter manageDeletedItemsPOOM... ");
    if (syncItem == NULL)
        return -1;

    const wchar_t* key = syncItem->getKey();

    LOG.debug("in managerDeletedItems... ");
    IP_OUTLOOK_APP* polApp = getOutlookApp();
    if (polApp == NULL)
        return -1;

    long oidToDelete = 0;
    wchar_t* local = NULL;
    HRESULT h = NULL;

    oidToDelete = wcstol(key, &local, 0);

    // to delete contacts
    if (dataType == olFolderContacts) {
        IContact *pContact;
        h = polApp->GetItemFromOid (oidToDelete, (IDispatch**)&pContact);
        if (h == S_OK) {
            pContact->Delete();
            pContact->Save();
            pContact->Release();            
            currentOidHash.erase(oidToDelete);
        }

    }
    // to delete appointments
    else if (dataType == olFolderCalendar){
        IAppointment *pAppointment;
        h = polApp->GetItemFromOid (oidToDelete, (IDispatch**)&pAppointment);
        if (h == S_OK) {
            // pAppointment->Cancel();  it sends the cancellation to the recipients
            pAppointment->Delete();
            pAppointment->Release();
        }

    }
    // to delete tasks
    else if (dataType == olFolderTasks){
        ITask *pTask;
        h = polApp->GetItemFromOid (oidToDelete, (IDispatch**)&pTask);
        if (h == S_OK) {
            pTask->Delete();
            pTask->Release();
        }
    }

    releaseOutlookApp(polApp);
    LOG.debug("Exiting manageDeletedItemsPOOM... ");
    return h;
}

void writeCurrentItems(int dataType, wchar_t* path, map<long,long> &currentOidHash) {

    IP_OUTLOOK_APP* polApp = getOutlookApp();
    if (polApp == NULL)
        return ;

    IPOutlookItemCollection *pItems;
    IFolder *pFolder;

    //IContact     *pContact;
    IAppointment *pAppointment;
    ITask        *pTask;

    polApp->GetDefaultFolder (dataType, &pFolder);
    pFolder->get_Items (&pItems);
    pItems->Sort(TEXT ("[Oid]"), FALSE);

    int countItems = 0;

    vector<long> currentOid;
    vector<long> currentHash;

    // if there is the file appOidToReset.dat the crc of these items must be put at 0
    vector<long> oidToReset;
    if (dataType == olFolderCalendar) {
        readAppOidToReset(oidToReset, path);
    }

    pItems->get_Count(&countItems);        

    if (dataType == olFolderContacts) {
        if (countItems != currentOidHash.size()) {
            LOG.info("map and count are different...");
        }
        map<long,long>::iterator it;
        for (it = currentOidHash.begin(); it != currentOidHash.end(); it++) {
            currentOid.push_back((*it).first);
            currentHash.push_back((*it).second);
        }

    }


    for (int j=0; j<countItems; j++) {
        long oid = 0;

        // Item values start from element number 1 not 0!!!!!
        // from dataType choose if a Contact or a Appointment
        // leave 2 populateString to semplify
        if (dataType == olFolderContacts) {                       
                        
            /*
            IDispatch* pDisp  = NULL;
            IItem*     ppItem = NULL;

            pItems->Item (j + 1, (IDispatch**)&pDisp);               
            HRESULT hr = pDisp->QueryInterface(IID_IItem, (LPVOID*)&ppItem);
            ppItem->get_Oid(&oid);
            WinContact* winC = contactToWinContact(ppItem, false);
            if (winC) { 
                long currentLongHash = winC->getCRC();
                // set these objlist to write to file...
                currentOid.push_back(oid);
                currentHash.push_back(currentLongHash);
                               
                delete winC;    
                ppItem->Release();
                pDisp->Release();   
            }                      
            */
            // OLD VERSION
            /*
            pItems->Item (j + 1, (IDispatch**)&pContact);
            pContact->get_Oid(&oid);

            long currentLongHash = calculateContactHash(pContact);
            // set these objlist to write to file...
            currentOid.push_back(oid);
            currentHash.push_back(currentLongHash);
            pContact->Release ();
            */

        }
        else if (dataType == olFolderCalendar){
            pItems->Item (j + 1, (IDispatch**)&pAppointment);

            pAppointment->get_Oid(&oid);

            long currentLongHash = 0;
            BOOL foundToReset = FALSE;

            vector<long>::iterator iterator;
            for (iterator = oidToReset.begin();
                 iterator != oidToReset.end();
                 iterator++) {

                if ((*iterator) == oid) {
                    oidToReset.erase(iterator);
                    foundToReset = TRUE;
                    break;
                }
            }

            if (!foundToReset) {
                currentLongHash = calculateAppointmentHash(pAppointment);
            }

            // set these objlist to write to file...
            currentOid.push_back(oid);

            currentHash.push_back(currentLongHash);
            pAppointment->Release ();
        }
        else if (dataType == olFolderTasks){
            pItems->Item (j + 1, (IDispatch**)&pTask);

            pTask->get_Oid(&oid);

            long currentLongHash = calculateTaskHash(pTask);

            // set these objlist to write to file...
            currentOid.push_back(oid);
            currentHash.push_back(currentLongHash);
            pTask->Release ();
        }

    }

    writeToFile(currentOid, currentHash, dataType, path);

    pItems->Release ();
    pFolder->Release ();
    currentOid.clear();
    currentHash.clear();
    releaseOutlookApp(polApp);
}

⌨️ 快捷键说明

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