]> git.meshlink.io Git - catta/blob - avahi-daemon/dbus-protocol.c
* more dbus build fixes
[catta] / avahi-daemon / dbus-protocol.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <glib.h>
23 #define DBUS_API_SUBJECT_TO_CHANGE
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib-lowlevel.h>
26     
27 #include "dbus-protocol.h"
28
29 static DBusConnection *bus = NULL;
30
31 static DBusHandlerResult
32 do_register (DBusConnection *conn, DBusMessage *message)
33 {
34     DBusError error;
35     char *s;
36
37     dbus_error_init (&error);
38
39     dbus_message_get_args (message, &error,
40                            DBUS_TYPE_STRING, &s,
41                            DBUS_TYPE_INVALID);
42
43     if (dbus_error_is_set (&error))
44     {
45         g_warning ("Error parsing register attempt");
46         dbus_error_free (&error);
47
48         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
49     }
50
51     g_message ("Register received from: %s", s);
52
53     return DBUS_HANDLER_RESULT_HANDLED;
54 }
55
56 static DBusHandlerResult
57 signal_filter (DBusConnection *conn, DBusMessage *message, void *user_data)
58 {
59     GMainLoop *loop = user_data;
60     DBusError error;
61
62     dbus_error_init (&error);
63
64     g_message ("dbus: interface=%s, path=%s, member=%s",
65                dbus_message_get_interface (message),
66                dbus_message_get_path (message),
67                dbus_message_get_member (message));
68
69     if (dbus_message_is_signal (message,
70                                 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL,
71                                 "Disconnected"))
72     {
73         /* No, we shouldn't quit, but until we get somewhere
74          * usefull such that we can restore our state, we will */
75         g_warning ("Disconnnected from d-bus, terminating...");
76
77         g_main_loop_quit (loop);
78         return DBUS_HANDLER_RESULT_HANDLED;
79     } else if (dbus_message_is_method_call (message, DBUS_SERVICE_AVAHI,
80                                             "Register"))
81     {
82         return do_register (conn, message);
83     } else if (dbus_message_is_signal (message,
84                                        DBUS_INTERFACE_ORG_FREEDESKTOP_DBUS,
85                                        "ServiceAcquired"))
86     {
87         char *name;
88
89         dbus_message_get_args (message, &error,
90                                DBUS_TYPE_STRING, &name,
91                                DBUS_TYPE_INVALID);
92
93         if (dbus_error_is_set (&error))
94         {
95             g_warning ("Error parsing NameAcquired message");
96             dbus_error_free (&error);
97
98             return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
99         }
100
101         g_message ("dbus: ServiceAcquired (%s)", name);
102
103         return DBUS_HANDLER_RESULT_HANDLED;
104     }
105
106     g_message ("dbus: missed event");
107
108     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
109 }
110
111 int
112 dbus_protocol_setup (GMainLoop *loop)
113 {
114     DBusError error;
115
116     dbus_error_init (&error);
117
118     bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
119
120     if (bus == NULL)
121     {
122         g_warning ("dbus_bus_get(): %s", error.message);
123         dbus_error_free (&error);
124
125         return 1;
126     }
127
128     dbus_connection_setup_with_g_main (bus, NULL);
129     dbus_connection_set_exit_on_disconnect (bus, FALSE);
130
131     dbus_bus_acquire_service (bus, DBUS_SERVICE_AVAHI, 0, &error);
132
133     if (dbus_error_is_set (&error))
134     {
135         g_warning ("dbus_error_is_set (): %s", error.message);
136         dbus_error_free (&error);
137
138         return 1;
139     }
140
141     dbus_connection_add_filter (bus, signal_filter, loop, NULL);
142     dbus_bus_add_match (bus,
143                         "type='method_call',interface='org.freedesktop.Avahi'",
144                         &error);
145
146     if (dbus_error_is_set (&error))
147     {
148         g_warning ("dbus_bus_add_match (): %s", error.message);
149         dbus_error_free (&error);
150
151         return 1;
152     }
153
154     return 0;
155 }
156
157 void
158 dbus_protocol_shutdown ()
159 {
160     if (bus) {
161         dbus_connection_disconnect(bus);
162         dbus_connection_unref(bus);
163     }
164 }