+#include "client.h"
+#include "internal.h"
+
+int
+avahi_client_set_errno (AvahiClient *client, int error)
+{
+ if (client == NULL) return error;
+
+ client->error = error;
+
+ return error;
+}
+
+static
+void avahi_client_state_change (AvahiClient *client, int state)
+{
+ if (client == NULL || client->callback == NULL)
+ return;
+
+ client->callback (client, state, client->user_data);
+}
+
+void
+avahi_client_state_request_callback (DBusPendingCall *call, void *data)
+{
+ AvahiClient *client = data;
+ DBusError error;
+ DBusMessage *reply;
+ int state, type;
+
+ dbus_error_init (&error);
+
+ reply = dbus_pending_call_steal_reply (call);
+
+ type = dbus_message_get_type (reply);
+
+ if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
+ {
+ dbus_message_get_args (reply, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
+
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "internal error parsing client state change for client\n");
+ return;
+ }
+
+ printf ("statechange (client) to %d\n", state);
+
+ avahi_client_state_change (client, state);
+ } else if (type == DBUS_MESSAGE_TYPE_ERROR) {
+ dbus_set_error_from_message (&error, reply);
+ fprintf (stderr, "Error from reply: %s\n", error.message);
+ }
+
+ dbus_pending_call_unref (call);
+}
+
+void
+avahi_client_schedule_state_request (AvahiClient *client)
+{
+ DBusMessage *message;
+ DBusPendingCall *pcall;
+
+ if (client == NULL) return;
+
+ message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "GetState");
+
+ dbus_connection_send_with_reply (client->bus, message, &pcall, -1);
+
+ dbus_pending_call_set_notify (pcall, avahi_client_state_request_callback, client, NULL);
+}
+
+static DBusHandlerResult
+filter_func (DBusConnection *bus, DBusMessage *message, void *data)
+{
+ AvahiClient *client = data;
+ DBusError error;
+
+ printf ("dbus: interface=%s, path=%s, member=%s\n",
+ dbus_message_get_interface (message),
+ dbus_message_get_path (message),
+ dbus_message_get_member (message));
+
+ dbus_error_init (&error);
+
+ if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
+ gchar *name, *old, *new;
+ dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
+
+ if (dbus_error_is_set (&error)) {
+ fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
+ dbus_error_free (&error);
+ goto out;
+ }
+
+ if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
+
+ if (old == NULL && new != NULL) {
+ fprintf(stderr, "Avahi Daemon connected\n");
+ avahi_client_state_change (client, AVAHI_CLIENT_RECONNECTED);
+ } else if (old != NULL && new == NULL) {
+ fprintf(stderr, "Avahi Daemon disconnected\n");
+ avahi_client_state_change (client, AVAHI_CLIENT_DISCONNECTED);
+ /* XXX: we really need to expire all entry groups */
+ }
+ }
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_NAME, "StateChanged")) {
+ printf ("server statehcange\n");
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged")) {
+ const char *path;
+ AvahiEntryGroup *n, *group = NULL;
+ path = dbus_message_get_path (message);
+
+ for (n = client->groups; n != NULL; n = n->groups_next)
+ {
+ if (strcmp (n->path, path) == 0)
+ {
+ group = n;
+ break;
+ }
+ }
+
+ if (group == NULL)
+ {
+ fprintf (stderr, "Received state change for unknown EntryGroup object (%s)\n", path);
+ } else {
+ int state;
+ DBusError error;
+ dbus_error_init (&error);
+ dbus_message_get_args (message, &error, DBUS_TYPE_INT32, &state, DBUS_TYPE_INVALID);
+ if (dbus_error_is_set (&error))
+ {
+ fprintf (stderr, "internal error parsing entrygroup statechange for %s\n", group->path);
+ goto out;
+ }
+ printf ("statechange (%s) to %d\n", group->path, state);
+ avahi_entry_group_state_change (group, state);
+ }
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemNew")) {
+ return avahi_domain_browser_event (client, AVAHI_BROWSER_NEW, message);
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, "ItemRemove")) {
+ return avahi_domain_browser_event (client, AVAHI_BROWSER_REMOVE, message);
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemNew")) {
+ return avahi_service_type_browser_event (client, AVAHI_BROWSER_NEW, message);
+ } else if (dbus_message_is_signal (message, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, "ItemRemove")) {
+ return avahi_service_type_browser_event (client, AVAHI_BROWSER_REMOVE, message);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+
+out:
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+