//! [3]
void EventEditor::saveEvent()
{
    if (m_mode == CreateMode) {
        // Create a new event object
        CalendarEvent event;
        event.setAccountId(m_accountId);
        event.setFolderId(m_folderId);
        event.setSubject(m_subject);
        event.setLocation(m_location);
        event.setStartTime(m_startTime);
        event.setEndTime(m_endTime);

        // Save the event to persistent storage
        m_calendarService->createEvent(event);
    } else if (m_mode == EditMode) {
        // Load the event from persistent storage
        CalendarEvent event = m_calendarService->event(m_eventKey.accountId(), m_eventKey.eventId());

        event.setSubject(m_subject);
        event.setLocation(m_location);
        event.setStartTime(m_startTime);
        event.setEndTime(m_endTime);

        // Save the updated event back to persistent storage
        m_calendarService->updateEvent(event);
    }
}
void CalendarThreadWrapper::addToCalendar()
{
    const QString viaStation = m_result->viaStation();
    QSettings settings(FAHRPLAN_SETTINGS_NAMESPACE, "fahrplan2");
    QString calendarEntryTitle;
    QString calendarEntryDesc;

    if (viaStation.isEmpty())
        calendarEntryTitle = tr("%1 to %2").arg(m_result->departureStation(),
                                                m_result->arrivalStation());
    else
        calendarEntryTitle = tr("%1 via %3 to %2").arg(m_result->departureStation(),
                                                       m_result->arrivalStation(),
                                                       viaStation);

    if (!m_result->info().isEmpty())
        calendarEntryDesc.append(m_result->info()).append("\n");

    const bool compactFormat = settings.value("compactCalendarEntries", false).toBool();
    for (int i=0; i < m_result->itemcount(); i++) {
        JourneyDetailResultItem *item = m_result->getItem(i);

        if (!compactFormat && !item->train().isEmpty())
            calendarEntryDesc.append("--- ").append(item->train()).append(" ---\n");

        calendarEntryDesc.append(formatStation(item->departureDateTime(),
                                               item->departureStation(),
                                               item->departureInfo()));
        calendarEntryDesc.append("\n");

        if (compactFormat && !item->train().isEmpty())
            calendarEntryDesc.append("--- ").append(item->train()).append(" ---\n");

        calendarEntryDesc.append(formatStation(item->arrivalDateTime(),
                                               item->arrivalStation(),
                                               item->arrivalInfo()));
        calendarEntryDesc.append("\n");

        if (!item->info().isEmpty()) {
            calendarEntryDesc.append(item->info()).append("\n");
        }

        if (!compactFormat)
            calendarEntryDesc.append("\n");
    }

    if (!compactFormat)
        calendarEntryDesc.append(
            tr("-- \nAdded by Fahrplan. Please, re-check the information before your journey."));

#ifdef BUILD_FOR_BLACKBERRY

    CalendarService service;

    QPair<AccountId, FolderId> folder;

    settings.beginGroup("Calendar");
    folder.first = settings.value("AccountId", -1).toInt();
    if (folder.first >= 0)
        folder.second = settings.value("FolderId", -1).toInt();

    if ((folder.first < 0) || (folder.second < 0))
        folder = service.defaultCalendarFolder();

    CalendarEvent event;
    event.setAccountId(folder.first);
    event.setFolderId(folder.second);
    event.setSubject(calendarEntryTitle);
    event.setStartTime(m_result->departureDateTime());
    event.setEndTime(m_result->arrivalDateTime());
    event.setBody(calendarEntryDesc);
    event.setReminder(-1);

    emit addCalendarEntryComplete(service.createEvent(event) == Result::Success);

#elif defined(BUILD_FOR_HARMATTAN) || defined(BUILD_FOR_MAEMO_5) || defined(BUILD_FOR_SYMBIAN)

    QOrganizerManager defaultManager;
    QOrganizerEvent event;
    event.setDisplayLabel(calendarEntryTitle);
    event.setStartDateTime(m_result->departureDateTime());
    event.setEndDateTime(m_result->arrivalDateTime());
    event.setDescription(calendarEntryDesc);

    QString id = settings.value("Calendar/CollectionId").toString();
    if (!id.isEmpty()) {
        QOrganizerCollectionId collectionId = QOrganizerCollectionId::fromString(id);
        if (!collectionId.isNull())
            event.setCollectionId(collectionId);
    }

    emit addCalendarEntryComplete(defaultManager.saveItem(&event));
#else
    emit addCalendarEntryComplete(false);
#endif

    QThread::currentThread()->exit(0);

    // Move back to GUI thread so the deleteLater() callback works (it requires
    // an event loop which is still alive)
    moveToThread(QCoreApplication::instance()->thread());
}