empathy-subscription-dialog.c 9.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright (C) 2012 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: Guillaume Desmottes <guillaume.desmottes@collabora.com>
 */

#include "config.h"
#include "empathy-subscription-dialog.h"

#include <glib/gi18n-lib.h>
25
#include <telepathy-glib/telepathy-glib-dbus.h>
26

27 28 29
#include "empathy-individual-widget.h"
#include "empathy-utils.h"

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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
G_DEFINE_TYPE (EmpathySubscriptionDialog, empathy_subscription_dialog, GTK_TYPE_MESSAGE_DIALOG)

enum
{
  PROP_INDIVIDUAL = 1,
  PROP_MESSAGE,
  N_PROPS
};

/*
enum
{
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];
*/

struct _EmpathySubscriptionDialogPriv
{
  FolksIndividual *individual;
  gchar *message;
};

static void
empathy_subscription_dialog_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);

  switch (property_id)
    {
      case PROP_INDIVIDUAL:
        g_value_set_object (value, self->priv->individual);
        break;
      case PROP_MESSAGE:
        g_value_set_string (value, self->priv->message);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
empathy_subscription_dialog_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);

  switch (property_id)
    {
      case PROP_INDIVIDUAL:
        g_assert (self->priv->individual == NULL); /* construct only */
        self->priv->individual = g_value_dup_object (value);
        break;
      case PROP_MESSAGE:
        g_assert (self->priv->message == NULL); /* construct only */
        self->priv->message = g_value_dup_string (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/**
 * block_contact_dialog_show:
 * @parent: the parent of this dialog (or %NULL)
 * @contact: the contact for this dialog
 * @abusive: a pointer to store the value of the abusive contact check box
 *  (or %NULL)
 *
 * Returns: %TRUE if the user wishes to block the contact
 */
static gboolean
block_contact_dialog_show (GtkWindow *parent,
    EmpathyContact *contact,
    GdkPixbuf *avatar,
    gboolean *abusive)
{
  GtkWidget *dialog;
  GtkWidget *abusive_check = NULL;
  int res;
  TpConnection *conn;

  dialog = gtk_message_dialog_new (parent, GTK_DIALOG_MODAL,
      GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, _("Block %s?"),
      empathy_contact_get_alias (contact));

  gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
125
      _("Are you sure you want to block “%s” from contacting you again?"),
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
      empathy_contact_get_alias (contact));

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

  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);
    }

  conn = empathy_contact_get_connection (contact);

  /* ask the user if they want to also report the contact as abusive */
  if (tp_connection_can_report_abusive (conn))
    {
      GtkWidget *vbox;

      vbox = gtk_message_dialog_get_message_area (
          GTK_MESSAGE_DIALOG (dialog));
      abusive_check = gtk_check_button_new_with_mnemonic (
          _("_Report this contact as abusive"));

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

  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;
    }

  gtk_widget_destroy (dialog);

  return res == GTK_RESPONSE_REJECT;
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
static void
response_cb (GtkDialog *dialog,
    gint reponse,
    EmpathySubscriptionDialog *self)
{
  EmpathyContact *contact;

  contact = empathy_contact_dup_from_folks_individual (self->priv->individual);

  if (reponse == GTK_RESPONSE_YES)
    {
      empathy_contact_add_to_contact_list (contact, "");
    }
  else if (reponse == GTK_RESPONSE_NO)
    {
      empathy_contact_remove_from_contact_list (contact);
    }
  else if (reponse == GTK_RESPONSE_REJECT)
    {
      gboolean abusive;

      /* confirm the blocking */
194
      if (block_contact_dialog_show (GTK_WINDOW (dialog), contact,
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
            NULL, &abusive))
        {
          TpContact *tp_contact;

          empathy_contact_remove_from_contact_list (contact);

          tp_contact = empathy_contact_get_tp_contact (contact);

          tp_contact_block_async (tp_contact, abusive, NULL, NULL);
        }
      else
        {
          /* if they don't confirm, return back to the
           * first dialog */
          return;
        }
    }

  gtk_widget_destroy (GTK_WIDGET (self));
}

static void
empathy_subscription_dialog_constructed (GObject *object)
{
  EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
  void (*chain_up) (GObject *) =
      ((GObjectClass *) empathy_subscription_dialog_parent_class)->constructed;
  GtkWidget *content;
  GtkWidget *individual_widget;
  const gchar *alias;
  gchar *tmp;
  EmpathyContact *contact;
  TpConnection *conn;

  if (chain_up != NULL)
    chain_up (object);

  g_assert (self->priv->individual != NULL);

  gtk_window_set_title (GTK_WINDOW (self), _("Subscription Request"));

  alias = folks_alias_details_get_alias (
      FOLKS_ALIAS_DETAILS (self->priv->individual));

  tmp = g_strdup_printf (
      _("%s would like permission to see when you are online"), alias);

  /* Why is there no gtk_message_dialog_set_text()? */
  g_object_set (self, "text", tmp, NULL);
  g_free (tmp);

  if (self->priv->message != NULL)
    {
      tmp = g_strdup_printf ("<i>%s</i>", self->priv->message);
      gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (self),
250
          "%s", tmp);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
      g_free (tmp);
    }

  /* Individual widget */
  individual_widget = empathy_individual_widget_new (self->priv->individual,
      EMPATHY_INDIVIDUAL_WIDGET_EDIT_ALIAS |
      EMPATHY_INDIVIDUAL_WIDGET_EDIT_GROUPS |
      EMPATHY_INDIVIDUAL_WIDGET_SHOW_DETAILS);

  gtk_container_set_border_width (GTK_CONTAINER (individual_widget), 8);

  content = gtk_dialog_get_content_area (GTK_DIALOG (self));

  gtk_box_pack_start (GTK_BOX (content), individual_widget, TRUE, TRUE, 0);
  gtk_widget_show (individual_widget);

  /* Add 'Block' button if supported */
  contact = empathy_contact_dup_from_folks_individual (self->priv->individual);
  conn = empathy_contact_get_connection (contact);

  if (tp_proxy_has_interface_by_id (conn,
        TP_IFACE_QUARK_CONNECTION_INTERFACE_CONTACT_BLOCKING))
    {
      gtk_dialog_add_button (GTK_DIALOG (self),
          _("_Block"), GTK_RESPONSE_REJECT);
    }

  g_object_unref (contact);

  gtk_dialog_add_buttons (GTK_DIALOG (self),
      _("_Decline"), GTK_RESPONSE_NO,
      _("_Accept"), GTK_RESPONSE_YES,
      NULL);

  g_signal_connect (self, "response",
      G_CALLBACK (response_cb), self);
}

static void
empathy_subscription_dialog_dispose (GObject *object)
{
  EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
  void (*chain_up) (GObject *) =
      ((GObjectClass *) empathy_subscription_dialog_parent_class)->dispose;

  g_clear_object (&self->priv->individual);

  if (chain_up != NULL)
    chain_up (object);
}

static void
empathy_subscription_dialog_finalize (GObject *object)
{
  EmpathySubscriptionDialog *self = EMPATHY_SUBSCRIPTION_DIALOG (object);
  void (*chain_up) (GObject *) =
      ((GObjectClass *) empathy_subscription_dialog_parent_class)->finalize;

  g_free (self->priv->message);

  if (chain_up != NULL)
    chain_up (object);
}

static void
empathy_subscription_dialog_class_init (
    EmpathySubscriptionDialogClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
  GParamSpec *spec;

  oclass->get_property = empathy_subscription_dialog_get_property;
  oclass->set_property = empathy_subscription_dialog_set_property;
  oclass->constructed = empathy_subscription_dialog_constructed;
  oclass->dispose = empathy_subscription_dialog_dispose;
  oclass->finalize = empathy_subscription_dialog_finalize;

  spec = g_param_spec_object ("individual", "individual",
      "FolksIndividual",
      FOLKS_TYPE_INDIVIDUAL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (oclass, PROP_INDIVIDUAL, spec);

  spec = g_param_spec_string ("message", "message",
      "Message",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (oclass, PROP_MESSAGE, spec);

  g_type_class_add_private (klass, sizeof (EmpathySubscriptionDialogPriv));
}

static void
empathy_subscription_dialog_init (EmpathySubscriptionDialog *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_SUBSCRIPTION_DIALOG, EmpathySubscriptionDialogPriv);
}

GtkWidget *
empathy_subscription_dialog_new (FolksIndividual *individual,
    const gchar *message)
{
  return g_object_new (EMPATHY_TYPE_SUBSCRIPTION_DIALOG,
      "individual", individual,
      "message", message,
      "message-type", GTK_MESSAGE_QUESTION,
      NULL);
}