geoclue-test-gui.c 28.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Geoclue
 * client-test-gui.c - Geoclue Test GUI
 *
 * Authors: Jussi Kukkonen <jku@linux.intel.com>
 * 
 * Copyright 2008 OpenedHand Ltd
 *           2008 Intel Corporation
 */
Jussi Kukkonen's avatar
Jussi Kukkonen committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#include <time.h>
#include <string.h>

#include <glib-object.h>
#include <gtk/gtk.h>

#include <geoclue/geoclue-master.h>
#include <geoclue/geoclue-master-client.h>
#include <geoclue/geoclue-address.h>
#include <geoclue/geoclue-position.h>

enum {
	COL_ADDRESS_PROVIDER = 0,
	COL_ADDRESS_PROVIDER_NAME,
25
	COL_ADDRESS_IS_MASTER,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
26 27 28 29 30 31 32 33 34 35 36 37
	COL_ADDRESS_COUNTRY,
	COL_ADDRESS_COUNTRYCODE,
	COL_ADDRESS_REGION,
	COL_ADDRESS_LOCALITY,
	COL_ADDRESS_AREA,
	COL_ADDRESS_POSTALCODE,
	COL_ADDRESS_STREET,
	NUM_ADDRESS_COLS
};
enum {
	COL_POSITION_PROVIDER = 0,
	COL_POSITION_PROVIDER_NAME,
38
	COL_POSITION_IS_MASTER,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	COL_POSITION_LAT,
	COL_POSITION_LON,
	COL_POSITION_ALT,
	NUM_POSITION_COLS
};


#define GEOCLUE_TYPE_TEST_GUI geoclue_test_gui_get_type()
#define GEOCLUE_TEST_GUI(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEOCLUE_TYPE_TEST_GUI, GeoclueTestGui))
#define GEOCLUE_TEST_GUI_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEOCLUE_TYPE_TEST_GUI, GeoclueTestGuiClass))
#define GEOCLUE_TEST_GUI_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEOCLUE_TYPE_TEST_GUI, GeoclueTestGuiClass))

typedef struct {
	GObject parent;
	
	GtkWidget *window;
	GtkTextBuffer *buffer;
	
	GeoclueMasterClient *client;
	char *master_client_path;
59 60
	GeoclueAccuracyLevel master_accuracy;
	GeoclueResourceFlags master_resources;
Jussi Kukkonen's avatar
Jussi Kukkonen committed
61 62
	
	GtkListStore *position_store;
63 64 65 66
	GList *position_providers; /* PositionProviders, first one is master */
	
	GtkListStore *address_store; 
	GList *address_providers; /* AddressProviders, first one is master */
Jussi Kukkonen's avatar
Jussi Kukkonen committed
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
} GeoclueTestGui;

typedef struct {
	GObjectClass parent_class;
} GeoclueTestGuiClass;

G_DEFINE_TYPE (GeoclueTestGui, geoclue_test_gui, G_TYPE_OBJECT)


static void
geoclue_test_gui_dispose (GObject *object)
{
	
	G_OBJECT_CLASS (geoclue_test_gui_parent_class)->dispose (object);
}


static void
geoclue_test_gui_class_init (GeoclueTestGuiClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	
	object_class->dispose = geoclue_test_gui_dispose;
}


static void
geoclue_test_gui_master_log_message (GeoclueTestGui *gui, char *message)
{
	GtkTextIter iter;
	char *line;
	time_t rawtime;
99
	struct tm *timeinfo;
Jussi Kukkonen's avatar
Jussi Kukkonen committed
100 101 102 103 104
	char time_buffer [20];
	
	time (&rawtime);
	timeinfo = localtime (&rawtime);
	
105
	strftime (time_buffer, 19, "%X", timeinfo);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
106 107 108 109 110 111 112 113
	line = g_strdup_printf ("%s: %s\n", time_buffer, message);
	
	gtk_text_buffer_get_end_iter (gui->buffer, &iter);
	gtk_text_buffer_insert (gui->buffer, &iter, line, -1);
	
	g_free (line);
}

114 115
static gboolean
get_matching_tree_iter (GtkTreeModel *model, GeoclueProvider *provider, GtkTreeIter *iter)
Jussi Kukkonen's avatar
Jussi Kukkonen committed
116
{
117
	GeoclueProvider *p = NULL;
Jussi Kukkonen's avatar
Jussi Kukkonen committed
118 119
	gboolean valid;
	
120 121
	g_assert (model);
	g_assert (provider);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
122
	
123
	valid = gtk_tree_model_get_iter_first (model, iter);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
124
	while (valid) {
125 126
		gtk_tree_model_get (model, iter,
		                    COL_ADDRESS_PROVIDER, &p,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
127
		                    -1);
128 129
		if (p == provider) {
			return TRUE;
Jussi Kukkonen's avatar
Jussi Kukkonen committed
130
		}
131 132
		
		valid = gtk_tree_model_iter_next (model, iter);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
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
	return FALSE;
}

static void
update_address (GeoclueTestGui *gui, GeoclueAddress *address, GHashTable *details)
{
	GtkTreeIter iter;
	
	g_assert (details);
	
	if (get_matching_tree_iter (GTK_TREE_MODEL (gui->address_store),
	                            GEOCLUE_PROVIDER (address),
	                            &iter)) {
		gtk_list_store_set (gui->address_store, &iter,
		                    COL_ADDRESS_COUNTRY, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_COUNTRY),
		                    COL_ADDRESS_COUNTRYCODE, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_COUNTRYCODE),
		                    COL_ADDRESS_REGION, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_REGION),
		                    COL_ADDRESS_LOCALITY, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_LOCALITY),
		                    COL_ADDRESS_AREA, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_AREA),
		                    COL_ADDRESS_POSTALCODE, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_POSTALCODE),
		                    COL_ADDRESS_STREET, g_hash_table_lookup (details, GEOCLUE_ADDRESS_KEY_STREET),
		                    -1);
	}
Jussi Kukkonen's avatar
Jussi Kukkonen committed
158 159 160 161 162 163 164 165 166
}


static void
update_position (GeoclueTestGui *gui, GeocluePosition *position, 
                 double lat, double lon, double alt)
{
	GtkTreeIter iter;
	
167 168 169 170 171 172 173
	if (get_matching_tree_iter (GTK_TREE_MODEL (gui->position_store),
	                            GEOCLUE_PROVIDER (position),
	                            &iter)) {
		gtk_list_store_set (gui->position_store, &iter,
		                    COL_POSITION_LAT, lat,
		                    COL_POSITION_LON, lon,
		                    COL_POSITION_ALT, alt,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
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
		                    -1);
	}
}


static void
address_changed (GeoclueAddress  *address,
                 int              timestamp,
                 GHashTable      *details,
                 GeoclueAccuracy *accuracy,
                 GeoclueTestGui  *gui)
{
	update_address (gui, address, details);
}


static void
position_changed (GeocluePosition      *position,
                  GeocluePositionFields fields,
                  int                   timestamp,
                  double                latitude,
                  double                longitude,
                  double                altitude,
                  GeoclueAccuracy      *accuracy,
                  GeoclueTestGui       *gui)
{
	update_position (gui, position, latitude, longitude, altitude);
}

203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
static void
address_callback (GeoclueAddress *address,
                  int timestamp,
                  GHashTable *details,
                  GeoclueAccuracy *accuracy,
                  GError *error,
                  gpointer userdata)
{
	if (error) {
		g_warning ("Error getting address: %s\n", error->message);
		g_error_free (error);
		details = geoclue_address_details_new ();
	}
	
	update_address (GEOCLUE_TEST_GUI (userdata), address, details);
}

220
static void 
221 222 223 224 225
info_callback (GeoclueProvider *provider,
               char *name,
               char *description,
               GError *error,
               gpointer userdata)
Jussi Kukkonen's avatar
Jussi Kukkonen committed
226 227
{
	GtkTreeIter iter;
228
	GeoclueTestGui *gui = GEOCLUE_TEST_GUI (userdata);
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
	if (error) {
		g_warning ("Error getting provider info: %s\n", error->message);
		g_error_free (error);
		return;
	}
	
	if (get_matching_tree_iter (GTK_TREE_MODEL (gui->address_store),
	                            provider,
	                            &iter)) {
		/* skip master, that's handled in master_*_provider_changed */
		if (strcmp (name, "Geoclue Master") != 0) {
			gtk_list_store_set (gui->address_store, &iter,
					    COL_ADDRESS_PROVIDER_NAME, name,
					    -1);
		}
	}
	
	if (get_matching_tree_iter (GTK_TREE_MODEL (gui->position_store),
	                            provider,
	                            &iter)) {
		/* skip master, that's handled in master_*_provider_changed */
		if (strcmp (name, "Geoclue Master") != 0) {
			gtk_list_store_set (gui->position_store, &iter,
			                    COL_POSITION_PROVIDER_NAME, name,
			                    -1);
		}
	}
}

static void 
260
add_to_address_store (GeoclueTestGui *gui, GeoclueAddress *address, gboolean is_master)
261 262
{
	GtkTreeIter iter;
263
	
264 265
	g_assert (gui->address_store);
	g_assert (address);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
266
	
267 268 269 270 271 272
	if (is_master) {
		/* master is already on the first line */
		gtk_tree_model_get_iter_first (GTK_TREE_MODEL (gui->address_store), &iter);
	} else {
		gtk_list_store_append (gui->address_store, &iter);
	}
Jussi Kukkonen's avatar
Jussi Kukkonen committed
273 274
	gtk_list_store_set (gui->address_store, &iter,
	                    COL_ADDRESS_PROVIDER, address,
275
	                    COL_ADDRESS_IS_MASTER, is_master,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
276 277
	                    -1);
	
278 279
	g_signal_connect (G_OBJECT (address), "address-changed",
				G_CALLBACK (address_changed), gui);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
280
	
281 282 283
	geoclue_provider_get_provider_info_async (GEOCLUE_PROVIDER (address), 
	                                          info_callback,
	                                          gui);
284 285
	geoclue_address_get_address_async (address, address_callback, gui);
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
286 287
}

288 289
static gboolean
get_next_provider (GDir *dir, char **name, char **service, char **path, char **ifaces)
Jussi Kukkonen's avatar
Jussi Kukkonen committed
290
{
291 292 293 294 295 296 297 298 299 300
	const char *filename;
	char *fullname;
	GKeyFile *keyfile;
	gboolean ret;
	GError *error;
	
	filename = g_dir_read_name (dir);
	if (!filename) {
		return FALSE;
	}
Jussi Kukkonen's avatar
Jussi Kukkonen committed
301
	
302 303 304 305 306 307
	fullname = g_build_filename (GEOCLUE_PROVIDERS_DIR, 
	                             filename, NULL);
	keyfile = g_key_file_new ();
	ret = g_key_file_load_from_file (keyfile, fullname, 
	                                 G_KEY_FILE_NONE, &error);
	g_free (fullname);
308
	
309 310 311
	if (!ret) {
		g_warning ("Error loading %s: %s", filename, error->message);
		g_error_free (error);
312
	} else {
313 314 315 316 317 318 319 320 321
		*name = g_key_file_get_value (keyfile, "Geoclue Provider",
		                              "Name", NULL);
		
		*service = g_key_file_get_value (keyfile, "Geoclue Provider",
		                                 "Service", NULL);
		*path = g_key_file_get_value (keyfile, "Geoclue Provider",
		                              "Path", NULL);
		*ifaces = g_key_file_get_value (keyfile, "Geoclue Provider",
		                              "Interfaces", NULL);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
322 323
	}
	
324 325
	g_key_file_free (keyfile);
	return TRUE;
326 327
}

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
static void 
position_callback (GeocluePosition *position,
                   GeocluePositionFields fields,
                   int timestamp,
                   double lat, double lon, double alt,
                   GeoclueAccuracy *accuracy,
                   GError *error,
                   gpointer userdata)
{
	if (error) {
		g_warning ("Error getting position: %s\n", error->message);
		g_error_free (error);
		lat = lon = alt = 0.0/0.0;
	}
	update_position (GEOCLUE_TEST_GUI (userdata), position, lat, lon, alt);
}

345
static void 
346
add_to_position_store (GeoclueTestGui *gui, GeocluePosition *position, gboolean is_master)
347 348
{
	GtkTreeIter iter;
349
	
350
	g_assert (gui->position_store);
351
	g_assert (position);
352
	
353 354 355 356 357 358
	if (is_master) {
		/* master is already on the first line */
		gtk_tree_model_get_iter_first (GTK_TREE_MODEL (gui->position_store), &iter);
	} else {
		gtk_list_store_append (gui->position_store, &iter);
	}
Jussi Kukkonen's avatar
Jussi Kukkonen committed
359 360
	gtk_list_store_set (gui->position_store, &iter,
	                    COL_POSITION_PROVIDER, position,
361
	                    COL_POSITION_IS_MASTER, is_master,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
362 363
	                    -1);
	
364 365
	g_signal_connect (G_OBJECT (position), "position-changed",
				G_CALLBACK (position_changed), gui);
366
	
367 368 369
	geoclue_provider_get_provider_info_async (GEOCLUE_PROVIDER (position), 
	                                          info_callback,
	                                          gui);
370
	geoclue_position_get_position_async (position, position_callback, gui);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
}


static GtkWidget *
get_address_tree_view (GeoclueTestGui *gui)
{
	GtkTreeView *view;
	GtkCellRenderer *renderer;
	
	view = GTK_TREE_VIEW (gtk_tree_view_new ());
	
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             "Provider",  
	                                             renderer,
	                                             "text",
	                                             COL_ADDRESS_PROVIDER_NAME,
	                                             NULL);
389
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_COUNTRY,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_COUNTRY,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_COUNTRYCODE,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_COUNTRYCODE,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_REGION,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_REGION,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_LOCALITY,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_LOCALITY,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_AREA,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_AREA,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_POSTALCODE,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_POSTALCODE,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             GEOCLUE_ADDRESS_KEY_STREET,  
	                                             renderer,
	                                             "text", 
	                                             COL_ADDRESS_STREET,
	                                             NULL);
	
	gui->address_store = gtk_list_store_new (NUM_ADDRESS_COLS, 
	                            G_TYPE_POINTER, 
	                            G_TYPE_STRING,
443
	                            G_TYPE_BOOLEAN,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
	                            G_TYPE_STRING,
	                            G_TYPE_STRING,
	                            G_TYPE_STRING,
	                            G_TYPE_STRING,
	                            G_TYPE_STRING,
	                            G_TYPE_STRING,
	                            G_TYPE_STRING);
	
	gtk_tree_view_set_model (view, GTK_TREE_MODEL(gui->address_store));
	
	return GTK_WIDGET (view);
}


static GtkWidget *
get_position_tree_view (GeoclueTestGui *gui)
{
	GtkTreeView *view;
	GtkCellRenderer *renderer;
	
	view = GTK_TREE_VIEW (gtk_tree_view_new ());
	
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             "Provider",  
	                                             renderer,
	                                             "text",
	                                             COL_POSITION_PROVIDER_NAME,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             "latitude",  
	                                             renderer,
	                                             "text", 
	                                             COL_POSITION_LAT,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             "longitude",  
	                                             renderer,
	                                             "text", 
	                                             COL_POSITION_LON,
	                                             NULL);
	renderer = gtk_cell_renderer_text_new ();
	gtk_tree_view_insert_column_with_attributes (view, -1,      
	                                             "altitude",  
	                                             renderer,
	                                             "text", 
	                                             COL_POSITION_ALT,
	                                             NULL);
	
	gui->position_store = gtk_list_store_new (NUM_POSITION_COLS, 
	                            G_TYPE_POINTER, 
	                            G_TYPE_STRING,
498
	                            G_TYPE_BOOLEAN,
Jussi Kukkonen's avatar
Jussi Kukkonen committed
499 500 501 502 503 504 505 506 507 508
	                            G_TYPE_DOUBLE,
	                            G_TYPE_DOUBLE,
	                            G_TYPE_DOUBLE);
	
	gtk_tree_view_set_model (view, GTK_TREE_MODEL(gui->position_store));
	
	return GTK_WIDGET (view);
}

static void
509 510 511 512 513 514
master_position_provider_changed (GeoclueMasterClient *client,
                                  char *name,
                                  char *description, 
                                  char *service,
                                  char *path,
                                  GeoclueTestGui *gui)
Jussi Kukkonen's avatar
Jussi Kukkonen committed
515 516
{
	GtkTreeIter iter;
517
	char *msg;
Jussi Kukkonen's avatar
Jussi Kukkonen committed
518
	
519 520 521 522 523
	if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (gui->position_store), 
					   &iter)) {
		gtk_list_store_set (gui->position_store, &iter,
				    COL_POSITION_PROVIDER_NAME, g_strdup_printf ("Master (%s)", name),
				    -1);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
524
	}
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	msg = g_strdup_printf ("Master: position provider changed: %s", name);
	geoclue_test_gui_master_log_message (gui, msg);
	g_free (msg);
}

static void
master_address_provider_changed (GeoclueMasterClient *client,
                                 char *name,
                                 char *description, 
                                 char *service,
                                 char *path,
                                 GeoclueTestGui *gui)
{
	GtkTreeIter iter;
	char *msg;
Jussi Kukkonen's avatar
Jussi Kukkonen committed
540
	
541 542 543 544 545
	if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (gui->address_store), 
					   &iter)) {
		gtk_list_store_set (gui->address_store, &iter,
				    COL_ADDRESS_PROVIDER_NAME, g_strdup_printf ("Master (%s)", name),
				    -1);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
546
	}
547
	msg = g_strdup_printf ("Master: address provider changed: %s", name);
548 549
	geoclue_test_gui_master_log_message (gui, msg);
	g_free (msg);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
550 551
}

552 553 554 555 556 557 558 559 560 561 562
static void
set_requirements_callback (GeoclueMasterClient *client,
                           GError *error,
                           gpointer userdata)
{
	if (error) {
		g_printerr ("Setting requirements failed : %s", error->message);
		g_error_free (error);
	}
}

563 564 565
static void
update_master_requirements (GeoclueTestGui *gui)
{
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	geoclue_master_client_set_requirements_async (gui->client, 
	                                              gui->master_accuracy,
	                                              0,
	                                              FALSE,
	                                              gui->master_resources,
	                                              set_requirements_callback,
	                                              NULL);
}

static void 
create_address_callback (GeoclueMasterClient *client,
                         GeoclueAddress      *address,
                         GError              *error,
                         gpointer             userdata)
{
	GeoclueTestGui *gui = GEOCLUE_TEST_GUI (userdata);
	
	if (error) {
		g_printerr ("Master Client: Failed to create address: %s", error->message);
		g_error_free (error);
		return;
	}
	add_to_address_store (gui, address, TRUE);
}

static void 
create_position_callback (GeoclueMasterClient *client,
                          GeocluePosition     *position,
                          GError              *error,
                          gpointer             userdata)
{
	GeoclueTestGui *gui = GEOCLUE_TEST_GUI (userdata);
	
	if (error) {
		g_printerr ("Master Client: Failed to create position: %s", error->message);
		g_error_free (error);
		return;
603
	}
604
	add_to_position_store (gui, position, TRUE);
605 606
}

607

Jussi Kukkonen's avatar
Jussi Kukkonen committed
608
static void
609 610 611 612 613
create_client_callback (GeoclueMaster       *master,
			GeoclueMasterClient *client,
			char                *object_path,
			GError              *error,
			gpointer             userdata)
Jussi Kukkonen's avatar
Jussi Kukkonen committed
614
{
615 616
	GDir *dir;
	char *name, *path, *service, *ifaces;
617 618
	GtkTreeIter iter;
	GeoclueTestGui *gui = GEOCLUE_TEST_GUI (userdata);
619
	
620 621 622
	if (error) {
		g_printerr ("Failed to create master client: %s", error->message);
		g_error_free (error);
623 624 625
		return;
	}
	
626 627
	gui->client = client;
	
628 629 630 631
	g_signal_connect (G_OBJECT (gui->client), "position-provider-changed",
	                  G_CALLBACK (master_position_provider_changed), gui);
	g_signal_connect (G_OBJECT (gui->client), "address-provider-changed",
	                  G_CALLBACK (master_address_provider_changed), gui);
632
	update_master_requirements (gui);
633
	
634
	/* add master providers to the lists */
635 636 637 638 639 640 641 642
	gtk_list_store_append (gui->position_store, &iter);
	geoclue_master_client_create_position_async (gui->client, 
	                                             create_position_callback,
	                                             gui);
	gtk_list_store_append (gui->address_store, &iter);
	geoclue_master_client_create_address_async (gui->client, 
	                                            create_address_callback,
	                                            gui);
643 644 645 646 647 648 649 650 651 652 653 654 655
	
	/* add individual providers based on files in GEOCLUE_PROVIDERS_DIR  */
	dir = g_dir_open (GEOCLUE_PROVIDERS_DIR, 0, &error);
	if (!dir) {
		g_warning ("Error opening %s: %s\n", 
		           GEOCLUE_PROVIDERS_DIR, error->message);
		g_error_free (error);
		return;
	}
	
	name = service = path = ifaces = NULL;
	while (get_next_provider (dir, &name, &service, &path, &ifaces)) {
		if (ifaces && strstr (ifaces, "org.freedesktop.Geoclue.Position")) {
656
			add_to_position_store (gui, geoclue_position_new (service, path), FALSE);
657 658
		}
		if (ifaces && strstr (ifaces, "org.freedesktop.Geoclue.Address")) {
659
			add_to_address_store (gui, geoclue_address_new (service, path), FALSE);
660 661 662 663 664 665
		}
		g_free (name);
		g_free (path);
		g_free (service);
		g_free (ifaces);
	}
666
	
667
	g_dir_close (dir);
668 669 670 671 672 673 674 675 676 677 678 679
	g_object_unref (master);
}

static void
geoclue_test_gui_load_providers (GeoclueTestGui *gui)
{
	GeoclueMaster *master;
	
	master = geoclue_master_get_default ();
	geoclue_master_create_client_async (master, 
	                                    create_client_callback,
	                                    gui);
680 681 682 683 684 685
}

static void
accuracy_combo_changed (GtkComboBox *combo, GeoclueTestGui *gui)
{
	GtkTreeIter iter;
686
	
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
	if (gtk_combo_box_get_active_iter (combo, &iter)) {
		GtkTreeModel *model;
		
		model = gtk_combo_box_get_model (combo);
		gtk_tree_model_get (model, &iter, 0, &gui->master_accuracy, -1); 
		
		update_master_requirements (gui);
	}
}

static void
gps_check_toggled (GtkCheckButton *btn, GeoclueTestGui *gui)
{
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (btn))) {
		gui->master_resources |= GEOCLUE_RESOURCE_GPS;
	} else {
		gui->master_resources &= ~GEOCLUE_RESOURCE_GPS;
	}
	update_master_requirements (gui);
}

static void
network_check_toggled (GtkCheckButton *btn, GeoclueTestGui *gui)
{
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (btn))) {
		gui->master_resources |= GEOCLUE_RESOURCE_NETWORK;
	} else {
		gui->master_resources &= ~GEOCLUE_RESOURCE_NETWORK;
	}
	update_master_requirements (gui);
}

static void
cell_check_toggled (GtkCheckButton *btn, GeoclueTestGui *gui)
{
	if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (btn))) {
		gui->master_resources |= GEOCLUE_RESOURCE_CELL;
	} else {
		gui->master_resources &= ~GEOCLUE_RESOURCE_CELL;
	}
	update_master_requirements (gui);
}

static GtkWidget*
get_accuracy_combo (GeoclueTestGui *gui)
{
	GtkListStore *store;
	GtkWidget *combo;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;
	
	store = gtk_list_store_new (2, G_TYPE_UINT, G_TYPE_STRING);
	
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 
	                    0, GEOCLUE_ACCURACY_LEVEL_COUNTRY, 
	                    1, "Country",
	                    -1);
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 
	                    0, GEOCLUE_ACCURACY_LEVEL_REGION, 
	                    1, "Region",
	                    -1);
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 
	                    0, GEOCLUE_ACCURACY_LEVEL_LOCALITY, 
	                    1, "Locality",
	                    -1);
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 
	                    0, GEOCLUE_ACCURACY_LEVEL_POSTALCODE, 
	                    1, "Postalcode",
	                    -1);
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 
	                    0, GEOCLUE_ACCURACY_LEVEL_STREET, 
	                    1, "Street",
	                    -1);
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter, 
	                    0, GEOCLUE_ACCURACY_LEVEL_DETAILED, 
	                    1, "Detailed",
	                    -1);
	
	combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
	renderer = gtk_cell_renderer_text_new ();
	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
	                                "text", 1, NULL);
	
	gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
	gui->master_accuracy = GEOCLUE_ACCURACY_LEVEL_COUNTRY;
	
	return combo;
781 782 783 784 785 786 787 788 789 790
}


static void
geoclue_test_gui_init (GeoclueTestGui *gui)
{
	GtkWidget *address_view;
	GtkWidget *position_view;
	GtkWidget *notebook;
	GtkWidget *box;
791 792
	GtkWidget *frame;
	GtkWidget *hbox, *vbox;
793
	GtkWidget *label;
794
	GtkWidget *combo, *check;
795 796 797 798 799 800 801 802 803 804 805
	GtkWidget *scrolled_win;
	GtkWidget *view;
	
	gui->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	g_signal_connect (G_OBJECT (gui->window), "destroy",
	                  G_CALLBACK (gtk_main_quit), NULL);
	
	view = gtk_text_view_new ();
	gtk_widget_set_size_request (GTK_WIDGET (view), 500, 200);
	g_object_set (G_OBJECT (view), "editable", FALSE, NULL);
	gui->buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
Jussi Kukkonen's avatar
Jussi Kukkonen committed
806 807
	
	
808
	box = gtk_vbox_new (FALSE, 8);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
809 810
	gtk_container_add (GTK_CONTAINER (gui->window), box);
	
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	frame = gtk_frame_new ("Master settings");
	gtk_box_pack_start (GTK_BOX (box), frame, FALSE, FALSE, 4);
	
	
	hbox = gtk_hbox_new (FALSE, 24);
	gtk_container_add (GTK_CONTAINER (frame), hbox);
	
	vbox = gtk_vbox_new (FALSE, 0);
	gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 8);
	label = gtk_label_new ("Required accuracy level:");
	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
	
	combo = get_accuracy_combo (gui); 
	g_signal_connect (combo, "changed",
	                  G_CALLBACK (accuracy_combo_changed), gui);
	gtk_box_pack_start (GTK_BOX (vbox), combo, FALSE, FALSE, 0);
	
	vbox = gtk_vbox_new (FALSE, 0);
	gtk_box_pack_start (GTK_BOX (hbox), vbox, FALSE, FALSE, 0);
	label = gtk_label_new ("Allow resources:");
	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
	
	check = gtk_check_button_new_with_label ("Network");
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check), TRUE);
	gui->master_resources |= GEOCLUE_RESOURCE_NETWORK;
	g_signal_connect (check, "toggled",
	                  G_CALLBACK (network_check_toggled), gui);
	gtk_box_pack_start (GTK_BOX (vbox), check, FALSE, FALSE, 0);
	
	check = gtk_check_button_new_with_label ("GPS");
	g_signal_connect (check, "toggled",
	                  G_CALLBACK (gps_check_toggled), gui);
	gtk_box_pack_start (GTK_BOX (vbox), check, FALSE, FALSE, 0);
	
	check = gtk_check_button_new_with_label ("Cell");
	g_signal_connect (check, "toggled",
	                  G_CALLBACK (cell_check_toggled), gui);
	gtk_box_pack_start (GTK_BOX (vbox), check, FALSE, FALSE, 0);
	
850 851 852 853
	notebook = gtk_notebook_new ();
	gtk_box_pack_start (GTK_BOX (box), notebook, FALSE, FALSE, 0);
	gtk_notebook_set_show_tabs (GTK_NOTEBOOK (notebook), TRUE);
	gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), TRUE);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
854 855
	
	address_view = get_address_tree_view (gui);
856 857
	label = gtk_label_new ("Address");
	gtk_notebook_append_page (GTK_NOTEBOOK (notebook), address_view, label);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
858
	
859
	position_view = get_position_tree_view (gui);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
860
	label = gtk_label_new ("Position");
861
	gtk_notebook_append_page (GTK_NOTEBOOK (notebook), position_view, label);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
862 863 864
	
	
	hbox = gtk_hbox_new (FALSE, 0);
865
	gtk_box_pack_start (GTK_BOX (box), hbox, FALSE, FALSE, 0);
Jussi Kukkonen's avatar
Jussi Kukkonen committed
866 867 868 869 870 871 872 873 874
	label = gtk_label_new ("Master log");
	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
	
	scrolled_win = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win), 
	                                GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
	gtk_container_add (GTK_CONTAINER (box), scrolled_win);
	gtk_container_add (GTK_CONTAINER (scrolled_win), view);
	
875 876
	geoclue_test_gui_load_providers (gui);
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
877 878 879 880 881 882 883 884 885 886 887
	geoclue_test_gui_master_log_message (gui, "Started Geoclue test UI");
	
	gtk_widget_show_all (gui->window);
}


int main (int argc, char **argv)
{
	GeoclueTestGui *gui;
	
	gtk_init (&argc, &argv);
888
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
889 890
	gui = g_object_new (GEOCLUE_TYPE_TEST_GUI, NULL);
	gtk_main ();
891
	
Jussi Kukkonen's avatar
Jussi Kukkonen committed
892 893 894 895
	g_object_unref (gui);
	
	return 0;
}