Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-qml-plugin-calendar] Support custom reminder times. Contributes…
… to JB#33108
  • Loading branch information
Chris Adams committed Jan 11, 2019
1 parent edda2b8 commit 4ebbb3a
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/calendardata.h
Expand Up @@ -33,6 +33,7 @@ struct Event {
NemoCalendarEvent::Recur recur;
QDate recurEndDate;
NemoCalendarEvent::Reminder reminder;
int customReminder; // minutes
QString uniqueId;
KDateTime recurrenceId;
bool readonly;
Expand Down
5 changes: 5 additions & 0 deletions src/calendarevent.cpp
Expand Up @@ -97,6 +97,11 @@ NemoCalendarEvent::Reminder NemoCalendarEvent::reminder() const
return mManager->getEvent(mUniqueId, mRecurrenceId).reminder;
}

int NemoCalendarEvent::customReminder() const
{
return mManager->getEvent(mUniqueId, mRecurrenceId).customReminder;
}

QString NemoCalendarEvent::uniqueId() const
{
return mUniqueId;
Expand Down
6 changes: 5 additions & 1 deletion src/calendarevent.h
Expand Up @@ -60,6 +60,7 @@ class NemoCalendarEvent : public QObject
Q_PROPERTY(QDateTime recurEndDate READ recurEndDate NOTIFY recurEndDateChanged)
Q_PROPERTY(bool hasRecurEndDate READ hasRecurEndDate NOTIFY hasRecurEndDateChanged)
Q_PROPERTY(NemoCalendarEvent::Reminder reminder READ reminder NOTIFY reminderChanged)
Q_PROPERTY(int customReminder READ customReminder NOTIFY customReminderChanged)
Q_PROPERTY(QString uniqueId READ uniqueId NOTIFY uniqueIdChanged)
Q_PROPERTY(QString recurrenceId READ recurrenceIdString CONSTANT)
Q_PROPERTY(QString color READ color NOTIFY colorChanged)
Expand Down Expand Up @@ -90,7 +91,8 @@ class NemoCalendarEvent : public QObject
Reminder1Hour,
Reminder2Hour,
Reminder1Day,
Reminder2Day
Reminder2Day,
ReminderCustom
};

enum TimeSpec {
Expand Down Expand Up @@ -138,6 +140,7 @@ class NemoCalendarEvent : public QObject
QDateTime recurEndDate() const;
bool hasRecurEndDate() const;
Reminder reminder() const;
int customReminder() const;
QString uniqueId() const;
QString color() const;
bool readonly() const;
Expand All @@ -164,6 +167,7 @@ private slots:
void allDayChanged();
void recurChanged();
void reminderChanged();
void customReminderChanged();
void uniqueIdChanged();
void colorChanged();
void calendarUidChanged();
Expand Down
14 changes: 14 additions & 0 deletions src/calendareventmodification.cpp
Expand Up @@ -11,6 +11,7 @@ NemoCalendarEventModification::NemoCalendarEventModification(QObject *parent) :
{
m_event.recur = NemoCalendarEvent::RecurOnce;
m_event.reminder = NemoCalendarEvent::ReminderNone;
m_event.customReminder = 0;
m_event.allDay = false;
m_event.readonly = false;
m_event.startTime = KDateTime(QDateTime(), KDateTime::LocalZone);
Expand Down Expand Up @@ -163,6 +164,19 @@ void NemoCalendarEventModification::setReminder(NemoCalendarEvent::Reminder remi
}
}

int NemoCalendarEventModification::customReminder() const
{
return m_event.customReminder;
}

void NemoCalendarEventModification::setCustomReminder(int minutes)
{
if (minutes != m_event.customReminder) {
m_event.customReminder = minutes;
emit customReminderChanged();
}
}

QString NemoCalendarEventModification::location() const
{
return m_event.location;
Expand Down
5 changes: 5 additions & 0 deletions src/calendareventmodification.h
Expand Up @@ -21,6 +21,7 @@ class NemoCalendarEventModification : public QObject
Q_PROPERTY(bool hasRecurEndDate READ hasRecurEndDate NOTIFY hasRecurEndDateChanged)
Q_PROPERTY(QString recurrenceId READ recurrenceIdString CONSTANT)
Q_PROPERTY(NemoCalendarEvent::Reminder reminder READ reminder WRITE setReminder NOTIFY reminderChanged)
Q_PROPERTY(int customReminder READ customReminder WRITE setCustomReminder NOTIFY customReminderChanged)
Q_PROPERTY(QString location READ location WRITE setLocation NOTIFY locationChanged)
Q_PROPERTY(QString calendarUid READ calendarUid WRITE setCalendarUid NOTIFY calendarUidChanged)

Expand Down Expand Up @@ -57,6 +58,9 @@ class NemoCalendarEventModification : public QObject
NemoCalendarEvent::Reminder reminder() const;
void setReminder(NemoCalendarEvent::Reminder);

int customReminder() const;
void setCustomReminder(int minutes);

QString location() const;
void setLocation(const QString &newLocation);

Expand All @@ -74,6 +78,7 @@ class NemoCalendarEventModification : public QObject
void allDayChanged();
void recurChanged();
void reminderChanged();
void customReminderChanged();
void locationChanged();
void recurEndDateChanged();
void hasRecurEndDateChanged();
Expand Down
17 changes: 14 additions & 3 deletions src/calendarimportevent.cpp
Expand Up @@ -101,12 +101,23 @@ NemoCalendarEvent::Reminder CalendarImportEvent::reminder() const
return NemoCalendarUtils::getReminder(mEvent);
}

int CalendarImportEvent::customReminder() const
{
if (reminder() == NemoCalendarEvent::ReminderNone) {
return 0; // FIXME: this doesn't know how to say "no reminder"
}

// note: returns minutes before event, so 15 minutes before = 15
return NemoCalendarUtils::getCustomReminder(mEvent);
}

int CalendarImportEvent::reminderSeconds() const
{
// FIXME: this doesn't know how to say "no reminder".
if (!mEvent)
return 0;
if (reminder() == NemoCalendarEvent::ReminderNone) {
return 0; // FIXME: this doesn't know how to say "no reminder"
}

// note: returns seconds after event, so 15 minutes before = -900
bool hasReminder = false;
return NemoCalendarUtils::getReminderSeconds(mEvent, &hasReminder);
}
Expand Down
6 changes: 4 additions & 2 deletions src/calendarimportevent.h
Expand Up @@ -49,8 +49,9 @@ class CalendarImportEvent : public QObject
Q_PROPERTY(QDateTime endTime READ endTime CONSTANT)
Q_PROPERTY(bool allDay READ allDay CONSTANT)
Q_PROPERTY(NemoCalendarEvent::Recur recur READ recur CONSTANT)
Q_PROPERTY(int reminderSeconds READ reminderSeconds CONSTANT)
Q_PROPERTY(NemoCalendarEvent::Reminder reminder READ reminder CONSTANT)
Q_PROPERTY(int customReminder READ customReminder CONSTANT)
Q_PROPERTY(int reminderSeconds READ reminderSeconds CONSTANT)
Q_PROPERTY(QString uniqueId READ uniqueId CONSTANT)
Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
Q_PROPERTY(QString location READ location CONSTANT)
Expand All @@ -71,8 +72,9 @@ class CalendarImportEvent : public QObject
QDateTime endTime() const;
bool allDay() const;
NemoCalendarEvent::Recur recur();
int reminderSeconds() const;
NemoCalendarEvent::Reminder reminder() const;
int customReminder() const; // minutes
int reminderSeconds() const;
QString uniqueId() const;
QString color() const;
QString location() const;
Expand Down
3 changes: 3 additions & 0 deletions src/calendarmanager.cpp
Expand Up @@ -778,6 +778,9 @@ void NemoCalendarManager::sendEventChangeSignals(const NemoCalendarData::Event &
if (newEvent.reminder != oldEvent.reminder)
emit eventObject->reminderChanged();

if (newEvent.customReminder != oldEvent.customReminder)
emit eventObject->customReminderChanged();

if (newEvent.startTime != oldEvent.startTime)
emit eventObject->startTimeChanged();

Expand Down
42 changes: 23 additions & 19 deletions src/calendarutils.cpp
Expand Up @@ -111,6 +111,18 @@ int NemoCalendarUtils::getReminderSeconds(const KCalCore::Event::Ptr &event, boo
return d.asSeconds();
}

int NemoCalendarUtils::getCustomReminder(const KCalCore::Event::Ptr &event)
{
bool hasReminder = false;
int sec = getReminderSeconds(event, &hasReminder);

if (!hasReminder) {
return 0;
}

return -1 * sec / 60; // inverse of customReminderToDuration
}

NemoCalendarEvent::Reminder NemoCalendarUtils::getReminder(const KCalCore::Event::Ptr &event)
{
bool hasReminder = false;
Expand All @@ -120,25 +132,17 @@ NemoCalendarEvent::Reminder NemoCalendarUtils::getReminder(const KCalCore::Event
return NemoCalendarEvent::ReminderNone;
}

if (sec >= 0) {
return NemoCalendarEvent::ReminderTime;
} else if (sec >= (-5 * 60)) {
return NemoCalendarEvent::Reminder5Min;
} else if (sec >= (-15 * 60)) {
return NemoCalendarEvent::Reminder15Min;
} else if (sec >= (-30 * 60)) {
return NemoCalendarEvent::Reminder30Min;
} else if (sec >= (-60 * 60)) {
return NemoCalendarEvent::Reminder1Hour;
} else if (sec >= (-2 * 60 * 60)) {
return NemoCalendarEvent::Reminder2Hour;
} else if (sec >= (-24 * 60 * 60)) {
return NemoCalendarEvent::Reminder1Day;
} else if (sec >= (-2 * 24 * 60 * 60)
&& sec <= (-3 * 24 * 60 * 60)) {
return NemoCalendarEvent::Reminder2Day;
} else {
return NemoCalendarEvent::ReminderNone;
switch (sec) {
case 0: return NemoCalendarEvent::ReminderTime;
case (-5 * 60): return NemoCalendarEvent::Reminder5Min;
case (-15 * 60): return NemoCalendarEvent::Reminder15Min;
case (-30 * 60): return NemoCalendarEvent::Reminder30Min;
case (-60 * 60): return NemoCalendarEvent::Reminder1Hour;
case (-2 * 60 * 60): return NemoCalendarEvent::Reminder2Hour;
case (-24 * 60 * 60): return NemoCalendarEvent::Reminder1Day;
case (-2 * 24 * 60 * 60): return NemoCalendarEvent::Reminder2Day;
default: return (sec > 0) ? NemoCalendarEvent::ReminderCustom
: NemoCalendarEvent::ReminderNone;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/calendarutils.h
Expand Up @@ -46,6 +46,7 @@ NemoCalendarEvent::Recur convertRecurrence(const KCalCore::Event::Ptr &event);
NemoCalendarEvent::Secrecy convertSecrecy(const KCalCore::Event::Ptr &event);
int getReminderSeconds(const KCalCore::Event::Ptr &event, bool *hasReminder);
NemoCalendarEvent::Reminder getReminder(const KCalCore::Event::Ptr &event);
int getCustomReminder(const KCalCore::Event::Ptr &event);
QList<NemoCalendarData::Attendee> getEventAttendees(const KCalCore::Event::Ptr &event, const QString &ownerEmail);
QList<QObject*> convertAttendeeList(const QList<NemoCalendarData::Attendee> &list);
NemoCalendarData::EventOccurrence getNextOccurrence(const KCalCore::Event::Ptr &event,
Expand Down
21 changes: 17 additions & 4 deletions src/calendarworker.cpp
Expand Up @@ -277,7 +277,7 @@ void NemoCalendarWorker::setEventData(KCalCore::Event::Ptr &event, const NemoCal
// setDtStart() overwrites allDay status based on KDateTime::isDateOnly(), avoid by setting that later
event->setAllDay(eventData.allDay);
event->setLocation(eventData.location);
setReminder(event, eventData.reminder);
setReminder(event, eventData.reminder, eventData.customReminder);
setRecurrence(event, eventData.recur);

if (eventData.recur != NemoCalendarEvent::RecurOnce) {
Expand Down Expand Up @@ -378,6 +378,11 @@ bool NemoCalendarWorker::setRecurrence(KCalCore::Event::Ptr &event, NemoCalendar
return false;
}

KCalCore::Duration NemoCalendarWorker::customReminderToDuration(int minutes) const
{
return KCalCore::Duration(-1 * minutes * 60);
}

KCalCore::Duration NemoCalendarWorker::reminderToDuration(NemoCalendarEvent::Reminder reminder) const
{
KCalCore::Duration offset(0);
Expand Down Expand Up @@ -411,12 +416,13 @@ KCalCore::Duration NemoCalendarWorker::reminderToDuration(NemoCalendarEvent::Rem
return offset;
}

bool NemoCalendarWorker::setReminder(KCalCore::Event::Ptr &event, NemoCalendarEvent::Reminder reminder)
bool NemoCalendarWorker::setReminder(KCalCore::Event::Ptr &event, NemoCalendarEvent::Reminder reminder, int customReminder)
{
if (!event)
return false;

if (NemoCalendarUtils::getReminder(event) == reminder)
if (reminder != NemoCalendarEvent::ReminderCustom
&& NemoCalendarUtils::getReminder(event) == reminder)
return false;

KCalCore::Alarm::List alarms = event->alarms();
Expand All @@ -429,7 +435,11 @@ bool NemoCalendarWorker::setReminder(KCalCore::Event::Ptr &event, NemoCalendarEv
if (reminder != NemoCalendarEvent::ReminderNone) {
KCalCore::Alarm::Ptr alarm = event->newAlarm();
alarm->setEnabled(true);
alarm->setStartOffset(reminderToDuration(reminder));
if (reminder == NemoCalendarEvent::ReminderCustom) {
alarm->setStartOffset(customReminderToDuration(customReminder));
} else {
alarm->setStartOffset(reminderToDuration(reminder));
}
alarm->setType(KCalCore::Alarm::Display);
}

Expand Down Expand Up @@ -710,6 +720,9 @@ NemoCalendarData::Event NemoCalendarWorker::createEventStruct(const KCalCore::Ev
event.recurEndDate = defaultRule->endDt().date();
}
event.reminder = NemoCalendarUtils::getReminder(e);
if (event.reminder == NemoCalendarEvent::ReminderCustom) {
event.customReminder = NemoCalendarUtils::getCustomReminder(e);
}
event.startTime = e->dtStart();
return event;
}
Expand Down
3 changes: 2 additions & 1 deletion src/calendarworker.h
Expand Up @@ -114,10 +114,11 @@ public slots:
bool saveExcludeNotebook(const QString &notebookUid, bool exclude);

bool setRecurrence(KCalCore::Event::Ptr &event, NemoCalendarEvent::Recur recur);
bool setReminder(KCalCore::Event::Ptr &event, NemoCalendarEvent::Reminder reminder);
bool setReminder(KCalCore::Event::Ptr &event, NemoCalendarEvent::Reminder reminder, int customReminder);
bool needSendCancellation(KCalCore::Event::Ptr &event) const;

KCalCore::Duration reminderToDuration(NemoCalendarEvent::Reminder reminder) const;
KCalCore::Duration customReminderToDuration(int minutes) const;
NemoCalendarData::Event createEventStruct(const KCalCore::Event::Ptr &event) const;
QHash<QString, NemoCalendarData::EventOccurrence> eventOccurrences(const QList<NemoCalendarData::Range> &ranges) const;
QHash<QDate, QStringList> dailyEventOccurrences(const QList<NemoCalendarData::Range> &ranges,
Expand Down

0 comments on commit 4ebbb3a

Please sign in to comment.