File: phonefsod-dbus.c

package info (click to toggle)
phonefsod 0.1%2Bgit20110827-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 344 kB
  • sloc: ansic: 2,076; sh: 56; makefile: 44
file content (522 lines) | stat: -rw-r--r-- 15,086 bytes parent folder | download
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
217
218
219
220
221
222
223
224
225
226
227
228
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
 *  Copyright (C) 2009-2010
 *      Authors (alphabetical) :
 *              Klaus 'mrmoku' Kurzmann <mok@fluxnetz.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Public License as published by
 *  the Free Software Foundation; version 2 of the license or any later version.
 *
 *  This program 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 Lesser Public License for more details.
 */

#include <string.h>
#include <gio/gio.h>
#include <shr-bindings.h>
#include "phonefsod-dbus.h"
#include "phonefsod-dbus-common.h"
#include "phonefsod-fso.h"
#include "phonefsod-globals.h"

static guint phonefsod_owner_id = 0;
static guint phoneuid_watcher_id = 0;
static PhonefsoUsage *usage;

/* phonefso - dbus method handlers */
static gboolean _set_offline_mode(PhonefsoUsage *object, GDBusMethodInvocation *invocation, gboolean state, gpointer user_data);
static gboolean _get_offline_mode(PhonefsoUsage *object, GDBusMethodInvocation *invocation, gpointer user_data);
static gboolean _set_default_brightness(PhonefsoUsage *object, GDBusMethodInvocation *invocation, int brightness, gpointer user_data);
static gboolean _get_default_brightness(PhonefsoUsage *object, GDBusMethodInvocation *invocation, gpointer user_data);
static gboolean _set_pdp_credentials(PhonefsoUsage *object, GDBusMethodInvocation *invocation, const char *apn, const char *user, const char *password, gpointer user_data);
static gboolean _set_pin(PhonefsoUsage *object, GDBusMethodInvocation *invocation, const char *pin, gboolean save, gpointer user_data);


/* private helper functions */
static void _write_pdp_credentials_to_config(void);
static void _write_default_brightness_to_config(void);
static void _write_offline_mode_to_config(void);

/* g_bus_own_name callbacks */
static void _on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data);
static void _on_name_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data);
static void _on_name_lost (GDBusConnection *connection, const gchar *name, gpointer user_data);

/* g_bus_watch_proxy callbacks */
static void _on_phoneuid_appeared(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data);
static void _on_phoneuid_vanished(GDBusConnection *connection, const gchar *name, gpointer user_data);

/* handle dbus errors */
static void _handle_dbus_error(GError *error, const gchar *msg);


int
phonefsod_dbus_setup()
{
	GError *error = NULL;

	system_bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
	if (error) {
		g_error("%d: %s", error->code, error->message);
		g_error_free(error);
		return 0;
	}

	/* connect and init FSO */
	if (!fso_init()) {
		return 0;
	}

	phonefsod_owner_id = g_bus_own_name_on_connection
		(system_bus, PHONEFSOD_SERVICE,
		 G_BUS_NAME_OWNER_FLAGS_REPLACE | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
		 _on_bus_acquired, _on_name_acquired, _on_name_lost, NULL);

	phoneuid_watcher_id = g_bus_watch_name_on_connection
		(system_bus, PHONEUID_SERVICE,
		 G_BUS_NAME_WATCHER_FLAGS_NONE,
		 _on_phoneuid_appeared, _on_phoneuid_vanished, NULL, NULL);

	phoneui.notification = phoneui_notification_proxy_new_sync
		(system_bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
		 PHONEUID_SERVICE, PHONEUID_NOTIFICATION_PATH, NULL, &error);

	phoneui.call_management = phoneui_call_management_proxy_new_sync
		(system_bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
		 PHONEUID_SERVICE, PHONEUID_CALL_MANAGEMENT_PATH, NULL, &error);

	phoneui.idle_screen = phoneui_idle_screen_proxy_new_sync
		(system_bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
		 PHONEUID_SERVICE, PHONEUID_IDLE_SCREEN_PATH, NULL, &error);

	phoneui.messages = phoneui_messages_proxy_new_sync
		(system_bus, G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
		 PHONEUID_SERVICE, PHONEUID_MESSAGES_PATH, NULL, &error);

	return 1;
}

void
phonefsod_dbus_shutdown()
{
	g_bus_unown_name(phonefsod_owner_id);
	g_object_unref(system_bus);
}


/* handlers for g_bus_own_name */

static void
_on_bus_acquired (GDBusConnection *connection,
		   const gchar     *name,
		   gpointer         user_data)
{
	/* This is where we'd export some objects on the bus */
	GError *error = NULL;

	g_debug("Yo, on the bus :-) (%s)", name);

	usage = phonefso_usage_skeleton_new();
	g_signal_connect(usage, "handle-set-offline-mode", G_CALLBACK(_set_offline_mode), NULL);
	g_signal_connect(usage, "handle-get-offline-mode", G_CALLBACK(_get_offline_mode), NULL);
	g_signal_connect(usage, "handle-set-default-brightness", G_CALLBACK(_set_default_brightness), NULL);
	g_signal_connect(usage, "handle-get-default-brightness", G_CALLBACK(_get_default_brightness), NULL);
	g_signal_connect(usage, "handle-set-pdp-credentials", G_CALLBACK(_set_pdp_credentials), NULL);
	g_signal_connect(usage, "handle-set-pin", G_CALLBACK(_set_pin), NULL);


	g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(usage),
					     connection,
					     PHONEFSOD_USAGE_PATH,
					     &error);

	if (error) {
		g_critical("Failed to register %s: %s", PHONEFSOD_USAGE_PATH, error->message);
		g_error_free(error);
	}
}

static void
_on_name_acquired (GDBusConnection *connection,
		    const gchar     *name,
		    gpointer         user_data)
{
	g_debug ("Acquired the name %s on the session bus\n", name);
}

static void
_on_name_lost (GDBusConnection *connection,
		const gchar     *name,
		gpointer         user_data)
{
	g_debug ("Lost the name %s on the session bus\n", name);
}


/* handlers for g_bus_watch_name */

static void
_on_phoneuid_appeared(GDBusConnection *connection,
			 const gchar *name,
			 const gchar *name_owner,
			 gpointer user_data)
{
	g_debug("yeah, phoneuid is on the bus (%s)", name_owner);

	if (sim_auth_needed && phoneui.notification) {
		phoneui_notification_call_display_sim_auth
			(phoneui.notification, 0, NULL,
			 phoneui_show_sim_auth_cb, NULL);
		return;
	}

}

static void
_on_phoneuid_vanished(GDBusConnection *connection,
			 const gchar *name,
			 gpointer user_data)
{
	g_critical("!!! ouch, phoneuid is gone - telephony won't work anymore !!!");
}



/* method call handlers */
static gboolean
_set_offline_mode(PhonefsoUsage *object,
		GDBusMethodInvocation *invocation,
		gboolean state,
		gpointer user_data)
{
	if (offline_mode ^ state) {
		offline_mode = state;
		_write_offline_mode_to_config();
		fso_set_functionality();
	}

	phonefso_usage_complete_set_offline_mode(object, invocation);

	return TRUE;
}

static gboolean
_get_offline_mode(PhonefsoUsage *object,
		    GDBusMethodInvocation *invocation,
		    gpointer user_data)
{
	phonefso_usage_complete_get_offline_mode(object, invocation, offline_mode);

	return TRUE;
}

static gboolean
_set_default_brightness(PhonefsoUsage *object,
			   GDBusMethodInvocation *invocation,
			   int brightness,
			   gpointer user_data)
{
	default_brightness = brightness;
	_write_default_brightness_to_config();
	fso_dimit(100, DIM_SCREEN_ALWAYS);

	phonefso_usage_complete_set_default_brightness(object, invocation);
}

static gboolean
_get_default_brightness(PhonefsoUsage *object,
			   GDBusMethodInvocation *invocation,
			   gpointer user_data)
{
	phonefso_usage_complete_get_default_brightness(object, invocation, default_brightness);

	return TRUE;
}

static gboolean
_set_pdp_credentials(PhonefsoUsage *object,
			GDBusMethodInvocation *invocation,
			const char *apn,
			const char *user,
			const char *password,			gpointer user_data)
{
	if (pdp_apn) {
		free(pdp_apn);
	}
	pdp_apn = strdup(apn);
	if (pdp_user) {
		free(pdp_user);
	}
	pdp_user = strdup(user);
	if (pdp_password) {
		free(pdp_password);
	}
	pdp_password = strdup(password);
	_write_pdp_credentials_to_config();
	fso_pdp_set_credentials();

	phonefso_usage_complete_set_pdp_credentials(object, invocation);

	return TRUE;
}

static gboolean
_set_pin(PhonefsoUsage *object,
	  GDBusMethodInvocation *invocation,
	  const char *pin,
	  gboolean save,
	  gpointer user_data)
{
	if (sim_pin)
		free(sim_pin);
	sim_pin = strdup(pin);
	fso_set_functionality();
	if (save) {
		/* TODO: save to config... maybe including ident of SIM
		 *		to be able to save PINs for multiple SIMs */
	}

	phonefso_usage_complete_set_pin(object, invocation);

	return TRUE;
}


/* private helpers */

static void
_write_offline_mode_to_config(void)
{
	GError *error = NULL;
	GKeyFile *keyfile;
	GKeyFileFlags flags;
	gsize size;
	char *config_data;

	keyfile = g_key_file_new();
	flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
	if (g_key_file_load_from_file
		(keyfile, PHONEFSOD_CONFIG, flags, &error)) {
		g_key_file_set_boolean(keyfile, "gsm",
					  "offline_mode", offline_mode);
		config_data = g_key_file_to_data(keyfile, &size, NULL);
	if (!config_data) {
		g_message("could not convert config data to write \
		offline mode to config");
	}
	else {
		if (!g_file_set_contents(PHONEFSOD_CONFIG, config_data,
			size, &error))
		{
			g_warning("failed writing offline mode \
			to config: %s", error->message);
			g_error_free(error);
		}
		g_free(config_data);
	}
		}

		if (keyfile)
			g_key_file_free(keyfile);
}

static void
_write_default_brightness_to_config(void)
{
	GError *error = NULL;
	GKeyFile *keyfile;
	GKeyFileFlags flags;
	gsize size;
	char *config_data;

	keyfile = g_key_file_new();
	flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
	if (g_key_file_load_from_file
		(keyfile, PHONEFSOD_CONFIG, flags, &error)) {
		g_key_file_set_integer(keyfile, "idle",
					  "default_brightness", default_brightness);
		config_data = g_key_file_to_data(keyfile, &size, NULL);
		if (!config_data) {
			g_message("could not convert config data to write \
			default brightness to config");
		}
		else {
			if (!g_file_set_contents(PHONEFSOD_CONFIG, config_data,
				size, &error))
			{
				g_warning("failed writing default brightness \
				to config: %s", error->message);
				g_error_free(error);
			}
			g_free(config_data);
		}
	}
	if (keyfile)
		g_key_file_free(keyfile);
}

static void
_write_pdp_credentials_to_config(void)
{
	GError *error = NULL;
	GKeyFile *keyfile;
	GKeyFileFlags flags;
	gsize size;
	char *config_data;

	keyfile = g_key_file_new();
	flags = G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS;
	if (g_key_file_load_from_file
		(keyfile, PHONEFSOD_CONFIG, flags, &error)) {
		g_key_file_set_string(keyfile, "gsm", "pdp_apn", pdp_apn);
	g_key_file_set_string(keyfile, "gsm", "pdp_user", pdp_user);
	g_key_file_set_string(keyfile, "gsm", "pdp_password", pdp_password);
	config_data = g_key_file_to_data(keyfile, &size, NULL);
	if (!config_data) {
		g_message("could not convert config data to write \
		default brightness to config");
	}
	else {
		if (!g_file_set_contents(PHONEFSOD_CONFIG, config_data,
			size, &error))
		{
			g_warning("failed writing pdp credentials \
			to config: %s", error->message);
			g_error_free(error);
		}
		g_free(config_data);
	}
		}

		if (keyfile)
			g_key_file_free(keyfile);
}


/* phoneuid - dbus callbacks */

void phoneui_show_incoming_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_call_management_call_display_incoming_finish
			(PHONEUI_CALL_MANAGEMENT(source), res, &error);
	_handle_dbus_error(error, "failed showing incoming call");
}

void phoneui_hide_incoming_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_call_management_call_hide_incoming_finish
			(PHONEUI_CALL_MANAGEMENT(source), res, &error);
	_handle_dbus_error(error, "failed hiding incoming call");
}

void phoneui_show_outgoing_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_call_management_call_display_outgoing_finish
			(PHONEUI_CALL_MANAGEMENT(source), res, &error);
	_handle_dbus_error(error, "failed showing outgoing call");
}

void phoneui_hide_outgoing_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_call_management_call_hide_outgoing_finish
			(PHONEUI_CALL_MANAGEMENT(source), res, &error);
	_handle_dbus_error(error, "failed hiding outgoing call");
}

void phoneui_display_message_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_messages_call_display_message_finish
			(PHONEUI_MESSAGES(source), res, &error);
	_handle_dbus_error(error, "failed displaying message");
}

void phoneui_show_dialog_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_notification_call_display_dialog_finish
			(PHONEUI_NOTIFICATION(source), res, &error);
	_handle_dbus_error(error, "failed displaying dialog");
}

void phoneui_show_sim_auth_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_notification_call_display_sim_auth_finish
			(PHONEUI_NOTIFICATION(source), res, &error);
	/* if showing sim auth worked... and _only_ then */
	if (!error)
		sim_auth_needed = FALSE;
	_handle_dbus_error(error, "failed displaying sim auth");
}

void phoneui_hide_sim_auth_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_notification_call_hide_sim_auth_finish
			(PHONEUI_NOTIFICATION(source), res, &error);
	_handle_dbus_error(error, "failed hiding sim auth");
}

void phoneui_show_ussd_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_notification_call_display_ussd_finish
			(PHONEUI_NOTIFICATION(source), res, &error);
	_handle_dbus_error(error, "failed displaying ussd");
}

void phoneui_show_idle_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_idle_screen_call_display_finish
			(PHONEUI_IDLE_SCREEN(source), res, &error);
	_handle_dbus_error(error, "failed displaying idle screen");
}

void phoneui_hide_idle_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_idle_screen_call_hide_finish
			(PHONEUI_IDLE_SCREEN(source), res, &error);
	_handle_dbus_error(error, "failed hiding idle screen");
}

void phoneui_toggle_idle_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_idle_screen_call_toggle_finish
			(PHONEUI_IDLE_SCREEN(source), res, &error);
	_handle_dbus_error(error, "failed toggling idle screen");
}

void phoneui_activate_screensaver_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_idle_screen_call_activate_screensaver_finish
			(PHONEUI_IDLE_SCREEN(source), res, &error);
	_handle_dbus_error(error, "failed activating screensaver");
}

void phoneui_deactivate_screensaver_cb(GObject *source, GAsyncResult *res, gpointer data)
{
	GError *error = NULL;
	phoneui_idle_screen_call_deactivate_screensaver_finish
			(PHONEUI_IDLE_SCREEN(source), res, &error);
	_handle_dbus_error(error, "failed deactivating screensaver");
}


void _handle_dbus_error(GError* error, const gchar* msg)
{
	if (error) {
		g_critical("%s: (%d) %s", msg, error->code, error->message);
		g_error_free(error);
	}
}