File: rb-proxy-config.c

package info (click to toggle)
rhythmbox 0.11.6-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 26,232 kB
  • ctags: 11,434
  • sloc: ansic: 97,824; xml: 36,080; sh: 9,341; python: 4,193; makefile: 2,130; cpp: 153
file content (312 lines) | stat: -rw-r--r-- 9,583 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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 *  Copyright (C) 2005  Ruben Vermeersch <ruben@Lambda1.be>
 *
 *  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.
 *
 *  The Rhythmbox authors hereby grants permission for non-GPL compatible
 *  GStreamer plugins to be used and distributed together with GStreamer
 *  and Rhythmbox. This permission is above and beyond the permissions granted
 *  by the GPL license by which Rhythmbox is covered. If you modify this code
 *  you may extend this exception to your version of the code, but you are not
 *  obligated to do so. If you do not wish to do so, delete this exception
 *  statement from your 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 St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 */

#include "config.h"

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

#include "rb-proxy-config.h"
#include "eel-gconf-extensions.h"
#include "rb-preferences.h"
#include "rb-debug.h"
#include "rb-dialog.h"

/**
 * SECTION:rb-proxy-config
 * @short_description: GConf HTTP proxy retriever
 *
 * This class knows how to retrieve the current HTTP proxy configuration
 * from GConf, and also emits signals when the configuration is changed.
 * It only supports manual proxy configuration (with authentication).  It
 * does not support any of the various automatic proxy configuration schemes.
 */

enum
{
	CONFIG_CHANGED,
	LAST_SIGNAL
};

static void	rb_proxy_config_class_init (RBProxyConfigClass *klass);
static void	rb_proxy_config_init (RBProxyConfig *config);
static void	rb_proxy_config_dispose (GObject *object);
static void	rb_proxy_config_finalize (GObject *object);
static void	rb_proxy_config_gconf_changed_cb (GConfClient *client,
						  guint cnxn_id,
				 		  GConfEntry *entry,
				 		  RBProxyConfig *config);
static void	check_auto_proxy_config (RBProxyConfig *config);
static void	get_proxy_config (RBProxyConfig *config);

static guint	rb_proxy_config_signals[LAST_SIGNAL] = { 0 };

struct _RBProxyConfigPrivate
{
	guint enabled_notify_id;
	guint host_notify_id;
	guint port_notify_id;

	guint auth_enabled_notify_id;
	guint username_notify_id;
	guint password_notify_id;
};

G_DEFINE_TYPE (RBProxyConfig, rb_proxy_config, G_TYPE_OBJECT)

static void
rb_proxy_config_class_init (RBProxyConfigClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = rb_proxy_config_dispose;
	object_class->finalize = rb_proxy_config_finalize;

	/**
	 * RBProxyConfig::config-changed:
	 * @config: the #RBProxyConfig
	 *
	 * Emitted when the HTTP proxy configuration is changed.
	 */
	rb_proxy_config_signals [CONFIG_CHANGED] =
		g_signal_new ("config-changed",
			      G_OBJECT_CLASS_TYPE (object_class),
			      G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (RBProxyConfigClass, config_changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE,
			      0);

	g_type_class_add_private (klass, sizeof (RBProxyConfigPrivate));
}

static void
rb_proxy_config_init (RBProxyConfig *config)
{
	config->priv = G_TYPE_INSTANCE_GET_PRIVATE (config, RB_TYPE_PROXY_CONFIG, RBProxyConfigPrivate);

	rb_debug ("watching HTTP proxy configuration");

	eel_gconf_monitor_add ("/system/http_proxy");
	config->priv->enabled_notify_id =
		eel_gconf_notification_add ("/system/http_proxy/use_http_proxy",
					(GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
					config);
	config->priv->host_notify_id =
		eel_gconf_notification_add ("/system/http_proxy/host",
					(GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
					config);
	config->priv->port_notify_id =
		eel_gconf_notification_add ("/system/http_proxy/port",
					(GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
					config);
	config->priv->auth_enabled_notify_id =
		eel_gconf_notification_add ("/system/http_proxy/use_authentication",
					(GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
					config);
	config->priv->username_notify_id =
		eel_gconf_notification_add ("/system/http_proxy/authentication_user",
					(GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
					config);
	config->priv->password_notify_id =
		eel_gconf_notification_add ("/system/http_proxy/authentication_password",
					(GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
					config);

	check_auto_proxy_config (config);

	get_proxy_config (config);
}

static void
rb_proxy_config_dispose (GObject *object)
{
	RBProxyConfig *config;

	g_return_if_fail (object != NULL);
	g_return_if_fail (RB_IS_PROXY_CONFIG (object));
	config = RB_PROXY_CONFIG (object);
	g_return_if_fail (config->priv != NULL);

	rb_debug ("Removing HTTP proxy config watch");
	eel_gconf_notification_remove (config->priv->enabled_notify_id);
	eel_gconf_notification_remove (config->priv->host_notify_id);
	eel_gconf_notification_remove (config->priv->port_notify_id);
	eel_gconf_notification_remove (config->priv->auth_enabled_notify_id);
	eel_gconf_notification_remove (config->priv->username_notify_id);
	eel_gconf_notification_remove (config->priv->password_notify_id);
	eel_gconf_monitor_remove ("/system/http_proxy");

	G_OBJECT_CLASS (rb_proxy_config_parent_class)->dispose (object);
}

static void
rb_proxy_config_finalize (GObject *object)
{
	RBProxyConfig *config;

	g_return_if_fail (object != NULL);
	g_return_if_fail (RB_IS_PROXY_CONFIG (object));
	config = RB_PROXY_CONFIG (object);
	g_return_if_fail (config->priv != NULL);

	g_free (config->host);
	g_free (config->username);
	g_free (config->password);

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

/**
 * rb_proxy_config_new:
 *
 * Return value: new proxy configuration retriever
 */
RBProxyConfig *
rb_proxy_config_new ()
{
	return g_object_new (RB_TYPE_PROXY_CONFIG, NULL);
}

static void
rb_proxy_config_gconf_changed_cb (GConfClient *client,
				  guint cnxn_id,
		 		  GConfEntry *entry,
		 		  RBProxyConfig *config)
{
	rb_debug ("HTTP proxy configuration changed");
	get_proxy_config (config);
	g_signal_emit (config, rb_proxy_config_signals[CONFIG_CHANGED], 0);
}

static void
check_auto_proxy_config (RBProxyConfig *config)
{
	char *mode;

	/* complain once if auto proxy mode is enabled */
	mode = eel_gconf_get_string ("/system/proxy/mode");
	if (mode != NULL && strcmp (mode, "auto") == 0) {
		if (eel_gconf_get_boolean (CONF_UI_AUTO_PROXY_COMPLAINT) == FALSE) {
			rb_error_dialog (NULL,
					 _("HTTP proxy configuration error"),
					 "%s", _("Rhythmbox does not support automatic proxy configuration"));
		}
		eel_gconf_set_boolean (CONF_UI_AUTO_PROXY_COMPLAINT, TRUE);
	} else {
		eel_gconf_set_boolean (CONF_UI_AUTO_PROXY_COMPLAINT, FALSE);
	}

	g_free (mode);
}

static void
get_proxy_config (RBProxyConfig *config)
{
	config->enabled = eel_gconf_get_boolean ("/system/http_proxy/use_http_proxy");

	g_free (config->host);
	config->host = eel_gconf_get_string ("/system/http_proxy/host");
	config->port = eel_gconf_get_integer ("/system/http_proxy/port");

	config->auth_enabled = eel_gconf_get_boolean ("/system/http_proxy/use_authentication");
	g_free (config->username);
	g_free (config->password);
	config->username = eel_gconf_get_string ("/system/http_proxy/authentication_user");
	config->password = eel_gconf_get_string ("/system/http_proxy/authentication_password");
	if (config->username == NULL || config->password == NULL) {
		rb_debug ("HTTP proxy authentication enabled, but username or password is missing");
		config->auth_enabled = FALSE;
	}

	if (config->enabled) {
		if (config->host == NULL || config->host[0] == '\0') {
			rb_debug ("HTTP proxy is enabled, but no proxy host is specified");
			config->enabled = FALSE;
		} else if (config->auth_enabled)
			rb_debug ("HTTP proxy URL is http://%s:<password>@%s:%u/",
				  config->username, config->host, config->port);
		else
			rb_debug ("HTTP proxy URL is http://%s:%u/",
				  config->host, config->port);
	} else {
		rb_debug ("HTTP proxy is disabled");
	}
}

/**
 * rb_proxy_config_get_libsoup_uri:
 * @config: a #RBProxyConfig
 *
 * Return value: a libsoup URI object containing the current HTTP proxy configuration.
 */
#if defined(HAVE_LIBSOUP_2_4)
SoupURI *
rb_proxy_config_get_libsoup_uri (RBProxyConfig *config)
{
	SoupURI *uri = NULL;

	if (!config->enabled)
		return NULL;

	uri = soup_uri_new (NULL);
	soup_uri_set_scheme (uri, SOUP_URI_SCHEME_HTTP);
	soup_uri_set_host (uri, config->host);
	soup_uri_set_port (uri, config->port);

	if (config->auth_enabled) {
		soup_uri_set_user (uri, config->username);
		soup_uri_set_password (uri, config->password);
	}

	return uri;
}
#elif defined(HAVE_LIBSOUP_2_2)
SoupUri *
rb_proxy_config_get_libsoup_uri (RBProxyConfig *config)
{
	SoupUri *uri = NULL;

	if (!config->enabled)
		return NULL;

	uri = g_new0 (SoupUri, 1);
	uri->protocol = SOUP_PROTOCOL_HTTP;

	uri->host = g_strdup (config->host);
	uri->port = config->port;
	if (config->auth_enabled) {
		uri->user = g_strdup (config->username);
		uri->passwd = g_strdup (config->password);
	}

	return uri;
}
#endif