empathy-individual-dialogs.c 8.53 KB
Newer Older
Travis Reitter's avatar
Travis Reitter committed
1 2
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
3
 * Copyright (C) 2007-2010 Collabora Ltd.
Travis Reitter's avatar
Travis Reitter committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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: Xavier Claessens <xclaesse@gmail.com>
 */

22
#include "config.h"
23
#include "empathy-individual-dialogs.h"
Travis Reitter's avatar
Travis Reitter committed
24 25

#include <glib/gi18n-lib.h>
26
#include <telepathy-glib/telepathy-glib-dbus.h>
Travis Reitter's avatar
Travis Reitter committed
27

28
#include "empathy-contact-widget.h"
29 30
#include "empathy-individual-manager.h"
#include "empathy-utils.h"
Travis Reitter's avatar
Travis Reitter committed
31

32 33
#define BULLET_POINT "\342\200\242"

Travis Reitter's avatar
Travis Reitter committed
34 35 36 37 38 39
static GtkWidget *new_individual_dialog = NULL;

/*
 *  New contact dialog
 */

40
static void
Travis Reitter's avatar
Travis Reitter committed
41
can_add_contact_to_account (TpAccount *account,
42 43
    EmpathyAccountChooserFilterResultCallback callback,
    gpointer callback_data,
Travis Reitter's avatar
Travis Reitter committed
44 45 46 47 48 49 50 51
    gpointer user_data)
{
  EmpathyIndividualManager *individual_manager;
  TpConnection *connection;
  gboolean result;

  connection = tp_account_get_connection (account);
  if (connection == NULL)
52 53 54 55
    {
      callback (FALSE, callback_data);
      return;
    }
Travis Reitter's avatar
Travis Reitter committed
56 57

  individual_manager = empathy_individual_manager_dup_singleton ();
58
  result = empathy_connection_can_add_personas (connection);
Travis Reitter's avatar
Travis Reitter committed
59 60
  g_object_unref (individual_manager);

61
  callback (result, callback_data);
Travis Reitter's avatar
Travis Reitter committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
}

static void
new_individual_response_cb (GtkDialog *dialog,
    gint response,
    GtkWidget *contact_widget)
{
  EmpathyIndividualManager *individual_manager;
  EmpathyContact *contact;

  individual_manager = empathy_individual_manager_dup_singleton ();
  contact = empathy_contact_widget_get_contact (contact_widget);

  if (contact && response == GTK_RESPONSE_OK)
    empathy_individual_manager_add_from_contact (individual_manager, contact);

  new_individual_dialog = NULL;
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (individual_manager);
}

void
empathy_new_individual_dialog_show (GtkWindow *parent)
{
  empathy_new_individual_dialog_show_with_individual (parent, NULL);
}

void
empathy_new_individual_dialog_show_with_individual (GtkWindow *parent,
    FolksIndividual *individual)
{
  GtkWidget *dialog;
  GtkWidget *button;
  EmpathyContact *contact = NULL;
  GtkWidget *contact_widget;

  g_return_if_fail (individual == NULL || FOLKS_IS_INDIVIDUAL (individual));

  if (new_individual_dialog)
    {
      gtk_window_present (GTK_WINDOW (new_individual_dialog));
      return;
    }

  /* Create dialog */
  dialog = gtk_dialog_new ();
  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
109
  gtk_window_set_title (GTK_WINDOW (dialog), _("New Contact"));
Travis Reitter's avatar
Travis Reitter committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

  /* Cancel button */
  button = gtk_button_new_with_label (GTK_STOCK_CANCEL);
  gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button,
      GTK_RESPONSE_CANCEL);
  gtk_widget_show (button);

  /* Add button */
  button = gtk_button_new_with_label (GTK_STOCK_ADD);
  gtk_button_set_use_stock (GTK_BUTTON (button), TRUE);
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, GTK_RESPONSE_OK);
  gtk_widget_show (button);

  /* Contact info widget */
125
  if (individual != NULL)
126
    contact = empathy_contact_dup_from_folks_individual (individual);
Travis Reitter's avatar
Travis Reitter committed
127

128
  contact_widget = empathy_contact_widget_new (contact);
Travis Reitter's avatar
Travis Reitter committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  gtk_container_set_border_width (GTK_CONTAINER (contact_widget), 8);
  gtk_box_pack_start (
      GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
      contact_widget, TRUE, TRUE, 0);
  empathy_contact_widget_set_account_filter (contact_widget,
      can_add_contact_to_account, NULL);
  gtk_widget_show (contact_widget);

  new_individual_dialog = dialog;

  g_signal_connect (dialog, "response", G_CALLBACK (new_individual_response_cb),
      contact_widget);

  if (parent != NULL)
    gtk_window_set_transient_for (GTK_WINDOW (dialog), parent);

  gtk_widget_show (dialog);
146 147

  tp_clear_object (&contact);
Travis Reitter's avatar
Travis Reitter committed
148
}
149

150 151 152 153 154 155 156 157 158 159 160 161
static char *
contact_pretty_name (TpContact *contact)
{
  const char *alias = tp_contact_get_alias (contact);
  const char *identifier = tp_contact_get_identifier (contact);

  if (tp_strdiff (alias, identifier))
    return g_strdup_printf ("%s (%s)", alias, identifier);
  else
    return g_strdup (alias);
}

162 163 164 165 166 167
/*
 * Block contact dialog
 */
gboolean
empathy_block_individual_dialog_show (GtkWindow *parent,
    FolksIndividual *individual,
168
    GdkPixbuf *avatar,
169 170 171 172
    gboolean *abusive)
{
  GtkWidget *dialog;
  GtkWidget *abusive_check = NULL;
173 174
  GeeSet *personas;
  GeeIterator *iter;
175 176 177 178
  GString *text = g_string_new ("");
  GString *blocked_str = g_string_new ("");
  GString *notblocked_str = g_string_new ("");
  guint npersonas_blocked = 0, npersonas_notblocked = 0;
179 180 181 182 183 184
  gboolean can_report_abuse = FALSE;
  int res;

  dialog = gtk_message_dialog_new (parent,
      GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
      _("Block %s?"),
185
      folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
186

187 188 189 190 191 192 193
  if (avatar != NULL)
    {
      GtkWidget *image = gtk_image_new_from_pixbuf (avatar);
      gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
      gtk_widget_show (image);
    }

194 195
  /* build a list of personas that support blocking */
  personas = folks_individual_get_personas (individual);
196 197
  iter = gee_iterable_iterator (GEE_ITERABLE (personas));
  while (gee_iterator_next (iter))
198
    {
199
      TpfPersona *persona = gee_iterator_get (iter);
200
      TpContact *contact;
201 202
      GString *s;
      char *str;
203
      TpConnection *conn;
204 205

      if (!TPF_IS_PERSONA (persona))
206
          goto while_finish;
207 208

      contact = tpf_persona_get_contact (persona);
209 210 211
      if (contact == NULL)
        goto while_finish;

212
      conn = tp_contact_get_connection (contact);
213

214 215
      if (tp_proxy_has_interface_by_id (conn,
            TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
216 217 218 219 220 221 222 223 224 225
        {
          s = blocked_str;
          npersonas_blocked++;
        }
      else
        {
          s = notblocked_str;
          npersonas_notblocked++;
        }

226
      if (tp_connection_can_report_abusive (conn))
227 228
        can_report_abuse = TRUE;

229 230 231
      str = contact_pretty_name (contact);
      g_string_append_printf (s, "\n " BULLET_POINT " %s", str);
      g_free (str);
232 233 234

while_finish:
      g_clear_object (&persona);
235
    }
236
  g_clear_object (&iter);
237

238
  g_string_append_printf (text,
239
      _("Are you sure you want to block “%s” from contacting you again?"),
240
      folks_alias_details_get_alias (FOLKS_ALIAS_DETAILS (individual)));
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

  if (npersonas_blocked > 0)
    g_string_append_printf (text, "\n\n%s\n%s",
        ngettext ("The following identity will be blocked:",
                  "The following identities will be blocked:",
                  npersonas_blocked),
        blocked_str->str);

  if (npersonas_notblocked > 0)
    g_string_append_printf (text, "\n\n%s\n%s",
        ngettext ("The following identity can not be blocked:",
                  "The following identities can not be blocked:",
                  npersonas_notblocked),
        notblocked_str->str);

256
  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
257
    "%s", text->str);
258 259 260 261 262 263 264 265 266 267 268 269 270 271

  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
      _("_Block"), GTK_RESPONSE_REJECT,
      NULL);

  if (can_report_abuse)
    {
      GtkWidget *vbox;

      vbox = gtk_message_dialog_get_message_area (GTK_MESSAGE_DIALOG (dialog));
      abusive_check = gtk_check_button_new_with_mnemonic (
          ngettext ("_Report this contact as abusive",
                    "_Report these contacts as abusive",
272
                    npersonas_blocked));
273 274 275 276 277

      gtk_box_pack_start (GTK_BOX (vbox), abusive_check, FALSE, TRUE, 0);
      gtk_widget_show (abusive_check);
    }

278 279 280
  g_string_free (text, TRUE);
  g_string_free (blocked_str, TRUE);
  g_string_free (notblocked_str, TRUE);
281 282 283 284 285 286 287 288 289 290 291 292

  res = gtk_dialog_run (GTK_DIALOG (dialog));

  if (abusive != NULL)
    {
      if (abusive_check != NULL)
        *abusive = gtk_toggle_button_get_active (
            GTK_TOGGLE_BUTTON (abusive_check));
      else
        *abusive = FALSE;
    }

293 294
  gtk_widget_destroy (dialog);

295 296
  return res == GTK_RESPONSE_REJECT;
}