EventEditor.cpp Example File
calendar/src/EventEditor.cpp
#include "EventEditor.hpp"
#include <bb/cascades/Option>
#include <bb/pim/calendar/CalendarEvent>
#include <bb/pim/calendar/CalendarFolder>
#include <bb/pim/calendar/EventSearchParameters>
using namespace bb::cascades;
using namespace bb::pim::calendar;
EventEditor::EventEditor(CalendarService *service, QObject *parent)
: QObject(parent)
, m_calendarService(service)
, m_mode(CreateMode)
{
}
void EventEditor::initializeFolderDropDown(DropDown *dropDown)
{
if (!dropDown)
return;
dropDown->removeAll();
bool firstEntry = true;
foreach (const CalendarFolder &folder, m_calendarService->folders()) {
if (folder.isReadOnly())
continue;
Option *option = new Option();
option->setText(folder.name());
QVariantMap value;
value["folderId"] = folder.id();
value["accountId"] = folder.accountId();
option->setValue(value);
if (firstEntry) {
firstEntry = false;
option->setSelected(true);
}
dropDown->add(option);
}
}
void EventEditor::loadEvent(const EventKey &eventKey)
{
m_eventKey = eventKey;
const CalendarEvent event = m_calendarService->event(m_eventKey.accountId(), m_eventKey.eventId());
m_subject = event.subject();
m_location = event.location();
m_startTime = event.startTime();
m_endTime = event.endTime();
m_folderId = event.folderId();
m_accountId = event.accountId();
emit subjectChanged();
emit locationChanged();
emit startTimeChanged();
emit endTimeChanged();
emit folderIdChanged();
emit accountIdChanged();
}
void EventEditor::saveEvent()
{
if (m_mode == CreateMode) {
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);
m_calendarService->createEvent(event);
} else if (m_mode == EditMode) {
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);
m_calendarService->updateEvent(event);
}
}
void EventEditor::reset()
{
m_subject.clear();
m_location.clear();
m_startTime = QDateTime::currentDateTime();
m_endTime = QDateTime::currentDateTime();
m_folderId = -1;
m_accountId = -1;
emit subjectChanged();
emit locationChanged();
emit startTimeChanged();
emit endTimeChanged();
emit folderIdChanged();
emit accountIdChanged();
}
void EventEditor::setSubject(const QString &subject)
{
if (m_subject == subject)
return;
m_subject = subject;
emit subjectChanged();
}
QString EventEditor::subject() const
{
return m_subject;
}
void EventEditor::setLocation(const QString &location)
{
if (m_location == location)
return;
m_location = location;
emit locationChanged();
}
QString EventEditor::location() const
{
return m_location;
}
void EventEditor::setStartTime(const QDateTime &startTime)
{
if (m_startTime == startTime)
return;
m_startTime = startTime;
emit startTimeChanged();
}
QDateTime EventEditor::startTime() const
{
return m_startTime;
}
void EventEditor::setEndTime(const QDateTime &endTime)
{
if (m_endTime == endTime)
return;
m_endTime = endTime;
emit endTimeChanged();
}
QDateTime EventEditor::endTime() const
{
return m_endTime;
}
void EventEditor::setFolderId(int folderId)
{
if (m_folderId == folderId)
return;
m_folderId = folderId;
emit folderIdChanged();
}
int EventEditor::folderId() const
{
return m_folderId;
}
void EventEditor::setAccountId(int accountId)
{
if (m_accountId == accountId)
return;
m_accountId = accountId;
emit accountIdChanged();
}
int EventEditor::accountId() const
{
return m_accountId;
}
void EventEditor::setMode(Mode mode)
{
if (m_mode == mode)
return;
m_mode = mode;
emit modeChanged();
}
EventEditor::Mode EventEditor::mode() const
{
return m_mode;
}