GSocketControlMessage: clean up confusing code
[platform/upstream/glib.git] / gio / gproxyconnection.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 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@colllabora.co.uk>
21  */
22
23 #include "config.h"
24
25 #include "gproxyconnection.h"
26
27 #include "gtcpconnection.h"
28 #include "glibintl.h"
29
30 #define g_proxy_connection_get_type _g_proxy_connection_get_type
31 G_DEFINE_TYPE (GProxyConnection,
32                g_proxy_connection, G_TYPE_TCP_CONNECTION);
33
34 enum
35 {
36   PROP_NONE,
37   PROP_IO_STREAM
38 };
39
40 struct _GProxyConnectionPrivate
41 {
42   GIOStream *io_stream;
43 };
44
45 static GInputStream *
46 g_proxy_connection_get_input_stream (GIOStream *io_stream)
47 {
48   GProxyConnection *connection;
49   connection = G_PROXY_PROXY_CONNECTION (io_stream);
50   return g_io_stream_get_input_stream (connection->priv->io_stream);
51 }
52
53 static GOutputStream *
54 g_proxy_connection_get_output_stream (GIOStream *io_stream)
55 {
56   GProxyConnection *connection;
57   connection = G_PROXY_PROXY_CONNECTION (io_stream);
58   return g_io_stream_get_output_stream (connection->priv->io_stream);
59 }
60
61 static void
62 g_proxy_connection_get_property (GObject    *object,
63                                  guint       prop_id,
64                                  GValue     *value,
65                                  GParamSpec *pspec)
66 {
67   GProxyConnection *connection = G_PROXY_PROXY_CONNECTION (object);
68
69   switch (prop_id)
70     {
71      case PROP_IO_STREAM:
72       g_value_set_object (value, connection->priv->io_stream);
73       break;
74
75      default:
76       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77     }
78 }
79
80 static void
81 g_proxy_connection_set_property (GObject      *object,
82                                         guint         prop_id,
83                                         const GValue *value,
84                                         GParamSpec   *pspec)
85 {
86   GProxyConnection *connection = G_PROXY_PROXY_CONNECTION (object);
87
88   switch (prop_id)
89     {
90      case PROP_IO_STREAM:
91       connection->priv->io_stream = G_IO_STREAM (g_value_dup_object (value));
92       break;
93
94      default:
95       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
96     }
97 }
98
99 static void
100 g_proxy_connection_finalize (GObject *object)
101 {
102   GProxyConnection *connection = G_PROXY_PROXY_CONNECTION (object);
103
104   g_object_unref (connection->priv->io_stream);
105
106   G_OBJECT_CLASS (g_proxy_connection_parent_class)->finalize (object);
107 }
108
109 static void
110 g_proxy_connection_class_init (GProxyConnectionClass *klass)
111 {
112   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113   GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
114
115   g_type_class_add_private (klass, sizeof (GProxyConnectionPrivate));
116
117   gobject_class->set_property = g_proxy_connection_set_property;
118   gobject_class->get_property = g_proxy_connection_get_property;
119   gobject_class->finalize = g_proxy_connection_finalize;
120
121   stream_class->get_input_stream = g_proxy_connection_get_input_stream;
122   stream_class->get_output_stream = g_proxy_connection_get_output_stream;
123
124   g_object_class_install_property (gobject_class,
125                                    PROP_IO_STREAM,
126                                    g_param_spec_object ("io-stream",
127                                                         P_("IO Stream"),
128                                                         P_("The altered streams"),
129                                                         G_TYPE_SOCKET_CONNECTION,
130                                                         G_PARAM_CONSTRUCT_ONLY |
131                                                         G_PARAM_READWRITE |
132                                                         G_PARAM_STATIC_STRINGS));
133 }
134
135 static void
136 g_proxy_connection_init (GProxyConnection *connection)
137 {
138   connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
139                                                   G_TYPE_PROXY_CONNECTION,
140                                                   GProxyConnectionPrivate);
141 }
142
143 GSocketConnection *
144 _g_proxy_connection_new (GTcpConnection *connection,
145                          GIOStream      *io_stream)
146 {
147   GSocket *socket;
148
149   socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (connection));
150
151   return G_SOCKET_CONNECTION (g_object_new (G_TYPE_PROXY_CONNECTION,
152                                             "io-stream", io_stream,
153                                             "socket", socket,
154                                             NULL));
155 }