fileclient2server.cpp

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

CPP
739
字号
/*
 * 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 <windows.h>
#include "Winbase.h"

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

#include "base/Log.h"
#include "spds/spdsutils.h"
#include "spds/Constants.h"
#include "spdm/spdmutils.h"
#include "pim/Utils.h"
#include "pim/FILEClient2Server.h"
#include "pim/FILEFileManagement.h"
#include "spds/FileData.h"
#include "pim/ClientSettings.h"
#include "vocl/WinNoteSIF.h"

#include "shellapi.h"

static wstring getXMLNote (const char* ptrFileContent, const wchar_t* fileName, bool isSIf);


/*
to get My documents foleder
SHGetSpecialFolderPath(NULL, internalPath, CSIDL_PERSONAL, FALSE);
*/

void setAllItemsFILE(Container* c, int dataType, const wchar_t* path, const wchar_t* dir, bool isSif) {

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

    wchar_t localTemp [DIM_LOCAL_TEMP];

    wchar_t **ptrArrayOfFileName    = NULL;
    wchar_t  *ptrFileContent        = NULL;
    char* fileContent = NULL;
    char* pp = NULL;
    wchar_t   filterPattern [256];
    wchar_t   internalPath  [DIM_LOCAL_TEMP];

    int numBytes            = 0;
    int count               = 0;
    int j                   = 0;
    unsigned long sizeFile  = 0;

    BOOL recursiveInDirectory = FALSE;
    filterPattern[0] = 0;
    internalPath[0]  = 0;

    /*
    * It avoids to send object greater then MaxObjSize.
    * In next releases it must me inserted into the
    * source.
    */

    //ClientSettings cs(APPLICATION_URI);
    //cs.readConfig();
    int fileRemoved = 0;
    //
    // decide the path of the files to read
    //

    if (dataType == constFiles) {
        wstring complete = appendToPath((wchar_t*)dir, NULL);
        wcscpy(internalPath, complete.c_str());   
        // wsprintf(internalPath, TEXT("%s%s"), dir, TEXT("\\"));
    }
    else if (dataType == constNotes) {
        wstring complete = appendToPath((wchar_t*)dir, NULL);
        wcscpy(internalPath, complete.c_str());  
        //wsprintf(internalPath,  TEXT("%s%s"), dir, TEXT("\\"));
        wcscpy(filterPattern, PWI_EXTENSION_PATTERN);
    }
    else if (dataType == constFavorites) {
        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wstring complete = appendToPath((wchar_t*)localTemp, NULL);
        wcscpy(internalPath, complete.c_str());  
        //wsprintf(internalPath, TEXT("%s%s"), localTemp, TEXT("\\"));
        recursiveInDirectory = TRUE;
    }

    // to retrieve num of files
    getFileList(internalPath, ptrArrayOfFileName, &count, TRUE, recursiveInDirectory, filterPattern);

    ptrArrayOfFileName = new wchar_t*[count];

    for ( int k = 0; k < count; k++) {
        ptrArrayOfFileName[k] = 0;
    }

    // to retrieve fileName
    getFileList(internalPath, ptrArrayOfFileName, &count, FALSE, recursiveInDirectory, filterPattern);

    SyncItem* syncItem = NULL;

    for (j = 0; j < count; j++) {

        // fileName with complete path (e.g. \\Program Files\\SyncClient PIM\\store\\example.bin)
        wcscpy(localTemp, ptrArrayOfFileName[j]);

        if (dataType == constFiles) {

            unsigned long sizeFile = 0;
            getFileSize(ptrArrayOfFileName[j], &sizeFile);
            if (sizeFile == 0)
                return;
            if (sizeFile > getMaxObjectSizeToSend()) {
                LOG.debug("Found file greater then maxObjectSize(%lu): sizefile = %lu", getMaxObjectSizeToSend(), sizeFile);
                fileRemoved++;
                continue;
            }
            fileContent = new char[sizeFile];
            readByteFromFile(ptrArrayOfFileName[j], (byte*)fileContent, &numBytes);
            FileData fileData;
            getFileName(ptrArrayOfFileName[j], localTemp);
            fileData.setName(localTemp);
            fileData.setSize(numBytes);
            fileData.setEnc(TEXT("base64"));
            fileData.setBody(fileContent, numBytes);
            delete [] fileContent; fileContent = NULL;
            fileContent = fileData.format();
            numBytes = strlen(fileContent);

        }
        else if (dataType == constNotes) {

            pp = readTextFromBinaryFileChar(ptrArrayOfFileName[j]);
            getFileName(ptrArrayOfFileName[j], localTemp);
            if (pp != NULL) {
                wstring note = getXMLNote(pp, localTemp, isSif);
                fileContent = toMultibyte((wchar_t*)note.data());
                numBytes = strlen(fileContent);
            }
        }
        else if (dataType == constFavorites) {

            ptrFileContent = readTextFromBinaryFile(ptrArrayOfFileName[j]);

            if (ptrFileContent != NULL) {
                translateText2Base64(&ptrFileContent);
            }
            fileContent = toMultibyte(ptrFileContent);
            numBytes = strlen(fileContent);

            wcscpy(localTemp, ptrArrayOfFileName[j]);

            wchar_t* t = wcsstr(ptrArrayOfFileName[j], internalPath);

            if (t != NULL) {
                wsprintf(localTemp, t + wcslen(internalPath));
                delete [] t; t = NULL;
            }
            if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
                convertSlashAt(localTemp);
        }

        encodeKey (localTemp);

        syncItem = new SyncItem(localTemp);

        if (fileContent == NULL) {
            syncItem->setData("", 1*sizeof(char));
        }
        else {
            syncItem->setData(fileContent, numBytes);
        }

        c->addItemToAllItems(syncItem);

        if (ptrFileContent != NULL) {
            delete [] ptrFileContent; ptrFileContent = NULL;
        }
        if (syncItem) {
            delete syncItem; syncItem = NULL;
        }
        if (fileContent != NULL) {
            delete [] fileContent; fileContent = NULL;
        }
        pp = NULL;

    }

    // delete array and pointer...
    for (j = 0; j < count; j++) {
        if (ptrArrayOfFileName[j] != NULL)
            delete [] ptrArrayOfFileName[j];
    }

    if (ptrArrayOfFileName != NULL)
        delete [] ptrArrayOfFileName;

    wsprintf(localTemp, SET_ALL_ITEMS, count - fileRemoved);
    LOG.debug("Exiting from setAllItemsFILE... ");
}

void setAllItemsFILEKey(Container* c, int dataType, const wchar_t* path, const wchar_t* dir) {

    LOG.debug("Enter in setAllItemsFILEKey... ");
    wchar_t localTemp [DIM_LOCAL_TEMP];

    wchar_t **ptrArrayOfFileName    = NULL;

    wchar_t   filterPattern [256];
    wchar_t   internalPath  [DIM_LOCAL_TEMP];

    int numBytes            = 0;
    int count               = 0;
    int j                   = 0;
    unsigned long sizeFile  = 0;

    BOOL recursiveInDirectory = FALSE;
    filterPattern[0] = 0;
    internalPath[0]  = 0;

    //
    // decide the path of the files to read
    //

    if (dataType == constFiles) {
        wstring complete = appendToPath((wchar_t*)dir, NULL);
        wcscpy(internalPath, complete.c_str());  
        //wsprintf(internalPath, TEXT("%s%s"), dir, TEXT("\\"));
    }
    else if (dataType == constNotes) {
        wstring complete = appendToPath((wchar_t*)dir, NULL);
        wcscpy(internalPath, complete.c_str());  
        //wsprintf(internalPath,  TEXT("%s%s"), dir, TEXT("\\"));
        wsprintf(filterPattern, TEXT("%s"), PWI_EXTENSION_PATTERN);
    }
    else if (dataType == constFavorites) {
        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wstring complete = appendToPath((wchar_t*)localTemp, NULL);
        wcscpy(internalPath, complete.c_str());  
        //wsprintf(internalPath, TEXT("%s%s"), localTemp, TEXT("\\"));
        recursiveInDirectory = TRUE;
    }

    // to retrieve num of files
    getFileList(internalPath, ptrArrayOfFileName, &count, TRUE, recursiveInDirectory, filterPattern);

    ptrArrayOfFileName = new wchar_t*[count];

    for ( int k = 0; k < count; k++) {
        ptrArrayOfFileName[k] = 0;
    }

    // to retrieve fileName
    getFileList(internalPath, ptrArrayOfFileName, &count, FALSE, recursiveInDirectory, filterPattern);

    SyncItem* syncItem = NULL;

    for (j = 0; j < count; j++) {

        // fileName with complete path (e.g. \\Program Files\\SyncClient PIM\\store\\example.bin)
        wcscpy(localTemp, ptrArrayOfFileName[j]);

        if (dataType == constFiles)
            getFileName(ptrArrayOfFileName[j], localTemp);
        else if (dataType == constNotes)
            getFileName(ptrArrayOfFileName[j], localTemp);
        else if (dataType == constFavorites) {

            wcscpy(localTemp, ptrArrayOfFileName[j]);
            wchar_t* t = wcsstr(ptrArrayOfFileName[j], internalPath);

            if (t != NULL) {
                wsprintf(localTemp, t + wcslen(internalPath));
                delete [] t;
            }
            if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
                convertSlashAt(localTemp);
        }

        encodeKey (localTemp);

        syncItem = new SyncItem(localTemp);
        syncItem->setData("", 1*sizeof(char));

        c->addItemToAllItems(syncItem);

        if (syncItem) {
            delete syncItem; syncItem = NULL;
        }
    }

    // delete array and pointer...
    for (j = 0; j < count; j++) {
        if (ptrArrayOfFileName[j] != NULL)
            delete [] ptrArrayOfFileName[j];
    }
    if (ptrArrayOfFileName != NULL)
        delete [] ptrArrayOfFileName;
    LOG.debug("Exiting from setAllItemsFILEKey... ");
}


/*
* The method replace the text file content with new one in xml format.
*/
static wstring getXMLNote (const char* ptrFileContent, const wchar_t* fileName, bool isSIf) {

    wstring name(fileName);
    int position  = name.find(PWI_EXTENSION);

    if (position != std::wstring::npos) {
        wstring s = name;
        name = s.substr(0,position);
    }

    wchar_t *cnt = toWideChar(ptrFileContent);
    wstring content(cnt);

    wchar_t date[64];
    doubleToSystemTime(date, getSystemTime());


    WinNote* winN = NULL;
    if (isSIf) { winN = new WinNoteSIF(); }
    else       { winN = new WinNote();    }

    winN->setProperty(TEXT("Subject"), name);   // filename (with no .pwi extension)
    winN->setProperty(TEXT("Body"), content);
    winN->setProperty(TEXT("Date"),    date);
    wstring xml = winN->toString();

    if (winN) { delete winN; }

    return xml;
}


/*
* text pointed by ptrData is translated in base64 and assigned to ptrData
* Used only for "favorites" source.
* TO BE DELETED

⌨️ 快捷键说明

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