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