summaryrefslogtreecommitdiff
path: root/src/egg-dbus-proxy.c
blob: 84906826076e5c771500aea41d28c74e7800d034 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006-2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * This program 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.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <string.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <dbus/dbus-glib.h>

#include "egg-debug.h"
#include "egg-dbus-monitor.h"
#include "egg-dbus-proxy.h"

static void     egg_dbus_proxy_finalize   (GObject        *object);

#define EGG_DBUS_PROXY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_DBUS_PROXY, EggDbusProxyPrivate))

/* this is a managed proxy, i.e. a proxy that handles messagebus and DBUS service restarts. */
struct EggDbusProxyPrivate
{
	gchar			*service;
	gchar			*interface;
	gchar			*path;
	DBusGProxy		*proxy;
	EggDbusMonitor		*monitor;
	gboolean		 assigned;
	DBusGConnection		*connection;
	gulong			 monitor_callback_id;
};

enum {
	PROXY_STATUS,
	LAST_SIGNAL
};

static guint	     signals [LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (EggDbusProxy, egg_dbus_proxy, G_TYPE_OBJECT)

/**
 * egg_dbus_proxy_connect:
 * @proxy: This class instance
 * Return value: success
 **/
static gboolean
egg_dbus_proxy_connect (EggDbusProxy *proxy)
{
	GError *error = NULL;

	g_return_val_if_fail (EGG_IS_DBUS_PROXY (proxy), FALSE);

	/* are already connected? */
	if (proxy->priv->proxy != NULL) {
		egg_debug ("already connected to %s", proxy->priv->service);
		return FALSE;
	}

	proxy->priv->proxy = dbus_g_proxy_new_for_name_owner (proxy->priv->connection,
							      proxy->priv->service,
							      proxy->priv->path,
							      proxy->priv->interface,
							      &error);
	/* check for any possible error */
	if (error) {
		egg_warning ("DBUS error: %s", error->message);
		g_error_free (error);
		proxy->priv->proxy = NULL;
	}

	/* shouldn't be, but make sure proxy valid */
	if (proxy->priv->proxy == NULL) {
		egg_debug ("proxy is NULL, maybe the daemon responsible "
			   "for %s is not running?", proxy->priv->service);
		return FALSE;
	}

	g_signal_emit (proxy, signals [PROXY_STATUS], 0, TRUE);

	return TRUE;
}

/**
 * egg_dbus_proxy_disconnect:
 * @proxy: This class instance
 * Return value: success
 **/
static gboolean
egg_dbus_proxy_disconnect (EggDbusProxy *proxy)
{
	g_return_val_if_fail (EGG_IS_DBUS_PROXY (proxy), FALSE);

	/* are already disconnected? */
	if (proxy->priv->proxy == NULL) {
		if (proxy->priv->service)
			egg_debug ("already disconnected from %s", proxy->priv->service);
		else 
			egg_debug ("already disconnected.");
		return FALSE;
	}

	g_signal_emit (proxy, signals [PROXY_STATUS], 0, FALSE);

	g_object_unref (proxy->priv->proxy);
	proxy->priv->proxy = NULL;

	return TRUE;
}

/**
 * dbus_monitor_connection_cb:
 * @proxy: The dbus raw proxy
 * @status: The status of the service, where TRUE is connected
 * @screensaver: This class instance
 **/
static void
dbus_monitor_connection_cb (EggDbusMonitor *monitor, gboolean status, EggDbusProxy *proxy)
{
	g_return_if_fail (EGG_IS_DBUS_PROXY (proxy));
	if (proxy->priv->assigned == FALSE)
		return;
	if (status)
		egg_dbus_proxy_connect (proxy);
	else
		egg_dbus_proxy_disconnect (proxy);
}

/**
 * egg_dbus_proxy_assign:
 * @proxy: This class instance
 * @connections: The bus connection
 * @service: The DBUS service name
 * @interface: The DBUS interface
 * @path: The DBUS path
 * Return value: The DBUS proxy, or NULL if we haven't connected yet.
 **/
DBusGProxy *
egg_dbus_proxy_assign (EggDbusProxy *proxy, DBusGConnection *connection,
		       const gchar *service, const gchar *path, const gchar *interface)
{
	g_return_val_if_fail (EGG_IS_DBUS_PROXY (proxy), NULL);
	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (service != NULL, NULL);
	g_return_val_if_fail (interface != NULL, NULL);
	g_return_val_if_fail (path != NULL, NULL);

	if (proxy->priv->assigned) {
		egg_warning ("already assigned proxy!");
		return NULL;
	}

	proxy->priv->service = g_strdup (service);
	proxy->priv->interface = g_strdup (interface);
	proxy->priv->path = g_strdup (path);
	proxy->priv->connection = connection;
	proxy->priv->assigned = TRUE;

	/* We have to save the connection and remove the signal id later as
	   instances of this object are likely to be registering with a
	   singleton object many times */
	egg_dbus_monitor_assign (proxy->priv->monitor, connection, service);

	/* try to connect and return proxy (or NULL if invalid) */
	egg_dbus_proxy_connect (proxy);

	return proxy->priv->proxy;
}

/**
 * egg_dbus_proxy_get_proxy:
 * @proxy: This class instance
 * Return value: The DBUS proxy, or NULL if we are not connected
 **/
DBusGProxy *
egg_dbus_proxy_get_proxy (EggDbusProxy *proxy)
{
	g_return_val_if_fail (EGG_IS_DBUS_PROXY (proxy), NULL);
	if (proxy->priv->assigned == FALSE)
		return NULL;
	return proxy->priv->proxy;
}

/**
 * egg_dbus_proxy_is_connected:
 * @proxy: This class instance
 * Return value: if we are connected to a valid proxy
 **/
gboolean
egg_dbus_proxy_is_connected (EggDbusProxy *proxy)
{
	g_return_val_if_fail (EGG_IS_DBUS_PROXY (proxy), FALSE);
	if (proxy->priv->assigned == FALSE)
		return FALSE;
	if (proxy->priv->proxy == NULL)
		return FALSE;
	return TRUE;
}

/**
 * egg_dbus_proxy_class_init:
 * @proxy: This class instance
 **/
static void
egg_dbus_proxy_class_init (EggDbusProxyClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = egg_dbus_proxy_finalize;
	g_type_class_add_private (klass, sizeof (EggDbusProxyPrivate));

	signals [PROXY_STATUS] =
		g_signal_new ("proxy-status",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (EggDbusProxyClass, proxy_status),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__BOOLEAN,
			      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
}

/**
 * egg_dbus_proxy_init:
 * @egg_dbus_proxy: This class instance
 **/
static void
egg_dbus_proxy_init (EggDbusProxy *proxy)
{
	proxy->priv = EGG_DBUS_PROXY_GET_PRIVATE (proxy);

	proxy->priv->connection = NULL;
	proxy->priv->proxy = NULL;
	proxy->priv->service = NULL;
	proxy->priv->interface = NULL;
	proxy->priv->path = NULL;
	proxy->priv->assigned = FALSE;
	proxy->priv->monitor = egg_dbus_monitor_new ();
	proxy->priv->monitor_callback_id =
		g_signal_connect (proxy->priv->monitor, "connection-changed",
				  G_CALLBACK (dbus_monitor_connection_cb), proxy);
	proxy->priv->monitor_callback_id = 0;
}

/**
 * egg_dbus_proxy_finalize:
 * @object: This class instance
 **/
static void
egg_dbus_proxy_finalize (GObject *object)
{
	EggDbusProxy *proxy;
	g_return_if_fail (object != NULL);
	g_return_if_fail (EGG_IS_DBUS_PROXY (object));

	proxy = EGG_DBUS_PROXY (object);
	proxy->priv = EGG_DBUS_PROXY_GET_PRIVATE (proxy);

	if (proxy->priv->monitor_callback_id != 0)
		g_signal_handler_disconnect (proxy->priv->monitor,
					     proxy->priv->monitor_callback_id);

	egg_dbus_proxy_disconnect (proxy);

	if (proxy->priv->proxy != NULL)
		g_object_unref (proxy->priv->proxy);
	g_object_unref (proxy->priv->monitor);
	g_free (proxy->priv->service);
	g_free (proxy->priv->interface);
	g_free (proxy->priv->path);

	G_OBJECT_CLASS (egg_dbus_proxy_parent_class)->finalize (object);
}

/**
 * egg_dbus_proxy_new:
 * Return value: new class instance.
 **/
EggDbusProxy *
egg_dbus_proxy_new (void)
{
	EggDbusProxy *proxy;
	proxy = g_object_new (EGG_TYPE_DBUS_PROXY, NULL);
	return EGG_DBUS_PROXY (proxy);
}