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" --- include/datetime/timezone-file.h | 2 +- include/datetime/timezones-live.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/datetime/timezone-file.h b/include/datetime/timezone-file.h index eca9c29..05dc7b0 100644 --- a/include/datetime/timezone-file.h +++ b/include/datetime/timezone-file.h @@ -34,7 +34,7 @@ namespace datetime { class FileTimezone: public Timezone { public: - FileTimezone(const std::string& filename); + FileTimezone(); ~FileTimezone(); private: diff --git a/include/datetime/timezones-live.h b/include/datetime/timezones-live.h index ca4ef31..49d9120 100644 --- a/include/datetime/timezones-live.h +++ b/include/datetime/timezones-live.h @@ -38,7 +38,7 @@ namespace datetime { class LiveTimezones: public Timezones { public: - LiveTimezones(const std::shared_ptr& settings, const std::string& filename); + LiveTimezones(const std::shared_ptr& settings); private: void update_geolocation(); -- 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 --- include/datetime/timezone-file.h | 54 -------- include/datetime/timezone-timedated.h | 54 ++++++++ include/datetime/timezones-live.h | 6 +- src/CMakeLists.txt | 2 +- src/main.cpp | 4 +- src/timezone-file.cpp | 238 ---------------------------------- src/timezone-timedated.cpp | 238 ++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 2 +- tests/test-timezone-file.cpp | 73 ----------- tests/test-timezone-timedated.cpp | 73 +++++++++++ 10 files changed, 372 insertions(+), 372 deletions(-) delete mode 100644 include/datetime/timezone-file.h create mode 100644 include/datetime/timezone-timedated.h delete mode 100644 src/timezone-file.cpp create mode 100644 src/timezone-timedated.cpp delete mode 100644 tests/test-timezone-file.cpp create mode 100644 tests/test-timezone-timedated.cpp (limited to 'include') diff --git a/include/datetime/timezone-file.h b/include/datetime/timezone-file.h deleted file mode 100644 index 05dc7b0..0000000 --- a/include/datetime/timezone-file.h +++ /dev/null @@ -1,54 +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 - */ - -#ifndef INDICATOR_DATETIME_FILE_TIMEZONE_H -#define INDICATOR_DATETIME_FILE_TIMEZONE_H - -#include // base class - -#include // std::string - -namespace unity { -namespace indicator { -namespace datetime { - -/** - * \brief A #Timezone that gets its information from monitoring a file, such as /etc/timezone - */ -class FileTimezone: public Timezone -{ -public: - FileTimezone(); - ~FileTimezone(); - -private: - class Impl; - friend Impl; - std::unique_ptr impl; - - // we have pointers in here, so disable copying - FileTimezone(const FileTimezone&) =delete; - FileTimezone& operator=(const FileTimezone&) =delete; -}; - -} // namespace datetime -} // namespace indicator -} // namespace unity - -#endif // INDICATOR_DATETIME_FILE_TIMEZONE_H diff --git a/include/datetime/timezone-timedated.h b/include/datetime/timezone-timedated.h new file mode 100644 index 0000000..3df9a3e --- /dev/null +++ b/include/datetime/timezone-timedated.h @@ -0,0 +1,54 @@ +/* + * 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 + */ + +#ifndef INDICATOR_DATETIME_TIMEDATED_TIMEZONE_H +#define INDICATOR_DATETIME_TIMEDATED_TIMEZONE_H + +#include // base class + +#include // std::string + +namespace unity { +namespace indicator { +namespace datetime { + +/** + * \brief A #Timezone that gets its information from monitoring a file, such as /etc/timezone + */ +class TimedatedTimezone: public Timezone +{ +public: + TimedatedTimezone(); + ~TimedatedTimezone(); + +private: + class Impl; + friend Impl; + std::unique_ptr impl; + + // we have pointers in here, so disable copying + TimedatedTimezone(const TimedatedTimezone&) =delete; + TimedatedTimezone& operator=(const TimedatedTimezone&) =delete; +}; + +} // namespace datetime +} // namespace indicator +} // namespace unity + +#endif // INDICATOR_DATETIME_TIMEDATED_TIMEZONE_H diff --git a/include/datetime/timezones-live.h b/include/datetime/timezones-live.h index 49d9120..b9d78a5 100644 --- a/include/datetime/timezones-live.h +++ b/include/datetime/timezones-live.h @@ -22,8 +22,8 @@ #include #include -#include #include +#include #include // shared_ptr<> @@ -32,7 +32,7 @@ namespace indicator { namespace datetime { /** - * \brief #Timezones object that uses a #FileTimezone and #GeoclueTimezone + * \brief #Timezones object that uses a #TimedatedTimezone and #GeoclueTimezone * to detect what timezone we're in */ class LiveTimezones: public Timezones @@ -44,7 +44,7 @@ private: void update_geolocation(); void update_timezones(); - FileTimezone m_file; + TimedatedTimezone m_file; std::shared_ptr m_settings; std::shared_ptr m_geo; }; 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 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9d26484..032b84e 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -58,7 +58,7 @@ add_test_by_name(test-locations) add_test_by_name(test-menus) add_test_by_name(test-planner) add_test_by_name(test-settings) -add_test_by_name(test-timezone-file) +add_test_by_name(test-timezone-timedated) add_test_by_name(test-utils) set (TEST_NAME manual-test-snap) diff --git a/tests/test-timezone-file.cpp b/tests/test-timezone-file.cpp deleted file mode 100644 index 0ec496d..0000000 --- a/tests/test-timezone-file.cpp +++ /dev/null @@ -1,73 +0,0 @@ - -/* - * Copyright 2013 Canonical Ltd. - * - * Authors: - * Charles Kerr - * - * 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 . - */ - -#include "timedated-fixture.h" - -#include - -#include // fopen() -//#include // chmod() -#include // sync() - -using unity::indicator::datetime::FileTimezone; - -/** - * Test that timezone-file picks up the initial value - */ -TEST_F(TimedateFixture, InitialValue) -{ - const std::string expected_timezone = "America/Chicago"; - set_timezone(expected_timezone); - FileTimezone tz; - ASSERT_EQ(expected_timezone, tz.timezone.get()); -} - -/** - * Test that changing the tz after we are running works. - */ -TEST_F(TimedateFixture, ChangedValue) -{ - const std::string initial_timezone = "America/Chicago"; - const std::string changed_timezone = "America/New_York"; - GMainLoop *l = g_main_loop_new(nullptr, FALSE); - - set_timezone(initial_timezone); - - FileTimezone tz; - ASSERT_EQ(initial_timezone, tz.timezone.get()); - - bool changed = false; - tz.timezone.changed().connect( - [&changed, this, l](const std::string& s){ - g_message("timezone changed to %s", s.c_str()); - changed = true; - g_main_loop_quit(l); - }); - - g_idle_add([](gpointer gself){ - static_cast(gself)->set_timezone("America/New_York"); - return G_SOURCE_REMOVE; - }, this); - - g_main_loop_run(l); - - ASSERT_TRUE(changed); - ASSERT_EQ(changed_timezone, tz.timezone.get()); -} diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp new file mode 100644 index 0000000..7086e96 --- /dev/null +++ b/tests/test-timezone-timedated.cpp @@ -0,0 +1,73 @@ + +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr + * + * 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 . + */ + +#include "timedated-fixture.h" + +#include + +#include // fopen() +//#include // chmod() +#include // sync() + +using unity::indicator::datetime::TimedatedTimezone; + +/** + * Test that timezone-file picks up the initial value + */ +TEST_F(TimedateFixture, InitialValue) +{ + const std::string expected_timezone = "America/Chicago"; + set_timezone(expected_timezone); + TimedatedTimezone tz; + ASSERT_EQ(expected_timezone, tz.timezone.get()); +} + +/** + * Test that changing the tz after we are running works. + */ +TEST_F(TimedateFixture, ChangedValue) +{ + const std::string initial_timezone = "America/Chicago"; + const std::string changed_timezone = "America/New_York"; + GMainLoop *l = g_main_loop_new(nullptr, FALSE); + + set_timezone(initial_timezone); + + TimedatedTimezone tz; + ASSERT_EQ(initial_timezone, tz.timezone.get()); + + bool changed = false; + tz.timezone.changed().connect( + [&changed, this, l](const std::string& s){ + g_message("timezone changed to %s", s.c_str()); + changed = true; + g_main_loop_quit(l); + }); + + g_idle_add([](gpointer gself){ + static_cast(gself)->set_timezone("America/New_York"); + return G_SOURCE_REMOVE; + }, this); + + g_main_loop_run(l); + + ASSERT_TRUE(changed); + ASSERT_EQ(changed_timezone, tz.timezone.get()); +} -- 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. --- include/datetime/timezone-timedated.h | 4 +- src/timezone-timedated.cpp | 128 +++++++++++++++++++++++----------- tests/test-timezone-timedated.cpp | 100 +++++++++++++++++++++----- tests/timedated-fixture.h | 3 +- 4 files changed, 174 insertions(+), 61 deletions(-) (limited to 'include') diff --git a/include/datetime/timezone-timedated.h b/include/datetime/timezone-timedated.h index 3df9a3e..5978e3e 100644 --- a/include/datetime/timezone-timedated.h +++ b/include/datetime/timezone-timedated.h @@ -20,6 +20,8 @@ #ifndef INDICATOR_DATETIME_TIMEDATED_TIMEZONE_H #define INDICATOR_DATETIME_TIMEDATED_TIMEZONE_H +#define DEFAULT_FILENAME "/etc/timezone" + #include // base class #include // std::string @@ -34,7 +36,7 @@ namespace datetime { class TimedatedTimezone: public Timezone { public: - TimedatedTimezone(); + TimedatedTimezone(std::string filename = DEFAULT_FILENAME); ~TimedatedTimezone(); private: 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}) { } diff --git a/tests/test-timezone-timedated.cpp b/tests/test-timezone-timedated.cpp index 7086e96..5cfc311 100644 --- a/tests/test-timezone-timedated.cpp +++ b/tests/test-timezone-timedated.cpp @@ -22,43 +22,99 @@ #include -#include // fopen() -//#include // chmod() -#include // sync() - using unity::indicator::datetime::TimedatedTimezone; +/*** +**** +***/ + +#define TIMEZONE_FILE (SANDBOX"/timezone") + +class TimezoneFixture: public TimedateFixture +{ + private: + + typedef TimedateFixture super; + + protected: + + void SetUp() override + { + super::SetUp(); + } + + void TearDown() override + { + super::TearDown(); + } + + public: + + /* convenience func to set the timezone file */ + void set_file(const std::string& text) + { + g_debug("set_file %s %s", TIMEZONE_FILE, text.c_str()); + auto fp = fopen(TIMEZONE_FILE, "w+"); + fprintf(fp, "%s\n", text.c_str()); + fclose(fp); + sync(); + } +}; + /** - * Test that timezone-file picks up the initial value + * Test that timezone-timedated warns, but doesn't crash, if the timezone file doesn't exist */ -TEST_F(TimedateFixture, InitialValue) +TEST_F(TimezoneFixture, NoFile) { - const std::string expected_timezone = "America/Chicago"; - set_timezone(expected_timezone); - TimedatedTimezone tz; + remove(TIMEZONE_FILE); + ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + + TimedatedTimezone tz(TIMEZONE_FILE); + testLogCount(G_LOG_LEVEL_WARNING, 1); +} + +/** + * Test that timezone-timedated gives a default of UTC if the file doesn't exist + */ +TEST_F(TimezoneFixture, DefaultValueNoFile) +{ + const std::string expected_timezone = "Etc/Utc"; + remove(TIMEZONE_FILE); + ASSERT_FALSE(g_file_test(TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + + TimedatedTimezone tz(TIMEZONE_FILE); ASSERT_EQ(expected_timezone, tz.timezone.get()); } +/** + * Test that timezone-timedated picks up the initial value + */ +TEST_F(TimezoneFixture, InitialValue) +{ + const std::string expected_timezone = "America/Chicago"; + set_file(expected_timezone); + TimedatedTimezone tz(TIMEZONE_FILE); +} + /** * Test that changing the tz after we are running works. */ -TEST_F(TimedateFixture, ChangedValue) +TEST_F(TimezoneFixture, ChangedValue) { const std::string initial_timezone = "America/Chicago"; const std::string changed_timezone = "America/New_York"; - GMainLoop *l = g_main_loop_new(nullptr, FALSE); - set_timezone(initial_timezone); + set_file(initial_timezone); - TimedatedTimezone tz; + TimedatedTimezone tz(TIMEZONE_FILE); ASSERT_EQ(initial_timezone, tz.timezone.get()); bool changed = false; tz.timezone.changed().connect( - [&changed, this, l](const std::string& s){ + [&changed, this](const std::string& s){ g_message("timezone changed to %s", s.c_str()); changed = true; - g_main_loop_quit(l); + g_main_loop_quit(loop); }); g_idle_add([](gpointer gself){ @@ -66,8 +122,20 @@ TEST_F(TimedateFixture, ChangedValue) return G_SOURCE_REMOVE; }, this); - g_main_loop_run(l); + g_main_loop_run(loop); ASSERT_TRUE(changed); ASSERT_EQ(changed_timezone, tz.timezone.get()); } + +/** + * Test that timezone-timedated picks up the initial value + */ +TEST_F(TimezoneFixture, IgnoreComments) +{ + const std::string comment = "# Created by cloud-init v. 0.7.5 on Thu, 24 Apr 2014 14:03:29 +0000"; + const std::string expected_timezone = "Europe/Berlin"; + set_file(comment + "\n" + expected_timezone); + TimedatedTimezone tz(TIMEZONE_FILE); + ASSERT_EQ(expected_timezone, tz.timezone.get()); +} diff --git a/tests/timedated-fixture.h b/tests/timedated-fixture.h index 0e89ea1..5ec425d 100644 --- a/tests/timedated-fixture.h +++ b/tests/timedated-fixture.h @@ -183,8 +183,6 @@ private: &local_error); g_assert_no_error (local_error); g_variant_unref(child); - - g_main_loop_quit(self->loop); } protected: @@ -215,6 +213,7 @@ protected: node_info = nullptr; object_register_id = 0; own_name = 0; + proxy = nullptr; // bring up the test bus bus = g_test_dbus_new(G_TEST_DBUS_NONE); -- cgit v1.2.3