2
* Copyright 2013 Canonical Ltd.
5
* Charles Kerr <charles.kerr@canonical.com>
7
* This program is free software: you can redistribute it and/or modify it
8
* under the terms of the GNU General Public License version 3, as published
9
* by the Free Software Foundation.
11
* This program is distributed in the hope that it will be useful, but
12
* WITHOUT ANY WARRANTY; without even the implied warranties of
13
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
14
* PURPOSE. See the GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License along
17
* with this program. If not, see <http://www.gnu.org/licenses/>.
20
#include <datetime/timezone-geoclue.h>
22
#define GEOCLUE_BUS_NAME "org.freedesktop.Geoclue.Master"
29
GeoclueTimezone::GeoclueTimezone():
30
m_cancellable(g_cancellable_new())
32
g_bus_get(G_BUS_TYPE_SESSION, m_cancellable, on_bus_got, this);
35
GeoclueTimezone::~GeoclueTimezone()
37
g_cancellable_cancel(m_cancellable);
38
g_object_unref(m_cancellable);
40
if (m_signal_subscription)
41
g_dbus_connection_signal_unsubscribe(m_connection, m_signal_subscription);
43
g_object_unref(m_connection);
51
GeoclueTimezone::on_bus_got(GObject* /*source*/,
56
GDBusConnection * connection;
59
connection = g_bus_get_finish(res, &error);
62
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
63
g_warning("Couldn't get bus: %s", error->message);
69
auto self = static_cast<GeoclueTimezone*>(gself);
71
self->m_connection = connection;
73
g_dbus_connection_call(self->m_connection,
75
"/org/freedesktop/Geoclue/Master",
76
"org.freedesktop.Geoclue.Master",
78
nullptr, // parameters
79
G_VARIANT_TYPE("(o)"),
80
G_DBUS_CALL_FLAGS_NONE,
89
GeoclueTimezone::on_client_created(GObject * source, GAsyncResult * res, gpointer gself)
93
if ((result = call_finish(source, res)))
95
auto self = static_cast<GeoclueTimezone*>(gself);
97
GVariant * child = g_variant_get_child_value(result, 0);
98
self->m_client_object_path = g_variant_get_string(child, nullptr);
99
g_variant_unref(child);
100
g_variant_unref(result);
102
self->m_signal_subscription = g_dbus_connection_signal_subscribe(
105
"org.freedesktop.Geoclue.Address", // inteface
106
"AddressChanged", // signal name
107
self->m_client_object_path.c_str(), // object path
109
G_DBUS_SIGNAL_FLAGS_NONE,
114
g_dbus_connection_call(self->m_connection,
116
self->m_client_object_path.c_str(),
117
"org.freedesktop.Geoclue.MasterClient",
119
g_variant_new("(iibi)", 2, 0, FALSE, 1023),
121
G_DBUS_CALL_FLAGS_NONE,
130
GeoclueTimezone::on_address_changed(GDBusConnection* /*connection*/,
131
const gchar* /*sender_name*/,
132
const gchar* /*object_path*/,
133
const gchar* /*interface_name*/,
134
const gchar* /*signal_name*/,
135
GVariant* parameters,
138
static_cast<GeoclueTimezone*>(gself)->setTimezoneFromAddressVariant(parameters);
142
GeoclueTimezone::on_requirements_set(GObject* source, GAsyncResult* res, gpointer gself)
146
if ((result = call_finish(source, res)))
148
auto self = static_cast<GeoclueTimezone*>(gself);
150
g_dbus_connection_call(self->m_connection,
152
self->m_client_object_path.c_str(),
153
"org.freedesktop.Geoclue.MasterClient",
157
G_DBUS_CALL_FLAGS_NONE,
163
g_variant_unref(result);
168
GeoclueTimezone::on_address_started(GObject * source, GAsyncResult * res, gpointer gself)
172
if ((result = call_finish(source, res)))
174
auto self = static_cast<GeoclueTimezone*>(gself);
176
g_dbus_connection_call(self->m_connection,
178
self->m_client_object_path.c_str(),
179
"org.freedesktop.Geoclue.Address",
182
G_VARIANT_TYPE("(ia{ss}(idd))"),
183
G_DBUS_CALL_FLAGS_NONE,
189
g_variant_unref(result);
194
GeoclueTimezone::on_address_got(GObject * source, GAsyncResult * res, gpointer gself)
198
if ((result = call_finish(source, res)))
200
static_cast<GeoclueTimezone*>(gself)->setTimezoneFromAddressVariant(result);
201
g_variant_unref(result);
206
GeoclueTimezone::setTimezoneFromAddressVariant(GVariant * variant)
208
g_return_if_fail(g_variant_is_of_type(variant, G_VARIANT_TYPE("(ia{ss}(idd))")));
210
const gchar * timezone_string = nullptr;
211
GVariant * dict = g_variant_get_child_value(variant, 1);
214
if (g_variant_lookup(dict, "timezone", "&s", &timezone_string))
215
timezone.set(timezone_string);
217
g_variant_unref(dict);
222
GeoclueTimezone::call_finish(GObject * source, GAsyncResult * res)
228
result = g_dbus_connection_call_finish(G_DBUS_CONNECTION(source), res, &error);
232
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
233
g_warning("AddressStart() failed: %s", error->message);
237
g_clear_pointer(&result, g_variant_unref);
247
} // namespace datetime
248
} // namespace indicator