udapted vi.po
[rhythmbox.git] / lib / rb-proxy-config.c
blobe531446918dfab9cc6c8c3b193fbcf6415b69a76
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.
21 #include "config.h"
23 #include <string.h>
24 #include <glib/gi18n.h>
26 #include "rb-proxy-config.h"
27 #include "eel-gconf-extensions.h"
28 #include "rb-preferences.h"
29 #include "rb-debug.h"
30 #include "rb-dialog.h"
32 enum
34 CONFIG_CHANGED,
35 LAST_SIGNAL
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,
43 guint cnxn_id,
44 GConfEntry *entry,
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;
54 guint host_notify_id;
55 guint port_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)
64 static void
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),
75 G_SIGNAL_RUN_LAST,
76 G_STRUCT_OFFSET (RBProxyConfigClass, config_changed),
77 NULL, NULL,
78 g_cclosure_marshal_VOID__VOID,
79 G_TYPE_NONE,
80 0);
82 g_type_class_add_private (klass, sizeof (RBProxyConfigPrivate));
85 static void
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,
96 config);
97 config->priv->host_notify_id =
98 eel_gconf_notification_add ("/system/http_proxy/host",
99 (GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
100 config);
101 config->priv->port_notify_id =
102 eel_gconf_notification_add ("/system/http_proxy/port",
103 (GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
104 config);
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,
108 config);
109 config->priv->username_notify_id =
110 eel_gconf_notification_add ("/system/http_proxy/authentication_user",
111 (GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
112 config);
113 config->priv->password_notify_id =
114 eel_gconf_notification_add ("/system/http_proxy/authentication_password",
115 (GConfClientNotifyFunc) rb_proxy_config_gconf_changed_cb,
116 config);
118 check_auto_proxy_config (config);
120 get_proxy_config (config);
123 static void
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);
145 static void
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);
162 RBProxyConfig *
163 rb_proxy_config_new ()
165 return g_object_new (RB_TYPE_PROXY_CONFIG, NULL);
168 static void
169 rb_proxy_config_gconf_changed_cb (GConfClient *client,
170 guint cnxn_id,
171 GConfEntry *entry,
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);
179 static void
180 check_auto_proxy_config (RBProxyConfig *config)
182 char *mode;
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);
193 } else {
194 eel_gconf_set_boolean (CONF_UI_AUTO_PROXY_COMPLAINT, FALSE);
197 g_free (mode);
200 static void
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);
226 else
227 rb_debug ("HTTP proxy URL is http://%s:%u/",
228 config->host, config->port);
229 } else {
230 rb_debug ("HTTP proxy is disabled");
234 #if defined(HAVE_LIBSOUP)
235 SoupUri *
236 rb_proxy_config_get_libsoup_uri (RBProxyConfig *config)
238 SoupUri *uri = NULL;
240 if (!config->enabled)
241 return NULL;
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);
253 return uri;
255 #endif