GApplication: allow handles_commandline and service
[platform/upstream/glib.git] / gio / gproxyaddress.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Collabora, Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors: Nicolas Dufresne <nicolas.dufresne@collabora.co.uk>
21  */
22
23 #include <config.h>
24 #include <glib.h>
25 #include <string.h>
26
27 #include <gio/gsocketaddress.h>
28
29 #include "gproxyaddress.h"
30 #include "glibintl.h"
31
32 /**
33  * SECTION:gproxyaddress
34  * @short_description: An internet address with proxy information
35  * @include: gio/gio.h
36  *
37  * Support for proxied #GInetSocketAddress.
38  */
39
40 /**
41  * GProxyAddress:
42  *
43  * A #GInetSocketAddress representing a connection via a proxy server
44  *
45  * Since: 2.26
46  **/
47
48 enum
49 {
50   PROP_0,
51   PROP_PROTOCOL,
52   PROP_DESTINATION_PROTOCOL,
53   PROP_DESTINATION_HOSTNAME,
54   PROP_DESTINATION_PORT,
55   PROP_USERNAME,
56   PROP_PASSWORD,
57   PROP_URI
58 };
59
60 struct _GProxyAddressPrivate
61 {
62   gchar          *uri;
63   gchar          *protocol;
64   gchar          *username;
65   gchar          *password;
66   gchar          *dest_protocol;
67   gchar          *dest_hostname;
68   guint16         dest_port;
69 };
70
71 G_DEFINE_TYPE_WITH_PRIVATE (GProxyAddress, g_proxy_address, G_TYPE_INET_SOCKET_ADDRESS)
72
73 static void
74 g_proxy_address_finalize (GObject *object)
75 {
76   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
77
78   g_free (proxy->priv->uri);
79   g_free (proxy->priv->protocol);
80   g_free (proxy->priv->username);
81   g_free (proxy->priv->password);
82   g_free (proxy->priv->dest_hostname);
83   g_free (proxy->priv->dest_protocol);
84
85   G_OBJECT_CLASS (g_proxy_address_parent_class)->finalize (object);
86 }
87
88 static void
89 g_proxy_address_set_property (GObject      *object,
90                               guint         prop_id,
91                               const GValue *value,
92                               GParamSpec   *pspec)
93 {
94   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
95
96   switch (prop_id)
97     {
98     case PROP_PROTOCOL:
99       g_free (proxy->priv->protocol);
100       proxy->priv->protocol = g_value_dup_string (value);
101       break;
102
103     case PROP_DESTINATION_PROTOCOL:
104       g_free (proxy->priv->dest_protocol);
105       proxy->priv->dest_protocol = g_value_dup_string (value);
106       break;
107
108     case PROP_DESTINATION_HOSTNAME:
109       g_free (proxy->priv->dest_hostname);
110       proxy->priv->dest_hostname = g_value_dup_string (value);
111       break;
112
113     case PROP_DESTINATION_PORT:
114       proxy->priv->dest_port = g_value_get_uint (value);
115       break;
116
117     case PROP_USERNAME:
118       g_free (proxy->priv->username);
119       proxy->priv->username = g_value_dup_string (value);
120       break;
121
122     case PROP_PASSWORD:
123       g_free (proxy->priv->password);
124       proxy->priv->password = g_value_dup_string (value);
125       break;
126
127     case PROP_URI:
128       g_free (proxy->priv->uri);
129       proxy->priv->uri = g_value_dup_string (value);
130       break;
131
132     default:
133       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
134     }
135 }
136
137 static void
138 g_proxy_address_get_property (GObject    *object,
139                               guint       prop_id,
140                               GValue     *value,
141                               GParamSpec *pspec)
142 {
143   GProxyAddress *proxy = G_PROXY_ADDRESS (object);
144
145   switch (prop_id)
146     {
147       case PROP_PROTOCOL:
148         g_value_set_string (value, proxy->priv->protocol);
149         break;
150
151       case PROP_DESTINATION_PROTOCOL:
152         g_value_set_string (value, proxy->priv->dest_protocol);
153         break;
154
155       case PROP_DESTINATION_HOSTNAME:
156         g_value_set_string (value, proxy->priv->dest_hostname);
157         break;
158
159       case PROP_DESTINATION_PORT:
160         g_value_set_uint (value, proxy->priv->dest_port);
161         break;
162
163       case PROP_USERNAME:
164         g_value_set_string (value, proxy->priv->username);
165         break;
166
167       case PROP_PASSWORD:
168         g_value_set_string (value, proxy->priv->password);
169         break;
170
171       case PROP_URI:
172         g_value_set_string (value, proxy->priv->uri);
173         break;
174
175       default:
176         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177     }
178 }
179
180 static void
181 g_proxy_address_class_init (GProxyAddressClass *klass)
182 {
183   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184
185   gobject_class->finalize = g_proxy_address_finalize;
186   gobject_class->set_property = g_proxy_address_set_property;
187   gobject_class->get_property = g_proxy_address_get_property;
188
189   g_object_class_install_property (gobject_class,
190                                    PROP_PROTOCOL,
191                                    g_param_spec_string ("protocol",
192                                                        P_("Protocol"),
193                                                        P_("The proxy protocol"),
194                                                        NULL,
195                                                        G_PARAM_READWRITE |
196                                                        G_PARAM_CONSTRUCT_ONLY |
197                                                        G_PARAM_STATIC_STRINGS));
198
199   g_object_class_install_property (gobject_class,
200                                    PROP_USERNAME,
201                                    g_param_spec_string ("username",
202                                                        P_("Username"),
203                                                        P_("The proxy username"),
204                                                        NULL,
205                                                        G_PARAM_READWRITE |
206                                                        G_PARAM_CONSTRUCT_ONLY |
207                                                        G_PARAM_STATIC_STRINGS));
208
209   g_object_class_install_property (gobject_class,
210                                    PROP_PASSWORD,
211                                    g_param_spec_string ("password",
212                                                        P_("Password"),
213                                                        P_("The proxy password"),
214                                                        NULL,
215                                                        G_PARAM_READWRITE |
216                                                        G_PARAM_CONSTRUCT_ONLY |
217                                                        G_PARAM_STATIC_STRINGS));
218
219   /**
220    * GProxyAddress:destination-protocol:
221    *
222    * The protocol being spoke to the destination host, or %NULL if
223    * the #GProxyAddress doesn't know.
224    *
225    * Since: 2.34
226    */
227   g_object_class_install_property (gobject_class,
228                                    PROP_DESTINATION_PROTOCOL,
229                                    g_param_spec_string ("destination-protocol",
230                                                        P_("Destionation Protocol"),
231                                                        P_("The proxy destination protocol"),
232                                                        NULL,
233                                                        G_PARAM_READWRITE |
234                                                        G_PARAM_CONSTRUCT_ONLY |
235                                                        G_PARAM_STATIC_STRINGS));
236
237   g_object_class_install_property (gobject_class,
238                                    PROP_DESTINATION_HOSTNAME,
239                                    g_param_spec_string ("destination-hostname",
240                                                        P_("Destination Hostname"),
241                                                        P_("The proxy destination hostname"),
242                                                        NULL,
243                                                        G_PARAM_READWRITE |
244                                                        G_PARAM_CONSTRUCT_ONLY |
245                                                        G_PARAM_STATIC_STRINGS));
246
247   g_object_class_install_property (gobject_class,
248                                    PROP_DESTINATION_PORT,
249                                    g_param_spec_uint ("destination-port",
250                                                       P_("Destination Port"),
251                                                       P_("The proxy destination port"),
252                                                       0, 65535, 0,
253                                                       G_PARAM_READWRITE |
254                                                       G_PARAM_CONSTRUCT_ONLY |
255                                                       G_PARAM_STATIC_STRINGS));
256
257   /**
258    * GProxyAddress:uri:
259    *
260    * The URI string that the proxy was constructed from (or %NULL
261    * if the creator didn't specify this).
262    *
263    * Since: 2.34
264    */
265   g_object_class_install_property (gobject_class,
266                                    PROP_URI,
267                                    g_param_spec_string ("uri",
268                                                         P_("URI"),
269                                                         P_("The proxy's URI"),
270                                                         NULL,
271                                                         G_PARAM_READWRITE |
272                                                         G_PARAM_CONSTRUCT_ONLY |
273                                                         G_PARAM_STATIC_STRINGS));
274 }
275
276 static void
277 g_proxy_address_init (GProxyAddress *proxy)
278 {
279   proxy->priv = g_proxy_address_get_instance_private (proxy);
280   proxy->priv->protocol = NULL;
281   proxy->priv->username = NULL;
282   proxy->priv->password = NULL;
283   proxy->priv->dest_hostname = NULL;
284   proxy->priv->dest_port = 0;
285 }
286
287 /**
288  * g_proxy_address_new:
289  * @inetaddr: The proxy server #GInetAddress.
290  * @port: The proxy server port.
291  * @protocol: The proxy protocol to support, in lower case (e.g. socks, http).
292  * @dest_hostname: The destination hostname the proxy should tunnel to.
293  * @dest_port: The destination port to tunnel to.
294  * @username: (allow-none): The username to authenticate to the proxy server
295  *     (or %NULL).
296  * @password: (allow-none): The password to authenticate to the proxy server
297  *     (or %NULL).
298  *
299  * Creates a new #GProxyAddress for @inetaddr with @protocol that should
300  * tunnel through @dest_hostname and @dest_port.
301  *
302  * (Note that this method doesn't set the #GProxyAddress:uri or
303  * #GProxyAddress:destination-protocol fields; use g_object_new()
304  * directly if you want to set those.)
305  *
306  * Returns: a new #GProxyAddress
307  *
308  * Since: 2.26
309  */
310 GSocketAddress *
311 g_proxy_address_new (GInetAddress  *inetaddr,
312                      guint16        port,
313                      const gchar   *protocol,
314                      const gchar   *dest_hostname,
315                      guint16        dest_port,
316                      const gchar   *username,
317                      const gchar   *password)
318 {
319   return g_object_new (G_TYPE_PROXY_ADDRESS,
320                        "address", inetaddr,
321                        "port", port,
322                        "protocol", protocol,
323                        "destination-hostname", dest_hostname,
324                        "destination-port", dest_port,
325                        "username", username,
326                        "password", password,
327                        NULL);
328 }
329
330
331 /**
332  * g_proxy_address_get_protocol:
333  * @proxy: a #GProxyAddress
334  *
335  * Gets @proxy's protocol. eg, "socks" or "http"
336  *
337  * Returns: the @proxy's protocol
338  *
339  * Since: 2.26
340  */
341 const gchar *
342 g_proxy_address_get_protocol (GProxyAddress *proxy)
343 {
344   return proxy->priv->protocol;
345 }
346
347 /**
348  * g_proxy_address_get_destination_protocol:
349  * @proxy: a #GProxyAddress
350  *
351  * Gets the protocol that is being spoken to the destination
352  * server; eg, "http" or "ftp".
353  *
354  * Returns: the @proxy's destination protocol
355  *
356  * Since: 2.34
357  */
358 const gchar *
359 g_proxy_address_get_destination_protocol (GProxyAddress *proxy)
360 {
361   return proxy->priv->dest_protocol;
362 }
363
364 /**
365  * g_proxy_address_get_destination_hostname:
366  * @proxy: a #GProxyAddress
367  *
368  * Gets @proxy's destination hostname; that is, the name of the host
369  * that will be connected to via the proxy, not the name of the proxy
370  * itself.
371  *
372  * Returns: the @proxy's destination hostname
373  *
374  * Since: 2.26
375  */
376 const gchar *
377 g_proxy_address_get_destination_hostname (GProxyAddress *proxy)
378 {
379   return proxy->priv->dest_hostname;
380 }
381
382 /**
383  * g_proxy_address_get_destination_port:
384  * @proxy: a #GProxyAddress
385  *
386  * Gets @proxy's destination port; that is, the port on the
387  * destination host that will be connected to via the proxy, not the
388  * port number of the proxy itself.
389  *
390  * Returns: the @proxy's destination port
391  *
392  * Since: 2.26
393  */
394 guint16
395 g_proxy_address_get_destination_port (GProxyAddress *proxy)
396 {
397   return proxy->priv->dest_port;
398 }
399
400 /**
401  * g_proxy_address_get_username:
402  * @proxy: a #GProxyAddress
403  *
404  * Gets @proxy's username.
405  *
406  * Returns: the @proxy's username
407  *
408  * Since: 2.26
409  */
410 const gchar *
411 g_proxy_address_get_username (GProxyAddress *proxy)
412 {
413   return proxy->priv->username;
414 }
415
416 /**
417  * g_proxy_address_get_password:
418  * @proxy: a #GProxyAddress
419  *
420  * Gets @proxy's password.
421  *
422  * Returns: the @proxy's password
423  *
424  * Since: 2.26
425  */
426 const gchar *
427 g_proxy_address_get_password (GProxyAddress *proxy)
428 {
429   return proxy->priv->password;
430 }
431
432
433 /**
434  * g_proxy_address_get_uri:
435  * @proxy: a #GProxyAddress
436  *
437  * Gets the proxy URI that @proxy was constructed from.
438  *
439  * Returns: the @proxy's URI, or %NULL if unknown
440  *
441  * Since: 2.34
442  */
443 const gchar *
444 g_proxy_address_get_uri (GProxyAddress *proxy)
445 {
446   return proxy->priv->uri;
447 }