From d6b290fda978379fb07285aaddfeb31686735667 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 30 Jul 2014 14:54:37 -0500 Subject: move Snap's guts into an Impl class --- include/datetime/snap.h | 6 +- src/snap.cpp | 185 +++++++++++++++++++++++++++--------------------- 2 files changed, 107 insertions(+), 84 deletions(-) diff --git a/include/datetime/snap.h b/include/datetime/snap.h index 78d9f65..ef5c868 100644 --- a/include/datetime/snap.h +++ b/include/datetime/snap.h @@ -27,7 +27,6 @@ #include #include -#include namespace unity { namespace indicator { @@ -49,9 +48,8 @@ public: appointment_func dismiss); private: - const std::shared_ptr m_engine; - const std::shared_ptr m_settings; - std::set m_notifications; + class Impl; + std::unique_ptr impl; }; } // namespace datetime diff --git a/src/snap.cpp b/src/snap.cpp index e9df256..0eb176c 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -27,6 +27,7 @@ #include #include +#include #include namespace uin = unity::indicator::notifications; @@ -39,114 +40,138 @@ namespace datetime { **** ***/ -namespace // unnamed namespace +class Snap::Impl { +public: -std::string get_alarm_uri(const Appointment& appointment, - const std::shared_ptr& settings) -{ - const char* FALLBACK {"/usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg"}; - - const std::string candidates[] = { appointment.audio_url, - settings->alarm_sound.get(), - FALLBACK }; + Impl(const std::shared_ptr& engine, + const std::shared_ptr& settings): + m_engine(engine), + m_settings(settings) + { + } - std::string uri; + ~Impl() + { + for (const auto& key : m_notifications) + m_engine->close (key); + } - for(const auto& candidate : candidates) + void operator()(const Appointment& appointment, + appointment_func show, + appointment_func dismiss) { - if (gst_uri_is_valid (candidate.c_str())) + if (!appointment.has_alarms) { - uri = candidate; - break; + dismiss(appointment); + return; + } + + // force the system to stay awake + auto awake = std::make_shared(m_engine->app_name()); + + // create the sound... + const auto uri = get_alarm_uri(appointment, m_settings); + const auto volume = m_settings->alarm_volume.get(); + const bool loop = m_engine->supports_actions(); + auto sound = std::make_shared(uri, volume, loop); + + // show a notification... + const auto minutes = std::chrono::minutes(m_settings->alarm_duration.get()); + const bool interactive = m_engine->supports_actions(); + uin::Builder b; + b.set_body (appointment.summary); + b.set_icon_name ("alarm-clock"); + b.add_hint (uin::Builder::HINT_SNAP); + b.add_hint (uin::Builder::HINT_TINT); + const auto timestr = appointment.begin.format (_("%a, %X")); + auto title = g_strdup_printf (_("Alarm %s"), timestr.c_str()); + b.set_title (title); + g_free (title); + b.set_timeout (std::chrono::duration_cast(minutes)); + if (interactive) { + b.add_action ("show", _("Show")); + b.add_action ("dismiss", _("Dismiss")); } - else if (g_file_test(candidate.c_str(), G_FILE_TEST_EXISTS)) + + // add the 'sound' and 'awake' objects to the capture so that + // 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] + (const std::string& action){ + if (action == "show") + show(appointment); + else + dismiss(appointment); + }); + + const auto key = m_engine->show(b); + if (key) + m_notifications.insert (key); + else + show(appointment); + } + +private: + + std::string get_alarm_uri(const Appointment& appointment, + const std::shared_ptr& settings) const + { + const char* FALLBACK {"/usr/share/sounds/ubuntu/ringtones/Suru arpeggio.ogg"}; + + const std::string candidates[] = { appointment.audio_url, + settings->alarm_sound.get(), + FALLBACK }; + + std::string uri; + + for(const auto& candidate : candidates) { - gchar* tmp = gst_filename_to_uri(candidate.c_str(), nullptr); - if (tmp != nullptr) + if (gst_uri_is_valid (candidate.c_str())) { - uri = tmp; - g_free (tmp); + uri = candidate; break; } + else if (g_file_test(candidate.c_str(), G_FILE_TEST_EXISTS)) + { + gchar* tmp = gst_filename_to_uri(candidate.c_str(), nullptr); + if (tmp != nullptr) + { + uri = tmp; + g_free (tmp); + break; + } + } } - } - return uri; -} + return uri; + } -} // unnamed namespace + const std::shared_ptr m_engine; + const std::shared_ptr m_settings; + std::set m_notifications; +}; /*** **** ***/ -Snap::Snap(const std::shared_ptr& engine, +Snap::Snap(const std::shared_ptr& engine, const std::shared_ptr& settings): - m_engine(engine), - m_settings(settings) + impl(new Impl(engine, settings)) { } Snap::~Snap() { - for (const auto& key : m_notifications) - m_engine->close (key); } -void Snap::operator()(const Appointment& appointment, - appointment_func show, - appointment_func dismiss) +void +Snap::operator()(const Appointment& appointment, + appointment_func show, + appointment_func dismiss) { - if (!appointment.has_alarms) - { - dismiss(appointment); - return; - } - - // force the system to stay awake - auto awake = std::make_shared(m_engine->app_name()); - - // create the sound... - const auto uri = get_alarm_uri(appointment, m_settings); - const auto volume = m_settings->alarm_volume.get(); - const bool loop = m_engine->supports_actions(); - auto sound = std::make_shared(uri, volume, loop); - - // show a notification... - const auto minutes = std::chrono::minutes(m_settings->alarm_duration.get()); - const bool interactive = m_engine->supports_actions(); - uin::Builder b; - b.set_body (appointment.summary); - b.set_icon_name ("alarm-clock"); - b.add_hint (uin::Builder::HINT_SNAP); - b.add_hint (uin::Builder::HINT_TINT); - const auto timestr = appointment.begin.format (_("%a, %X")); - auto title = g_strdup_printf (_("Alarm %s"), timestr.c_str()); - b.set_title (title); - g_free (title); - b.set_timeout (std::chrono::duration_cast(minutes)); - if (interactive) { - b.add_action ("show", _("Show")); - b.add_action ("dismiss", _("Dismiss")); - } - - // add the 'sound' and 'awake' objects to the capture so that - // 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] - (const std::string& action){ - if (action == "show") - show(appointment); - else - dismiss(appointment); - }); - - const auto key = m_engine->show(b); - if (key) - m_notifications.insert (key); - else - show(appointment); + (*impl)(appointment, show, dismiss); } /*** -- cgit v1.2.3