[kdbus] Import gio/gkdbus* files from previous patchset.
[platform/upstream/glib.git] / gio / gkdbusconnection.c
1 /*  GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2013 Samsung Electronics
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  * Author: Michal Eljasiewicz   <m.eljasiewic@samsung.com>
21  * Author: Lukasz Skalski       <l.skalski@samsung.com>
22  */
23
24 #include "config.h"
25 #include "gkdbusconnection.h"
26 #include "gunixconnection.h"
27
28 /**
29  * SECTION:gkdbusconnection
30  * @short_description: A kdbus connection
31  * @include: gio/gio.h
32  * @see_also: #GIOStream, #GKdbusClient
33  *
34  * #GKdbusConnection is a #GIOStream for a connected kdbus bus.
35  */
36
37 #define g_kdbus_connection_get_type _g_kdbus_connection_get_type
38 G_DEFINE_TYPE (GKdbusConnection, g_kdbus_connection, G_TYPE_IO_STREAM);
39
40 struct _GKdbusConnectionPrivate
41 {
42   GKdbus    *kdbus;
43   gboolean   in_dispose;
44 };
45
46
47 /**
48  * g_kdbus_connection_new:
49  *
50  */
51 GKdbusConnection *
52 _g_kdbus_connection_new (void)
53 {
54   return g_object_new(G_TYPE_KDBUS_CONNECTION,NULL);
55 }
56
57
58 /**
59  * g_kdbus_connection_connect:
60  *
61  */
62 gboolean
63 _g_kdbus_connection_connect (GKdbusConnection  *connection,
64                              const gchar       *address,
65                              GCancellable      *cancellable,
66                              GError           **error)
67 {
68   g_return_val_if_fail (G_IS_KDBUS_CONNECTION (connection), FALSE);
69
70   return _g_kdbus_open (connection->priv->kdbus,address,error);
71 }
72
73
74 /**
75  * g_kdbus_connection_constructed:
76  *
77  */
78 static void
79 g_kdbus_connection_constructed (GObject  *object)
80 {
81   GKdbusConnection *connection = G_KDBUS_CONNECTION (object);
82
83   g_assert (connection->priv->kdbus != NULL);
84 }
85
86
87 /**
88  * g_kdbus_connection_finalize:
89  *
90  */
91 static void
92 g_kdbus_connection_finalize (GObject  *object)
93 {
94   GKdbusConnection *connection = G_KDBUS_CONNECTION (object);
95
96   g_object_unref (connection->priv->kdbus);
97
98   G_OBJECT_CLASS (g_kdbus_connection_parent_class)->finalize (object);
99 }
100
101
102 /**
103  * g_kdbus_connection_dispose:
104  *
105  */
106 static void
107 g_kdbus_connection_dispose (GObject  *object)
108 {
109   GKdbusConnection *connection = G_KDBUS_CONNECTION (object);
110
111   connection->priv->in_dispose = TRUE;
112
113   G_OBJECT_CLASS (g_kdbus_connection_parent_class)
114     ->dispose (object);
115
116   connection->priv->in_dispose = FALSE;
117 }
118
119
120 /**
121  * g_kdbus_connection_close:
122  *
123  */
124 static gboolean
125 g_kdbus_connection_close (GIOStream     *stream,
126                           GCancellable  *cancellable,
127                           GError       **error)
128 {
129   GKdbusConnection *connection = G_KDBUS_CONNECTION (stream);
130
131   if (connection->priv->in_dispose)
132     return TRUE;
133
134   return _g_kdbus_close (connection->priv->kdbus, error);
135 }
136
137
138 /**
139  * g_kdbus_connection_close_async:
140  *
141  */
142 static void
143 g_kdbus_connection_close_async (GIOStream            *stream,
144                                 int                   io_priority,
145                                 GCancellable         *cancellable,
146                                 GAsyncReadyCallback   callback,
147                                 gpointer              user_data)
148 {
149   GTask *task;
150   GIOStreamClass *class;
151   GError *error;
152
153   class = G_IO_STREAM_GET_CLASS (stream);
154
155   task = g_task_new (stream, cancellable, callback, user_data);
156
157   error = NULL;
158   if (class->close_fn &&
159       !class->close_fn (stream, cancellable, &error))
160     g_task_return_error (task, error);
161   else
162     g_task_return_boolean (task, TRUE);
163
164   g_object_unref (task);
165 }
166
167
168 /**
169  * g_kdbus_connection_close_finish:
170  *
171  */
172 static gboolean
173 g_kdbus_connection_close_finish (GIOStream     *stream,
174                                  GAsyncResult  *result,
175                                  GError       **error)
176 {
177   return g_task_propagate_boolean (G_TASK (result), error);
178 }
179
180
181 /**
182  * g_kdbus_connection_class_init:
183  *
184  */
185 static void
186 g_kdbus_connection_class_init (GKdbusConnectionClass  *klass)
187 {
188   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189   GIOStreamClass *stream_class = G_IO_STREAM_CLASS (klass);
190
191   g_type_class_add_private (klass, sizeof (GKdbusConnectionPrivate));
192
193   gobject_class->constructed = g_kdbus_connection_constructed;
194   gobject_class->finalize = g_kdbus_connection_finalize;
195   gobject_class->dispose = g_kdbus_connection_dispose;
196
197   stream_class->close_fn = g_kdbus_connection_close;
198   stream_class->close_async = g_kdbus_connection_close_async;
199   stream_class->close_finish = g_kdbus_connection_close_finish;
200 }
201
202
203 /**
204  * g_kdbus_connection_init:
205  *
206  */
207 static void
208 g_kdbus_connection_init (GKdbusConnection  *connection)
209 {
210   connection->priv = G_TYPE_INSTANCE_GET_PRIVATE (connection,
211                                                   G_TYPE_KDBUS_CONNECTION,
212                                                   GKdbusConnectionPrivate);
213   connection->priv->kdbus = g_object_new(G_TYPE_KDBUS,NULL);
214 }
215
216
217 /**
218  * g_kdbus_connection_get_kdbus:
219  *
220  */
221 GKdbus *
222 _g_kdbus_connection_get_kdbus (GKdbusConnection  *connection)
223 {
224   g_return_val_if_fail (G_IS_KDBUS_CONNECTION (connection), NULL);
225
226   return connection->priv->kdbus;
227 }