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

📄 fileclient2server.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * 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
 */
#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 "shellapi.h"

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


/*
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) {

    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)
        wsprintf(internalPath, TEXT("%s%s"), dir, TEXT("\\"));
    else if (dataType == constNotes) {
        wsprintf(internalPath,  TEXT("%s%s"), dir, TEXT("\\"));
        wcscpy(filterPattern, PWI_EXTENSION_PATTERN);
    }
    else if (dataType == constFavorites) {
        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        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);
                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)
        wsprintf(internalPath, TEXT("%s%s"), dir, TEXT("\\"));
    else if (dataType == constNotes) {
        wsprintf(internalPath,  TEXT("%s%s"), dir, TEXT("\\"));
        wsprintf(filterPattern, TEXT("%s"), PWI_EXTENSION_PATTERN);
    }
    else if (dataType == constFavorites) {
        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        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) {

    char* t  = NULL;
    wstring name(fileName);

    int position  = name.find(PWI_EXTENSION);
    /*
    string s = name;
    name = string(s, position);
    */

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

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

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

    wstring xml = wstring(TEXT("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    xml += TEXT("<note><Subject>");
    xml += name; // filename (with no .pwi extension)
    xml += TEXT("</Subject><Body>") ;
    xml += content ;
    xml += TEXT("</Body><Date>");
    xml += date;
    xml += TEXT("</Date></note>");

    return xml;
}


/*
* text pointed by ptrData is translated in base64 and assigned to ptrData
* Used only for "favorites" source.
* TO BE DELETED
*
*/
void translateText2Base64(wchar_t** ptrFileContent) {

    wchar_t* ptr = NULL;
    ptr = encodeBase64UTF8(*ptrFileContent);
    if (*ptrFileContent != NULL) {
        delete *ptrFileContent;
        *ptrFileContent = NULL;
    }
    *ptrFileContent = ptr;
}


/*
* get the data content of a file by filename and return these data into base64 format.
* It's useful to read data to set into setAllItems, setModifiedItem, setNewItems
NO MORE USED


char* getB64DataContentByFilename(wchar_t* filename) {

    unsigned long sizeFile      = 0;
    int      numBytes           = 0;
    byte*    ptrFileContent     = NULL;
    char*    base64             = NULL;
    wchar_t* ptrFileContentb64  = NULL;
    int encodeLen               = 0;
    getFileSize(filename, &sizeFile);

    if (sizeFile == 0)
        goto finally;

    ptrFileContent = new byte[sizeFile];

    // read bytes from file

⌨️ 快捷键说明

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