diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clock.cpp | 221 | ||||
-rw-r--r-- | src/snap.cpp | 11 |
2 files changed, 190 insertions, 42 deletions
diff --git a/src/clock.cpp b/src/clock.cpp index f41a0cc..a04e074 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -18,10 +18,16 @@ */ #include <datetime/clock.h> +#include <datetime/dbus-shared.h> #include <glib.h> #include <gio/gio.h> +#include <map> +#include <set> +#include <string> +#include <vector> + namespace unity { namespace indicator { namespace datetime { @@ -30,58 +36,191 @@ namespace datetime { **** ***/ -Clock::Clock(): - m_cancellable(g_cancellable_new()) +class Clock::Impl { - g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_system_bus_ready, this); -} +public: -Clock::~Clock() -{ - g_cancellable_cancel(m_cancellable); - g_clear_object(&m_cancellable); + Impl(Clock& owner): + m_owner(owner), + m_cancellable(g_cancellable_new()) + { + g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_bus_ready, this); + } - if (m_sleep_subscription_id) - g_dbus_connection_signal_unsubscribe(m_system_bus, m_sleep_subscription_id); + ~Impl() + { + g_cancellable_cancel(m_cancellable); + g_object_unref(m_cancellable); - g_clear_object(&m_system_bus); -} + for(const auto& tag : m_watched_names) + g_bus_unwatch_name(tag); + } -void -Clock::on_system_bus_ready(GObject*, GAsyncResult * res, gpointer gself) -{ - GDBusConnection * system_bus; +private: + + static void on_bus_ready(GObject * /*source_object*/, + GAsyncResult * res, + gpointer gself) + { + GError * error = NULL; + GDBusConnection * bus; + + if ((bus = g_bus_get_finish(res, &error))) + { + auto self = static_cast<Impl*>(gself); + + auto tag = g_bus_watch_name_on_connection(bus, + "org.freedesktop.login1", + G_BUS_NAME_WATCHER_FLAGS_NONE, + on_login1_appeared, + on_login1_vanished, + gself, nullptr); + self->m_watched_names.insert(tag); + + tag = g_bus_watch_name_on_connection(bus, + BUS_POWERD_NAME, + G_BUS_NAME_WATCHER_FLAGS_NONE, + on_powerd_appeared, + on_powerd_vanished, + gself, nullptr); + self->m_watched_names.insert(tag); + + g_object_unref(bus); + } + else if (error != nullptr) + { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("%s Couldn't get system bus: %s", G_STRLOC, error->message); + + g_error_free(error); + } + } + + void remember_subscription(const std::string & name, + GDBusConnection * bus, + guint tag) + { + g_object_ref(bus); + + auto deleter = [tag](GDBusConnection* bus){ + g_dbus_connection_signal_unsubscribe(bus, tag); + g_object_unref(G_OBJECT(bus)); + }; + + m_subscriptions[name].push_back(std::shared_ptr<GDBusConnection>(bus, deleter)); + } + + /** + *** DBus Chatter: org.freedesktop.login1 + *** + *** Fire Clock::minute_changed() signal on login1's PrepareForSleep signal + **/ + + static void on_login1_appeared(GDBusConnection * bus, + const gchar * name, + const gchar * name_owner, + gpointer gself) + { + auto tag = g_dbus_connection_signal_subscribe(bus, + name_owner, + "org.freedesktop.login1.Manager", // interface + "PrepareForSleep", // signal name + "/org/freedesktop/login1", // object path + nullptr, // arg0 + G_DBUS_SIGNAL_FLAGS_NONE, + on_prepare_for_sleep, + gself, + nullptr); - if ((system_bus = g_bus_get_finish(res, nullptr))) + static_cast<Impl*>(gself)->remember_subscription(name, bus, tag); + } + + static void on_login1_vanished(GDBusConnection * /*system_bus*/, + const gchar * name, + gpointer gself) + { + static_cast<Impl*>(gself)->m_subscriptions[name].clear(); + } + + static void on_prepare_for_sleep(GDBusConnection* /*connection*/, + const gchar* /*sender_name*/, + const gchar* /*object_path*/, + const gchar* /*interface_name*/, + const gchar* /*signal_name*/, + GVariant* /*parameters*/, + gpointer gself) + { + g_debug("firing clock.minute_changed() due to PrepareForSleep"); + static_cast<Impl*>(gself)->m_owner.minute_changed(); + } + + /** + *** DBus Chatter: com.canonical.powerd + *** + *** Fire Clock::minute_changed() signal when powerd says the system's + *** has awoken from sleep -- the old timestamp is likely out-of-date + **/ + + static void on_powerd_appeared(GDBusConnection * bus, + const gchar * name, + const gchar * name_owner, + gpointer gself) + { + auto tag = g_dbus_connection_signal_subscribe(bus, + name_owner, + BUS_POWERD_INTERFACE, + "SysPowerStateChange", + BUS_POWERD_PATH, + nullptr, // arg0 + G_DBUS_SIGNAL_FLAGS_NONE, + on_sys_power_state_change, + gself, // user_data + nullptr); // user_data closure + + + static_cast<Impl*>(gself)->remember_subscription(name, bus, tag); + } + + static void on_powerd_vanished(GDBusConnection * /*bus*/, + const gchar * name, + gpointer gself) { - auto self = static_cast<Clock*>(gself); - - self->m_system_bus = system_bus; - - self->m_sleep_subscription_id = g_dbus_connection_signal_subscribe( - system_bus, - nullptr, - "org.freedesktop.login1.Manager", // interface - "PrepareForSleep", // signal name - "/org/freedesktop/login1", // object path - nullptr, // arg0 - G_DBUS_SIGNAL_FLAGS_NONE, - on_prepare_for_sleep, - self, - nullptr); + static_cast<Impl*>(gself)->m_subscriptions[name].clear(); } + + static void on_sys_power_state_change(GDBusConnection* /*connection*/, + const gchar* /*sender_name*/, + const gchar* /*object_path*/, + const gchar* /*interface_name*/, + const gchar* /*signal_name*/, + GVariant* /*parameters*/, + gpointer gself) + { + g_debug("firing clock.minute_changed() due to state change"); + static_cast<Impl*>(gself)->m_owner.minute_changed(); + } + + /*** + **** + ***/ + + Clock& m_owner; + GCancellable * m_cancellable = nullptr; + std::set<guint> m_watched_names; + std::map<std::string,std::vector<std::shared_ptr<GDBusConnection>>> m_subscriptions; +}; + +/*** +**** +***/ + +Clock::Clock(): + m_impl(new Impl{*this}) +{ } -void -Clock::on_prepare_for_sleep(GDBusConnection* /*connection*/, - const gchar* /*sender_name*/, - const gchar* /*object_path*/, - const gchar* /*interface_name*/, - const gchar* /*signal_name*/, - GVariant* /*parameters*/, - gpointer gself) +Clock::~Clock() { - static_cast<Clock*>(gself)->minute_changed(); } /*** diff --git a/src/snap.cpp b/src/snap.cpp index b3368a1..c2cbc0a 100644 --- a/src/snap.cpp +++ b/src/snap.cpp @@ -94,7 +94,16 @@ public: b.add_hint (uin::Builder::HINT_TINT); b.add_hint (uin::Builder::HINT_NONSHAPEDICON); - const auto timefmt = is_locale_12h() ? _("%a, %l:%M %p") : _("%a, %H:%M"); + const char * timefmt; + if (is_locale_12h()) { + /** strftime(3) format for abbreviated weekday, + hours, minutes in a 12h locale; e.g. Wed, 2:00 PM */ + timefmt = _("%a, %l:%M %p"); + } else { + /** A strftime(3) format for abbreviated weekday, + hours, minutes in a 24h locale; e.g. Wed, 14:00 */ + timefmt = _("%a, %H:%M"); + } const auto timestr = appointment.begin.format(timefmt); auto title = g_strdup_printf(_("Alarm %s"), timestr.c_str()); b.set_title (title); |