winevent.cpp

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

CPP
878
字号
/*
 * 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 "base/util/utils.h"
#include "vocl/WinEvent.h"
#include "vocl/VConverter.h"
#include "base/stringUtils.h"
#include "base/timeUtils.h"
#include "base/globalsdef.h"

USE_NAMESPACE

using namespace std;



// Constructor
WinEvent::WinEvent() {
    vCalendar = L"";
    excludeDate.clear();
    includeDate.clear();
    recipients.clear();
    useTimezone = false;
}

// Constructor: fills propertyMap parsing the passed data
WinEvent::WinEvent(const wstring dataString) {
    vCalendar = L"";
    excludeDate.clear();
    includeDate.clear();
    recipients.clear();
    useTimezone = false;

    parse(dataString);
}

// Destructor
WinEvent::~WinEvent() {
}




// Format and return a vCalendar string from the propertyMap.
wstring& WinEvent::toString() {

    vCalendar = L"";

    //
    // Conversion: WinEvent -> vObject.
    // --------------------------------
    //
    VObject* vo = new VObject();
    VProperty* vp  = NULL;
    DATE startdate = NULL;
    wstring element;


    bool isRecurring = false;
    if (getProperty(L"IsRecurring", element)) {
        isRecurring = (element != TEXT("0"));
    }


    vp = new VProperty(TEXT("BEGIN"), TEXT("VCALENDAR"));
    vo->addProperty(vp);
    delete vp; vp = NULL;

    vp = new VProperty(TEXT("VERSION"), VCALENDAR_VERSION);
    vo->addProperty(vp);
    delete vp; vp = NULL;


    // TIMEZONE: placed out of VEVENT. 
    // Adding it only if the event is recurring.
    if (useTimezone && isRecurring) {
        addTimezone(vo);
    }

    vp = new VProperty(TEXT("BEGIN"), TEXT("VEVENT"));
    vo->addProperty(vp);
    delete vp; vp = NULL;


    // Folder path.
    if (getProperty(L"Folder", element)) {
        vp = new VProperty(L"X-FUNAMBOL-FOLDER");
        vp->addValue(element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }

    if (getProperty(L"AllDayEvent", element)) {
        vp = new VProperty(TEXT("X-FUNAMBOL-ALLDAY"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Start", element)) {
        stringTimeToDouble(element, &startdate);                // Used later for reminder...
        vp = new VProperty(TEXT("DTSTART"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"End", element)) {
        vp = new VProperty(TEXT("DTEND"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }


    if (getProperty(L"BusyStatus", element)) {
        vp = new VProperty(TEXT("X-MICROSOFT-CDO-BUSYSTATUS"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Categories", element)) {
        vp = new VProperty(TEXT("CATEGORIES"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Body", element)) {
        vp = new VProperty(TEXT("DESCRIPTION"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Location", element)) {
        vp = new VProperty(TEXT("LOCATION"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Importance", element)) {
        vp = new VProperty(TEXT("PRIORITY"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"MeetingStatus", element)) {
        vp = new VProperty(TEXT("STATUS"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"ReplyTime", element)) {
        vp = new VProperty(TEXT("X-MICROSOFT-CDO-REPLYTIME"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Subject", element)) {
        vp = new VProperty(TEXT("SUMMARY"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Sensitivity", element)) {
        long sensitivity = _wtoi(element.c_str());
        vp = new VProperty(TEXT("CLASS"));
        if(sensitivity == winPrivate) {
            vp->addValue(TEXT("PRIVATE"));
        }
        else if (sensitivity == winConfidential) {
            vp->addValue(TEXT("CONFIDENTIAL"));
        }
        else {  // default value
            vp->addValue(TEXT("PUBLIC"));
        }
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }

    //
    // ReminderSet
    //
    if (getProperty(L"ReminderSet", element)) {
        bool bReminder = (element != TEXT("0"));

        if(bReminder == true) {
            long minBefore;
            if (getProperty(L"ReminderMinutesBeforeStart", element)) {
                minBefore = _wtoi(element.c_str());
                double minStartDate = startdate * 1440;
                //subtract the alarm
                minStartDate -= minBefore;
                wstring runtime;
                doubleToStringTime(runtime, minStartDate/1440);

                vp = new VProperty(L"AALARM");
                vp->addValue(runtime.c_str());                      // "RunTime"
                vp->addValue(L"");                                  // "Snooze Time" (empty)
                vp->addValue(L"0");                                 // "Repeat Count"

                getProperty(L"ReminderSoundFile", element);         // (empty if not found)
                vp->addValue(element.c_str());                      // "Audio Content" = sound file path

                vo->addProperty(vp);
                delete vp; vp = NULL;
            }
        }
        else {
            // No reminder: send empty "AALARM:"
            vp = new VProperty(L"AALARM");
            vo->addProperty(vp);
            delete vp; vp = NULL;
        }
    }


    if (isRecurring) {
        //
        // Recurrence pattern -> RRULE
        //
        wstring rRule = recPattern.toString();
        if(rRule != L"") {
            vp = new VProperty(TEXT("RRULE"), rRule.c_str());
            vo->addProperty(vp);
            delete vp; vp = NULL;
        }

        list<wstring>::iterator it;

        // Exceptions: EXDATE
        vp = new VProperty(TEXT("EXDATE"));
        for (it  = excludeDate.begin(); it != excludeDate.end(); it++) {
            wstring date = (*it);
            vp->addValue(date.c_str());
        }
        vo->addProperty(vp);
        delete vp; vp = NULL;

        // Exceptions: RDATE (should be empty for Outlook and WM)
        vp = new VProperty(TEXT("RDATE"));
        for (it  = includeDate.begin(); it != includeDate.end(); it++) {
            wstring date = (*it);
            vp->addValue(date.c_str());
        }
        vo->addProperty(vp);
        delete vp; vp = NULL;

    }
    else {
        // Not recurring: send empty "RRULE:"
        vp = new VProperty(TEXT("RRULE"));
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }



    //
    // TODO: format ATTENDEES
    //


    //
    // ---- Other Funambol defined properties ----
    // Support for other fields that don't have a
    // specific correspondence in vCalendar.
    if (getProperty(TEXT("BillingInformation"), element)) {
        vp = new VProperty(TEXT("X-FUNAMBOL-BILLINGINFO"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Companies", element)) {
        vp = new VProperty(TEXT("X-FUNAMBOL-COMPANIES"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"Mileage", element)) {
        vp = new VProperty(TEXT("X-FUNAMBOL-MILEAGE"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"NoAging", element)) {
        vp = new VProperty(TEXT("X-FUNAMBOL-NOAGING"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }
    if (getProperty(L"ReminderOptions", element)) {
        vp = new VProperty(TEXT("X-FUNAMBOL-AALARMOPTIONS"), element.c_str());
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }


    vp = new VProperty(TEXT("END"), TEXT("VEVENT"));
    vo->addProperty(vp);
    delete vp; vp = NULL;

    vp = new VProperty(TEXT("END"), TEXT("VCALENDAR"));
    vo->addProperty(vp);
    delete vp; vp = NULL;


    //
    // Format the vCalendar.
    // ---------------------
    //
    WCHAR* tmp = vo->toString();
    if (tmp) {
        vCalendar = tmp;
        delete [] tmp;
    }
    return vCalendar;
}



void WinEvent::addTimezone(VObject* vo) {

    VProperty* vp  = NULL;
    wstring element;

    //
    // TZ: "signed numeric indicating the number of hours and possibly minutes from UTC."
    // TZ = - (Bias + StandardBias)     [StandardBias is usually = 0]
    //
    wstring bias = formatBias(tzInfo.Bias + tzInfo.StandardBias);
    vp = new VProperty(TEXT("TZ"), bias.c_str());
    vo->addProperty(vp);
    delete vp; vp = NULL;

    //
    // DAYLIGHT: "sequence of components that define the daylight savings time rule."
    //
    int yearBegin = 0;
    int yearEnd = 5000;
    getIntervalOfRecurrence(&yearBegin, &yearEnd);


    // DST offset = - (Bias + StandardBias + DaylightBias)
    // [StandardBias is usually = 0]
    wstring hasDST;
    int diffBias = tzInfo.Bias +  + tzInfo.StandardBias + tzInfo.DaylightBias;
    wstring daylightBias;
    if (diffBias != 0) { 
        hasDST = TEXT("TRUE");
        daylightBias = formatBias(diffBias);
    }
    else {
        hasDST = TEXT("FALSE");
    }


    // Max 6 iterations (for infinite recurrences).
    if (yearEnd - yearBegin > MAX_DAYLIGHT_PROPS) {
        yearEnd = yearBegin + MAX_DAYLIGHT_PROPS;
    }
    if (hasDayLightSaving(&tzInfo)) {
        // Add a DAYLIGHT property for every year that this appointment occurr. (max = 6)
        for (int year = yearBegin; year <= yearEnd; year++) {

            wstring daylightDate = getDateFromTzRule(year, tzInfo.DaylightDate);
            wstring standardDate = getDateFromTzRule(year, tzInfo.StandardDate);

            // "DAYLIGHT:TRUE;-0900;20080406T020000;20081026T020000;Pacific Standard Time;Pacific Daylight Time"
            vp = new VProperty(TEXT("DAYLIGHT"));
            vp->addValue(hasDST.c_str());               // DST flag
            if (hasDST == TEXT("TRUE")) {
                vp->addValue(daylightBias.c_str());     // DST offset = (Bias + DaylightBias)
                vp->addValue(daylightDate.c_str());     // Date and time when the DST begins
                vp->addValue(standardDate.c_str());     // Date and time when the DST ends
                vp->addValue(tzInfo.StandardName);      // Standard time designation (optional, could be empty)
                vp->addValue(tzInfo.DaylightName);      // DST designation (optional, could be empty)
            }
            vo->addProperty(vp);
            delete vp; vp = NULL;

            if (hasDST == TEXT("FALSE")) {
                break;    // Send only 1 property, are all the same.
            }
        }
    } else {
        // there is no DAYLIGHT saving
        vp = new VProperty(TEXT("DAYLIGHT"));
        vp->addValue(TEXT("FALSE"));
        vo->addProperty(vp);
        delete vp; vp = NULL;
    }

}




//
// Parse a vCalendar string and fills the propertyMap.
//
int WinEvent::parse(const wstring dataString) {

    WCHAR* element = NULL;
    DATE startDate = NULL;
    DATE endDate   = NULL;
    wstring startDateValue, endDateValue;

    //
    // Parse the vCalendar and fill the VObject.
    // -----------------------------------------
    //
    VObject* vo = VConverter::parse(dataString.c_str());
    if (!vo) {
        setError(1, ERR_ITEM_VOBJ_PARSE);
        LOG.error("%s", getLastErrorMsg());
        return 1;
    }
    // Check if VObject type and version are the correct ones.
    if (!checkVCalendarTypeAndVersion(vo)) {
        if (vo) delete vo;
        return 1;
    }


⌨️ 快捷键说明

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