Calendar.cpp Example File
calendar/src/Calendar.cpp
#include "Calendar.hpp"
#include "EventEditor.hpp"
#include "EventViewer.hpp"
#include <bb/pim/calendar/CalendarEvent>
#include <bb/pim/calendar/CalendarFolder>
#include <bb/pim/calendar/EventKey>
#include <bb/pim/calendar/EventSearchParameters>
using namespace bb::cascades;
using namespace bb::pim::calendar;
Q_DECLARE_METATYPE(bb::pim::calendar::CalendarEvent)
Calendar::Calendar(QObject *parent)
: QObject(parent)
, m_model(new GroupDataModel(this))
, m_calendarService(new CalendarService())
, m_eventViewer(new EventViewer(m_calendarService, this))
, m_eventEditor(new EventEditor(m_calendarService, this))
{
m_model->setGrouping(ItemGrouping::None);
bool ok = connect(m_calendarService, SIGNAL(eventsRefreshed(bb::pim::calendar::EventRefresh)), SLOT(filterEvents()));
Q_ASSERT(ok);
Q_UNUSED(ok);
setFilter("today");
filterEvents();
}
void Calendar::setCurrentEvent(const QVariantList &indexPath)
{
if (indexPath.isEmpty()) {
m_currentEventKey = EventKey();
} else {
const QVariantMap entry = m_model->data(indexPath).toMap();
m_currentEventKey.setEventId(entry.value("eventId").toInt());
m_currentEventKey.setAccountId(entry.value("accountId").toInt());
}
}
void Calendar::createEvent()
{
m_eventEditor->reset();
m_eventEditor->setMode(EventEditor::CreateMode);
}
void Calendar::editEvent()
{
m_eventEditor->loadEvent(m_currentEventKey);
m_eventEditor->setMode(EventEditor::EditMode);
}
void Calendar::viewEvent()
{
m_eventViewer->setEventKey(m_currentEventKey);
}
void Calendar::deleteEvent()
{
const CalendarEvent event = m_calendarService->event(m_currentEventKey.accountId(), m_currentEventKey.eventId());
m_calendarService->deleteEvent(event);
}
bb::cascades::GroupDataModel* Calendar::model() const
{
return m_model;
}
QString Calendar::filter() const
{
return m_filter;
}
void Calendar::setFilter(const QString &filter)
{
if (m_filter == filter)
return;
m_filter = filter;
emit filterChanged();
const QDate today = QDate::currentDate();
const QTime midnight(0, 0, 0);
if (m_filter == QLatin1String("today")) {
m_searchStartTime = QDateTime(today, midnight);
m_searchEndTime = QDateTime(today.addDays(1), midnight);
} else if (m_filter == QLatin1String("week")) {
const QDate thisMonday = today.addDays(-(today.dayOfWeek() - 1));
m_searchStartTime = QDateTime(thisMonday, midnight);
m_searchEndTime = QDateTime(thisMonday.addDays(7), midnight);
} else if (m_filter == QLatin1String("month")) {
const QDate firstDayInMonth = QDate(today.year(), today.month(), 1);
m_searchStartTime = QDateTime(firstDayInMonth, midnight);
m_searchEndTime = QDateTime(firstDayInMonth.addDays(firstDayInMonth.daysInMonth()), midnight);
}
filterEvents();
}
EventViewer* Calendar::eventViewer() const
{
return m_eventViewer;
}
EventEditor* Calendar::eventEditor() const
{
return m_eventEditor;
}
void Calendar::filterEvents()
{
EventSearchParameters searchParameters;
searchParameters.setStart(m_searchStartTime);
searchParameters.setEnd(m_searchEndTime);
searchParameters.setDetails(DetailLevel::Weekly);
const QList<CalendarEvent> events = m_calendarService->events(searchParameters);
m_model->clear();
foreach (const CalendarEvent &event, events) {
QVariantMap entry;
entry["eventId"] = event.id();
entry["accountId"] = event.accountId();
entry["subject"] = event.subject();
entry["startTime"] = event.startTime().toString(Qt::DefaultLocaleShortDate);
entry["endTime"] = event.endTime().toString(Qt::DefaultLocaleShortDate);
m_model->insert(entry);
}
}