<commit_before>#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include "E_Connman.h"

#define CONNMAN_BUS_NAME "net.connman"
#define CONNMAN_MANAGER_IFACE CONNMAN_BUS_NAME ".Manager"

static struct
{
   E_DBus_Signal_Handler *name_owner_changed;
   E_DBus_Signal_Handler *services_changed;
   E_DBus_Signal_Handler *prop_changed;
} handlers;

static struct
{
   DBusPendingCall *get_name_owner;
} pending;

static unsigned int init_count;
static E_DBus_Connection *conn;

EAPI int E_CONNMAN_EVENT_MANAGER_IN;
EAPI int E_CONNMAN_EVENT_MANAGER_OUT;

static inline void
_e_connman_system_name_owner_exit(void)
{
   ecore_event_add(E_CONNMAN_EVENT_MANAGER_OUT, NULL, NULL, NULL);
}

static void _manager_services_changed(void *data __UNUSED__, DBusMessage *msg)
{
}

static void _manager_prop_changed(void *data __UNUSED__, DBusMessage *msg)
{
}

static inline void
_e_connman_system_name_owner_enter(void)
{
   if (!handlers.prop_changed)
     handlers.prop_changed = e_dbus_signal_handler_add(conn, CONNMAN_BUS_NAME,
                                 "/", CONNMAN_MANAGER_IFACE, "PropertyChanged",
                                 _manager_prop_changed, NULL);

   if (!handlers.services_changed)
     handlers.services_changed = e_dbus_signal_handler_add(conn, CONNMAN_BUS_NAME,
                                 "/", CONNMAN_MANAGER_IFACE, "ServicesChanged",
                                 _manager_services_changed, NULL);

   ecore_event_add(E_CONNMAN_EVENT_MANAGER_IN, NULL, NULL, NULL);
}

static void
_e_connman_system_name_owner_changed(void *data __UNUSED__, DBusMessage *msg)
{
   const char *name, *from, *to;
   DBusError err;

   dbus_error_init(&err);
   if (!dbus_message_get_args(msg, &err,
                              DBUS_TYPE_STRING, &name,
                              DBUS_TYPE_STRING, &from,
                              DBUS_TYPE_STRING, &to,
                              DBUS_TYPE_INVALID))
     {
        ERR("could not get NameOwnerChanged arguments: %s: %s",
            err.name, err.message);
        dbus_error_free(&err);
        return;
     }

   if (strcmp(name, CONNMAN_BUS_NAME) != 0)
     return;

   DBG("NameOwnerChanged %s from=[%s] to=[%s]", name, from, to);

   if (from[0] == '\0' && to[0] != '\0')
     _e_connman_system_name_owner_enter();
   else if (from[0] != '\0' && to[0] == '\0')
     _e_connman_system_name_owner_exit();
   else
     ERR("unknow change from %s to %s", from, to);
}

static void
_e_connman_get_name_owner(void *data __UNUSED__, DBusMessage *msg, DBusError *err)
{
   pending.get_name_owner = NULL;
   DBG("get_name_owner msg=%p", msg);

   if (dbus_error_is_set(err))
     {
        if (!strcmp(err->name, DBUS_ERROR_NAME_HAS_NO_OWNER))
          ERR("could not get bus name owner: %s %s", err->name, err->message);
        return;
     }

   _e_connman_system_name_owner_enter();
}

/**
 * Initialize E Connection Manager (E_Connman) system.
 *
 * This will connect to ConnMan through DBus and watch for it going in and out.
 *
 * Interesting events are:
 *   - E_CONNMAN_EVENT_MANAGER_IN: issued when connman is avaiable.
 *   - E_CONNMAN_EVENT_MANAGER_OUT: issued when connman connection is lost.
 */
unsigned int
e_connman_system_init(E_DBus_Connection *edbus_conn)
{
   init_count++;

   if (init_count > 1)
      return init_count;

   E_CONNMAN_EVENT_MANAGER_IN = ecore_event_type_new();
   E_CONNMAN_EVENT_MANAGER_OUT = ecore_event_type_new();
<commit_msg>e/connman: monitor owner instead of bus names<commit_after>#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>

#include "E_Connman.h"

#define CONNMAN_BUS_NAME "net.connman"
#define CONNMAN_MANAGER_IFACE CONNMAN_BUS_NAME ".Manager"

static struct
{
   E_DBus_Signal_Handler *name_owner_changed;
   E_DBus_Signal_Handler *services_changed;
   E_DBus_Signal_Handler *prop_changed;
} handlers;

static struct
{
   DBusPendingCall *get_name_owner;
} pending;

static unsigned int init_count;
static E_DBus_Connection *conn;
static char *bus_owner;

EAPI int E_CONNMAN_EVENT_MANAGER_IN;
EAPI int E_CONNMAN_EVENT_MANAGER_OUT;

static inline void
_e_connman_system_name_owner_exit(void)
{
   e_dbus_signal_handler_del(conn, handlers.services_changed);
   handlers.services_changed = NULL;
   e_dbus_signal_handler_del(conn, handlers.prop_changed);
   handlers.prop_changed = NULL;

   free(bus_owner);
   bus_owner = NULL;

   ecore_event_add(E_CONNMAN_EVENT_MANAGER_OUT, NULL, NULL, NULL);
}

static void _manager_services_changed(void *data __UNUSED__, DBusMessage *msg)
{
}

static void _manager_prop_changed(void *data __UNUSED__, DBusMessage *msg)
{
}

static inline void
_e_connman_system_name_owner_enter(const char *owner)
{
   free(bus_owner);
   bus_owner = strdup(owner);

   handlers.prop_changed = e_dbus_signal_handler_add(conn, bus_owner,
                                 "/", CONNMAN_MANAGER_IFACE, "PropertyChanged",
                                 _manager_prop_changed, NULL);

   handlers.services_changed = e_dbus_signal_handler_add(conn, bus_owner,
                                 "/", CONNMAN_MANAGER_IFACE, "ServicesChanged",
                                 _manager_services_changed, NULL);

   ecore_event_add(E_CONNMAN_EVENT_MANAGER_IN, NULL, NULL, NULL);
}

static void
_e_connman_system_name_owner_changed(void *data __UNUSED__, DBusMessage *msg)
{
   const char *name, *from, *to;
   DBusError err;

   dbus_error_init(&err);
   if (!dbus_message_get_args(msg, &err,
                              DBUS_TYPE_STRING, &name,
                              DBUS_TYPE_STRING, &from,
                              DBUS_TYPE_STRING, &to,
                              DBUS_TYPE_INVALID))
     {
        ERR("could not get NameOwnerChanged arguments: %s: %s",
            err.name, err.message);
        dbus_error_free(&err);
        return;
     }

   if (strcmp(name, CONNMAN_BUS_NAME) != 0)
     return;

   DBG("NameOwnerChanged %s from=[%s] to=[%s]", name, from, to);

   if (from[0] == '\0' && to[0] != '\0')
     _e_connman_system_name_owner_enter(to);
   else if (from[0] != '\0' && to[0] == '\0')
     _e_connman_system_name_owner_exit();
   else
     ERR("unknow change from %s to %s", from, to);
}

static void
_e_connman_get_name_owner(void *data __UNUSED__, DBusMessage *msg, DBusError *err)
{
   const char *owner;

   pending.get_name_owner = NULL;
   DBG("get_name_owner msg=%p", msg);

   if (dbus_error_is_set(err))
     {
        if (!strcmp(err->name, DBUS_ERROR_NAME_HAS_NO_OWNER))
          ERR("could not get bus name owner: %s %s", err->name, err->message);
        return;
     }

   if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &owner,
                              DBUS_TYPE_INVALID))
     {
        ERR("Could not get name owner");
        return;
     }

   _e_connman_system_name_owner_enter(owner);
}

/**
 * Initialize E Connection Manager (E_Connman) system.
 *
 * This will connect to ConnMan through DBus and watch for it going in and out.
 *
 * Interesting events are:
 *   - E_CONNMAN_EVENT_MANAGER_IN: issued when connman is avaiable.
 *   - E_CONNMAN_EVENT_MANAGER_OUT: issued when connman connection is lost.
 */
unsigned int
e_connman_system_init(E_DBus_Connection *edbus_conn)
{
   init_count++;

   if (init_count > 1)
      return init_count;

   E_CONNMAN_EVENT_MANAGER_IN = ecore_event_type_new();
   E_CONNMAN_EVENT_MANAGER_OUT = ecore_event_type_new();

{'fraction_non_alphanumeric': 0.09924791834542036, 'fraction_numerical': 0.0026860059092130005, 'mean_word_length': 3.2971725331794577, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6489075', 'n_tokens_mistral': 2656, 'n_tokens_neox': 2626, 'n_words': 526}