Merge lp:~charlesk/indicator-datetime/lp-1465806-inject-missing-triggers-to-clock-app-alarms into lp:indicator-datetime/15.04

Proposed by Charles Kerr
Status: Merged
Approved by: Ted Gould
Approved revision: 417
Merged at revision: 416
Proposed branch: lp:~charlesk/indicator-datetime/lp-1465806-inject-missing-triggers-to-clock-app-alarms
Merge into: lp:indicator-datetime/15.04
Diff against target: 374 lines (+321/-2)
5 files modified
src/engine-eds.cpp (+135/-2)
tests/CMakeLists.txt (+1/-0)
tests/print-to.h (+24/-0)
tests/test-eds-ics-missing-trigger.cpp (+116/-0)
tests/test-eds-ics-missing-trigger.ics (+45/-0)
To merge this branch: bzr merge lp:~charlesk/indicator-datetime/lp-1465806-inject-missing-triggers-to-clock-app-alarms
Reviewer Review Type Date Requested Status
Ted Gould (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+262297@code.launchpad.net

Commit message

Fix invalid valarms in older clock-app alarms.

Description of the change

When we connect to an EClient, look for clock-app alarms whose valarms don't have triggers. If any are found, inject one.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote :

+1

review: Approve
Revision history for this message
Pat McGowan (pat-mcgowan) wrote :

Does this run every time? Any concern about the overhead such that we would want to ensure it only runs once to fix them and never again? Or is there a way to avoid getting alarms that already have triggers?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/engine-eds.cpp'
2--- src/engine-eds.cpp 2015-05-21 12:47:24 +0000
3+++ src/engine-eds.cpp 2015-06-18 04:58:22 +0000
4@@ -297,10 +297,14 @@
5 // add the client to our collection
6 auto self = static_cast<Impl*>(gself);
7 g_debug("got a client for %s", e_cal_client_get_local_attachment_store(E_CAL_CLIENT(client)));
8- self->m_clients[e_client_get_source(client)] = E_CAL_CLIENT(client);
9+ auto source = e_client_get_source(client);
10+ auto ecc = E_CAL_CLIENT(client);
11+ self->m_clients[source] = ecc;
12+
13+ self->ensure_client_alarms_have_triggers(ecc);
14
15 // now create a view for it so that we can listen for changes
16- e_cal_client_get_view (E_CAL_CLIENT(client),
17+ e_cal_client_get_view (ecc,
18 "#t", // match all
19 self->m_cancellable,
20 on_client_view_ready,
21@@ -409,6 +413,135 @@
22 static_cast<Impl*>(gself)->set_dirty_soon();
23 }
24
25+ /***
26+ ****
27+ ***/
28+
29+ // old ubuntu-clock-app alarms created VTODO VALARMS without the
30+ // required 'TRIGGER' property... http://pad.lv/1465806
31+
32+ void ensure_client_alarms_have_triggers(ECalClient* client)
33+ {
34+ // ask the EDS server for all the ubuntu-clock-app alarms...
35+
36+ auto sexp = g_strdup_printf("has-categories? '%s'", TAG_ALARM);
37+
38+ e_cal_client_get_object_list_as_comps(
39+ client,
40+ sexp,
41+ m_cancellable,
42+ ensure_client_alarms_have_triggers_async_cb,
43+ this);
44+
45+ g_clear_pointer(&sexp, g_free);
46+ }
47+
48+ static void ensure_client_alarms_have_triggers_async_cb(
49+ GObject * oclient,
50+ GAsyncResult * res,
51+ gpointer gself)
52+ {
53+ ECalClient * client = E_CAL_CLIENT(oclient);
54+ GError * error = nullptr;
55+ GSList * components = nullptr;
56+
57+ if (e_cal_client_get_object_list_as_comps_finish(client,
58+ res,
59+ &components,
60+ &error))
61+ {
62+ auto self = static_cast<Impl*>(gself);
63+ self->ensure_canonical_alarms_have_triggers(client, components);
64+ e_cal_client_free_ecalcomp_slist(components);
65+ }
66+ else if (error != nullptr)
67+ {
68+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
69+ g_warning("can't get clock-app alarm list: %s", error->message);
70+
71+ g_error_free(error);
72+ }
73+ }
74+
75+ void ensure_canonical_alarms_have_triggers(ECalClient * client,
76+ GSList * components)
77+ {
78+ GSList * modify_slist = nullptr;
79+
80+ // for each component..
81+ for (auto l=components; l!=nullptr; l=l->next)
82+ {
83+ bool changed = false;
84+
85+ // for each alarm...
86+ auto component = E_CAL_COMPONENT(l->data);
87+ auto auids = e_cal_component_get_alarm_uids(component);
88+ for(auto l=auids; l!=nullptr; l=l->next)
89+ {
90+ auto auid = static_cast<const char*>(l->data);
91+ auto alarm = e_cal_component_get_alarm(component, auid);
92+ if (alarm == nullptr)
93+ continue;
94+
95+ // if the alarm has no trigger, add one.
96+ ECalComponentAlarmTrigger trigger;
97+ e_cal_component_alarm_get_trigger(alarm, &trigger);
98+ if (trigger.type == E_CAL_COMPONENT_ALARM_TRIGGER_NONE)
99+ {
100+ trigger.type = E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START;
101+ trigger.u.rel_duration = icaldurationtype_from_int(0);
102+ e_cal_component_alarm_set_trigger (alarm, trigger);
103+ changed = true;
104+ }
105+
106+ g_clear_pointer(&alarm, e_cal_component_alarm_free);
107+ }
108+ g_clear_pointer(&auids, cal_obj_uid_list_free);
109+
110+ if (changed)
111+ {
112+ auto icc = e_cal_component_get_icalcomponent(component); // icc owned by ecc
113+ modify_slist = g_slist_prepend(modify_slist, icc);
114+ }
115+ }
116+
117+ if (modify_slist != nullptr)
118+ {
119+ e_cal_client_modify_objects(client,
120+ modify_slist,
121+ E_CAL_OBJ_MOD_ALL,
122+ m_cancellable,
123+ ensure_canonical_alarms_have_triggers_async_cb,
124+ this);
125+
126+ g_clear_pointer(&modify_slist, g_slist_free);
127+ }
128+ }
129+
130+ // log a warning if e_cal_client_modify_objects() failed
131+ static void ensure_canonical_alarms_have_triggers_async_cb(
132+ GObject * oclient,
133+ GAsyncResult * res,
134+ gpointer /*gself*/)
135+ {
136+ GError * error = nullptr;
137+
138+ e_cal_client_modify_objects_finish (E_CAL_CLIENT(oclient), res, &error);
139+
140+ if (error != nullptr)
141+ {
142+ if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
143+ g_warning("couldn't add alarm triggers: %s", error->message);
144+
145+ g_error_free(error);
146+ }
147+ }
148+
149+ /***
150+ ****
151+ ***/
152+
153+
154 typedef std::function<void(const std::vector<Appointment>&)> appointment_func;
155
156 struct Task
157
158=== modified file 'tests/CMakeLists.txt'
159--- tests/CMakeLists.txt 2015-05-21 12:47:24 +0000
160+++ tests/CMakeLists.txt 2015-06-18 04:58:22 +0000
161@@ -98,6 +98,7 @@
162 add_eds_ics_test_by_name(test-eds-ics-repeating-events)
163 add_eds_ics_test_by_name(test-eds-ics-nonrepeating-events)
164 add_eds_ics_test_by_name(test-eds-ics-repeating-valarms)
165+add_eds_ics_test_by_name(test-eds-ics-missing-trigger)
166
167
168 # disabling the timezone unit tests because they require
169
170=== modified file 'tests/print-to.h'
171--- tests/print-to.h 2015-05-21 12:47:24 +0000
172+++ tests/print-to.h 2015-06-18 04:58:22 +0000
173@@ -47,6 +47,30 @@
174 *os << '}';
175 }
176
177+void
178+PrintTo(const Appointment& appointment, std::ostream* os)
179+{
180+ *os << '{';
181+
182+ *os << "{uid:'" << appointment.uid << "'}"
183+ << "{color:'" << appointment.color << "'}"
184+ << "{summary:'" << appointment.summary << "'}"
185+ << "{activation_url:'" << appointment.activation_url << "'}";
186+
187+ *os << "{begin:";
188+ PrintTo(appointment.begin, os);
189+ *os << '}';
190+
191+ *os << "{end:";
192+ PrintTo(appointment.end, os);
193+ *os << '}';
194+
195+ for(const auto& alarm : appointment.alarms)
196+ PrintTo(alarm, os);
197+
198+ *os << '}';
199+}
200+
201 } // namespace datetime
202 } // namespace indicator
203 } // namespace unity
204
205=== added file 'tests/test-eds-ics-missing-trigger.cpp'
206--- tests/test-eds-ics-missing-trigger.cpp 1970-01-01 00:00:00 +0000
207+++ tests/test-eds-ics-missing-trigger.cpp 2015-06-18 04:58:22 +0000
208@@ -0,0 +1,116 @@
209+/*
210+ * Copyright 2015 Canonical Ltd.
211+ *
212+ * This program is free software: you can redistribute it and/or modify it
213+ * under the terms of the GNU General Public License version 3, as published
214+ * by the Free Software Foundation.
215+ *
216+ * This program is distributed in the hope that it will be useful, but
217+ * WITHOUT ANY WARRANTY; without even the implied warranties of
218+ * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
219+ * PURPOSE. See the GNU General Public License for more details.
220+ *
221+ * You should have received a copy of the GNU General Public License along
222+ * with this program. If not, see <http://www.gnu.org/licenses/>.
223+ *
224+ * Authors:
225+ * Charles Kerr <charles.kerr@canonical.com>
226+ */
227+
228+#include <algorithm>
229+
230+#include <datetime/alarm-queue-simple.h>
231+#include <datetime/clock-mock.h>
232+#include <datetime/engine-eds.h>
233+#include <datetime/planner-range.h>
234+
235+#include <gtest/gtest.h>
236+
237+#include "glib-fixture.h"
238+#include "print-to.h"
239+#include "timezone-mock.h"
240+#include "wakeup-timer-mock.h"
241+
242+using namespace unity::indicator::datetime;
243+using VAlarmFixture = GlibFixture;
244+
245+/***
246+****
247+***/
248+
249+TEST_F(VAlarmFixture, MissingTriggers)
250+{
251+ // start the EDS engine
252+ auto engine = std::make_shared<EdsEngine>();
253+
254+ // we need a consistent timezone for the planner and our local DateTimes
255+ constexpr char const * zone_str {"America/Chicago"};
256+ auto tz = std::make_shared<MockTimezone>(zone_str);
257+ auto gtz = g_time_zone_new(zone_str);
258+
259+ // make a planner that looks at the first half of 2015 in EDS
260+ auto planner = std::make_shared<SimpleRangePlanner>(engine, tz);
261+ const DateTime range_begin {gtz, 2015,1, 1, 0, 0, 0.0};
262+ const DateTime range_end {gtz, 2015,6,31,23,59,59.5};
263+ planner->range().set(std::make_pair(range_begin, range_end));
264+
265+ // give EDS a moment to load
266+ if (planner->appointments().get().empty()) {
267+ g_message("waiting a moment for EDS to load...");
268+ auto on_appointments_changed = [this](const std::vector<Appointment>& appointments){
269+ g_message("ah, they loaded");
270+ if (!appointments.empty())
271+ g_main_loop_quit(loop);
272+ };
273+ core::ScopedConnection conn(planner->appointments().changed().connect(on_appointments_changed));
274+ constexpr int max_wait_sec = 10;
275+ wait_msec(max_wait_sec * G_TIME_SPAN_MILLISECOND);
276+ }
277+
278+ // build expected: one-time alarm
279+ std::vector<Appointment> expected;
280+ Appointment a;
281+ a.type = Appointment::UBUNTU_ALARM;
282+ a.uid = "20150617T211838Z-6217-32011-2036-1@ubuntu-phablet";
283+ a.color = "#becedd";
284+ a.summary = "One Time Alarm";
285+ a.begin = DateTime { gtz, 2015, 6, 18, 10, 0, 0};
286+ a.end = a.begin;
287+ a.alarms.resize(1);
288+ a.alarms[0].audio_url = "file:///usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg";
289+ a.alarms[0].time = a.begin;
290+ a.alarms[0].text = a.summary;
291+ expected.push_back(a);
292+
293+ // build expected: recurring alarm
294+ a.uid = "20150617T211913Z-6217-32011-2036-5@ubuntu-phablet";
295+ a.summary = "Recurring Alarm";
296+ a.alarms[0].text = a.summary;
297+ std::array<DateTime,14> recurrences {
298+ DateTime{ gtz, 2015, 6, 18, 10, 1, 0 },
299+ DateTime{ gtz, 2015, 6, 19, 10, 1, 0 },
300+ DateTime{ gtz, 2015, 6, 20, 10, 1, 0 },
301+ DateTime{ gtz, 2015, 6, 21, 10, 1, 0 },
302+ DateTime{ gtz, 2015, 6, 22, 10, 1, 0 },
303+ DateTime{ gtz, 2015, 6, 23, 10, 1, 0 },
304+ DateTime{ gtz, 2015, 6, 24, 10, 1, 0 },
305+ DateTime{ gtz, 2015, 6, 25, 10, 1, 0 },
306+ DateTime{ gtz, 2015, 6, 26, 10, 1, 0 },
307+ DateTime{ gtz, 2015, 6, 27, 10, 1, 0 },
308+ DateTime{ gtz, 2015, 6, 28, 10, 1, 0 },
309+ DateTime{ gtz, 2015, 6, 29, 10, 1, 0 },
310+ DateTime{ gtz, 2015, 6, 30, 10, 1, 0 },
311+ DateTime{ gtz, 2015, 7, 1, 10, 1, 0 }
312+ };
313+ for (const auto& time : recurrences) {
314+ a.begin = a.end = a.alarms[0].time = time;
315+ expected.push_back(a);
316+ }
317+
318+ // the planner should match what we've got in the calendar.ics file
319+ const auto appts = planner->appointments().get();
320+ EXPECT_EQ(expected, appts);
321+
322+ // cleanup
323+ g_time_zone_unref(gtz);
324+}
325
326=== added file 'tests/test-eds-ics-missing-trigger.ics'
327--- tests/test-eds-ics-missing-trigger.ics 1970-01-01 00:00:00 +0000
328+++ tests/test-eds-ics-missing-trigger.ics 2015-06-18 04:58:22 +0000
329@@ -0,0 +1,45 @@
330+BEGIN:VCALENDAR
331+CALSCALE:GREGORIAN
332+PRODID:-//Ximian//NONSGML Evolution Calendar//EN
333+VERSION:2.0
334+X-EVOLUTION-DATA-REVISION:2015-06-17T21:19:13.980613Z(3)
335+BEGIN:VTODO
336+UID:20150617T211838Z-6217-32011-2036-1@ubuntu-phablet
337+DTSTAMP:20150617T211838Z
338+DTSTART:20150618T100000
339+SUMMARY:One Time Alarm
340+CATEGORIES:x-canonical-alarm
341+CREATED:20150617T211838Z
342+LAST-MODIFIED:20150617T211838Z
343+BEGIN:VALARM
344+X-EVOLUTION-ALARM-UID:20150617T211838Z-6217-32011-2036-2@ubuntu-phablet
345+ACTION:AUDIO
346+ATTACH:file:///usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg
347+END:VALARM
348+BEGIN:VALARM
349+X-EVOLUTION-ALARM-UID:20150617T211838Z-6217-32011-2036-3@ubuntu-phablet
350+ACTION:DISPLAY
351+DESCRIPTION:One Time Alarm
352+END:VALARM
353+END:VTODO
354+BEGIN:VTODO
355+UID:20150617T211913Z-6217-32011-2036-5@ubuntu-phablet
356+DTSTAMP:20150617T211913Z
357+DTSTART:20150618T100100
358+RRULE:FREQ=DAILY
359+SUMMARY:Recurring Alarm
360+CATEGORIES:x-canonical-alarm
361+CREATED:20150617T211913Z
362+LAST-MODIFIED:20150617T211913Z
363+BEGIN:VALARM
364+X-EVOLUTION-ALARM-UID:20150617T211913Z-6217-32011-2036-6@ubuntu-phablet
365+ACTION:AUDIO
366+ATTACH:file:///usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg
367+END:VALARM
368+BEGIN:VALARM
369+X-EVOLUTION-ALARM-UID:20150617T211913Z-6217-32011-2036-7@ubuntu-phablet
370+ACTION:DISPLAY
371+DESCRIPTION:Recurring Alarm
372+END:VALARM
373+END:VTODO
374+END:VCALENDAR

Subscribers

People subscribed via source and target branches