diff options
author | Charles Kerr <charles.kerr@canonical.com> | 2014-09-13 17:14:20 -0500 |
---|---|---|
committer | Charles Kerr <charles.kerr@canonical.com> | 2014-09-13 17:14:20 -0500 |
commit | 4416b0398eff0a7f0462e72b6111aef8cd090b24 (patch) | |
tree | cd67e472c2a12bde928809a7407873799a3190e5 /src | |
parent | 187b660a2a80f4aabf0b87c3fae02cb0841dd2e0 (diff) | |
parent | b20b22f6b7e76f87ccea0bf24bf082a4187c3175 (diff) | |
download | ayatana-indicator-datetime-4416b0398eff0a7f0462e72b6111aef8cd090b24.tar.gz ayatana-indicator-datetime-4416b0398eff0a7f0462e72b6111aef8cd090b24.tar.bz2 ayatana-indicator-datetime-4416b0398eff0a7f0462e72b6111aef8cd090b24.zip |
get the system bus once instead of twice.
Diffstat (limited to 'src')
-rw-r--r-- | src/clock.cpp | 62 |
1 files changed, 45 insertions, 17 deletions
diff --git a/src/clock.cpp b/src/clock.cpp index 6831732..35690dc 100644 --- a/src/clock.cpp +++ b/src/clock.cpp @@ -41,34 +41,61 @@ class Clock::Impl public: Impl(Clock& owner): - m_owner(owner) + m_owner(owner), + m_cancellable(g_cancellable_new()) { - auto tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, - "org.freedesktop.login1", - G_BUS_NAME_WATCHER_FLAGS_NONE, - on_login1_appeared, - on_login1_vanished, - this, nullptr); - m_watched_names.insert(tag); - - tag = g_bus_watch_name(G_BUS_TYPE_SYSTEM, - BUS_POWERD_NAME, - G_BUS_NAME_WATCHER_FLAGS_NONE, - on_powerd_appeared, - on_powerd_vanished, - this, nullptr); - m_watched_names.insert(tag); + g_bus_get(G_BUS_TYPE_SYSTEM, m_cancellable, on_bus_ready, this); } - ~Impl() { + g_cancellable_cancel(m_cancellable); + g_object_unref(m_cancellable); + for(const auto& tag : m_watched_names) g_bus_unwatch_name(tag); } 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) @@ -178,6 +205,7 @@ private: ***/ 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; }; |