1
/* location-geoclue.c -- Geoclue location provider source
2
This file is part of Redshift.
4
Redshift is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Redshift is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Redshift. If not, see <http://www.gnu.org/licenses/>.
17
Copyright (c) 2010 Mathieu Trudel-Lapierre <mathieu-tl@ubuntu.com>
23
#include <geoclue/geoclue-master.h>
24
#include <geoclue/geoclue-position.h>
26
#include "location-geoclue.h"
30
# define _(s) gettext(s)
35
#define DEFAULT_PROVIDER "org.freedesktop.Geoclue.Providers.UbuntuGeoIP"
36
#define DEFAULT_PROVIDER_PATH "/org/freedesktop/Geoclue/Providers/UbuntuGeoIP"
39
location_geoclue_init(location_geoclue_state_t *state)
43
state->position = NULL;
44
state->provider = NULL;
45
state->provider_path = NULL;
51
location_geoclue_start(location_geoclue_state_t *state)
53
GeoclueMaster *master = NULL;
54
GeoclueMasterClient *client = NULL;
58
if (!(state->provider && state->provider_path)) {
59
master = geoclue_master_get_default();
60
client = geoclue_master_create_client(master, NULL, NULL);
62
if (!geoclue_master_client_set_requirements(client,
63
GEOCLUE_ACCURACY_LEVEL_REGION,
65
GEOCLUE_RESOURCE_NETWORK,
67
g_printerr(_("Can't set requirements for master: %s"),
70
g_object_unref(client);
75
state->position = geoclue_master_client_create_position(client, NULL);
77
state->position = geoclue_position_new(state->provider,
78
state->provider_path);
81
if (geoclue_provider_get_provider_info(GEOCLUE_PROVIDER(state->position),
83
fprintf(stdout, _("Started Geoclue provider `%s'.\n"), name);
86
fputs(_("Could not find a usable Geoclue provider.\n"), stderr);
87
fputs(_("Try setting name and path to specify which to use.\n"), stderr);
95
location_geoclue_free(location_geoclue_state_t *state)
97
if (state->position != NULL) g_object_unref(state->position);
101
location_geoclue_print_help(FILE *f)
103
fputs(_("Use the location as discovered by a Geoclue provider.\n"), f);
106
/* TRANSLATORS: Geoclue help output
107
left column must not be translated */
108
fputs(_(" name=N\tName of Geoclue provider (or `default')\n"
109
" path=N\tPath of Geoclue provider (or `default')\n"), f);
114
location_geoclue_set_option(location_geoclue_state_t *state,
115
const char *key, const char *value)
117
const char *provider = NULL;
118
const char *path = NULL;
120
/* Parse string value */
121
if (key != NULL && strcasecmp(key, "name") == 0) {
122
if (value != NULL && strcasecmp(value, "default") == 0) {
123
provider = DEFAULT_PROVIDER;
124
} else if (value != NULL) {
127
fputs(_("Must specify a provider `name' (or use `default').\n"), stderr);
131
/* TODO I don't think we own the string here, should be copied. */
132
state->provider = provider;
133
} else if (key != NULL && strcasecmp(key, "path") == 0) {
134
if (value != NULL && strcasecmp(value, "default") == 0) {
135
path = DEFAULT_PROVIDER_PATH;
136
} else if (value != NULL) {
139
fputs(_("Must specify a provider `path' (or use `default').\n"), stderr);
143
/* TODO I don't think we own the string here, should be copied. */
144
state->provider_path = path;
145
} else if (key == NULL) {
148
fprintf(stderr, _("Unknown method parameter: `%s'.\n"), key);
156
location_geoclue_get_location(location_geoclue_state_t *state,
157
float *lat, float *lon)
159
GeocluePositionFields fields;
160
GError *error = NULL;
161
double latitude = 0, longitude = 0;
163
fields = geoclue_position_get_position(state->position, NULL,
164
&latitude, &longitude, NULL,
167
g_printerr(_("Could not get location: %s.\n"), error->message);
172
if (fields & GEOCLUE_POSITION_FIELDS_LATITUDE &&
173
fields & GEOCLUE_POSITION_FIELDS_LONGITUDE) {
174
fprintf(stdout, _("According to the geoclue provider"
175
" we're at: %.2f, %.2f\n"),
176
latitude, longitude);
178
g_warning(_("Provider does not have a valid location available."));