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