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

📄 utilsrecurrence.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 <stdio.h>
#include <windows.h>
#include <Winbase.h>
#include "base/Log.h"
#include "base/util/utils.h"
#include "base/base64.h"
#include "base/util/XMLProcessor.h"

#include "pim/Utils.h"
#include "pim/UtilsRecurrence.h"
#include "pim/RecurrenceException.h"

#define STRING_NO_RECURRENCE    TEXT("<RecurrenceType/><Interval/><MonthOfYear/><DayOfMonth/><DayOfWeekMask/><Instance/><PatternStartDate/><NoEndDate/><PatternEndDate/><Occurrences/><Exceptions/>")
#define EXCLUDE_DATE            TEXT("ExcludeDate")
#define INCLUDE_DATE            TEXT("IncludeDate")

int convertDayOfWeekMaskBackward(int dayOfWeekMaskSent) {
    int dayOfWeekMask = 0;
    if ((dayOfWeekMaskSent % 2) != 0) {
        dayOfWeekMask = dayOfWeekMaskSent >> 1;
        dayOfWeekMask += 64;
    }
    else {
        dayOfWeekMask = dayOfWeekMaskSent >> 1;
    }
    return dayOfWeekMask;
}
int convertDayOfWeekMaskForward(int dayOfWeekMaskSent) {
    int dayOfWeekMask = 0;
    if (dayOfWeekMaskSent >= 64) {
        dayOfWeekMask = dayOfWeekMaskSent << 1;
        dayOfWeekMask -= 127;
    }
    else {
        dayOfWeekMask = dayOfWeekMaskSent << 1;
    }
    return dayOfWeekMask;
}

wstring getRecurrenceTags(IRecurrencePattern* pRecurrence, VARIANT_BOOL isRecurring,
                          int x, int y, OlDefaultFolders olFolder, DATE startdate, BOOL isAllDay) {

    wstring appointmentStringItem = TEXT("");
    wchar_t tempDate [DIM_LOCAL_TEMP];
    VARIANT_BOOL variantBool = NULL; // 0==false -1==true

    if (isRecurring == FALSE) {
        return STRING_NO_RECURRENCE;
    }

    int z = 0;
    BOOL changeDay = FALSE, isBackward = FALSE;
    BOOL changeYearBackward = FALSE, changeYearForward = FALSE;

    double toCompare = MAX_DATE_DOUBLE;
    double toCompare4000 = MAX_DATE_DOUBLE_4000;

    BSTR element        = NULL      ;
    long elementLong    = 0         ;

    DATE date           = NULL      ;
    wstring element_s;

    long            recurrenceType = 0,
                    interval       = 0,
                    monthOfYear    = 0,
                    dayOfMonth     = 0,
                    dayOfWeekMask  = 0,
                    instance       = 0;

    pRecurrence->get_RecurrenceType(&recurrenceType);
    pRecurrence->get_Interval(&interval);
    pRecurrence->get_MonthOfYear(&monthOfYear);
    pRecurrence->get_DayOfMonth(&dayOfMonth);
    pRecurrence->get_Instance(&instance);
    pRecurrence->get_DayOfWeekMask(&dayOfWeekMask);

    //
    // Modification to get right information about changing day, month...
    //
    TIME_ZONE_INFORMATION tmz;
    int k = GetTimeZoneInformation(&tmz);

    z = (x-y) < 0 ? (x-y)*(-1) : (x-y) ;

    //
    // Change day?
    //
    changeDay = z > 12 ? TRUE : FALSE;

    if (isAllDay)
        changeDay = FALSE;

    if (changeDay) {
        if (tmz.Bias < 0)
            isBackward = TRUE;
        else
            isBackward = FALSE;
    }

    //
    // RecurrenceType
    //
    if (recurrenceType < 0) {
        recurrenceType = 0;
    }
    wsprintf(tempDate, TEXT("%i"), recurrenceType);
    appointmentStringItem  = appointmentStringItem + TEXT("<RecurrenceType>") + tempDate + TEXT("</RecurrenceType>");

    //
    // Interval
    //
    wsprintf(tempDate, TEXT("%i"), interval);
    appointmentStringItem  = appointmentStringItem + TEXT("<Interval>") + tempDate + TEXT("</Interval>");

    //
    // Day of Month
    //
    if (dayOfMonth != 0) {

        if (changeDay) {
            if (isBackward) {
                dayOfMonth = dayOfMonth - 1;
                if (dayOfMonth == 0) {
                    if (monthOfYear == 5 || monthOfYear == 7 || monthOfYear == 10) {
                        // if May, July, October the month before has 30 days
                        dayOfMonth = 30;
                    } else {
                        dayOfMonth = 31;
                    }
                    changeYearBackward = TRUE;
                }
            } else { // is
                dayOfMonth = dayOfMonth + 1;
                if (dayOfMonth >  31  ||
                      (dayOfMonth == 31 &&
                        (monthOfYear == 4 || monthOfYear == 6 || monthOfYear == 9)
                       )
                    ) {
                    dayOfMonth = 1;
                    changeYearForward = TRUE;
                }
            }

        } //endif

        wsprintf(tempDate, TEXT("%i"), dayOfMonth);
        appointmentStringItem  = appointmentStringItem + TEXT("<DayOfMonth>") + tempDate + TEXT("</DayOfMonth>");
    } else {
        appointmentStringItem  = appointmentStringItem + TEXT("<DayOfMonth>0</DayOfMonth>");
    }

    //
    // Month of Year
    //
    if (monthOfYear != 0) {
        if (changeYearBackward) {
            monthOfYear = monthOfYear - 1;
            if (monthOfYear == 0) {
                monthOfYear = 12;
            }
        } else if (changeYearForward) {
            monthOfYear = monthOfYear + 1;
            if (monthOfYear > 12) {
                monthOfYear = 1;
            }
        }
        wsprintf(tempDate, TEXT("%i"), monthOfYear);
        appointmentStringItem  = appointmentStringItem + TEXT("<MonthOfYear>") + tempDate + TEXT("</MonthOfYear>");
    } else {
        appointmentStringItem  = appointmentStringItem + TEXT("<MonthOfYear>0</MonthOfYear>");
    }
    //
    // DayOfWeekMask
    //
    if (dayOfWeekMask != 0) {
        if (changeDay) {
            // do not change week mask
            if (isBackward) {
                dayOfWeekMask = convertDayOfWeekMaskBackward(dayOfWeekMask);
            } else {
                dayOfWeekMask = convertDayOfWeekMaskForward(dayOfWeekMask);
            }
        }
        wsprintf(tempDate, TEXT("%i"), dayOfWeekMask);
        appointmentStringItem  = appointmentStringItem + TEXT("<DayOfWeekMask>") + tempDate + TEXT("</DayOfWeekMask>");
    } else {
        appointmentStringItem  = appointmentStringItem + TEXT("<DayOfWeekMask>0</DayOfWeekMask>");
    }

    //
    // Instance
    //
    if (instance != 0) {
        wsprintf(tempDate, TEXT("%i"), instance);
        appointmentStringItem  = appointmentStringItem + TEXT("<Instance>") + tempDate + TEXT("</Instance>");
    } else {
        appointmentStringItem  = appointmentStringItem + TEXT("<Instance>0</Instance>");
    }

    //
    // Pattern Start Date
    //
    pRecurrence->get_PatternStartDate(&date);
    wsprintf(tempDate, TEXT(""));
    toCompare = MAX_DATE_DOUBLE;

    //
    // We use the start date to avoid every problem
    //
    if (date != toCompare && date < toCompare4000 ) {
        if (isAllDay) {
            doubleToSystemTimeBirthday(tempDate, startdate);
        } else {
            doubleToSystemTime(tempDate, startdate);
        }
        //doubleToSystemTime(tempDate, date);

    }
    appointmentStringItem  = appointmentStringItem + TEXT("<PatternStartDate>") + tempDate + TEXT("</PatternStartDate>");

    //
    // No End Date
    //
    pRecurrence->get_NoEndDate(&variantBool);
    wsprintf(tempDate, TEXT("%i"), variantBool);
    normalizeBoolean(tempDate);
    appointmentStringItem  = appointmentStringItem + TEXT("<NoEndDate>") + tempDate + TEXT("</NoEndDate>");

    //
    // Pattern End Date
    //
    pRecurrence->get_PatternEndDate(&date);
    wsprintf(tempDate, TEXT(""));
    toCompare = MAX_DATE_DOUBLE;

    if (date != toCompare && date < toCompare4000 ) {

        doubleToSystemTime(tempDate, date);
        /*
        if (isAllDay) {
            doubleToSystemTimeBirthday(tempDate, date);
        } else {
            SYSTEMTIME t;
            VariantTimeToSystemTime(date, &t);
            localTimeToUTC2(t);

            //
            // the ppc doesn't update the end date as desktop Oultlook.
            // so we need to change the day if there is change day
            //
            if (changeDay) {
                if (isBackward) {
                    date -= 1;
                } else {
                    date += 1;
                }
            }

            wsprintf(tempDate, TEXT("%d-%02d-%02d"), t.wYear, t.wMonth, t.wDay);
        }
        */

    }
    appointmentStringItem  = appointmentStringItem + TEXT("<PatternEndDate>") + tempDate + TEXT("</PatternEndDate>");

    //
    // Occurrences
    //

    pRecurrence->get_Occurrences(&elementLong);
    if (elementLong < 0) {
        elementLong = 0;
    }
    wsprintf(tempDate, TEXT("%i"), elementLong);
    appointmentStringItem  = appointmentStringItem + TEXT("<Occurrences>") + tempDate + TEXT("</Occurrences>");

    //
    // ---------------- EXCEPTIONS for calendar only ---------
    //

    if (olFolder == olFolderCalendar) {
        if (isRecurring == -1) {
            wstring exceptionItems = createExceptions(pRecurrence, startdate, isAllDay);
            appointmentStringItem += exceptionItems;
        } else {
            appointmentStringItem += TEXT("<Exceptions/>");
        }
    } else if (olFolder == olFolderTasks) {
        //
        // Exceptions in POOM tasks are not permitted!!
        //
        appointmentStringItem += TEXT("<Exceptions/>");
    }

    //
    // ---------------- END EXCEPTIONS ------------------------
    //

    return appointmentStringItem;

}

/*
* Modificated exception creation from client to server.
* The normalization is done in the begin sync process. At this point the items that are
* here should not have any exceptions appointments excepts deletions
*/
wstring createExceptions(IRecurrencePattern* pRecurrence, DATE startdate, BOOL isAllDay) {

    IExceptions* pExceptions;
    int numExceptions;

    pRecurrence->get_Exceptions(&pExceptions);
    pExceptions->get_Count(&numExceptions);

    wstring exceptions = TEXT("<Exceptions>");

    for (int i = 0; i < numExceptions; i++) {
        IException* pException;
        pExceptions->Item(i + 1, &pException);
        wstring exception = createException(pException, startdate, isAllDay);
        exceptions += exception;
        pException->Release();
    }

    pExceptions->Release();

    // the exceptions if exists are only in exclude date and so deleted and never created
    exceptions +=  TEXT("<IncludeDate/>");

    exceptions +=  TEXT("</Exceptions>");

    return exceptions;
}

wstring createException(IException* pException, DATE start_date, BOOL isAllDay) {

    wstring exceptionItem = TEXT("");
    DATE originalDate = 0;
    VARIANT_BOOL varBool; // -1 = TRUE, 0 = FALSE
    wchar_t tempDate [DIM_LOCAL_TEMP];

    pException->get_OriginalDate(&originalDate);

    DATE offset = (start_date) - ((long)start_date) ;
    originalDate += offset;
    if (isAllDay) {
        doubleToSystemTimeBirthday(tempDate, originalDate);    // YYYY-MM-DD
    }
    else {
        doubleToSystemTime(tempDate, originalDate);
    }

    // if the recurrence is deleted get the delation and go exit
    pException->get_Deleted(&varBool);
    if (varBool == -1) {
        exceptionItem += TEXT("<ExcludeDate>") + wstring(tempDate) + TEXT("</ExcludeDate>");
    } else {
        LOG.info("Warning: exception in date %s is not deleted.", tempDate);
    }
    return exceptionItem;
}

/**
 * Parse the recurrence tags in the item sent from server to client.
 *
 * @param pRecurrence       the POOM object to fill
 * @param ptrData           the SIF item to parse
 * @param recurrenceStart   the start date of the event or task
 * @param x                 used to check the day shift
 * @param y                 used to check the day shift
 */
void parseRecurrenceTags(IRecurrencePattern *pRecurrence,
                         const wchar_t* ptrData,
                         DATE recurrenceStart,
                         int x, int y) {

    wchar_t* dummy                 = NULL;
    wchar_t* dummyEvent            = NULL;
    wstring  dummyEvent_s;
    VARIANT_BOOL isNotAllDayEvent  = TRUE;
    long            recurrenceType = 0,
                    interval       = 0,
                    monthOfYear    = 0,
                    dayOfMonth     = 0,
                    dayOfWeekMask  = 0,
                    instance       = 0;

    //
    // FOR RECURRENCE
    // to get if a modification in the UTC goes into day before or after
    // store the initial value of day and check the last value
    //
    int z = 0;
    BOOL changeDay = FALSE, isBackward = FALSE;
    BOOL changeYearBackward = FALSE, changeYearForward = FALSE;
    //
    // Modification to get right information about changing day, month...
    //

⌨️ 快捷键说明

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