fileserver2client.cpp

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

CPP
512
字号
/*
 * 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 "pimstore.h"
#include <stdio.h>

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

#include "vocl/WinNoteSIF.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"

#include "base/stringUtils.h"


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


/**
 * Array of notes fields names for SIF format.
 */
static const wchar_t* noteFields[] = {
    {L"Subject"  },
    {L"Body"     },
    {NULL}
};



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) {
        wstring complete = appendToPath((wchar_t*)dir, tmp);
        wcscpy(localTemp, complete.c_str());   
        //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);

        wchar_t* wdata = toWideChar(data);
        delete [] data; data = NULL;
        wstring ptrDataString(wdata);
        delete [] wdata; wdata = NULL;

        wstring complete = appendToPath((wchar_t*)dir, tmp);
        wcscpy(localTemp, complete.c_str());   
        //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;

        }
        // -------------------

        bool isSif = isSIF(item->getDataType());

        // Parse the data string, and fill WinNote properties
        WinNote* winN = NULL;
        if (isSif) { winN = new WinNoteSIF(ptrDataString, noteFields); }
        else       { winN = new WinNote   (ptrDataString);             }
        
        wstring body;
        winN->getProperty(TEXT("Body"), body); 

        replaceAll(TEXT("\r\n"), TEXT("\n"),   body);
        replaceAll(TEXT("\n"),   TEXT("\r\n"), body);
        writeTextToFile(localTemp, body.c_str());

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

        SHGetSpecialFolderPath(NULL, localTemp, CSIDL_FAVORITES, FALSE);
        wstring complete = appendToPath((wchar_t*)localTemp, tmp);
        wcscpy(localTemp, complete.c_str());   

        //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) {
        
        wstring complete = appendToPath((wchar_t*)dir, tmp);
        wcscpy(localTemp, complete.c_str());    
        //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);

        wchar_t* wdata = toWideChar(data);
        delete [] data; data = NULL;
        wstring ptrDataString(wdata);
        delete [] wdata; wdata = NULL;

        bool isSif = isSIF(item->getDataType());

        // Parse the data string, and fill WinNote properties
        WinNote* winN = NULL;
        if (isSif) { winN = new WinNoteSIF(ptrDataString, noteFields); }
        else       { winN = new WinNote   (ptrDataString);             }
        
        wstring subject, body;
        winN->getProperty(TEXT("Subject"), subject);
        winN->getProperty(TEXT("Body"),    body); 

        replaceAll(TEXT("\r\n"), TEXT("\n"),   body);
        replaceAll(TEXT("\n"),   TEXT("\r\n"), body);

        wchar_t* fileName = createFileName(subject.c_str());
        wstring complete = appendToPath((wchar_t*)dir, fileName);
        wcscpy(localTemp, complete.c_str());
        //wsprintf(localTemp, TEXT("%s%s%s"), dir, TEXT("\\"), fileName);

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

        getUniqueFileName(localTemp);

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

        writeTextToFile(localTemp, body.c_str());

        if (fileName) { 
            delete [] fileName; 
            fileName = NULL;
        }
        if (winN) { 
            delete winN; 
        }
    }
    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) {
        wstring complete = appendToPath((wchar_t*)dir, key);
        wcscpy(localTemp, complete.c_str());        
        // 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 + =
减小字号Ctrl + -
显示快捷键?