From 15a383a7b027d281dcdaa85a43eb3563031366bf Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 10:35:27 -0500 Subject: add SnoozePlanner, AggregatePlanner --- src/planner-aggregate.cpp | 106 ++++++++++++++++++++++++++++++++++++++++++ src/planner-snooze.cpp | 115 ++++++++++++++++++++++++++++++++++++++++++++++ src/planner.cpp | 88 +++++++++++++++++++++++++++++++++++ 3 files changed, 309 insertions(+) create mode 100644 src/planner-aggregate.cpp create mode 100644 src/planner-snooze.cpp create mode 100644 src/planner.cpp (limited to 'src') diff --git a/src/planner-aggregate.cpp b/src/planner-aggregate.cpp new file mode 100644 index 0000000..7458f0c --- /dev/null +++ b/src/planner-aggregate.cpp @@ -0,0 +1,106 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include + +namespace unity { +namespace indicator { +namespace datetime { + +/*** +**** +***/ + +class AggregatePlanner::Impl +{ +public: + Impl(AggregatePlanner* owner): + m_owner(owner) + { + } + + ~Impl() =default; + + core::Property>& appointments() + { + return m_appointments; + } + + void add(const std::shared_ptr& planner) + { + m_planners.push_back(planner); + + auto on_changed = [this](const std::vector&){rebuild();}; + auto connection = planner->appointments().changed().connect(on_changed); + m_connections.push_back(connection); + } + +private: + + void rebuild() + { + // use a sorted aggregate vector of all our planners + std::vector all; + for (const auto& planner : m_planners) { + const auto& walk = planner->appointments().get(); + all.insert(std::end(all), std::begin(walk), std::end(walk)); + } + m_owner->sort(all); + m_appointments.set(all); + } + + const AggregatePlanner* m_owner = nullptr; + core::Property> m_appointments; + std::vector> m_planners; + std::vector m_connections; +}; + +/*** +**** +***/ + +AggregatePlanner::AggregatePlanner(): + impl(new Impl{this}) +{ +} + +AggregatePlanner::~AggregatePlanner() +{ +} + +core::Property>& +AggregatePlanner::appointments() +{ + return impl->appointments(); +} + +void +AggregatePlanner::add(const std::shared_ptr& planner) +{ + return impl->add(planner); +} + +/*** +**** +***/ + +} // namespace datetime +} // namespace indicator +} // namespace unity + diff --git a/src/planner-snooze.cpp b/src/planner-snooze.cpp new file mode 100644 index 0000000..6d427ca --- /dev/null +++ b/src/planner-snooze.cpp @@ -0,0 +1,115 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include + +#include // e_uid_new() + +namespace unity { +namespace indicator { +namespace datetime { + +/*** +**** +***/ + +class SnoozePlanner::Impl +{ +public: + + Impl(SnoozePlanner* owner, + const std::shared_ptr& settings, + const std::shared_ptr& clock): + m_owner(owner), + m_settings(settings), + m_clock(clock) + { + } + + ~Impl() + { + } + + virtual core::Property>& appointments() + { + return m_appointments; + } + + void add(const Appointment& appt_in) + { + Appointment appt = appt_in; + + // adjust the time + const auto appt_length_secs = appt.end - appt.begin; + appt.begin = m_clock->localtime().add_full(0,0,0,0,m_settings->snooze_duration.get(),0); + appt.end = appt.begin.add_full(0,0,0,0,0,appt_length_secs); + + // give it a new ID + gchar* uid = e_uid_new(); + appt.uid = uid; + g_free(uid); + + // add it to our appointment list + auto tmp = appointments().get(); + tmp.push_back(appt); + m_owner->sort(tmp); + m_appointments.set(tmp); + } + +private: + + const SnoozePlanner* const m_owner; + const std::shared_ptr m_settings; + const std::shared_ptr m_clock; + core::Property> m_appointments; +}; + +/*** +**** +***/ + +SnoozePlanner::SnoozePlanner(const std::shared_ptr& settings, + const std::shared_ptr& clock): + impl(new Impl{this, settings, clock}) +{ +} + +SnoozePlanner::~SnoozePlanner() +{ +} + +void +SnoozePlanner::add(const Appointment& appointment) +{ + impl->add(appointment); +} + +core::Property>& +SnoozePlanner::appointments() +{ + return impl->appointments(); +} + +/*** +**** +***/ + +} // namespace datetime +} // namespace indicator +} // namespace unity diff --git a/src/planner.cpp b/src/planner.cpp new file mode 100644 index 0000000..3f73030 --- /dev/null +++ b/src/planner.cpp @@ -0,0 +1,88 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * Authors: + * Charles Kerr + */ + +#include + +namespace unity { +namespace indicator { +namespace datetime { + +/*** +**** +***/ + +Planner::Planner() +{ +} + +Planner::~Planner() +{ +} + +void +Planner::set_range_to_calendar_month(const DateTime& dt) +{ + // use appointments that occur in dt's calendar month + impl->set_range_to_upcoming_month(dt.add_full(0, // no years + 0, // no months + -(dt.day_of_month()-1), + -dt.hour(), + -dt.minute(), + -dt.seconds()); +} + +void +Planner::set_range_to_upcoming_month(const DateTime& begin) +{ + // use appointments that occur in [dt...dt+1month] + const auto end = begin.add_full(0, 1, 0, 0, 0, -0.1); + const char * fmt = "%F %T"; + g_debug("RangePlanner %p setting range [%s..%s]", + this, + begin.format(fmt).c_str(), + end.format(fmt).c_str()); + range().set(std::make_pair(begin,end)); +} + +void +Planner::sort(std::vector& appts) +{ + std::sort(std::begin(appts), + std::end(appts), + [](const Appointment& a, const Appointment& b){return a.begin < b.begin;}); +} + +void +Planner::trim(std::vector& appts, + const DateTime& begin, + const DateTime& end) +{ + decltype(appts) tmp; + auto predicate = [begin,end](const Appointment& a){return begin<=a.begin && a.begin<=end;} + std::copy(std::begin(appts), std::end(appts), std::back_inserter(tmp), predicate); + appts.swap(tmp); +} + +/*** +**** +***/ + +} // namespace datetime +} // namespace indicator +} // namespace unity -- cgit v1.2.3 From d3732ff1a0d149544f89d08fa489e4f0fca8da07 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 10:37:03 -0500 Subject: add snooze properties to our schema; export it on the bus; add tests --- src/CMakeLists.txt | 2 ++ src/exporter.cpp | 1 + src/settings-live.cpp | 12 ++++++++++++ 3 files changed, 15 insertions(+) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e583334..2852a75 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,8 @@ set (SERVICE_CXX_SOURCES locations-settings.cpp menu.cpp notifications.cpp + planner-aggregate.cpp + planner-snooze.cpp planner-month.cpp planner-range.cpp planner-upcoming.cpp diff --git a/src/exporter.cpp b/src/exporter.cpp index 1d45705..05b21eb 100644 --- a/src/exporter.cpp +++ b/src/exporter.cpp @@ -145,6 +145,7 @@ private: bind_uint_property(m_alarm_props, "default-volume", m_settings->alarm_volume); bind_string_property(m_alarm_props, "default-sound", m_settings->alarm_sound); bind_string_property(m_alarm_props, "haptic-feedback", m_settings->alarm_haptic); + bind_uint_property(m_alarm_props, "snooze-duration", m_settings->snooze_duration); } /*** diff --git a/src/settings-live.cpp b/src/settings-live.cpp index a8338ed..8ea06a4 100644 --- a/src/settings-live.cpp +++ b/src/settings-live.cpp @@ -56,6 +56,7 @@ LiveSettings::LiveSettings(): update_alarm_volume(); update_alarm_duration(); update_alarm_haptic(); + update_snooze_duration(); // now listen for clients to change the properties s.t. we can sync update GSettings @@ -135,6 +136,10 @@ LiveSettings::LiveSettings(): alarm_haptic.changed().connect([this](const std::string& value){ g_settings_set_string(m_settings, SETTINGS_ALARM_HAPTIC_S, value.c_str()); }); + + snooze_duration.changed().connect([this](unsigned int value){ + g_settings_set_uint(m_settings, SETTINGS_SNOOZE_DURATION_S, value); + }); } /*** @@ -249,6 +254,11 @@ void LiveSettings::update_alarm_haptic() g_free(val); } +void LiveSettings::update_snooze_duration() +{ + snooze_duration.set(g_settings_get_uint(m_settings, SETTINGS_SNOOZE_DURATION_S)); +} + /*** **** ***/ @@ -298,6 +308,8 @@ void LiveSettings::update_key(const std::string& key) update_alarm_duration(); else if (key == SETTINGS_ALARM_HAPTIC_S) update_alarm_haptic(); + else if (key == SETTINGS_SNOOZE_DURATION_S) + update_snooze_duration(); } /*** -- cgit v1.2.3 From 63493709a92c76169ad065cc3804b13dbd786537 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 11:15:58 -0500 Subject: add planner.cpp to the build --- src/CMakeLists.txt | 1 + src/planner.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2852a75..512cc5c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -25,6 +25,7 @@ set (SERVICE_CXX_SOURCES locations-settings.cpp menu.cpp notifications.cpp + planner.cpp planner-aggregate.cpp planner-snooze.cpp planner-month.cpp diff --git a/src/planner.cpp b/src/planner.cpp index 3f73030..15dab52 100644 --- a/src/planner.cpp +++ b/src/planner.cpp @@ -19,6 +19,8 @@ #include +#include + namespace unity { namespace indicator { namespace datetime { @@ -35,6 +37,7 @@ Planner::~Planner() { } +#if 0 void Planner::set_range_to_calendar_month(const DateTime& dt) { @@ -59,6 +62,7 @@ Planner::set_range_to_upcoming_month(const DateTime& begin) end.format(fmt).c_str()); range().set(std::make_pair(begin,end)); } +#endif void Planner::sort(std::vector& appts) @@ -73,9 +77,9 @@ Planner::trim(std::vector& appts, const DateTime& begin, const DateTime& end) { - decltype(appts) tmp; - auto predicate = [begin,end](const Appointment& a){return begin<=a.begin && a.begin<=end;} - std::copy(std::begin(appts), std::end(appts), std::back_inserter(tmp), predicate); + std::vector tmp; + auto predicate = [begin,end](const Appointment& a){return begin<=a.begin && a.begin<=end;}; + std::copy_if(std::begin(appts), std::end(appts), std::back_inserter(tmp), predicate); appts.swap(tmp); } -- cgit v1.2.3 From 000f91caf98706d21868e0b41e42388ef68fc9e5 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 11:16:01 -0500 Subject: update Snap Decisions to include snooze --- src/main.cpp | 32 ++++++++++++++++---------------- src/snap.cpp | 24 +++++++++++------------- 2 files changed, 27 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 48d3d20..54517c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include #include #include @@ -37,8 +39,6 @@ #include // bindtextdomain() #include -#include - #include #include // exit() @@ -90,6 +90,7 @@ namespace } std::shared_ptr create_simple_alarm_queue(const std::shared_ptr& clock, + const std::shared_ptr& snooze_planner, const std::shared_ptr& engine, const std::shared_ptr& tz) { @@ -102,8 +103,14 @@ namespace upcoming_planner->date().set(now); }); + // create an aggregate planner that folds together the above + // upcoming-events planner and locally-generated snooze events + std::shared_ptr planner = std::make_shared(); + planner->add(upcoming_planner); + planner->add(snooze_planner); + auto wakeup_timer = std::make_shared(clock); - return std::make_shared(clock, upcoming_planner, wakeup_timer); + return std::make_shared(clock, planner, wakeup_timer); } } @@ -126,21 +133,14 @@ main(int /*argc*/, char** /*argv*/) MenuFactory factory(actions, state); // set up the snap decisions + auto snooze_planner = std::make_shared(state->settings, state->clock); auto notification_engine = std::make_shared("indicator-datetime-service"); std::unique_ptr snap (new Snap(notification_engine, state->settings)); - auto alarm_queue = create_simple_alarm_queue(state->clock, engine, timezone); - alarm_queue->alarm_reached().connect([&snap](const Appointment& appt){ - auto snap_show = [](const Appointment& a){ - const char* url; - if(!a.url.empty()) - url = a.url.c_str(); - else // alarm doesn't have a URl associated with it; use a fallback - url = "appid://com.ubuntu.clock/clock/current-user-version"; - url_dispatch_send(url, nullptr, nullptr); - }; - auto snap_dismiss = [](const Appointment&){}; - (*snap)(appt, snap_show, snap_dismiss); - }); + auto alarm_queue = create_simple_alarm_queue(state->clock, snooze_planner, engine, timezone); + auto on_snooze = [snooze_planner](const Appointment& a) {snooze_planner->add(a);}; + auto on_ok = [](const Appointment&){}; + auto on_alarm_reached = [&snap, &on_snooze, &on_ok](const Appointment& a) {(*snap)(a, on_snooze, on_ok);}; + alarm_queue->alarm_reached().connect(on_alarm_reached); // create the menus std::vector> menus; diff --git a/src/snap.cpp b/src/snap.cpp index 0b2322a..2a0fb5b 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -59,12 +59,12 @@ public: } void operator()(const Appointment& appointment, - appointment_func show, - appointment_func dismiss) + appointment_func snooze, + appointment_func ok) { if (!appointment.has_alarms) { - dismiss(appointment); + ok(appointment); return; } @@ -98,26 +98,24 @@ public: g_free (title); b.set_timeout (std::chrono::duration_cast(minutes)); if (interactive) { - b.add_action ("show", _("Show")); - b.add_action ("dismiss", _("Dismiss")); + b.add_action ("snooze", _("Snooze")); + b.add_action ("ok", _("OK")); } // add 'sound', 'haptic', and 'awake' objects to the capture so // they stay alive until the closed callback is called; i.e., // for the lifespan of the notficiation - b.set_closed_callback([appointment, show, dismiss, sound, awake, haptic] + b.set_closed_callback([appointment, snooze, ok, sound, awake, haptic] (const std::string& action){ - if (action == "show") - show(appointment); + if (action == "snooze") + snooze(appointment); else - dismiss(appointment); + ok(appointment); }); const auto key = m_engine->show(b); if (key) m_notifications.insert (key); - else - show(appointment); } private: @@ -177,9 +175,9 @@ Snap::~Snap() void Snap::operator()(const Appointment& appointment, appointment_func show, - appointment_func dismiss) + appointment_func ok) { - (*impl)(appointment, show, dismiss); + (*impl)(appointment, show, ok); } /*** -- cgit v1.2.3 From b0b9b94246fad179165cca2f4d5026409349fde0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 23:19:58 -0500 Subject: reverse OK/snooze button order --- src/snap.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/snap.cpp b/src/snap.cpp index 2a0fb5b..505980c 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -98,8 +98,8 @@ public: g_free (title); b.set_timeout (std::chrono::duration_cast(minutes)); if (interactive) { - b.add_action ("snooze", _("Snooze")); b.add_action ("ok", _("OK")); + b.add_action ("snooze", _("Snooze")); } // add 'sound', 'haptic', and 'awake' objects to the capture so -- cgit v1.2.3 From db764f321a1450da886c672b3ccd82d1e5dfedd6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 23:37:35 -0500 Subject: remove the new code that we're not using yet --- src/planner.cpp | 38 -------------------------------------- 1 file changed, 38 deletions(-) (limited to 'src') diff --git a/src/planner.cpp b/src/planner.cpp index 15dab52..6ca9ca7 100644 --- a/src/planner.cpp +++ b/src/planner.cpp @@ -37,33 +37,6 @@ Planner::~Planner() { } -#if 0 -void -Planner::set_range_to_calendar_month(const DateTime& dt) -{ - // use appointments that occur in dt's calendar month - impl->set_range_to_upcoming_month(dt.add_full(0, // no years - 0, // no months - -(dt.day_of_month()-1), - -dt.hour(), - -dt.minute(), - -dt.seconds()); -} - -void -Planner::set_range_to_upcoming_month(const DateTime& begin) -{ - // use appointments that occur in [dt...dt+1month] - const auto end = begin.add_full(0, 1, 0, 0, 0, -0.1); - const char * fmt = "%F %T"; - g_debug("RangePlanner %p setting range [%s..%s]", - this, - begin.format(fmt).c_str(), - end.format(fmt).c_str()); - range().set(std::make_pair(begin,end)); -} -#endif - void Planner::sort(std::vector& appts) { @@ -72,17 +45,6 @@ Planner::sort(std::vector& appts) [](const Appointment& a, const Appointment& b){return a.begin < b.begin;}); } -void -Planner::trim(std::vector& appts, - const DateTime& begin, - const DateTime& end) -{ - std::vector tmp; - auto predicate = [begin,end](const Appointment& a){return begin<=a.begin && a.begin<=end;}; - std::copy_if(std::begin(appts), std::end(appts), std::back_inserter(tmp), predicate); - appts.swap(tmp); -} - /*** **** ***/ -- cgit v1.2.3 From 593c16de05fe3806edeeb3bc105ce63b71b8b545 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 2 Sep 2014 23:44:00 -0500 Subject: minor copyediting: rename a variable for better clarity --- src/planner-snooze.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/planner-snooze.cpp b/src/planner-snooze.cpp index 6d427ca..51ad0d2 100644 --- a/src/planner-snooze.cpp +++ b/src/planner-snooze.cpp @@ -55,10 +55,10 @@ public: { Appointment appt = appt_in; - // adjust the time - const auto appt_length_secs = appt.end - appt.begin; + // reschedule the alarm to go off N minutes from now + const auto alarm_duration_secs = appt.end - appt.begin; appt.begin = m_clock->localtime().add_full(0,0,0,0,m_settings->snooze_duration.get(),0); - appt.end = appt.begin.add_full(0,0,0,0,0,appt_length_secs); + appt.end = appt.begin.add_full(0,0,0,0,0,alarm_duration_secs); // give it a new ID gchar* uid = e_uid_new(); -- cgit v1.2.3