poomfilemanagement.cpp

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

CPP
244
字号
/*
 * 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".
 */
//
// @author Marco Magistrali
//

#include <windows.h>
#include <oleauto.h>

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

#include "base/Log.h"

#include "pim/Utils.h"
#include "pim/POOMFileManagement.h"
#include "pim/SettingFunctions.h"

/*
*
*   FILE MANAGEMENT
*
*
*/

void writeToFile(vector<long> &currentOid, vector<long> &currentHash, int dataType, wchar_t* path) {

    wchar_t wfilename[DIM_FILE];
    wchar_t* pt = NULL;
    char filename [DIM_FILE];
    BOOL usedDefaultChar;

    if (dataType == olFolderContacts)
        pt = TEXT(FILENAME_CONTACTS);
    else if (dataType == olFolderCalendar)
        pt = TEXT(FILENAME_APPOINTMENTS);
    else if (dataType == olFolderTasks)
        pt = TEXT(FILENAME_TASKS);

    if (wcscmp(path, TEXT("\\")) == 0)
        wsprintf(wfilename, TEXT("\\%s"),pt);
    else
        wsprintf(wfilename, TEXT("%s\\%s"),path, pt);

    WideCharToMultiByte (CP_ACP, 0, wfilename, 255, filename, 255, "?", &usedDefaultChar);

    FILE* f;
    if (dataType == olFolderContacts) {
        f = fopen(filename, "w+");
        fwprintf(f, TEXT("<contacts>\n"));
    }
    else if (dataType == olFolderCalendar){
        f = fopen(filename, "w+");
        fwprintf(f, TEXT("<appointments>\n"));
    }
    else if (dataType == olFolderTasks){
        f = fopen(filename, "w+");
        fwprintf(f, TEXT("<tasks>\n"));
    }


    fwprintf(f, TEXT("<num>%i</num>\n"), currentOid.size());
    int oidLength = currentOid.size();

    for (int i = 0; i < oidLength; i++) {
        long currentLongOid = currentOid[i];
        long currentLongHash = currentHash[i];

        fwprintf(f, TEXT("<item>\n"));
        fwprintf(f, TEXT("<oid>%i</oid>\n"), currentLongOid);
        fwprintf(f, TEXT("<hash>%i</hash>\n"), currentLongHash);
        fwprintf(f, TEXT("</item>\n"));
    }

    if (dataType == olFolderContacts) {
        fwprintf(f, TEXT("</contacts>\n"));
    }
    else if (dataType == olFolderCalendar){
        fwprintf(f, TEXT("</appointments>\n"));
    }
    else if (dataType == olFolderTasks){
        fwprintf(f, TEXT("</tasks>\n"));
    }

    fflush(f);
    fclose(f);
}


// method used to check if file with previous appointments or contacts exist! if not exists force slow sync
BOOL existsFile(int dataType, wchar_t* path) {

    wchar_t wfilename[DIM_FILE];
    wchar_t* pt = NULL;
    char filename [DIM_FILE];
    BOOL usedDefaultChar;
    BOOL exists = FALSE;

    if (dataType == olFolderContacts)
        pt = TEXT(FILENAME_CONTACTS);
    else if (dataType == olFolderCalendar)
        pt = TEXT(FILENAME_APPOINTMENTS);
    else if (dataType == olFolderTasks)
        pt = TEXT(FILENAME_TASKS);
    else if (dataType == constFiles)
        pt = TEXT(FILENAME_FILES);
    else if (dataType == constNotes)
        pt = TEXT(FILENAME_NOTES);
    else if (dataType == constFavorites)
        pt = TEXT(FILENAME_FAVORITES);

    wsprintf(wfilename, TEXT("%s\\%s"),path, pt);

    WideCharToMultiByte (CP_ACP, 0, wfilename, DIM_FILE -1, filename, DIM_FILE -1, "?", &usedDefaultChar);

    FILE* f = NULL;
    f = fopen(filename, "r");

    if (f != NULL) {
        fflush(f);
        fclose(f);
        exists = TRUE;
    }

    return exists;

}


void readFromFile(long* previousCountItem, vector<long> &previousOid, vector<long> &previousHash, int dataType, wchar_t* path) {

    wchar_t wfilename[DIM_FILE];
    wchar_t* pt = NULL;
    char filename [DIM_FILE];
    BOOL usedDefaultChar;

    if (dataType == olFolderContacts)
        pt = TEXT(FILENAME_CONTACTS);
    else if (dataType == olFolderCalendar)
        pt = TEXT(FILENAME_APPOINTMENTS);
    else if (dataType == olFolderTasks)
        pt = TEXT(FILENAME_TASKS);

    if (wcscmp(path, TEXT("\\")) == 0)
        wsprintf(wfilename, TEXT("\\%s"), pt);
    else
        wsprintf(wfilename, TEXT("%s\\%s"),path, pt);

    WideCharToMultiByte (CP_ACP, 0, wfilename, DIM_FILE -1, filename, DIM_FILE -1, "?", &usedDefaultChar);

    wchar_t* element = NULL;
    wchar_t line[100];
    FILE* f;
    wchar_t* tmp = NULL;
    if (dataType == olFolderContacts)
        f = fopen(filename, "r");
    else if (dataType == olFolderCalendar)
        f = fopen(filename, "r");
    else if (dataType == olFolderTasks)
        f = fopen(filename, "r");

    while(fgetws(line, 99, f) != NULL) {

        if ((element = getElementContent(line, TEXT("oid"), NULL)) != NULL) {
            previousOid.push_back(wcstol(element, &tmp, 0));
        }

        else if ((element = getElementContent(line, TEXT("hash"), NULL)) != NULL) {
            previousHash.push_back(wcstol(element, &tmp, 0));
        }

        else if ((element = getElementContent(line, TEXT("num"), NULL)) != NULL) {
            (*previousCountItem) = wcstol(element, &tmp, 0);
        }
        delete [] element;

    }
        fflush(f);
        fclose(f);
}



void readAppOidToReset(vector<long> &oidToReset, wchar_t* wpath) {

    wstring filename(L"appOidToReset.dat");
    wstring path(wpath);
    path += L"\\";
    path += filename;

    wchar_t* element = NULL;
    wchar_t line[100];
    FILE* f;
    wchar_t* tmp = NULL;
    f = _wfopen(path.c_str(), L"r");

    if (!f) {
        return;
    }

    while(fgetws(line, 99, f) != NULL) {
        if ((element = getElementContent(line, TEXT("oid"), NULL)) != NULL) {
            oidToReset.push_back(wcstol(element, &tmp, 0));
        }
        delete [] element;
    }
    fflush(f);
    fclose(f);

    DeleteFile(path.c_str());
}

⌨️ 快捷键说明

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