aboutsummaryrefslogtreecommitdiff
path: root/src/locations-settings.cpp
blob: 646a360d52bc8c3be4335604f1bf251cd6ba9f60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * 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 <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Charles Kerr <charles.kerr@canonical.com>
 */

#include <datetime/locations-settings.h>

#include <datetime/settings-shared.h>
#include <datetime/timezones.h>
#include <datetime/utils.h>

#include <algorithm> // std::find()

namespace unity {
namespace indicator {
namespace datetime {

SettingsLocations::SettingsLocations(const std::string& schemaId,
                                     const std::shared_ptr<Timezones>& timezones):
    m_timezones(timezones)
{
    auto deleter = [](GSettings* s){g_object_unref(s);};
    m_settings = std::unique_ptr<GSettings,std::function<void(GSettings*)>>(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<std::string>&){reload();});

    reload();
}

void
SettingsLocations::onSettingsChanged(gpointer gself)
{
    static_cast<SettingsLocations*>(gself)->reload();
}

void
SettingsLocations::reload()
{
    std::vector<Location> v;
    auto settings = m_settings.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);
        Location l(zone, name);
        v.push_back(l);
        g_free(name);
    }

    // add the other detected timezones
    for (const auto& zone : m_timezones->timezones.get())
    {
        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, SETTINGS_SHOW_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);
        }

        g_strfreev(user_locations);
    }

    locations.set(v);
}

} // namespace datetime
} // namespace indicator
} // namespace unity