Blame telepathy-account-widgets/tp-account-widgets/tpaw-contactinfo-utils.c

Packit 79f644
/*
Packit 79f644
 * Copyright (C) 2007-2011 Collabora Ltd.
Packit 79f644
 *
Packit 79f644
 * This library is free software; you can redistribute it and/or
Packit 79f644
 * modify it under the terms of the GNU Lesser General Public
Packit 79f644
 * License as published by the Free Software Foundation; either
Packit 79f644
 * version 2.1 of the License, or (at your option) any later version.
Packit 79f644
 *
Packit 79f644
 * This library is distributed in the hope that it will be useful,
Packit 79f644
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 79f644
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 79f644
 * Lesser General Public License for more details.
Packit 79f644
 *
Packit 79f644
 * You should have received a copy of the GNU Lesser General Public
Packit 79f644
 * License along with this library; if not, write to the Free Software
Packit 79f644
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
Packit 79f644
 *
Packit 79f644
 * Authors: Xavier Claessens <xclaesse@gmail.com>
Packit 79f644
 *          Philip Withnall <philip.withnall@collabora.co.uk>
Packit 79f644
 *          Danielle Madeley <danielle.madeley@collabora.co.uk>
Packit 79f644
 */
Packit 79f644
Packit 79f644
#include "config.h"
Packit 79f644
#include "tpaw-contactinfo-utils.h"
Packit 79f644
Packit 79f644
#include <stdlib.h>
Packit 79f644
#include <glib/gi18n-lib.h>
Packit 79f644
Packit 79f644
#include "tpaw-time.h"
Packit 79f644
#include "tpaw-string-parser.h"
Packit 79f644
Packit 79f644
static gchar *
Packit 79f644
linkify_first_value (GStrv values)
Packit 79f644
{
Packit 79f644
  return tpaw_add_link_markup (values[0]);
Packit 79f644
}
Packit 79f644
Packit 79f644
static gchar *
Packit 79f644
format_idle_time (GStrv values)
Packit 79f644
{
Packit 79f644
  const gchar *value = values[0];
Packit 79f644
  int duration = strtol (value, NULL, 10);
Packit 79f644
Packit 79f644
  if (duration <= 0)
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  return tpaw_duration_to_string (duration);
Packit 79f644
}
Packit 79f644
Packit 79f644
static gchar *
Packit 79f644
format_server (GStrv values)
Packit 79f644
{
Packit 79f644
  g_assert (values[0] != NULL);
Packit 79f644
Packit 79f644
  if (values[1] == NULL)
Packit 79f644
    return g_markup_escape_text (values[0], -1);
Packit 79f644
  else
Packit 79f644
    return g_markup_printf_escaped ("%s (%s)", values[0], values[1]);
Packit 79f644
}
Packit 79f644
Packit 79f644
static gchar *
Packit 79f644
presence_hack (GStrv values)
Packit 79f644
{
Packit 79f644
  if (tp_str_empty (values[0]))
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  return g_markup_escape_text (values[0], -1);
Packit 79f644
}
Packit 79f644
Packit 79f644
typedef struct
Packit 79f644
{
Packit 79f644
  const gchar *field_name;
Packit 79f644
  const gchar *title;
Packit 79f644
  TpawContactInfoFormatFunc format;
Packit 79f644
} InfoFieldData;
Packit 79f644
Packit 79f644
/* keep this syncronised with info_field_data below */
Packit 79f644
static const char *info_field_names[] =
Packit 79f644
{
Packit 79f644
  "fn",
Packit 79f644
  "tel",
Packit 79f644
  "email",
Packit 79f644
  "url",
Packit 79f644
  "bday",
Packit 79f644
Packit 79f644
  "x-idle-time",
Packit 79f644
  "x-irc-server",
Packit 79f644
  "x-host",
Packit 79f644
Packit 79f644
  "x-presence-status-message",
Packit 79f644
Packit 79f644
  NULL
Packit 79f644
};
Packit 79f644
Packit 79f644
static InfoFieldData info_field_data[G_N_ELEMENTS (info_field_names)] =
Packit 79f644
{
Packit 79f644
  { "fn",    N_("Full Name"),      NULL },
Packit 79f644
  { "tel",   N_("Phone Number"),   NULL },
Packit 79f644
  { "email", N_("E-mail Address"), linkify_first_value },
Packit 79f644
  { "url",   N_("Website"),        linkify_first_value },
Packit 79f644
  { "bday",  N_("Birthday"),       NULL },
Packit 79f644
Packit 79f644
  /* Note to translators: this is the caption for a string of the form "5
Packit 79f644
   * minutes ago", and refers to the time since the contact last interacted
Packit 79f644
   * with their IM client. */
Packit 79f644
  { "x-idle-time",  N_("Last Seen:"),      format_idle_time },
Packit 79f644
  { "x-irc-server", N_("Server:"),         format_server },
Packit 79f644
  { "x-host",       N_("Connected From:"), format_server },
Packit 79f644
Packit 79f644
  /* FIXME: once Idle implements SimplePresence using this information, we can
Packit 79f644
   * and should bin this. */
Packit 79f644
  { "x-presence-status-message", N_("Away Message:"), presence_hack },
Packit 79f644
Packit 79f644
  { NULL, NULL }
Packit 79f644
};
Packit 79f644
Packit 79f644
typedef struct
Packit 79f644
{
Packit 79f644
  const gchar *type;
Packit 79f644
  const gchar *title;
Packit 79f644
} InfoParameterData;
Packit 79f644
Packit 79f644
static InfoParameterData info_parameter_data[] =
Packit 79f644
{
Packit 79f644
  { "work", N_("work") },
Packit 79f644
  { "home", N_("home") },
Packit 79f644
  { "cell", N_("mobile") },
Packit 79f644
  { "voice", N_("voice") },
Packit 79f644
  { "pref", N_("preferred") },
Packit 79f644
  { "postal", N_("postal") },
Packit 79f644
  { "parcel", N_("parcel") },
Packit 79f644
  { NULL, NULL }
Packit 79f644
};
Packit 79f644
Packit 79f644
const char **
Packit 79f644
tpaw_contact_info_get_field_names (guint *nnames)
Packit 79f644
{
Packit 79f644
  if (nnames != NULL)
Packit 79f644
    *nnames = G_N_ELEMENTS (info_field_names) - 1;
Packit 79f644
Packit 79f644
  return info_field_names;
Packit 79f644
}
Packit 79f644
Packit 79f644
gboolean
Packit 79f644
tpaw_contact_info_lookup_field (const gchar *field_name,
Packit 79f644
    const gchar **title,
Packit 79f644
    TpawContactInfoFormatFunc *format)
Packit 79f644
{
Packit 79f644
  guint i;
Packit 79f644
Packit 79f644
  for (i = 0; info_field_data[i].field_name != NULL; i++)
Packit 79f644
    {
Packit 79f644
      if (tp_strdiff (info_field_data[i].field_name, field_name) == FALSE)
Packit 79f644
        {
Packit 79f644
          if (title != NULL)
Packit 79f644
            *title = gettext (info_field_data[i].title);
Packit 79f644
Packit 79f644
          if (format != NULL)
Packit 79f644
            *format = info_field_data[i].format;
Packit 79f644
Packit 79f644
          return TRUE;
Packit 79f644
        }
Packit 79f644
    }
Packit 79f644
Packit 79f644
  return FALSE;
Packit 79f644
}
Packit 79f644
Packit 79f644
static char *
Packit 79f644
build_parameters_string (GStrv parameters)
Packit 79f644
{
Packit 79f644
  GPtrArray *output = g_ptr_array_new ();
Packit 79f644
  char *join;
Packit 79f644
  GStrv iter;
Packit 79f644
Packit 79f644
  for (iter = parameters; iter != NULL && *iter != NULL; iter++)
Packit 79f644
    {
Packit 79f644
      static const char *prefix = "type=";
Packit 79f644
      const char *param = *iter;
Packit 79f644
      InfoParameterData *iter2;
Packit 79f644
Packit 79f644
      if (!g_str_has_prefix (param, prefix))
Packit 79f644
        continue;
Packit 79f644
Packit 79f644
      param += strlen (prefix);
Packit 79f644
Packit 79f644
      for (iter2 = info_parameter_data; iter2->type != NULL; iter2++)
Packit 79f644
        {
Packit 79f644
          if (!tp_strdiff (iter2->type, param))
Packit 79f644
            {
Packit 79f644
              g_ptr_array_add (output, gettext (iter2->title));
Packit 79f644
              break;
Packit 79f644
            }
Packit 79f644
        }
Packit 79f644
    }
Packit 79f644
Packit 79f644
  if (output->len == 0)
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  g_ptr_array_add (output, NULL); /* NULL-terminate */
Packit 79f644
Packit 79f644
  join = g_strjoinv (", ", (char **) output->pdata);
Packit 79f644
  g_ptr_array_unref (output);
Packit 79f644
Packit 79f644
  return join;
Packit 79f644
}
Packit 79f644
Packit 79f644
char *
Packit 79f644
tpaw_contact_info_field_label (const char *field_name,
Packit 79f644
    GStrv parameters,
Packit 79f644
    gboolean show_parameters)
Packit 79f644
{
Packit 79f644
  char *ret;
Packit 79f644
  const char *title;
Packit 79f644
  char *join = NULL;
Packit 79f644
Packit 79f644
  if (!tpaw_contact_info_lookup_field (field_name, &title, NULL))
Packit 79f644
    return NULL;
Packit 79f644
Packit 79f644
  if (show_parameters)
Packit 79f644
    join = build_parameters_string (parameters);
Packit 79f644
Packit 79f644
  if (join != NULL)
Packit 79f644
    ret = g_strdup_printf ("%s (%s)", title, join);
Packit 79f644
  else
Packit 79f644
    ret = g_strdup_printf ("%s", title);
Packit 79f644
Packit 79f644
  g_free (join);
Packit 79f644
Packit 79f644
  return ret;
Packit 79f644
}
Packit 79f644
Packit 79f644
static gint
Packit 79f644
contact_info_field_name_cmp (const gchar *name1,
Packit 79f644
    const gchar *name2)
Packit 79f644
{
Packit 79f644
  guint i;
Packit 79f644
Packit 79f644
  if (tp_strdiff (name1, name2) == FALSE)
Packit 79f644
    return 0;
Packit 79f644
Packit 79f644
  /* We use the order of info_field_data */
Packit 79f644
  for (i = 0; info_field_data[i].field_name != NULL; i++)
Packit 79f644
    {
Packit 79f644
      if (tp_strdiff (info_field_data[i].field_name, name1) == FALSE)
Packit 79f644
        return -1;
Packit 79f644
      if (tp_strdiff (info_field_data[i].field_name, name2) == FALSE)
Packit 79f644
        return +1;
Packit 79f644
    }
Packit 79f644
Packit 79f644
  return g_strcmp0 (name1, name2);
Packit 79f644
}
Packit 79f644
Packit 79f644
gint
Packit 79f644
tpaw_contact_info_field_cmp (TpContactInfoField *field1,
Packit 79f644
    TpContactInfoField *field2)
Packit 79f644
{
Packit 79f644
  return contact_info_field_name_cmp (field1->field_name, field2->field_name);
Packit 79f644
}
Packit 79f644
Packit 79f644
gint
Packit 79f644
tpaw_contact_info_field_spec_cmp (TpContactInfoFieldSpec *spec1,
Packit 79f644
    TpContactInfoFieldSpec *spec2)
Packit 79f644
{
Packit 79f644
    return contact_info_field_name_cmp (spec1->name, spec2->name);
Packit 79f644
}
Packit 79f644