appointmentbuilder.cpp

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

CPP
1,079
字号

    vector<RecurrenceException> listExceptions;
    list<wstring>::iterator it;
    vector<RecurrenceException>::iterator exc;
    vector<long> oids;
    vector<long>::iterator oidsIterator;
    int numExceptions               = 0;
    IExceptions* pExceptions        = NULL;


    if (oidToUpdate) {
        //
        // It's an UPDATED item: Clear the recurrence pattern & save event: 
        // if the event is recurring or not, we have to delete any previous information 
        // regarding recurrence and exceptions.
        //
        pOutlookApp = getOutlookApp();
        if (!pOutlookApp) { goto finally; }

        // 'pAppointmentOld' is the IAppointment (old interface)
        hr = pOutlookApp->GetItemFromOid(oidToUpdate, (IDispatch**)&pAppointmentOld);
        if (!pAppointmentOld || FAILED(hr)) { goto finally; }

        hr = pAppointmentOld->ClearRecurrencePattern();
        if (FAILED(hr)) {
            LOG.error("Error clearing rec pattern");
        }

        hr = pAppointmentOld->Save();
        if (FAILED(hr)) {
            LOG.error("Error saving appointment \"%ls\"", winE.getName().c_str());
            goto finally;
        }
        pAppointmentOld->Release();
        pAppointmentOld = NULL;
    }



    //
    // Check if it's an all-day event / recurring.
    //
    bool isAllDay = false, isRecurring = false;
    int tmp = 0;
    if (winE.getIntProperty(TEXT("AllDayEvent"), &tmp)) {
        if (tmp == 1) isAllDay = true;
    }
    if (winE.getIntProperty(TEXT("IsRecurring"), &tmp)) {
        if (tmp == 1) isRecurring = true;
    }


    //
    // Set all normal properties.
    //
    for (int i=0; i < ePIMPR_RECURRING_TYPE; i++) {
        exists = false;
        wstring& value = winE.getPropertyRef(appointmentFields[i], &exists);

        if (i == ePIMPR_TIMEZONE) {
            // Correction: timezone is stored into a tzInfo structure.
            exists = winE.hasTimezone();
        }

        if (exists) {
            switch (i)
            {
            case ePIMPR_START:
            case ePIMPR_END:
                {
                    // Date values: "yyyyMMdd" or "YYYYMMDDThhmmssZ"
                    date = getDatePropertyFromString(value, isAllDay);
                    setDatePropertyToMapi(&prgPropvalPoom[incNumProp], PIMPR_props_app[i], date);
                    //LOG.debug("%2d. %ls = %ls (date) (%lu %lu)", incNumProp+1, appointmentFields[i], value.c_str(), date.dwHighDateTime, date.dwLowDateTime);
                    break;
                }
            case ePIMPR_ALL_DAY_EVENT:
            case ePIMPR_SENSITIVITY:
            case ePIMPR_IS_RECURRING:
            case ePIMPR_REMINDER_SET:
            case ePIMPR_REMINDER_OPTIONS:
          //case ePIMPR_DURATION:
            case ePIMPR_BUSY_STATUS:
            case ePIMPR_REMINDER_MINUTES_BEFORE_START:
            case ePIMPR_MEETING_STATUS:
                {
                    // Int values: should add safe checks?                  ***** TODO ******
                    int val = getIntPropertyFromWinItem(value);
                    setIntPropertyToMapi(&prgPropvalPoom[incNumProp], PIMPR_props_app[i], val);
                    //LOG.debug("%2d. %ls = %d (int)", incNumProp+1, appointmentFields[i], val);
                    break;
                }
            case ePIMPR_TIMEZONE:
                {
                    // Timezone
                    setTimezoneToMapi(&prgPropvalPoom[incNumProp], PIMPR_props_app[i], winE.getTimezone());
                    break;
                }
            case ePIMPR_BODY_TEXT:
                {
                    convertNewlines(value);     // BUG #1562
                    setStringPropertyToMapi(&prgPropvalPoom[incNumProp], PIMPR_props_app[i], value);
                    //LOG.debug("%2d. %ls = %ls (body)", incNumProp+1, appointmentFields[i], value.c_str());
                    break;
                }
            default:
                {
                    // String values.
                    setStringPropertyToMapi(&prgPropvalPoom[incNumProp], PIMPR_props_app[i], value);
                    //LOG.debug("%2d. %ls = %ls (string)", incNumProp+1, appointmentFields[i], value.c_str());
                    break;
                }
            }
            incNumProp++;
        }
    }


    if (isRecurring) {
        //
        // Manage recurring properties: need to retrieve all values together
        // ---------------------------- to apply safe checks and conversions.
        //
        winRec = winE.getRecPattern();
        if (!winRec) { goto finally; }

        winE.getProperty(TEXT("Start"), start);
        winE.getProperty(TEXT("End"),   end);
        incNumProp = setMAPIRecurrenceProps(prgPropvalPoom, incNumProp, *winRec, start, 
                                            end, isAllDay, winE.getTimezone(), true);
    }


    //
    // Save all props together (with rec props if recurring).
    //
    hr = saveItemProps(pAppointment, incNumProp, prgPropvalPoom);
    if (FAILED(hr)) {
        LOG.error("Error saving appointment \"%ls\"", winE.getName().c_str());
        goto finally;
    }

    if (!isRecurring) {
        // Not recurring: finished.
        goto finally;
    }


    //
    // Get again the IAppointment to manage exceptions.
    //
    long appOid = 0;
    hr = pAppointment->get_Oid(&appOid);
    if (!appOid || FAILED(hr))          { goto finally; }

    if (!pOutlookApp) {
        pOutlookApp = getOutlookApp();
        if (!pOutlookApp)               { goto finally; }
    }
    hr = pOutlookApp->GetItemFromOid(appOid, (IDispatch**)&pAppointmentOld);
    if (!pAppointmentOld || FAILED(hr)) { goto finally; }

    hr = pAppointmentOld->GetRecurrencePattern(&pRecurrence);
    if (!pRecurrence || FAILED(hr))     { goto finally; }


    // Safe check?
    //if (pRecurrence->get_Exceptions(&pExceptions)) {
    //    if (pExceptions->get_Count(&numExceptions)) {
    //        if (numExceptions > 0) {
    //            LOG.info("Warning: event still have %d exceptions...", numExceptions);
    //        }
    //    }
    //}


    //
    // Manage appointment exceptions (old style).
    // ------------------------------------------
    // note: MUST set exceptions AFTER the item is saved.
    // note: MUST clear rec pattern before handling exceptions.
    //
    exceptionList* exDates = winE.getExcludeDates();
    exceptionList* inDates = winE.getIncludeDates();
    if (!exDates->size() && !inDates->size()) {
        // No exceptions.
        goto finally;
    }


    // Fill listExceptions with EXDATEs
    listExceptions.clear();
    for (it = exDates->begin(); it != exDates->end(); it++) {
        RecurrenceException rec;
        DATE originalDate = 0;
        LOG.debug("originalDate = %ls (allday = %d)", (*it).c_str(), isAllDay);

        if (isAllDay) { systemTimeToDoubleBirthFormat((*it).c_str(), &originalDate, TEXT("START")); }
        else          { systemTimeToDouble           ((*it).c_str(), &originalDate, NULL         ); }

        rec.setOriginalDate(originalDate);
        rec.setIsDeleted(TRUE);
        listExceptions.push_back(rec);
    }

    // Fill listExceptions with RDATEs
    for (it = inDates->begin(); it != inDates->end(); it++) {
        RecurrenceException rec;
        DATE originalDate;
        LOG.debug("originalDate = %ls (allday = %d)", (*it).c_str(), isAllDay);

        if (isAllDay) { systemTimeToDoubleBirthFormat((*it).c_str(), &originalDate, TEXT("START")); }
        else          { systemTimeToDouble           ((*it).c_str(), &originalDate, NULL         ); }

        rec.setOriginalDate(originalDate);
        rec.setIsDeleted(FALSE);
        rec.setAllDayEvent(isAllDay);
        listExceptions.push_back(rec);
    }


    LOG.debug("Adding exceptions...");
    for (exc = listExceptions.begin(); exc != listExceptions.end(); exc++) {
        if ((*exc).isDeleted()) {
            //
            // EXDATE: delete the occurrence
            //
            IAppointment* pExAppointment;
            hr = pRecurrence->GetOccurrence((*exc).getOriginalDate(), &pExAppointment);
            if(!pExAppointment || FAILED(hr)) {
                LOG.error("Error in handling calendar exception. pExAppointment is NULL. Date: %f", (*exc).getOriginalDate());
                continue;
            }
            pExAppointment->Delete();
            pExAppointment->Release();
        }

        else {
            //
            // RDATE: not supported by pOutlook. Create a new indipendent event and
            // add recurring event to 'oidToReset' list (will be sent as modified next time)
            //
            oids.push_back(appOid);
            long oid = createAppointmentFromException(pAppointmentOld, *exc, pOutlookApp);
            oids.push_back(oid);
        }
    }

    if (oids.size() > 0) {
        writeOidToReset(oids, path.c_str());
    }

    hr = S_OK;


finally:

    // Release objects.
    if (pRecurrence)     { pRecurrence->Release();     }
    if (pAppointmentOld) { pAppointmentOld->Release(); }

    if (prgPropvalPoom) {
        delete [] prgPropvalPoom;
    }

    //LOG.debug("Exiting winEventToAppointment");
    return hr;
}

ArrayList* getAppointmentProperties(){

    int i = 0;
    ArrayList* props = new ArrayList();

    while (appointmentFields[i]){
        Property prop;
        char* field = toMultibyte(appointmentFields[i]);
        prop.setPropName(field);
        props->add(prop);
        
        delete [] field;

        i++;
    }

    return props;
}

ArrayList* getVCalendarProperties(){

    ArrayList* props = new ArrayList();
    Property* p = new Property();
    ArrayList valenums;
    StringBuffer val;


    p->setPropName("BEGIN");
    val = "VCALENDAR";
    valenums.add(val);
    p->setValEnums(&valenums);
    props->add(*p);

    valenums.clear();

    p->setPropName("END");
    val = "VCALENDAR";
    valenums.add(val);
    p->setValEnums(&valenums);
    props->add(*p);

    valenums.clear();
    
    p->setPropName("VERSION");
    val = "1.0";
    valenums.add(val);
    p->setValEnums(&valenums);
    props->add(*p);

    p->setPropName("X-FUNAMBOL-ALLDAY");
    props->add(*p);

    p->setPropName("DESCRIPTION");
    props->add(*p);
    p->setPropName("X-MICROSOFT-CDO-BUSYSTATUS");
    props->add(*p);
    p->setPropName("CATEGORIES");
    props->add(*p);
    p->setPropName("DTEND");
    props->add(*p);
    p->setPropName("LOCATION");
    props->add(*p);
    p->setPropName("STATUS");
    props->add(*p);
    p->setPropName("X-FUNAMBOL-AALARMOPTIONS");
    props->add(*p);
    p->setPropName("AALARM");
    props->add(*p);
    p->setPropName("CLASS");
    //TODO: Values?
    props->add(*p);
    p->setPropName("DTSTART");
    props->add(*p);
    p->setPropName("SUMMARY");
    props->add(*p);
    p->setPropName("RRULE");
    props->add(*p);
    p->setPropName("EXDATE");
    props->add(*p);
    p->setPropName("RDATE");
    props->add(*p);
    p->setPropName("TZ");
    props->add(*p);
    p->setPropName("DAYLIGHT");
    props->add(*p);

    delete p; p = NULL;

    return props;
}

⌨️ 快捷键说明

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