From db7b874bbf1a98f85ffe511be230164c40fe080d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 17 Dec 2013 22:03:43 -0600 Subject: add locations + tests --- src/locations-settings.cpp | 105 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/locations-settings.cpp (limited to 'src/locations-settings.cpp') diff --git a/src/locations-settings.cpp b/src/locations-settings.cpp new file mode 100644 index 0000000..ed8f998 --- /dev/null +++ b/src/locations-settings.cpp @@ -0,0 +1,105 @@ +/* + * 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 + +#include // std::find() + +namespace unity { +namespace indicator { +namespace datetime { + +SettingsLocations::SettingsLocations (const std::string& schemaId, + const std::shared_ptr& timezones): + timezones_(timezones) +{ + auto deleter = [&](GSettings* s){g_object_unref(s);}; + settings_ = std::unique_ptr>(g_settings_new(schemaId.c_str()), deleter); + const char * keys[] = { "changed::" SETTINGS_LOCATIONS_S, + "changed::" SETTINGS_SHOW_LOCATIONS_S }; + for (int i=0, n=G_N_ELEMENTS(keys); itimezone.changed().connect([this](const std::string&){reload();}); + 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; + + // add the primary timezone first + std::string zone = timezones_->timezone.get(); + if (!zone.empty()) + { + gchar * name = get_current_zone_name (zone.c_str(), settings_.get()); + Location l (zone, name); + v.push_back (l); + g_free (name); + } + + // add the other detected timezones + for (const auto& zone : timezones_->timezones.get()) + { + gchar * name = get_current_zone_name (zone.c_str(), settings_.get()); + Location l (zone, name); + if (std::find (v.begin(), v.end(), l) == v.end()) + v.push_back (l); + g_free (name); + } + + // maybe add the user-specified locations + if (g_settings_get_boolean (settings_.get(), SETTINGS_SHOW_LOCATIONS_S)) + { + gchar ** user_locations = g_settings_get_strv (settings_.get(), SETTINGS_LOCATIONS_S); + + for (int i=0; user_locations[i]; i++) + { + 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); + g_free (name); + g_free (zone); + } + + g_strfreev (user_locations); + } + + locations.set (v); +} + +} // namespace datetime +} // namespace indicator +} // namespace unity -- cgit v1.2.3 From ee64bb2698adfe27e55615a8856b0e2c78ad8469 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 14 Jan 2014 23:07:10 -0600 Subject: Function: add fully-tested ActionGroups, per-profile Menus, state object. Form: Add code annotations/comments. Remove dead code. Use Mir style guide. Todo: GSettings toggles, sync with new dbus-test-runner API, get GNOME Panel building again --- src/locations-settings.cpp | 59 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 29 deletions(-) (limited to 'src/locations-settings.cpp') diff --git a/src/locations-settings.cpp b/src/locations-settings.cpp index ed8f998..646a360 100644 --- a/src/locations-settings.cpp +++ b/src/locations-settings.cpp @@ -29,16 +29,16 @@ namespace unity { namespace indicator { namespace datetime { -SettingsLocations::SettingsLocations (const std::string& schemaId, - const std::shared_ptr& timezones): - timezones_(timezones) +SettingsLocations::SettingsLocations(const std::string& schemaId, + const std::shared_ptr& timezones): + m_timezones(timezones) { - auto deleter = [&](GSettings* s){g_object_unref(s);}; - settings_ = std::unique_ptr>(g_settings_new(schemaId.c_str()), deleter); + 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 (int i=0, n=G_N_ELEMENTS(keys); itimezone.changed().connect([this](const std::string&){reload();}); timezones->timezones.changed().connect([this](const std::set&){reload();}); @@ -47,7 +47,7 @@ SettingsLocations::SettingsLocations (const std::string& schemaId, } void -SettingsLocations::onSettingsChanged (gpointer gself) +SettingsLocations::onSettingsChanged(gpointer gself) { static_cast(gself)->reload(); } @@ -56,48 +56,49 @@ void SettingsLocations::reload() { std::vector v; + auto settings = m_settings.get(); // add the primary timezone first - std::string zone = timezones_->timezone.get(); + auto zone = m_timezones->timezone.get(); if (!zone.empty()) { - gchar * name = get_current_zone_name (zone.c_str(), settings_.get()); - Location l (zone, name); - v.push_back (l); - g_free (name); + gchar * name = get_current_zone_name(zone.c_str(), settings); + Location l(zone, name); + v.push_back(l); + g_free(name); } // add the other detected timezones - for (const auto& zone : timezones_->timezones.get()) + for (const auto& zone : m_timezones->timezones.get()) { - gchar * name = get_current_zone_name (zone.c_str(), settings_.get()); - Location l (zone, name); - if (std::find (v.begin(), v.end(), l) == v.end()) - v.push_back (l); - g_free (name); + gchar * name = get_current_zone_name(zone.c_str(), settings); + Location l(zone, name); + if (std::find(v.begin(), v.end(), l) == v.end()) + v.push_back(l); + g_free(name); } // maybe add the user-specified locations - if (g_settings_get_boolean (settings_.get(), SETTINGS_SHOW_LOCATIONS_S)) + if (g_settings_get_boolean(settings, SETTINGS_SHOW_LOCATIONS_S)) { - gchar ** user_locations = g_settings_get_strv (settings_.get(), SETTINGS_LOCATIONS_S); + gchar ** user_locations = g_settings_get_strv(settings, SETTINGS_LOCATIONS_S); for (int i=0; user_locations[i]; i++) { 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); - g_free (name); - g_free (zone); + 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); + g_free(name); + g_free(zone); } - g_strfreev (user_locations); + g_strfreev(user_locations); } - locations.set (v); + locations.set(v); } } // namespace datetime -- cgit v1.2.3 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. --- src/locations-settings.cpp | 50 +++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) (limited to 'src/locations-settings.cpp') 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); -- cgit v1.2.3 From 197309468247c893bdaa37ef47a98db695b2ea78 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 30 Jan 2014 13:44:12 -0600 Subject: following on the review comment covered in the last commit, use shared_ptr instead of shared_ptr where possible. --- src/locations-settings.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/locations-settings.cpp') diff --git a/src/locations-settings.cpp b/src/locations-settings.cpp index 9b90bc0..ef3085f 100644 --- a/src/locations-settings.cpp +++ b/src/locations-settings.cpp @@ -29,8 +29,8 @@ namespace unity { namespace indicator { namespace datetime { -SettingsLocations::SettingsLocations(const std::shared_ptr& settings, - const std::shared_ptr& timezones): +SettingsLocations::SettingsLocations(const std::shared_ptr& settings, + const std::shared_ptr& timezones): m_settings(settings), m_timezones(timezones) { -- cgit v1.2.3