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
|
/*
* 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/dbus-shared.h>
#include <datetime/timezone-timedated.h>
#include <gio/gio.h>
#include <cerrno>
#include <cstdlib>
namespace ayatana {
namespace indicator {
namespace datetime {
/***
****
***/
class TimedatedTimezone::Impl
{
public:
Impl(TimedatedTimezone& owner, GDBusConnection* connection):
m_owner{owner},
m_connection{G_DBUS_CONNECTION(g_object_ref(G_OBJECT(connection)))},
m_cancellable{g_cancellable_new()}
{
// set the fallback value
m_owner.timezone.set("Etc/Utc");
// watch for timedate1 on the bus
m_watcher_id = g_bus_watch_name_on_connection(
m_connection,
Bus::Timedate1::BUSNAME,
G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
on_timedate1_appeared,
on_timedate1_vanished,
this,
nullptr);
// listen for changed properties
m_signal_subscription_id = g_dbus_connection_signal_subscribe(
m_connection,
Bus::Timedate1::IFACE,
Bus::Properties::IFACE,
Bus::Properties::Signals::PROPERTIES_CHANGED,
Bus::Timedate1::ADDR,
nullptr,
G_DBUS_SIGNAL_FLAGS_NONE,
on_properties_changed,
this,
nullptr);
}
~Impl()
{
g_cancellable_cancel(m_cancellable);
g_clear_object(&m_cancellable);
g_bus_unwatch_name(m_watcher_id);
g_dbus_connection_signal_unsubscribe(m_connection, m_signal_subscription_id);
g_clear_object(&m_connection);
}
private:
static void on_timedate1_appeared(GDBusConnection * /*connection*/,
const gchar * name,
const gchar * /*name_owner*/,
gpointer gself)
{
g_debug("%s appeared on bus", name);
static_cast<Impl*>(gself)->ask_for_timezone();
}
static void on_timedate1_vanished(GDBusConnection * /*connection*/,
const gchar * name,
gpointer /*gself*/)
{
g_debug("%s not present on bus", name);
}
static void on_properties_changed(GDBusConnection * /*connection*/,
const gchar * /*sender_name*/,
const gchar * /*object_path*/,
const gchar * /*interface_name*/,
const gchar * /*signal_name*/,
GVariant * parameters,
gpointer gself)
{
auto self = static_cast<Impl*>(gself);
GVariant* changed_properties {};
gchar** invalidated_properties {};
g_variant_get(parameters, "(s@a{sv}^as)", NULL, &changed_properties, &invalidated_properties);
const char* tz {};
if (g_variant_lookup(changed_properties, Bus::Timedate1::Properties::TIMEZONE, "&s", &tz, NULL))
{
if (tz != nullptr)
self->set_timezone(tz);
else
g_warning("%s no timezone found", G_STRLOC);
}
else if (g_strv_contains(invalidated_properties, Bus::Timedate1::Properties::TIMEZONE))
{
self->ask_for_timezone();
}
g_variant_unref(changed_properties);
g_strfreev(invalidated_properties);
}
void ask_for_timezone()
{
g_dbus_connection_call(
m_connection,
Bus::Timedate1::BUSNAME,
Bus::Timedate1::ADDR,
Bus::Properties::IFACE,
Bus::Properties::Methods::GET,
g_variant_new("(ss)", Bus::Timedate1::IFACE, Bus::Timedate1::Properties::TIMEZONE),
G_VARIANT_TYPE("(v)"),
G_DBUS_CALL_FLAGS_NONE,
-1,
m_cancellable,
on_get_timezone_ready,
this);
}
static void on_get_timezone_ready(GObject * connection,
GAsyncResult * res,
gpointer gself)
{
GError* error {};
GVariant* v = g_dbus_connection_call_finish(G_DBUS_CONNECTION(connection), res, &error);
if (error != nullptr)
{
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
g_warning("%s Couldn't get timezone: %s", G_STRLOC, error->message);
}
else if (v != nullptr)
{
GVariant* tzv {};
g_variant_get(v, "(v)", &tzv);
const char* tz = g_variant_get_string(tzv, nullptr);
if (tz != nullptr)
static_cast<Impl*>(gself)->set_timezone(tz);
else
g_warning("%s no timezone found", G_STRLOC);
g_clear_pointer(&tzv, g_variant_unref);
g_clear_pointer(&v, g_variant_unref);
}
}
void set_timezone(const std::string& tz)
{
g_return_if_fail(!tz.empty());
g_debug("set timezone: '%s'", tz.c_str());
m_owner.timezone.set(tz);
}
/***
****
***/
TimedatedTimezone& m_owner;
GDBusConnection* m_connection {};
GCancellable* m_cancellable {};
unsigned long m_signal_subscription_id {};
unsigned int m_watcher_id {};
};
/***
****
***/
TimedatedTimezone::TimedatedTimezone(GDBusConnection* connection):
impl{new Impl{*this, connection}}
{
}
TimedatedTimezone::~TimedatedTimezone()
{
}
/***
****
***/
} // namespace datetime
} // namespace indicator
} // namespace ayatana
|