767097d8a04f028e691b6274923b5e9bc3e84127
[platform/upstream/glib.git] / gio / gdbusmethodinvocation.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 <stdlib.h>
26
27 #include "gdbusutils.h"
28 #include "gdbusconnection.h"
29 #include "gdbusmessage.h"
30 #include "gdbusmethodinvocation.h"
31 #include "gdbusintrospection.h"
32 #include "gdbuserror.h"
33 #include "gdbusprivate.h"
34
35 #include "glibintl.h"
36 #include "gioalias.h"
37
38 /**
39  * SECTION:gdbusmethodinvocation
40  * @short_description: Object for handling remote calls
41  * @include: gio/gio.h
42  *
43  * Instances of the #GDBusMethodInvocation class are used when
44  * handling D-Bus method calls. It provides a way to asynchronously
45  * return results and errors.
46  *
47  * The normal way to obtain a #GDBusMethodInvocation object is to receive
48  * it as an argument to the handle_method_call() function in a
49  * #GDBusInterfaceVTable that was passed to g_dbus_connection_register_object().
50  */
51
52 struct _GDBusMethodInvocationPrivate
53 {
54   /* construct-only properties */
55   gchar           *sender;
56   gchar           *object_path;
57   gchar           *interface_name;
58   gchar           *method_name;
59   const GDBusMethodInfo *method_info;
60   GDBusConnection *connection;
61   GDBusMessage    *message;
62   GVariant        *parameters;
63   gpointer         user_data;
64 };
65
66 enum
67 {
68   PROP_0,
69   PROP_SENDER,
70   PROP_OBJECT_PATH,
71   PROP_INTERFACE_NAME,
72   PROP_METHOD_NAME,
73   PROP_METHOD_INFO,
74   PROP_CONNECTION,
75   PROP_PARAMETERS,
76   PROP_MESSAGE,
77   PROP_USER_DATA
78 };
79
80 G_DEFINE_TYPE (GDBusMethodInvocation, g_dbus_method_invocation, G_TYPE_OBJECT);
81
82 static void
83 g_dbus_method_invocation_finalize (GObject *object)
84 {
85   GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
86
87   g_free (invocation->priv->sender);
88   g_free (invocation->priv->object_path);
89   g_free (invocation->priv->interface_name);
90   g_free (invocation->priv->method_name);
91   g_object_unref (invocation->priv->connection);
92   g_object_unref (invocation->priv->message);
93   g_variant_unref (invocation->priv->parameters);
94
95   G_OBJECT_CLASS (g_dbus_method_invocation_parent_class)->finalize (object);
96 }
97
98 static void
99 g_dbus_method_invocation_get_property (GObject    *object,
100                                        guint       prop_id,
101                                        GValue     *value,
102                                        GParamSpec *pspec)
103 {
104   GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
105
106   switch (prop_id)
107     {
108     case PROP_SENDER:
109       g_value_set_string (value, g_dbus_method_invocation_get_sender (invocation));
110       break;
111
112     case PROP_OBJECT_PATH:
113       g_value_set_string (value, g_dbus_method_invocation_get_object_path (invocation));
114       break;
115
116     case PROP_INTERFACE_NAME:
117       g_value_set_string (value, g_dbus_method_invocation_get_interface_name (invocation));
118       break;
119
120     case PROP_METHOD_NAME:
121       g_value_set_string (value, g_dbus_method_invocation_get_method_name (invocation));
122       break;
123
124     case PROP_METHOD_INFO:
125       g_value_set_boxed (value, g_dbus_method_invocation_get_method_info (invocation));
126       break;
127
128     case PROP_CONNECTION:
129       g_value_set_object (value, g_dbus_method_invocation_get_connection (invocation));
130       break;
131
132     case PROP_PARAMETERS:
133       g_value_set_boxed (value, g_dbus_method_invocation_get_parameters (invocation));
134       break;
135
136     case PROP_MESSAGE:
137       g_value_set_object (value, g_dbus_method_invocation_get_message (invocation));
138       break;
139
140     case PROP_USER_DATA:
141       g_value_set_pointer (value, g_dbus_method_invocation_get_user_data (invocation));
142       break;
143
144     default:
145       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146       break;
147     }
148 }
149
150 static void
151 g_dbus_method_invocation_set_property (GObject      *object,
152                                        guint         prop_id,
153                                        const GValue *value,
154                                        GParamSpec   *pspec)
155 {
156   GDBusMethodInvocation *invocation = G_DBUS_METHOD_INVOCATION (object);
157
158   switch (prop_id)
159     {
160     case PROP_SENDER:
161       invocation->priv->sender = g_value_dup_string (value);
162       break;
163
164     case PROP_OBJECT_PATH:
165       invocation->priv->object_path = g_value_dup_string (value);
166       break;
167
168     case PROP_INTERFACE_NAME:
169       invocation->priv->interface_name = g_value_dup_string (value);
170       break;
171
172     case PROP_METHOD_NAME:
173       invocation->priv->method_name = g_value_dup_string (value);
174       break;
175
176     case PROP_METHOD_INFO:
177       invocation->priv->method_info = g_value_dup_boxed (value);
178       break;
179
180     case PROP_CONNECTION:
181       invocation->priv->connection = g_value_dup_object (value);
182       break;
183
184     case PROP_PARAMETERS:
185       invocation->priv->parameters = g_value_dup_boxed (value);
186       break;
187
188     case PROP_MESSAGE:
189       invocation->priv->message = g_value_dup_object (value);
190       break;
191
192     case PROP_USER_DATA:
193       invocation->priv->user_data = g_value_get_pointer (value);
194       break;
195
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198       break;
199     }
200 }
201
202
203 static void
204 g_dbus_method_invocation_class_init (GDBusMethodInvocationClass *klass)
205 {
206   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
207
208   gobject_class->finalize     = g_dbus_method_invocation_finalize;
209   gobject_class->set_property = g_dbus_method_invocation_set_property;
210   gobject_class->get_property = g_dbus_method_invocation_get_property;
211
212   /**
213    * GDBusMethodInvocation:sender:
214    *
215    * The bus name that invoked the method or %NULL if the connection is not a bus connection.
216    *
217    * Since: 2.26
218    */
219   g_object_class_install_property (gobject_class,
220                                    PROP_SENDER,
221                                    g_param_spec_string ("sender",
222                                                         P_("Sender"),
223                                                         P_("The bus name that invoked the method."),
224                                                         NULL,
225                                                         G_PARAM_READABLE |
226                                                         G_PARAM_WRITABLE |
227                                                         G_PARAM_CONSTRUCT_ONLY |
228                                                         G_PARAM_STATIC_NAME |
229                                                         G_PARAM_STATIC_BLURB |
230                                                         G_PARAM_STATIC_NICK));
231
232   /**
233    * GDBusMethodInvocation:object-path:
234    *
235    * The object path the method was invoked on.
236    *
237    * Since: 2.26
238    */
239   g_object_class_install_property (gobject_class,
240                                    PROP_OBJECT_PATH,
241                                    g_param_spec_string ("object-path",
242                                                         P_("Object Path"),
243                                                         P_("The object path the method was invoked on."),
244                                                         NULL,
245                                                         G_PARAM_READABLE |
246                                                         G_PARAM_WRITABLE |
247                                                         G_PARAM_CONSTRUCT_ONLY |
248                                                         G_PARAM_STATIC_NAME |
249                                                         G_PARAM_STATIC_BLURB |
250                                                         G_PARAM_STATIC_NICK));
251
252   /**
253    * GDBusMethodInvocation:interface-name:
254    *
255    * The name of the D-Bus interface the method was invoked on.
256    *
257    * Since: 2.26
258    */
259   g_object_class_install_property (gobject_class,
260                                    PROP_INTERFACE_NAME,
261                                    g_param_spec_string ("interface-name",
262                                                         P_("Interface Name"),
263                                                         P_("The name of the D-Bus interface the method was invoked on."),
264                                                         NULL,
265                                                         G_PARAM_READABLE |
266                                                         G_PARAM_WRITABLE |
267                                                         G_PARAM_CONSTRUCT_ONLY |
268                                                         G_PARAM_STATIC_NAME |
269                                                         G_PARAM_STATIC_BLURB |
270                                                         G_PARAM_STATIC_NICK));
271
272   /**
273    * GDBusMethodInvocation:method-name:
274    *
275    * The name of the method that was invoked.
276    *
277    * Since: 2.26
278    */
279   g_object_class_install_property (gobject_class,
280                                    PROP_METHOD_NAME,
281                                    g_param_spec_string ("method-name",
282                                                         P_("Method Name"),
283                                                         P_("The name of the method that was invoked."),
284                                                         NULL,
285                                                         G_PARAM_READABLE |
286                                                         G_PARAM_WRITABLE |
287                                                         G_PARAM_CONSTRUCT_ONLY |
288                                                         G_PARAM_STATIC_NAME |
289                                                         G_PARAM_STATIC_BLURB |
290                                                         G_PARAM_STATIC_NICK));
291
292   /**
293    * GDBusMethodInvocation:method-info:
294    *
295    * Information about the method that was invoked, if any.
296    *
297    * Since: 2.26
298    */
299   g_object_class_install_property (gobject_class,
300                                    PROP_METHOD_INFO,
301                                    g_param_spec_boxed ("method-info",
302                                                        P_("Method Info"),
303                                                        P_("Information about the method that was invoked, if any."),
304                                                        G_TYPE_DBUS_METHOD_INFO,
305                                                        G_PARAM_READABLE |
306                                                        G_PARAM_WRITABLE |
307                                                        G_PARAM_CONSTRUCT_ONLY |
308                                                        G_PARAM_STATIC_NAME |
309                                                        G_PARAM_STATIC_BLURB |
310                                                        G_PARAM_STATIC_NICK));
311
312   /**
313    * GDBusMethodInvocation:connection:
314    *
315    * The #GDBusConnection the method was invoked on.
316    *
317    * Since: 2.26
318    */
319   g_object_class_install_property (gobject_class,
320                                    PROP_CONNECTION,
321                                    g_param_spec_object ("connection",
322                                                         P_("Connection"),
323                                                         P_("The #GDBusConnection the method was invoked on."),
324                                                         G_TYPE_DBUS_CONNECTION,
325                                                         G_PARAM_READABLE |
326                                                         G_PARAM_WRITABLE |
327                                                         G_PARAM_CONSTRUCT_ONLY |
328                                                         G_PARAM_STATIC_NAME |
329                                                         G_PARAM_STATIC_BLURB |
330                                                         G_PARAM_STATIC_NICK));
331
332   /**
333    * GDBusMethodInvocation:message:
334    *
335    * The D-Bus message.
336    *
337    * Since: 2.26
338    */
339   g_object_class_install_property (gobject_class,
340                                    PROP_MESSAGE,
341                                    g_param_spec_object ("message",
342                                                         P_("Message"),
343                                                         P_("The D-Bus Message."),
344                                                         G_TYPE_DBUS_MESSAGE,
345                                                         G_PARAM_READABLE |
346                                                         G_PARAM_WRITABLE |
347                                                         G_PARAM_CONSTRUCT_ONLY |
348                                                         G_PARAM_STATIC_NAME |
349                                                         G_PARAM_STATIC_BLURB |
350                                                         G_PARAM_STATIC_NICK));
351
352   /**
353    * GDBusMethodInvocation:parameters:
354    *
355    * The parameters as a #GVariant tuple.
356    *
357    * Since: 2.26
358    */
359   g_object_class_install_property (gobject_class,
360                                    PROP_PARAMETERS,
361                                    g_param_spec_boxed ("parameters",
362                                                        P_("Parameters"),
363                                                        P_("The parameters as a #GVariant tuple."),
364                                                        G_TYPE_VARIANT,
365                                                        G_PARAM_READABLE |
366                                                        G_PARAM_WRITABLE |
367                                                        G_PARAM_CONSTRUCT_ONLY |
368                                                        G_PARAM_STATIC_NAME |
369                                                        G_PARAM_STATIC_BLURB |
370                                                        G_PARAM_STATIC_NICK));
371
372   /**
373    * GDBusMethodInvocation:user-data:
374    *
375    * The @user_data #gpointer passed to g_dbus_connection_register_object().
376    *
377    * Since: 2.26
378    */
379   g_object_class_install_property (gobject_class,
380                                    PROP_USER_DATA,
381                                    g_param_spec_pointer ("user-data",
382                                                          P_("User Data"),
383                                                          P_("The gpointer passed to g_dbus_connection_register_object()."),
384                                                          G_PARAM_READABLE |
385                                                          G_PARAM_WRITABLE |
386                                                          G_PARAM_CONSTRUCT_ONLY |
387                                                          G_PARAM_STATIC_NAME |
388                                                          G_PARAM_STATIC_BLURB |
389                                                          G_PARAM_STATIC_NICK));
390
391   g_type_class_add_private (klass, sizeof (GDBusMethodInvocationPrivate));
392 }
393
394 static void
395 g_dbus_method_invocation_init (GDBusMethodInvocation *invocation)
396 {
397   invocation->priv = G_TYPE_INSTANCE_GET_PRIVATE (invocation,
398                                                   G_TYPE_DBUS_METHOD_INVOCATION,
399                                                   GDBusMethodInvocationPrivate);
400 }
401
402 /**
403  * g_dbus_method_invocation_get_sender:
404  * @invocation: A #GDBusMethodInvocation.
405  *
406  * Gets the bus name that invoked the method.
407  *
408  * Returns: A string. Do not free, it is owned by @invocation.
409  *
410  * Since: 2.26
411  */
412 const gchar *
413 g_dbus_method_invocation_get_sender (GDBusMethodInvocation *invocation)
414 {
415   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
416   return invocation->priv->sender;
417 }
418
419 /**
420  * g_dbus_method_invocation_get_object_path:
421  * @invocation: A #GDBusMethodInvocation.
422  *
423  * Gets the object path the method was invoked on.
424  *
425  * Returns: A string. Do not free, it is owned by @invocation.
426  *
427  * Since: 2.26
428  */
429 const gchar *
430 g_dbus_method_invocation_get_object_path (GDBusMethodInvocation *invocation)
431 {
432   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
433   return invocation->priv->object_path;
434 }
435
436 /**
437  * g_dbus_method_invocation_get_interface_name:
438  * @invocation: A #GDBusMethodInvocation.
439  *
440  * Gets the name of the D-Bus interface the method was invoked on.
441  *
442  * Returns: A string. Do not free, it is owned by @invocation.
443  *
444  * Since: 2.26
445  */
446 const gchar *
447 g_dbus_method_invocation_get_interface_name (GDBusMethodInvocation *invocation)
448 {
449   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
450   return invocation->priv->interface_name;
451 }
452
453 /**
454  * g_dbus_method_invocation_get_method_info:
455  * @invocation: A #GDBusMethodInvocation.
456  *
457  * Gets information about the method call, if any.
458  *
459  * Returns: A #GDBusMethodInfo or %NULL. Do not free, it is owned by @invocation.
460  *
461  * Since: 2.26
462  */
463 const GDBusMethodInfo *
464 g_dbus_method_invocation_get_method_info (GDBusMethodInvocation *invocation)
465 {
466   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
467   return invocation->priv->method_info;
468 }
469
470 /**
471  * g_dbus_method_invocation_get_method_name:
472  * @invocation: A #GDBusMethodInvocation.
473  *
474  * Gets the name of the method that was invoked.
475  *
476  * Returns: A string. Do not free, it is owned by @invocation.
477  *
478  * Since: 2.26
479  */
480 const gchar *
481 g_dbus_method_invocation_get_method_name (GDBusMethodInvocation *invocation)
482 {
483   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
484   return invocation->priv->method_name;
485 }
486
487 /**
488  * g_dbus_method_invocation_get_connection:
489  * @invocation: A #GDBusMethodInvocation.
490  *
491  * Gets the #GDBusConnection the method was invoked on.
492  *
493  * Returns: A #GDBusConnection. Do not free, it is owned by @invocation.
494  *
495  * Since: 2.26
496  */
497 GDBusConnection *
498 g_dbus_method_invocation_get_connection (GDBusMethodInvocation *invocation)
499 {
500   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
501   return invocation->priv->connection;
502 }
503
504 /**
505  * g_dbus_method_invocation_get_message:
506  * @invocation: A #GDBusMethodInvocation.
507  *
508  * Gets the #GDBusMessage for the method invocation. This is useful if
509  * you need to use low-level protocol features, such as UNIX file
510  * descriptor passing, that cannot be properly expressed in the
511  * #GVariant API.
512  *
513  * See <xref linkend="gdbus-server"/> and <xref
514  * linkend="gdbus-unix-fd-client"/> for an example of how to use this
515  * low-level API to send and receive UNIX file descriptors.
516  *
517  * Returns: A #GDBusMessage. Do not free, it is owned by @invocation.
518  *
519  * Since: 2.26
520  */
521 GDBusMessage *
522 g_dbus_method_invocation_get_message (GDBusMethodInvocation *invocation)
523 {
524   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
525   return invocation->priv->message;
526 }
527
528 /**
529  * g_dbus_method_invocation_get_parameters:
530  * @invocation: A #GDBusMethodInvocation.
531  *
532  * Gets the parameters of the method invocation.
533  *
534  * Returns: A #GVariant. Do not free, it is owned by @invocation.
535  *
536  * Since: 2.26
537  */
538 GVariant *
539 g_dbus_method_invocation_get_parameters (GDBusMethodInvocation *invocation)
540 {
541   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
542   return invocation->priv->parameters;
543 }
544
545 /**
546  * g_dbus_method_invocation_get_user_data:
547  * @invocation: A #GDBusMethodInvocation.
548  *
549  * Gets the @user_data #gpointer passed to g_dbus_connection_register_object().
550  *
551  * Returns: A #gpointer.
552  *
553  * Since: 2.26
554  */
555 gpointer
556 g_dbus_method_invocation_get_user_data (GDBusMethodInvocation *invocation)
557 {
558   g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), NULL);
559   return invocation->priv->user_data;
560 }
561
562 /**
563  * g_dbus_method_invocation_new:
564  * @sender: The bus name that invoked the method or %NULL if @connection is not a bus connection.
565  * @object_path: The object path the method was invoked on.
566  * @interface_name: The name of the D-Bus interface the method was invoked on.
567  * @method_name: The name of the method that was invoked.
568  * @method_info: Information about the method call or %NULL.
569  * @connection: The #GDBusConnection the method was invoked on.
570  * @message: The D-Bus message as a #GDBusMessage.
571  * @parameters: The parameters as a #GVariant tuple.
572  * @user_data: The @user_data #gpointer passed to g_dbus_connection_register_object().
573  *
574  * Creates a new #GDBusMethodInvocation object.
575  *
576  * Returns: A #GDBusMethodInvocation. Free with g_object_unref().
577  *
578  * Since: 2.26
579  */
580 GDBusMethodInvocation *
581 g_dbus_method_invocation_new (const gchar           *sender,
582                               const gchar           *object_path,
583                               const gchar           *interface_name,
584                               const gchar           *method_name,
585                               const GDBusMethodInfo *method_info,
586                               GDBusConnection       *connection,
587                               GDBusMessage          *message,
588                               GVariant              *parameters,
589                               gpointer               user_data)
590 {
591   g_return_val_if_fail (sender == NULL || g_dbus_is_name (sender), NULL);
592   g_return_val_if_fail (g_variant_is_object_path (object_path), NULL);
593   g_return_val_if_fail (interface_name == NULL || g_dbus_is_interface_name (interface_name), NULL);
594   g_return_val_if_fail (g_dbus_is_member_name (method_name), NULL);
595   g_return_val_if_fail (G_IS_DBUS_CONNECTION (connection), NULL);
596   g_return_val_if_fail (G_IS_DBUS_MESSAGE (message), NULL);
597   g_return_val_if_fail (g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE), NULL);
598
599   return G_DBUS_METHOD_INVOCATION (g_object_new (G_TYPE_DBUS_METHOD_INVOCATION,
600                                                  "sender", sender,
601                                                  "object-path", object_path,
602                                                  "interface-name", interface_name,
603                                                  "method-name", method_name,
604                                                  "method-info", method_info,
605                                                  "connection", connection,
606                                                  "message", message,
607                                                  "parameters", parameters,
608                                                  "user-data", user_data,
609                                                  NULL));
610 }
611
612 /* ---------------------------------------------------------------------------------------------------- */
613
614 /**
615  * g_dbus_method_invocation_return_value:
616  * @invocation: A #GDBusMethodInvocation.
617  * @parameters: A #GVariant tuple with out parameters for the method or %NULL if not passing any parameters.
618  *
619  * Finishes handling a D-Bus method call by returning @parameters.
620  *
621  * It is an error if @parameters is not of the right format.
622  *
623  * This method will free @invocation, you cannot use it afterwards.
624  *
625  * Since: 2.26
626  */
627 void
628 g_dbus_method_invocation_return_value (GDBusMethodInvocation *invocation,
629                                        GVariant              *parameters)
630 {
631   GDBusMessage *reply;
632   GError *error;
633
634   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
635   g_return_if_fail ((parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE));
636
637   if (parameters != NULL)
638     g_variant_ref_sink (parameters);
639
640   /* if we have introspection data, check that the signature of @parameters is correct */
641   if (invocation->priv->method_info != NULL)
642     {
643       gchar *signature;
644       const gchar *type_string;
645
646       type_string = "()";
647       if (parameters != NULL)
648         type_string = g_variant_get_type_string (parameters);
649       signature = _g_dbus_compute_complete_signature (invocation->priv->method_info->out_args, TRUE);
650
651       if (g_strcmp0 (type_string, signature) != 0)
652         {
653           g_warning (_("Type of return value is incorrect, got `%s', expected  `%s'"),
654                      type_string,
655                      signature);
656           g_free (signature);
657           goto out;
658         }
659       g_free (signature);
660     }
661
662   reply = g_dbus_message_new_method_reply (invocation->priv->message);
663   g_dbus_message_set_body (reply, parameters);
664   error = NULL;
665   if (!g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, &error))
666     {
667       g_warning (_("Error sending message: %s"), error->message);
668       g_error_free (error);
669     }
670   g_object_unref (reply);
671
672  out:
673   g_object_unref (invocation);
674   if (parameters != NULL)
675     g_variant_unref (parameters);
676 }
677
678 /* ---------------------------------------------------------------------------------------------------- */
679
680 /**
681  * g_dbus_method_invocation_return_error:
682  * @invocation: A #GDBusMethodInvocation.
683  * @domain: A #GQuark for the #GError error domain.
684  * @code: The error code.
685  * @format: printf()-style format.
686  * @...: Parameters for @format.
687  *
688  * Finishes handling a D-Bus method call by returning an error.
689  *
690  * See g_dbus_error_encode_gerror() for details about what error name
691  * will be returned on the wire. In a nutshell, if the given error is
692  * registered using g_dbus_error_register_error() the name given
693  * during registration is used. Otherwise, a name of the form
694  * <literal>org.gtk.GDBus.UnmappedGError.Quark...</literal> is
695  * used. This provides transparent mapping of #GError between
696  * applications using GDBus.
697  *
698  * If you are writing an application intended to be portable,
699  * <emphasis>always</emphasis> register errors with g_dbus_error_register_error()
700  * or use g_dbus_method_invocation_return_dbus_error().
701  *
702  * This method will free @invocation, you cannot use it afterwards.
703  *
704  * Since: 2.26
705  */
706 void
707 g_dbus_method_invocation_return_error (GDBusMethodInvocation *invocation,
708                                        GQuark                 domain,
709                                        gint                   code,
710                                        const gchar           *format,
711                                        ...)
712 {
713   va_list var_args;
714
715   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
716   g_return_if_fail (format != NULL);
717
718   va_start (var_args, format);
719   g_dbus_method_invocation_return_error_valist (invocation,
720                                                 domain,
721                                                 code,
722                                                 format,
723                                                 var_args);
724   va_end (var_args);
725 }
726
727 /**
728  * g_dbus_method_invocation_return_error_valist:
729  * @invocation: A #GDBusMethodInvocation.
730  * @domain: A #GQuark for the #GError error domain.
731  * @code: The error code.
732  * @format: printf()-style format.
733  * @var_args: #va_list of parameters for @format.
734  *
735  * Like g_dbus_method_invocation_return_error() but intended for
736  * language bindings.
737  *
738  * This method will free @invocation, you cannot use it afterwards.
739  *
740  * Since: 2.26
741  */
742 void
743 g_dbus_method_invocation_return_error_valist (GDBusMethodInvocation *invocation,
744                                               GQuark                 domain,
745                                               gint                   code,
746                                               const gchar           *format,
747                                               va_list                var_args)
748 {
749   gchar *literal_message;
750
751   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
752   g_return_if_fail (format != NULL);
753
754   literal_message = g_strdup_vprintf (format, var_args);
755   g_dbus_method_invocation_return_error_literal (invocation,
756                                                  domain,
757                                                  code,
758                                                  literal_message);
759   g_free (literal_message);
760 }
761
762 /**
763  * g_dbus_method_invocation_return_error_literal:
764  * @invocation: A #GDBusMethodInvocation.
765  * @domain: A #GQuark for the #GError error domain.
766  * @code: The error code.
767  * @message: The error message.
768  *
769  * Like g_dbus_method_invocation_return_error() but without printf()-style formatting.
770  *
771  * This method will free @invocation, you cannot use it afterwards.
772  *
773  * Since: 2.26
774  */
775 void
776 g_dbus_method_invocation_return_error_literal (GDBusMethodInvocation *invocation,
777                                                GQuark                 domain,
778                                                gint                   code,
779                                                const gchar           *message)
780 {
781   GError *error;
782
783   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
784   g_return_if_fail (message != NULL);
785
786   error = g_error_new_literal (domain, code, message);
787   g_dbus_method_invocation_return_gerror (invocation, error);
788   g_error_free (error);
789 }
790
791 /**
792  * g_dbus_method_invocation_return_gerror:
793  * @invocation: A #GDBusMethodInvocation.
794  * @error: A #GError.
795  *
796  * Like g_dbus_method_invocation_return_error() but takes a #GError
797  * instead of the error domain, error code and message.
798  *
799  * This method will free @invocation, you cannot use it afterwards.
800  *
801  * Since: 2.26
802  */
803 void
804 g_dbus_method_invocation_return_gerror (GDBusMethodInvocation *invocation,
805                                         const GError          *error)
806 {
807   gchar *dbus_error_name;
808
809   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
810   g_return_if_fail (error != NULL);
811
812   dbus_error_name = g_dbus_error_encode_gerror (error);
813
814   g_dbus_method_invocation_return_dbus_error (invocation,
815                                               dbus_error_name,
816                                               error->message);
817   g_free (dbus_error_name);
818 }
819
820 /**
821  * g_dbus_method_invocation_return_dbus_error:
822  * @invocation: A #GDBusMethodInvocation.
823  * @error_name: A valid D-Bus error name.
824  * @error_message: A valid D-Bus error message.
825  *
826  * Finishes handling a D-Bus method call by returning an error.
827  *
828  * This method will free @invocation, you cannot use it afterwards.
829  *
830  * Since: 2.26
831  */
832 void
833 g_dbus_method_invocation_return_dbus_error (GDBusMethodInvocation *invocation,
834                                             const gchar           *error_name,
835                                             const gchar           *error_message)
836 {
837   GDBusMessage *reply;
838
839   g_return_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation));
840   g_return_if_fail (error_name != NULL && g_dbus_is_name (error_name));
841   g_return_if_fail (error_message != NULL);
842
843   reply = g_dbus_message_new_method_error_literal (invocation->priv->message,
844                                                    error_name,
845                                                    error_message);
846   g_dbus_connection_send_message (g_dbus_method_invocation_get_connection (invocation), reply, NULL, NULL);
847   g_object_unref (reply);
848
849   g_object_unref (invocation);
850 }
851
852 #define __G_DBUS_METHOD_INVOCATION_C__
853 #include "gioaliasdef.c"