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