1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 * Copyright (C) 2005 Ruben Vermeersch <ruben@Lambda1.be>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <glib/gi18n.h>
26 #include "rb-proxy-config.h"
27 #include "eel-gconf-extensions.h"
28 #include "rb-preferences.h"
30 #include "rb-dialog.h"
38 static void rb_proxy_config_class_init (RBProxyConfigClass
*klass
);
39 static void rb_proxy_config_init (RBProxyConfig
*config
);
40 static void rb_proxy_config_dispose (GObject
*object
);
41 static void rb_proxy_config_finalize (GObject
*object
);
42 static void rb_proxy_config_gconf_changed_cb (GConfClient
*client
,
45 RBProxyConfig
*config
);
46 static void check_auto_proxy_config (RBProxyConfig
*config
);
47 static void get_proxy_config (RBProxyConfig
*config
);
49 static guint rb_proxy_config_signals
[LAST_SIGNAL
] = { 0 };
51 struct _RBProxyConfigPrivate
53 guint enabled_notify_id
;
57 guint auth_enabled_notify_id
;
58 guint username_notify_id
;
59 guint password_notify_id
;
62 G_DEFINE_TYPE (RBProxyConfig
, rb_proxy_config
, G_TYPE_OBJECT
)
65 rb_proxy_config_class_init (RBProxyConfigClass
*klass
)
67 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
69 object_class
->dispose
= rb_proxy_config_dispose
;
70 object_class
->finalize
= rb_proxy_config_finalize
;
72 rb_proxy_config_signals
[CONFIG_CHANGED
] =
73 g_signal_new ("config-changed",
74 G_OBJECT_CLASS_TYPE (object_class
),
76 G_STRUCT_OFFSET (RBProxyConfigClass
, config_changed
),
78 g_cclosure_marshal_VOID__VOID
,
82 g_type_class_add_private (klass
, sizeof (RBProxyConfigPrivate
));
86 rb_proxy_config_init (RBProxyConfig
*config
)
88 config
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (config
, RB_TYPE_PROXY_CONFIG
, RBProxyConfigPrivate
);
90 rb_debug ("watching HTTP proxy configuration");
92 eel_gconf_monitor_add ("/system/http_proxy");
93 config
->priv
->enabled_notify_id
=
94 eel_gconf_notification_add ("/system/http_proxy/use_http_proxy",
95 (GConfClientNotifyFunc
) rb_proxy_config_gconf_changed_cb
,
97 config
->priv
->host_notify_id
=
98 eel_gconf_notification_add ("/system/http_proxy/host",
99 (GConfClientNotifyFunc
) rb_proxy_config_gconf_changed_cb
,
101 config
->priv
->port_notify_id
=
102 eel_gconf_notification_add ("/system/http_proxy/port",
103 (GConfClientNotifyFunc
) rb_proxy_config_gconf_changed_cb
,
105 config
->priv
->auth_enabled_notify_id
=
106 eel_gconf_notification_add ("/system/http_proxy/use_authentication",
107 (GConfClientNotifyFunc
) rb_proxy_config_gconf_changed_cb
,
109 config
->priv
->username_notify_id
=
110 eel_gconf_notification_add ("/system/http_proxy/authentication_user",
111 (GConfClientNotifyFunc
) rb_proxy_config_gconf_changed_cb
,
113 config
->priv
->password_notify_id
=
114 eel_gconf_notification_add ("/system/http_proxy/authentication_password",
115 (GConfClientNotifyFunc
) rb_proxy_config_gconf_changed_cb
,
118 check_auto_proxy_config (config
);
120 get_proxy_config (config
);
124 rb_proxy_config_dispose (GObject
*object
)
126 RBProxyConfig
*config
;
128 g_return_if_fail (object
!= NULL
);
129 g_return_if_fail (RB_IS_PROXY_CONFIG (object
));
130 config
= RB_PROXY_CONFIG (object
);
131 g_return_if_fail (config
->priv
!= NULL
);
133 rb_debug ("Removing HTTP proxy config watch");
134 eel_gconf_notification_remove (config
->priv
->enabled_notify_id
);
135 eel_gconf_notification_remove (config
->priv
->host_notify_id
);
136 eel_gconf_notification_remove (config
->priv
->port_notify_id
);
137 eel_gconf_notification_remove (config
->priv
->auth_enabled_notify_id
);
138 eel_gconf_notification_remove (config
->priv
->username_notify_id
);
139 eel_gconf_notification_remove (config
->priv
->password_notify_id
);
140 eel_gconf_monitor_remove ("/system/http_proxy");
142 G_OBJECT_CLASS (rb_proxy_config_parent_class
)->dispose (object
);
146 rb_proxy_config_finalize (GObject
*object
)
148 RBProxyConfig
*config
;
150 g_return_if_fail (object
!= NULL
);
151 g_return_if_fail (RB_IS_PROXY_CONFIG (object
));
152 config
= RB_PROXY_CONFIG (object
);
153 g_return_if_fail (config
->priv
!= NULL
);
155 g_free (config
->host
);
156 g_free (config
->username
);
157 g_free (config
->password
);
159 G_OBJECT_CLASS (rb_proxy_config_parent_class
)->finalize (object
);
163 rb_proxy_config_new ()
165 return g_object_new (RB_TYPE_PROXY_CONFIG
, NULL
);
169 rb_proxy_config_gconf_changed_cb (GConfClient
*client
,
172 RBProxyConfig
*config
)
174 rb_debug ("HTTP proxy configuration changed");
175 get_proxy_config (config
);
176 g_signal_emit (config
, rb_proxy_config_signals
[CONFIG_CHANGED
], 0);
180 check_auto_proxy_config (RBProxyConfig
*config
)
184 /* complain once if auto proxy mode is enabled */
185 mode
= eel_gconf_get_string ("/system/proxy/mode");
186 if (mode
!= NULL
&& strcmp (mode
, "auto") == 0) {
187 if (eel_gconf_get_boolean (CONF_UI_AUTO_PROXY_COMPLAINT
) == FALSE
) {
188 rb_error_dialog (NULL
,
189 _("HTTP proxy configuration error"),
190 "%s", _("Rhythmbox does not support automatic proxy configuration"));
192 eel_gconf_set_boolean (CONF_UI_AUTO_PROXY_COMPLAINT
, TRUE
);
194 eel_gconf_set_boolean (CONF_UI_AUTO_PROXY_COMPLAINT
, FALSE
);
201 get_proxy_config (RBProxyConfig
*config
)
203 config
->enabled
= eel_gconf_get_boolean ("/system/http_proxy/use_http_proxy");
205 g_free (config
->host
);
206 config
->host
= eel_gconf_get_string ("/system/http_proxy/host");
207 config
->port
= eel_gconf_get_integer ("/system/http_proxy/port");
209 config
->auth_enabled
= eel_gconf_get_boolean ("/system/http_proxy/use_authentication");
210 g_free (config
->username
);
211 g_free (config
->password
);
212 config
->username
= eel_gconf_get_string ("/system/http_proxy/authentication_user");
213 config
->password
= eel_gconf_get_string ("/system/http_proxy/authentication_password");
214 if (config
->username
== NULL
|| config
->password
== NULL
) {
215 rb_debug ("HTTP proxy authentication enabled, but username or password is missing");
216 config
->auth_enabled
= FALSE
;
219 if (config
->enabled
) {
220 if (config
->host
== NULL
|| config
->host
[0] == '\0') {
221 rb_debug ("HTTP proxy is enabled, but no proxy host is specified");
222 config
->enabled
= FALSE
;
223 } else if (config
->auth_enabled
)
224 rb_debug ("HTTP proxy URL is http://%s:<password>@%s:%u/",
225 config
->username
, config
->host
, config
->port
);
227 rb_debug ("HTTP proxy URL is http://%s:%u/",
228 config
->host
, config
->port
);
230 rb_debug ("HTTP proxy is disabled");
234 #if defined(HAVE_LIBSOUP)
236 rb_proxy_config_get_libsoup_uri (RBProxyConfig
*config
)
240 if (!config
->enabled
)
243 uri
= g_new0 (SoupUri
, 1);
244 uri
->protocol
= SOUP_PROTOCOL_HTTP
;
246 uri
->host
= g_strdup (config
->host
);
247 uri
->port
= config
->port
;
248 if (config
->auth_enabled
) {
249 uri
->user
= g_strdup (config
->username
);
250 uri
->passwd
= g_strdup (config
->password
);