From 9c81a4d60d06b1f33001602cd0cde9844c9233a6 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 16 Jan 2014 16:44:06 -0600 Subject: update SettingsLocations class to use the "Settings" class instead of using GSettings directly. --- include/datetime/locations-settings.h | 18 ++--- src/locations-settings.cpp | 50 +++++-------- tests/test-locations.cc | 133 +++++++++++++++++----------------- 3 files changed, 89 insertions(+), 112 deletions(-) diff --git a/include/datetime/locations-settings.h b/include/datetime/locations-settings.h index eaabf73..4072736 100644 --- a/include/datetime/locations-settings.h +++ b/include/datetime/locations-settings.h @@ -22,18 +22,15 @@ #include // base class -#include -#include +#include +#include namespace unity { namespace indicator { namespace datetime { -class Timezones; - /** - * \brief Settings implentation which builds its list from the - * user's GSettings and from the Timezones passed in the ctor. + * \brief #Locations implementation which builds its list from the #Settings. */ class SettingsLocations: public Locations { @@ -42,15 +39,12 @@ public: * @param[in] schemaId the settings schema to load * @param[in] timezones the timezones to always show first in the list */ - SettingsLocations (const std::string& schemaId, + SettingsLocations (const std::shared_ptr& settings, const std::shared_ptr& timezones); -protected: - std::unique_ptr> m_settings; - std::shared_ptr m_timezones; - private: - static void onSettingsChanged (gpointer gself); + std::shared_ptr m_settings; + std::shared_ptr m_timezones; void reload(); }; diff --git a/src/locations-settings.cpp b/src/locations-settings.cpp index 646a360..9b90bc0 100644 --- a/src/locations-settings.cpp +++ b/src/locations-settings.cpp @@ -29,49 +29,39 @@ namespace unity { namespace indicator { namespace datetime { -SettingsLocations::SettingsLocations(const std::string& schemaId, +SettingsLocations::SettingsLocations(const std::shared_ptr& settings, const std::shared_ptr& timezones): + m_settings(settings), m_timezones(timezones) { - auto deleter = [](GSettings* s){g_object_unref(s);}; - m_settings = std::unique_ptr>(g_settings_new(schemaId.c_str()), deleter); - const char * keys[] = { "changed::" SETTINGS_LOCATIONS_S, - "changed::" SETTINGS_SHOW_LOCATIONS_S }; - for (const auto& key : keys) - g_signal_connect_swapped(m_settings.get(), key, G_CALLBACK(onSettingsChanged), this); - - timezones->timezone.changed().connect([this](const std::string&){reload();}); - timezones->timezones.changed().connect([this](const std::set&){reload();}); + m_settings->locations.changed().connect([this](const std::vector&){reload();}); + m_settings->show_locations.changed().connect([this](bool){reload();}); + m_timezones->timezone.changed().connect([this](const std::string&){reload();}); + m_timezones->timezones.changed().connect([this](const std::set&){reload();}); reload(); } -void -SettingsLocations::onSettingsChanged(gpointer gself) -{ - static_cast(gself)->reload(); -} - void SettingsLocations::reload() { std::vector v; - auto settings = m_settings.get(); + const std::string timezone_name = m_settings->timezone_name.get(); // add the primary timezone first auto zone = m_timezones->timezone.get(); if (!zone.empty()) { - gchar * name = get_current_zone_name(zone.c_str(), settings); + gchar * name = get_beautified_timezone_name(zone.c_str(), timezone_name.c_str()); Location l(zone, name); v.push_back(l); g_free(name); } // add the other detected timezones - for (const auto& zone : m_timezones->timezones.get()) + for(const auto& zone : m_timezones->timezones.get()) { - gchar * name = get_current_zone_name(zone.c_str(), settings); + gchar * name = get_beautified_timezone_name(zone.c_str(), timezone_name.c_str()); Location l(zone, name); if (std::find(v.begin(), v.end(), l) == v.end()) v.push_back(l); @@ -79,23 +69,19 @@ SettingsLocations::reload() } // maybe add the user-specified locations - if (g_settings_get_boolean(settings, SETTINGS_SHOW_LOCATIONS_S)) + if (m_settings->show_locations.get()) { - gchar ** user_locations = g_settings_get_strv(settings, SETTINGS_LOCATIONS_S); - - for (int i=0; user_locations[i]; i++) + for(const auto& locstr : m_settings->locations.get()) { - gchar * zone; - gchar * name; - split_settings_location(user_locations[i], &zone, &name); - Location l(zone, name); - if (std::find(v.begin(), v.end(), l) == v.end()) - v.push_back(l); + gchar* zone; + gchar* name; + split_settings_location(locstr.c_str(), &zone, &name); + Location loc(zone, name); + if (std::find(v.begin(), v.end(), loc) == v.end()) + v.push_back(loc); g_free(name); g_free(zone); } - - g_strfreev(user_locations); } locations.set(v); diff --git a/tests/test-locations.cc b/tests/test-locations.cc index edaac69..65adbc7 100644 --- a/tests/test-locations.cc +++ b/tests/test-locations.cc @@ -21,18 +21,11 @@ #include "glib-fixture.h" -#include -#include #include -#include - -#include - -#include -#include using unity::indicator::datetime::Location; using unity::indicator::datetime::Locations; +using unity::indicator::datetime::Settings; using unity::indicator::datetime::SettingsLocations; using unity::indicator::datetime::Timezones; @@ -48,8 +41,9 @@ class LocationsFixture: public GlibFixture protected: - GSettings * settings = nullptr; - std::shared_ptr timezones; + //GSettings * settings = nullptr; + std::shared_ptr m_settings; + std::shared_ptr m_timezones; const std::string nyc = "America/New_York"; const std::string chicago = "America/Chicago"; @@ -57,20 +51,24 @@ class LocationsFixture: public GlibFixture { super::SetUp(); - settings = g_settings_new(SETTINGS_INTERFACE); - const gchar * location_strv[] = { "America/Los_Angeles Oakland", "America/Chicago Chicago", "America/Chicago Oklahoma City", "America/Toronto Toronto", "Europe/London London", "Europe/Berlin Berlin", NULL }; - g_settings_set_strv(settings, SETTINGS_LOCATIONS_S, location_strv); - g_settings_set_boolean(settings, SETTINGS_SHOW_LOCATIONS_S, true); - - timezones.reset(new Timezones); - timezones->timezone.set(chicago); - timezones->timezones.set(std::set({ nyc, chicago })); + m_settings.reset(new Settings); + m_settings->show_locations.set(true); + m_settings->locations.set({"America/Los_Angeles Oakland", + "America/Chicago Chicago", + "America/Chicago Oklahoma City", + "America/Toronto Toronto", + "Europe/London London", + "Europe/Berlin Berlin"}); + + m_timezones.reset(new Timezones); + m_timezones->timezone.set(chicago); + m_timezones->timezones.set(std::set({ nyc, chicago })); } virtual void TearDown() { - //timezones.reset(nullptr); - g_clear_object(&settings); + m_timezones.reset(); + m_settings.reset(); super::TearDown(); } @@ -78,42 +76,42 @@ class LocationsFixture: public GlibFixture TEST_F(LocationsFixture, Timezones) { - g_settings_set_boolean(settings, SETTINGS_SHOW_LOCATIONS_S, false); + m_settings->show_locations.set(false); - SettingsLocations locations(SETTINGS_INTERFACE, timezones); - std::vector l = locations.locations.get(); + SettingsLocations locations(m_settings, m_timezones); + const auto l = locations.locations.get(); EXPECT_EQ(2, l.size()); - EXPECT_EQ("Chicago", l[0].name); - EXPECT_EQ(chicago, l[0].zone); - EXPECT_EQ("New York", l[1].name); - EXPECT_EQ(nyc, l[1].zone); + EXPECT_STREQ("Chicago", l[0].name().c_str()); + EXPECT_EQ(chicago, l[0].zone()); + EXPECT_EQ("New York", l[1].name()); + EXPECT_EQ(nyc, l[1].zone()); } TEST_F(LocationsFixture, SettingsLocations) { - SettingsLocations locations(SETTINGS_INTERFACE, timezones); + SettingsLocations locations(m_settings, m_timezones); - std::vector l = locations.locations.get(); + const auto l = locations.locations.get(); EXPECT_EQ(7, l.size()); - EXPECT_EQ("Chicago", l[0].name); - EXPECT_EQ(chicago, l[0].zone); - EXPECT_EQ("New York", l[1].name); - EXPECT_EQ(nyc, l[1].zone); - EXPECT_EQ("Oakland", l[2].name); - EXPECT_EQ("America/Los_Angeles", l[2].zone); - EXPECT_EQ("Oklahoma City", l[3].name); - EXPECT_EQ("America/Chicago", l[3].zone); - EXPECT_EQ("Toronto", l[4].name); - EXPECT_EQ("America/Toronto", l[4].zone); - EXPECT_EQ("London", l[5].name); - EXPECT_EQ("Europe/London", l[5].zone); - EXPECT_EQ("Berlin", l[6].name); - EXPECT_EQ("Europe/Berlin", l[6].zone); + EXPECT_EQ("Chicago", l[0].name()); + EXPECT_EQ(chicago, l[0].zone()); + EXPECT_EQ("New York", l[1].name()); + EXPECT_EQ(nyc, l[1].zone()); + EXPECT_EQ("Oakland", l[2].name()); + EXPECT_EQ("America/Los_Angeles", l[2].zone()); + EXPECT_EQ("Oklahoma City", l[3].name()); + EXPECT_EQ("America/Chicago", l[3].zone()); + EXPECT_EQ("Toronto", l[4].name()); + EXPECT_EQ("America/Toronto", l[4].zone()); + EXPECT_EQ("London", l[5].name()); + EXPECT_EQ("Europe/London", l[5].zone()); + EXPECT_EQ("Berlin", l[6].name()); + EXPECT_EQ("Europe/Berlin", l[6].zone()); } TEST_F(LocationsFixture, ChangeLocationStrings) { - SettingsLocations locations(SETTINGS_INTERFACE, timezones); + SettingsLocations locations(m_settings, m_timezones); bool locations_changed = false; locations.locations.changed().connect([&locations_changed, this](const std::vector&){ @@ -121,33 +119,32 @@ TEST_F(LocationsFixture, ChangeLocationStrings) g_main_loop_quit(loop); }); - g_idle_add([](gpointer gsettings){ - const gchar * strv[] = { "America/Los_Angeles Oakland", "Europe/London London", "Europe/Berlin Berlin", NULL }; - g_settings_set_strv(static_cast(gsettings), SETTINGS_LOCATIONS_S, strv); + g_idle_add([](gpointer settings){ + static_cast(settings)->locations.set({"America/Los_Angeles Oakland", "Europe/London London", "Europe/Berlin Berlin"}); return G_SOURCE_REMOVE; - }, settings); + }, m_settings.get()); g_main_loop_run(loop); EXPECT_TRUE(locations_changed); - std::vector l = locations.locations.get(); + const auto l = locations.locations.get(); EXPECT_EQ(5, l.size()); - EXPECT_EQ("Chicago", l[0].name); - EXPECT_EQ(chicago, l[0].zone); - EXPECT_EQ("New York", l[1].name); - EXPECT_EQ(nyc, l[1].zone); - EXPECT_EQ("Oakland", l[2].name); - EXPECT_EQ("America/Los_Angeles", l[2].zone); - EXPECT_EQ("London", l[3].name); - EXPECT_EQ("Europe/London", l[3].zone); - EXPECT_EQ("Berlin", l[4].name); - EXPECT_EQ("Europe/Berlin", l[4].zone); + EXPECT_EQ("Chicago", l[0].name()); + EXPECT_EQ(chicago, l[0].zone()); + EXPECT_EQ("New York", l[1].name()); + EXPECT_EQ(nyc, l[1].zone()); + EXPECT_EQ("Oakland", l[2].name()); + EXPECT_EQ("America/Los_Angeles", l[2].zone()); + EXPECT_EQ("London", l[3].name()); + EXPECT_EQ("Europe/London", l[3].zone()); + EXPECT_EQ("Berlin", l[4].name()); + EXPECT_EQ("Europe/Berlin", l[4].zone()); locations_changed = false; } TEST_F(LocationsFixture, ChangeLocationVisibility) { - SettingsLocations locations(SETTINGS_INTERFACE, timezones); + SettingsLocations locations(m_settings, m_timezones); bool locations_changed = false; locations.locations.changed().connect([&locations_changed, this](const std::vector&){ @@ -155,18 +152,18 @@ TEST_F(LocationsFixture, ChangeLocationVisibility) g_main_loop_quit(loop); }); - g_idle_add([](gpointer gsettings){ - g_settings_set_boolean(static_cast(gsettings), SETTINGS_SHOW_LOCATIONS_S, false); + g_idle_add([](gpointer settings){ + static_cast(settings)->show_locations.set(false); return G_SOURCE_REMOVE; - }, settings); + }, m_settings.get()); g_main_loop_run(loop); EXPECT_TRUE(locations_changed); - std::vector l = locations.locations.get(); + const auto l = locations.locations.get(); EXPECT_EQ(2, l.size()); - EXPECT_EQ("Chicago", l[0].name); - EXPECT_EQ(chicago, l[0].zone); - EXPECT_EQ("New York", l[1].name); - EXPECT_EQ(nyc, l[1].zone); + EXPECT_EQ("Chicago", l[0].name()); + EXPECT_EQ(chicago, l[0].zone()); + EXPECT_EQ("New York", l[1].name()); + EXPECT_EQ(nyc, l[1].zone()); } -- cgit v1.2.3