From 32ebc44249a2ce81bfa3eff39f31e9ae8eb28ef9 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Tue, 2 Jun 2015 16:12:13 +1200 Subject: Build against libecal 3.16 --- src/engine-eds.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/engine-eds.cpp b/src/engine-eds.cpp index 474bac5..f1c691e 100644 --- a/src/engine-eds.cpp +++ b/src/engine-eds.cpp @@ -263,6 +263,7 @@ private: g_debug("%s connecting a client to source %s", G_STRFUNC, source_uid); e_cal_client_connect(source, source_type, + -1, self->m_cancellable, on_client_connected, gself); -- cgit v1.2.3 From 5d0a213d11ac42667a1d34f20dd504ab6e1241b4 Mon Sep 17 00:00:00 2001 From: Sebastien Bacher Date: Thu, 27 Aug 2015 15:04:46 +0100 Subject: under unity8 start system-settings instead unity-control-center, don't special case u-c-c by looking for the binary, Unity installations come with it installed --- src/actions-live.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/actions-live.cpp b/src/actions-live.cpp index 3cbfb78..90186c3 100644 --- a/src/actions-live.cpp +++ b/src/actions-live.cpp @@ -61,18 +61,18 @@ void LiveActions::dispatch_url(const std::string& url) void LiveActions::desktop_open_settings_app() { - auto path = g_find_program_in_path("unity-control-center"); - - if ((path != nullptr) && (g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "Unity") == 0)) + if (g_getenv ("MIR_SOCKET") != nullptr) + { + dispatch_url("settings:///system/time-date"); + } + else if ((g_strcmp0 (g_getenv ("XDG_CURRENT_DESKTOP"), "Unity") == 0)) { - execute_command("unity-control-center datetime"); + execute_command("unity-control-center datetime"); } else { execute_command("gnome-control-center datetime"); } - - g_free (path); } bool LiveActions::desktop_has_calendar_app() const -- cgit v1.2.3 From 132ac93bbd4141824cfa22f6516366f953fce9a0 Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Mon, 31 Aug 2015 22:08:36 +0100 Subject: Use timedated's Timezone property instead of watching /etc/timezone Still need to rename everything to not use "timezone-file" --- src/CMakeLists.txt | 3 +- src/main.cpp | 4 +- src/timezone-file.cpp | 199 +++++++++++++++++++++++++++---------------------- src/timezones-live.cpp | 5 +- 4 files changed, 113 insertions(+), 98 deletions(-) (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5fc822c..6f27e99 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,8 +1,7 @@ set (SERVICE_LIB "indicatordatetimeservice") set (SERVICE_EXEC "indicator-datetime-service") -add_definitions (-DTIMEZONE_FILE="/etc/timezone" - -DG_LOG_DOMAIN="Indicator-Datetime") +add_definitions (-DG_LOG_DOMAIN="Indicator-Datetime") # handwritten sources set (SERVICE_C_SOURCES diff --git a/src/main.cpp b/src/main.cpp index 907d49f..460f98c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,7 +68,7 @@ namespace { // create the live objects auto live_settings = std::make_shared(); - auto live_timezones = std::make_shared(live_settings, TIMEZONE_FILE); + auto live_timezones = std::make_shared(live_settings); auto live_clock = std::make_shared(timezone_); // create a full-month planner currently pointing to the current month @@ -128,7 +128,7 @@ main(int /*argc*/, char** /*argv*/) textdomain(GETTEXT_PACKAGE); auto engine = create_engine(); - auto timezone_ = std::make_shared(TIMEZONE_FILE); + auto timezone_ = std::make_shared(); auto state = create_state(engine, timezone_); auto actions = std::make_shared(state); MenuFactory factory(actions, state); diff --git a/src/timezone-file.cpp b/src/timezone-file.cpp index 3c73913..15e4f61 100644 --- a/src/timezone-file.cpp +++ b/src/timezone-file.cpp @@ -36,10 +36,11 @@ class FileTimezone::Impl { public: - Impl(FileTimezone& owner, const std::string& filename): - m_owner(owner) + Impl(FileTimezone& owner): + m_owner(owner), + m_loop(g_main_loop_new(nullptr, FALSE)) { - set_filename(filename); + monitor_timezone_property(); } ~Impl() @@ -51,113 +52,128 @@ private: void clear() { - if (m_monitor_handler_id) - g_signal_handler_disconnect(m_monitor, m_monitor_handler_id); - - g_clear_object (&m_monitor); - - m_filename.clear(); - } - - void set_filename(const std::string& filename) - { - clear(); - - auto tmp = realpath(filename.c_str(), nullptr); - if(tmp != nullptr) + if (m_bus_watch_id) { - m_filename = tmp; - free(tmp); - } - else - { - g_warning("Unable to resolve path '%s': %s", filename.c_str(), g_strerror(errno)); - m_filename = filename; // better than nothing? + g_bus_unwatch_name (m_bus_watch_id); + m_bus_watch_id = 0; } - auto file = g_file_new_for_path(m_filename.c_str()); - GError * err = nullptr; - m_monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, nullptr, &err); - g_object_unref(file); - if (err) - { - g_warning("%s Unable to monitor timezone file '%s': %s", G_STRLOC, TIMEZONE_FILE, err->message); - g_error_free(err); - } - else + if (m_properties_changed_id) { - m_monitor_handler_id = g_signal_connect_swapped(m_monitor, "changed", G_CALLBACK(on_file_changed), this); - g_debug("%s Monitoring timezone file '%s'", G_STRLOC, m_filename.c_str()); + g_signal_handler_disconnect(m_proxy, m_properties_changed_id); + m_properties_changed_id = 0; } - reload(); + g_clear_object(&m_proxy); + g_clear_pointer(&m_loop, g_main_loop_unref); } - static void on_file_changed(gpointer gself) + static void on_properties_changed(GDBusProxy *proxy G_GNUC_UNUSED, + GVariant *changed_properties /* a{sv} */, + GStrv invalidated_properties G_GNUC_UNUSED, + gpointer gself) { - static_cast(gself)->reload(); + auto self = static_cast(gself); + char *tz; + + if (g_variant_lookup(changed_properties, "Timezone", "s", &tz, NULL)) + { + g_debug("on_properties_changed: got timezone '%s'", tz); + self->notify_timezone(tz); + g_free (tz); + } } - void reload() + static void on_proxy_ready(GObject *object G_GNUC_UNUSED, + GAsyncResult *res, + gpointer gself) { - const auto new_timezone = get_timezone_from_file(m_filename); + auto self = static_cast(gself); + GError *error = nullptr; + self->m_proxy = g_dbus_proxy_new_finish(res, &error); - if (!new_timezone.empty()) - m_owner.timezone.set(new_timezone); - } + if (error) + { + g_warning ("Couldn't create proxy to read timezone: %s", error->message); + goto out; + } - /*** - **** - ***/ + /* Read the property */ + GVariant *prop; + prop = g_dbus_proxy_get_cached_property(self->m_proxy, "Timezone"); - std::string get_timezone_from_file(const std::string& filename) - { - 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) + if (!prop || !g_variant_is_of_type(prop, G_VARIANT_TYPE_STRING)) { - 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_warning("Couldn't read the Timezone property, defaulting to Etc/Utc"); + self->notify_timezone("Etc/Utc"); + goto out; + } - g_strstrip(line->str); + const gchar *tz; + tz = g_variant_get_string(prop, nullptr); - if (!line->len) // skip empty lines - continue; + self->notify_timezone(tz); - if (*line->str=='#') // skip comments - continue; + self->m_properties_changed_id = g_signal_connect(self->m_proxy, + "g-properties-changed", + (GCallback) on_properties_changed, + gself); - ret = line->str; - } +out: + g_clear_pointer(&error, g_error_free); + g_clear_pointer(&prop, g_variant_unref); + if (self->m_loop && g_main_loop_is_running(self->m_loop)) + g_main_loop_quit(self->m_loop); + } - g_string_free(line, true); - } + static void on_name_appeared(GDBusConnection *connection, + const gchar *name, + const gchar *name_owner G_GNUC_UNUSED, + gpointer gself G_GNUC_UNUSED) + { + g_debug ("timedate1 appeared"); + g_dbus_proxy_new(connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + name, + "/org/freedesktop/timedate1", + "org.freedesktop.timedate1", + nullptr, + on_proxy_ready, + gself); + } - if (io_channel != nullptr) - { - g_io_channel_shutdown(io_channel, false, nullptr); - g_io_channel_unref(io_channel); - } + static void on_name_vanished(GDBusConnection *connection G_GNUC_UNUSED, + const gchar *name G_GNUC_UNUSED, + gpointer gself) + { + auto self = static_cast(gself); + g_debug ("timedate1 vanished"); + + g_signal_handler_disconnect(self->m_proxy, + self->m_properties_changed_id); + self->m_properties_changed_id = 0; + g_clear_object(&self->m_proxy); + g_clear_pointer(&self->m_proxy, g_main_loop_unref); + } - if (error != nullptr) - { - g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename.c_str(), error->message); - g_error_free(error); - } + void monitor_timezone_property() + { + m_bus_watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, + "org.freedesktop.timedate1", + G_BUS_NAME_WATCHER_FLAGS_AUTO_START, + on_name_appeared, + on_name_vanished, + this, + nullptr); + + g_main_loop_run(m_loop); + } - return ret; + void notify_timezone(std::string new_timezone) + { + if (!new_timezone.empty()) + m_owner.timezone.set(new_timezone); } /*** @@ -165,17 +181,18 @@ private: ***/ FileTimezone & m_owner; - std::string m_filename; - GFileMonitor * m_monitor = nullptr; - unsigned long m_monitor_handler_id = 0; + unsigned long m_properties_changed_id = 0; + unsigned long m_bus_watch_id = 0; + GDBusProxy *m_proxy = nullptr; + GMainLoop *m_loop = nullptr; }; /*** **** ***/ -FileTimezone::FileTimezone(const std::string& filename): - impl(new Impl{*this, filename}) +FileTimezone::FileTimezone(): + impl(new Impl{*this}) { } diff --git a/src/timezones-live.cpp b/src/timezones-live.cpp index 4902b76..c5dd43c 100644 --- a/src/timezones-live.cpp +++ b/src/timezones-live.cpp @@ -25,9 +25,8 @@ namespace unity { namespace indicator { namespace datetime { -LiveTimezones::LiveTimezones(const std::shared_ptr& settings, - const std::string& filename): - m_file(filename), +LiveTimezones::LiveTimezones(const std::shared_ptr& settings): + m_file(), m_settings(settings) { m_file.timezone.changed().connect([this](const std::string&){update_timezones();}); -- cgit v1.2.3 From f016debfbaac39f509f5947b098c141a03a3b65f Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Tue, 1 Sep 2015 10:25:41 +0100 Subject: Add a timeout so we can't hang forever --- src/timezone-file.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'src') diff --git a/src/timezone-file.cpp b/src/timezone-file.cpp index 15e4f61..ef9d78a 100644 --- a/src/timezone-file.cpp +++ b/src/timezone-file.cpp @@ -64,6 +64,12 @@ private: m_properties_changed_id = 0; } + if (m_timeout_id) + { + g_source_remove(m_timeout_id); + m_timeout_id = 0; + } + g_clear_object(&m_proxy); g_clear_pointer(&m_loop, g_main_loop_unref); } @@ -124,6 +130,12 @@ out: g_clear_pointer(&prop, g_variant_unref); if (self->m_loop && g_main_loop_is_running(self->m_loop)) g_main_loop_quit(self->m_loop); + + if (self->m_timeout_id) + { + g_source_remove(self->m_timeout_id); + self->m_timeout_id = 0; + } } static void on_name_appeared(GDBusConnection *connection, @@ -143,6 +155,20 @@ out: gself); } + static gboolean quit_loop(gpointer gself) + { + auto self = static_cast(gself); + + g_warning("Timed out when getting initial value of timezone, defaulting to UTC"); + self->notify_timezone("Etc/Utc"); + + g_main_loop_quit(self->m_loop); + + self->m_timeout_id = 0; + + return G_SOURCE_REMOVE; + } + static void on_name_vanished(GDBusConnection *connection G_GNUC_UNUSED, const gchar *name G_GNUC_UNUSED, gpointer gself) @@ -167,6 +193,8 @@ out: this, nullptr); + /* Incase something breaks, we don't want to hang */ + m_timeout_id = g_timeout_add(500, quit_loop, this); g_main_loop_run(m_loop); } @@ -183,6 +211,7 @@ out: FileTimezone & m_owner; unsigned long m_properties_changed_id = 0; unsigned long m_bus_watch_id = 0; + unsigned long m_timeout_id = 0; GDBusProxy *m_proxy = nullptr; GMainLoop *m_loop = nullptr; }; -- cgit v1.2.3 From a3937830bf656d5a8e2f368757b947cef0c8b1de Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Tue, 1 Sep 2015 10:52:13 +0100 Subject: Rename FileTimezone to TimedatedTimezone --- src/CMakeLists.txt | 2 +- src/main.cpp | 4 +- src/timezone-file.cpp | 238 --------------------------------------------- src/timezone-timedated.cpp | 238 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 241 insertions(+), 241 deletions(-) delete mode 100644 src/timezone-file.cpp create mode 100644 src/timezone-timedated.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6f27e99..f8d219a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -33,9 +33,9 @@ set (SERVICE_CXX_SOURCES settings-live.cpp snap.cpp sound.cpp - timezone-file.cpp timezone-geoclue.cpp timezones-live.cpp + timezone-timedated.cpp utils.c wakeup-timer-mainloop.cpp wakeup-timer-powerd.cpp) diff --git a/src/main.cpp b/src/main.cpp index 460f98c..c7769c9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,8 +31,8 @@ #include #include #include -#include #include +#include #include #include @@ -128,7 +128,7 @@ main(int /*argc*/, char** /*argv*/) textdomain(GETTEXT_PACKAGE); auto engine = create_engine(); - auto timezone_ = std::make_shared(); + auto timezone_ = std::make_shared(); auto state = create_state(engine, timezone_); auto actions = std::make_shared(state); MenuFactory factory(actions, state); diff --git a/src/timezone-file.cpp b/src/timezone-file.cpp deleted file mode 100644 index ef9d78a..0000000 --- a/src/timezone-file.cpp +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright 2013 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 - -#include -#include - -namespace unity { -namespace indicator { -namespace datetime { - -/*** -**** -***/ - -class FileTimezone::Impl -{ -public: - - Impl(FileTimezone& owner): - m_owner(owner), - m_loop(g_main_loop_new(nullptr, FALSE)) - { - monitor_timezone_property(); - } - - ~Impl() - { - clear(); - } - -private: - - void clear() - { - if (m_bus_watch_id) - { - g_bus_unwatch_name (m_bus_watch_id); - m_bus_watch_id = 0; - } - - if (m_properties_changed_id) - { - g_signal_handler_disconnect(m_proxy, m_properties_changed_id); - m_properties_changed_id = 0; - } - - if (m_timeout_id) - { - g_source_remove(m_timeout_id); - m_timeout_id = 0; - } - - g_clear_object(&m_proxy); - g_clear_pointer(&m_loop, g_main_loop_unref); - } - - static void on_properties_changed(GDBusProxy *proxy G_GNUC_UNUSED, - GVariant *changed_properties /* a{sv} */, - GStrv invalidated_properties G_GNUC_UNUSED, - gpointer gself) - { - auto self = static_cast(gself); - char *tz; - - if (g_variant_lookup(changed_properties, "Timezone", "s", &tz, NULL)) - { - g_debug("on_properties_changed: got timezone '%s'", tz); - self->notify_timezone(tz); - g_free (tz); - } - } - - static void on_proxy_ready(GObject *object G_GNUC_UNUSED, - GAsyncResult *res, - gpointer gself) - { - auto self = static_cast(gself); - GError *error = nullptr; - self->m_proxy = g_dbus_proxy_new_finish(res, &error); - - if (error) - { - g_warning ("Couldn't create proxy to read timezone: %s", error->message); - goto out; - } - - /* Read the property */ - GVariant *prop; - prop = g_dbus_proxy_get_cached_property(self->m_proxy, "Timezone"); - - if (!prop || !g_variant_is_of_type(prop, G_VARIANT_TYPE_STRING)) - { - g_warning("Couldn't read the Timezone property, defaulting to Etc/Utc"); - self->notify_timezone("Etc/Utc"); - goto out; - } - - const gchar *tz; - tz = g_variant_get_string(prop, nullptr); - - self->notify_timezone(tz); - - self->m_properties_changed_id = g_signal_connect(self->m_proxy, - "g-properties-changed", - (GCallback) on_properties_changed, - gself); - -out: - g_clear_pointer(&error, g_error_free); - g_clear_pointer(&prop, g_variant_unref); - if (self->m_loop && g_main_loop_is_running(self->m_loop)) - g_main_loop_quit(self->m_loop); - - if (self->m_timeout_id) - { - g_source_remove(self->m_timeout_id); - self->m_timeout_id = 0; - } - } - - static void on_name_appeared(GDBusConnection *connection, - const gchar *name, - const gchar *name_owner G_GNUC_UNUSED, - gpointer gself G_GNUC_UNUSED) - { - g_debug ("timedate1 appeared"); - g_dbus_proxy_new(connection, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - name, - "/org/freedesktop/timedate1", - "org.freedesktop.timedate1", - nullptr, - on_proxy_ready, - gself); - } - - static gboolean quit_loop(gpointer gself) - { - auto self = static_cast(gself); - - g_warning("Timed out when getting initial value of timezone, defaulting to UTC"); - self->notify_timezone("Etc/Utc"); - - g_main_loop_quit(self->m_loop); - - self->m_timeout_id = 0; - - return G_SOURCE_REMOVE; - } - - static void on_name_vanished(GDBusConnection *connection G_GNUC_UNUSED, - const gchar *name G_GNUC_UNUSED, - gpointer gself) - { - auto self = static_cast(gself); - g_debug ("timedate1 vanished"); - - g_signal_handler_disconnect(self->m_proxy, - self->m_properties_changed_id); - self->m_properties_changed_id = 0; - g_clear_object(&self->m_proxy); - g_clear_pointer(&self->m_proxy, g_main_loop_unref); - } - - void monitor_timezone_property() - { - m_bus_watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, - "org.freedesktop.timedate1", - G_BUS_NAME_WATCHER_FLAGS_AUTO_START, - on_name_appeared, - on_name_vanished, - this, - nullptr); - - /* Incase something breaks, we don't want to hang */ - m_timeout_id = g_timeout_add(500, quit_loop, this); - g_main_loop_run(m_loop); - } - - void notify_timezone(std::string new_timezone) - { - if (!new_timezone.empty()) - m_owner.timezone.set(new_timezone); - } - - /*** - **** - ***/ - - FileTimezone & m_owner; - unsigned long m_properties_changed_id = 0; - unsigned long m_bus_watch_id = 0; - unsigned long m_timeout_id = 0; - GDBusProxy *m_proxy = nullptr; - GMainLoop *m_loop = nullptr; -}; - -/*** -**** -***/ - -FileTimezone::FileTimezone(): - impl(new Impl{*this}) -{ -} - -FileTimezone::~FileTimezone() -{ -} - -/*** -**** -***/ - -} // namespace datetime -} // namespace indicator -} // namespace unity diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp new file mode 100644 index 0000000..778697a --- /dev/null +++ b/src/timezone-timedated.cpp @@ -0,0 +1,238 @@ +/* + * Copyright 2013 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 + +#include +#include + +namespace unity { +namespace indicator { +namespace datetime { + +/*** +**** +***/ + +class TimedatedTimezone::Impl +{ +public: + + Impl(TimedatedTimezone& owner): + m_owner(owner), + m_loop(g_main_loop_new(nullptr, FALSE)) + { + monitor_timezone_property(); + } + + ~Impl() + { + clear(); + } + +private: + + void clear() + { + if (m_bus_watch_id) + { + g_bus_unwatch_name (m_bus_watch_id); + m_bus_watch_id = 0; + } + + if (m_properties_changed_id) + { + g_signal_handler_disconnect(m_proxy, m_properties_changed_id); + m_properties_changed_id = 0; + } + + if (m_timeout_id) + { + g_source_remove(m_timeout_id); + m_timeout_id = 0; + } + + g_clear_object(&m_proxy); + g_clear_pointer(&m_loop, g_main_loop_unref); + } + + static void on_properties_changed(GDBusProxy *proxy G_GNUC_UNUSED, + GVariant *changed_properties /* a{sv} */, + GStrv invalidated_properties G_GNUC_UNUSED, + gpointer gself) + { + auto self = static_cast(gself); + char *tz; + + if (g_variant_lookup(changed_properties, "Timezone", "s", &tz, NULL)) + { + g_debug("on_properties_changed: got timezone '%s'", tz); + self->notify_timezone(tz); + g_free (tz); + } + } + + static void on_proxy_ready(GObject *object G_GNUC_UNUSED, + GAsyncResult *res, + gpointer gself) + { + auto self = static_cast(gself); + GError *error = nullptr; + self->m_proxy = g_dbus_proxy_new_finish(res, &error); + + if (error) + { + g_warning ("Couldn't create proxy to read timezone: %s", error->message); + goto out; + } + + /* Read the property */ + GVariant *prop; + prop = g_dbus_proxy_get_cached_property(self->m_proxy, "Timezone"); + + if (!prop || !g_variant_is_of_type(prop, G_VARIANT_TYPE_STRING)) + { + g_warning("Couldn't read the Timezone property, defaulting to Etc/Utc"); + self->notify_timezone("Etc/Utc"); + goto out; + } + + const gchar *tz; + tz = g_variant_get_string(prop, nullptr); + + self->notify_timezone(tz); + + self->m_properties_changed_id = g_signal_connect(self->m_proxy, + "g-properties-changed", + (GCallback) on_properties_changed, + gself); + +out: + g_clear_pointer(&error, g_error_free); + g_clear_pointer(&prop, g_variant_unref); + if (self->m_loop && g_main_loop_is_running(self->m_loop)) + g_main_loop_quit(self->m_loop); + + if (self->m_timeout_id) + { + g_source_remove(self->m_timeout_id); + self->m_timeout_id = 0; + } + } + + static void on_name_appeared(GDBusConnection *connection, + const gchar *name, + const gchar *name_owner G_GNUC_UNUSED, + gpointer gself G_GNUC_UNUSED) + { + g_debug ("timedate1 appeared"); + g_dbus_proxy_new(connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, + name, + "/org/freedesktop/timedate1", + "org.freedesktop.timedate1", + nullptr, + on_proxy_ready, + gself); + } + + static gboolean quit_loop(gpointer gself) + { + auto self = static_cast(gself); + + g_warning("Timed out when getting initial value of timezone, defaulting to UTC"); + self->notify_timezone("Etc/Utc"); + + g_main_loop_quit(self->m_loop); + + self->m_timeout_id = 0; + + return G_SOURCE_REMOVE; + } + + static void on_name_vanished(GDBusConnection *connection G_GNUC_UNUSED, + const gchar *name G_GNUC_UNUSED, + gpointer gself) + { + auto self = static_cast(gself); + g_debug ("timedate1 vanished"); + + g_signal_handler_disconnect(self->m_proxy, + self->m_properties_changed_id); + self->m_properties_changed_id = 0; + g_clear_object(&self->m_proxy); + g_clear_pointer(&self->m_proxy, g_main_loop_unref); + } + + void monitor_timezone_property() + { + m_bus_watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, + "org.freedesktop.timedate1", + G_BUS_NAME_WATCHER_FLAGS_AUTO_START, + on_name_appeared, + on_name_vanished, + this, + nullptr); + + /* Incase something breaks, we don't want to hang */ + m_timeout_id = g_timeout_add(500, quit_loop, this); + g_main_loop_run(m_loop); + } + + void notify_timezone(std::string new_timezone) + { + if (!new_timezone.empty()) + m_owner.timezone.set(new_timezone); + } + + /*** + **** + ***/ + + TimedatedTimezone & m_owner; + unsigned long m_properties_changed_id = 0; + unsigned long m_bus_watch_id = 0; + unsigned long m_timeout_id = 0; + GDBusProxy *m_proxy = nullptr; + GMainLoop *m_loop = nullptr; +}; + +/*** +**** +***/ + +TimedatedTimezone::TimedatedTimezone(): + impl(new Impl{*this}) +{ +} + +TimedatedTimezone::~TimedatedTimezone() +{ +} + +/*** +**** +***/ + +} // namespace datetime +} // namespace indicator +} // namespace unity -- cgit v1.2.3 From 1da27f9088a759e78e88a390583d8646f2a82656 Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Tue, 1 Sep 2015 11:10:09 +0100 Subject: Add some comments --- src/timezone-timedated.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp index 778697a..bfe38f4 100644 --- a/src/timezone-timedated.cpp +++ b/src/timezone-timedated.cpp @@ -195,6 +195,8 @@ out: /* Incase something breaks, we don't want to hang */ m_timeout_id = g_timeout_add(500, quit_loop, this); + + /* We need to block until we've read the tz once */ g_main_loop_run(m_loop); } -- cgit v1.2.3 From 29b5c4da1e44534352d29536bb6ad1c721406b8a Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Thu, 3 Sep 2015 10:54:20 +0100 Subject: Avoid nested GMainLoops by reading from the file on startup Restore some tests for this functionality. --- src/timezone-timedated.cpp | 128 ++++++++++++++++++++++++++++++--------------- 1 file changed, 86 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp index bfe38f4..a2918f0 100644 --- a/src/timezone-timedated.cpp +++ b/src/timezone-timedated.cpp @@ -36,10 +36,11 @@ class TimedatedTimezone::Impl { public: - Impl(TimedatedTimezone& owner): + Impl(TimedatedTimezone& owner, std::string filename): m_owner(owner), - m_loop(g_main_loop_new(nullptr, FALSE)) + m_filename(filename) { + g_debug("Filename is '%s'", filename.c_str()); monitor_timezone_property(); } @@ -64,14 +65,7 @@ private: m_properties_changed_id = 0; } - if (m_timeout_id) - { - g_source_remove(m_timeout_id); - m_timeout_id = 0; - } - g_clear_object(&m_proxy); - g_clear_pointer(&m_loop, g_main_loop_unref); } static void on_properties_changed(GDBusProxy *proxy G_GNUC_UNUSED, @@ -128,14 +122,6 @@ private: out: g_clear_pointer(&error, g_error_free); g_clear_pointer(&prop, g_variant_unref); - if (self->m_loop && g_main_loop_is_running(self->m_loop)) - g_main_loop_quit(self->m_loop); - - if (self->m_timeout_id) - { - g_source_remove(self->m_timeout_id); - self->m_timeout_id = 0; - } } static void on_name_appeared(GDBusConnection *connection, @@ -155,20 +141,6 @@ out: gself); } - static gboolean quit_loop(gpointer gself) - { - auto self = static_cast(gself); - - g_warning("Timed out when getting initial value of timezone, defaulting to UTC"); - self->notify_timezone("Etc/Utc"); - - g_main_loop_quit(self->m_loop); - - self->m_timeout_id = 0; - - return G_SOURCE_REMOVE; - } - static void on_name_vanished(GDBusConnection *connection G_GNUC_UNUSED, const gchar *name G_GNUC_UNUSED, gpointer gself) @@ -185,27 +157,100 @@ out: void monitor_timezone_property() { - m_bus_watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM, + GError *err = nullptr; + GDBusConnection *conn; + + /* + * 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. + */ + conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, + nullptr, + &err); + + if (err) + { + g_warning("Couldn't get bus connection: '%s'", err->message); + g_error_free(err); + return; + } + + m_bus_watch_id = g_bus_watch_name_on_connection(conn, "org.freedesktop.timedate1", - G_BUS_NAME_WATCHER_FLAGS_AUTO_START, + G_BUS_NAME_WATCHER_FLAGS_NONE, on_name_appeared, on_name_vanished, this, nullptr); - /* Incase something breaks, we don't want to hang */ - m_timeout_id = g_timeout_add(500, quit_loop, this); - - /* We need to block until we've read the tz once */ - g_main_loop_run(m_loop); + g_object_unref (conn); } void notify_timezone(std::string new_timezone) { + g_debug("notify_timezone '%s'", new_timezone.c_str()); if (!new_timezone.empty()) m_owner.timezone.set(new_timezone); } + std::string get_timezone_from_file(const std::string& filename) + { + 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); + + if (!line->len) // skip empty lines + continue; + + if (*line->str=='#') // skip comments + continue; + + ret = line->str; + } + + g_string_free(line, true); + } else + /* Default to UTC */ + ret = "Etc/Utc"; + + if (io_channel != nullptr) + { + g_io_channel_shutdown(io_channel, false, nullptr); + g_io_channel_unref(io_channel); + } + + if (error != nullptr) + { + g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename.c_str(), error->message); + g_error_free(error); + } + + return ret; + } + /*** **** ***/ @@ -213,17 +258,16 @@ out: TimedatedTimezone & m_owner; unsigned long m_properties_changed_id = 0; unsigned long m_bus_watch_id = 0; - unsigned long m_timeout_id = 0; GDBusProxy *m_proxy = nullptr; - GMainLoop *m_loop = nullptr; + std::string m_filename; }; /*** **** ***/ -TimedatedTimezone::TimedatedTimezone(): - impl(new Impl{*this}) +TimedatedTimezone::TimedatedTimezone(std::string filename): + impl(new Impl{*this, filename}) { } -- cgit v1.2.3 From bda03d74c5a36bdaac89a64ff73fcc92dbd795a0 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Thu, 3 Sep 2015 18:00:36 +0200 Subject: timezone-timedated: subscribe to PropertiesChanged directly This avoids some unnecessary calls that GDBusProxy makes and the name watching. --- src/timezone-timedated.cpp | 132 +++++++++++---------------------------------- 1 file changed, 30 insertions(+), 102 deletions(-) (limited to 'src') diff --git a/src/timezone-timedated.cpp b/src/timezone-timedated.cpp index a2918f0..fa99cd2 100644 --- a/src/timezone-timedated.cpp +++ b/src/timezone-timedated.cpp @@ -53,112 +53,42 @@ private: void clear() { - if (m_bus_watch_id) + if (m_connection && m_signal_subscription_id) { - g_bus_unwatch_name (m_bus_watch_id); - m_bus_watch_id = 0; + g_dbus_connection_signal_unsubscribe (m_connection, m_signal_subscription_id); + m_signal_subscription_id = 0; } - if (m_properties_changed_id) - { - g_signal_handler_disconnect(m_proxy, m_properties_changed_id); - m_properties_changed_id = 0; - } - - g_clear_object(&m_proxy); - } - - static void on_properties_changed(GDBusProxy *proxy G_GNUC_UNUSED, - GVariant *changed_properties /* a{sv} */, - GStrv invalidated_properties G_GNUC_UNUSED, - gpointer gself) - { - auto self = static_cast(gself); - char *tz; - - if (g_variant_lookup(changed_properties, "Timezone", "s", &tz, NULL)) - { - g_debug("on_properties_changed: got timezone '%s'", tz); - self->notify_timezone(tz); - g_free (tz); - } + g_clear_object(&m_connection); } - static void on_proxy_ready(GObject *object G_GNUC_UNUSED, - GAsyncResult *res, + 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); - GError *error = nullptr; - self->m_proxy = g_dbus_proxy_new_finish(res, &error); + const char *tz; + GVariant *changed_properties; + gchar **invalidated_properties; - if (error) - { - g_warning ("Couldn't create proxy to read timezone: %s", error->message); - goto out; - } - - /* Read the property */ - GVariant *prop; - prop = g_dbus_proxy_get_cached_property(self->m_proxy, "Timezone"); - - if (!prop || !g_variant_is_of_type(prop, G_VARIANT_TYPE_STRING)) - { - g_warning("Couldn't read the Timezone property, defaulting to Etc/Utc"); - self->notify_timezone("Etc/Utc"); - goto out; - } + g_variant_get (parameters, "(s@a{sv}^as)", NULL, &changed_properties, &invalidated_properties); - const gchar *tz; - tz = g_variant_get_string(prop, nullptr); - - self->notify_timezone(tz); - - self->m_properties_changed_id = g_signal_connect(self->m_proxy, - "g-properties-changed", - (GCallback) on_properties_changed, - gself); - -out: - g_clear_pointer(&error, g_error_free); - g_clear_pointer(&prop, g_variant_unref); - } - - static void on_name_appeared(GDBusConnection *connection, - const gchar *name, - const gchar *name_owner G_GNUC_UNUSED, - gpointer gself G_GNUC_UNUSED) - { - g_debug ("timedate1 appeared"); - g_dbus_proxy_new(connection, - G_DBUS_PROXY_FLAGS_NONE, - NULL, - name, - "/org/freedesktop/timedate1", - "org.freedesktop.timedate1", - nullptr, - on_proxy_ready, - gself); - } - - static void on_name_vanished(GDBusConnection *connection G_GNUC_UNUSED, - const gchar *name G_GNUC_UNUSED, - gpointer gself) - { - auto self = static_cast(gself); - g_debug ("timedate1 vanished"); + 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)); - g_signal_handler_disconnect(self->m_proxy, - self->m_properties_changed_id); - self->m_properties_changed_id = 0; - g_clear_object(&self->m_proxy); - g_clear_pointer(&self->m_proxy, g_main_loop_unref); + g_variant_unref (changed_properties); + g_strfreev (invalidated_properties); } void monitor_timezone_property() { GError *err = nullptr; - GDBusConnection *conn; /* * There is an unlikely race which happens if there is an activation @@ -170,7 +100,7 @@ out: * Make sure the bus is around at least until we add the match rules, * otherwise things (tests) are sad. */ - conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, + m_connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &err); @@ -181,15 +111,14 @@ out: return; } - m_bus_watch_id = g_bus_watch_name_on_connection(conn, + m_signal_subscription_id = g_dbus_connection_signal_subscribe(m_connection, "org.freedesktop.timedate1", - G_BUS_NAME_WATCHER_FLAGS_NONE, - on_name_appeared, - on_name_vanished, - this, - nullptr); - - g_object_unref (conn); + "org.freedesktop.DBus.Properties", + "PropertiesChanged", + "/org/freedesktop/timedate1", + NULL, G_DBUS_SIGNAL_FLAGS_NONE, + on_properties_changed, + this, nullptr); } void notify_timezone(std::string new_timezone) @@ -256,9 +185,8 @@ out: ***/ TimedatedTimezone & m_owner; - unsigned long m_properties_changed_id = 0; - unsigned long m_bus_watch_id = 0; - GDBusProxy *m_proxy = nullptr; + GDBusConnection *m_connection = nullptr; + unsigned long m_signal_subscription_id = 0; std::string m_filename; }; -- cgit v1.2.3