<commit_before>/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/*
 * Copyright (C) 2007-2009 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
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 */

#include <config.h>

#include <string.h>

#include <telepathy-glib/util.h>

#include <gtk/gtk.h>

#include <libempathy/empathy-utils.h>

#include "empathy-protocol-chooser.h"
#include "empathy-ui-utils.h"

#define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
#include <libempathy/empathy-debug.h>

/**
 * SECTION:empathy-protocol-chooser
 * @title: EmpathyProtocolChooser
 * @short_description: A widget used to choose from a list of protocols
 * @include: libempathy-gtk/empathy-protocol-chooser.h
 *
 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
 * chooser of available protocols.
 */

/**
 * EmpathyProtocolChooser:
 * @parent: parent object
 *
 * Widget which extends #GtkComboBox to provide a chooser of available
 * protocols.
 */

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
typedef struct
{
  GtkListStore *store;
  gboolean dispose_run;

} EmpathyProtocolChooserPriv;

enum
{
  COL_ICON,
  COL_LABEL,
  COL_CM,
  COL_PROTOCOL,
  COL_COUNT
};

G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
    GTK_TYPE_COMBO_BOX);

static gint
protocol_chooser_sort_protocol_value (TpConnectionManagerProtocol *protocol)
{
  guint i;
  const gchar *names[] = {
    "jabber",
    "salut",
    "gtalk",
    NULL
  };

  for (i = 0 ; names[i]; i++)
    {
      if (strcmp (protocol->name, names[i]) == 0)
        return i;
    }

  return i;
}

static gint
protocol_chooser_sort_func (GtkTreeModel *model,
    GtkTreeIter  *iter_a,
    GtkTreeIter  *iter_b,
    gpointer      user_data)
{
  TpConnectionManagerProtocol *protocol_a;
  TpConnectionManagerProtocol *protocol_b;
  gint cmp;

  gtk_tree_model_get (model, iter_a,
      COL_PROTOCOL, &protocol_a,
      -1);
  gtk_tree_model_get (model, iter_b,
      COL_PROTOCOL, &protocol_b,
      -1);

  cmp = protocol_chooser_sort_protocol_value (protocol_a);
  cmp -= protocol_chooser_sort_protocol_value (protocol_b);
  if (cmp == 0)
    {
      cmp = strcmp (protocol_a->name, protocol_b->name);
    }

  return cmp;
}

static void
protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
    TpConnectionManager *cm)
{
  EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
  const TpConnectionManagerProtocol * const *iter;

  for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
    {
      const TpConnectionManagerProtocol *proto = *iter;
      gchar *icon_name;
      gchar *display_name;


      icon_name = g_strdup_printf ("im-%s", proto->name);

      if (!tp_strdiff (cm->name, "haze") && !tp_strdiff (proto->name, "msn"))
        display_name = g_strdup_printf ("msn (Haze)");
      else
        display_name = g_strdup (proto->name);

      gtk_list_store_insert_with_values (priv->store, NULL, 0,
          COL_ICON, icon_name,
          COL_LABEL, display_name,
          COL_CM, cm,
          COL_PROTOCOL, proto,
          -1);

      g_free (display_name);
      g_free (icon_name);
    }
}


static void
protocol_choosers_cms_listed (TpConnectionManager * const *cms,
    gsize n_cms,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpConnectionManager * const *iter;

  if (error !=NULL)
    {
      DEBUG ("Failed to get connection managers: %s", error->message);<commit_msg>Put (Haze) behind all protocols coming from haze<commit_after>/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
/*
 * Copyright (C) 2007-2009 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
 *
 * Authors: <NAME> <<EMAIL>>
 *          <NAME> <<EMAIL>>
 */

#include <config.h>

#include <string.h>

#include <telepathy-glib/util.h>

#include <gtk/gtk.h>

#include <libempathy/empathy-utils.h>

#include "empathy-protocol-chooser.h"
#include "empathy-ui-utils.h"

#define DEBUG_FLAG EMPATHY_DEBUG_ACCOUNT
#include <libempathy/empathy-debug.h>

/**
 * SECTION:empathy-protocol-chooser
 * @title: EmpathyProtocolChooser
 * @short_description: A widget used to choose from a list of protocols
 * @include: libempathy-gtk/empathy-protocol-chooser.h
 *
 * #EmpathyProtocolChooser is a widget which extends #GtkComboBox to provides a
 * chooser of available protocols.
 */

/**
 * EmpathyProtocolChooser:
 * @parent: parent object
 *
 * Widget which extends #GtkComboBox to provide a chooser of available
 * protocols.
 */

#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyProtocolChooser)
typedef struct
{
  GtkListStore *store;
  gboolean dispose_run;

} EmpathyProtocolChooserPriv;

enum
{
  COL_ICON,
  COL_LABEL,
  COL_CM,
  COL_PROTOCOL,
  COL_COUNT
};

G_DEFINE_TYPE (EmpathyProtocolChooser, empathy_protocol_chooser,
    GTK_TYPE_COMBO_BOX);

static gint
protocol_chooser_sort_protocol_value (TpConnectionManagerProtocol *protocol)
{
  guint i;
  const gchar *names[] = {
    "jabber",
    "salut",
    "gtalk",
    NULL
  };

  for (i = 0 ; names[i]; i++)
    {
      if (strcmp (protocol->name, names[i]) == 0)
        return i;
    }

  return i;
}

static gint
protocol_chooser_sort_func (GtkTreeModel *model,
    GtkTreeIter  *iter_a,
    GtkTreeIter  *iter_b,
    gpointer      user_data)
{
  TpConnectionManagerProtocol *protocol_a;
  TpConnectionManagerProtocol *protocol_b;
  gint cmp;

  gtk_tree_model_get (model, iter_a,
      COL_PROTOCOL, &protocol_a,
      -1);
  gtk_tree_model_get (model, iter_b,
      COL_PROTOCOL, &protocol_b,
      -1);

  cmp = protocol_chooser_sort_protocol_value (protocol_a);
  cmp -= protocol_chooser_sort_protocol_value (protocol_b);
  if (cmp == 0)
    {
      cmp = strcmp (protocol_a->name, protocol_b->name);
    }

  return cmp;
}

static void
protocol_choosers_add_cm (EmpathyProtocolChooser *chooser,
    TpConnectionManager *cm)
{
  EmpathyProtocolChooserPriv *priv = GET_PRIV (chooser);
  const TpConnectionManagerProtocol * const *iter;

  for (iter = cm->protocols; iter != NULL && *iter != NULL; iter++)
    {
      const TpConnectionManagerProtocol *proto = *iter;
      gchar *icon_name;
      gchar *display_name;


      icon_name = g_strdup_printf ("im-%s", proto->name);

      if (!tp_strdiff (cm->name, "haze"))
        display_name = g_strdup_printf ("%s (Haze)", proto->name);
      else
        display_name = g_strdup (proto->name);

      gtk_list_store_insert_with_values (priv->store, NULL, 0,
          COL_ICON, icon_name,
          COL_LABEL, display_name,
          COL_CM, cm,
          COL_PROTOCOL, proto,
          -1);

      g_free (display_name);
      g_free (icon_name);
    }
}


static void
protocol_choosers_cms_listed (TpConnectionManager * const *cms,
    gsize n_cms,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpConnectionManager * const *iter;

  if (error !=NULL)
    {
      DEBUG ("Failed to get connection managers: %s", error->message);
{'fraction_non_alphanumeric': 0.1042236763831053, 'fraction_numerical': 0.007138607971445568, 'mean_word_length': 3.5511640498105037, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 47, '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': '920484', 'n_tokens_mistral': 2920, 'n_tokens_neox': 2783, 'n_words': 782}