2003-03-12 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-bus.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-bus.c  Convenience functions for communicating with the bus.
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 1.2
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dbus-bus.h"
26 #include "dbus-protocol.h"
27 #include "dbus-internals.h"
28
29 /**
30  * @defgroup DBusBus Message bus APIs
31  * @ingroup DBus
32  * @brief Functions for communicating with the message bus
33  *
34  * @{
35  */
36
37 /**
38  * Registers a connection with the bus. This must be the first
39  * thing an application does when connecting to the message bus.
40  *
41  * @todo if we get an error reply, it has to be converted into
42  * DBusError and returned
43  * 
44  * @param connection the connection
45  * @param error place to store errors
46  * @returns the client's unique service name, #NULL on error
47  */
48 char*
49 dbus_bus_register_client (DBusConnection *connection,
50                           DBusError      *error)
51 {
52   DBusMessage *message, *reply;
53   char *name;
54   
55   message = dbus_message_new (DBUS_SERVICE_DBUS,
56                               DBUS_MESSAGE_HELLO);
57
58   if (!message)
59     {
60       _DBUS_SET_OOM (error);
61       return NULL;
62     }
63   
64   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
65
66   dbus_message_unref (message);
67   
68   if (reply == NULL)
69     {
70       _DBUS_ASSERT_ERROR_IS_SET (error);
71       return NULL;
72     }
73
74   if (!dbus_message_get_args (reply, error,
75                               DBUS_TYPE_STRING, &name,
76                               0))
77     {
78       _DBUS_ASSERT_ERROR_IS_SET (error);
79       return NULL;
80     }
81   
82   return name;
83 }
84
85 /**
86  * Asks the bus to try to acquire a certain service.
87  *
88  * @todo these docs are not complete, need to document the
89  * return value and flags
90  * 
91  * @todo if we get an error reply, it has to be converted into
92  * DBusError and returned
93  *
94  * @param connection the connection
95  * @param service_name the service name
96  * @param flags flags
97  * @param error location to store the error
98  * @returns a result code, -1 if error is set
99  */ 
100 int
101 dbus_bus_acquire_service (DBusConnection *connection,
102                           const char     *service_name,
103                           unsigned int    flags,
104                           DBusError      *error)
105 {
106   DBusMessage *message, *reply;
107   int service_result;
108   
109   message = dbus_message_new (DBUS_SERVICE_DBUS,
110                               DBUS_MESSAGE_ACQUIRE_SERVICE);
111
112   if (message == NULL)
113     {
114       _DBUS_SET_OOM (error);
115       return -1;
116     }
117  
118   if (!dbus_message_append_args (message,
119                                  DBUS_TYPE_STRING, service_name,
120                                  DBUS_TYPE_UINT32, flags,
121                                  0))
122     {
123       dbus_message_unref (message);
124       _DBUS_SET_OOM (error);
125       return -1;
126     }
127   
128   reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
129                                                      error);
130   
131   dbus_message_unref (message);
132   
133   if (reply == NULL)
134     {
135       _DBUS_ASSERT_ERROR_IS_SET (error);
136       return -1;
137     }
138
139   if (!dbus_message_get_args (reply, error,
140                               DBUS_TYPE_UINT32, &service_result,
141                               0))
142     {
143       _DBUS_ASSERT_ERROR_IS_SET (error);
144       return -1;
145     }
146
147   return service_result;
148 }
149
150 /**
151  * Checks whether a certain service exists.
152  *
153  * @todo the SERVICE_EXISTS message should use BOOLEAN not UINT32
154  *
155  * @param connection the connection
156  * @param service_name the service name
157  * @param error location to store any errors
158  * @returns #TRUE if the service exists, #FALSE if not or on error
159  */
160 dbus_bool_t
161 dbus_bus_service_exists (DBusConnection *connection,
162                          const char     *service_name,
163                          DBusError      *error)
164 {
165   DBusMessage *message, *reply;
166   unsigned int exists;
167   
168   message = dbus_message_new (DBUS_SERVICE_DBUS,
169                               DBUS_MESSAGE_SERVICE_EXISTS);
170   if (message == NULL)
171     {
172       _DBUS_SET_OOM (error);
173       return FALSE;
174     }
175   
176   if (!dbus_message_append_args (message,
177                                  DBUS_TYPE_STRING, service_name,
178                                  0))
179     {
180       dbus_message_unref (message);
181       _DBUS_SET_OOM (error);
182       return FALSE;
183     }
184   
185   reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
186   dbus_message_unref (message);
187
188   if (reply == NULL)
189     {
190       _DBUS_ASSERT_ERROR_IS_SET (error);
191       return FALSE;
192     }
193
194   if (!dbus_message_get_args (reply, error,
195                               DBUS_TYPE_UINT32, &exists,
196                               0))
197     {
198       _DBUS_ASSERT_ERROR_IS_SET (error);
199       return FALSE;
200     }
201   
202   return (exists != FALSE);
203 }
204
205 /** @} */