aboutsummaryrefslogtreecommitdiff
path: root/src/timezone-timedated.cpp
blob: a2918f040496196b9923e424a462c4c66db71313 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * 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/timezone-timedated.h>

#include <gio/gio.h>

#include <cerrno>
#include <cstdlib>

namespace unity {
namespace indicator {
namespace datetime {

/***
****
***/

class TimedatedTimezone::Impl
{
public:

    Impl(TimedatedTimezone& owner, std::string filename):
        m_owner(owner),
        m_filename(filename)
    {
        g_debug("Filename is '%s'", filename.c_str());
        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;
        }

        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<Impl*>(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<Impl*>(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);
    }

    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<Impl*>(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()
    {
        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_NONE,
                on_name_appeared,
                on_name_vanished,
                this,
                nullptr);

        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;
    }

    /***
    ****
    ***/

    TimedatedTimezone & m_owner;
    unsigned long m_properties_changed_id = 0;
    unsigned long m_bus_watch_id = 0;
    GDBusProxy *m_proxy = nullptr;
    std::string m_filename;
};

/***
****
***/

TimedatedTimezone::TimedatedTimezone(std::string filename):
    impl(new Impl{*this, filename})
{
}

TimedatedTimezone::~TimedatedTimezone()
{
}

/***
****
***/

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