<commit_before>#include <stdlib.h>
#include "E_DBus.h"
#include "e_dbus_private.h"

typedef struct E_DBus_Pending_Call_Data E_DBus_Pending_Call_Data;
struct E_DBus_Pending_Call_Data
{
  E_DBus_Method_Return_Cb cb_return;
  void                   *data;
};

static void
cb_pending(DBusPendingCall *pending, void *user_data)
{
  DBusMessage *msg;
  DBusError err;
  E_DBus_Pending_Call_Data *data = user_data;

  if (!dbus_pending_call_get_completed(pending))
  {
    printf("NOT COMPLETED\n");
    return;
  }

  dbus_error_init(&err);
  msg = dbus_pending_call_steal_reply(pending);
  if (!msg)
  {
    if (data->cb_return)
    {
      dbus_set_error(&err, "org.enlightenment.DBus.NoReply", "There was no reply to this method call.");
      data->cb_return(data->data, NULL, &err);
      dbus_error_free(&err);
    }
    return;
  }

  if (dbus_set_error_from_message(&err, msg))
  {
    if (data->cb_return)
      data->cb_return(data->data, NULL, &err);
    dbus_error_free(&err);
  }
  else
  {
    if (data->cb_return)
      data->cb_return(data->data, msg, &err);
  }

  dbus_message_unref(msg);
  dbus_pending_call_unref(pending);
}


/**
 * @brief Send a DBus message with callbacks
 * @param conn The DBus connection
 * @param msg  The message to send
 * @param cb_return A callback function for returns (only used if @a msg is a method-call)
 * @param timeout   A timeout in milliseconds, after which a synthetic error will be generated
 * @return a DBusPendingCall that can be used to cancel the current call
 */
EAPI DBusPendingCall *
e_dbus_message_send(E_DBus_Connection *conn, DBusMessage *msg, E_DBus_Method_Return_Cb cb_return, int timeout, void *data)
{
  DBusPendingCall *pending;

  if (!dbus_connection_send_with_reply(conn->conn, msg, &pending, timeout))
    return NULL;

  if (cb_return && pending)
  {
    E_DBus_Pending_Call_Data *pdata;

    pdata = malloc(sizeof(E_DBus_Pending_Call_Data));
    pdata->cb_return = cb_return;
    pdata->data = data;

    if (!dbus_pending_call_set_notify(pending, cb_pending, pdata, free))
      free(pdata);
  }

  return pending;
}

static void
cb_method_call(void *data, DBusMessage *msg, DBusError *err)
{
  E_DBus_Callback *cb = data;<commit_msg>unref message when fail to set_notify or when not completed.<commit_after>#include <stdlib.h>
#include "E_DBus.h"
#include "e_dbus_private.h"

typedef struct E_DBus_Pending_Call_Data E_DBus_Pending_Call_Data;
struct E_DBus_Pending_Call_Data
{
  E_DBus_Method_Return_Cb cb_return;
  void                   *data;
};

static void
cb_pending(DBusPendingCall *pending, void *user_data)
{
  DBusMessage *msg;
  DBusError err;
  E_DBus_Pending_Call_Data *data = user_data;

  if (!dbus_pending_call_get_completed(pending))
  {
    printf("NOT COMPLETED\n");
    free(data);
    dbus_message_unref(msg);
    dbus_pending_call_unref(pending);
    return;
  }

  dbus_error_init(&err);
  msg = dbus_pending_call_steal_reply(pending);
  if (!msg)
  {
    if (data->cb_return)
    {
      dbus_set_error(&err, "org.enlightenment.DBus.NoReply", "There was no reply to this method call.");
      data->cb_return(data->data, NULL, &err);
      dbus_error_free(&err);
    }
    return;
  }

  if (dbus_set_error_from_message(&err, msg))
  {
    if (data->cb_return)
      data->cb_return(data->data, NULL, &err);
    dbus_error_free(&err);
  }
  else
  {
    if (data->cb_return)
      data->cb_return(data->data, msg, &err);
  }

  dbus_message_unref(msg);
  dbus_pending_call_unref(pending);
}


/**
 * @brief Send a DBus message with callbacks
 * @param conn The DBus connection
 * @param msg  The message to send
 * @param cb_return A callback function for returns (only used if @a msg is a method-call)
 * @param timeout   A timeout in milliseconds, after which a synthetic error will be generated
 * @return a DBusPendingCall that can be used to cancel the current call
 */
EAPI DBusPendingCall *
e_dbus_message_send(E_DBus_Connection *conn, DBusMessage *msg, E_DBus_Method_Return_Cb cb_return, int timeout, void *data)
{
  DBusPendingCall *pending;

  if (!dbus_connection_send_with_reply(conn->conn, msg, &pending, timeout))
    return NULL;

  if (cb_return && pending)
  {
    E_DBus_Pending_Call_Data *pdata;

    pdata = malloc(sizeof(E_DBus_Pending_Call_Data));
    pdata->cb_return = cb_return;
    pdata->data = data;

    if (!dbus_pending_call_set_notify(pending, cb_pending, pdata, free))
    {
      free(pdata);
      dbus_message_unref(msg);
      dbus_pending_call_cancel(pending);
      return NULL;
    }
  }

  return pending;
}

static void
cb_method_call(void *data, DBusMessage *msg, DBusError *err)
{
  E_DBus_Callback *cb = data;
{'fraction_non_alphanumeric': 0.10633727175080558, 'fraction_numerical': 0.0, 'mean_word_length': 4.028077753779698, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '4985737', 'n_tokens_mistral': 1715, 'n_tokens_neox': 1701, 'n_words': 381}