Skip to content

Commit

Permalink
[mkcal] Enable / disable alarms on notebook visibility change. Contri…
Browse files Browse the repository at this point in the history
…butes to JB#49136
  • Loading branch information
dcaliste committed Mar 12, 2020
1 parent d757f2b commit 8d6eafd
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 9 deletions.
5 changes: 4 additions & 1 deletion rpm/mkcal-qt5.spec
Expand Up @@ -13,10 +13,13 @@ Requires(postun): /sbin/ldconfig
BuildRequires: pkgconfig(Qt5Core)
BuildRequires: pkgconfig(Qt5Gui)
BuildRequires: pkgconfig(Qt5DBus)
BuildRequires: pkgconfig(libkcalcoren-qt5)
BuildRequires: kcalcore-qt5-devel >= 4.10.2+9git23
BuildRequires: pkgconfig(sqlite3)
BuildRequires: pkgconfig(timed-qt5) >= 2.88
BuildRequires: pkgconfig(QmfClient)
Requires: kcalcore-qt5 >= 4.10.2+9git23
# One cannot use the pkgconfig test for kcalcore because the version is not update
# there due to local patching.

%description
Extended KDE kcal calendar library port for Maemo
Expand Down
88 changes: 80 additions & 8 deletions src/extendedstorage.cpp
Expand Up @@ -65,9 +65,8 @@ using namespace mKCal;
class mKCal::ExtendedStorage::Private
{
public:
Private(const ExtendedCalendar::Ptr &cal, bool validateNotebooks)
: mCalendar(cal),
mValidateNotebooks(validateNotebooks),
Private(bool validateNotebooks)
: mValidateNotebooks(validateNotebooks),
mIsUncompletedTodosLoaded(false),
mIsCompletedTodosDateLoaded(false),
mIsCompletedTodosCreatedLoaded(false),
Expand All @@ -82,7 +81,6 @@ class mKCal::ExtendedStorage::Private
mDefaultNotebook(0)
{}

ExtendedCalendar::Ptr mCalendar;
bool mValidateNotebooks;
QDate mStart;
QDate mEnd;
Expand All @@ -101,15 +99,17 @@ class mKCal::ExtendedStorage::Private
QHash<QString, Notebook::Ptr> mNotebooks; // uid to notebook
Notebook::Ptr mDefaultNotebook;

void setAlarmsForNotebook(const KCalCore::Incidence::List &incidences, const QString &nbuid);
#if defined(TIMED_SUPPORT)
void setAlarms(const Incidence::Ptr &incidence, Timed::Event::List &events, const KDateTime &now);
void setAlarms(const Incidence::Ptr &incidence, const QString &nbuid, Timed::Event::List &events, const KDateTime &now);
void commitEvents(Timed::Event::List &events);
#endif
};
//@endcond

ExtendedStorage::ExtendedStorage(const ExtendedCalendar::Ptr &cal, bool validateNotebooks)
: CalStorage(cal),
d(new ExtendedStorage::Private(cal, validateNotebooks))
d(new ExtendedStorage::Private(validateNotebooks))
{
// Add the calendar as observer
registerObserver(cal.data());
Expand Down Expand Up @@ -393,13 +393,22 @@ bool ExtendedStorage::updateNotebook(const Notebook::Ptr &nb)
return false;
}

bool wasVisible = calendar()->isVisible(nb->uid());
if (!calendar()->updateNotebook(nb->uid(), nb->isVisible())) {
qCWarning(lcMkcal) << "cannot update notebook" << nb->uid() << "in calendar";
return false;
}
if (!modifyNotebook(nb, DBUpdate)) {
return false;
}
if (wasVisible && !nb->isVisible()) {
clearAlarms(nb->uid());
} else if (!wasVisible && nb->isVisible()) {
Incidence::List list;
if (allIncidences(&list, nb->uid())) {
d->setAlarmsForNotebook(list, nb->uid());
}
}

return true;
}
Expand Down Expand Up @@ -569,6 +578,25 @@ void ExtendedStorage::resetAlarms(const Incidence::List &incidences)
#endif
}

void ExtendedStorage::setAlarms(const Incidence::List &incidences)
{
#if defined(TIMED_SUPPORT)
const KDateTime now = KDateTime::currentLocalDateTime();
Timed::Event::List events;
foreach (const Incidence::Ptr incidence, incidences) {
// The incidence from the list must be in the calendar and in a notebook.
const QString &nbuid = calendar()->notebook(incidence->uid());
if (!calendar()->isVisible(incidence) || nbuid.isEmpty()) {
continue;
}
d->setAlarms(incidence, nbuid, events, now);
}
d->commitEvents(events);
#else
Q_UNUSED(incidences);
#endif
}

void ExtendedStorage::setAlarms(const Incidence::Ptr &incidence)
{
#if defined(TIMED_SUPPORT)
Expand Down Expand Up @@ -738,12 +766,28 @@ Notebook::Ptr ExtendedStorage::createDefaultNotebook(QString name, QString color
return nbDefault;
}

void ExtendedStorage::Private::setAlarmsForNotebook(const KCalCore::Incidence::List &incidences, const QString &nbuid)
{
#if defined(TIMED_SUPPORT)
const KDateTime now = KDateTime::currentLocalDateTime();
// list of all timed events
Timed::Event::List events;
foreach (const Incidence::Ptr incidence, incidences) {
setAlarms(incidence, nbuid, events, now);
}
commitEvents(events);
#else
Q_UNUSED(incidences);
Q_UNUSED(nbuid);
#endif
}

#if defined(TIMED_SUPPORT)
void ExtendedStorage::Private::setAlarms(const Incidence::Ptr &incidence, Timed::Event::List &events,
void ExtendedStorage::Private::setAlarms(const Incidence::Ptr &incidence,
const QString &nbuid,
Timed::Event::List &events,
const KDateTime &now)
{
const QString nbuid = mCalendar->notebook(incidence->uid());
const Alarm::List alarms = incidence->alarms();
foreach (const Alarm::Ptr alarm, alarms) {
if (!alarm->enabled()) {
Expand Down Expand Up @@ -855,4 +899,32 @@ void ExtendedStorage::Private::setAlarms(const Incidence::Ptr &incidence, Timed:
}
}
}

void ExtendedStorage::Private::commitEvents(Timed::Event::List &events)
{
if (events.count() > 0) {
Timed::Interface timed;
if (!timed.isValid()) {
qCWarning(lcMkcal) << "cannot set alarm for incidence: "
<< "alarm interface is not valid" << timed.lastError();
return;
}
QDBusReply < QList<QVariant> > reply = timed.add_events_sync(events);
if (reply.isValid()) {
foreach (QVariant v, reply.value()) {
bool ok = true;
uint cookie = v.toUInt(&ok);
if (ok && cookie) {
qCDebug(lcMkcal) << "added alarm: " << cookie;
} else {
qCWarning(lcMkcal) << "failed to add alarm";
}
}
} else {
qCWarning(lcMkcal) << "failed to add alarms: " << reply.error().message();
}
} else {
qCDebug(lcMkcal) << "No alarms to send";
}
}
#endif
5 changes: 5 additions & 0 deletions src/extendedstorage.h
Expand Up @@ -654,6 +654,11 @@ class MKCAL_EXPORT ExtendedStorage
*/
void setAlarms(const KCalCore::Incidence::Ptr &incidence);

/**
Set alarms for a list of incidences.
*/
void setAlarms(const KCalCore::Incidence::List &incidences);

/**
Creates and sets a default notebook. Usually called for an empty
calendar.
Expand Down

0 comments on commit 8d6eafd

Please sign in to comment.