<commit_before>/*
 * simple-channel-request.c - simple channel request service.
 *
 * Copyright © 2010 Collabora Ltd.
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "simple-channel-request.h"

#include <telepathy-glib/client.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/proxy-subclass.h>
#include <telepathy-glib/svc-channel-request.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/telepathy-glib.h>
#include <telepathy-glib/util.h>

#include "tests/lib/util.h"
#include "tests/lib/textchan-null.h"

static void channel_request_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE (TpTestsSimpleChannelRequest,
    tp_tests_simple_channel_request,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CHANNEL_REQUEST,
        channel_request_iface_init);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
        tp_dbus_properties_mixin_iface_init)
    )


/* TP_IFACE_CHANNEL_REQUEST is implied */
static const char *CHANNEL_REQUEST_INTERFACES[] = { NULL };

enum
{
  PROP_0,
  PROP_PATH,
  PROP_ACCOUNT,
  PROP_USER_ACTION_TIME,
  PROP_PREFERRED_HANDLER,
  PROP_REQUESTS,
  PROP_CONNECTION,
  PROP_INTERFACES,
};

struct _TpTestsSimpleChannelRequestPrivate
{
  /* D-Bus properties */
  gchar *account_path;
  gint64 user_action_time;
  gchar *preferred_handler;
  GPtrArray *requests;

  /* Our own path */
  gchar *path;

  /* connection used to create channels */
  TpTestsSimpleConnection *conn;
};

static void
handle_channels_cb (TpClient *client,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpTestsSimpleChannelRequest *self = SIMPLE_CHANNEL_REQUEST (weak_object);

  if (error != NULL)
    {
      tp_svc_channel_request_emit_failed (self,
          tp_error_get_dbus_name (error->code), error->message);
      return;
    }

  tp_svc_channel_request_emit_succeeded (self);
}

static void
add_channel (TpTestsSimpleChannelRequest *self,
    GPtrArray *channels)
{
  const gchar *target_id;
  gchar *chan_path;
  GHashTable *request;
  GHashTable *props;

  request = g_ptr_array_index (self->priv->requests, 0);
  target_id = tp_asv_get_string (request, TP_PROP_CHANNEL_TARGET_ID);
  g_assert (target_id != NULL);

  chan_path = tp_tests_simple_connection_ensure_text_chan (self->priv->conn,
      target_id, &props);

  g_ptr_array_add (channels, tp_value_array_build (2,
      DBUS_TYPE_G_OBJECT_PATH, chan_path,
      TP_HASH_TYPE_STRING_VARIANT_MAP, props,
      G_TYPE_INVALID));

  g_free (chan_path);
  g_hash_table_unref (props);
}

static void
free_channel_details (gpointer data,
    gpointer user_data)
{
  g_boxed_free (TP_STRUCT_TYPE_CHANNEL_DETAILS, data);
}

static void
tp_tests_simple_channel_request_proceed (TpSvcChannelRequest *request,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelRequest *self = SIMPLE_CHANNEL_REQUEST (request);
  TpClient *client;
  TpDBusDaemon *dbus;
  gchar *client_path;
  GPtrArray *channels;
  GPtrArray *satisfied;
  GHashTable *info;
  TpBaseConnection *base_conn = (TpBaseConnection *) self->priv->conn;
  GHashTable *req;

  req = g_ptr_array_index (self->priv->requests, 0);
  g_assert (req != NULL);
  if (tp_asv_get_boolean (req, "ProceedFail", NULL))
    {
      /* We have been asked to fail */
     GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
          "Computer says no" };

      dbus_g_method_return_error (context, &error);
      return;
    }

  tp_svc_channel_request_return_from_proceed (context);

  if (tp_asv_get_boolean (req, "FireFailed", NULL))
    {
      /* We have been asked to fire the 'Failed' signal */
      tp_svc_channel_request_emit_failed (self, TP_ERROR_STR_INVALID_ARGUMENT,
          "Let's fail!");
      return;
    }

  /* We just support handling request having a preferred handler */
  if (self->priv->preferred_handler == NULL ||
      !tp_strdiff (self->priv->preferred_handler, ""))
    {
      tp_svc_channel_request_emit_failed (self, TP_ERROR_STR_NOT_AVAILABLE,
          "ChannelRequest doesn't have a preferred handler");
      return;
    }

  if (!tp_strdiff (self->priv->preferred_handler, "Fake"))
    {
      /* Pretend that the channel has been handled */
      tp_svc_channel_request_emit_succeeded (self);
      return;
    }

  /* Call HandleChannels() on the preferred handler */
  client_path = g_strdelimit (g_strdup_printf ("/%s",
        self->priv->preferred_handler), ".", '/');

  dbus = tp_dbus_daemon_dup (NULL);
  g_assert (dbus != NULL);

  client = tp_tests_object_new_static_class (TP_TYPE_CLIENT,
          "dbus-daemon", dbus,
          "bus-name", self->priv->preferred_handler,
          "object-path", client_path,
          NULL);

  tp_proxy_add_interface_by_id (TP_PROXY (client), TP_IFACE_QUARK_CLIENT);
  tp_proxy_add_interface_by_id (TP_PROXY (client),
      TP_IFACE_QUARK_CLIENT_HANDLER);

  channels = g_ptr_array_sized_new (1);
  add_channel (self, channels);

  satisfied = g_ptr_array_sized_new (1);
  g_ptr_array_add (satisfied, self->priv->path);

  info = g_hash_table_new (NULL, NULL);<commit_msg>simple-channel-request: fire SucceededWithChannel in the fake case<commit_after>/*
 * simple-channel-request.c - simple channel request service.
 *
 * Copyright © 2010 Collabora Ltd.
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.
 */

#include "simple-channel-request.h"

#include <telepathy-glib/client.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/proxy-subclass.h>
#include <telepathy-glib/svc-channel-request.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/telepathy-glib.h>
#include <telepathy-glib/util.h>

#include "tests/lib/util.h"
#include "tests/lib/textchan-null.h"

static void channel_request_iface_init (gpointer, gpointer);

G_DEFINE_TYPE_WITH_CODE (TpTestsSimpleChannelRequest,
    tp_tests_simple_channel_request,
    G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CHANNEL_REQUEST,
        channel_request_iface_init);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
        tp_dbus_properties_mixin_iface_init)
    )


/* TP_IFACE_CHANNEL_REQUEST is implied */
static const char *CHANNEL_REQUEST_INTERFACES[] = { NULL };

enum
{
  PROP_0,
  PROP_PATH,
  PROP_ACCOUNT,
  PROP_USER_ACTION_TIME,
  PROP_PREFERRED_HANDLER,
  PROP_REQUESTS,
  PROP_CONNECTION,
  PROP_INTERFACES,
};

struct _TpTestsSimpleChannelRequestPrivate
{
  /* D-Bus properties */
  gchar *account_path;
  gint64 user_action_time;
  gchar *preferred_handler;
  GPtrArray *requests;

  /* Our own path */
  gchar *path;

  /* connection used to create channels */
  TpTestsSimpleConnection *conn;
};

static void
handle_channels_cb (TpClient *client,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpTestsSimpleChannelRequest *self = SIMPLE_CHANNEL_REQUEST (weak_object);

  if (error != NULL)
    {
      tp_svc_channel_request_emit_failed (self,
          tp_error_get_dbus_name (error->code), error->message);
      return;
    }

  tp_svc_channel_request_emit_succeeded (self);
}

static void
add_channel (TpTestsSimpleChannelRequest *self,
    GPtrArray *channels)
{
  const gchar *target_id;
  gchar *chan_path;
  GHashTable *request;
  GHashTable *props;

  request = g_ptr_array_index (self->priv->requests, 0);
  target_id = tp_asv_get_string (request, TP_PROP_CHANNEL_TARGET_ID);
  g_assert (target_id != NULL);

  chan_path = tp_tests_simple_connection_ensure_text_chan (self->priv->conn,
      target_id, &props);

  g_ptr_array_add (channels, tp_value_array_build (2,
      DBUS_TYPE_G_OBJECT_PATH, chan_path,
      TP_HASH_TYPE_STRING_VARIANT_MAP, props,
      G_TYPE_INVALID));

  g_free (chan_path);
  g_hash_table_unref (props);
}

static void
free_channel_details (gpointer data,
    gpointer user_data)
{
  g_boxed_free (TP_STRUCT_TYPE_CHANNEL_DETAILS, data);
}

static void
tp_tests_simple_channel_request_proceed (TpSvcChannelRequest *request,
    DBusGMethodInvocation *context)
{
  TpTestsSimpleChannelRequest *self = SIMPLE_CHANNEL_REQUEST (request);
  TpClient *client;
  TpDBusDaemon *dbus;
  gchar *client_path;
  GPtrArray *channels;
  GPtrArray *satisfied;
  GHashTable *info;
  TpBaseConnection *base_conn = (TpBaseConnection *) self->priv->conn;
  GHashTable *req;

  req = g_ptr_array_index (self->priv->requests, 0);
  g_assert (req != NULL);
  if (tp_asv_get_boolean (req, "ProceedFail", NULL))
    {
      /* We have been asked to fail */
     GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
          "Computer says no" };

      dbus_g_method_return_error (context, &error);
      return;
    }

  tp_svc_channel_request_return_from_proceed (context);

  if (tp_asv_get_boolean (req, "FireFailed", NULL))
    {
      /* We have been asked to fire the 'Failed' signal */
      tp_svc_channel_request_emit_failed (self, TP_ERROR_STR_INVALID_ARGUMENT,
          "Let's fail!");
      return;
    }

  /* We just support handling request having a preferred handler */
  if (self->priv->preferred_handler == NULL ||
      !tp_strdiff (self->priv->preferred_handler, ""))
    {
      tp_svc_channel_request_emit_failed (self, TP_ERROR_STR_NOT_AVAILABLE,
          "ChannelRequest doesn't have a preferred handler");
      return;
    }

  if (!tp_strdiff (self->priv->preferred_handler, "Fake"))
    {
      /* Pretend that the channel has been handled */
      GHashTable *props;

      props = g_hash_table_new (NULL, NULL);

      tp_svc_channel_request_emit_succeeded_with_channel (self,
          base_conn->object_path, props, "/chan", props);
      tp_svc_channel_request_emit_succeeded (self);

      g_hash_table_unref (props);
      return;
    }

  /* Call HandleChannels() on the preferred handler */
  client_path = g_strdelimit (g_strdup_printf ("/%s",
        self->priv->preferred_handler), ".", '/');

  dbus = tp_dbus_daemon_dup (NULL);
  g_assert (dbus != NULL);

  client = tp_tests_object_new_static_class (TP_TYPE_CLIENT,
          "dbus-daemon", dbus,
          "bus-name", self->priv->preferred_handler,
          "object-path", client_path,
          NULL);

  tp_proxy_add_interface_by_id (TP_PROXY (client), TP_IFACE_QUARK_CLIENT);
  tp_proxy_add_interface_by_id (TP_PROXY (client),
      TP_IFACE_QUARK_CLIENT_HANDLER);

  channels = g_ptr_array_sized_new (1);
  add_channel (self, channels);

  satisfied = g_ptr_array_sized_new (1);
  g_ptr_array_add (satisfied, self->priv->path);

  info = g_hash_table_new (NULL, NULL);
{'fraction_non_alphanumeric': 0.10007461294534602, 'fraction_numerical': 0.002238388360380526, 'mean_word_length': 4.177691936262675, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 62, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3085486', 'n_tokens_mistral': 3848, 'n_tokens_neox': 3725, 'n_words': 747}