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

📄 fileserver2client.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/FILEServer2Client.h"
#include "pim/FILEFileManagement.h"
#include "base/util/XMLProcessor.h"

#include "base/util/WString.h"
#include "base/quoted-printable.h"


#define FILE_BODY       T("body")
#define FILE_ENC        T("enc")


//--
//#include "base\util\WString.h"

/*
* Translate base64 data to text...

void translateBase642Text(wchar_t** ptrData) {

    wchar_t* ret = NULL;
    ret = decodeBase64WUTF8(*ptrData);
    if (*ptrData != NULL) {
        delete *ptrData;
        *ptrData = NULL;
    }

    *ptrData = ret;

}

char* translateBase642Text(wchar_t** ptrData, int* myLen) {

    char* ret = NULL;
    ret = decodeBase64WUTF8(*ptrData, myLen);
    if (*ptrData != NULL) {
        delete *ptrData;
        *ptrData = NULL;
    }
    return ret;
}
*/

static wchar_t charDenied[] = {'\\', '|', '*', '/', '?', '<', '>', ':', '"', ' '};
static wchar_t charPermittedAsEnd[] = {' '};

/**
 * Normalize the string name to obtain a valid filename by removing
 * all the invalid characters
 *
 * @param name the arbitrary name
 * @return the valid filename
 */
wchar_t* createFileName(const wchar_t* tmp1) {

    int i = 0;
    wchar_t *p1 = NULL, *p2 = NULL, *p3 = NULL;
    wchar_t* toRet = NULL;
    wchar_t* tmp = NULL;
    wchar_t* ret = NULL;
    BOOL foundValidChar = FALSE;
    int len = 0;

    if (tmp1 == NULL) {
        toRet = new wchar_t[20];
        wcscpy(toRet, TEXT("mynote"));
        goto finally;
    }

    len = wcslen(tmp1);

    if (len == 0) {
        toRet = new wchar_t[20];
        wcscpy(toRet, TEXT("mynote"));
        goto finally;
    }

    // the note name should be max 20 chars
    if (len > 20)
        len = 20;

    ret  = new wchar_t[len + 1];
    toRet = new wchar_t[len + 1];
    tmp = new wchar_t[len + 1];

    memset(ret,   0, (len+1)*sizeof(wchar_t));
    memset(tmp,   0, (len+1)*sizeof(wchar_t));
    memset(toRet, 0, (len+1)*sizeof(wchar_t));

    wcsncpy(ret, tmp1, len);

    p2 = &ret[len];
    for (i = 0; i < len; i++) {
        if (wcschr(charDenied, ret[i]) != NULL) {
            if (foundValidChar == TRUE && wcschr(charPermittedAsEnd, ret[i]) == NULL) {
                p2 = &ret[i];
                break;
            }

        } else if (foundValidChar == FALSE) {
            foundValidChar = TRUE;
            p1 = &ret[i];
        }

    }
    if (p1 == NULL) {
        toRet = new wchar_t[20];
        wcscpy(toRet, TEXT("mynote"));
        goto finally;
    }

    wcsncpy(tmp, p1, p2-p1);

    len = wcslen(tmp);

    if (len == 0) {
        toRet = new wchar_t[20];
        wcscpy(toRet, TEXT("mynote"));
        goto finally;
    }

    p1 = tmp;
    p2 = &tmp[len];

    if (*p2 == '\0')
        p3 = p2-1;
    else
        p3 = p2;

    do {
        if (*p3 == ' ') {
            p2 = p3;
            if (p3 == p1) {
                toRet = new wchar_t[20];
                wcscpy(toRet, TEXT("mynote"));
                goto finally;
            }

            p3--;
        }
        else
            break;

    } while(p3 - p1 > 0);

    if (p3 == p1 && len > 1) {
        toRet = new wchar_t[20];
        wcscpy(toRet, TEXT("mynote"));
        goto finally;
    }

    wcsncpy(toRet, p1, p2-p1);

    finally:

    if (ret) { delete [] ret; ret = NULL; }
    if (tmp) { delete [] tmp; tmp = NULL; }

    return toRet;

}

/*
* method used only for notes and not for files. Files cannot be modified...
*/

long FILEmanageUpdatedItems(SyncItem* item, int dataType, const wchar_t* path, const wchar_t* dir) {

    LOG.debug("in managerUpdatedItems ... ");

    wchar_t localTemp   [DIM_LOCAL_TEMP];
    wchar_t tmp         [DIM_LOCAL_TEMP];
    wchar_t* ptrData    = NULL;
    char* data          = NULL;
    int size = 0;

    size = item->getDataSize();

    // ptrData = toWideChar(data);

    //delete [] data; data = NULL;

    // used to remove start CarriageReturn into return data stream.
    // to be remove when correct into api methods.
    // 2004-07-08 fixed in SC-API
    //removeStartCarriage(&ptrData);

    // ptrData = (wchar_t*) item->getData();
    DWORD   result = 0;

    wsprintf(tmp, TEXT("%s"), (wchar_t*)item->getKey());
    decodeKey (tmp);

    // dir from server arrives with \ at the end. It must be removed

    if ( tmp[wcslen(tmp) - 1] == L'\\')
        tmp[wcslen(tmp) - 1] = 0;

    
    if (dataType == constFiles) {

        wsprintf(localTemp, TEXT("%s%s%s"), dir, TEXT("\\"), tmp);
        writeByteToFile(localTemp, (byte*)item->getData(), size);
        wsprintf(tmp, TEXT("%s"), (wchar_t*)item->getKey());

    } //-- end updated files
    else if (dataType == constNotes) {

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

        wsprintf(localTemp, TEXT("%s%s%s"), dir, TEXT("\\"), tmp);
        
        // if file doesn't exist. ---------
        result = GetFileAttributes(localTemp);

        if (result == 0xFFFFFFFF || result == -1) {

            if (ptrData != NULL) {
                delete ptrData;
                ptrData = NULL;
            }

            return -1;

        }
        // -------------------
        unsigned int startpos = 0, endpos = 0;
        const char* bdy = XMLProcessor::getElementContent(data, "Body", 0, &startpos, &endpos);

        if (!bdy) {
            LOG.debug("Error in parsing note object. Body not found");
            return -1;
        }
        StringBuffer sb(bdy, endpos - startpos);

        sb.replaceAll("\r\n", "\n");
        sb.replaceAll("\n", "\r\n");
        // decode special chars
        sb.replaceAll("&lt;", "<");
        sb.replaceAll("&gt;", ">");
        sb.replaceAll("&amp;", "&");


        wchar_t* toFile = toWideChar(sb.c_str());
        writeTextToFile(localTemp, toFile);


        if (toFile) {delete [] toFile; toFile = NULL; }

    }
    else if (dataType == constFavorites) {
        wchar_t internalPath [DIM_LOCAL_TEMP];

        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wsprintf(internalPath, TEXT("%s%s"), localTemp, TEXT("\\"));

        wsprintf(localTemp, TEXT("%s%s"), internalPath, tmp);
        
        if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
            convertAtSlash(localTemp);

        result = GetFileAttributes(localTemp);

        if (result == 0xFFFFFFFF || result == -1) {
            if (ptrData != NULL) {
                delete ptrData;
                ptrData = NULL;
            }
            return -1;
        }

        // translate base64 into text
        if (ptrData != NULL) {
            ptrData = toWideChar(data);
        }

        writeTextToTextFile(localTemp, ptrData);
    }

    if (ptrData != NULL)
        delete ptrData;

    return 0;

}

wchar_t* FILEmanageNewItems(SyncItem* item, int dataType, const wchar_t* path, const wchar_t* dir) {

    LOG.debug("in manageNewItems ... ");

    wchar_t localTemp [DIM_LOCAL_TEMP];
    wchar_t tmp [DIM_LOCAL_TEMP];
    wchar_t * ptrData   = NULL;
    char* cptrData      = NULL;
    char* content       = NULL;
    char* reversed      = NULL;
    int num             = 0;
    int size            = 0;
    char* data          = NULL;

    wsprintf(tmp, TEXT("%s"), (wchar_t*)item->getKey());

    size = item->getDataSize();

    // dir from server arrives with \ at the end. It must be removed

    if ( tmp[wcslen(tmp) - 1] == L'\\')
        tmp[wcslen(tmp) - 1] = 0;

    // to create a binaryfile
    if (dataType == constFiles) {

        wsprintf(localTemp, TEXT("%s%s%s"), dir, TEXT("\\"), tmp);
        writeByteToFile(localTemp, (byte*)item->getData(), size);
        wsprintf(tmp, TEXT("%s"), (wchar_t*)item->getKey());

    } else if (dataType == constNotes) {

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

        unsigned int startpos = 0, endpos = 0;

        char* sbj = XMLProcessor::copyElementContent(data, "Subject", NULL);
        const char* bdy = XMLProcessor::getElementContent(data, "Body", 0, &startpos, &endpos);

        if (!bdy) {
            LOG.debug("Error in parsing note object. Body not found");
            return NULL;
        }
        wchar_t* sbjw = toWideChar(sbj);
        wchar_t* subj2 = createFileName(sbjw);

        StringBuffer sb(bdy, endpos - startpos);

        // this is because some notes are sent with \n only
        sb.replaceAll("\r\n", "\n");
        sb.replaceAll("\n", "\r\n");
        // decode special chars
        sb.replaceAll("&lt;", "<");
        sb.replaceAll("&gt;", ">");
        sb.replaceAll("&amp;", "&");

        wsprintf(localTemp, TEXT("%s%s%s"), dir, TEXT("\\"), subj2);

        if (wcsstr(subj2, PWI_EXTENSION) == NULL) {
            wcscat(localTemp, PWI_EXTENSION);
            wcscat(tmp, PWI_EXTENSION);
        }

        getUniqueFileName(localTemp);

        // tmp contains the file name
        getFileName(localTemp, tmp);

        wchar_t* toFile = toWideChar(sb.c_str());
        writeTextToFile(localTemp, toFile);

        if (sbj) { delete [] sbj; sbj = NULL; }
        if (subj2) { delete [] subj2; subj2 = NULL; }
        if (sbjw) { delete [] sbjw; sbjw = NULL; }
        if (toFile) { delete [] toFile; toFile = NULL; }

    }
    else if (dataType == constFavorites) {
        wchar_t internalPath [DIM_LOCAL_TEMP];

        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wsprintf(internalPath, TEXT("%s%s"), localTemp, TEXT("\\"));

        wsprintf(localTemp, TEXT("%s%s"), internalPath,  tmp);

        if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
            convertAtSlash(localTemp);

        ptrData = toWideChar(data);

        writeTextToTextFile(localTemp, ptrData);

        wsprintf(tmp, TEXT("%s"), (wchar_t*)item->getKey());

    }

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

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

    encodeKey(tmp);
    wchar_t* ret = new wchar_t[wcslen(tmp)+ 1];
    wcscpy(ret, tmp);
    return ret;

}

long FILEmanageDeletedItems(SyncItem* syncItem, int dataType, const wchar_t* path, const wchar_t* dir) {

    long ret = 0;
    LOG.debug("in managerDeletedItems... ");
    wchar_t localTemp [DIM_LOCAL_TEMP];

    wchar_t key [DIM_LOCAL_TEMP];

    wsprintf(key, TEXT("%s"), syncItem->getKey());
    decodeKey (key);

    if (dataType == constFiles || dataType == constNotes) {
        wsprintf(localTemp, TEXT("%s%s%s"), dir, TEXT("\\"), key);
        if( DeleteFile(localTemp) == FALSE ) {
        }   
    }
    else if (dataType == constFavorites) {

        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wcscat(localTemp, TEXT("\\"));
        wcscat(localTemp, key);

        if (FILE_SYSTEM_SYNC_SOURCE == TRUE)
            convertAtSlash(localTemp);
        // dir from server arrives with \ at the end. It must be removed

        if ( localTemp[wcslen(localTemp) - 1] == L'\\')
            localTemp[wcslen(localTemp) - 1] = 0;

        // in favorites, try to delete file. if fails, it's a directory.
        // so enter in dir and delete every files.
        if( DeleteFile(localTemp) == FALSE ) {
            if (wcsstr(localTemp, URL_EXTENSION) == NULL) {
                if (dbg)
                    MessageBox (NULL, TEXT("Try delete dir"), TEXT ("Debug eVC"), MB_SETFOREGROUND |MB_OK);

                wchar_t** ptrFilenameInDir      = NULL;
                int countFileInDir              = 0;

                wcscat(localTemp, TEXT("\\"));
                // to retrieve num of files in directory
                getFileList(localTemp, ptrFilenameInDir, &countFileInDir, TRUE, FALSE, NULL);
                ptrFilenameInDir = new wchar_t*[countFileInDir];

                // to retrieve fileName
                getFileList(localTemp, ptrFilenameInDir, &countFileInDir, FALSE, FALSE, NULL);

                int i;

                for (i = 0; i < countFileInDir; i++) {

                    DeleteFile(ptrFilenameInDir[i]);
                }
                // remove \ at the end of buffer
                localTemp[wcslen(localTemp) - 1] = 0;
                if (RemoveDirectory(localTemp)) {
                } else {
                }

                for (i = 0; i < countFileInDir; i++) {
                    if (ptrFilenameInDir[i] != NULL)
                        delete ptrFilenameInDir[i];
                }
                if (ptrFilenameInDir != NULL)
                    delete [] ptrFilenameInDir;
            }
            else {}
        }

    }
    return ret;
}

⌨️ 快捷键说明

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