<commit_before>/*
 * avahi-avahi_olpc-activity-manager.c - Source for
 * SalutAvahiOlpcActivityManager
 * Copyright (C) 2008 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <avahi-gobject/ga-service-browser.h>

#include "avahi-olpc-activity-manager.h"
#include "avahi-olpc-activity.h"
#include "avahi-discovery-client.h"
#include "olpc-activity.h"

#define DEBUG_FLAG DEBUG_OLPC_ACTIVITY
#include "debug.h"

G_DEFINE_TYPE (SalutAvahiOlpcActivityManager, salut_avahi_olpc_activity_manager,
    SALUT_TYPE_OLPC_ACTIVITY_MANAGER);

/* properties */
enum {
  PROP_CLIENT = 1,
  LAST_PROP
};

/* private structure */
typedef struct _SalutAvahiOlpcActivityManagerPrivate SalutAvahiOlpcActivityManagerPrivate;

struct _SalutAvahiOlpcActivityManagerPrivate
{
  SalutAvahiDiscoveryClient *discovery_client;
  GaServiceBrowser *browser;

  gboolean dispose_has_run;
};

#define SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), SALUT_TYPE_AVAHI_OLPC_ACTIVITY_MANAGER, SalutAvahiOlpcActivityManagerPrivate))

static void
salut_avahi_olpc_activity_manager_init (SalutAvahiOlpcActivityManager *self)
{
  SalutAvahiOlpcActivityManagerPrivate *priv =
    SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE (self);

  priv->browser = ga_service_browser_new (SALUT_DNSSD_OLPC_ACTIVITY);
}

static void
salut_avahi_olpc_activity_manager_get_property (GObject *object,
                                                guint property_id,
                                                GValue *value,
                                                GParamSpec *pspec)
{
  SalutAvahiOlpcActivityManager *self = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER (object);
  SalutAvahiOlpcActivityManagerPrivate *priv = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE (self);

  switch (property_id)
    {
      case PROP_CLIENT:
        g_value_set_object (value, priv->discovery_client);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
salut_avahi_olpc_activity_manager_set_property (GObject *object,
                                                guint property_id,
                                                const GValue *value,
                                                GParamSpec *pspec)
{
  SalutAvahiOlpcActivityManager *self = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER (object);
  SalutAvahiOlpcActivityManagerPrivate *priv = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE (self);

  switch (property_id)
    {
      case PROP_CLIENT:
        priv->discovery_client = g_value_get_object (value);
        g_object_ref (priv->discovery_client);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static gboolean
split_activity_name (const gchar *name,
                     gchar **room_name,
                     gchar **contact_name)
{
  gchar **tmp;

  tmp = g_strsplit (name, ":", 2);
  if (tmp[0] == NULL || tmp[1] == NULL)
    {
      DEBUG ("Ignoring invalid OLPC activity DNS-SD with no ':': %s", name);
      return FALSE;
    }

  if (room_name != NULL)
    *room_name = g_strdup (tmp[0]);
  if (contact_name != NULL)
  *contact_name = g_strdup (tmp[1]);

  g_strfreev (tmp);
  return TRUE;
}

static void
browser_found (GaServiceBrowser *browser,
               AvahiIfIndex interface,
               AvahiProtocol protocol,
               const char *name,
               const char *type,
               const char *domain,
               GaLookupResultFlags flags,
               SalutAvahiOlpcActivityManager *self)
{
  SalutOlpcActivityManager *mgr = SALUT_OLPC_ACTIVITY_MANAGER (self);
  SalutOlpcActivity *activity;
  gchar *room_name = NULL;
  gchar *contact_name = NULL;
  TpHandleRepoIface *room_repo = tp_base_connection_get_handles
    ((TpBaseConnection *) mgr->connection, TP_HANDLE_TYPE_ROOM);
  TpHandle room;
  GError *error = NULL;
  SalutContactManager *contact_manager;
  SalutContact *contact;

  if (flags & AVAHI_LOOKUP_RESULT_OUR_OWN)
    return;

  if (!split_activity_name (name, &room_name, &contact_name))
    return;

  room = tp_handle_ensure (room_repo, room_name, NULL, &error);
  if (room == 0)
    {
      DEBUG ("invalid room name %s: %s", room_name, error->message);
      g_free (room_name);
      g_free (contact_name);
      return;
    }
<commit_msg>avahi-olpc-activity-manager: ignore browser-found callbacks if disconnected<commit_after>/*
 * avahi-avahi_olpc-activity-manager.c - Source for
 * SalutAvahiOlpcActivityManager
 * Copyright (C) 2008 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <dbus/dbus-glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <avahi-gobject/ga-service-browser.h>

#include "avahi-olpc-activity-manager.h"
#include "avahi-olpc-activity.h"
#include "avahi-discovery-client.h"
#include "olpc-activity.h"

#define DEBUG_FLAG DEBUG_OLPC_ACTIVITY
#include "debug.h"

G_DEFINE_TYPE (SalutAvahiOlpcActivityManager, salut_avahi_olpc_activity_manager,
    SALUT_TYPE_OLPC_ACTIVITY_MANAGER);

/* properties */
enum {
  PROP_CLIENT = 1,
  LAST_PROP
};

/* private structure */
typedef struct _SalutAvahiOlpcActivityManagerPrivate SalutAvahiOlpcActivityManagerPrivate;

struct _SalutAvahiOlpcActivityManagerPrivate
{
  SalutAvahiDiscoveryClient *discovery_client;
  GaServiceBrowser *browser;

  gboolean dispose_has_run;
};

#define SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), SALUT_TYPE_AVAHI_OLPC_ACTIVITY_MANAGER, SalutAvahiOlpcActivityManagerPrivate))

static void
salut_avahi_olpc_activity_manager_init (SalutAvahiOlpcActivityManager *self)
{
  SalutAvahiOlpcActivityManagerPrivate *priv =
    SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE (self);

  priv->browser = ga_service_browser_new (SALUT_DNSSD_OLPC_ACTIVITY);
}

static void
salut_avahi_olpc_activity_manager_get_property (GObject *object,
                                                guint property_id,
                                                GValue *value,
                                                GParamSpec *pspec)
{
  SalutAvahiOlpcActivityManager *self = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER (object);
  SalutAvahiOlpcActivityManagerPrivate *priv = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE (self);

  switch (property_id)
    {
      case PROP_CLIENT:
        g_value_set_object (value, priv->discovery_client);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
salut_avahi_olpc_activity_manager_set_property (GObject *object,
                                                guint property_id,
                                                const GValue *value,
                                                GParamSpec *pspec)
{
  SalutAvahiOlpcActivityManager *self = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER (object);
  SalutAvahiOlpcActivityManagerPrivate *priv = SALUT_AVAHI_OLPC_ACTIVITY_MANAGER_GET_PRIVATE (self);

  switch (property_id)
    {
      case PROP_CLIENT:
        priv->discovery_client = g_value_get_object (value);
        g_object_ref (priv->discovery_client);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static gboolean
split_activity_name (const gchar *name,
                     gchar **room_name,
                     gchar **contact_name)
{
  gchar **tmp;

  tmp = g_strsplit (name, ":", 2);
  if (tmp[0] == NULL || tmp[1] == NULL)
    {
      DEBUG ("Ignoring invalid OLPC activity DNS-SD with no ':': %s", name);
      return FALSE;
    }

  if (room_name != NULL)
    *room_name = g_strdup (tmp[0]);
  if (contact_name != NULL)
  *contact_name = g_strdup (tmp[1]);

  g_strfreev (tmp);
  return TRUE;
}

static void
browser_found (GaServiceBrowser *browser,
               AvahiIfIndex interface,
               AvahiProtocol protocol,
               const char *name,
               const char *type,
               const char *domain,
               GaLookupResultFlags flags,
               SalutAvahiOlpcActivityManager *self)
{
  SalutOlpcActivityManager *mgr = SALUT_OLPC_ACTIVITY_MANAGER (self);
  SalutOlpcActivity *activity;
  gchar *room_name = NULL;
  gchar *contact_name = NULL;
  TpBaseConnection *base_conn = (TpBaseConnection *) mgr->connection;
  TpHandleRepoIface *room_repo = tp_base_connection_get_handles (
      base_conn, TP_HANDLE_TYPE_ROOM);
  TpHandle room;
  GError *error = NULL;
  SalutContactManager *contact_manager;
  SalutContact *contact;

  if (flags & AVAHI_LOOKUP_RESULT_OUR_OWN)
    return;

  if (base_conn->status == TP_CONNECTION_STATUS_DISCONNECTED)
    return;

  if (!split_activity_name (name, &room_name, &contact_name))
    return;

  room = tp_handle_ensure (room_repo, room_name, NULL, &error);
  if (room == 0)
    {
      DEBUG ("invalid room name %s: %s", room_name, error->message);
      g_free (room_name);
      g_free (contact_name);
      return;
    }

{'fraction_non_alphanumeric': 0.08022405772334568, 'fraction_numerical': 0.004557106237539163, 'mean_word_length': 3.160347551342812, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1863260', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3324, 'n_words': 790}