GDBusInterface: add dup_object() method
[platform/upstream/glib.git] / gio / gdbusinterface.c
1 /* GDBus - GLib D-Bus Library
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
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: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include "gdbusobject.h"
26 #include "gdbusinterface.h"
27
28 #include "glibintl.h"
29
30 /**
31  * SECTION:gdbusinterface
32  * @short_description: Base type for D-Bus interfaces
33  * @include: gio/gio.h
34  *
35  * The #GDBusInterface type is the base type for D-Bus interfaces both
36  * on the service side (see #GDBusInterfaceSkeleton) and client side
37  * (see #GDBusProxy).
38  */
39
40 typedef GDBusInterfaceIface GDBusInterfaceInterface;
41 G_DEFINE_INTERFACE (GDBusInterface, g_dbus_interface, G_TYPE_OBJECT)
42
43 static void
44 g_dbus_interface_default_init (GDBusInterfaceIface *iface)
45 {
46 }
47
48 /* ---------------------------------------------------------------------------------------------------- */
49
50 /**
51  * g_dbus_interface_get_info:
52  * @interface_: An exported D-Bus interface.
53  *
54  * Gets D-Bus introspection information for the D-Bus interface
55  * implemented by @interface_.
56  *
57  * Returns: (transfer none): A #GDBusInterfaceInfo. Do not free.
58  *
59  * Since: 2.30
60  */
61 GDBusInterfaceInfo *
62 g_dbus_interface_get_info (GDBusInterface *interface_)
63 {
64   g_return_val_if_fail (G_IS_DBUS_INTERFACE (interface_), NULL);
65   return G_DBUS_INTERFACE_GET_IFACE (interface_)->get_info (interface_);
66 }
67
68 /**
69  * g_dbus_interface_get_object: (skip)
70  * @interface_: An exported D-Bus interface.
71  *
72  * Gets the #GDBusObject that @interface_ belongs to, if any.
73  *
74  * <warning>It is not safe to use the returned object if @interface_
75  * or the returned object is being used from other threads. See
76  * g_dbus_interface_dup_object() for a thread-safe
77  * alternative.</warning>
78  *
79  * Returns: (transfer none): A #GDBusObject or %NULL. The returned
80  * reference belongs to @interface_ and should not be freed.
81  *
82  * Since: 2.30
83  */
84 GDBusObject *
85 g_dbus_interface_get_object (GDBusInterface *interface_)
86 {
87   g_return_val_if_fail (G_IS_DBUS_INTERFACE (interface_), NULL);
88   return G_DBUS_INTERFACE_GET_IFACE (interface_)->get_object (interface_);
89 }
90
91 /**
92  * g_dbus_interface_dup_object:
93  * @interface_: An exported D-Bus interface.
94  *
95  * Gets the #GDBusObject that @interface_ belongs to, if any.
96  *
97  * Returns: (transfer full): A #GDBusObject or %NULL. The returned
98  * reference should be freed with g_object_unref().
99  *
100  * Since: 2.32
101  *
102  * Rename to: g_dbus_interface_get_object
103  */
104 GDBusObject *
105 g_dbus_interface_dup_object (GDBusInterface *interface_)
106 {
107   GDBusObject *ret;
108   g_return_val_if_fail (G_IS_DBUS_INTERFACE (interface_), NULL);
109   if (G_LIKELY (G_DBUS_INTERFACE_GET_IFACE (interface_)->dup_object != NULL))
110     {
111       ret = G_DBUS_INTERFACE_GET_IFACE (interface_)->dup_object (interface_);
112     }
113   else
114     {
115       g_warning ("No dup_object() vfunc on type %s - using get_object() in a way that is not thread-safe.",
116                  g_type_name_from_instance ((GTypeInstance *) interface_));
117       ret = G_DBUS_INTERFACE_GET_IFACE (interface_)->get_object (interface_);
118       if (ret != NULL)
119         g_object_ref (ret);
120     }
121   return ret;
122 }
123
124 /**
125  * g_dbus_interface_set_object:
126  * @interface_: An exported D-Bus interface.
127  * @object: A #GDBusObject or %NULL.
128  *
129  * Sets the #GDBusObject for @interface_ to @object.
130  *
131  * Note that @interface_ will hold a weak reference to @object.
132  *
133  * Since: 2.30
134  */
135 void
136 g_dbus_interface_set_object (GDBusInterface    *interface_,
137                              GDBusObject       *object)
138 {
139   g_return_if_fail (G_IS_DBUS_INTERFACE (interface_));
140   g_return_if_fail (object == NULL || G_IS_DBUS_OBJECT (object));
141   G_DBUS_INTERFACE_GET_IFACE (interface_)->set_object (interface_, object);
142 }