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
=== modified file 'src/engine-eds.cpp'
--- src/engine-eds.cpp 2015-05-21 12:47:24 +0000
+++ src/engine-eds.cpp 2015-06-18 04:58:22 +0000
@@ -297,10 +297,14 @@
297 // add the client to our collection297 // add the client to our collection
298 auto self = static_cast<Impl*>(gself);298 auto self = static_cast<Impl*>(gself);
299 g_debug("got a client for %s", e_cal_client_get_local_attachment_store(E_CAL_CLIENT(client)));299 g_debug("got a client for %s", e_cal_client_get_local_attachment_store(E_CAL_CLIENT(client)));
300 self->m_clients[e_client_get_source(client)] = E_CAL_CLIENT(client);300 auto source = e_client_get_source(client);
301 auto ecc = E_CAL_CLIENT(client);
302 self->m_clients[source] = ecc;
303
304 self->ensure_client_alarms_have_triggers(ecc);
301305
302 // now create a view for it so that we can listen for changes306 // now create a view for it so that we can listen for changes
303 e_cal_client_get_view (E_CAL_CLIENT(client),307 e_cal_client_get_view (ecc,
304 "#t", // match all308 "#t", // match all
305 self->m_cancellable,309 self->m_cancellable,
306 on_client_view_ready,310 on_client_view_ready,
@@ -409,6 +413,135 @@
409 static_cast<Impl*>(gself)->set_dirty_soon();413 static_cast<Impl*>(gself)->set_dirty_soon();
410 }414 }
411415
416 /***
417 ****
418 ***/
419
420 // old ubuntu-clock-app alarms created VTODO VALARMS without the
421 // required 'TRIGGER' property... http://pad.lv/1465806
422
423 void ensure_client_alarms_have_triggers(ECalClient* client)
424 {
425 // ask the EDS server for all the ubuntu-clock-app alarms...
426
427 auto sexp = g_strdup_printf("has-categories? '%s'", TAG_ALARM);
428
429 e_cal_client_get_object_list_as_comps(
430 client,
431 sexp,
432 m_cancellable,
433 ensure_client_alarms_have_triggers_async_cb,
434 this);
435
436 g_clear_pointer(&sexp, g_free);
437 }
438
439 static void ensure_client_alarms_have_triggers_async_cb(
440 GObject * oclient,
441 GAsyncResult * res,
442 gpointer gself)
443 {
444 ECalClient * client = E_CAL_CLIENT(oclient);
445 GError * error = nullptr;
446 GSList * components = nullptr;
447
448 if (e_cal_client_get_object_list_as_comps_finish(client,
449 res,
450 &components,
451 &error))
452 {
453 auto self = static_cast<Impl*>(gself);
454 self->ensure_canonical_alarms_have_triggers(client, components);
455 e_cal_client_free_ecalcomp_slist(components);
456 }
457 else if (error != nullptr)
458 {
459 if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
460 g_warning("can't get clock-app alarm list: %s", error->message);
461
462 g_error_free(error);
463 }
464 }
465
466 void ensure_canonical_alarms_have_triggers(ECalClient * client,
467 GSList * components)
468 {
469 GSList * modify_slist = nullptr;
470
471 // for each component..
472 for (auto l=components; l!=nullptr; l=l->next)
473 {
474 bool changed = false;
475
476 // for each alarm...
477 auto component = E_CAL_COMPONENT(l->data);
478 auto auids = e_cal_component_get_alarm_uids(component);
479 for(auto l=auids; l!=nullptr; l=l->next)
480 {
481 auto auid = static_cast<const char*>(l->data);
482 auto alarm = e_cal_component_get_alarm(component, auid);
483 if (alarm == nullptr)
484 continue;
485
486 // if the alarm has no trigger, add one.
487 ECalComponentAlarmTrigger trigger;
488 e_cal_component_alarm_get_trigger(alarm, &trigger);
489 if (trigger.type == E_CAL_COMPONENT_ALARM_TRIGGER_NONE)
490 {
491 trigger.type = E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START;
492 trigger.u.rel_duration = icaldurationtype_from_int(0);
493 e_cal_component_alarm_set_trigger (alarm, trigger);
494 changed = true;
495 }
496
497 g_clear_pointer(&alarm, e_cal_component_alarm_free);
498 }
499 g_clear_pointer(&auids, cal_obj_uid_list_free);
500
501 if (changed)
502 {
503 auto icc = e_cal_component_get_icalcomponent(component); // icc owned by ecc
504 modify_slist = g_slist_prepend(modify_slist, icc);
505 }
506 }
507
508 if (modify_slist != nullptr)
509 {
510 e_cal_client_modify_objects(client,
511 modify_slist,
512 E_CAL_OBJ_MOD_ALL,
513 m_cancellable,
514 ensure_canonical_alarms_have_triggers_async_cb,
515 this);
516
517 g_clear_pointer(&modify_slist, g_slist_free);
518 }
519 }
520
521 // log a warning if e_cal_client_modify_objects() failed
522 static void ensure_canonical_alarms_have_triggers_async_cb(
523 GObject * oclient,
524 GAsyncResult * res,
525 gpointer /*gself*/)
526 {
527 GError * error = nullptr;
528
529 e_cal_client_modify_objects_finish (E_CAL_CLIENT(oclient), res, &error);
530
531 if (error != nullptr)
532 {
533 if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
534 g_warning("couldn't add alarm triggers: %s", error->message);
535
536 g_error_free(error);
537 }
538 }
539
540 /***
541 ****
542 ***/
543
544
412 typedef std::function<void(const std::vector<Appointment>&)> appointment_func;545 typedef std::function<void(const std::vector<Appointment>&)> appointment_func;
413546
414 struct Task547 struct Task
415548
=== modified file 'tests/CMakeLists.txt'
--- tests/CMakeLists.txt 2015-05-21 12:47:24 +0000
+++ tests/CMakeLists.txt 2015-06-18 04:58:22 +0000
@@ -98,6 +98,7 @@
98add_eds_ics_test_by_name(test-eds-ics-repeating-events)98add_eds_ics_test_by_name(test-eds-ics-repeating-events)
99add_eds_ics_test_by_name(test-eds-ics-nonrepeating-events)99add_eds_ics_test_by_name(test-eds-ics-nonrepeating-events)
100add_eds_ics_test_by_name(test-eds-ics-repeating-valarms)100add_eds_ics_test_by_name(test-eds-ics-repeating-valarms)
101add_eds_ics_test_by_name(test-eds-ics-missing-trigger)
101102
102103
103# disabling the timezone unit tests because they require104# disabling the timezone unit tests because they require
104105
=== modified file 'tests/print-to.h'
--- tests/print-to.h 2015-05-21 12:47:24 +0000
+++ tests/print-to.h 2015-06-18 04:58:22 +0000
@@ -47,6 +47,30 @@
47 *os << '}';47 *os << '}';
48}48}
4949
50void
51PrintTo(const Appointment& appointment, std::ostream* os)
52{
53 *os << '{';
54
55 *os << "{uid:'" << appointment.uid << "'}"
56 << "{color:'" << appointment.color << "'}"
57 << "{summary:'" << appointment.summary << "'}"
58 << "{activation_url:'" << appointment.activation_url << "'}";
59
60 *os << "{begin:";
61 PrintTo(appointment.begin, os);
62 *os << '}';
63
64 *os << "{end:";
65 PrintTo(appointment.end, os);
66 *os << '}';
67
68 for(const auto& alarm : appointment.alarms)
69 PrintTo(alarm, os);
70
71 *os << '}';
72}
73
50} // namespace datetime74} // namespace datetime
51} // namespace indicator75} // namespace indicator
52} // namespace unity76} // namespace unity
5377
=== added file 'tests/test-eds-ics-missing-trigger.cpp'
--- tests/test-eds-ics-missing-trigger.cpp 1970-01-01 00:00:00 +0000
+++ tests/test-eds-ics-missing-trigger.cpp 2015-06-18 04:58:22 +0000
@@ -0,0 +1,116 @@
1/*
2 * Copyright 2015 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3, as published
6 * by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranties of
10 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 * PURPOSE. See the GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Charles Kerr <charles.kerr@canonical.com>
18 */
19
20#include <algorithm>
21
22#include <datetime/alarm-queue-simple.h>
23#include <datetime/clock-mock.h>
24#include <datetime/engine-eds.h>
25#include <datetime/planner-range.h>
26
27#include <gtest/gtest.h>
28
29#include "glib-fixture.h"
30#include "print-to.h"
31#include "timezone-mock.h"
32#include "wakeup-timer-mock.h"
33
34using namespace unity::indicator::datetime;
35using VAlarmFixture = GlibFixture;
36
37/***
38****
39***/
40
41TEST_F(VAlarmFixture, MissingTriggers)
42{
43 // start the EDS engine
44 auto engine = std::make_shared<EdsEngine>();
45
46 // we need a consistent timezone for the planner and our local DateTimes
47 constexpr char const * zone_str {"America/Chicago"};
48 auto tz = std::make_shared<MockTimezone>(zone_str);
49 auto gtz = g_time_zone_new(zone_str);
50
51 // make a planner that looks at the first half of 2015 in EDS
52 auto planner = std::make_shared<SimpleRangePlanner>(engine, tz);
53 const DateTime range_begin {gtz, 2015,1, 1, 0, 0, 0.0};
54 const DateTime range_end {gtz, 2015,6,31,23,59,59.5};
55 planner->range().set(std::make_pair(range_begin, range_end));
56
57 // give EDS a moment to load
58 if (planner->appointments().get().empty()) {
59 g_message("waiting a moment for EDS to load...");
60 auto on_appointments_changed = [this](const std::vector<Appointment>& appointments){
61 g_message("ah, they loaded");
62 if (!appointments.empty())
63 g_main_loop_quit(loop);
64 };
65 core::ScopedConnection conn(planner->appointments().changed().connect(on_appointments_changed));
66 constexpr int max_wait_sec = 10;
67 wait_msec(max_wait_sec * G_TIME_SPAN_MILLISECOND);
68 }
69
70 // build expected: one-time alarm
71 std::vector<Appointment> expected;
72 Appointment a;
73 a.type = Appointment::UBUNTU_ALARM;
74 a.uid = "20150617T211838Z-6217-32011-2036-1@ubuntu-phablet";
75 a.color = "#becedd";
76 a.summary = "One Time Alarm";
77 a.begin = DateTime { gtz, 2015, 6, 18, 10, 0, 0};
78 a.end = a.begin;
79 a.alarms.resize(1);
80 a.alarms[0].audio_url = "file:///usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg";
81 a.alarms[0].time = a.begin;
82 a.alarms[0].text = a.summary;
83 expected.push_back(a);
84
85 // build expected: recurring alarm
86 a.uid = "20150617T211913Z-6217-32011-2036-5@ubuntu-phablet";
87 a.summary = "Recurring Alarm";
88 a.alarms[0].text = a.summary;
89 std::array<DateTime,14> recurrences {
90 DateTime{ gtz, 2015, 6, 18, 10, 1, 0 },
91 DateTime{ gtz, 2015, 6, 19, 10, 1, 0 },
92 DateTime{ gtz, 2015, 6, 20, 10, 1, 0 },
93 DateTime{ gtz, 2015, 6, 21, 10, 1, 0 },
94 DateTime{ gtz, 2015, 6, 22, 10, 1, 0 },
95 DateTime{ gtz, 2015, 6, 23, 10, 1, 0 },
96 DateTime{ gtz, 2015, 6, 24, 10, 1, 0 },
97 DateTime{ gtz, 2015, 6, 25, 10, 1, 0 },
98 DateTime{ gtz, 2015, 6, 26, 10, 1, 0 },
99 DateTime{ gtz, 2015, 6, 27, 10, 1, 0 },
100 DateTime{ gtz, 2015, 6, 28, 10, 1, 0 },
101 DateTime{ gtz, 2015, 6, 29, 10, 1, 0 },
102 DateTime{ gtz, 2015, 6, 30, 10, 1, 0 },
103 DateTime{ gtz, 2015, 7, 1, 10, 1, 0 }
104 };
105 for (const auto& time : recurrences) {
106 a.begin = a.end = a.alarms[0].time = time;
107 expected.push_back(a);
108 }
109
110 // the planner should match what we've got in the calendar.ics file
111 const auto appts = planner->appointments().get();
112 EXPECT_EQ(expected, appts);
113
114 // cleanup
115 g_time_zone_unref(gtz);
116}
0117
=== added file 'tests/test-eds-ics-missing-trigger.ics'
--- tests/test-eds-ics-missing-trigger.ics 1970-01-01 00:00:00 +0000
+++ tests/test-eds-ics-missing-trigger.ics 2015-06-18 04:58:22 +0000
@@ -0,0 +1,45 @@
1BEGIN:VCALENDAR
2CALSCALE:GREGORIAN
3PRODID:-//Ximian//NONSGML Evolution Calendar//EN
4VERSION:2.0
5X-EVOLUTION-DATA-REVISION:2015-06-17T21:19:13.980613Z(3)
6BEGIN:VTODO
7UID:20150617T211838Z-6217-32011-2036-1@ubuntu-phablet
8DTSTAMP:20150617T211838Z
9DTSTART:20150618T100000
10SUMMARY:One Time Alarm
11CATEGORIES:x-canonical-alarm
12CREATED:20150617T211838Z
13LAST-MODIFIED:20150617T211838Z
14BEGIN:VALARM
15X-EVOLUTION-ALARM-UID:20150617T211838Z-6217-32011-2036-2@ubuntu-phablet
16ACTION:AUDIO
17ATTACH:file:///usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg
18END:VALARM
19BEGIN:VALARM
20X-EVOLUTION-ALARM-UID:20150617T211838Z-6217-32011-2036-3@ubuntu-phablet
21ACTION:DISPLAY
22DESCRIPTION:One Time Alarm
23END:VALARM
24END:VTODO
25BEGIN:VTODO
26UID:20150617T211913Z-6217-32011-2036-5@ubuntu-phablet
27DTSTAMP:20150617T211913Z
28DTSTART:20150618T100100
29RRULE:FREQ=DAILY
30SUMMARY:Recurring Alarm
31CATEGORIES:x-canonical-alarm
32CREATED:20150617T211913Z
33LAST-MODIFIED:20150617T211913Z
34BEGIN:VALARM
35X-EVOLUTION-ALARM-UID:20150617T211913Z-6217-32011-2036-6@ubuntu-phablet
36ACTION:AUDIO
37ATTACH:file:///usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg
38END:VALARM
39BEGIN:VALARM
40X-EVOLUTION-ALARM-UID:20150617T211913Z-6217-32011-2036-7@ubuntu-phablet
41ACTION:DISPLAY
42DESCRIPTION:Recurring Alarm
43END:VALARM
44END:VTODO
45END:VCALENDAR

Subscribers

People subscribed via source and target branches