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