empathy-individual-menu.c 31.8 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 25 26 27 28 29
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2008-2010 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: Xavier Claessens <xclaesse@gmail.com>
 *          Travis Reitter <travis.reitter@collabora.co.uk>
 */

#include "config.h"

#include <string.h>

#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <telepathy-glib/util.h>
30

31 32
#include <folks/folks.h>
#include <folks/folks-telepathy.h>
33

34
#include <libempathy/empathy-request-util.h>
35 36 37 38 39 40 41 42
#include <libempathy/empathy-individual-manager.h>
#include <libempathy/empathy-chatroom-manager.h>
#include <libempathy/empathy-utils.h>

#include "empathy-individual-menu.h"
#include "empathy-images.h"
#include "empathy-log-window.h"
#include "empathy-contact-dialogs.h"
43
#include "empathy-gtk-enum-types.h"
44
#include "empathy-individual-edit-dialog.h"
45
#include "empathy-individual-information-dialog.h"
46 47
#include "empathy-ui-utils.h"
#include "empathy-share-my-desktop.h"
48
#include "empathy-linking-dialog.h"
49

50 51 52 53 54 55 56 57 58 59 60 61
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyIndividualMenu)

typedef struct {
  FolksIndividual *individual; /* owned */
  EmpathyIndividualFeatureFlags features;
} EmpathyIndividualMenuPriv;

enum {
  PROP_INDIVIDUAL = 1,
  PROP_FEATURES,
};

62 63 64 65 66 67 68
enum {
  SIGNAL_LINK_CONTACTS_ACTIVATED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

69 70
G_DEFINE_TYPE (EmpathyIndividualMenu, empathy_individual_menu, GTK_TYPE_MENU);

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static void
individual_menu_add_personas (GtkMenuShell *menu,
    FolksIndividual *individual,
    EmpathyIndividualFeatureFlags features)
{
  GtkWidget *item;
  GList *personas, *l;
  guint persona_count = 0;

  g_return_if_fail (GTK_IS_MENU (menu));
  g_return_if_fail (FOLKS_IS_INDIVIDUAL (individual));
  g_return_if_fail (empathy_folks_individual_contains_contact (individual));

  personas = folks_individual_get_personas (individual);

  /* Make sure we've got enough valid entries for these menu items to add
   * functionality */
  for (l = personas; l != NULL; l = l->next)
    {
90
      if (!empathy_folks_persona_is_interesting (FOLKS_PERSONA (l->data)))
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        continue;

      persona_count++;
    }

  /* return early if these entries would add nothing beyond the "quick" items */
  if (persona_count <= 1)
    return;

  /* add a separator before the list of personas */
  item = gtk_separator_menu_item_new ();
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
  gtk_widget_show (item);

  personas = folks_individual_get_personas (individual);
  for (l = personas; l != NULL; l = l->next)
    {
      GtkWidget *image;
      GtkWidget *contact_item;
      GtkWidget *contact_submenu;
      TpContact *tp_contact;
      EmpathyContact *contact;
      TpfPersona *persona = l->data;
      gchar *label;
      FolksPersonaStore *store;
      const gchar *account;
      GtkWidget *action;

119
      if (!empathy_folks_persona_is_interesting (FOLKS_PERSONA (l->data)))
120 121 122 123 124 125 126
        continue;

      tp_contact = tpf_persona_get_contact (persona);
      contact = empathy_contact_dup_from_tp_contact (tp_contact);

      store = folks_persona_get_store (FOLKS_PERSONA (persona));
      account = folks_persona_store_get_display_name (store);
127 128 129 130

      /* Translators: this is used in the context menu for a contact. The first
       * parameter is a contact ID (e.g. foo@jabber.org) and the second is one
       * of the user's account IDs (e.g. me@hotmail.com). */
131 132 133 134
      label = g_strdup_printf (_("%s (%s)"),
          folks_persona_get_display_id (FOLKS_PERSONA (persona)), account);

      contact_item = gtk_image_menu_item_new_with_label (label);
135 136
      gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (contact_item),
                                                 TRUE);
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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
      contact_submenu = gtk_menu_new ();
      gtk_menu_item_set_submenu (GTK_MENU_ITEM (contact_item), contact_submenu);
      image = gtk_image_new_from_icon_name (
          empathy_icon_name_for_contact (contact), GTK_ICON_SIZE_MENU);
      gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (contact_item), image);
      gtk_widget_show (image);

      /* Chat */
      if (features & EMPATHY_INDIVIDUAL_FEATURE_CHAT)
        {
          action = empathy_individual_chat_menu_item_new (NULL, contact);
          gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
          gtk_widget_show (action);
        }

      if (features & EMPATHY_INDIVIDUAL_FEATURE_CALL)
        {
          /* Audio Call */
          action = empathy_individual_audio_call_menu_item_new (NULL, contact);
          gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
          gtk_widget_show (action);

          /* Video Call */
          action = empathy_individual_video_call_menu_item_new (NULL, contact);
          gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
          gtk_widget_show (action);
        }

      /* Log */
      if (features & EMPATHY_INDIVIDUAL_FEATURE_LOG)
        {
          action = empathy_individual_log_menu_item_new (NULL, contact);
          gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
          gtk_widget_show (action);
        }

      /* Invite */
      action = empathy_individual_invite_menu_item_new (NULL, contact);
      gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
      gtk_widget_show (action);

      /* File transfer */
      action = empathy_individual_file_transfer_menu_item_new (NULL, contact);
      gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
      gtk_widget_show (action);

      /* Share my desktop */
      action = empathy_individual_share_my_desktop_menu_item_new (NULL,
          contact);
      gtk_menu_shell_append (GTK_MENU_SHELL (contact_submenu), action);
      gtk_widget_show (action);

      gtk_menu_shell_append (GTK_MENU_SHELL (menu), contact_item);
      gtk_widget_show (contact_item);

      g_free (label);
      g_object_unref (contact);
    }
}

197 198 199 200 201 202 203 204 205 206
static void
individual_link_menu_item_activate_cb (EmpathyIndividualMenu *self)
{
  EmpathyIndividualMenuPriv *priv = GET_PRIV (self);
  GtkWidget *dialog;

  dialog = empathy_linking_dialog_show (priv->individual, NULL);
  g_signal_emit (self, signals[SIGNAL_LINK_CONTACTS_ACTIVATED], 0, dialog);
}

207 208
static void
empathy_individual_menu_init (EmpathyIndividualMenu *self)
209
{
210 211
  EmpathyIndividualMenuPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_INDIVIDUAL_MENU, EmpathyIndividualMenuPriv);
212

213 214
  self->priv = priv;
}
215

216 217 218 219 220 221 222 223
static void
constructed (GObject *object)
{
  EmpathyIndividualMenuPriv *priv = GET_PRIV (object);
  GtkMenuShell *shell;
  GtkWidget *item;
  FolksIndividual *individual;
  EmpathyIndividualFeatureFlags features;
224

225 226 227 228
  /* Build the menu */
  shell = GTK_MENU_SHELL (object);
  individual = priv->individual;
  features = priv->features;
229 230 231 232

  /* Chat */
  if (features & EMPATHY_INDIVIDUAL_FEATURE_CHAT)
    {
233
      item = empathy_individual_chat_menu_item_new (individual, NULL);
234 235 236 237 238 239 240 241 242 243
      if (item != NULL)
        {
          gtk_menu_shell_append (shell, item);
          gtk_widget_show (item);
        }
    }

  if (features & EMPATHY_INDIVIDUAL_FEATURE_CALL)
    {
      /* Audio Call */
244
      item = empathy_individual_audio_call_menu_item_new (individual, NULL);
245 246 247 248
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);

      /* Video Call */
249
      item = empathy_individual_video_call_menu_item_new (individual, NULL);
250 251 252 253 254 255 256
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);
    }

  /* Log */
  if (features & EMPATHY_INDIVIDUAL_FEATURE_LOG)
    {
257
      item = empathy_individual_log_menu_item_new (individual, NULL);
258 259 260 261 262
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);
    }

  /* Invite */
263
  item = empathy_individual_invite_menu_item_new (individual, NULL);
264 265 266 267
  gtk_menu_shell_append (shell, item);
  gtk_widget_show (item);

  /* File transfer */
268
  item = empathy_individual_file_transfer_menu_item_new (individual, NULL);
269 270 271 272 273 274
  gtk_menu_shell_append (shell, item);
  gtk_widget_show (item);

  /* Share my desktop */
  /* FIXME we should add the "Share my desktop" menu item if Vino is
  a registered handler in MC5 */
275
  item = empathy_individual_share_my_desktop_menu_item_new (individual, NULL);
276 277 278
  gtk_menu_shell_append (shell, item);
  gtk_widget_show (item);

279
  /* Menu items to target specific contacts */
280
  individual_menu_add_personas (GTK_MENU_SHELL (object), individual, features);
281

282 283 284
  /* Separator */
  if (features & (EMPATHY_INDIVIDUAL_FEATURE_EDIT |
      EMPATHY_INDIVIDUAL_FEATURE_INFO |
285 286
      EMPATHY_INDIVIDUAL_FEATURE_FAVOURITE |
      EMPATHY_INDIVIDUAL_FEATURE_LINK))
287 288 289 290 291 292 293 294 295 296 297 298 299 300
    {
      item = gtk_separator_menu_item_new ();
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);
    }

  /* Edit */
  if (features & EMPATHY_INDIVIDUAL_FEATURE_EDIT)
    {
      item = empathy_individual_edit_menu_item_new (individual);
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);
    }

301 302 303 304 305
  /* Link */
  if (features & EMPATHY_INDIVIDUAL_FEATURE_LINK)
    {
      item = empathy_individual_link_menu_item_new (individual);
      gtk_menu_shell_append (shell, item);
306 307 308 309

      g_signal_connect_swapped (item, "activate",
          (GCallback) individual_link_menu_item_activate_cb, object);

310 311 312
      gtk_widget_show (item);
    }

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
  /* Info */
  if (features & EMPATHY_INDIVIDUAL_FEATURE_INFO)
    {
      item = empathy_individual_info_menu_item_new (individual);
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);
    }

  /* Favorite checkbox */
  if (features & EMPATHY_INDIVIDUAL_FEATURE_FAVOURITE)
    {
      item = empathy_individual_favourite_menu_item_new (individual);
      gtk_menu_shell_append (shell, item);
      gtk_widget_show (item);
    }
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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
}

static void
get_property (GObject *object,
    guint param_id,
    GValue *value,
    GParamSpec *pspec)
{
  EmpathyIndividualMenuPriv *priv;

  priv = GET_PRIV (object);

  switch (param_id)
    {
      case PROP_INDIVIDUAL:
        g_value_set_object (value, priv->individual);
        break;
      case PROP_FEATURES:
        g_value_set_flags (value, priv->features);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
set_property (GObject *object,
    guint param_id,
    const GValue *value,
    GParamSpec *pspec)
{
  EmpathyIndividualMenuPriv *priv;

  priv = GET_PRIV (object);

  switch (param_id)
    {
      case PROP_INDIVIDUAL:
        priv->individual = g_value_dup_object (value);
        break;
      case PROP_FEATURES:
        priv->features = g_value_get_flags (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
dispose (GObject *object)
{
  EmpathyIndividualMenuPriv *priv = GET_PRIV (object);

  tp_clear_object (&priv->individual);

  G_OBJECT_CLASS (empathy_individual_menu_parent_class)->dispose (object);
}

static void
empathy_individual_menu_class_init (EmpathyIndividualMenuClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed = constructed;
  object_class->get_property = get_property;
  object_class->set_property = set_property;
  object_class->dispose = dispose;

  /**
   * EmpathyIndividualMenu:individual:
   *
   * The #FolksIndividual the menu is for.
   */
  g_object_class_install_property (object_class, PROP_INDIVIDUAL,
      g_param_spec_object ("individual",
          "Individual",
          "The #FolksIndividual the menu is for.",
          FOLKS_TYPE_INDIVIDUAL,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

  /**
   * EmpathyIndividualMenu:features:
   *
   * A set of feature flags controlling which entries are shown.
   */
  g_object_class_install_property (object_class, PROP_FEATURES,
      g_param_spec_flags ("features",
          "Features",
          "A set of feature flags controlling which entries are shown.",
          EMPATHY_TYPE_INDIVIDUAL_FEATURE_FLAGS,
          EMPATHY_INDIVIDUAL_FEATURE_NONE,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));

423 424 425 426 427 428
  signals[SIGNAL_LINK_CONTACTS_ACTIVATED] =
      g_signal_new ("link-contacts-activated", G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST, 0, NULL, NULL,
          g_cclosure_marshal_VOID__OBJECT,
          G_TYPE_NONE, 1, EMPATHY_TYPE_LINKING_DIALOG);

429 430 431 432 433 434 435 436 437
  g_type_class_add_private (object_class, sizeof (EmpathyIndividualMenuPriv));
}

GtkWidget *
empathy_individual_menu_new (FolksIndividual *individual,
    EmpathyIndividualFeatureFlags features)
{
  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
  g_return_val_if_fail (features != EMPATHY_INDIVIDUAL_FEATURE_NONE, NULL);
438

439 440 441 442
  return g_object_new (EMPATHY_TYPE_INDIVIDUAL_MENU,
      "individual", individual,
      "features", features,
      NULL);
443 444
}

445
/* Like menu_item_set_first_contact(), but always operates upon the given
446 447
 * contact. If the contact is non-NULL, it is assumed that the menu entry should
 * be sensitive. */
448 449 450 451
static gboolean
menu_item_set_contact (GtkWidget *item,
    EmpathyContact *contact,
    GCallback activate_callback,
452
    EmpathyActionType action_type)
453
{
454 455 456 457 458
  gboolean can_do_action = FALSE;

  if (contact != NULL)
    can_do_action = empathy_contact_can_do_action (contact, action_type);
  gtk_widget_set_sensitive (item, can_do_action);
459

460
  if (can_do_action == TRUE)
461
    {
462 463 464 465
      /* We want to make sure that the EmpathyContact stays alive while the
       * signal is connected. */
      g_signal_connect_data (item, "activate", G_CALLBACK (activate_callback),
          g_object_ref (contact), (GClosureNotify) g_object_unref, 0);
466
    }
467

468
  return can_do_action;
469
}
470

471
/**
472 473 474
 * Set the given menu @item to call @activate_callback using the TpContact
 * (associated with @individual) with the highest availability who is also valid
 * whenever @item is activated.
475
 *
476 477 478
 * @action_type is the type of action performed by the menu entry; this is used
 * so that only contacts which can perform that action (e.g. are capable of
 * receiving video calls) are selected, as appropriate.
479 480 481 482 483
 */
static GtkWidget *
menu_item_set_first_contact (GtkWidget *item,
    FolksIndividual *individual,
    GCallback activate_callback,
484
    EmpathyActionType action_type)
485
{
486
  EmpathyContact *best_contact;
487

488 489 490 491
  best_contact = empathy_contact_dup_best_for_action (individual, action_type);
  menu_item_set_contact (item, best_contact, G_CALLBACK (activate_callback),
      action_type);
  tp_clear_object (&best_contact);
492 493 494 495 496 497 498 499 500 501

  return item;
}

static void
empathy_individual_chat_menu_item_activated (GtkMenuItem *item,
  EmpathyContact *contact)
{
  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

502
  empathy_chat_with_contact (contact, empathy_get_current_action_time ());
503 504 505
}

GtkWidget *
506 507
empathy_individual_chat_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
508 509 510 511
{
  GtkWidget *item;
  GtkWidget *image;

512 513 514
  g_return_val_if_fail ((FOLKS_IS_INDIVIDUAL (individual) &&
      empathy_folks_individual_contains_contact (individual)) ||
      EMPATHY_IS_CONTACT (contact),
515 516 517 518 519 520 521 522
      NULL);

  item = gtk_image_menu_item_new_with_mnemonic (_("_Chat"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_MESSAGE,
      GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

523 524 525
  if (contact != NULL)
    {
      menu_item_set_contact (item, contact,
526 527
          G_CALLBACK (empathy_individual_chat_menu_item_activated),
          EMPATHY_ACTION_CHAT);
528 529 530 531
    }
  else
    {
      menu_item_set_first_contact (item, individual,
532 533
          G_CALLBACK (empathy_individual_chat_menu_item_activated),
          EMPATHY_ACTION_CHAT);
534
    }
535 536 537 538 539 540

  return item;
}

static void
empathy_individual_audio_call_menu_item_activated (GtkMenuItem *item,
541
  EmpathyContact *contact)
542 543 544
{
  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

545
  empathy_call_new_with_streams (contact, TRUE, FALSE,
546
      empathy_get_current_action_time ());
547 548 549
}

GtkWidget *
550 551
empathy_individual_audio_call_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
552 553 554 555
{
  GtkWidget *item;
  GtkWidget *image;

556 557 558
  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
      EMPATHY_IS_CONTACT (contact),
      NULL);
559 560 561 562 563 564

  item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Audio Call"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VOIP, GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

565 566 567 568
  if (contact != NULL)
    {
      menu_item_set_contact (item, contact,
          G_CALLBACK (empathy_individual_audio_call_menu_item_activated),
569
          EMPATHY_ACTION_AUDIO_CALL);
570 571 572 573 574
    }
  else
    {
      menu_item_set_first_contact (item, individual,
          G_CALLBACK (empathy_individual_audio_call_menu_item_activated),
575
          EMPATHY_ACTION_AUDIO_CALL);
576
    }
577 578 579 580 581 582

  return item;
}

static void
empathy_individual_video_call_menu_item_activated (GtkMenuItem *item,
583
  EmpathyContact *contact)
584 585 586
{
  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

587
  empathy_call_new_with_streams (contact, TRUE, TRUE,
588
      empathy_get_current_action_time ());
589 590 591
}

GtkWidget *
592 593
empathy_individual_video_call_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
594 595 596 597
{
  GtkWidget *item;
  GtkWidget *image;

598 599 600
  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
      EMPATHY_IS_CONTACT (contact),
      NULL);
601 602 603 604 605 606 607

  item = gtk_image_menu_item_new_with_mnemonic (C_("menu item", "_Video Call"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_VIDEO_CALL,
      GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

608 609 610 611
  if (contact != NULL)
    {
      menu_item_set_contact (item, contact,
          G_CALLBACK (empathy_individual_video_call_menu_item_activated),
612
          EMPATHY_ACTION_VIDEO_CALL);
613 614 615 616 617
    }
  else
    {
      menu_item_set_first_contact (item, individual,
          G_CALLBACK (empathy_individual_video_call_menu_item_activated),
618
          EMPATHY_ACTION_VIDEO_CALL);
619
    }
620 621 622 623 624

  return item;
}

static void
625 626
empathy_individual_log_menu_item_activated (GtkMenuItem *item,
  EmpathyContact *contact)
627 628 629 630 631 632 633
{
  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

  empathy_log_window_show (empathy_contact_get_account (contact),
      empathy_contact_get_id (contact), FALSE, NULL);
}

634 635 636 637 638 639 640 641 642 643 644
GtkWidget *
empathy_individual_log_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
{
  GtkWidget *item;
  GtkWidget *image;

  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
      EMPATHY_IS_CONTACT (contact),
      NULL);

645 646 647 648 649
  item = gtk_image_menu_item_new_with_mnemonic (_("_Previous Conversations"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_LOG, GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

650 651 652 653
  if (contact != NULL)
    {
      menu_item_set_contact (item, contact,
          G_CALLBACK (empathy_individual_log_menu_item_activated),
654
          EMPATHY_ACTION_VIEW_LOGS);
655 656 657 658 659
    }
  else
    {
      menu_item_set_first_contact (item, individual,
          G_CALLBACK (empathy_individual_log_menu_item_activated),
660
          EMPATHY_ACTION_VIEW_LOGS);
661
    }
662 663 664 665 666

  return item;
}

static void
667 668
empathy_individual_file_transfer_menu_item_activated (GtkMenuItem *item,
    EmpathyContact *contact)
669 670 671 672 673 674 675
{
  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

  empathy_send_file_with_file_chooser (contact);
}

GtkWidget *
676 677
empathy_individual_file_transfer_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
678 679 680 681
{
  GtkWidget *item;
  GtkWidget *image;

682 683 684
  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
      EMPATHY_IS_CONTACT (contact),
      NULL);
685 686 687

  item = gtk_image_menu_item_new_with_mnemonic (_("Send File"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_DOCUMENT_SEND,
688
      GTK_ICON_SIZE_MENU);
689 690 691
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

692 693 694 695
  if (contact != NULL)
    {
      menu_item_set_contact (item, contact,
          G_CALLBACK (empathy_individual_file_transfer_menu_item_activated),
696
          EMPATHY_ACTION_SEND_FILE);
697 698 699 700 701
    }
  else
    {
      menu_item_set_first_contact (item, individual,
          G_CALLBACK (empathy_individual_file_transfer_menu_item_activated),
702
          EMPATHY_ACTION_SEND_FILE);
703
    }
704 705 706 707 708

  return item;
}

static void
709 710
empathy_individual_share_my_desktop_menu_item_activated (GtkMenuItem *item,
    EmpathyContact *contact)
711 712 713 714 715 716 717
{
  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

  empathy_share_my_desktop_share_with_contact (contact);
}

GtkWidget *
718 719
empathy_individual_share_my_desktop_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
720 721 722 723
{
  GtkWidget *item;
  GtkWidget *image;

724 725 726
  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
      EMPATHY_IS_CONTACT (contact),
      NULL);
727 728 729 730 731 732

  item = gtk_image_menu_item_new_with_mnemonic (_("Share My Desktop"));
  image = gtk_image_new_from_icon_name (GTK_STOCK_NETWORK, GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

733 734 735 736
  if (contact != NULL)
    {
      menu_item_set_contact (item, contact,
          G_CALLBACK (empathy_individual_share_my_desktop_menu_item_activated),
737
          EMPATHY_ACTION_SHARE_MY_DESKTOP);
738 739 740 741 742
    }
  else
    {
      menu_item_set_first_contact (item, individual,
          G_CALLBACK (empathy_individual_share_my_desktop_menu_item_activated),
743
          EMPATHY_ACTION_SHARE_MY_DESKTOP);
744
    }
745 746 747 748 749 750 751 752

  return item;
}

static void
favourite_menu_item_toggled_cb (GtkCheckMenuItem *item,
  FolksIndividual *individual)
{
753 754
  folks_favourite_details_set_is_favourite (
      FOLKS_FAVOURITE_DETAILS (individual),
755
      gtk_check_menu_item_get_active (item));
756 757 758 759 760 761 762 763 764 765
}

GtkWidget *
empathy_individual_favourite_menu_item_new (FolksIndividual *individual)
{
  GtkWidget *item;

  item = gtk_check_menu_item_new_with_label (_("Favorite"));

  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item),
766 767
      folks_favourite_details_get_is_favourite (
          FOLKS_FAVOURITE_DETAILS (individual)));
768 769 770 771 772 773 774 775 776 777

  g_signal_connect (item, "toggled",
      G_CALLBACK (favourite_menu_item_toggled_cb), individual);

  return item;
}

static void
individual_info_menu_item_activate_cb (FolksIndividual *individual)
{
778
  empathy_individual_information_dialog_show (individual, NULL);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
}

GtkWidget *
empathy_individual_info_menu_item_new (FolksIndividual *individual)
{
  GtkWidget *item;
  GtkWidget *image;

  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);
  g_return_val_if_fail (empathy_folks_individual_contains_contact (individual),
      NULL);

  item = gtk_image_menu_item_new_with_mnemonic (_("Infor_mation"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_CONTACT_INFORMATION,
                GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

  g_signal_connect_swapped (item, "activate",
          G_CALLBACK (individual_info_menu_item_activate_cb),
          individual);

  return item;
}

static void
individual_edit_menu_item_activate_cb (FolksIndividual *individual)
{
807
  empathy_individual_edit_dialog_show (individual, NULL);
808 809 810 811 812 813 814 815 816 817 818 819 820
}

GtkWidget *
empathy_individual_edit_menu_item_new (FolksIndividual *individual)
{
  EmpathyIndividualManager *manager;
  GtkWidget *item;
  GtkWidget *image;
  gboolean enable = FALSE;
  EmpathyContact *contact;

  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);

821
  contact = empathy_contact_dup_from_folks_individual (individual);
822 823 824 825 826 827 828 829 830 831

  g_return_val_if_fail (EMPATHY_IS_CONTACT (contact), NULL);

  if (empathy_individual_manager_initialized ())
    {
      TpConnection *connection;

      manager = empathy_individual_manager_dup_singleton ();
      connection = empathy_contact_get_connection (contact);

832 833
      enable = (empathy_connection_can_alias_personas (connection) &&
                empathy_connection_can_group_personas (connection));
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853

      g_object_unref (manager);
    }

  item = gtk_image_menu_item_new_with_mnemonic (
      C_("Edit individual (contextual menu)", "_Edit"));
  image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);

  gtk_widget_set_sensitive (item, enable);

  g_signal_connect_swapped (item, "activate",
      G_CALLBACK (individual_edit_menu_item_activate_cb), individual);

  g_object_unref (contact);

  return item;
}

854 855 856 857 858 859 860 861 862
GtkWidget *
empathy_individual_link_menu_item_new (FolksIndividual *individual)
{
  GtkWidget *item;
  /*GtkWidget *image;*/

  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual), NULL);

  item = gtk_image_menu_item_new_with_mnemonic (
863 864
      /* Translators: this is a verb meaning "to connect two contacts together
       * to form a meta-contact". */
865
      C_("Link individual (contextual menu)", "_Link Contacts…"));
866 867 868 869 870
  /* TODO */
  /*image = gtk_image_new_from_icon_name (GTK_STOCK_EDIT, GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
  gtk_widget_show (image);*/

871 872 873 874 875
  /* Only allow trusted Individuals to be linked */
  gtk_widget_set_sensitive (item,
      folks_individual_get_trust_level (individual) ==
          FOLKS_TRUST_LEVEL_PERSONAS);

876 877 878
  return item;
}

879 880 881
typedef struct
{
  FolksIndividual *individual;
882
  EmpathyContact *contact;
883 884 885 886 887
  EmpathyChatroom *chatroom;
} RoomSubMenuData;

static RoomSubMenuData *
room_sub_menu_data_new (FolksIndividual *individual,
888 889
    EmpathyContact *contact,
    EmpathyChatroom *chatroom)
890 891 892
{
  RoomSubMenuData *data;

893 894 895 896 897
  data = g_slice_new0 (RoomSubMenuData);
  if (individual != NULL)
    data->individual = g_object_ref (individual);
  if (contact != NULL)
    data->contact = g_object_ref (contact);
898
  data->chatroom = g_object_ref (chatroom);
899

900 901 902 903 904 905
  return data;
}

static void
room_sub_menu_data_free (RoomSubMenuData *data)
{
906 907
  tp_clear_object (&data->individual);
  tp_clear_object (&data->contact);
908 909 910 911 912 913 914 915 916
  g_object_unref (data->chatroom);
  g_slice_free (RoomSubMenuData, data);
}

static void
room_sub_menu_activate_cb (GtkWidget *item,
         RoomSubMenuData *data)
{
  EmpathyTpChat *chat;
917 918 919
  EmpathyChatroomManager *mgr;
  EmpathyContact *contact = NULL;
  GList *personas, *l;
920 921 922 923 924 925 926 927

  chat = empathy_chatroom_get_tp_chat (data->chatroom);
  if (chat == NULL)
    {
      /* channel was invalidated. Ignoring */
      return;
    }

928 929
  mgr = empathy_chatroom_manager_dup_singleton (NULL);

930 931 932
  if (data->contact != NULL)
    contact = g_object_ref (data->contact);
  else
933
    {
934 935 936 937 938 939 940
      /* find the first of this Individual's contacts who can join this room */
      personas = folks_individual_get_personas (data->individual);
      for (l = personas; l != NULL && contact == NULL; l = g_list_next (l))
        {
          TpfPersona *persona = l->data;
          TpContact *tp_contact;
          GList *rooms;
941

942
          if (!empathy_folks_persona_is_interesting (FOLKS_PERSONA (l->data)))
943
            continue;
944

945 946
          tp_contact = tpf_persona_get_contact (persona);
          contact = empathy_contact_dup_from_tp_contact (tp_contact);
947

948 949
          rooms = empathy_chatroom_manager_get_chatrooms (mgr,
              empathy_contact_get_account (contact));
950

951 952
          if (g_list_find (rooms, data->chatroom) == NULL)
            tp_clear_object (&contact);
953

954
          /* if contact != NULL here, we've found our match */
955

956 957
          g_list_free (rooms);
        }
958 959 960 961 962 963 964
    }

  g_object_unref (mgr);

  if (contact == NULL)
    {
      /* contact disappeared. Ignoring */
965
      goto out;
966
    }
967 968 969 970 971 972 973

  g_return_if_fail (EMPATHY_IS_CONTACT (contact));

  /* send invitation */
  empathy_contact_list_add (EMPATHY_CONTACT_LIST (chat),
      contact, _("Inviting you to this room"));

974
out:
975 976 977 978 979
  g_object_unref (contact);
}

static GtkWidget *
create_room_sub_menu (FolksIndividual *individual,
980
                      EmpathyContact *contact,
981 982 983 984 985 986
                      EmpathyChatroom *chatroom)
{
  GtkWidget *item;
  RoomSubMenuData *data;

  item = gtk_menu_item_new_with_label (empathy_chatroom_get_name (chatroom));
987
  data = room_sub_menu_data_new (individual, contact, chatroom);
988 989 990 991 992 993 994 995
  g_signal_connect_data (item, "activate",
      G_CALLBACK (room_sub_menu_activate_cb), data,
      (GClosureNotify) room_sub_menu_data_free, 0);

  return item;
}

GtkWidget *
996 997
empathy_individual_invite_menu_item_new (FolksIndividual *individual,
    EmpathyContact *contact)
998 999 1000 1001 1002
{
  GtkWidget *item;
  GtkWidget *image;
  GtkWidget *room_item;
  EmpathyChatroomManager *mgr;
1003 1004 1005 1006
  GList *personas;
  GList *rooms = NULL;
  GList *names = NULL;
  GList *l;
1007
  GtkWidget *submenu = NULL;
1008 1009 1010
  /* map of chat room names to their objects; just a utility to remove
   * duplicates and to make construction of the alphabetized list easier */
  GHashTable *name_room_map;
1011

1012 1013 1014
  g_return_val_if_fail (FOLKS_IS_INDIVIDUAL (individual) ||
      EMPATHY_IS_CONTACT (contact),
      NULL);
1015

1016 1017
  name_room_map = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
      g_object_unref);
1018 1019 1020 1021 1022 1023 1024 1025

  item = gtk_image_menu_item_new_with_mnemonic (_("_Invite to Chat Room"));
  image = gtk_image_new_from_icon_name (EMPATHY_IMAGE_GROUP_MESSAGE,
      GTK_ICON_SIZE_MENU);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);

  mgr = empathy_chatroom_manager_dup_singleton (NULL);

1026
  if (contact != NULL)
1027
    {
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
      rooms = empathy_chatroom_manager_get_chatrooms (mgr,
          empathy_contact_get_account (contact));
    }
  else
    {
      /* collect the rooms from amongst all accounts for this Individual */
      personas = folks_individual_get_personas (individual);
      for (l = personas; l != NULL; l = g_list_next (l))
        {
          TpfPersona *persona = l->data;
          GList *rooms_cur;
          TpContact *tp_contact;
          EmpathyContact *contact_cur;
1041

1042
          if (!empathy_folks_persona_is_interesting (FOLKS_PERSONA (l->data)))
1043
            continue;
1044

1045 1046
          tp_contact = tpf_persona_get_contact (persona);
          contact_cur = empathy_contact_dup_from_tp_contact (tp_contact);
1047

1048 1049 1050
          rooms_cur = empathy_chatroom_manager_get_chatrooms (mgr,
              empathy_contact_get_account (contact_cur));
          rooms = g_list_concat (rooms, rooms_cur);
1051

1052 1053
          g_object_unref (contact_cur);
        }
1054 1055 1056
    }

  /* alphabetize the rooms */
1057 1058 1059
  for (l = rooms; l != NULL; l = g_list_next (l))
    {
      EmpathyChatroom *chatroom = l->data;
1060
      gboolean existed;
1061 1062 1063

      if (empathy_chatroom_get_tp_chat (chatroom) != NULL)
        {
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
          const gchar *name;

          name = empathy_chatroom_get_name (chatroom);
          existed = (g_hash_table_lookup (name_room_map, name) != NULL);
          g_hash_table_insert (name_room_map, (gpointer) name,
              g_object_ref (chatroom));

          /* this will take care of duplicates in rooms */
          if (!existed)
            {
              names = g_list_insert_sorted (names, (gpointer) name,
                  (GCompareFunc) g_strcmp0);
            }
1077 1078 1079
        }
    }

1080 1081 1082 1083 1084 1085 1086 1087 1088
  for (l = names; l != NULL; l = g_list_next (l))
    {
      const gchar *name = l->data;
      EmpathyChatroom *chatroom;

      if (G_UNLIKELY (submenu == NULL))
        submenu = gtk_menu_new ();

      chatroom = g_hash_table_lookup (name_room_map, name);
1089
      room_item = create_room_sub_menu (individual, contact, chatroom);
1090 1091 1092 1093
      gtk_menu_shell_append ((GtkMenuShell *) submenu, room_item);
      gtk_widget_show (room_item);
    }

1094 1095 1096 1097 1098 1099 1100
  if (submenu)
    gtk_menu_item_set_submenu (GTK_MENU_ITEM (item), submenu);
  else
    gtk_widget_set_sensitive (item, FALSE);

  gtk_widget_show (image);

1101
  g_hash_table_destroy (name_room_map);
1102
  g_object_unref (mgr);
1103
  g_list_free (names);
1104 1105 1106 1107
  g_list_free (rooms);

  return item;
}