summaryrefslogtreecommitdiff
path: root/telepathy-glib/svc-interface.c
blob: 3c3dcd2983ed85178825cb4a873ab550e9c8d6a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright © 2014 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>
#include <telepathy-glib/svc-interface.h>

/**
 * SECTION:svc-interface
 * @title: TpSvcInterface
 * @short_description: glue to export `TpSvc` interfaces on D-Bus
 *
 * #TpSvcInterfaceInfo describes a dbus-glib-style #GInterface in sufficient
 * detail to export it on a #GDBusConnection.
 *
 * Since: 0.99.10
 */

/**
 * TpSvcInterfaceInfo: (skip)
 * @ref_count: currently -1 since these structures can only be statically
 *  allocated; will be used for the reference count in the same way
 *  as #GDBusInterfaceInfo if necessary
 * @interface_info: the GDBus interface information
 * @vtable: the GDBus vtable, which must expect the object that
 *  implements the `TpSvc` interface (*not* the #GDBusInterfaceSkeleton!)
 *  as its `user_data`
 * @signals: (array zero-terminated=1): a %NULL-terminated array of
 *  GLib signal names in the same order as `interface_info->signals`
 *
 * The necessary glue between a dbus-glib-style `TpSvc` #GInterface
 * and telepathy-glib.
 *
 * These structs are intended to be programmatically-generated.
 *
 * Since: 0.99.10
 */

static GQuark
quark (void)
{
  static GQuark q = 0;

  if (G_UNLIKELY (q == 0))
    {
      q = g_quark_from_static_string (
          "tp_svc_interface_set_dbus_interface_info");
    }

  return q;
}

/**
 * tp_svc_interface_peek_dbus_interface_info: (skip)
 * @g_interface: The #GType of a service interface
 *
 * See whether the given interface has Telepathy code generation data
 * attached.
 *
 * Returns: (transfer null): a #TpSvcInterfaceInfo struct, or %NULL
 *
 * Since: 0.99.10
 */
const TpSvcInterfaceInfo *
tp_svc_interface_peek_dbus_interface_info (GType g_interface)
{
  return g_type_get_qdata (g_interface, quark ());
}

/**
 * tp_svc_interface_set_dbus_interface_info: (skip)
 * @g_interface: The #GType of a service interface
 * @info: struct encapsulating the #GDBusInterfaceInfo, the #GDBusVTable
 *  and the GLib signal names corresponding to D-Bus signals.
 *  The #GDBusVTable methods must expect the object
 *  implementing @g_interface as their user-data.
 *
 * Declare that @g_interface implements the given D-Bus interface, with the
 * given vtable. This may only be called once per GInterface, usually from
 * a section of its base_init function that only runs once.
 *
 * This is typically only used within generated code; there is normally no
 * reason to call it manually.
 *
 * Since: 0.99.10
 */
void
tp_svc_interface_set_dbus_interface_info (GType g_interface,
    const TpSvcInterfaceInfo *info)
{
  g_return_if_fail (G_TYPE_IS_INTERFACE (g_interface));
  g_return_if_fail (info->ref_count == -1);
  g_return_if_fail (tp_svc_interface_peek_dbus_interface_info (g_interface)
      == NULL);

  /* g_type_set_qdata wants a non-const pointer */
  g_type_set_qdata (g_interface, quark (), (gpointer) info);
}

/* this is the core library, we don't have debug infrastructure yet */
#define CRITICAL(format, ...) \
  g_log (G_LOG_DOMAIN "/properties", G_LOG_LEVEL_CRITICAL, "%s: " format, \
      G_STRFUNC, ##__VA_ARGS__)

static GQuark
_iface_prop_info_quark (void)
{
  static GQuark q = 0;

  if (G_UNLIKELY (q == 0))
    q = g_quark_from_static_string (
        "tp_svc_interface_get_dbus_properties_info");

  return q;
}

/**
 * tp_svc_interface_get_dbus_properties_info: (skip)
 * @g_interface: The #GType of a service interface
 *
 * Retrieves the D-Bus property metadata for the given interface, if any.
 * This function is typically not useful outside telepathy-glib itself, but may
 * be useful for domain-specific variations on the theme of SetProperty. If in
 * doubt, you probably don't need this function.
 *
 * Returns: D-Bus property metadata for @g_interface, or %NULL if it has
 *  none.
 * Since: 0.15.8
 */
TpDBusPropertiesMixinIfaceInfo *
tp_svc_interface_get_dbus_properties_info (GType g_interface)
{
  return g_type_get_qdata (g_interface, _iface_prop_info_quark ());
}

/**
 * tp_svc_interface_set_dbus_properties_info:
 * @g_interface: The #GType of a service interface
 * @info: an interface description
 *
 * Declare that @g_interface implements the given D-Bus interface, with the
 * given properties. This may only be called once per GInterface, usually from
 * a section of its base_init function that only runs once.
 *
 * This is typically only used within generated code; there is normally no
 * reason to call it manually.
 *
 * Since: 0.7.3
 */
void
tp_svc_interface_set_dbus_properties_info (GType g_interface,
    TpDBusPropertiesMixinIfaceInfo *info)
{
  GQuark q = _iface_prop_info_quark ();
  TpDBusPropertiesMixinPropInfo *prop;

  g_return_if_fail (G_TYPE_IS_INTERFACE (g_interface));
  g_return_if_fail (g_type_get_qdata (g_interface, q) == NULL);
  g_return_if_fail (info->dbus_interface != 0);
  g_return_if_fail (info->props != NULL);

  for (prop = info->props; prop->name != 0; prop++)
    {
      g_return_if_fail (prop->flags != 0);
      g_return_if_fail (
        (prop->flags & ~( TP_DBUS_PROPERTIES_MIXIN_FLAG_READ
                        | TP_DBUS_PROPERTIES_MIXIN_FLAG_WRITE
                        | TP_DBUS_PROPERTIES_MIXIN_FLAG_EMITS_CHANGED
                        | TP_DBUS_PROPERTIES_MIXIN_FLAG_EMITS_INVALIDATED
                        )) == 0);

      /* Check that at most one change-related flag is set. */
      if ((prop->flags & TP_DBUS_PROPERTIES_MIXIN_FLAG_EMITS_CHANGED) &&
          (prop->flags & TP_DBUS_PROPERTIES_MIXIN_FLAG_EMITS_INVALIDATED))
        {
          CRITICAL ("at most one of EMITS_CHANGED and EMITS_INVALIDATED may be "
              "specified for a property, but %s.%s has both",
              g_quark_to_string (info->dbus_interface),
              g_quark_to_string (prop->name));
          g_return_if_reached ();
        }

      g_return_if_fail (prop->dbus_signature != NULL);
      g_return_if_fail (prop->dbus_signature[0] != '\0');
      g_return_if_fail (prop->type != 0);
    }

  g_type_set_qdata (g_interface, q, info);
}

/**
 * tp_dbus_g_method_return_not_implemented: (skip)
 * @context: The D-Bus method invocation context
 *
 * Return the Telepathy error NotImplemented from the method invocation
 * given by @context.
 */
void
tp_dbus_g_method_return_not_implemented (GDBusMethodInvocation *context)
{
  g_dbus_method_invocation_return_dbus_error (context,
      TP_ERROR_STR_NOT_IMPLEMENTED, "Not implemented");
}