polari-room.c 29.1 KB
Newer Older
Florian Müllner's avatar
Florian Müllner committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 program.  If not, see <http://www.gnu.org/licenses/>.";
 */

#include <string.h>

#include "polari-room.h"
22
#include "polari-util.h"
23
#include "polari-tp-autocleanup.h"
Florian Müllner's avatar
Florian Müllner committed
24

25 26 27 28 29 30 31 32
typedef struct _PolariRoomPrivate PolariRoomPrivate;

struct _PolariRoom {
    GObject parent_instance;

    PolariRoomPrivate *priv;
};

Florian Müllner's avatar
Florian Müllner committed
33
struct _PolariRoomPrivate {
34
  TpAccount *account;
Florian Müllner's avatar
Florian Müllner committed
35 36 37
  TpChannel *channel;

  GIcon *icon;
38
  char  *channel_name;
Florian Müllner's avatar
Florian Müllner committed
39
  char  *display_name;
Florian Müllner's avatar
Florian Müllner committed
40
  char  *topic;
Florian Müllner's avatar
Florian Müllner committed
41

42
  char *self_nick;
43
  char *self_user;
44

45 46
  char *channel_error;

47 48
  TpHandleType type;

49
  guint self_contact_notify_id;
Florian Müllner's avatar
Florian Müllner committed
50

51 52
  gboolean ignore_identify;

Florian Müllner's avatar
Florian Müllner committed
53
  TpProxySignalConnection *properties_changed_id;
Florian Müllner's avatar
Florian Müllner committed
54 55 56 57 58 59 60 61
};

enum
{
  PROP_0,

  PROP_ID,
  PROP_ICON,
62 63
  PROP_ACCOUNT,
  PROP_TYPE,
64
  PROP_CHANNEL_ERROR,
65
  PROP_CHANNEL_NAME,
Florian Müllner's avatar
Florian Müllner committed
66 67
  PROP_CHANNEL,
  PROP_DISPLAY_NAME,
Florian Müllner's avatar
Florian Müllner committed
68
  PROP_TOPIC,
Florian Müllner's avatar
Florian Müllner committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

  LAST_PROP
};

static GParamSpec *props[LAST_PROP];

enum
{
  MEMBER_JOINED,
  MEMBER_LEFT,
  MEMBER_DISCONNECTED,
  MEMBER_RENAMED,
  MEMBER_KICKED,
  MEMBER_BANNED,

84 85
  MEMBERS_CHANGED,

86 87
  IDENTIFY_SENT,

Florian Müllner's avatar
Florian Müllner committed
88 89 90 91 92
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

93 94
static GRegex *color_code_regex = NULL;

Florian Müllner's avatar
Florian Müllner committed
95 96
G_DEFINE_TYPE_WITH_PRIVATE (PolariRoom, polari_room, G_TYPE_OBJECT)

97 98 99
static void polari_room_set_channel (PolariRoom *room, TpChannel *channel);


100 101
/**
 * polari_room_id_from_channel:
102 103 104
 * @account: a TpAccount
 * @name: the room name
 * @type: the room type
105
 *
106 107
 * Returns: (transfer full): a room ID corresponding to @account, @name
 *                           and @type
108 109
 */
char *
110 111 112
polari_create_room_id (TpAccount    *account,
                       const char   *name,
                       TpHandleType  type)
113
{
114 115
  g_autofree char *folded_name = NULL;
  char *id;
116

117 118
  g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
  g_return_val_if_fail (name != NULL, NULL);
119

120 121 122 123 124 125
  folded_name = g_utf8_strdown (name, -1);
  id = g_strdup_printf ("%s/%d/%s",
                        tp_proxy_get_object_path (TP_PROXY (account)),
                        type, folded_name);

  return id;
126 127
}

128 129 130 131 132 133
static gboolean
match_self_nick (PolariRoom *room,
                 const char *text)
{
  PolariRoomPrivate *priv = room->priv;

134
  return polari_util_match_nick (text, priv->self_nick);
135 136
}

Florian Müllner's avatar
Florian Müllner committed
137 138
gboolean
polari_room_should_highlight_message (PolariRoom *room,
139 140
                                      const char *sender,
                                      const char *message)
Florian Müllner's avatar
Florian Müllner committed
141 142 143 144 145 146 147
{
  PolariRoomPrivate *priv;

  g_return_val_if_fail (POLARI_IS_ROOM (room), FALSE);

  priv = room->priv;

148
  if (priv->type != TP_HANDLE_TYPE_ROOM)
149
    return TRUE;
Florian Müllner's avatar
Florian Müllner committed
150

151
  if (match_self_nick (room, sender))
Florian Müllner's avatar
Florian Müllner committed
152 153
    return FALSE;

154
  return match_self_nick (room, message);
Florian Müllner's avatar
Florian Müllner committed
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
const char *
polari_room_get_channel_error (PolariRoom *room)
{
  g_return_val_if_fail (POLARI_IS_ROOM (room), NULL);

  return room->priv->channel_error;
}

void
polari_room_set_channel_error (PolariRoom *room,
                               const char *channel_error)
{
  g_return_if_fail (POLARI_IS_ROOM (room));

  if (g_strcmp0 (room->priv->channel_error, channel_error) == 0)
    return;

  g_free (room->priv->channel_error);
  room->priv->channel_error = g_strdup (channel_error);

  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_CHANNEL_ERROR]);
}

180 181 182 183 184 185 186
void
polari_room_set_topic (PolariRoom *room,
                       const char *topic)
{
  g_return_if_fail (POLARI_IS_ROOM (room));

  tp_cli_channel_interface_subject_call_set_subject (room->priv->channel, -1,
187
      topic, NULL, NULL, NULL, NULL);
188 189 190

}

191 192 193 194 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
void
polari_room_add_member (PolariRoom *room,
                        TpContact  *member)
{
  TpChannel *channel;

  g_return_if_fail (POLARI_IS_ROOM (room));

  channel = room->priv->channel;

  if (!tp_proxy_has_interface_by_id (TP_PROXY (channel),
                                     TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
    return;

  {
    TpHandle handle = tp_contact_get_handle (member);
    GArray handles = { (char *)&handle, 1 };

    tp_cli_channel_interface_group_call_add_members (channel,
                                   -1, &handles, NULL, NULL, NULL, NULL, NULL);
  }
}

void
polari_room_remove_member (PolariRoom *room,
                           TpContact  *member)
{
  TpChannel *channel;

  g_return_if_fail (POLARI_IS_ROOM (room));

  channel = room->priv->channel;

  if (!tp_proxy_has_interface_by_id (TP_PROXY (channel),
                                     TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
    return;

  {
    TpHandle handle = tp_contact_get_handle (member);
    GArray handles = { (char *)&handle, 1 };

    tp_cli_channel_interface_group_call_remove_members (channel,
                                   -1, &handles, NULL, NULL, NULL, NULL, NULL);
  }
}

237 238 239 240 241 242
static void
on_identify_message_sent (GObject      *source,
                          GAsyncResult *result,
                          gpointer      user_data)
{
  TpTextChannel *channel = TP_TEXT_CHANNEL (source);
243
  g_autoptr(GTask) task = user_data;
244 245 246 247 248 249 250 251 252 253 254 255 256 257
  PolariRoom *room = g_task_get_source_object (task);
  GError *error = NULL;

  if (!tp_text_channel_send_message_finish (channel, result, NULL, &error))
    {
      room->priv->ignore_identify = FALSE;

      g_task_return_error (task, error);
      return;
    }

  g_task_return_boolean (task, TRUE);
}

258 259 260 261 262
/**
 * polari_room_send_identify_message_async:
 *  @username: (nullable): Username to use in identify command or %NULL
 */

263 264
void
polari_room_send_identify_message_async (PolariRoom          *room,
265
                                         const char          *command,
266 267 268 269 270 271
                                         const char          *username,
                                         const char          *password,
                                         GAsyncReadyCallback  callback,
                                         gpointer             user_data)
{
  PolariRoomPrivate *priv;
272 273 274
  g_autoptr(TpMessage) message = NULL;
  g_autoptr(GTask) task = NULL;;
  g_autofree char *text = NULL;
275 276

  g_return_if_fail (POLARI_IS_ROOM (room));
277
  g_return_if_fail (command != NULL && password != NULL);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

  priv = room->priv;

  task = g_task_new (room, NULL, callback, user_data);

  if (priv->channel == NULL)
    {
      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_NOT_CONNECTED,
                               "The room is disconnected.");
      return;
    }

  /* Don't emit ::identify-sent for our own identify message */
  room->priv->ignore_identify = TRUE;

293 294 295 296 297
  if (username == NULL)
    text = g_strdup_printf ("%s %s", command, password);
  else
    text = g_strdup_printf ("%s %s %s", command, username, password);

298 299 300
  message = tp_client_message_new_text (TP_CHANNEL_TEXT_MESSAGE_TYPE_NORMAL, text);

  tp_text_channel_send_message_async (TP_TEXT_CHANNEL (priv->channel), message,
301
                                      0, on_identify_message_sent, g_steal_pointer (&task));
302 303 304 305 306 307 308 309 310 311 312 313 314
}

gboolean
polari_room_send_identify_message_finish (PolariRoom    *room,
                                          GAsyncResult  *result,
                                          GError       **error)
{
  g_return_val_if_fail (POLARI_IS_ROOM (room), FALSE);
  g_return_val_if_fail (g_task_is_valid (result, room), FALSE);

  return g_task_propagate_boolean (G_TASK (result), error);
}

315 316 317 318 319 320 321 322
static char *
strip_color_codes (const char *string) {
  if (G_UNLIKELY (color_code_regex == NULL))
    color_code_regex = g_regex_new ("\x03(?:[0-9]{1,2}(?:,[0-9]{1,2})?)?",
                                    G_REGEX_OPTIMIZE, 0, NULL);
  return g_regex_replace_literal (color_code_regex, string, -1, 0, "", 0, NULL);
}

323 324 325 326
static void
update_self_nick (PolariRoom *room)
{
  PolariRoomPrivate *priv = room->priv;
327
  const char *nick;
Florian Müllner's avatar
Florian Müllner committed
328
  g_autofree char *basenick = NULL;
329 330 331

  g_clear_pointer (&priv->self_nick, g_free);

332 333 334 335
  if (priv->channel)
    {
      TpConnection *conn;
      TpContact *self;
336

337 338 339 340 341 342 343 344 345
      conn = tp_channel_get_connection (priv->channel);
      self = tp_connection_get_self_contact (conn);

      nick = tp_contact_get_alias (self);
    }
  else
    {
      nick = tp_account_get_nickname (priv->account);
    }
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
  basenick = polari_util_get_basenick (nick);

  /*
      - we want highlights for 'nick-away'/'nick' and vice-versa
      - we don't want highlights for 'the-nick'/'the'

     To tell those cases apart, match against the account's username
     instead of the basenick if:

     (1) basenick matches at the start of the username
     (2) the username matches at the start of the nickname
     ==> basenick <= username <= nickname

     Using the username for highlighting should not produce false
     negatives in that case, and may prevent false positives in
     case the regular nickname contains non-alnums
   */
  if (strstr (priv->self_user, basenick) == priv->self_user &&
      strstr (nick, priv->self_user) == nick)
    priv->self_nick = g_strdup (priv->self_user);
  else
    priv->self_nick = g_strdup (basenick);
}

static void
update_self_user (PolariRoom *room)
{
  PolariRoomPrivate *priv = room->priv;
  const GHashTable *parameters;
  const char *username;
  int len;

  g_clear_pointer (&priv->self_user, g_free);

  parameters = tp_account_get_parameters (priv->account);
  username = tp_asv_get_string (parameters, "account");

  len = strlen (username);
  do
    if (g_ascii_isalnum (username[len - 1]))
      break;
  while (--len > 0);

  priv->self_user = g_utf8_casefold (username, len);
391 392
}

Florian Müllner's avatar
Florian Müllner committed
393
static void
Florian Müllner's avatar
Florian Müllner committed
394 395
set_display_name (PolariRoom *room,
                  const char *display_name)
Florian Müllner's avatar
Florian Müllner committed
396 397 398
{
  PolariRoomPrivate *priv = room->priv;

Florian Müllner's avatar
Florian Müllner committed
399 400
  g_free (priv->display_name);
  priv->display_name = g_strdup (display_name);
Florian Müllner's avatar
Florian Müllner committed
401 402 403 404 405 406 407 408 409 410 411

  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_DISPLAY_NAME]);
}

static void
update_icon (PolariRoom *room)
{
  PolariRoomPrivate *priv = room->priv;

  g_clear_object (&priv->icon);

412
  if (priv->type == TP_HANDLE_TYPE_CONTACT)
413
    priv->icon = g_themed_icon_new ("avatar-default-symbolic");
Florian Müllner's avatar
Florian Müllner committed
414 415 416 417

  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_ICON]);
}

418
static void
Florian Müllner's avatar
Florian Müllner committed
419 420
on_self_contact_notify (GObject    *object G_GNUC_UNUSED,
                        GParamSpec *pspec G_GNUC_UNUSED,
421 422 423 424 425
                        gpointer    user_data)
{
  update_self_nick (POLARI_ROOM (user_data));
}

Florian Müllner's avatar
Florian Müllner committed
426
static void
Florian Müllner's avatar
Florian Müllner committed
427
on_group_contacts_changed (TpChannel  *channel G_GNUC_UNUSED,
Florian Müllner's avatar
Florian Müllner committed
428 429
                           GPtrArray  *added,
                           GPtrArray  *removed,
Florian Müllner's avatar
Florian Müllner committed
430 431
                           GPtrArray  *local_pending G_GNUC_UNUSED,
                           GPtrArray  *remote_pending G_GNUC_UNUSED,
Florian Müllner's avatar
Florian Müllner committed
432 433 434 435 436
                           TpContact  *actor,
                           GHashTable *details,
                           gpointer    user_data)
{
  TpChannelGroupChangeReason reason;
437
  const char *raw_message;
438
  g_autofree char *message = NULL;
439
  guint i;
Florian Müllner's avatar
Florian Müllner committed
440 441

  reason = tp_asv_get_uint32 (details, "change-reason", NULL);
442 443 444 445
  raw_message = tp_asv_get_string (details, "message");

  if (raw_message)
    message = strip_color_codes (raw_message);
Florian Müllner's avatar
Florian Müllner committed
446 447 448 449

  switch (reason)
    {
    case TP_CHANNEL_GROUP_CHANGE_REASON_RENAMED:
450 451 452 453 454 455 456 457 458 459 460

      if (removed->len != 1 || added->len != 1) {
        const char *removed_alias = removed->len > 0 ? tp_contact_get_alias (g_ptr_array_index (removed, 0)) : "undefined";
        const char *added_alias = added->len > 0 ? tp_contact_get_alias (g_ptr_array_index (added, 0)) : "undefined";
        g_warning ("Renamed '%s' to '%s'. Expected to have 1 removed and 1 added, but got %u removed and %u added",
                   removed_alias, added_alias, removed->len, added->len);
      } else {
        g_signal_emit (user_data, signals[MEMBER_RENAMED], 0,
                       g_ptr_array_index (removed, 0),
                       g_ptr_array_index (added, 0));
      }
Florian Müllner's avatar
Florian Müllner committed
461 462 463 464
      break;
    case TP_CHANNEL_GROUP_CHANGE_REASON_OFFLINE:
      for (i = 0; i < removed->len; i++)
        g_signal_emit (user_data, signals[MEMBER_DISCONNECTED], 0,
465
                       g_ptr_array_index (removed, i), message);
Florian Müllner's avatar
Florian Müllner committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
      break;
    case TP_CHANNEL_GROUP_CHANGE_REASON_KICKED:
      for (i = 0; i < removed->len; i++)
        g_signal_emit (user_data, signals[MEMBER_KICKED], 0,
                       g_ptr_array_index (removed, i), actor);
      break;
    case TP_CHANNEL_GROUP_CHANGE_REASON_BANNED:
      for (i = 0; i < removed->len; i++)
        g_signal_emit (user_data, signals[MEMBER_BANNED], 0,
                       g_ptr_array_index (removed, i), actor);
      break;
    case TP_CHANNEL_GROUP_CHANGE_REASON_NONE:
      for (i = 0; i < removed->len; i++)
        g_signal_emit (user_data, signals[MEMBER_LEFT], 0,
                       g_ptr_array_index (removed, i), message);
      for (i = 0; i < added->len; i++)
        g_signal_emit (user_data, signals[MEMBER_JOINED], 0,
                       g_ptr_array_index (added, i));
      break;
485 486 487 488 489 490 491 492 493
    case TP_CHANNEL_GROUP_CHANGE_REASON_BUSY:
    case TP_CHANNEL_GROUP_CHANGE_REASON_ERROR:
    case TP_CHANNEL_GROUP_CHANGE_REASON_INVALID_CONTACT:
    case TP_CHANNEL_GROUP_CHANGE_REASON_INVITED:
    case TP_CHANNEL_GROUP_CHANGE_REASON_NO_ANSWER:
    case TP_CHANNEL_GROUP_CHANGE_REASON_PERMISSION_DENIED:
    case TP_CHANNEL_GROUP_CHANGE_REASON_SEPARATED:
    default:
      break; /* no special handling */
Florian Müllner's avatar
Florian Müllner committed
494
    }
495 496

  g_signal_emit (user_data, signals[MEMBERS_CHANGED], 0);
Florian Müllner's avatar
Florian Müllner committed
497 498
}

499
static void
Florian Müllner's avatar
Florian Müllner committed
500
on_message_sent (TpTextChannel      *channel G_GNUC_UNUSED,
501
                 TpSignalledMessage *message,
Florian Müllner's avatar
Florian Müllner committed
502 503
                 guint               flags G_GNUC_UNUSED,
                 char               *token G_GNUC_UNUSED,
504 505 506 507
                 gpointer            user_data)
{
  PolariRoom *room = user_data;
  PolariRoomPrivate *priv = room->priv;
508
  g_autofree char *command = NULL, *username = NULL, *password = NULL, *text = NULL;
509 510 511 512 513 514

  if (priv->type != TP_HANDLE_TYPE_CONTACT)
    return;

  text = tp_message_to_text (TP_MESSAGE (message), NULL);

515
  if (polari_util_match_identify_message (text, &command, &username, &password))
516
    {
517
      if (!priv->ignore_identify)
518
        g_signal_emit (room, signals[IDENTIFY_SENT], 0, command, username, password);
519 520

      priv->ignore_identify = FALSE;
521 522 523
    }
}

524
static void
Florian Müllner's avatar
Florian Müllner committed
525 526 527 528
on_channel_invalidated (TpProxy  *channel G_GNUC_UNUSED,
                        guint     domain G_GNUC_UNUSED,
                        int       code G_GNUC_UNUSED,
                        char     *message G_GNUC_UNUSED,
529 530 531 532 533
                        gpointer  user_data)
{
  polari_room_set_channel (POLARI_ROOM (user_data), NULL);
}

Florian Müllner's avatar
Florian Müllner committed
534 535 536 537 538
static void
update_subject (PolariRoom *room,
                GHashTable *properties)
{
  PolariRoomPrivate *priv = room->priv;
539
  const char *raw_subject;
540
  g_autofree char *subject = NULL;
Florian Müllner's avatar
Florian Müllner committed
541

542 543 544
  raw_subject = tp_asv_get_string (properties, "Subject");

  if (raw_subject == NULL)
Florian Müllner's avatar
Florian Müllner committed
545 546
    return;

547 548
  subject = strip_color_codes (raw_subject);
  if (g_strcmp0 (priv->topic, subject) == 0)
549
    return;
550

Florian Müllner's avatar
Florian Müllner committed
551
  g_free (priv->topic);
552
  priv->topic = g_steal_pointer (&subject);
Florian Müllner's avatar
Florian Müllner committed
553 554 555 556 557

  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_TOPIC]);
}

static void
Florian Müllner's avatar
Florian Müllner committed
558 559
subject_get_all (TpProxy      *proxy G_GNUC_UNUSED,
                 GHashTable   *properties G_GNUC_UNUSED,
560 561
                 const GError *error,
                 gpointer      user_data,
Florian Müllner's avatar
Florian Müllner committed
562
                 GObject      *object G_GNUC_UNUSED)
Florian Müllner's avatar
Florian Müllner committed
563 564 565 566 567 568 569 570
{
  if (error)
    return;

  update_subject (POLARI_ROOM (user_data), properties);
}

static void
Florian Müllner's avatar
Florian Müllner committed
571
properties_changed (TpProxy     *proxy G_GNUC_UNUSED,
572 573
                    const char  *iface_name,
                    GHashTable  *changed,
Florian Müllner's avatar
Florian Müllner committed
574
                    const char **invalidated G_GNUC_UNUSED,
575
                    gpointer     data,
Florian Müllner's avatar
Florian Müllner committed
576
                    GObject    *weak_ref G_GNUC_UNUSED)
Florian Müllner's avatar
Florian Müllner committed
577 578 579 580 581 582 583
{
  if (strcmp (iface_name, TP_IFACE_CHANNEL_INTERFACE_SUBJECT) != 0)
    return;

  update_subject (POLARI_ROOM (data), changed);
}

584 585
static void
on_contact_info_ready (GObject      *source,
Florian Müllner's avatar
Florian Müllner committed
586
                       GAsyncResult *res G_GNUC_UNUSED,
587 588 589 590
                       gpointer      data)
{
  PolariRoom *room = data;
  PolariRoomPrivate *priv = room->priv;
Florian Müllner's avatar
Florian Müllner committed
591 592
  g_autolist (TpContactInfoField) infos = NULL;
  GList *l;
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612

  infos = tp_contact_dup_contact_info (TP_CONTACT (source));
  for (l = infos; l; l = l->next)
    {
      TpContactInfoField *f = l->data;

      if (strcmp (f->field_name, "fn") != 0)
        continue;

      if (f->field_value && *f->field_value)
        {
          g_free (priv->topic);
          priv->topic = g_strdup (*f->field_value);

          g_object_notify_by_pspec (G_OBJECT (room), props[PROP_TOPIC]);
        }
      break;
    }
}

613 614 615 616 617 618 619
static void
polari_room_set_account (PolariRoom *room,
                         TpAccount  *account)
{
  PolariRoomPrivate *priv;

  g_return_if_fail (POLARI_IS_ROOM (room));
620
  g_return_if_fail (TP_IS_ACCOUNT (account));
621 622 623

  priv = room->priv;

Florian Müllner's avatar
Florian Müllner committed
624 625
  if (g_set_object (&priv->account, account))
    g_object_notify_by_pspec (G_OBJECT (room), props[PROP_ACCOUNT]);
626

627
  update_self_user (room);
628
  update_self_nick (room);
629 630 631 632
}

static void
polari_room_set_type (PolariRoom *room,
633
                      guint       type)
634 635 636 637 638 639 640 641 642 643 644 645
{
  PolariRoomPrivate *priv;

  g_return_if_fail (POLARI_IS_ROOM (room));

  priv = room->priv;

  if (priv->type == type)
    return;

  priv->type = type;

646 647
  g_object_freeze_notify (G_OBJECT (room));

648
  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_TYPE]);
649 650 651
  update_icon (room);

  g_object_thaw_notify (G_OBJECT (room));
652 653 654 655 656 657 658 659 660 661 662 663
}

static void
polari_room_set_channel_name (PolariRoom *room,
                              const char *channel_name)
{
  PolariRoomPrivate *priv;

  g_return_if_fail (POLARI_IS_ROOM (room));

  priv = room->priv;

664
  g_free (priv->channel_name);
665

Florian Müllner's avatar
Florian Müllner committed
666
  if (channel_name)
667 668 669 670 671 672 673 674 675 676 677 678
    {
      /* Tp enforces lower-case for all channel names[0], so we need to either
       * convert the name to lower-case once here or each time we compare it to
       * a channel identifier in check_channel(); case isn't relevant for the
       * display name on the other hand, so we can use the original string which
       * matches what the user requested.
       *
       * [0] http://cgit.freedesktop.org/telepathy/telepathy-idle/tree/src/idle-handles.c#n158
       */
      priv->channel_name = g_utf8_strdown (channel_name, -1);
      set_display_name (room, channel_name + (channel_name[0] == '#' ? 1 : 0));
    }
Florian Müllner's avatar
Florian Müllner committed
679
  else
680 681 682 683
    {
      priv->channel_name = NULL;
      set_display_name (room, NULL);
    }
684

685 686 687
  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_CHANNEL_NAME]);
}

688 689 690 691 692 693 694 695 696 697 698
static gboolean
check_channel (PolariRoom *room,
               TpChannel  *channel)
{
  PolariRoomPrivate *priv = room->priv;
  TpAccount *account;

  g_return_val_if_fail (priv->account != NULL && priv->channel_name != NULL, FALSE);

  account = tp_connection_get_account (tp_channel_get_connection (channel));
  return account == priv->account &&
699
         strcmp (tp_channel_get_identifier (channel), priv->channel_name) == 0;
700 701
}

Florian Müllner's avatar
Florian Müllner committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
static void
polari_room_set_channel (PolariRoom *room,
                         TpChannel  *channel)
{
  PolariRoomPrivate *priv;

  g_return_if_fail (POLARI_IS_ROOM (room));
  g_return_if_fail (channel == NULL || TP_IS_TEXT_CHANNEL (channel));

  priv = room->priv;

  if (priv->channel == channel)
    return;

  if (priv->channel)
    {
718
      g_signal_handlers_disconnect_by_data (priv->channel, room);
719 720
      g_signal_handler_disconnect (tp_channel_get_connection (priv->channel),
                                   priv->self_contact_notify_id);
Florian Müllner's avatar
Florian Müllner committed
721 722 723

      tp_proxy_signal_connection_disconnect (priv->properties_changed_id);

Florian Müllner's avatar
Florian Müllner committed
724 725 726
      g_clear_object (&priv->channel);
    }

727
  if (channel && check_channel (room, channel))
Florian Müllner's avatar
Florian Müllner committed
728
    {
729 730
      TpContact *target = tp_channel_get_target_contact (channel);

Florian Müllner's avatar
Florian Müllner committed
731 732
      priv->channel = g_object_ref (channel);

733 734 735 736 737 738 739 740 741
      /* If we have a target contact, the chat is private; assume that this
       * is mutually exclusive with subject/topic support and look for the
       * contact's full name as topic.
       */
      if (target)
        tp_contact_request_contact_info_async (target, NULL,
                                               on_contact_info_ready, room);
      else
        tp_cli_dbus_properties_call_get_all (channel, -1,
Florian Müllner's avatar
Florian Müllner committed
742
                                     TP_IFACE_CHANNEL_INTERFACE_SUBJECT,
743
                                     subject_get_all, room, NULL, NULL);
Florian Müllner's avatar
Florian Müllner committed
744 745


746 747 748 749
      priv->self_contact_notify_id =
        g_signal_connect (tp_channel_get_connection (channel),
                          "notify::self-contact",
                          G_CALLBACK (on_self_contact_notify), room);
750 751 752 753 754 755 756 757
      g_object_connect (channel,
                        "signal::group-contacts-changed",
                        G_CALLBACK (on_group_contacts_changed), room,
                        "signal::message-sent",
                        G_CALLBACK (on_message_sent), room,
                        "signal::invalidated",
			G_CALLBACK (on_channel_invalidated), room,
                        NULL);
Florian Müllner's avatar
Florian Müllner committed
758 759
      priv->properties_changed_id =
        tp_cli_dbus_properties_connect_to_properties_changed (
760
                                 channel, properties_changed,
Florian Müllner's avatar
Florian Müllner committed
761
                                 room, NULL, NULL, NULL);
Florian Müllner's avatar
Florian Müllner committed
762 763
    }

Florian Müllner's avatar
Florian Müllner committed
764
  g_object_freeze_notify (G_OBJECT (room));
Florian Müllner's avatar
Florian Müllner committed
765

Florian Müllner's avatar
Florian Müllner committed
766
  update_self_nick (room);
Florian Müllner's avatar
Florian Müllner committed
767

Florian Müllner's avatar
Florian Müllner committed
768
  g_object_notify_by_pspec (G_OBJECT (room), props[PROP_CHANNEL]);
Florian Müllner's avatar
Florian Müllner committed
769

Florian Müllner's avatar
Florian Müllner committed
770
  g_object_thaw_notify (G_OBJECT (room));
Florian Müllner's avatar
Florian Müllner committed
771 772 773 774 775 776 777 778 779 780 781 782 783
}

static void
polari_room_get_property (GObject    *object,
                          guint       prop_id,
                          GValue     *value,
                          GParamSpec *pspec)
{
  PolariRoomPrivate *priv = POLARI_ROOM(object)->priv;

  switch (prop_id)
    {
    case PROP_ID:
784 785 786 787
      g_value_take_string (value,
                           polari_create_room_id (priv->account,
                                                  priv->channel_name,
                                                  priv->type));
Florian Müllner's avatar
Florian Müllner committed
788 789 790 791
      break;
    case PROP_ICON:
      g_value_set_object (value, priv->icon);
      break;
792 793 794 795
    case PROP_ACCOUNT:
      g_value_set_object (value, priv->account);
      break;
    case PROP_TYPE:
796
      g_value_set_uint (value, priv->type);
797
      break;
798 799 800
    case PROP_CHANNEL_ERROR:
      g_value_set_string (value, priv->channel_error);
      break;
801 802 803
    case PROP_CHANNEL_NAME:
      g_value_set_string (value, priv->channel_name);
      break;
Florian Müllner's avatar
Florian Müllner committed
804 805 806 807 808 809
    case PROP_CHANNEL:
      g_value_set_object (value, priv->channel);
      break;
    case PROP_DISPLAY_NAME:
      g_value_set_string (value, priv->display_name);
      break;
Florian Müllner's avatar
Florian Müllner committed
810 811 812
    case PROP_TOPIC:
      g_value_set_string (value, priv->topic);
      break;
Florian Müllner's avatar
Florian Müllner committed
813 814 815 816 817 818 819 820 821 822 823
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
polari_room_set_property (GObject      *object,
                          guint         prop_id,
                          const GValue *value,
                          GParamSpec   *pspec)
{
Florian Müllner's avatar
Florian Müllner committed
824 825
  PolariRoom *room;

Florian Müllner's avatar
Florian Müllner committed
826 827
  g_return_if_fail (POLARI_IS_ROOM (object));
  g_return_if_fail (G_IS_OBJECT (object));
Florian Müllner's avatar
Florian Müllner committed
828 829

  room = POLARI_ROOM(object);
Florian Müllner's avatar
Florian Müllner committed
830 831 832

  switch (prop_id)
    {
833 834 835 836
    case PROP_ACCOUNT:
      polari_room_set_account (room, g_value_get_object (value));
      break;
    case PROP_TYPE:
837
      polari_room_set_type (room, g_value_get_uint (value));
838
      break;
839 840 841
    case PROP_CHANNEL_ERROR:
      polari_room_set_channel_error (room, g_value_get_string (value));
      break;
842 843 844
    case PROP_CHANNEL_NAME:
      polari_room_set_channel_name (room, g_value_get_string (value));
      break;
Florian Müllner's avatar
Florian Müllner committed
845 846 847 848 849 850 851 852 853 854 855
    case PROP_CHANNEL:
      polari_room_set_channel (room, g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
polari_room_dispose (GObject *object)
{
856 857
  PolariRoomPrivate *priv = POLARI_ROOM (object)->priv;

Florian Müllner's avatar
Florian Müllner committed
858
  polari_room_set_channel (POLARI_ROOM (object), NULL);
859
  g_clear_object (&priv->account);
Florian Müllner's avatar
Florian Müllner committed
860 861 862 863 864 865 866 867
  G_OBJECT_CLASS (polari_room_parent_class)->dispose (object);
}

static void
polari_room_finalize (GObject *object)
{
  PolariRoomPrivate *priv = POLARI_ROOM (object)->priv;

868
  g_clear_pointer (&priv->channel_name, g_free);
Florian Müllner's avatar
Florian Müllner committed
869
  g_clear_pointer (&priv->display_name, g_free);
Florian Müllner's avatar
Florian Müllner committed
870
  g_clear_pointer (&priv->self_nick, g_free);
871
  g_clear_pointer (&priv->self_user, g_free);
Florian Müllner's avatar
Florian Müllner committed
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890

  G_OBJECT_CLASS (polari_room_parent_class)->finalize (object);
}

static void
polari_room_class_init (PolariRoomClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = polari_room_get_property;
  object_class->set_property = polari_room_set_property;
  object_class->dispose = polari_room_dispose;
  object_class->finalize = polari_room_finalize;

  props[PROP_ID] =
    g_param_spec_string ("id",
                         "Id",
                         "Room identifier",
                         NULL,
891
                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
Florian Müllner's avatar
Florian Müllner committed
892 893 894 895 896 897

  props[PROP_DISPLAY_NAME] =
    g_param_spec_string ("display-name",
                         "Display name",
                         "Display name",
                         NULL,
898
                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
Florian Müllner's avatar
Florian Müllner committed
899

Florian Müllner's avatar
Florian Müllner committed
900 901 902 903 904
  props[PROP_TOPIC] =
    g_param_spec_string ("topic",
                         "Topic",
                         "Topic",
                         NULL,
905
                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
Florian Müllner's avatar
Florian Müllner committed
906

Florian Müllner's avatar
Florian Müllner committed
907 908 909 910 911
  props[PROP_ICON] =
    g_param_spec_object ("icon",
                         "Icon",
                         "Icon",
                         G_TYPE_ICON,
912
                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY);
Florian Müllner's avatar
Florian Müllner committed
913

914 915 916 917 918
  props[PROP_ACCOUNT] =
    g_param_spec_object ("account",
                         "Account",
                         "Account",
                         TP_TYPE_ACCOUNT,
919
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
920 921

  props[PROP_TYPE] =
922
    g_param_spec_uint ("type",
923 924 925 926 927
                       "Type",
                       "Type",
                       TP_HANDLE_TYPE_NONE,
                       TP_HANDLE_TYPE_GROUP,
                       TP_HANDLE_TYPE_ROOM,
928
                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
929

930 931 932 933 934 935 936
  props[PROP_CHANNEL_ERROR] =
    g_param_spec_string ("channel-error",
                         "Channel error",
                         "Channel error",
                         NULL,
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);

937 938 939 940 941
  props[PROP_CHANNEL_NAME] =
    g_param_spec_string ("channel-name",
                         "Channel name",
                         "Channel name",
                         NULL,
942
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
943

Florian Müllner's avatar
Florian Müllner committed
944 945 946 947 948
  props[PROP_CHANNEL] =
    g_param_spec_object ("channel",
                         "Channel",
                         "Channel",
                         TP_TYPE_CHANNEL,
949
                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
Florian Müllner's avatar
Florian Müllner committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

  g_object_class_install_properties (object_class, LAST_PROP, props);

  signals[MEMBER_JOINED] =
    g_signal_new ("member-joined",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1, TP_TYPE_CONTACT);

  signals[MEMBER_LEFT] =
    g_signal_new ("member-left",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, TP_TYPE_CONTACT, G_TYPE_STRING);

  signals[MEMBER_DISCONNECTED] =
    g_signal_new ("member-disconnected",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
975
                  G_TYPE_NONE, 2, TP_TYPE_CONTACT, G_TYPE_STRING);
Florian Müllner's avatar
Florian Müllner committed
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999

  signals[MEMBER_RENAMED] =
    g_signal_new ("member-renamed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, TP_TYPE_CONTACT, TP_TYPE_CONTACT);

  signals[MEMBER_KICKED] =
    g_signal_new ("member-kicked",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, TP_TYPE_CONTACT, TP_TYPE_CONTACT);

  signals[MEMBER_BANNED] =
    g_signal_new ("member-banned",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 2, TP_TYPE_CONTACT, TP_TYPE_CONTACT);
1000 1001 1002 1003 1004 1005 1006 1007

  signals[MEMBERS_CHANGED] =
    g_signal_new ("members-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);
1008 1009 1010 1011 1012 1013 1014

  signals[IDENTIFY_SENT] =
    g_signal_new ("identify-sent",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  0,
                  NULL, NULL, NULL,
1015
                  G_TYPE_NONE, 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
Florian Müllner's avatar
Florian Müllner committed
1016 1017 1018 1019 1020 1021 1022
}

static void
polari_room_init (PolariRoom *room)
{
  room->priv = polari_room_get_instance_private (room);
}