Esempio n. 1
0
static void
on_name_lost_or_acquired (GDBusConnection  *connection,
                          const gchar      *sender_name,
                          const gchar      *object_path,
                          const gchar      *interface_name,
                          const gchar      *signal_name,
                          GVariant         *parameters,
                          gpointer          user_data)
{
  Client *client = user_data;
  const gchar *name;

  if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
      g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
      g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
    goto out;

  if (g_strcmp0 (signal_name, "NameLost") == 0)
    {
      g_variant_get (parameters, "(&s)", &name);
      if (g_strcmp0 (name, client->name) == 0)
        {
          call_lost_handler (client);
        }
    }
  else if (g_strcmp0 (signal_name, "NameAcquired") == 0)
    {
      g_variant_get (parameters, "(&s)", &name);
      if (g_strcmp0 (name, client->name) == 0)
        {
          call_acquired_handler (client);
        }
    }
 out:
  ;
}
Esempio n. 2
0
static void
request_name_cb (GObject      *source_object,
                 GAsyncResult *res,
                 gpointer      user_data)
{
  Client *client = user_data;
  GVariant *result;
  guint32 request_name_reply;
  gboolean subscribe;

  request_name_reply = 0;
  result = NULL;

  /* don't use client->connection - it may be NULL already */
  result = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
                                          res,
                                          NULL);
  if (result != NULL)
    {
      g_variant_get (result, "(u)", &request_name_reply);
      g_variant_unref (result);
    }

  subscribe = FALSE;

  switch (request_name_reply)
    {
    case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
      /* We got the name - now listen for NameLost and NameAcquired */
      call_acquired_handler (client);
      subscribe = TRUE;
      client->needs_release = TRUE;
      break;

    case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
      /* Waiting in line - listen for NameLost and NameAcquired */
      call_lost_handler (client);
      subscribe = TRUE;
      client->needs_release = TRUE;
      break;

    default:
      /* assume we couldn't get the name - explicit fallthrough */
    case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
    case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
      /* Some other part of the process is already owning the name */
      call_lost_handler (client);
      break;
    }


  if (subscribe)
    {
      GDBusConnection *connection = NULL;

      /* if cancelled, there is no point in subscribing to signals - if not, make sure
       * we use a known good Connection object since it may be set to NULL at any point
       * after being cancelled
       */
      G_LOCK (lock);
      if (!client->cancelled)
        connection = g_object_ref (client->connection);
      G_UNLOCK (lock);

      /* start listening to NameLost and NameAcquired messages */
      if (connection != NULL)
        {
          client->name_lost_subscription_id =
            g_dbus_connection_signal_subscribe (connection,
                                                "org.freedesktop.DBus",
                                                "org.freedesktop.DBus",
                                                "NameLost",
                                                "/org/freedesktop/DBus",
                                                client->name,
                                                G_DBUS_SIGNAL_FLAGS_NONE,
                                                on_name_lost_or_acquired,
                                                client,
                                                NULL);
          client->name_acquired_subscription_id =
            g_dbus_connection_signal_subscribe (connection,
                                                "org.freedesktop.DBus",
                                                "org.freedesktop.DBus",
                                                "NameAcquired",
                                                "/org/freedesktop/DBus",
                                                client->name,
                                                G_DBUS_SIGNAL_FLAGS_NONE,
                                                on_name_lost_or_acquired,
                                                client,
                                                NULL);
          g_object_unref (connection);
        }
    }

  client_unref (client);
}
Esempio n. 3
0
static void
request_name_cb (GObject      *source_object,
                 GAsyncResult *res,
                 gpointer      user_data)
{
  Client *client = user_data;
  GVariant *result;
  guint32 request_name_reply;
  gboolean subscribe;

  request_name_reply = 0;
  result = NULL;

  result = g_dbus_connection_call_finish (client->connection,
                                          res,
                                          NULL);
  if (result != NULL)
    {
      g_variant_get (result, "(u)", &request_name_reply);
      g_variant_unref (result);
    }

  subscribe = FALSE;

  switch (request_name_reply)
    {
    case 1: /* DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER */
      /* We got the name - now listen for NameLost and NameAcquired */
      call_acquired_handler (client);
      subscribe = TRUE;
      client->needs_release = TRUE;
      break;

    case 2: /* DBUS_REQUEST_NAME_REPLY_IN_QUEUE */
      /* Waiting in line - listen for NameLost and NameAcquired */
      call_lost_handler (client);
      subscribe = TRUE;
      client->needs_release = TRUE;
      break;

    default:
      /* assume we couldn't get the name - explicit fallthrough */
    case 3: /* DBUS_REQUEST_NAME_REPLY_EXISTS */
    case 4: /* DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER */
      /* Some other part of the process is already owning the name */
      call_lost_handler (client);
      break;
    }

  if (subscribe)
    {
      /* start listening to NameLost and NameAcquired messages */
      client->name_lost_subscription_id =
        g_dbus_connection_signal_subscribe (client->connection,
                                            "org.freedesktop.DBus",
                                            "org.freedesktop.DBus",
                                            "NameLost",
                                            "/org/freedesktop/DBus",
                                            client->name,
                                            on_name_lost_or_acquired,
                                            client,
                                            NULL);
      client->name_acquired_subscription_id =
        g_dbus_connection_signal_subscribe (client->connection,
                                            "org.freedesktop.DBus",
                                            "org.freedesktop.DBus",
                                            "NameAcquired",
                                            "/org/freedesktop/DBus",
                                            client->name,
                                            on_name_lost_or_acquired,
                                            client,
                                            NULL);
    }

  client_unref (client);
}