From 53721ecc9f3d3d2c0b65b06d313d57e266ba31ad Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 8 Apr 2016 15:46:50 -0500 Subject: pull the timezone from timedate1 regardless of whether it appears on the bus before or after we startup --- src/timezone-timedated.cpp | 249 ++++++++++++++++++++++++--------------------- 1 file changed, 135 insertions(+), 114 deletions(-) (limited to 'src/timezone-timedated.cpp') diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp index d38557b..9254612 100644 --- a/src/timezone-timedated.cpp +++ b/src/timezone-timedated.cpp @@ -17,6 +17,7 @@ * Charles Kerr */ +#include #include #include @@ -36,166 +37,186 @@ class TimedatedTimezone::Impl { public: - Impl(TimedatedTimezone& owner, std::string filename): - m_owner(owner), - m_filename(filename) + Impl(TimedatedTimezone& owner): + m_owner{owner}, + m_cancellable{g_cancellable_new()} { - g_debug("Filename is '%s'", filename.c_str()); - monitor_timezone_property(); + // set the fallback value + m_owner.timezone.set("Etc/Utc"); + + // watch for timedate1 on the bus + m_watcher_id = g_bus_watch_name( + G_BUS_TYPE_SYSTEM, + Bus::Timedate1::BUSNAME, + G_BUS_NAME_WATCHER_FLAGS_AUTO_START, + on_timedate1_appeared, + on_timedate1_vanished, + this, + nullptr); } ~Impl() { - clear(); - } + g_cancellable_cancel(m_cancellable); + g_clear_object(&m_cancellable); -private: + g_bus_unwatch_name(m_watcher_id); - void clear() - { - if (m_connection && m_signal_subscription_id) + if (m_connection != nullptr) { - g_dbus_connection_signal_unsubscribe (m_connection, m_signal_subscription_id); - m_signal_subscription_id = 0; - } + if (m_signal_subscription_id) + g_dbus_connection_signal_unsubscribe(m_connection, m_signal_subscription_id); - g_clear_object(&m_connection); + g_clear_object(&m_connection); + } } - static void on_properties_changed (GDBusConnection *connection G_GNUC_UNUSED, - const gchar *sender_name G_GNUC_UNUSED, - const gchar *object_path G_GNUC_UNUSED, - const gchar *interface_name G_GNUC_UNUSED, - const gchar *signal_name G_GNUC_UNUSED, - GVariant *parameters, - gpointer gself) - { - auto self = static_cast(gself); - const char *tz; - GVariant *changed_properties; - gchar **invalidated_properties; - - g_variant_get (parameters, "(s@a{sv}^as)", NULL, &changed_properties, &invalidated_properties); +private: - if (g_variant_lookup(changed_properties, "Timezone", "&s", &tz, NULL)) - self->notify_timezone(tz); - else if (g_strv_contains (invalidated_properties, "Timezone")) - self->notify_timezone(self->get_timezone_from_file(self->m_filename)); + static void on_timedate1_appeared(GDBusConnection * connection, + const gchar * /*name*/, + const gchar * /*name_owner*/, + gpointer gself) + { + static_cast(gself)->timedate1_appeared(connection); + } + void timedate1_appeared(GDBusConnection* connection) + { + // cache m_connection for later... + g_clear_object(&m_connection); + m_connection = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(connection))); - g_variant_unref (changed_properties); - g_strfreev (invalidated_properties); + ensure_propchange_subscription(); + ask_for_timezone(); } - void monitor_timezone_property() + void ensure_propchange_subscription() { - GError *err = nullptr; - - /* - * There is an unlikely race which happens if there is an activation - * and timezone change before our match rule is added. - */ - notify_timezone(get_timezone_from_file(m_filename)); - - /* - * Make sure the bus is around at least until we add the match rules, - * otherwise things (tests) are sad. - */ - m_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, - nullptr, - &err); - - if (err) + if (m_signal_subscription_id == 0) { - g_warning("Couldn't get bus connection: '%s'", err->message); - g_error_free(err); - return; - } - - m_signal_subscription_id = g_dbus_connection_signal_subscribe(m_connection, - "org.freedesktop.timedate1", - "org.freedesktop.DBus.Properties", - "PropertiesChanged", - "/org/freedesktop/timedate1", - NULL, G_DBUS_SIGNAL_FLAGS_NONE, + m_signal_subscription_id = g_dbus_connection_signal_subscribe( + m_connection, + Bus::Timedate1::IFACE, + Bus::Properties::IFACE, + Bus::Properties::Signals::PROPERTIES_CHANGED, + Bus::Timedate1::ADDR, + nullptr, + G_DBUS_SIGNAL_FLAGS_NONE, on_properties_changed, - this, nullptr); + this, + nullptr); + } } - void notify_timezone(std::string new_timezone) + static void on_timedate1_vanished(GDBusConnection * /*connection*/, + const gchar * name, + gpointer /*gself*/) { - g_debug("notify_timezone '%s'", new_timezone.c_str()); - if (!new_timezone.empty()) - m_owner.timezone.set(new_timezone); + g_debug("%s not present on system bus", name); } - std::string get_timezone_from_file(const std::string& filename) + static void on_properties_changed(GDBusConnection * /*connection*/, + const gchar * /*sender_name*/, + const gchar * /*object_path*/, + const gchar * /*interface_name*/, + const gchar * /*signal_name*/, + GVariant * parameters, + gpointer gself) { - GError * error; - GIOChannel * io_channel; - std::string ret; - - // read through filename line-by-line until we fine a nonempty non-comment line - error = nullptr; - io_channel = g_io_channel_new_file(filename.c_str(), "r", &error); - if (error == nullptr) - { - auto line = g_string_new(nullptr); - - while(ret.empty()) - { - const auto io_status = g_io_channel_read_line_string(io_channel, line, nullptr, &error); - if ((io_status == G_IO_STATUS_EOF) || (io_status == G_IO_STATUS_ERROR)) - break; - if (error != nullptr) - break; - - g_strstrip(line->str); + auto self = static_cast(gself); - if (!line->len) // skip empty lines - continue; + GVariant* changed_properties {}; + gchar** invalidated_properties {}; + g_variant_get(parameters, "(s@a{sv}^as)", NULL, &changed_properties, &invalidated_properties); - if (*line->str=='#') // skip comments - continue; + const char* tz {}; + if (g_variant_lookup(changed_properties, Bus::Timedate1::Properties::TIMEZONE, "&s", &tz, NULL)) + { + if (tz != nullptr) + self->set_timezone(tz); + else + g_warning("%s no timezone found", G_STRLOC); + } + else if (g_strv_contains(invalidated_properties, Bus::Timedate1::Properties::TIMEZONE)) + { + self->ask_for_timezone(); + } - ret = line->str; - } + g_variant_unref(changed_properties); + g_strfreev(invalidated_properties); + } - g_string_free(line, true); - } else - /* Default to UTC */ - ret = "Etc/Utc"; + void ask_for_timezone() + { + g_return_if_fail(m_connection != nullptr); + + g_dbus_connection_call( + m_connection, + Bus::Timedate1::BUSNAME, + Bus::Timedate1::ADDR, + Bus::Properties::IFACE, + Bus::Properties::Methods::GET, + g_variant_new("(ss)", Bus::Timedate1::IFACE, Bus::Timedate1::Properties::TIMEZONE), + G_VARIANT_TYPE("(v)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + m_cancellable, + on_get_timezone_ready, + this); + } - if (io_channel != nullptr) + static void on_get_timezone_ready(GObject * connection, + GAsyncResult * res, + gpointer gself) + { + GError* error {}; + GVariant* v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(connection), res, &error); + if (v == nullptr) { - g_io_channel_shutdown(io_channel, false, nullptr); - g_io_channel_unref(io_channel); + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("%s Couldn't get timezone: %s", G_STRLOC, error->message); } - - if (error != nullptr) + else { - g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename.c_str(), error->message); - g_error_free(error); + GVariant* tzv {}; + g_variant_get(v, "(v)", &tzv); + const char* tz = g_variant_get_string(tzv, nullptr); + + if (tz != nullptr) + static_cast(gself)->set_timezone(tz); + else + g_warning("%s no timezone found", G_STRLOC); + + g_clear_pointer(&tzv, g_variant_unref); + g_clear_pointer(&v, g_variant_unref); } + } + + void set_timezone(const std::string& tz) + { + g_return_if_fail(!tz.empty()); - return ret; + g_debug("set timezone: '%s'", tz.c_str()); + m_owner.timezone.set(tz); } /*** **** ***/ - TimedatedTimezone & m_owner; - GDBusConnection *m_connection = nullptr; - unsigned long m_signal_subscription_id = 0; - std::string m_filename; + TimedatedTimezone& m_owner; + GCancellable* m_cancellable {}; + GDBusConnection* m_connection {}; + unsigned long m_signal_subscription_id {}; + unsigned int m_watcher_id {}; }; /*** **** ***/ -TimedatedTimezone::TimedatedTimezone(std::string filename): - impl(new Impl{*this, filename}) +TimedatedTimezone::TimedatedTimezone(): + impl{new Impl{*this}} { } -- cgit v1.2.3 From 8b30701b2052c0ae30d585248bce41b6d6fb3985 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Fri, 8 Apr 2016 16:37:55 -0500 Subject: in timezone-timedated, check for error!=nullptr before passing it to g_error_matches() --- src/timezone-timedated.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/timezone-timedated.cpp') diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp index 9254612..e123531 100644 --- a/src/timezone-timedated.cpp +++ b/src/timezone-timedated.cpp @@ -171,12 +171,12 @@ private: { GError* error {}; GVariant* v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(connection), res, &error); - if (v == nullptr) + if (error != nullptr) { if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning("%s Couldn't get timezone: %s", G_STRLOC, error->message); } - else + else if (v != nullptr) { GVariant* tzv {}; g_variant_get(v, "(v)", &tzv); -- cgit v1.2.3 From 3cd2c8e20343ef4b4efe7a34daba72f9af23205d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 12 Apr 2016 12:03:36 -0500 Subject: in TimedatedTimezone, take a GDBusConnection argument in the ctor to simplify state management --- include/datetime/timezone-timedated.h | 4 +- src/main.cpp | 14 ++++++- src/timezone-timedated.cpp | 71 +++++++++++++---------------------- 3 files changed, 42 insertions(+), 47 deletions(-) (limited to 'src/timezone-timedated.cpp') diff --git a/include/datetime/timezone-timedated.h b/include/datetime/timezone-timedated.h index 0857706..e0af184 100644 --- a/include/datetime/timezone-timedated.h +++ b/include/datetime/timezone-timedated.h @@ -22,6 +22,8 @@ #include // base class +#include // GDBusConnection* + #include // std::string namespace ayatana { @@ -34,7 +36,7 @@ namespace datetime { class TimedatedTimezone: public Timezone { public: - TimedatedTimezone(); + TimedatedTimezone(GDBusConnection* connection); ~TimedatedTimezone(); private: diff --git a/src/main.cpp b/src/main.cpp index 0da55a2..bb77c0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -71,7 +71,7 @@ namespace { // create the live objects auto live_settings = std::make_shared(); - auto live_timezones = std::make_shared(live_settings); + auto live_timezones = std::make_shared(live_settings, timezone_); auto live_clock = std::make_shared(timezone_); // create a full-month planner currently pointing to the current month @@ -132,8 +132,17 @@ main(int /*argc*/, char** /*argv*/) bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); textdomain(GETTEXT_PACKAGE); + // get the system bus + GError* error {}; + auto system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error); + if (error != nullptr) { + g_critical("Unable to get system bus: %s", error->message); + g_clear_error(&error); + return 0; + } + auto engine = create_engine(); - auto timezone_ = std::make_shared(); + auto timezone_ = std::make_shared(system_bus); auto state = create_state(engine, timezone_); auto actions = std::make_shared(state); MenuFactory factory(actions, state); @@ -171,5 +180,6 @@ main(int /*argc*/, char** /*argv*/) g_main_loop_run(loop); g_main_loop_unref(loop); + g_clear_object(&system_bus); return 0; } diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp index e123531..1b6497e 100644 --- a/src/timezone-timedated.cpp +++ b/src/timezone-timedated.cpp @@ -37,22 +37,36 @@ class TimedatedTimezone::Impl { public: - Impl(TimedatedTimezone& owner): + Impl(TimedatedTimezone& owner, GDBusConnection* connection): m_owner{owner}, + m_connection{G_DBUS_CONNECTION(g_object_ref(G_OBJECT(connection)))}, m_cancellable{g_cancellable_new()} { // set the fallback value m_owner.timezone.set("Etc/Utc"); // watch for timedate1 on the bus - m_watcher_id = g_bus_watch_name( - G_BUS_TYPE_SYSTEM, + m_watcher_id = g_bus_watch_name_on_connection( + m_connection, Bus::Timedate1::BUSNAME, G_BUS_NAME_WATCHER_FLAGS_AUTO_START, on_timedate1_appeared, on_timedate1_vanished, this, nullptr); + + // listen for changed properties + m_signal_subscription_id = g_dbus_connection_signal_subscribe( + m_connection, + Bus::Timedate1::IFACE, + Bus::Properties::IFACE, + Bus::Properties::Signals::PROPERTIES_CHANGED, + Bus::Timedate1::ADDR, + nullptr, + G_DBUS_SIGNAL_FLAGS_NONE, + on_properties_changed, + this, + nullptr); } ~Impl() @@ -62,57 +76,28 @@ public: g_bus_unwatch_name(m_watcher_id); - if (m_connection != nullptr) - { - if (m_signal_subscription_id) - g_dbus_connection_signal_unsubscribe(m_connection, m_signal_subscription_id); + g_dbus_connection_signal_unsubscribe(m_connection, m_signal_subscription_id); - g_clear_object(&m_connection); - } + g_clear_object(&m_connection); } private: - static void on_timedate1_appeared(GDBusConnection * connection, - const gchar * /*name*/, + static void on_timedate1_appeared(GDBusConnection * /*connection*/, + const gchar * name, const gchar * /*name_owner*/, gpointer gself) { - static_cast(gself)->timedate1_appeared(connection); - } - void timedate1_appeared(GDBusConnection* connection) - { - // cache m_connection for later... - g_clear_object(&m_connection); - m_connection = G_DBUS_CONNECTION(g_object_ref(G_OBJECT(connection))); + g_debug("%s appeared on bus", name); - ensure_propchange_subscription(); - ask_for_timezone(); - } - - void ensure_propchange_subscription() - { - if (m_signal_subscription_id == 0) - { - m_signal_subscription_id = g_dbus_connection_signal_subscribe( - m_connection, - Bus::Timedate1::IFACE, - Bus::Properties::IFACE, - Bus::Properties::Signals::PROPERTIES_CHANGED, - Bus::Timedate1::ADDR, - nullptr, - G_DBUS_SIGNAL_FLAGS_NONE, - on_properties_changed, - this, - nullptr); - } + static_cast(gself)->ask_for_timezone(); } static void on_timedate1_vanished(GDBusConnection * /*connection*/, const gchar * name, gpointer /*gself*/) { - g_debug("%s not present on system bus", name); + g_debug("%s not present on bus", name); } static void on_properties_changed(GDBusConnection * /*connection*/, @@ -148,8 +133,6 @@ private: void ask_for_timezone() { - g_return_if_fail(m_connection != nullptr); - g_dbus_connection_call( m_connection, Bus::Timedate1::BUSNAME, @@ -205,8 +188,8 @@ private: ***/ TimedatedTimezone& m_owner; - GCancellable* m_cancellable {}; GDBusConnection* m_connection {}; + GCancellable* m_cancellable {}; unsigned long m_signal_subscription_id {}; unsigned int m_watcher_id {}; }; @@ -215,8 +198,8 @@ private: **** ***/ -TimedatedTimezone::TimedatedTimezone(): - impl{new Impl{*this}} +TimedatedTimezone::TimedatedTimezone(GDBusConnection* connection): + impl{new Impl{*this, connection}} { } -- cgit v1.2.3