gclue-service-client.c 37.5 KB
Newer Older
Zeeshan Ali's avatar
Zeeshan Ali committed
1 2 3
/* vim: set et ts=8 sw=8: */
/* gclue-service-client.c
 *
4
 * Copyright 2013 Red Hat, Inc.
Zeeshan Ali's avatar
Zeeshan Ali committed
5
 *
6 7 8 9
 * Geoclue is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
Zeeshan Ali's avatar
Zeeshan Ali committed
10
 *
11 12 13 14
 * Geoclue is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
Zeeshan Ali's avatar
Zeeshan Ali committed
15
 *
16 17 18
 * You should have received a copy of the GNU General Public License along
 * with Geoclue; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Zeeshan Ali's avatar
Zeeshan Ali committed
19 20 21 22 23 24 25
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 */

#include <glib/gi18n.h>

#include "gclue-service-client.h"
26
#include "gclue-service-location.h"
27
#include "gclue-locator.h"
28
#include "gclue-enum-types.h"
29
#include "gclue-config.h"
30 31

#define DEFAULT_ACCURACY_LEVEL GCLUE_ACCURACY_LEVEL_CITY
32
#define DEFAULT_AGENT_STARTUP_WAIT_SECS 5
Zeeshan Ali's avatar
Zeeshan Ali committed
33 34

static void
35
gclue_service_client_client_iface_init (GClueDBusClientIface *iface);
36 37
static void
gclue_service_client_initable_iface_init (GInitableIface *iface);
Zeeshan Ali's avatar
Zeeshan Ali committed
38

39 40
typedef struct _StartData StartData;

Zeeshan Ali's avatar
Zeeshan Ali committed
41 42
struct _GClueServiceClientPrivate
{
43
        GClueClientInfo *client_info;
44
        char *path;
45
        GDBusConnection *connection;
46
        GClueAgent *agent_proxy;
47 48
        StartData *pending_auth_start_data;
        guint pending_auth_timeout_id;
49 50 51

        GClueServiceLocation *location;
        GClueServiceLocation *prev_location;
52
        guint distance_threshold;
53
        guint time_threshold;
54

55 56
        GClueLocator *locator;

57 58
        /* Number of times location has been updated */
        guint locations_updated;
59 60

        gboolean agent_stopped; /* Agent stopped client, not the app */
Zeeshan Ali's avatar
Zeeshan Ali committed
61 62
};

63 64 65 66 67 68 69 70 71
G_DEFINE_TYPE_WITH_CODE (GClueServiceClient,
                         gclue_service_client,
                         GCLUE_DBUS_TYPE_CLIENT_SKELETON,
                         G_IMPLEMENT_INTERFACE (GCLUE_DBUS_TYPE_CLIENT,
                                                gclue_service_client_client_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                gclue_service_client_initable_iface_init)
                         G_ADD_PRIVATE (GClueServiceClient));

Zeeshan Ali's avatar
Zeeshan Ali committed
72 73 74
enum
{
        PROP_0,
75
        PROP_CLIENT_INFO,
Zeeshan Ali's avatar
Zeeshan Ali committed
76
        PROP_PATH,
77
        PROP_CONNECTION,
78
        PROP_AGENT_PROXY,
Zeeshan Ali's avatar
Zeeshan Ali committed
79 80 81 82 83
        LAST_PROP
};

static GParamSpec *gParamSpecs[LAST_PROP];

84 85 86 87 88 89
static char *
next_location_path (GClueServiceClient *client)
{
        GClueServiceClientPrivate *priv = client->priv;
        char *path, *index_str;

90
        index_str = g_strdup_printf ("%u", (priv->locations_updated)++),
91 92 93 94 95 96
        path = g_strjoin ("/", priv->path, "Location", index_str, NULL);
        g_free (index_str);

        return path;
}

97 98 99 100 101 102 103 104 105 106 107
/* We don't use the gdbus-codegen provided gclue_client_emit_location_updated()
 * as that sends the signal to all listeners on the bus
 */
static gboolean
emit_location_updated (GClueServiceClient *client,
                       const char         *old,
                       const char         *new,
                       GError            **error)
{
        GClueServiceClientPrivate *priv = client->priv;
        GVariant *variant;
108
        const char *peer;
109 110

        variant = g_variant_new ("(oo)", old, new);
111
        peer = gclue_client_info_get_bus_name (priv->client_info);
112 113

        return g_dbus_connection_emit_signal (priv->connection,
114
                                              peer,
115 116 117 118 119 120 121 122
                                              priv->path,
                                              "org.freedesktop.GeoClue2.Client",
                                              "LocationUpdated",
                                              variant,
                                              error);
}

static gboolean
123 124
distance_below_threshold (GClueServiceClient *client,
                          GClueLocation      *location)
125 126
{
        GClueServiceClientPrivate *priv = client->priv;
Ankit's avatar
Ankit committed
127
        GClueLocation *cur_location;
128 129
        gdouble distance;
        gdouble threshold_km;
130

131
        if (priv->distance_threshold == 0)
132
                return FALSE;
133

134 135 136
        g_object_get (priv->location,
                      "location", &cur_location,
                      NULL);
137
        distance = gclue_location_get_distance_from (cur_location, location);
138 139
        g_object_unref (cur_location);

140
        threshold_km = priv->distance_threshold / 1000.0;
141 142 143 144
        if (distance < threshold_km) {
                g_debug ("Distance from previous location is %f km and "
                         "below threshold of %f km.",
                         distance, threshold_km); 
145
                return TRUE;
146
        }
147

148
        return FALSE;
149 150
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
static gboolean
time_below_threshold (GClueServiceClient *client,
                      GClueLocation      *location)
{
        GClueServiceClientPrivate *priv = client->priv;
        GClueLocation *cur_location;
        gint64 cur_ts, ts;
        guint64 diff_ts;

        if (priv->time_threshold == 0)
                return FALSE;

        g_object_get (priv->location,
                      "location", &cur_location,
                      NULL);

167 168
        cur_ts = gclue_location_get_timestamp (cur_location);
        ts = gclue_location_get_timestamp (location);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        diff_ts = ABS (ts - cur_ts);

        g_object_unref (cur_location);

        if (diff_ts < priv->time_threshold) {
                g_debug ("Time difference between previous and new location"
                         " is %" G_GUINT64_FORMAT " seconds and"
                         " below threshold of %" G_GUINT32_FORMAT " seconds.",
                         diff_ts, priv->time_threshold);
                return TRUE;
        }

        return FALSE;
}

184 185 186 187
static gboolean
below_threshold (GClueServiceClient *client,
                 GClueLocation      *location)
{
188 189
        return (distance_below_threshold (client, location) ||
                time_below_threshold (client, location));
190 191
}

192 193 194 195 196 197 198 199
static gboolean
on_prev_location_timeout (gpointer user_data)
{
        g_object_unref (user_data);

        return FALSE;
}

200
static void
201 202 203
on_locator_location_changed (GObject    *gobject,
                             GParamSpec *pspec,
                             gpointer    user_data)
204
{
205 206
        GClueServiceClient *client = GCLUE_SERVICE_CLIENT (user_data);
        GClueServiceClientPrivate *priv = client->priv;
207
        GClueLocationSource *locator = GCLUE_LOCATION_SOURCE (gobject);
Ankit's avatar
Ankit committed
208
        GClueLocation *location_info;
209
        char *path = NULL;
210
        const char *prev_path;
211 212
        GError *error = NULL;

213
        location_info = gclue_location_source_get_location (locator);
214 215
        if (location_info == NULL)
                return; /* No location found yet */
216 217 218 219 220 221 222 223 224

        if (priv->location != NULL && below_threshold (client, location_info)) {
                g_debug ("Updating location, below threshold");
                g_object_set (priv->location,
                              "location", location_info,
                              NULL);
                return;
        }

225 226 227 228
        if (priv->prev_location != NULL)
                // Lets try to ensure that apps are not still accessing the
                // last location before unrefing (and therefore destroying) it.
                g_timeout_add_seconds (5, on_prev_location_timeout, priv->prev_location);
229 230
        priv->prev_location = priv->location;

231
        path = next_location_path (client);
232
        priv->location = gclue_service_location_new (priv->client_info,
233 234 235 236 237
                                                     path,
                                                     priv->connection,
                                                     location_info,
                                                     &error);
        if (priv->location == NULL)
238 239
                goto error_out;

240 241 242 243 244
        if (priv->prev_location != NULL)
                prev_path = gclue_service_location_get_path (priv->prev_location);
        else
                prev_path = "/";

245
        gclue_dbus_client_set_location (GCLUE_DBUS_CLIENT (client), path);
246 247

        if (!emit_location_updated (client, prev_path, path, &error))
248 249 250 251 252 253 254
                goto error_out;
        goto out;

error_out:
        g_warning ("Failed to update location info: %s", error->message);
        g_error_free (error);
out:
255
        g_free (path);
256 257
}

258 259 260 261 262
static void
start_client (GClueServiceClient *client, GClueAccuracyLevel accuracy_level)
{
        GClueServiceClientPrivate *priv = client->priv;

263
        gclue_dbus_client_set_active (GCLUE_DBUS_CLIENT (client), TRUE);
264
        priv->locator = gclue_locator_new (accuracy_level);
265
        gclue_locator_set_time_threshold (priv->locator, 0);
266 267 268 269 270
        g_signal_connect (priv->locator,
                          "notify::location",
                          G_CALLBACK (on_locator_location_changed),
                          client);

271
        gclue_location_source_start (GCLUE_LOCATION_SOURCE (priv->locator));
272 273 274 275 276 277
}

static void
stop_client (GClueServiceClient *client)
{
        g_clear_object (&client->priv->locator);
278
        gclue_dbus_client_set_active (GCLUE_DBUS_CLIENT (client), FALSE);
279 280
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
static GClueAccuracyLevel
ensure_valid_accuracy_level (GClueAccuracyLevel accuracy_level,
                             GClueAccuracyLevel max_accuracy)
{
        GClueAccuracyLevel accuracy;
        GEnumClass *enum_class;
        GEnumValue *enum_value;

        accuracy = CLAMP (accuracy_level,
                          GCLUE_ACCURACY_LEVEL_COUNTRY,
                          max_accuracy);

        enum_class = g_type_class_ref (GCLUE_TYPE_ACCURACY_LEVEL);
        enum_value = g_enum_get_value (enum_class, accuracy);

        if (enum_value == NULL) {
                GClueAccuracyLevel i;

                g_debug ("Invalid accuracy level %u requested", accuracy_level);
                for (i = accuracy; i >= GCLUE_ACCURACY_LEVEL_COUNTRY; i--) {
                        enum_value = g_enum_get_value (enum_class, i);

                        if (enum_value != NULL) {
                                accuracy = i;

                                break;
                        }
                }
        }

        return accuracy;
}

314 315 316 317 318 319 320
static void
on_agent_props_changed (GDBusProxy *agent_proxy,
                        GVariant   *changed_properties,
                        GStrv       invalidated_properties,
                        gpointer    user_data)
{
        GClueServiceClient *client = GCLUE_SERVICE_CLIENT (user_data);
321
        GClueDBusClient *gdbus_client;
322 323 324 325
        GVariantIter *iter;
        GVariant *value;
        gchar *key;
        
326
        if (g_variant_n_children (changed_properties) <= 0)
327 328 329 330 331 332
                return;

        g_variant_get (changed_properties, "a{sv}", &iter);
        while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) {
                GClueAccuracyLevel max_accuracy;
                const char *id;
333
                gboolean system_app;
334 335 336 337

                if (strcmp (key, "MaxAccuracyLevel") != 0)
                        continue;

338 339
                gdbus_client = GCLUE_DBUS_CLIENT (client);
                id = gclue_dbus_client_get_desktop_id (gdbus_client);
340
                max_accuracy = g_variant_get_uint32 (value);
341
                system_app = (gclue_client_info_get_xdg_id
342
                              (client->priv->client_info) == NULL);
343 344 345 346 347 348 349
                /* FIXME: We should be handling all values of max accuracy
                 *        level here, not just 0 and non-0.
                 */
                if (max_accuracy != 0 && client->priv->agent_stopped) {
                        GClueAccuracyLevel accuracy;

                        client->priv->agent_stopped = FALSE;
350 351
                        accuracy = gclue_dbus_client_get_requested_accuracy_level
                                (gdbus_client);
352
                        accuracy = ensure_valid_accuracy_level (accuracy, max_accuracy);
353 354 355 356

                        start_client (client, accuracy);
                        g_debug ("Re-started '%s'.", id);
                } else if (max_accuracy == 0 &&
357
                           gclue_dbus_client_get_active (gdbus_client) &&
358
                           !system_app) {
359 360 361 362 363 364 365 366 367 368
                        stop_client (client);
                        client->priv->agent_stopped = TRUE;
                        g_debug ("Stopped '%s'.", id);
                }

                break;
        }
        g_variant_iter_free (iter);
}

369
struct _StartData
370 371 372
{
        GClueServiceClient *client;
        GDBusMethodInvocation *invocation;
373 374
        char *desktop_id;
        GClueAccuracyLevel accuracy_level;
375
};
376

377 378 379 380 381
static void
start_data_free (StartData *data)
{
        g_object_unref (data->client);
        g_object_unref (data->invocation);
382
        g_free(data->desktop_id);
383 384 385
        g_slice_free (StartData, data);
}

386
static void
387
complete_start (StartData *data)
388
{
389
        GClueDBusClient *gdbus_client = GCLUE_DBUS_CLIENT (data->client);
390
        start_client (data->client, data->accuracy_level);
391

392 393
        gclue_dbus_client_complete_start (gdbus_client,
                                          data->invocation);
394
        g_debug ("'%s' started.",
395
                 gclue_dbus_client_get_desktop_id (gdbus_client));
396 397 398
        start_data_free (data);
}

399 400 401 402 403 404
static void
on_authorize_app_ready (GObject      *source_object,
                        GAsyncResult *res,
                        gpointer      user_data)
{
        StartData *data = (StartData *) user_data;
405
        GClueServiceClientPrivate *priv = data->client->priv;
406 407 408
        GError *error = NULL;
        gboolean authorized = FALSE;

409 410
        if (!gclue_agent_call_authorize_app_finish (GCLUE_AGENT (source_object),
                                                    &authorized,
411
                                                    &data->accuracy_level,
412 413 414
                                                    res,
                                                    &error))
                goto error_out;
415 416

        if (!authorized) {
417 418 419 420 421 422 423 424 425
                guint32 uid;

                uid = gclue_client_info_get_user_id (priv->client_info);

                g_set_error (&error,
                             G_DBUS_ERROR,
                             G_DBUS_ERROR_ACCESS_DENIED,
                             "Agent rejected '%s' for user '%u'. Please ensure "
                             "that '%s' has installed a valid %s.desktop file.",
426
                             data->desktop_id,
427
                             uid,
428 429
                             data->desktop_id,
                             data->desktop_id);
430 431 432
                goto error_out;
        }

433
        complete_start (data);
434 435 436 437 438 439

        return;

error_out:
        g_dbus_method_invocation_take_error (data->invocation, error);
        start_data_free (data);
440 441
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static void
handle_post_agent_check_auth (StartData *data)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (data->client)->priv;
        GClueAccuracyLevel max_accuracy;
        GClueConfig *config;
        GClueAppPerm app_perm;
        guint32 uid;

        uid = gclue_client_info_get_user_id (priv->client_info);
        max_accuracy = gclue_agent_get_max_accuracy_level (priv->agent_proxy);

        if (max_accuracy == 0) {
                // Agent disabled geolocation for the user
                g_dbus_method_invocation_return_error (data->invocation,
                                                       G_DBUS_ERROR,
                                                       G_DBUS_ERROR_ACCESS_DENIED,
                                                       "Geolocation disabled for"
                                                       " UID %u",
                                                       uid);
                start_data_free (data);
                return;
        }
        g_debug ("requested accuracy level: %u. "
                 "Max accuracy level allowed by agent: %u",
                 data->accuracy_level, max_accuracy);
468
        data->accuracy_level = ensure_valid_accuracy_level (data->accuracy_level, max_accuracy);
469 470 471 472 473 474

        config = gclue_config_get_singleton ();
        app_perm = gclue_config_get_app_perm (config,
                                              data->desktop_id,
                                              priv->client_info);

475
        if (app_perm == GCLUE_APP_PERM_ALLOWED) {
476 477 478 479 480 481 482 483 484 485 486 487
                complete_start (data);
                return;
        }

        gclue_agent_call_authorize_app (priv->agent_proxy,
                                        data->desktop_id,
                                        data->accuracy_level,
                                        NULL,
                                        on_authorize_app_ready,
                                        data);
}

488 489 490 491 492 493 494
static gboolean
handle_pending_auth (gpointer user_data)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (user_data)->priv;
        StartData *data = priv->pending_auth_start_data;
        guint32 uid;

495 496
        g_return_val_if_fail (data != NULL, G_SOURCE_REMOVE);

497 498
        uid = gclue_client_info_get_user_id (priv->client_info);
        if (priv->agent_proxy == NULL) {
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
                GClueConfig *config = gclue_config_get_singleton ();

                if (gclue_config_get_num_allowed_agents (config) == 0) {
                        /* If there are no white-listed agents, there is no
                         * point in requiring an agent */
                        complete_start (data);
                } else {
                        g_dbus_method_invocation_return_error
                                (data->invocation,
                                 G_DBUS_ERROR,
                                 G_DBUS_ERROR_ACCESS_DENIED,
                                 "'%s' disallowed, no agent "
                                 "for UID %u",
                                 data->desktop_id,
                                 uid);
                        start_data_free (data);
                }
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
        } else {
                handle_post_agent_check_auth (data);
        }

        priv->pending_auth_timeout_id = 0;
        priv->pending_auth_start_data = NULL;

        return G_SOURCE_REMOVE;
}

static void
set_pending_auth_timeout_enable (GClueDBusClient *client)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (client)->priv;

        if (priv->pending_auth_timeout_id > 0)
                return;
        priv->pending_auth_timeout_id = g_timeout_add_seconds
                (DEFAULT_AGENT_STARTUP_WAIT_SECS, handle_pending_auth, client);
}

static void
set_pending_auth_timeout_disable (GClueDBusClient *client)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (client)->priv;

        if (priv->pending_auth_timeout_id == 0)
                return;

        g_source_remove (priv->pending_auth_timeout_id);
        priv->pending_auth_timeout_id = 0;
}

Zeeshan Ali's avatar
Zeeshan Ali committed
549
static gboolean
550
gclue_service_client_handle_start (GClueDBusClient       *client,
551
                                   GDBusMethodInvocation *invocation)
Zeeshan Ali's avatar
Zeeshan Ali committed
552
{
553
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (client)->priv;
554
        GClueConfig *config;
555
        StartData *data;
556
        const char *desktop_id;
557 558
        GClueAppPerm app_perm;
        guint32 uid;
559
        gboolean system_app = FALSE;
560

561
        if (priv->locator != NULL) {
562
                /* Already started */
563 564
                gclue_dbus_client_complete_start (client, invocation);

565
                return TRUE;
566
        }
567

568
        desktop_id = gclue_client_info_get_xdg_id (priv->client_info);
569
        if (desktop_id == NULL) {
570
                /* Non-xdg app */
571
                desktop_id = gclue_dbus_client_get_desktop_id (client);
572 573
                system_app = TRUE;
        }
574

575 576 577 578 579 580
        if (desktop_id == NULL) {
                g_dbus_method_invocation_return_error_literal (invocation,
                                                               G_DBUS_ERROR,
                                                               G_DBUS_ERROR_ACCESS_DENIED,
                                                               "'DesktopId' property must be set");
                return TRUE;
581 582
        }

583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
        config = gclue_config_get_singleton ();
        uid = gclue_client_info_get_user_id (priv->client_info);
        app_perm = gclue_config_get_app_perm (config,
                                              desktop_id,
                                              priv->client_info);
        if (app_perm == GCLUE_APP_PERM_DISALLOWED) {
                g_dbus_method_invocation_return_error (invocation,
                                                       G_DBUS_ERROR,
                                                       G_DBUS_ERROR_ACCESS_DENIED,
                                                       "'%s' disallowed by "
                                                       "configuration for UID %u",
                                                       desktop_id,
                                                       uid);
                return TRUE;
        }

599
        data = g_slice_new (StartData);
600
        data->client = g_object_ref (GCLUE_SERVICE_CLIENT (client));
601
        data->invocation =  g_object_ref (invocation);
602
        data->desktop_id =  g_strdup (desktop_id);
603

604
        data->accuracy_level = gclue_dbus_client_get_requested_accuracy_level (client);
605 606
        data->accuracy_level = ensure_valid_accuracy_level
                (data->accuracy_level, GCLUE_ACCURACY_LEVEL_EXACT);
607

608 609 610 611 612 613 614 615
        if (system_app) {
                /* Since we have no reliable way to identify system apps, no
                 * need for auth for them. */
                complete_start (data);

                return TRUE;
        }

616 617
        /* No agent == No authorization */
        if (priv->agent_proxy == NULL) {
618 619 620 621 622 623 624 625 626 627 628
                /* Already a pending Start()? Denied! */
                if (priv->pending_auth_start_data) {
                        g_dbus_method_invocation_return_error_literal
                                (invocation,
                                 G_DBUS_ERROR,
                                 G_DBUS_ERROR_ACCESS_DENIED,
                                 "An authorization request is already pending");
                } else {
                        priv->pending_auth_start_data = data;
                        set_pending_auth_timeout_enable (client);
                }
629 630 631
                return TRUE;
        }

632
        handle_post_agent_check_auth (data);
Zeeshan Ali's avatar
Zeeshan Ali committed
633 634 635 636 637

        return TRUE;
}

static gboolean
638
gclue_service_client_handle_stop (GClueDBusClient       *client,
639
                                  GDBusMethodInvocation *invocation)
Zeeshan Ali's avatar
Zeeshan Ali committed
640
{
641
        stop_client (GCLUE_SERVICE_CLIENT (client));
642 643
        gclue_dbus_client_complete_stop (client, invocation);
        g_debug ("'%s' stopped.", gclue_dbus_client_get_desktop_id (client));
Zeeshan Ali's avatar
Zeeshan Ali committed
644 645 646 647 648 649 650 651 652

        return TRUE;
}

static void
gclue_service_client_finalize (GObject *object)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (object)->priv;

653
        g_clear_pointer (&priv->path, g_free);
654
        g_clear_object (&priv->connection);
655 656
        set_pending_auth_timeout_disable (GCLUE_DBUS_CLIENT (object));
        g_clear_pointer (&priv->pending_auth_start_data, start_data_free);
657 658 659 660 661
        if (priv->agent_proxy != NULL)
                g_signal_handlers_disconnect_by_func
                                (priv->agent_proxy,
                                 G_CALLBACK (on_agent_props_changed),
                                 object);
662
        g_clear_object (&priv->agent_proxy);
663
        g_clear_object (&priv->locator);
664 665
        g_clear_object (&priv->location);
        g_clear_object (&priv->prev_location);
666
        g_clear_object (&priv->client_info);
Zeeshan Ali's avatar
Zeeshan Ali committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680

        /* Chain up to the parent class */
        G_OBJECT_CLASS (gclue_service_client_parent_class)->finalize (object);
}

static void
gclue_service_client_get_property (GObject    *object,
                                   guint       prop_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
        GClueServiceClient *client = GCLUE_SERVICE_CLIENT (object);

        switch (prop_id) {
681 682
        case PROP_CLIENT_INFO:
                g_value_set_object (value, client->priv->client_info);
683 684
                break;

Zeeshan Ali's avatar
Zeeshan Ali committed
685 686 687 688
        case PROP_PATH:
                g_value_set_string (value, client->priv->path);
                break;

689 690 691 692
        case PROP_CONNECTION:
                g_value_set_object (value, client->priv->connection);
                break;

693 694 695 696
        case PROP_AGENT_PROXY:
                g_value_set_object (value, client->priv->agent_proxy);
                break;

Zeeshan Ali's avatar
Zeeshan Ali committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

static void
gclue_service_client_set_property (GObject      *object,
                                   guint         prop_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
        GClueServiceClient *client = GCLUE_SERVICE_CLIENT (object);

        switch (prop_id) {
711 712
        case PROP_CLIENT_INFO:
                client->priv->client_info = g_value_dup_object (value);
713 714
                break;

Zeeshan Ali's avatar
Zeeshan Ali committed
715 716 717 718
        case PROP_PATH:
                client->priv->path = g_value_dup_string (value);
                break;

719 720 721 722
        case PROP_CONNECTION:
                client->priv->connection = g_value_dup_object (value);
                break;

723 724
        case PROP_AGENT_PROXY:
                client->priv->agent_proxy = g_value_dup_object (value);
725
                if (client->priv->agent_proxy != NULL)
726 727 728 729
                        g_signal_connect (client->priv->agent_proxy,
                                          "g-properties-changed",
                                          G_CALLBACK (on_agent_props_changed),
                                          object);
730 731
                if (client->priv->pending_auth_start_data != NULL)
                        handle_pending_auth (client);
732 733
                break;

Zeeshan Ali's avatar
Zeeshan Ali committed
734 735 736 737 738
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        }
}

739 740 741 742 743 744 745 746 747 748 749 750 751 752
static void
gclue_service_client_handle_method_call (GDBusConnection       *connection,
                                         const gchar           *sender,
                                         const gchar           *object_path,
                                         const gchar           *interface_name,
                                         const gchar           *method_name,
                                         GVariant              *parameters,
                                         GDBusMethodInvocation *invocation,
                                         gpointer               user_data)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (user_data)->priv;
        GDBusInterfaceSkeletonClass *skeleton_class;
        GDBusInterfaceVTable *skeleton_vtable;

753
        if (!gclue_client_info_check_bus_name (priv->client_info, sender)) {
754 755 756 757
                g_dbus_method_invocation_return_error_literal (invocation,
                                                               G_DBUS_ERROR,
                                                               G_DBUS_ERROR_ACCESS_DENIED,
                                                               "Access denied");
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
                return;
        }

        skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (gclue_service_client_parent_class);
        skeleton_vtable = skeleton_class->get_vtable (G_DBUS_INTERFACE_SKELETON (user_data));
        skeleton_vtable->method_call (connection,
                                      sender,
                                      object_path,
                                      interface_name,
                                      method_name,
                                      parameters,
                                      invocation,
                                      user_data);
}

773
static GVariant *
774 775 776 777 778 779 780 781 782 783 784 785
gclue_service_client_handle_get_property (GDBusConnection *connection,
                                          const gchar     *sender,
                                          const gchar     *object_path,
                                          const gchar     *interface_name,
                                          const gchar     *property_name,
                                          GError         **error,
                                          gpointer        user_data)
{
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (user_data)->priv;
        GDBusInterfaceSkeletonClass *skeleton_class;
        GDBusInterfaceVTable *skeleton_vtable;

786
        if (!gclue_client_info_check_bus_name (priv->client_info, sender)) {
787 788 789 790
                g_set_error (error,
                             G_DBUS_ERROR,
                             G_DBUS_ERROR_ACCESS_DENIED,
                             "Access denied");
791
                return NULL;
792 793 794 795
        }

        skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (gclue_service_client_parent_class);
        skeleton_vtable = skeleton_class->get_vtable (G_DBUS_INTERFACE_SKELETON (user_data));
796 797 798 799 800 801 802
        return skeleton_vtable->get_property (connection,
                                              sender,
                                              object_path,
                                              interface_name,
                                              property_name,
                                              error,
                                              user_data);
803 804
}

805
static gboolean
806 807 808 809 810 811 812 813 814
gclue_service_client_handle_set_property (GDBusConnection *connection,
                                          const gchar     *sender,
                                          const gchar     *object_path,
                                          const gchar     *interface_name,
                                          const gchar     *property_name,
                                          GVariant        *variant,
                                          GError         **error,
                                          gpointer        user_data)
{
815
        GClueDBusClient *client = GCLUE_DBUS_CLIENT (user_data);
816
        GClueServiceClientPrivate *priv = GCLUE_SERVICE_CLIENT (client)->priv;
817 818
        GDBusInterfaceSkeletonClass *skeleton_class;
        GDBusInterfaceVTable *skeleton_vtable;
819
        gboolean ret;
820

821
        if (!gclue_client_info_check_bus_name (priv->client_info, sender)) {
822 823 824 825
                g_set_error (error,
                             G_DBUS_ERROR,
                             G_DBUS_ERROR_ACCESS_DENIED,
                             "Access denied");
826
                return FALSE;
827 828 829 830
        }

        skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (gclue_service_client_parent_class);
        skeleton_vtable = skeleton_class->get_vtable (G_DBUS_INTERFACE_SKELETON (user_data));
831 832 833 834 835 836 837 838 839
        ret = skeleton_vtable->set_property (connection,
                                             sender,
                                             object_path,
                                             interface_name,
                                             property_name,
                                             variant,
                                             error,
                                             user_data);
        if (ret && strcmp (property_name, "DistanceThreshold") == 0) {
840
                priv->distance_threshold = gclue_dbus_client_get_distance_threshold
841
                        (client);
842
                g_debug ("New distance threshold: %u", priv->distance_threshold);
843 844 845
        } else if (ret && strcmp (property_name, "TimeThreshold") == 0) {
                priv->time_threshold = gclue_dbus_client_get_time_threshold
                        (client);
846 847 848 849 850
                gclue_locator_set_time_threshold (priv->locator,
                                                  priv->time_threshold);
                g_debug ("%s: New time-threshold:  %u",
                         G_OBJECT_TYPE_NAME (client),
                         priv->time_threshold);
851 852 853
        }

        return ret;
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
}

static const GDBusInterfaceVTable gclue_service_client_vtable =
{
        gclue_service_client_handle_method_call,
        gclue_service_client_handle_get_property,
        gclue_service_client_handle_set_property,
        {NULL}
};

static GDBusInterfaceVTable *
gclue_service_client_get_vtable (GDBusInterfaceSkeleton *skeleton G_GNUC_UNUSED)
{
        return (GDBusInterfaceVTable *) &gclue_service_client_vtable;
}

Zeeshan Ali's avatar
Zeeshan Ali committed
870 871 872 873
static void
gclue_service_client_class_init (GClueServiceClientClass *klass)
{
        GObjectClass *object_class;
874
        GDBusInterfaceSkeletonClass *skeleton_class;
Zeeshan Ali's avatar
Zeeshan Ali committed
875 876 877 878 879 880

        object_class = G_OBJECT_CLASS (klass);
        object_class->finalize = gclue_service_client_finalize;
        object_class->get_property = gclue_service_client_get_property;
        object_class->set_property = gclue_service_client_set_property;

881 882 883
        skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);
        skeleton_class->get_vtable = gclue_service_client_get_vtable;

884 885 886 887 888 889
        gParamSpecs[PROP_CLIENT_INFO] = g_param_spec_object ("client-info",
                                                             "ClientInfo",
                                                             "Information on client",
                                                             GCLUE_TYPE_CLIENT_INFO,
                                                             G_PARAM_READWRITE |
                                                             G_PARAM_CONSTRUCT_ONLY);
890
        g_object_class_install_property (object_class,
891 892
                                         PROP_CLIENT_INFO,
                                         gParamSpecs[PROP_CLIENT_INFO]);
893

Zeeshan Ali's avatar
Zeeshan Ali committed
894 895 896 897 898 899 900 901 902
        gParamSpecs[PROP_PATH] = g_param_spec_string ("path",
                                                      "Path",
                                                      "Path",
                                                      NULL,
                                                      G_PARAM_READWRITE |
                                                      G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_PATH,
                                         gParamSpecs[PROP_PATH]);
903 904 905 906 907 908 909 910 911 912

        gParamSpecs[PROP_CONNECTION] = g_param_spec_object ("connection",
                                                            "Connection",
                                                            "DBus Connection",
                                                            G_TYPE_DBUS_CONNECTION,
                                                            G_PARAM_READWRITE |
                                                            G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_CONNECTION,
                                         gParamSpecs[PROP_CONNECTION]);
913 914 915 916

        gParamSpecs[PROP_AGENT_PROXY] = g_param_spec_object ("agent-proxy",
                                                             "AgentProxy",
                                                             "Proxy to app authorization agent",
917
                                                             GCLUE_TYPE_AGENT_PROXY,
918 919 920 921 922
                                                             G_PARAM_READWRITE |
                                                             G_PARAM_CONSTRUCT);
        g_object_class_install_property (object_class,
                                         PROP_AGENT_PROXY,
                                         gParamSpecs[PROP_AGENT_PROXY]);
Zeeshan Ali's avatar
Zeeshan Ali committed
923 924 925
}

static void
926
gclue_service_client_client_iface_init (GClueDBusClientIface *iface)
Zeeshan Ali's avatar
Zeeshan Ali committed
927 928 929 930 931
{
        iface->handle_start = gclue_service_client_handle_start;
        iface->handle_stop = gclue_service_client_handle_stop;
}

932 933 934 935 936
static gboolean
gclue_service_client_initable_init (GInitable    *initable,
                                    GCancellable *cancellable,
                                    GError      **error)
{
937
        if (!g_dbus_interface_skeleton_export
938 939 940
                                (G_DBUS_INTERFACE_SKELETON (initable),
                                 GCLUE_SERVICE_CLIENT (initable)->priv->connection,
                                 GCLUE_SERVICE_CLIENT (initable)->priv->path,
941 942 943
                                 error))
                return FALSE;

944
        return TRUE;
945 946 947 948 949 950 951 952
}

static void
gclue_service_client_initable_iface_init (GInitableIface *iface)
{
        iface->init = gclue_service_client_initable_init;
}

Zeeshan Ali's avatar
Zeeshan Ali committed
953 954 955 956
static void
gclue_service_client_init (GClueServiceClient *client)
{
        client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client,
957
                                                    GCLUE_TYPE_SERVICE_CLIENT,
Zeeshan Ali's avatar
Zeeshan Ali committed
958
                                                    GClueServiceClientPrivate);
959 960
        gclue_dbus_client_set_requested_accuracy_level
                (GCLUE_DBUS_CLIENT (client), DEFAULT_ACCURACY_LEVEL);
Zeeshan Ali's avatar
Zeeshan Ali committed
961 962 963
}

GClueServiceClient *
964
gclue_service_client_new (GClueClientInfo *info,
965
                          const char      *path,
966
                          GDBusConnection *connection,
967
                          GClueAgent      *agent_proxy,
968
                          GError         **error)
Zeeshan Ali's avatar
Zeeshan Ali committed
969
{
970
        return g_initable_new (GCLUE_TYPE_SERVICE_CLIENT,
971 972
                               NULL,
                               error,
973
                               "client-info", info,
974 975
                               "path", path,
                               "connection", connection,
976
                               "agent-proxy", agent_proxy,
977
                               NULL);
Zeeshan Ali's avatar
Zeeshan Ali committed
978 979 980 981 982
}

const gchar *
gclue_service_client_get_path (GClueServiceClient *client)
{
983
        g_return_val_if_fail (GCLUE_IS_SERVICE_CLIENT(client), NULL);
Zeeshan Ali's avatar
Zeeshan Ali committed
984 985 986

        return client->priv->path;
}
987

988 989
GClueClientInfo *
gclue_service_client_get_client_info (GClueServiceClient *client)
990 991 992
{
        g_return_val_if_fail (GCLUE_IS_SERVICE_CLIENT(client), NULL);

993
        return client->priv->client_info;
994
}